2 * Copyright (c) 2014-present, Facebook, Inc.
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
8 #include <gtest/gtest.h>
11 static std::vector<YGNodeRef> getChildren(YGNodeRef const node)
13 const uint32_t count = YGNodeGetChildCount(node);
14 std::vector<YGNodeRef> children;
15 children.reserve(count);
16 for (uint32_t i = 0 ; i < count ; i++) {
17 children.push_back(YGNodeGetChild(node, i));
22 TEST(YogaTest, set_children_adds_children_to_parent) {
23 YGNodeRef const root = YGNodeNew();
24 YGNodeRef const root_child0 = YGNodeNew();
25 YGNodeRef const root_child1 = YGNodeNew();
27 YGNodeSetChildren(root, {root_child0, root_child1});
29 const std::vector<YGNodeRef> children = getChildren(root);
30 const std::vector<YGNodeRef> expectedChildren = {root_child0, root_child1};
31 ASSERT_EQ(children, expectedChildren);
33 const std::vector<YGNodeRef> owners = {YGNodeGetOwner(root_child0), YGNodeGetOwner(root_child1)};
34 const std::vector<YGNodeRef> expectedOwners = {root, root};
35 ASSERT_EQ(owners, expectedOwners);
37 YGNodeFreeRecursive(root);
40 TEST(YogaTest, set_children_to_empty_removes_old_children) {
41 YGNodeRef const root = YGNodeNew();
42 YGNodeRef const root_child0 = YGNodeNew();
43 YGNodeRef const root_child1 = YGNodeNew();
45 YGNodeSetChildren(root, {root_child0, root_child1});
46 YGNodeSetChildren(root, {});
48 const std::vector<YGNodeRef> children = getChildren(root);
49 const std::vector<YGNodeRef> expectedChildren = {};
50 ASSERT_EQ(children, expectedChildren);
52 const std::vector<YGNodeRef> owners = {YGNodeGetOwner(root_child0), YGNodeGetOwner(root_child1)};
53 const std::vector<YGNodeRef> expectedOwners = {nullptr, nullptr};
54 ASSERT_EQ(owners, expectedOwners);
56 YGNodeFreeRecursive(root);
59 TEST(YogaTest, set_children_replaces_non_common_children) {
60 YGNodeRef const root = YGNodeNew();
61 YGNodeRef const root_child0 = YGNodeNew();
62 YGNodeRef const root_child1 = YGNodeNew();
64 YGNodeSetChildren(root, {root_child0, root_child1});
66 YGNodeRef const root_child2 = YGNodeNew();
67 YGNodeRef const root_child3 = YGNodeNew();
69 YGNodeSetChildren(root, {root_child2, root_child3});
71 const std::vector<YGNodeRef> children = getChildren(root);
72 const std::vector<YGNodeRef> expectedChildren = {root_child2, root_child3};
73 ASSERT_EQ(children, expectedChildren);
75 const std::vector<YGNodeRef> owners = {YGNodeGetOwner(root_child0), YGNodeGetOwner(root_child1)};
76 const std::vector<YGNodeRef> expectedOwners = {nullptr, nullptr};
77 ASSERT_EQ(owners, expectedOwners);
79 YGNodeFreeRecursive(root);
80 YGNodeFree(root_child0);
81 YGNodeFree(root_child1);
84 TEST(YogaTest, set_children_keeps_and_reorders_common_children) {
85 YGNodeRef const root = YGNodeNew();
86 YGNodeRef const root_child0 = YGNodeNew();
87 YGNodeRef const root_child1 = YGNodeNew();
88 YGNodeRef const root_child2 = YGNodeNew();
90 YGNodeSetChildren(root, {root_child0, root_child1, root_child2});
92 YGNodeRef const root_child3 = YGNodeNew();
94 YGNodeSetChildren(root, {root_child2, root_child1, root_child3});
96 const std::vector<YGNodeRef> children = getChildren(root);
97 const std::vector<YGNodeRef> expectedChildren = {root_child2, root_child1, root_child3};
98 ASSERT_EQ(children, expectedChildren);
100 const std::vector<YGNodeRef> owners = {
101 YGNodeGetOwner(root_child0),
102 YGNodeGetOwner(root_child1),
103 YGNodeGetOwner(root_child2),
104 YGNodeGetOwner(root_child3)
106 const std::vector<YGNodeRef> expectedOwners = {nullptr, root, root, root};
107 ASSERT_EQ(owners, expectedOwners);
109 YGNodeFreeRecursive(root);
110 YGNodeFree(root_child0);