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 / YGDirtiedTest.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/YGNode.h>
10
11 static void _dirtied(YGNodeRef node) {
12   int* dirtiedCount = (int*)node->getContext();
13   (*dirtiedCount)++;
14 }
15
16 TEST(YogaTest, dirtied) {
17   const YGNodeRef root = YGNodeNew();
18   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
19   YGNodeStyleSetWidth(root, 100);
20   YGNodeStyleSetHeight(root, 100);
21
22   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
23
24   int dirtiedCount = 0;
25   root->setContext(&dirtiedCount);
26   root->setDirtiedFunc(_dirtied);
27
28   ASSERT_EQ(0, dirtiedCount);
29
30   // `_dirtied` MUST be called in case of explicit dirtying.
31   root->setDirty(true);
32   ASSERT_EQ(1, dirtiedCount);
33
34   // `_dirtied` MUST be called ONCE.
35   root->setDirty(true);
36   ASSERT_EQ(1, dirtiedCount);
37 }
38
39 TEST(YogaTest, dirtied_propagation) {
40   const YGNodeRef root = YGNodeNew();
41   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
42   YGNodeStyleSetWidth(root, 100);
43   YGNodeStyleSetHeight(root, 100);
44
45   const YGNodeRef root_child0 = YGNodeNew();
46   YGNodeStyleSetWidth(root_child0, 50);
47   YGNodeStyleSetHeight(root_child0, 20);
48   YGNodeInsertChild(root, root_child0, 0);
49
50   const YGNodeRef root_child1 = YGNodeNew();
51   YGNodeStyleSetWidth(root_child1, 50);
52   YGNodeStyleSetHeight(root_child1, 20);
53   YGNodeInsertChild(root, root_child1, 1);
54
55   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
56
57   int dirtiedCount = 0;
58   root->setContext(&dirtiedCount);
59   root->setDirtiedFunc(_dirtied);
60
61   ASSERT_EQ(0, dirtiedCount);
62
63   // `_dirtied` MUST be called for the first time.
64   root_child0->markDirtyAndPropogate();
65   ASSERT_EQ(1, dirtiedCount);
66
67   // `_dirtied` must NOT be called for the second time.
68   root_child0->markDirtyAndPropogate();
69   ASSERT_EQ(1, dirtiedCount);
70 }
71
72 TEST(YogaTest, dirtied_hierarchy) {
73   const YGNodeRef root = YGNodeNew();
74   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
75   YGNodeStyleSetWidth(root, 100);
76   YGNodeStyleSetHeight(root, 100);
77
78   const YGNodeRef root_child0 = YGNodeNew();
79   YGNodeStyleSetWidth(root_child0, 50);
80   YGNodeStyleSetHeight(root_child0, 20);
81   YGNodeInsertChild(root, root_child0, 0);
82
83   const YGNodeRef root_child1 = YGNodeNew();
84   YGNodeStyleSetWidth(root_child1, 50);
85   YGNodeStyleSetHeight(root_child1, 20);
86   YGNodeInsertChild(root, root_child1, 1);
87
88   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
89
90   int dirtiedCount = 0;
91   root_child0->setContext(&dirtiedCount);
92   root_child0->setDirtiedFunc(_dirtied);
93
94   ASSERT_EQ(0, dirtiedCount);
95
96   // `_dirtied` must NOT be called for descendants.
97   root->markDirtyAndPropogate();
98   ASSERT_EQ(0, dirtiedCount);
99
100   // `_dirtied` must NOT be called for the sibling node.
101   root_child1->markDirtyAndPropogate();
102   ASSERT_EQ(0, dirtiedCount);
103
104   // `_dirtied` MUST be called in case of explicit dirtying.
105   root_child0->markDirtyAndPropogate();
106   ASSERT_EQ(1, dirtiedCount);
107 }