Merge "Add new layouting support for TextLabel and ImageView." into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-third-party / yoga / YGMeasureCacheTest.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 _measureMax(YGNodeRef node,
13                           float width,
14                           YGMeasureMode widthMode,
15                           float height,
16                           YGMeasureMode heightMode) {
17   int* measureCount = (int*)node->getContext();
18   (*measureCount)++;
19
20   return YGSize{
21       .width = widthMode == YGMeasureModeUndefined ? 10 : width,
22       .height = heightMode == YGMeasureModeUndefined ? 10 : height,
23   };
24 }
25
26 static YGSize _measureMin(YGNodeRef node,
27                           float width,
28                           YGMeasureMode widthMode,
29                           float height,
30                           YGMeasureMode heightMode) {
31   int* measureCount = (int*)node->getContext();
32   *measureCount = *measureCount + 1;
33   return YGSize{
34       .width =
35           widthMode == YGMeasureModeUndefined || (widthMode == YGMeasureModeAtMost && width > 10)
36               ? 10
37               : width,
38       .height =
39           heightMode == YGMeasureModeUndefined || (heightMode == YGMeasureModeAtMost && height > 10)
40               ? 10
41               : height,
42   };
43 }
44
45 static YGSize _measure_84_49(YGNodeRef node,
46                              float width,
47                              YGMeasureMode widthMode,
48                              float height,
49                              YGMeasureMode heightMode) {
50   int* measureCount = (int*)node->getContext();
51   if (measureCount) {
52     (*measureCount)++;
53   }
54
55   return YGSize{
56       .width = 84.f, .height = 49.f,
57   };
58 }
59
60 TEST(YogaTest, measure_once_single_flexible_child) {
61   const YGNodeRef root = YGNodeNew();
62   YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
63   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
64   YGNodeStyleSetWidth(root, 100);
65   YGNodeStyleSetHeight(root, 100);
66
67   const YGNodeRef root_child0 = YGNodeNew();
68   int measureCount = 0;
69   root_child0->setContext(&measureCount);
70   root_child0->setMeasureFunc(_measureMax);
71   YGNodeStyleSetFlexGrow(root_child0, 1);
72   YGNodeInsertChild(root, root_child0, 0);
73
74   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
75
76   ASSERT_EQ(1, measureCount);
77
78   YGNodeFreeRecursive(root);
79 }
80
81 TEST(YogaTest, remeasure_with_same_exact_width_larger_than_needed_height) {
82   const YGNodeRef root = YGNodeNew();
83
84   const YGNodeRef root_child0 = YGNodeNew();
85   int measureCount = 0;
86   root_child0->setContext(&measureCount);
87   root_child0->setMeasureFunc(_measureMin);
88   YGNodeInsertChild(root, root_child0, 0);
89
90   YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
91   YGNodeCalculateLayout(root, 100, 50, YGDirectionLTR);
92
93   ASSERT_EQ(1, measureCount);
94
95   YGNodeFreeRecursive(root);
96 }
97
98 TEST(YogaTest, remeasure_with_same_atmost_width_larger_than_needed_height) {
99   const YGNodeRef root = YGNodeNew();
100   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
101
102   const YGNodeRef root_child0 = YGNodeNew();
103   int measureCount = 0;
104   root_child0->setContext(&measureCount);
105   root_child0->setMeasureFunc(_measureMin);
106   YGNodeInsertChild(root, root_child0, 0);
107
108   YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
109   YGNodeCalculateLayout(root, 100, 50, YGDirectionLTR);
110
111   ASSERT_EQ(1, measureCount);
112
113   YGNodeFreeRecursive(root);
114 }
115
116 TEST(YogaTest, remeasure_with_computed_width_larger_than_needed_height) {
117   const YGNodeRef root = YGNodeNew();
118   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
119
120   const YGNodeRef root_child0 = YGNodeNew();
121   int measureCount = 0;
122   root_child0->setContext(&measureCount);
123   root_child0->setMeasureFunc(_measureMin);
124   YGNodeInsertChild(root, root_child0, 0);
125
126   YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
127   YGNodeStyleSetAlignItems(root, YGAlignStretch);
128   YGNodeCalculateLayout(root, 10, 50, YGDirectionLTR);
129
130   ASSERT_EQ(1, measureCount);
131
132   YGNodeFreeRecursive(root);
133 }
134
135 TEST(YogaTest, remeasure_with_atmost_computed_width_undefined_height) {
136   const YGNodeRef root = YGNodeNew();
137   YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
138
139   const YGNodeRef root_child0 = YGNodeNew();
140   int measureCount = 0;
141   root_child0->setContext(&measureCount);
142   root_child0->setMeasureFunc(_measureMin);
143   YGNodeInsertChild(root, root_child0, 0);
144
145   YGNodeCalculateLayout(root, 100, YGUndefined, YGDirectionLTR);
146   YGNodeCalculateLayout(root, 10, YGUndefined, YGDirectionLTR);
147
148   ASSERT_EQ(1, measureCount);
149
150   YGNodeFreeRecursive(root);
151 }
152
153 TEST(YogaTest, remeasure_with_already_measured_value_smaller_but_still_float_equal) {
154   int measureCount = 0;
155
156   const YGNodeRef root = YGNodeNew();
157   YGNodeStyleSetWidth(root, 288.f);
158   YGNodeStyleSetHeight(root, 288.f);
159   YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
160
161   const YGNodeRef root_child0 = YGNodeNew();
162   YGNodeStyleSetPadding(root_child0, YGEdgeAll, 2.88f);
163   YGNodeStyleSetFlexDirection(root_child0, YGFlexDirectionRow);
164   YGNodeInsertChild(root, root_child0, 0);
165
166   const YGNodeRef root_child0_child0 = YGNodeNew();
167   root_child0_child0->setContext(&measureCount);
168   root_child0_child0->setMeasureFunc(_measure_84_49);
169   YGNodeInsertChild(root_child0, root_child0_child0, 0);
170
171   YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
172
173   YGNodeFreeRecursive(root);
174
175   ASSERT_EQ(1, measureCount);
176 }