forked from facebook/yoga
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathYGPersistenceTest.cpp
252 lines (189 loc) · 8.89 KB
/
YGPersistenceTest.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* 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.
*/
// @Generated by gentest/gentest.rb from gentest/fixtures/YGPercentageTest.html
#include <gtest/gtest.h>
#include <yoga/Yoga.h>
TEST(YogaTest, cloning_shared_root) {
const YGConfigRef config = YGConfigNew();
const YGNodeRef root = YGNodeNewWithConfig(config);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexGrow(root_child0, 1);
YGNodeStyleSetFlexBasis(root_child0, 50);
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexGrow(root_child1, 1);
YGNodeInsertChild(root, root_child1, 1);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root));
ASSERT_FLOAT_EQ(100, YGNodeLayoutGetWidth(root));
ASSERT_FLOAT_EQ(100, YGNodeLayoutGetHeight(root));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_FLOAT_EQ(100, YGNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(75, YGNodeLayoutGetHeight(root_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child1));
ASSERT_FLOAT_EQ(75, YGNodeLayoutGetTop(root_child1));
ASSERT_FLOAT_EQ(100, YGNodeLayoutGetWidth(root_child1));
ASSERT_FLOAT_EQ(25, YGNodeLayoutGetHeight(root_child1));
const YGNodeRef root2 = YGNodeClone(root);
YGNodeStyleSetWidth(root2, 100);
ASSERT_EQ(2, YGNodeGetChildCount(root2));
// The children should have referential equality at this point.
ASSERT_EQ(root_child0, YGNodeGetChild(root2, 0));
ASSERT_EQ(root_child1, YGNodeGetChild(root2, 1));
YGNodeCalculateLayout(root2, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(2, YGNodeGetChildCount(root2));
// Relayout with no changed input should result in referential equality.
ASSERT_EQ(root_child0, YGNodeGetChild(root2, 0));
ASSERT_EQ(root_child1, YGNodeGetChild(root2, 1));
YGNodeStyleSetWidth(root2, 150);
YGNodeStyleSetHeight(root2, 200);
YGNodeCalculateLayout(root2, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(2, YGNodeGetChildCount(root2));
// Relayout with changed input should result in cloned children.
const YGNodeRef root2_child0 = YGNodeGetChild(root2, 0);
const YGNodeRef root2_child1 = YGNodeGetChild(root2, 1);
ASSERT_NE(root_child0, root2_child0);
ASSERT_NE(root_child1, root2_child1);
// Everything in the root should remain unchanged.
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root));
ASSERT_FLOAT_EQ(100, YGNodeLayoutGetWidth(root));
ASSERT_FLOAT_EQ(100, YGNodeLayoutGetHeight(root));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_FLOAT_EQ(100, YGNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(75, YGNodeLayoutGetHeight(root_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child1));
ASSERT_FLOAT_EQ(75, YGNodeLayoutGetTop(root_child1));
ASSERT_FLOAT_EQ(100, YGNodeLayoutGetWidth(root_child1));
ASSERT_FLOAT_EQ(25, YGNodeLayoutGetHeight(root_child1));
// The new root now has new layout.
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root2));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root2));
ASSERT_FLOAT_EQ(150, YGNodeLayoutGetWidth(root2));
ASSERT_FLOAT_EQ(200, YGNodeLayoutGetHeight(root2));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root2_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root2_child0));
ASSERT_FLOAT_EQ(150, YGNodeLayoutGetWidth(root2_child0));
ASSERT_FLOAT_EQ(125, YGNodeLayoutGetHeight(root2_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root2_child1));
ASSERT_FLOAT_EQ(125, YGNodeLayoutGetTop(root2_child1));
ASSERT_FLOAT_EQ(150, YGNodeLayoutGetWidth(root2_child1));
ASSERT_FLOAT_EQ(75, YGNodeLayoutGetHeight(root2_child1));
YGNodeFreeRecursive(root2);
YGNodeFreeRecursive(root);
YGConfigFree(config);
}
TEST(YogaTest, mutating_children_of_a_clone_clones) {
const YGConfigRef config = YGConfigNew();
const YGNodeRef root = YGNodeNewWithConfig(config);
ASSERT_EQ(0, YGNodeGetChildCount(root));
const YGNodeRef root2 = YGNodeClone(root);
ASSERT_EQ(0, YGNodeGetChildCount(root2));
const YGNodeRef root2_child0 = YGNodeNewWithConfig(config);
YGNodeInsertChild(root2, root2_child0, 0);
ASSERT_EQ(0, YGNodeGetChildCount(root));
ASSERT_EQ(1, YGNodeGetChildCount(root2));
const YGNodeRef root3 = YGNodeClone(root2);
ASSERT_EQ(1, YGNodeGetChildCount(root2));
ASSERT_EQ(1, YGNodeGetChildCount(root3));
ASSERT_EQ(YGNodeGetChild(root2, 0), YGNodeGetChild(root3, 0));
const YGNodeRef root3_child1 = YGNodeNewWithConfig(config);
YGNodeInsertChild(root3, root3_child1, 1);
ASSERT_EQ(1, YGNodeGetChildCount(root2));
ASSERT_EQ(2, YGNodeGetChildCount(root3));
ASSERT_EQ(root3_child1, YGNodeGetChild(root3, 1));
ASSERT_NE(YGNodeGetChild(root2, 0), YGNodeGetChild(root3, 0));
const YGNodeRef root4 = YGNodeClone(root3);
ASSERT_EQ(root3_child1, YGNodeGetChild(root4, 1));
YGNodeRemoveChild(root4, root3_child1);
ASSERT_EQ(2, YGNodeGetChildCount(root3));
ASSERT_EQ(1, YGNodeGetChildCount(root4));
ASSERT_NE(YGNodeGetChild(root3, 0), YGNodeGetChild(root4, 0));
YGNodeFreeRecursive(root4);
YGNodeFreeRecursive(root3);
YGNodeFreeRecursive(root2);
YGNodeFreeRecursive(root);
YGConfigFree(config);
}
TEST(YogaTest, cloning_two_levels) {
const YGConfigRef config = YGConfigNew();
const YGNodeRef root = YGNodeNewWithConfig(config);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexGrow(root_child0, 1);
YGNodeStyleSetFlexBasis(root_child0, 15);
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexGrow(root_child1, 1);
YGNodeInsertChild(root, root_child1, 1);
const YGNodeRef root_child1_0 = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexBasis(root_child1_0, 10);
YGNodeStyleSetFlexGrow(root_child1_0, 1);
YGNodeInsertChild(root_child1, root_child1_0, 0);
const YGNodeRef root_child1_1 = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexBasis(root_child1_1, 25);
YGNodeInsertChild(root_child1, root_child1_1, 1);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_FLOAT_EQ(40, YGNodeLayoutGetHeight(root_child0));
ASSERT_FLOAT_EQ(60, YGNodeLayoutGetHeight(root_child1));
ASSERT_FLOAT_EQ(35, YGNodeLayoutGetHeight(root_child1_0));
ASSERT_FLOAT_EQ(25, YGNodeLayoutGetHeight(root_child1_1));
const YGNodeRef root2_child0 = YGNodeClone(root_child0);
const YGNodeRef root2_child1 = YGNodeClone(root_child1);
const YGNodeRef root2 = YGNodeClone(root);
YGNodeStyleSetFlexGrow(root2_child0, 0);
YGNodeStyleSetFlexBasis(root2_child0, 40);
YGNodeRemoveAllChildren(root2);
YGNodeInsertChild(root2, root2_child0, 0);
YGNodeInsertChild(root2, root2_child1, 1);
ASSERT_EQ(2, YGNodeGetChildCount(root2));
YGNodeCalculateLayout(root2, YGUndefined, YGUndefined, YGDirectionLTR);
// Original root is unchanged
ASSERT_FLOAT_EQ(40, YGNodeLayoutGetHeight(root_child0));
ASSERT_FLOAT_EQ(60, YGNodeLayoutGetHeight(root_child1));
ASSERT_FLOAT_EQ(35, YGNodeLayoutGetHeight(root_child1_0));
ASSERT_FLOAT_EQ(25, YGNodeLayoutGetHeight(root_child1_1));
// New root has new layout at the top
ASSERT_FLOAT_EQ(40, YGNodeLayoutGetHeight(root2_child0));
ASSERT_FLOAT_EQ(60, YGNodeLayoutGetHeight(root2_child1));
// The deeper children are untouched.
ASSERT_EQ(YGNodeGetChild(root2_child1, 0), root_child1_0);
ASSERT_EQ(YGNodeGetChild(root2_child1, 1), root_child1_1);
YGNodeFreeRecursive(root2);
YGNodeFreeRecursive(root);
YGConfigFree(config);
}
TEST(YogaTest, cloning_and_freeing) {
const int32_t initialInstanceCount = YGNodeGetInstanceCount();
const YGConfigRef config = YGConfigNew();
const YGNodeRef root = YGNodeNewWithConfig(config);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNewWithConfig(config);
YGNodeInsertChild(root, root_child1, 1);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
const YGNodeRef root2 = YGNodeClone(root);
// Freeing the original root should be safe as long as we don't free its
// children.
YGNodeFree(root);
YGNodeCalculateLayout(root2, YGUndefined, YGUndefined, YGDirectionLTR);
YGNodeFreeRecursive(root2);
YGNodeFree(root_child0);
YGNodeFree(root_child1);
YGConfigFree(config);
ASSERT_EQ(initialInstanceCount, YGNodeGetInstanceCount());
}