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 static YGSize _measure_for_aspect_ratio(YGNodeRef node,
14 YGMeasureMode widthMode,
16 YGMeasureMode heightMode) {
18 .width = widthMode == YGMeasureModeExactly ? width : 50,
19 .height = heightMode == YGMeasureModeExactly ? height : 50,
23 TEST(YogaTest, aspect_ratio_cross_defined) {
24 const YGNodeRef root = YGNodeNew();
25 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
26 YGNodeStyleSetWidth(root, 100);
27 YGNodeStyleSetHeight(root, 100);
29 const YGNodeRef root_child0 = YGNodeNew();
30 YGNodeStyleSetWidth(root_child0, 50);
31 YGNodeStyleSetAspectRatio(root_child0, 1);
32 YGNodeInsertChild(root, root_child0, 0);
34 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
36 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
37 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
38 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
39 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
41 YGNodeFreeRecursive(root);
44 TEST(YogaTest, aspect_ratio_main_defined) {
45 const YGNodeRef root = YGNodeNew();
46 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
47 YGNodeStyleSetWidth(root, 100);
48 YGNodeStyleSetHeight(root, 100);
50 const YGNodeRef root_child0 = YGNodeNew();
51 YGNodeStyleSetHeight(root_child0, 50);
52 YGNodeStyleSetAspectRatio(root_child0, 1);
53 YGNodeInsertChild(root, root_child0, 0);
55 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
57 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
58 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
59 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
60 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
62 YGNodeFreeRecursive(root);
65 TEST(YogaTest, aspect_ratio_both_dimensions_defined_row) {
66 const YGNodeRef root = YGNodeNew();
67 YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
68 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
69 YGNodeStyleSetWidth(root, 100);
70 YGNodeStyleSetHeight(root, 100);
72 const YGNodeRef root_child0 = YGNodeNew();
73 YGNodeStyleSetWidth(root_child0, 100);
74 YGNodeStyleSetHeight(root_child0, 50);
75 YGNodeStyleSetAspectRatio(root_child0, 1);
76 YGNodeInsertChild(root, root_child0, 0);
78 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
80 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
81 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
82 ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
83 ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
85 YGNodeFreeRecursive(root);
88 TEST(YogaTest, aspect_ratio_both_dimensions_defined_column) {
89 const YGNodeRef root = YGNodeNew();
90 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
91 YGNodeStyleSetWidth(root, 100);
92 YGNodeStyleSetHeight(root, 100);
94 const YGNodeRef root_child0 = YGNodeNew();
95 YGNodeStyleSetWidth(root_child0, 100);
96 YGNodeStyleSetHeight(root_child0, 50);
97 YGNodeStyleSetAspectRatio(root_child0, 1);
98 YGNodeInsertChild(root, root_child0, 0);
100 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
102 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
103 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
104 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
105 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
107 YGNodeFreeRecursive(root);
110 TEST(YogaTest, aspect_ratio_align_stretch) {
111 const YGNodeRef root = YGNodeNew();
112 YGNodeStyleSetWidth(root, 100);
113 YGNodeStyleSetHeight(root, 100);
115 const YGNodeRef root_child0 = YGNodeNew();
116 YGNodeStyleSetAspectRatio(root_child0, 1);
117 YGNodeInsertChild(root, root_child0, 0);
119 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
121 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
122 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
123 ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
124 ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
126 YGNodeFreeRecursive(root);
129 TEST(YogaTest, aspect_ratio_flex_grow) {
130 const YGNodeRef root = YGNodeNew();
131 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
132 YGNodeStyleSetWidth(root, 100);
133 YGNodeStyleSetHeight(root, 100);
135 const YGNodeRef root_child0 = YGNodeNew();
136 YGNodeStyleSetHeight(root_child0, 50);
137 YGNodeStyleSetFlexGrow(root_child0, 1);
138 YGNodeStyleSetAspectRatio(root_child0, 1);
139 YGNodeInsertChild(root, root_child0, 0);
141 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
143 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
144 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
145 ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
146 ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
148 YGNodeFreeRecursive(root);
151 TEST(YogaTest, aspect_ratio_flex_shrink) {
152 const YGNodeRef root = YGNodeNew();
153 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
154 YGNodeStyleSetWidth(root, 100);
155 YGNodeStyleSetHeight(root, 100);
157 const YGNodeRef root_child0 = YGNodeNew();
158 YGNodeStyleSetHeight(root_child0, 150);
159 YGNodeStyleSetFlexShrink(root_child0, 1);
160 YGNodeStyleSetAspectRatio(root_child0, 1);
161 YGNodeInsertChild(root, root_child0, 0);
163 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
165 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
166 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
167 ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
168 ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
170 YGNodeFreeRecursive(root);
173 TEST(YogaTest, aspect_ratio_flex_shrink_2) {
174 const YGNodeRef root = YGNodeNew();
175 YGNodeStyleSetWidth(root, 100);
176 YGNodeStyleSetHeight(root, 100);
178 const YGNodeRef root_child0 = YGNodeNew();
179 YGNodeStyleSetHeightPercent(root_child0, 100);
180 YGNodeStyleSetFlexShrink(root_child0, 1);
181 YGNodeStyleSetAspectRatio(root_child0, 1);
182 YGNodeInsertChild(root, root_child0, 0);
184 const YGNodeRef root_child1 = YGNodeNew();
185 YGNodeStyleSetHeightPercent(root_child1, 100);
186 YGNodeStyleSetFlexShrink(root_child1, 1);
187 YGNodeStyleSetAspectRatio(root_child1, 1);
188 YGNodeInsertChild(root, root_child1, 1);
190 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
192 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
193 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
194 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
195 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
197 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child1));
198 ASSERT_EQ(50, YGNodeLayoutGetTop(root_child1));
199 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child1));
200 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child1));
202 YGNodeFreeRecursive(root);
205 TEST(YogaTest, aspect_ratio_basis) {
206 const YGNodeRef root = YGNodeNew();
207 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
208 YGNodeStyleSetWidth(root, 100);
209 YGNodeStyleSetHeight(root, 100);
211 const YGNodeRef root_child0 = YGNodeNew();
212 YGNodeStyleSetFlexBasis(root_child0, 50);
213 YGNodeStyleSetAspectRatio(root_child0, 1);
214 YGNodeInsertChild(root, root_child0, 0);
216 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
218 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
219 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
220 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
221 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
223 YGNodeFreeRecursive(root);
226 TEST(YogaTest, aspect_ratio_absolute_layout_width_defined) {
227 const YGNodeRef root = YGNodeNew();
228 YGNodeStyleSetWidth(root, 100);
229 YGNodeStyleSetHeight(root, 100);
231 const YGNodeRef root_child0 = YGNodeNew();
232 YGNodeStyleSetPositionType(root_child0, YGPositionTypeAbsolute);
233 YGNodeStyleSetPosition(root_child0, YGEdgeLeft, 0);
234 YGNodeStyleSetPosition(root_child0, YGEdgeTop, 0);
235 YGNodeStyleSetWidth(root_child0, 50);
236 YGNodeStyleSetAspectRatio(root_child0, 1);
237 YGNodeInsertChild(root, root_child0, 0);
239 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
241 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
242 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
243 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
244 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
246 YGNodeFreeRecursive(root);
249 TEST(YogaTest, aspect_ratio_absolute_layout_height_defined) {
250 const YGNodeRef root = YGNodeNew();
251 YGNodeStyleSetWidth(root, 100);
252 YGNodeStyleSetHeight(root, 100);
254 const YGNodeRef root_child0 = YGNodeNew();
255 YGNodeStyleSetPositionType(root_child0, YGPositionTypeAbsolute);
256 YGNodeStyleSetPosition(root_child0, YGEdgeLeft, 0);
257 YGNodeStyleSetPosition(root_child0, YGEdgeTop, 0);
258 YGNodeStyleSetHeight(root_child0, 50);
259 YGNodeStyleSetAspectRatio(root_child0, 1);
260 YGNodeInsertChild(root, root_child0, 0);
262 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
264 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
265 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
266 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
267 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
269 YGNodeFreeRecursive(root);
272 TEST(YogaTest, aspect_ratio_with_max_cross_defined) {
273 const YGNodeRef root = YGNodeNew();
274 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
275 YGNodeStyleSetWidth(root, 100);
276 YGNodeStyleSetHeight(root, 100);
278 const YGNodeRef root_child0 = YGNodeNew();
279 YGNodeStyleSetHeight(root_child0, 50);
280 YGNodeStyleSetMaxWidth(root_child0, 40);
281 YGNodeStyleSetAspectRatio(root_child0, 1);
282 YGNodeInsertChild(root, root_child0, 0);
284 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
286 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
287 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
288 ASSERT_EQ(40, YGNodeLayoutGetWidth(root_child0));
289 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
291 YGNodeFreeRecursive(root);
294 TEST(YogaTest, aspect_ratio_with_max_main_defined) {
295 const YGNodeRef root = YGNodeNew();
296 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
297 YGNodeStyleSetWidth(root, 100);
298 YGNodeStyleSetHeight(root, 100);
300 const YGNodeRef root_child0 = YGNodeNew();
301 YGNodeStyleSetWidth(root_child0, 50);
302 YGNodeStyleSetMaxHeight(root_child0, 40);
303 YGNodeStyleSetAspectRatio(root_child0, 1);
304 YGNodeInsertChild(root, root_child0, 0);
306 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
308 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
309 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
310 ASSERT_EQ(40, YGNodeLayoutGetWidth(root_child0));
311 ASSERT_EQ(40, YGNodeLayoutGetHeight(root_child0));
313 YGNodeFreeRecursive(root);
316 TEST(YogaTest, aspect_ratio_with_min_cross_defined) {
317 const YGNodeRef root = YGNodeNew();
318 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
319 YGNodeStyleSetWidth(root, 100);
320 YGNodeStyleSetHeight(root, 100);
322 const YGNodeRef root_child0 = YGNodeNew();
323 YGNodeStyleSetHeight(root_child0, 30);
324 YGNodeStyleSetMinWidth(root_child0, 40);
325 YGNodeStyleSetAspectRatio(root_child0, 1);
326 YGNodeInsertChild(root, root_child0, 0);
328 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
330 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
331 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
332 ASSERT_EQ(40, YGNodeLayoutGetWidth(root_child0));
333 ASSERT_EQ(30, YGNodeLayoutGetHeight(root_child0));
335 YGNodeFreeRecursive(root);
338 TEST(YogaTest, aspect_ratio_with_min_main_defined) {
339 const YGNodeRef root = YGNodeNew();
340 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
341 YGNodeStyleSetWidth(root, 100);
342 YGNodeStyleSetHeight(root, 100);
344 const YGNodeRef root_child0 = YGNodeNew();
345 YGNodeStyleSetWidth(root_child0, 30);
346 YGNodeStyleSetMinHeight(root_child0, 40);
347 YGNodeStyleSetAspectRatio(root_child0, 1);
348 YGNodeInsertChild(root, root_child0, 0);
350 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
352 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
353 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
354 ASSERT_EQ(40, YGNodeLayoutGetWidth(root_child0));
355 ASSERT_EQ(40, YGNodeLayoutGetHeight(root_child0));
357 YGNodeFreeRecursive(root);
360 TEST(YogaTest, aspect_ratio_double_cross) {
361 const YGNodeRef root = YGNodeNew();
362 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
363 YGNodeStyleSetWidth(root, 100);
364 YGNodeStyleSetHeight(root, 100);
366 const YGNodeRef root_child0 = YGNodeNew();
367 YGNodeStyleSetHeight(root_child0, 50);
368 YGNodeStyleSetAspectRatio(root_child0, 2);
369 YGNodeInsertChild(root, root_child0, 0);
371 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
373 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
374 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
375 ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
376 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
378 YGNodeFreeRecursive(root);
381 TEST(YogaTest, aspect_ratio_half_cross) {
382 const YGNodeRef root = YGNodeNew();
383 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
384 YGNodeStyleSetWidth(root, 100);
385 YGNodeStyleSetHeight(root, 100);
387 const YGNodeRef root_child0 = YGNodeNew();
388 YGNodeStyleSetHeight(root_child0, 100);
389 YGNodeStyleSetAspectRatio(root_child0, 0.5);
390 YGNodeInsertChild(root, root_child0, 0);
392 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
394 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
395 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
396 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
397 ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
399 YGNodeFreeRecursive(root);
402 TEST(YogaTest, aspect_ratio_double_main) {
403 const YGNodeRef root = YGNodeNew();
404 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
405 YGNodeStyleSetWidth(root, 100);
406 YGNodeStyleSetHeight(root, 100);
408 const YGNodeRef root_child0 = YGNodeNew();
409 YGNodeStyleSetWidth(root_child0, 50);
410 YGNodeStyleSetAspectRatio(root_child0, 0.5);
411 YGNodeInsertChild(root, root_child0, 0);
413 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
415 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
416 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
417 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
418 ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
420 YGNodeFreeRecursive(root);
423 TEST(YogaTest, aspect_ratio_half_main) {
424 const YGNodeRef root = YGNodeNew();
425 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
426 YGNodeStyleSetWidth(root, 100);
427 YGNodeStyleSetHeight(root, 100);
429 const YGNodeRef root_child0 = YGNodeNew();
430 YGNodeStyleSetWidth(root_child0, 100);
431 YGNodeStyleSetAspectRatio(root_child0, 2);
432 YGNodeInsertChild(root, root_child0, 0);
434 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
436 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
437 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
438 ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
439 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
441 YGNodeFreeRecursive(root);
444 TEST(YogaTest, aspect_ratio_with_measure_func) {
445 const YGNodeRef root = YGNodeNew();
446 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
447 YGNodeStyleSetWidth(root, 100);
448 YGNodeStyleSetHeight(root, 100);
450 const YGNodeRef root_child0 = YGNodeNew();
451 root_child0->setMeasureFunc(_measure_for_aspect_ratio);
452 YGNodeStyleSetAspectRatio(root_child0, 1);
453 YGNodeInsertChild(root, root_child0, 0);
455 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
457 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
458 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
459 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
460 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
462 YGNodeFreeRecursive(root);
465 TEST(YogaTest, aspect_ratio_width_height_flex_grow_row) {
466 const YGNodeRef root = YGNodeNew();
467 YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
468 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
469 YGNodeStyleSetWidth(root, 100);
470 YGNodeStyleSetHeight(root, 200);
472 const YGNodeRef root_child0 = YGNodeNew();
473 YGNodeStyleSetWidth(root_child0, 50);
474 YGNodeStyleSetHeight(root_child0, 50);
475 YGNodeStyleSetFlexGrow(root_child0, 1);
476 YGNodeStyleSetAspectRatio(root_child0, 1);
477 YGNodeInsertChild(root, root_child0, 0);
479 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
481 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
482 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
483 ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
484 ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
486 YGNodeFreeRecursive(root);
489 TEST(YogaTest, aspect_ratio_width_height_flex_grow_column) {
490 const YGNodeRef root = YGNodeNew();
491 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
492 YGNodeStyleSetWidth(root, 200);
493 YGNodeStyleSetHeight(root, 100);
495 const YGNodeRef root_child0 = YGNodeNew();
496 YGNodeStyleSetWidth(root_child0, 50);
497 YGNodeStyleSetHeight(root_child0, 50);
498 YGNodeStyleSetFlexGrow(root_child0, 1);
499 YGNodeStyleSetAspectRatio(root_child0, 1);
500 YGNodeInsertChild(root, root_child0, 0);
502 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
504 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
505 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
506 ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
507 ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
509 YGNodeFreeRecursive(root);
512 TEST(YogaTest, aspect_ratio_height_as_flex_basis) {
513 const YGNodeRef root = YGNodeNew();
514 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
515 YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
516 YGNodeStyleSetWidth(root, 200);
517 YGNodeStyleSetHeight(root, 200);
519 const YGNodeRef root_child0 = YGNodeNew();
520 YGNodeStyleSetHeight(root_child0, 50);
521 YGNodeStyleSetFlexGrow(root_child0, 1);
522 YGNodeStyleSetAspectRatio(root_child0, 1);
523 YGNodeInsertChild(root, root_child0, 0);
525 const YGNodeRef root_child1 = YGNodeNew();
526 YGNodeStyleSetHeight(root_child1, 100);
527 YGNodeStyleSetFlexGrow(root_child1, 1);
528 YGNodeStyleSetAspectRatio(root_child1, 1);
529 YGNodeInsertChild(root, root_child1, 1);
531 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
533 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
534 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
535 ASSERT_EQ(75, YGNodeLayoutGetWidth(root_child0));
536 ASSERT_EQ(75, YGNodeLayoutGetHeight(root_child0));
538 ASSERT_EQ(75, YGNodeLayoutGetLeft(root_child1));
539 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child1));
540 ASSERT_EQ(125, YGNodeLayoutGetWidth(root_child1));
541 ASSERT_EQ(125, YGNodeLayoutGetHeight(root_child1));
543 YGNodeFreeRecursive(root);
546 TEST(YogaTest, aspect_ratio_width_as_flex_basis) {
547 const YGNodeRef root = YGNodeNew();
548 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
549 YGNodeStyleSetWidth(root, 200);
550 YGNodeStyleSetHeight(root, 200);
552 const YGNodeRef root_child0 = YGNodeNew();
553 YGNodeStyleSetWidth(root_child0, 50);
554 YGNodeStyleSetFlexGrow(root_child0, 1);
555 YGNodeStyleSetAspectRatio(root_child0, 1);
556 YGNodeInsertChild(root, root_child0, 0);
558 const YGNodeRef root_child1 = YGNodeNew();
559 YGNodeStyleSetWidth(root_child1, 100);
560 YGNodeStyleSetFlexGrow(root_child1, 1);
561 YGNodeStyleSetAspectRatio(root_child1, 1);
562 YGNodeInsertChild(root, root_child1, 1);
564 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
566 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
567 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
568 ASSERT_EQ(75, YGNodeLayoutGetWidth(root_child0));
569 ASSERT_EQ(75, YGNodeLayoutGetHeight(root_child0));
571 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child1));
572 ASSERT_EQ(75, YGNodeLayoutGetTop(root_child1));
573 ASSERT_EQ(125, YGNodeLayoutGetWidth(root_child1));
574 ASSERT_EQ(125, YGNodeLayoutGetHeight(root_child1));
576 YGNodeFreeRecursive(root);
579 TEST(YogaTest, aspect_ratio_overrides_flex_grow_row) {
580 const YGNodeRef root = YGNodeNew();
581 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
582 YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
583 YGNodeStyleSetWidth(root, 100);
584 YGNodeStyleSetHeight(root, 100);
586 const YGNodeRef root_child0 = YGNodeNew();
587 YGNodeStyleSetWidth(root_child0, 50);
588 YGNodeStyleSetFlexGrow(root_child0, 1);
589 YGNodeStyleSetAspectRatio(root_child0, 0.5);
590 YGNodeInsertChild(root, root_child0, 0);
592 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
594 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
595 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
596 ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
597 ASSERT_EQ(200, YGNodeLayoutGetHeight(root_child0));
599 YGNodeFreeRecursive(root);
602 TEST(YogaTest, aspect_ratio_overrides_flex_grow_column) {
603 const YGNodeRef root = YGNodeNew();
604 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
605 YGNodeStyleSetWidth(root, 100);
606 YGNodeStyleSetHeight(root, 100);
608 const YGNodeRef root_child0 = YGNodeNew();
609 YGNodeStyleSetHeight(root_child0, 50);
610 YGNodeStyleSetFlexGrow(root_child0, 1);
611 YGNodeStyleSetAspectRatio(root_child0, 2);
612 YGNodeInsertChild(root, root_child0, 0);
614 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
616 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
617 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
618 ASSERT_EQ(200, YGNodeLayoutGetWidth(root_child0));
619 ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
621 YGNodeFreeRecursive(root);
624 TEST(YogaTest, aspect_ratio_left_right_absolute) {
625 const YGNodeRef root = YGNodeNew();
626 YGNodeStyleSetWidth(root, 100);
627 YGNodeStyleSetHeight(root, 100);
629 const YGNodeRef root_child0 = YGNodeNew();
630 YGNodeStyleSetPositionType(root_child0, YGPositionTypeAbsolute);
631 YGNodeStyleSetPosition(root_child0, YGEdgeLeft, 10);
632 YGNodeStyleSetPosition(root_child0, YGEdgeTop, 10);
633 YGNodeStyleSetPosition(root_child0, YGEdgeRight, 10);
634 YGNodeStyleSetAspectRatio(root_child0, 1);
635 YGNodeInsertChild(root, root_child0, 0);
637 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
639 ASSERT_EQ(10, YGNodeLayoutGetLeft(root_child0));
640 ASSERT_EQ(10, YGNodeLayoutGetTop(root_child0));
641 ASSERT_EQ(80, YGNodeLayoutGetWidth(root_child0));
642 ASSERT_EQ(80, YGNodeLayoutGetHeight(root_child0));
644 YGNodeFreeRecursive(root);
647 TEST(YogaTest, aspect_ratio_top_bottom_absolute) {
648 const YGNodeRef root = YGNodeNew();
649 YGNodeStyleSetWidth(root, 100);
650 YGNodeStyleSetHeight(root, 100);
652 const YGNodeRef root_child0 = YGNodeNew();
653 YGNodeStyleSetPositionType(root_child0, YGPositionTypeAbsolute);
654 YGNodeStyleSetPosition(root_child0, YGEdgeLeft, 10);
655 YGNodeStyleSetPosition(root_child0, YGEdgeTop, 10);
656 YGNodeStyleSetPosition(root_child0, YGEdgeBottom, 10);
657 YGNodeStyleSetAspectRatio(root_child0, 1);
658 YGNodeInsertChild(root, root_child0, 0);
660 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
662 ASSERT_EQ(10, YGNodeLayoutGetLeft(root_child0));
663 ASSERT_EQ(10, YGNodeLayoutGetTop(root_child0));
664 ASSERT_EQ(80, YGNodeLayoutGetWidth(root_child0));
665 ASSERT_EQ(80, YGNodeLayoutGetHeight(root_child0));
667 YGNodeFreeRecursive(root);
670 TEST(YogaTest, aspect_ratio_width_overrides_align_stretch_row) {
671 const YGNodeRef root = YGNodeNew();
672 YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
673 YGNodeStyleSetWidth(root, 100);
674 YGNodeStyleSetHeight(root, 100);
676 const YGNodeRef root_child0 = YGNodeNew();
677 YGNodeStyleSetWidth(root_child0, 50);
678 YGNodeStyleSetAspectRatio(root_child0, 1);
679 YGNodeInsertChild(root, root_child0, 0);
681 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
683 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
684 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
685 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
686 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
688 YGNodeFreeRecursive(root);
691 TEST(YogaTest, aspect_ratio_height_overrides_align_stretch_column) {
692 const YGNodeRef root = YGNodeNew();
693 YGNodeStyleSetWidth(root, 100);
694 YGNodeStyleSetHeight(root, 100);
696 const YGNodeRef root_child0 = YGNodeNew();
697 YGNodeStyleSetHeight(root_child0, 50);
698 YGNodeStyleSetAspectRatio(root_child0, 1);
699 YGNodeInsertChild(root, root_child0, 0);
701 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
703 ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
704 ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
705 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
706 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
708 YGNodeFreeRecursive(root);
711 TEST(YogaTest, aspect_ratio_allow_child_overflow_parent_size) {
712 const YGNodeRef root = YGNodeNew();
713 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
714 YGNodeStyleSetWidth(root, 100);
716 const YGNodeRef root_child0 = YGNodeNew();
717 YGNodeStyleSetHeight(root_child0, 50);
718 YGNodeStyleSetAspectRatio(root_child0, 4);
719 YGNodeInsertChild(root, root_child0, 0);
721 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
723 ASSERT_EQ(100, YGNodeLayoutGetWidth(root));
724 ASSERT_EQ(50, YGNodeLayoutGetHeight(root));
726 ASSERT_EQ(200, YGNodeLayoutGetWidth(root_child0));
727 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
729 YGNodeFreeRecursive(root);
732 TEST(YogaTest, aspect_ratio_defined_main_with_margin) {
733 const YGNodeRef root = YGNodeNew();
734 YGNodeStyleSetAlignItems(root, YGAlignCenter);
735 YGNodeStyleSetJustifyContent(root, YGJustifyCenter);
736 YGNodeStyleSetWidth(root, 100);
737 YGNodeStyleSetHeight(root, 100);
739 const YGNodeRef root_child0 = YGNodeNew();
740 YGNodeStyleSetHeight(root_child0, 50);
741 YGNodeStyleSetAspectRatio(root_child0, 1);
742 YGNodeStyleSetMargin(root_child0, YGEdgeLeft, 10);
743 YGNodeStyleSetMargin(root_child0, YGEdgeRight, 10);
744 YGNodeInsertChild(root, root_child0, 0);
746 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
748 ASSERT_EQ(100, YGNodeLayoutGetWidth(root));
749 ASSERT_EQ(100, YGNodeLayoutGetHeight(root));
751 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
752 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
754 YGNodeFreeRecursive(root);
757 TEST(YogaTest, aspect_ratio_defined_cross_with_margin) {
758 const YGNodeRef root = YGNodeNew();
759 YGNodeStyleSetAlignItems(root, YGAlignCenter);
760 YGNodeStyleSetJustifyContent(root, YGJustifyCenter);
761 YGNodeStyleSetWidth(root, 100);
762 YGNodeStyleSetHeight(root, 100);
764 const YGNodeRef root_child0 = YGNodeNew();
765 YGNodeStyleSetWidth(root_child0, 50);
766 YGNodeStyleSetAspectRatio(root_child0, 1);
767 YGNodeStyleSetMargin(root_child0, YGEdgeLeft, 10);
768 YGNodeStyleSetMargin(root_child0, YGEdgeRight, 10);
769 YGNodeInsertChild(root, root_child0, 0);
771 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
773 ASSERT_EQ(100, YGNodeLayoutGetWidth(root));
774 ASSERT_EQ(100, YGNodeLayoutGetHeight(root));
776 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
777 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
779 YGNodeFreeRecursive(root);
782 TEST(YogaTest, aspect_ratio_defined_cross_with_main_margin) {
783 const YGNodeRef root = YGNodeNew();
784 YGNodeStyleSetAlignItems(root, YGAlignCenter);
785 YGNodeStyleSetJustifyContent(root, YGJustifyCenter);
786 YGNodeStyleSetWidth(root, 100);
787 YGNodeStyleSetHeight(root, 100);
789 const YGNodeRef root_child0 = YGNodeNew();
790 YGNodeStyleSetWidth(root_child0, 50);
791 YGNodeStyleSetAspectRatio(root_child0, 1);
792 YGNodeStyleSetMargin(root_child0, YGEdgeTop, 10);
793 YGNodeStyleSetMargin(root_child0, YGEdgeBottom, 10);
794 YGNodeInsertChild(root, root_child0, 0);
796 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
798 ASSERT_EQ(100, YGNodeLayoutGetWidth(root));
799 ASSERT_EQ(100, YGNodeLayoutGetHeight(root));
801 ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
802 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
804 YGNodeFreeRecursive(root);
807 TEST(YogaTest, aspect_ratio_should_prefer_explicit_height) {
808 const YGConfigRef config = YGConfigNew();
809 YGConfigSetUseWebDefaults(config, true);
811 const YGNodeRef root = YGNodeNewWithConfig(config);
812 YGNodeStyleSetFlexDirection(root, YGFlexDirectionColumn);
814 const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
815 YGNodeStyleSetFlexDirection(root_child0, YGFlexDirectionColumn);
816 YGNodeInsertChild(root, root_child0, 0);
818 const YGNodeRef root_child0_child0 = YGNodeNewWithConfig(config);
819 YGNodeStyleSetFlexDirection(root_child0_child0, YGFlexDirectionColumn);
820 YGNodeStyleSetHeight(root_child0_child0, 100);
821 YGNodeStyleSetAspectRatio(root_child0_child0, 2);
822 YGNodeInsertChild(root_child0, root_child0_child0, 0);
824 YGNodeCalculateLayout(root, 100, 200, YGDirectionLTR);
826 ASSERT_EQ(100, YGNodeLayoutGetWidth(root));
827 ASSERT_EQ(200, YGNodeLayoutGetHeight(root));
829 ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
830 ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
832 ASSERT_EQ(200, YGNodeLayoutGetWidth(root_child0_child0));
833 ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0_child0));
835 YGNodeFreeRecursive(root);
838 TEST(YogaTest, aspect_ratio_should_prefer_explicit_width) {
839 const YGConfigRef config = YGConfigNew();
840 YGConfigSetUseWebDefaults(config, true);
842 const YGNodeRef root = YGNodeNewWithConfig(config);
843 YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
845 const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
846 YGNodeStyleSetFlexDirection(root_child0, YGFlexDirectionRow);
847 YGNodeInsertChild(root, root_child0, 0);
849 const YGNodeRef root_child0_child0 = YGNodeNewWithConfig(config);
850 YGNodeStyleSetFlexDirection(root_child0_child0, YGFlexDirectionRow);
851 YGNodeStyleSetWidth(root_child0_child0, 100);
852 YGNodeStyleSetAspectRatio(root_child0_child0, 0.5);
853 YGNodeInsertChild(root_child0, root_child0_child0, 0);
855 YGNodeCalculateLayout(root, 200, 100, YGDirectionLTR);
857 ASSERT_EQ(200, YGNodeLayoutGetWidth(root));
858 ASSERT_EQ(100, YGNodeLayoutGetHeight(root));
860 ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
861 ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
863 ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0_child0));
864 ASSERT_EQ(200, YGNodeLayoutGetHeight(root_child0_child0));
866 YGNodeFreeRecursive(root);
869 TEST(YogaTest, aspect_ratio_should_prefer_flexed_dimension) {
870 const YGConfigRef config = YGConfigNew();
871 YGConfigSetUseWebDefaults(config, true);
873 const YGNodeRef root = YGNodeNewWithConfig(config);
875 const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
876 YGNodeStyleSetFlexDirection(root_child0, YGFlexDirectionColumn);
877 YGNodeStyleSetAspectRatio(root_child0, 2);
878 YGNodeStyleSetFlexGrow(root_child0, 1);
879 YGNodeInsertChild(root, root_child0, 0);
881 const YGNodeRef root_child0_child0 = YGNodeNewWithConfig(config);
882 YGNodeStyleSetAspectRatio(root_child0_child0, 4);
883 YGNodeStyleSetFlexGrow(root_child0_child0, 1);
884 YGNodeInsertChild(root_child0, root_child0_child0, 0);
886 YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
888 ASSERT_EQ(100, YGNodeLayoutGetWidth(root));
889 ASSERT_EQ(100, YGNodeLayoutGetHeight(root));
891 ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
892 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
894 ASSERT_EQ(200, YGNodeLayoutGetWidth(root_child0_child0));
895 ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0_child0));
897 YGNodeFreeRecursive(root);