Merge "Add new layouting support for TextLabel and ImageView." into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-third-party / yoga / YGTreeMutationTest.cpp
1 /**
2  * Copyright (c) 2014-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7
8 #include <gtest/gtest.h>
9 #include <yoga/Yoga.h>
10
11 static std::vector<YGNodeRef> getChildren(YGNodeRef const node)
12 {
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));
18   }
19   return children;
20 }
21
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();
26
27   YGNodeSetChildren(root, {root_child0, root_child1});
28
29   const std::vector<YGNodeRef> children = getChildren(root);
30   const std::vector<YGNodeRef> expectedChildren = {root_child0, root_child1};
31   ASSERT_EQ(children, expectedChildren);
32
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);
36
37   YGNodeFreeRecursive(root);
38 }
39
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();
44
45   YGNodeSetChildren(root, {root_child0, root_child1});
46   YGNodeSetChildren(root, {});
47
48   const std::vector<YGNodeRef> children = getChildren(root);
49   const std::vector<YGNodeRef> expectedChildren = {};
50   ASSERT_EQ(children, expectedChildren);
51
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);
55
56   YGNodeFreeRecursive(root);
57 }
58
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();
63
64   YGNodeSetChildren(root, {root_child0, root_child1});
65
66   YGNodeRef const root_child2 = YGNodeNew();
67   YGNodeRef const root_child3 = YGNodeNew();
68
69   YGNodeSetChildren(root, {root_child2, root_child3});
70
71   const std::vector<YGNodeRef> children = getChildren(root);
72   const std::vector<YGNodeRef> expectedChildren = {root_child2, root_child3};
73   ASSERT_EQ(children, expectedChildren);
74
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);
78
79   YGNodeFreeRecursive(root);
80   YGNodeFree(root_child0);
81   YGNodeFree(root_child1);
82 }
83
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();
89
90   YGNodeSetChildren(root, {root_child0, root_child1, root_child2});
91
92   YGNodeRef const root_child3 = YGNodeNew();
93
94   YGNodeSetChildren(root, {root_child2, root_child1, root_child3});
95
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);
99
100   const std::vector<YGNodeRef> owners = {
101     YGNodeGetOwner(root_child0),
102     YGNodeGetOwner(root_child1),
103     YGNodeGetOwner(root_child2),
104     YGNodeGetOwner(root_child3)
105   };
106   const std::vector<YGNodeRef> expectedOwners = {nullptr, root, root, root};
107   ASSERT_EQ(owners, expectedOwners);
108
109   YGNodeFreeRecursive(root);
110   YGNodeFree(root_child0);
111 }