Rebase of facebook flexbox to yoga
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-third-party / yoga / YGAspectRatioTest.cpp
1 /**
2  * Copyright (c) 2014-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7
8 #include <gtest/gtest.h>
9 #include <yoga/YGNode.h>
10 #include <yoga/Yoga.h>
11
12 static YGSize _measure_for_aspect_ratio(YGNodeRef node,
13                                                         float width,
14                                                         YGMeasureMode widthMode,
15                                                         float height,
16                                                         YGMeasureMode heightMode) {
17   return YGSize{
18       .width = widthMode == YGMeasureModeExactly ? width : 50,
19       .height = heightMode == YGMeasureModeExactly ? height : 50,
20   };
21 }
22
23 TEST(YogaTest, aspect_ratio_cross_defined) {
24   const YGNodeRef root = YGNodeNew();
25   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
26   YGNodeStyleSetWidth(root, 100);
27   YGNodeStyleSetHeight(root, 100);
28
29   const YGNodeRef root_child0 = YGNodeNew();
30   YGNodeStyleSetWidth(root_child0, 50);
31   YGNodeStyleSetAspectRatio(root_child0, 1);
32   YGNodeInsertChild(root, root_child0, 0);
33
34   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
35
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));
40
41   YGNodeFreeRecursive(root);
42 }
43
44 TEST(YogaTest, aspect_ratio_main_defined) {
45   const YGNodeRef root = YGNodeNew();
46   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
47   YGNodeStyleSetWidth(root, 100);
48   YGNodeStyleSetHeight(root, 100);
49
50   const YGNodeRef root_child0 = YGNodeNew();
51   YGNodeStyleSetHeight(root_child0, 50);
52   YGNodeStyleSetAspectRatio(root_child0, 1);
53   YGNodeInsertChild(root, root_child0, 0);
54
55   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
56
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));
61
62   YGNodeFreeRecursive(root);
63 }
64
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);
71
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);
77
78   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
79
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));
84
85   YGNodeFreeRecursive(root);
86 }
87
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);
93
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);
99
100   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
101
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));
106
107   YGNodeFreeRecursive(root);
108 }
109
110 TEST(YogaTest, aspect_ratio_align_stretch) {
111   const YGNodeRef root = YGNodeNew();
112   YGNodeStyleSetWidth(root, 100);
113   YGNodeStyleSetHeight(root, 100);
114
115   const YGNodeRef root_child0 = YGNodeNew();
116   YGNodeStyleSetAspectRatio(root_child0, 1);
117   YGNodeInsertChild(root, root_child0, 0);
118
119   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
120
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));
125
126   YGNodeFreeRecursive(root);
127 }
128
129 TEST(YogaTest, aspect_ratio_flex_grow) {
130   const YGNodeRef root = YGNodeNew();
131   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
132   YGNodeStyleSetWidth(root, 100);
133   YGNodeStyleSetHeight(root, 100);
134
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);
140
141   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
142
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));
147
148   YGNodeFreeRecursive(root);
149 }
150
151 TEST(YogaTest, aspect_ratio_flex_shrink) {
152   const YGNodeRef root = YGNodeNew();
153   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
154   YGNodeStyleSetWidth(root, 100);
155   YGNodeStyleSetHeight(root, 100);
156
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);
162
163   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
164
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));
169
170   YGNodeFreeRecursive(root);
171 }
172
173 TEST(YogaTest, aspect_ratio_flex_shrink_2) {
174   const YGNodeRef root = YGNodeNew();
175   YGNodeStyleSetWidth(root, 100);
176   YGNodeStyleSetHeight(root, 100);
177
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);
183
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);
189
190   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
191
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));
196
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));
201
202   YGNodeFreeRecursive(root);
203 }
204
205 TEST(YogaTest, aspect_ratio_basis) {
206   const YGNodeRef root = YGNodeNew();
207   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
208   YGNodeStyleSetWidth(root, 100);
209   YGNodeStyleSetHeight(root, 100);
210
211   const YGNodeRef root_child0 = YGNodeNew();
212   YGNodeStyleSetFlexBasis(root_child0, 50);
213   YGNodeStyleSetAspectRatio(root_child0, 1);
214   YGNodeInsertChild(root, root_child0, 0);
215
216   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
217
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));
222
223   YGNodeFreeRecursive(root);
224 }
225
226 TEST(YogaTest, aspect_ratio_absolute_layout_width_defined) {
227   const YGNodeRef root = YGNodeNew();
228   YGNodeStyleSetWidth(root, 100);
229   YGNodeStyleSetHeight(root, 100);
230
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);
238
239   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
240
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));
245
246   YGNodeFreeRecursive(root);
247 }
248
249 TEST(YogaTest, aspect_ratio_absolute_layout_height_defined) {
250   const YGNodeRef root = YGNodeNew();
251   YGNodeStyleSetWidth(root, 100);
252   YGNodeStyleSetHeight(root, 100);
253
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);
261
262   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
263
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));
268
269   YGNodeFreeRecursive(root);
270 }
271
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);
277
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);
283
284   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
285
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));
290
291   YGNodeFreeRecursive(root);
292 }
293
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);
299
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);
305
306   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
307
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));
312
313   YGNodeFreeRecursive(root);
314 }
315
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);
321
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);
327
328   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
329
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));
334
335   YGNodeFreeRecursive(root);
336 }
337
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);
343
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);
349
350   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
351
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));
356
357   YGNodeFreeRecursive(root);
358 }
359
360 TEST(YogaTest, aspect_ratio_double_cross) {
361   const YGNodeRef root = YGNodeNew();
362   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
363   YGNodeStyleSetWidth(root, 100);
364   YGNodeStyleSetHeight(root, 100);
365
366   const YGNodeRef root_child0 = YGNodeNew();
367   YGNodeStyleSetHeight(root_child0, 50);
368   YGNodeStyleSetAspectRatio(root_child0, 2);
369   YGNodeInsertChild(root, root_child0, 0);
370
371   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
372
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));
377
378   YGNodeFreeRecursive(root);
379 }
380
381 TEST(YogaTest, aspect_ratio_half_cross) {
382   const YGNodeRef root = YGNodeNew();
383   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
384   YGNodeStyleSetWidth(root, 100);
385   YGNodeStyleSetHeight(root, 100);
386
387   const YGNodeRef root_child0 = YGNodeNew();
388   YGNodeStyleSetHeight(root_child0, 100);
389   YGNodeStyleSetAspectRatio(root_child0, 0.5);
390   YGNodeInsertChild(root, root_child0, 0);
391
392   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
393
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));
398
399   YGNodeFreeRecursive(root);
400 }
401
402 TEST(YogaTest, aspect_ratio_double_main) {
403   const YGNodeRef root = YGNodeNew();
404   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
405   YGNodeStyleSetWidth(root, 100);
406   YGNodeStyleSetHeight(root, 100);
407
408   const YGNodeRef root_child0 = YGNodeNew();
409   YGNodeStyleSetWidth(root_child0, 50);
410   YGNodeStyleSetAspectRatio(root_child0, 0.5);
411   YGNodeInsertChild(root, root_child0, 0);
412
413   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
414
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));
419
420   YGNodeFreeRecursive(root);
421 }
422
423 TEST(YogaTest, aspect_ratio_half_main) {
424   const YGNodeRef root = YGNodeNew();
425   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
426   YGNodeStyleSetWidth(root, 100);
427   YGNodeStyleSetHeight(root, 100);
428
429   const YGNodeRef root_child0 = YGNodeNew();
430   YGNodeStyleSetWidth(root_child0, 100);
431   YGNodeStyleSetAspectRatio(root_child0, 2);
432   YGNodeInsertChild(root, root_child0, 0);
433
434   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
435
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));
440
441   YGNodeFreeRecursive(root);
442 }
443
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);
449
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);
454
455   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
456
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));
461
462   YGNodeFreeRecursive(root);
463 }
464
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);
471
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);
478
479   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
480
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));
485
486   YGNodeFreeRecursive(root);
487 }
488
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);
494
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);
501
502   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
503
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));
508
509   YGNodeFreeRecursive(root);
510 }
511
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);
518
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);
524
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);
530
531   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
532
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));
537
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));
542
543   YGNodeFreeRecursive(root);
544 }
545
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);
551
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);
557
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);
563
564   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
565
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));
570
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));
575
576   YGNodeFreeRecursive(root);
577 }
578
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);
585
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);
591
592   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
593
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));
598
599   YGNodeFreeRecursive(root);
600 }
601
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);
607
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);
613
614   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
615
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));
620
621   YGNodeFreeRecursive(root);
622 }
623
624 TEST(YogaTest, aspect_ratio_left_right_absolute) {
625   const YGNodeRef root = YGNodeNew();
626   YGNodeStyleSetWidth(root, 100);
627   YGNodeStyleSetHeight(root, 100);
628
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);
636
637   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
638
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));
643
644   YGNodeFreeRecursive(root);
645 }
646
647 TEST(YogaTest, aspect_ratio_top_bottom_absolute) {
648   const YGNodeRef root = YGNodeNew();
649   YGNodeStyleSetWidth(root, 100);
650   YGNodeStyleSetHeight(root, 100);
651
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);
659
660   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
661
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));
666
667   YGNodeFreeRecursive(root);
668 }
669
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);
675
676   const YGNodeRef root_child0 = YGNodeNew();
677   YGNodeStyleSetWidth(root_child0, 50);
678   YGNodeStyleSetAspectRatio(root_child0, 1);
679   YGNodeInsertChild(root, root_child0, 0);
680
681   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
682
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));
687
688   YGNodeFreeRecursive(root);
689 }
690
691 TEST(YogaTest, aspect_ratio_height_overrides_align_stretch_column) {
692   const YGNodeRef root = YGNodeNew();
693   YGNodeStyleSetWidth(root, 100);
694   YGNodeStyleSetHeight(root, 100);
695
696   const YGNodeRef root_child0 = YGNodeNew();
697   YGNodeStyleSetHeight(root_child0, 50);
698   YGNodeStyleSetAspectRatio(root_child0, 1);
699   YGNodeInsertChild(root, root_child0, 0);
700
701   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
702
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));
707
708   YGNodeFreeRecursive(root);
709 }
710
711 TEST(YogaTest, aspect_ratio_allow_child_overflow_parent_size) {
712   const YGNodeRef root = YGNodeNew();
713   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
714   YGNodeStyleSetWidth(root, 100);
715
716   const YGNodeRef root_child0 = YGNodeNew();
717   YGNodeStyleSetHeight(root_child0, 50);
718   YGNodeStyleSetAspectRatio(root_child0, 4);
719   YGNodeInsertChild(root, root_child0, 0);
720
721   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
722
723   ASSERT_EQ(100, YGNodeLayoutGetWidth(root));
724   ASSERT_EQ(50, YGNodeLayoutGetHeight(root));
725
726   ASSERT_EQ(200, YGNodeLayoutGetWidth(root_child0));
727   ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
728
729   YGNodeFreeRecursive(root);
730 }
731
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);
738
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);
745
746   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
747
748   ASSERT_EQ(100, YGNodeLayoutGetWidth(root));
749   ASSERT_EQ(100, YGNodeLayoutGetHeight(root));
750
751   ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
752   ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
753
754   YGNodeFreeRecursive(root);
755 }
756
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);
763
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);
770
771   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
772
773   ASSERT_EQ(100, YGNodeLayoutGetWidth(root));
774   ASSERT_EQ(100, YGNodeLayoutGetHeight(root));
775
776   ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
777   ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
778
779   YGNodeFreeRecursive(root);
780 }
781
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);
788
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);
795
796   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
797
798   ASSERT_EQ(100, YGNodeLayoutGetWidth(root));
799   ASSERT_EQ(100, YGNodeLayoutGetHeight(root));
800
801   ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
802   ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
803
804   YGNodeFreeRecursive(root);
805 }
806
807 TEST(YogaTest, aspect_ratio_should_prefer_explicit_height) {
808   const YGConfigRef config = YGConfigNew();
809   YGConfigSetUseWebDefaults(config, true);
810
811   const YGNodeRef root = YGNodeNewWithConfig(config);
812   YGNodeStyleSetFlexDirection(root, YGFlexDirectionColumn);
813
814   const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
815   YGNodeStyleSetFlexDirection(root_child0, YGFlexDirectionColumn);
816   YGNodeInsertChild(root, root_child0, 0);
817
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);
823
824   YGNodeCalculateLayout(root, 100, 200, YGDirectionLTR);
825
826   ASSERT_EQ(100, YGNodeLayoutGetWidth(root));
827   ASSERT_EQ(200, YGNodeLayoutGetHeight(root));
828
829   ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
830   ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
831
832   ASSERT_EQ(200, YGNodeLayoutGetWidth(root_child0_child0));
833   ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0_child0));
834
835   YGNodeFreeRecursive(root);
836 }
837
838 TEST(YogaTest, aspect_ratio_should_prefer_explicit_width) {
839   const YGConfigRef config = YGConfigNew();
840   YGConfigSetUseWebDefaults(config, true);
841
842   const YGNodeRef root = YGNodeNewWithConfig(config);
843   YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
844
845   const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
846   YGNodeStyleSetFlexDirection(root_child0, YGFlexDirectionRow);
847   YGNodeInsertChild(root, root_child0, 0);
848
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);
854
855   YGNodeCalculateLayout(root, 200, 100, YGDirectionLTR);
856
857   ASSERT_EQ(200, YGNodeLayoutGetWidth(root));
858   ASSERT_EQ(100, YGNodeLayoutGetHeight(root));
859
860   ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
861   ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
862
863   ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0_child0));
864   ASSERT_EQ(200, YGNodeLayoutGetHeight(root_child0_child0));
865
866   YGNodeFreeRecursive(root);
867 }
868
869 TEST(YogaTest, aspect_ratio_should_prefer_flexed_dimension) {
870   const YGConfigRef config = YGConfigNew();
871   YGConfigSetUseWebDefaults(config, true);
872
873   const YGNodeRef root = YGNodeNewWithConfig(config);
874
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);
880
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);
885
886   YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
887
888   ASSERT_EQ(100, YGNodeLayoutGetWidth(root));
889   ASSERT_EQ(100, YGNodeLayoutGetHeight(root));
890
891   ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
892   ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0));
893
894   ASSERT_EQ(200, YGNodeLayoutGetWidth(root_child0_child0));
895   ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0_child0));
896
897   YGNodeFreeRecursive(root);
898 }