Summary: In the persistent version of Yoga, a YogaNode can be shared between two YogaTrees, that means that a YogaNode could have more than one Parent at one point in time. That's why the concept of Parent of a YogaNode is not a 1-1 relationship anymore. This diff changes the semantic of Parent of a YogaNode to Owner of a Yoga Node. CC sebmarkbage and priteshrnandgaonkar for more context. Technically this diff renames the field YogaNode.parent to YogaNode.owner (and every internal field, Getter and Setter that is related to parent) Note that as part of this diff I also modified the CSSLayoutDEPRECATED version of Yoga in order to keep compatibility with the C++ implementation. Reviewed By: priteshrnandgaonkar Differential Revision: D7352778 fbshipit-source-id: dcf1af5e72bfc3063b5c4bda197d7952a9194768
112 lines
3.6 KiB
C++
112 lines
3.6 KiB
C++
/**
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include <gtest/gtest.h>
|
|
#include <yoga/Yoga.h>
|
|
|
|
static std::vector<YGNodeRef> getChildren(YGNodeRef const node)
|
|
{
|
|
const uint32_t count = YGNodeGetChildCount(node);
|
|
std::vector<YGNodeRef> children;
|
|
children.reserve(count);
|
|
for (uint32_t i = 0 ; i < count ; i++) {
|
|
children.push_back(YGNodeGetChild(node, i));
|
|
}
|
|
return children;
|
|
}
|
|
|
|
TEST(YogaTest, set_children_adds_children_to_parent) {
|
|
YGNodeRef const root = YGNodeNew();
|
|
YGNodeRef const root_child0 = YGNodeNew();
|
|
YGNodeRef const root_child1 = YGNodeNew();
|
|
|
|
YGNodeSetChildren(root, {root_child0, root_child1});
|
|
|
|
const std::vector<YGNodeRef> children = getChildren(root);
|
|
const std::vector<YGNodeRef> expectedChildren = {root_child0, root_child1};
|
|
ASSERT_EQ(children, expectedChildren);
|
|
|
|
const std::vector<YGNodeRef> owners = {YGNodeGetOwner(root_child0), YGNodeGetOwner(root_child1)};
|
|
const std::vector<YGNodeRef> expectedOwners = {root, root};
|
|
ASSERT_EQ(owners, expectedOwners);
|
|
|
|
YGNodeFreeRecursive(root);
|
|
}
|
|
|
|
TEST(YogaTest, set_children_to_empty_removes_old_children) {
|
|
YGNodeRef const root = YGNodeNew();
|
|
YGNodeRef const root_child0 = YGNodeNew();
|
|
YGNodeRef const root_child1 = YGNodeNew();
|
|
|
|
YGNodeSetChildren(root, {root_child0, root_child1});
|
|
YGNodeSetChildren(root, {});
|
|
|
|
const std::vector<YGNodeRef> children = getChildren(root);
|
|
const std::vector<YGNodeRef> expectedChildren = {};
|
|
ASSERT_EQ(children, expectedChildren);
|
|
|
|
const std::vector<YGNodeRef> owners = {YGNodeGetOwner(root_child0), YGNodeGetOwner(root_child1)};
|
|
const std::vector<YGNodeRef> expectedOwners = {nullptr, nullptr};
|
|
ASSERT_EQ(owners, expectedOwners);
|
|
|
|
YGNodeFreeRecursive(root);
|
|
}
|
|
|
|
TEST(YogaTest, set_children_replaces_non_common_children) {
|
|
YGNodeRef const root = YGNodeNew();
|
|
YGNodeRef const root_child0 = YGNodeNew();
|
|
YGNodeRef const root_child1 = YGNodeNew();
|
|
|
|
YGNodeSetChildren(root, {root_child0, root_child1});
|
|
|
|
YGNodeRef const root_child2 = YGNodeNew();
|
|
YGNodeRef const root_child3 = YGNodeNew();
|
|
|
|
YGNodeSetChildren(root, {root_child2, root_child3});
|
|
|
|
const std::vector<YGNodeRef> children = getChildren(root);
|
|
const std::vector<YGNodeRef> expectedChildren = {root_child2, root_child3};
|
|
ASSERT_EQ(children, expectedChildren);
|
|
|
|
const std::vector<YGNodeRef> owners = {YGNodeGetOwner(root_child0), YGNodeGetOwner(root_child1)};
|
|
const std::vector<YGNodeRef> expectedOwners = {nullptr, nullptr};
|
|
ASSERT_EQ(owners, expectedOwners);
|
|
|
|
YGNodeFreeRecursive(root);
|
|
YGNodeFree(root_child0);
|
|
YGNodeFree(root_child1);
|
|
}
|
|
|
|
TEST(YogaTest, set_children_keeps_and_reorders_common_children) {
|
|
YGNodeRef const root = YGNodeNew();
|
|
YGNodeRef const root_child0 = YGNodeNew();
|
|
YGNodeRef const root_child1 = YGNodeNew();
|
|
YGNodeRef const root_child2 = YGNodeNew();
|
|
|
|
YGNodeSetChildren(root, {root_child0, root_child1, root_child2});
|
|
|
|
YGNodeRef const root_child3 = YGNodeNew();
|
|
|
|
YGNodeSetChildren(root, {root_child2, root_child1, root_child3});
|
|
|
|
const std::vector<YGNodeRef> children = getChildren(root);
|
|
const std::vector<YGNodeRef> expectedChildren = {root_child2, root_child1, root_child3};
|
|
ASSERT_EQ(children, expectedChildren);
|
|
|
|
const std::vector<YGNodeRef> owners = {
|
|
YGNodeGetOwner(root_child0),
|
|
YGNodeGetOwner(root_child1),
|
|
YGNodeGetOwner(root_child2),
|
|
YGNodeGetOwner(root_child3)
|
|
};
|
|
const std::vector<YGNodeRef> expectedOwners = {nullptr, root, root, root};
|
|
ASSERT_EQ(owners, expectedOwners);
|
|
|
|
YGNodeFreeRecursive(root);
|
|
YGNodeFree(root_child0);
|
|
}
|