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 _measureMax(YGNodeRef node,
14 YGMeasureMode widthMode,
16 YGMeasureMode heightMode) {
17 int* measureCount = (int*)node->getContext();
21 .width = widthMode == YGMeasureModeUndefined ? 10 : width,
22 .height = heightMode == YGMeasureModeUndefined ? 10 : height,
26 static YGSize _measureMin(YGNodeRef node,
28 YGMeasureMode widthMode,
30 YGMeasureMode heightMode) {
31 int* measureCount = (int*)node->getContext();
32 *measureCount = *measureCount + 1;
35 widthMode == YGMeasureModeUndefined || (widthMode == YGMeasureModeAtMost && width > 10)
39 heightMode == YGMeasureModeUndefined || (heightMode == YGMeasureModeAtMost && height > 10)
45 static YGSize _measure_84_49(YGNodeRef node,
47 YGMeasureMode widthMode,
49 YGMeasureMode heightMode) {
50 int* measureCount = (int*)node->getContext();
56 .width = 84.f, .height = 49.f,
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);
67 const YGNodeRef root_child0 = YGNodeNew();
69 root_child0->setContext(&measureCount);
70 root_child0->setMeasureFunc(_measureMax);
71 YGNodeStyleSetFlexGrow(root_child0, 1);
72 YGNodeInsertChild(root, root_child0, 0);
74 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
76 ASSERT_EQ(1, measureCount);
78 YGNodeFreeRecursive(root);
81 TEST(YogaTest, remeasure_with_same_exact_width_larger_than_needed_height) {
82 const YGNodeRef root = YGNodeNew();
84 const YGNodeRef root_child0 = YGNodeNew();
86 root_child0->setContext(&measureCount);
87 root_child0->setMeasureFunc(_measureMin);
88 YGNodeInsertChild(root, root_child0, 0);
90 YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
91 YGNodeCalculateLayout(root, 100, 50, YGDirectionLTR);
93 ASSERT_EQ(1, measureCount);
95 YGNodeFreeRecursive(root);
98 TEST(YogaTest, remeasure_with_same_atmost_width_larger_than_needed_height) {
99 const YGNodeRef root = YGNodeNew();
100 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
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);
108 YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
109 YGNodeCalculateLayout(root, 100, 50, YGDirectionLTR);
111 ASSERT_EQ(1, measureCount);
113 YGNodeFreeRecursive(root);
116 TEST(YogaTest, remeasure_with_computed_width_larger_than_needed_height) {
117 const YGNodeRef root = YGNodeNew();
118 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
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);
126 YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
127 YGNodeStyleSetAlignItems(root, YGAlignStretch);
128 YGNodeCalculateLayout(root, 10, 50, YGDirectionLTR);
130 ASSERT_EQ(1, measureCount);
132 YGNodeFreeRecursive(root);
135 TEST(YogaTest, remeasure_with_atmost_computed_width_undefined_height) {
136 const YGNodeRef root = YGNodeNew();
137 YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
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);
145 YGNodeCalculateLayout(root, 100, YGUndefined, YGDirectionLTR);
146 YGNodeCalculateLayout(root, 10, YGUndefined, YGDirectionLTR);
148 ASSERT_EQ(1, measureCount);
150 YGNodeFreeRecursive(root);
153 TEST(YogaTest, remeasure_with_already_measured_value_smaller_but_still_float_equal) {
154 int measureCount = 0;
156 const YGNodeRef root = YGNodeNew();
157 YGNodeStyleSetWidth(root, 288.f);
158 YGNodeStyleSetHeight(root, 288.f);
159 YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
161 const YGNodeRef root_child0 = YGNodeNew();
162 YGNodeStyleSetPadding(root_child0, YGEdgeAll, 2.88f);
163 YGNodeStyleSetFlexDirection(root_child0, YGFlexDirectionRow);
164 YGNodeInsertChild(root, root_child0, 0);
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);
171 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
173 YGNodeFreeRecursive(root);
175 ASSERT_EQ(1, measureCount);