Rebase of facebook flexbox to yoga
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-third-party / yoga / YGMeasureModeTest.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 struct _MeasureConstraint {
13   float width;
14   YGMeasureMode widthMode;
15   float height;
16   YGMeasureMode heightMode;
17 };
18
19 struct _MeasureConstraintList {
20   uint32_t length;
21   struct _MeasureConstraint *constraints;
22 };
23
24 static YGSize _measure_for_mode_test(YGNodeRef node,
25                                                  float width,
26                                                  YGMeasureMode widthMode,
27                                                  float height,
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;
38
39   return YGSize{
40       .width = widthMode == YGMeasureModeUndefined ? 10 : width,
41       .height = heightMode == YGMeasureModeUndefined ? 10 : width,
42   };
43 }
44
45 TEST(YogaTest, exactly_measure_stretched_child_column) {
46   struct _MeasureConstraintList constraintList = _MeasureConstraintList{
47       .length = 0,
48       .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
49   };
50
51   const YGNodeRef root = YGNodeNew();
52   YGNodeStyleSetWidth(root, 100);
53   YGNodeStyleSetHeight(root, 100);
54
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);
61
62   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
63
64   ASSERT_EQ(1, constraintList.length);
65
66   ASSERT_FLOAT_EQ(100, constraintList.constraints[0].width);
67   ASSERT_EQ(YGMeasureModeExactly, constraintList.constraints[0].widthMode);
68
69   free(constraintList.constraints);
70   YGNodeFreeRecursive(root);
71 }
72
73 TEST(YogaTest, exactly_measure_stretched_child_row) {
74   struct _MeasureConstraintList constraintList = _MeasureConstraintList{
75       .length = 0,
76       .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
77   };
78
79   const YGNodeRef root = YGNodeNew();
80   YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
81   YGNodeStyleSetWidth(root, 100);
82   YGNodeStyleSetHeight(root, 100);
83
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);
89
90   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
91
92   ASSERT_EQ(1, constraintList.length);
93
94   ASSERT_FLOAT_EQ(100, constraintList.constraints[0].height);
95   ASSERT_EQ(YGMeasureModeExactly, constraintList.constraints[0].heightMode);
96
97   free(constraintList.constraints);
98   YGNodeFreeRecursive(root);
99 }
100
101 TEST(YogaTest, at_most_main_axis_column) {
102   struct _MeasureConstraintList constraintList = _MeasureConstraintList{
103       .length = 0,
104       .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
105   };
106
107   const YGNodeRef root = YGNodeNew();
108   YGNodeStyleSetWidth(root, 100);
109   YGNodeStyleSetHeight(root, 100);
110
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);
115
116   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
117
118   ASSERT_EQ(1, constraintList.length);
119
120   ASSERT_FLOAT_EQ(100, constraintList.constraints[0].height);
121   ASSERT_EQ(YGMeasureModeAtMost, constraintList.constraints[0].heightMode);
122
123   free(constraintList.constraints);
124   YGNodeFreeRecursive(root);
125 }
126
127 TEST(YogaTest, at_most_cross_axis_column) {
128   struct _MeasureConstraintList constraintList = _MeasureConstraintList{
129       .length = 0,
130       .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
131   };
132
133   const YGNodeRef root = YGNodeNew();
134   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
135   YGNodeStyleSetWidth(root, 100);
136   YGNodeStyleSetHeight(root, 100);
137
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);
142
143   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
144
145   ASSERT_EQ(1, constraintList.length);
146
147   ASSERT_FLOAT_EQ(100, constraintList.constraints[0].width);
148   ASSERT_EQ(YGMeasureModeAtMost, constraintList.constraints[0].widthMode);
149
150   free(constraintList.constraints);
151   YGNodeFreeRecursive(root);
152 }
153
154 TEST(YogaTest, at_most_main_axis_row) {
155   struct _MeasureConstraintList constraintList = _MeasureConstraintList{
156       .length = 0,
157       .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
158   };
159
160   const YGNodeRef root = YGNodeNew();
161   YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
162   YGNodeStyleSetWidth(root, 100);
163   YGNodeStyleSetHeight(root, 100);
164
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);
169
170   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
171
172   ASSERT_EQ(1, constraintList.length);
173
174   ASSERT_FLOAT_EQ(100, constraintList.constraints[0].width);
175   ASSERT_EQ(YGMeasureModeAtMost, constraintList.constraints[0].widthMode);
176
177   free(constraintList.constraints);
178   YGNodeFreeRecursive(root);
179 }
180
181 TEST(YogaTest, at_most_cross_axis_row) {
182   struct _MeasureConstraintList constraintList = _MeasureConstraintList{
183       .length = 0,
184       .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
185   };
186
187   const YGNodeRef root = YGNodeNew();
188   YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
189   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
190   YGNodeStyleSetWidth(root, 100);
191   YGNodeStyleSetHeight(root, 100);
192
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);
197
198   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
199
200   ASSERT_EQ(1, constraintList.length);
201
202   ASSERT_FLOAT_EQ(100, constraintList.constraints[0].height);
203   ASSERT_EQ(YGMeasureModeAtMost, constraintList.constraints[0].heightMode);
204
205   free(constraintList.constraints);
206   YGNodeFreeRecursive(root);
207 }
208
209 TEST(YogaTest, flex_child) {
210   struct _MeasureConstraintList constraintList = _MeasureConstraintList{
211       .length = 0,
212       .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
213   };
214
215   const YGNodeRef root = YGNodeNew();
216   YGNodeStyleSetHeight(root, 100);
217
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);
223
224   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
225
226   ASSERT_EQ(2, constraintList.length);
227
228   ASSERT_FLOAT_EQ(100, constraintList.constraints[0].height);
229   ASSERT_EQ(YGMeasureModeAtMost, constraintList.constraints[0].heightMode);
230
231   ASSERT_FLOAT_EQ(100, constraintList.constraints[1].height);
232   ASSERT_EQ(YGMeasureModeExactly, constraintList.constraints[1].heightMode);
233
234   free(constraintList.constraints);
235   YGNodeFreeRecursive(root);
236 }
237
238 TEST(YogaTest, flex_child_with_flex_basis) {
239   struct _MeasureConstraintList constraintList = _MeasureConstraintList{
240       .length = 0,
241       .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
242   };
243
244   const YGNodeRef root = YGNodeNew();
245   YGNodeStyleSetHeight(root, 100);
246
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);
253
254   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
255
256   ASSERT_EQ(1, constraintList.length);
257
258   ASSERT_FLOAT_EQ(100, constraintList.constraints[0].height);
259   ASSERT_EQ(YGMeasureModeExactly, constraintList.constraints[0].heightMode);
260
261   free(constraintList.constraints);
262   YGNodeFreeRecursive(root);
263 }
264
265 TEST(YogaTest, overflow_scroll_column) {
266   struct _MeasureConstraintList constraintList = _MeasureConstraintList{
267       .length = 0,
268       .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
269   };
270
271   const YGNodeRef root = YGNodeNew();
272   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
273   YGNodeStyleSetOverflow(root, YGOverflowScroll);
274   YGNodeStyleSetHeight(root, 100);
275   YGNodeStyleSetWidth(root, 100);
276
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);
281
282   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
283
284   ASSERT_EQ(1, constraintList.length);
285
286   ASSERT_FLOAT_EQ(100, constraintList.constraints[0].width);
287   ASSERT_EQ(YGMeasureModeAtMost, constraintList.constraints[0].widthMode);
288
289   ASSERT_TRUE(YGFloatIsUndefined(constraintList.constraints[0].height));
290   ASSERT_EQ(YGMeasureModeUndefined, constraintList.constraints[0].heightMode);
291
292   free(constraintList.constraints);
293   YGNodeFreeRecursive(root);
294 }
295
296 TEST(YogaTest, overflow_scroll_row) {
297   struct _MeasureConstraintList constraintList = _MeasureConstraintList{
298       .length = 0,
299       .constraints = (struct _MeasureConstraint *) malloc(10 * sizeof(struct _MeasureConstraint)),
300   };
301
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);
308
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);
313
314   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
315
316   ASSERT_EQ(1, constraintList.length);
317
318   ASSERT_TRUE(YGFloatIsUndefined(constraintList.constraints[0].width));
319   ASSERT_EQ(YGMeasureModeUndefined, constraintList.constraints[0].widthMode);
320
321   ASSERT_FLOAT_EQ(100, constraintList.constraints[0].height);
322   ASSERT_EQ(YGMeasureModeAtMost, constraintList.constraints[0].heightMode);
323
324   free(constraintList.constraints);
325   YGNodeFreeRecursive(root);
326 }