forked from facebook/yoga
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathYGDirtiedTest.cpp
109 lines (84 loc) · 3.11 KB
/
YGDirtiedTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include <gtest/gtest.h>
#include <yoga/YGNode.h>
static void _dirtied(YGNodeRef node) {
int* dirtiedCount = (int*)node->getContext();
(*dirtiedCount)++;
}
TEST(YogaTest, dirtied) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
int dirtiedCount = 0;
root->setContext(&dirtiedCount);
root->setDirtiedFunc(_dirtied);
ASSERT_EQ(0, dirtiedCount);
// `_dirtied` MUST be called in case of explicit dirtying.
root->setDirty(true);
ASSERT_EQ(1, dirtiedCount);
// `_dirtied` MUST be called ONCE.
root->setDirty(true);
ASSERT_EQ(1, dirtiedCount);
}
TEST(YogaTest, dirtied_propagation) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 50);
YGNodeStyleSetHeight(root_child0, 20);
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNew();
YGNodeStyleSetWidth(root_child1, 50);
YGNodeStyleSetHeight(root_child1, 20);
YGNodeInsertChild(root, root_child1, 1);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
int dirtiedCount = 0;
root->setContext(&dirtiedCount);
root->setDirtiedFunc(_dirtied);
ASSERT_EQ(0, dirtiedCount);
// `_dirtied` MUST be called for the first time.
root_child0->markDirtyAndPropogate();
ASSERT_EQ(1, dirtiedCount);
// `_dirtied` must NOT be called for the second time.
root_child0->markDirtyAndPropogate();
ASSERT_EQ(1, dirtiedCount);
}
TEST(YogaTest, dirtied_hierarchy) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 50);
YGNodeStyleSetHeight(root_child0, 20);
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNew();
YGNodeStyleSetWidth(root_child1, 50);
YGNodeStyleSetHeight(root_child1, 20);
YGNodeInsertChild(root, root_child1, 1);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
int dirtiedCount = 0;
root_child0->setContext(&dirtiedCount);
root_child0->setDirtiedFunc(_dirtied);
ASSERT_EQ(0, dirtiedCount);
// `_dirtied` must NOT be called for descendants.
root->markDirtyAndPropogate();
ASSERT_EQ(0, dirtiedCount);
// `_dirtied` must NOT be called for the sibling node.
root_child1->markDirtyAndPropogate();
ASSERT_EQ(0, dirtiedCount);
// `_dirtied` MUST be called in case of explicit dirtying.
root_child0->markDirtyAndPropogate();
ASSERT_EQ(1, dirtiedCount);
}