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>
9 #include <yoga/YGNode.h>
11 static void _dirtied(YGNodeRef node) {
12 int* dirtiedCount = (int*)node->getContext();
16 TEST(YogaTest, dirtied) {
17 const YGNodeRef root = YGNodeNew();
18 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
19 YGNodeStyleSetWidth(root, 100);
20 YGNodeStyleSetHeight(root, 100);
22 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
25 root->setContext(&dirtiedCount);
26 root->setDirtiedFunc(_dirtied);
28 ASSERT_EQ(0, dirtiedCount);
30 // `_dirtied` MUST be called in case of explicit dirtying.
32 ASSERT_EQ(1, dirtiedCount);
34 // `_dirtied` MUST be called ONCE.
36 ASSERT_EQ(1, dirtiedCount);
39 TEST(YogaTest, dirtied_propagation) {
40 const YGNodeRef root = YGNodeNew();
41 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
42 YGNodeStyleSetWidth(root, 100);
43 YGNodeStyleSetHeight(root, 100);
45 const YGNodeRef root_child0 = YGNodeNew();
46 YGNodeStyleSetWidth(root_child0, 50);
47 YGNodeStyleSetHeight(root_child0, 20);
48 YGNodeInsertChild(root, root_child0, 0);
50 const YGNodeRef root_child1 = YGNodeNew();
51 YGNodeStyleSetWidth(root_child1, 50);
52 YGNodeStyleSetHeight(root_child1, 20);
53 YGNodeInsertChild(root, root_child1, 1);
55 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
58 root->setContext(&dirtiedCount);
59 root->setDirtiedFunc(_dirtied);
61 ASSERT_EQ(0, dirtiedCount);
63 // `_dirtied` MUST be called for the first time.
64 root_child0->markDirtyAndPropogate();
65 ASSERT_EQ(1, dirtiedCount);
67 // `_dirtied` must NOT be called for the second time.
68 root_child0->markDirtyAndPropogate();
69 ASSERT_EQ(1, dirtiedCount);
72 TEST(YogaTest, dirtied_hierarchy) {
73 const YGNodeRef root = YGNodeNew();
74 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
75 YGNodeStyleSetWidth(root, 100);
76 YGNodeStyleSetHeight(root, 100);
78 const YGNodeRef root_child0 = YGNodeNew();
79 YGNodeStyleSetWidth(root_child0, 50);
80 YGNodeStyleSetHeight(root_child0, 20);
81 YGNodeInsertChild(root, root_child0, 0);
83 const YGNodeRef root_child1 = YGNodeNew();
84 YGNodeStyleSetWidth(root_child1, 50);
85 YGNodeStyleSetHeight(root_child1, 20);
86 YGNodeInsertChild(root, root_child1, 1);
88 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
91 root_child0->setContext(&dirtiedCount);
92 root_child0->setDirtiedFunc(_dirtied);
94 ASSERT_EQ(0, dirtiedCount);
96 // `_dirtied` must NOT be called for descendants.
97 root->markDirtyAndPropogate();
98 ASSERT_EQ(0, dirtiedCount);
100 // `_dirtied` must NOT be called for the sibling node.
101 root_child1->markDirtyAndPropogate();
102 ASSERT_EQ(0, dirtiedCount);
104 // `_dirtied` MUST be called in case of explicit dirtying.
105 root_child0->markDirtyAndPropogate();
106 ASSERT_EQ(1, dirtiedCount);