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>
10 #include <yoga/Yoga.h>
12 struct _MeasureConstraint {
14 YGMeasureMode widthMode;
16 YGMeasureMode heightMode;
19 struct _MeasureConstraintList {
21 struct _MeasureConstraint *constraints;
24 static YGSize _measure_for_mode_test(YGNodeRef node,
26 YGMeasureMode widthMode,
28 YGMeasureMode heightMode) {
29 struct _MeasureConstraintList* constraintList =
30 (struct _MeasureConstraintList*)node->getContext();
31 struct _MeasureConstraint *constraints = constraintList->constraints;
32 uint32_t currentIndex = constraintList->length;
33 (&constraints[currentIndex])->width = width;
34 (&constraints[currentIndex])->widthMode = widthMode;
35 (&constraints[currentIndex])->height = height;
36 (&constraints[currentIndex])->heightMode = heightMode;
37 constraintList->length = currentIndex + 1;
40 .width = widthMode == YGMeasureModeUndefined ? 10 : width,
41 .height = heightMode == YGMeasureModeUndefined ? 10 : width,
45 TEST(YogaTest, exactly_measure_stretched_child_column) {
46 struct _MeasureConstraintList constraintList = _MeasureConstraintList{
48 .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
51 const YGNodeRef root = YGNodeNew();
52 YGNodeStyleSetWidth(root, 100);
53 YGNodeStyleSetHeight(root, 100);
55 const YGNodeRef root_child0 = YGNodeNew();
56 // root_child0->setContext(&constraintList);
57 root_child0->setContext(&constraintList);
58 root_child0->setMeasureFunc(_measure_for_mode_test);
59 // root_child0->setMeasureFunc(_measure_for_mode_test);
60 YGNodeInsertChild(root, root_child0, 0);
62 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
64 ASSERT_EQ(1, constraintList.length);
66 ASSERT_FLOAT_EQ(100, constraintList.constraints[0].width);
67 ASSERT_EQ(YGMeasureModeExactly, constraintList.constraints[0].widthMode);
69 free(constraintList.constraints);
70 YGNodeFreeRecursive(root);
73 TEST(YogaTest, exactly_measure_stretched_child_row) {
74 struct _MeasureConstraintList constraintList = _MeasureConstraintList{
76 .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
79 const YGNodeRef root = YGNodeNew();
80 YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
81 YGNodeStyleSetWidth(root, 100);
82 YGNodeStyleSetHeight(root, 100);
84 const YGNodeRef root_child0 = YGNodeNew();
85 // root_child0->setContext(&constraintList);
86 root_child0->setContext(&constraintList);
87 root_child0->setMeasureFunc(_measure_for_mode_test);
88 YGNodeInsertChild(root, root_child0, 0);
90 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
92 ASSERT_EQ(1, constraintList.length);
94 ASSERT_FLOAT_EQ(100, constraintList.constraints[0].height);
95 ASSERT_EQ(YGMeasureModeExactly, constraintList.constraints[0].heightMode);
97 free(constraintList.constraints);
98 YGNodeFreeRecursive(root);
101 TEST(YogaTest, at_most_main_axis_column) {
102 struct _MeasureConstraintList constraintList = _MeasureConstraintList{
104 .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
107 const YGNodeRef root = YGNodeNew();
108 YGNodeStyleSetWidth(root, 100);
109 YGNodeStyleSetHeight(root, 100);
111 const YGNodeRef root_child0 = YGNodeNew();
112 root_child0->setContext(&constraintList);
113 root_child0->setMeasureFunc(_measure_for_mode_test);
114 YGNodeInsertChild(root, root_child0, 0);
116 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
118 ASSERT_EQ(1, constraintList.length);
120 ASSERT_FLOAT_EQ(100, constraintList.constraints[0].height);
121 ASSERT_EQ(YGMeasureModeAtMost, constraintList.constraints[0].heightMode);
123 free(constraintList.constraints);
124 YGNodeFreeRecursive(root);
127 TEST(YogaTest, at_most_cross_axis_column) {
128 struct _MeasureConstraintList constraintList = _MeasureConstraintList{
130 .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
133 const YGNodeRef root = YGNodeNew();
134 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
135 YGNodeStyleSetWidth(root, 100);
136 YGNodeStyleSetHeight(root, 100);
138 const YGNodeRef root_child0 = YGNodeNew();
139 root_child0->setContext(&constraintList);
140 root_child0->setMeasureFunc(_measure_for_mode_test);
141 YGNodeInsertChild(root, root_child0, 0);
143 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
145 ASSERT_EQ(1, constraintList.length);
147 ASSERT_FLOAT_EQ(100, constraintList.constraints[0].width);
148 ASSERT_EQ(YGMeasureModeAtMost, constraintList.constraints[0].widthMode);
150 free(constraintList.constraints);
151 YGNodeFreeRecursive(root);
154 TEST(YogaTest, at_most_main_axis_row) {
155 struct _MeasureConstraintList constraintList = _MeasureConstraintList{
157 .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
160 const YGNodeRef root = YGNodeNew();
161 YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
162 YGNodeStyleSetWidth(root, 100);
163 YGNodeStyleSetHeight(root, 100);
165 const YGNodeRef root_child0 = YGNodeNew();
166 root_child0->setContext(&constraintList);
167 root_child0->setMeasureFunc(_measure_for_mode_test);
168 YGNodeInsertChild(root, root_child0, 0);
170 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
172 ASSERT_EQ(1, constraintList.length);
174 ASSERT_FLOAT_EQ(100, constraintList.constraints[0].width);
175 ASSERT_EQ(YGMeasureModeAtMost, constraintList.constraints[0].widthMode);
177 free(constraintList.constraints);
178 YGNodeFreeRecursive(root);
181 TEST(YogaTest, at_most_cross_axis_row) {
182 struct _MeasureConstraintList constraintList = _MeasureConstraintList{
184 .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
187 const YGNodeRef root = YGNodeNew();
188 YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
189 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
190 YGNodeStyleSetWidth(root, 100);
191 YGNodeStyleSetHeight(root, 100);
193 const YGNodeRef root_child0 = YGNodeNew();
194 root_child0->setContext(&constraintList);
195 root_child0->setMeasureFunc(_measure_for_mode_test);
196 YGNodeInsertChild(root, root_child0, 0);
198 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
200 ASSERT_EQ(1, constraintList.length);
202 ASSERT_FLOAT_EQ(100, constraintList.constraints[0].height);
203 ASSERT_EQ(YGMeasureModeAtMost, constraintList.constraints[0].heightMode);
205 free(constraintList.constraints);
206 YGNodeFreeRecursive(root);
209 TEST(YogaTest, flex_child) {
210 struct _MeasureConstraintList constraintList = _MeasureConstraintList{
212 .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
215 const YGNodeRef root = YGNodeNew();
216 YGNodeStyleSetHeight(root, 100);
218 const YGNodeRef root_child0 = YGNodeNew();
219 YGNodeStyleSetFlexGrow(root_child0, 1);
220 root_child0->setContext(&constraintList);
221 root_child0->setMeasureFunc(_measure_for_mode_test);
222 YGNodeInsertChild(root, root_child0, 0);
224 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
226 ASSERT_EQ(2, constraintList.length);
228 ASSERT_FLOAT_EQ(100, constraintList.constraints[0].height);
229 ASSERT_EQ(YGMeasureModeAtMost, constraintList.constraints[0].heightMode);
231 ASSERT_FLOAT_EQ(100, constraintList.constraints[1].height);
232 ASSERT_EQ(YGMeasureModeExactly, constraintList.constraints[1].heightMode);
234 free(constraintList.constraints);
235 YGNodeFreeRecursive(root);
238 TEST(YogaTest, flex_child_with_flex_basis) {
239 struct _MeasureConstraintList constraintList = _MeasureConstraintList{
241 .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
244 const YGNodeRef root = YGNodeNew();
245 YGNodeStyleSetHeight(root, 100);
247 const YGNodeRef root_child0 = YGNodeNew();
248 YGNodeStyleSetFlexGrow(root_child0, 1);
249 YGNodeStyleSetFlexBasis(root_child0, 0);
250 root_child0->setContext(&constraintList);
251 root_child0->setMeasureFunc(_measure_for_mode_test);
252 YGNodeInsertChild(root, root_child0, 0);
254 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
256 ASSERT_EQ(1, constraintList.length);
258 ASSERT_FLOAT_EQ(100, constraintList.constraints[0].height);
259 ASSERT_EQ(YGMeasureModeExactly, constraintList.constraints[0].heightMode);
261 free(constraintList.constraints);
262 YGNodeFreeRecursive(root);
265 TEST(YogaTest, overflow_scroll_column) {
266 struct _MeasureConstraintList constraintList = _MeasureConstraintList{
268 .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
271 const YGNodeRef root = YGNodeNew();
272 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
273 YGNodeStyleSetOverflow(root, YGOverflowScroll);
274 YGNodeStyleSetHeight(root, 100);
275 YGNodeStyleSetWidth(root, 100);
277 const YGNodeRef root_child0 = YGNodeNew();
278 root_child0->setContext(&constraintList);
279 root_child0->setMeasureFunc(_measure_for_mode_test);
280 YGNodeInsertChild(root, root_child0, 0);
282 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
284 ASSERT_EQ(1, constraintList.length);
286 ASSERT_FLOAT_EQ(100, constraintList.constraints[0].width);
287 ASSERT_EQ(YGMeasureModeAtMost, constraintList.constraints[0].widthMode);
289 ASSERT_TRUE(YGFloatIsUndefined(constraintList.constraints[0].height));
290 ASSERT_EQ(YGMeasureModeUndefined, constraintList.constraints[0].heightMode);
292 free(constraintList.constraints);
293 YGNodeFreeRecursive(root);
296 TEST(YogaTest, overflow_scroll_row) {
297 struct _MeasureConstraintList constraintList = _MeasureConstraintList{
299 .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
302 const YGNodeRef root = YGNodeNew();
303 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
304 YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
305 YGNodeStyleSetOverflow(root, YGOverflowScroll);
306 YGNodeStyleSetHeight(root, 100);
307 YGNodeStyleSetWidth(root, 100);
309 const YGNodeRef root_child0 = YGNodeNew();
310 root_child0->setContext(&constraintList);
311 root_child0->setMeasureFunc(_measure_for_mode_test);
312 YGNodeInsertChild(root, root_child0, 0);
314 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
316 ASSERT_EQ(1, constraintList.length);
318 ASSERT_TRUE(YGFloatIsUndefined(constraintList.constraints[0].width));
319 ASSERT_EQ(YGMeasureModeUndefined, constraintList.constraints[0].widthMode);
321 ASSERT_FLOAT_EQ(100, constraintList.constraints[0].height);
322 ASSERT_EQ(YGMeasureModeAtMost, constraintList.constraints[0].heightMode);
324 free(constraintList.constraints);
325 YGNodeFreeRecursive(root);