2019-06-06 19:36:56 -07:00
|
|
|
/*
|
2022-10-04 13:59:32 -07:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2016-08-31 04:58:35 -07:00
|
|
|
*
|
2019-10-15 10:30:08 -07:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-08-31 04:58:35 -07:00
|
|
|
*/
|
2019-10-15 10:30:08 -07:00
|
|
|
|
2016-08-31 04:58:35 -07:00
|
|
|
#include <gtest/gtest.h>
|
2023-08-29 23:27:25 -07:00
|
|
|
#include <yoga/YGNode.h>
|
2017-01-31 09:28:10 -08:00
|
|
|
#include <yoga/Yoga.h>
|
2016-08-31 04:58:35 -07:00
|
|
|
|
2019-03-25 05:37:36 -07:00
|
|
|
static YGSize _measureMax(
|
|
|
|
YGNodeRef node,
|
|
|
|
float width,
|
|
|
|
YGMeasureMode widthMode,
|
|
|
|
float height,
|
|
|
|
YGMeasureMode heightMode) {
|
2023-08-29 23:27:25 -07:00
|
|
|
int* measureCount = (int*) node->getContext();
|
2016-11-09 11:23:21 -08:00
|
|
|
(*measureCount)++;
|
|
|
|
|
2017-01-31 09:28:10 -08:00
|
|
|
return YGSize{
|
2023-01-09 13:59:19 -08:00
|
|
|
widthMode == YGMeasureModeUndefined ? 10 : width,
|
|
|
|
heightMode == YGMeasureModeUndefined ? 10 : height,
|
2016-10-25 07:33:58 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-25 05:37:36 -07:00
|
|
|
static YGSize _measureMin(
|
|
|
|
YGNodeRef node,
|
|
|
|
float width,
|
|
|
|
YGMeasureMode widthMode,
|
|
|
|
float height,
|
|
|
|
YGMeasureMode heightMode) {
|
2023-08-29 23:27:25 -07:00
|
|
|
int* measureCount = (int*) node->getContext();
|
2016-10-25 07:33:58 -07:00
|
|
|
*measureCount = *measureCount + 1;
|
2017-01-31 09:28:10 -08:00
|
|
|
return YGSize{
|
2023-01-09 13:59:19 -08:00
|
|
|
widthMode == YGMeasureModeUndefined ||
|
2019-03-25 05:37:36 -07:00
|
|
|
(widthMode == YGMeasureModeAtMost && width > 10)
|
|
|
|
? 10
|
|
|
|
: width,
|
2023-01-09 13:59:19 -08:00
|
|
|
heightMode == YGMeasureModeUndefined ||
|
2019-03-25 05:37:36 -07:00
|
|
|
(heightMode == YGMeasureModeAtMost && height > 10)
|
|
|
|
? 10
|
|
|
|
: height,
|
2016-08-31 04:58:35 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-25 05:37:36 -07:00
|
|
|
static YGSize _measure_84_49(
|
|
|
|
YGNodeRef node,
|
2023-05-11 09:43:36 -07:00
|
|
|
float /*width*/,
|
|
|
|
YGMeasureMode /*widthMode*/,
|
|
|
|
float /*height*/,
|
|
|
|
YGMeasureMode /*heightMode*/) {
|
2023-08-29 23:27:25 -07:00
|
|
|
int* measureCount = (int*) node->getContext();
|
2017-01-24 18:57:07 -08:00
|
|
|
if (measureCount) {
|
|
|
|
(*measureCount)++;
|
|
|
|
}
|
|
|
|
|
2023-01-09 13:59:19 -08:00
|
|
|
return YGSize{84.f, 49.f};
|
2017-01-24 18:57:07 -08:00
|
|
|
}
|
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
TEST(YogaTest, measure_once_single_flexible_child) {
|
|
|
|
const YGNodeRef root = YGNodeNew();
|
|
|
|
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
|
|
|
|
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
|
|
|
|
YGNodeStyleSetWidth(root, 100);
|
|
|
|
YGNodeStyleSetHeight(root, 100);
|
2016-08-31 04:58:35 -07:00
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef root_child0 = YGNodeNew();
|
2016-10-25 07:33:58 -07:00
|
|
|
int measureCount = 0;
|
2023-08-29 23:27:25 -07:00
|
|
|
root_child0->setContext(&measureCount);
|
|
|
|
root_child0->setMeasureFunc(_measureMax);
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeStyleSetFlexGrow(root_child0, 1);
|
|
|
|
YGNodeInsertChild(root, root_child0, 0);
|
2016-08-31 04:58:35 -07:00
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
|
2016-10-25 07:33:58 -07:00
|
|
|
|
|
|
|
ASSERT_EQ(1, measureCount);
|
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeFreeRecursive(root);
|
2016-10-25 07:33:58 -07:00
|
|
|
}
|
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
TEST(YogaTest, remeasure_with_same_exact_width_larger_than_needed_height) {
|
|
|
|
const YGNodeRef root = YGNodeNew();
|
2016-10-25 07:33:58 -07:00
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef root_child0 = YGNodeNew();
|
2016-10-25 07:33:58 -07:00
|
|
|
int measureCount = 0;
|
2023-08-29 23:27:25 -07:00
|
|
|
root_child0->setContext(&measureCount);
|
|
|
|
root_child0->setMeasureFunc(_measureMin);
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeInsertChild(root, root_child0, 0);
|
2016-10-25 07:33:58 -07:00
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
|
|
|
|
YGNodeCalculateLayout(root, 100, 50, YGDirectionLTR);
|
2016-08-31 04:58:35 -07:00
|
|
|
|
|
|
|
ASSERT_EQ(1, measureCount);
|
2016-09-29 04:09:56 -07:00
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeFreeRecursive(root);
|
2016-08-31 04:58:35 -07:00
|
|
|
}
|
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
TEST(YogaTest, remeasure_with_same_atmost_width_larger_than_needed_height) {
|
|
|
|
const YGNodeRef root = YGNodeNew();
|
|
|
|
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
|
2016-08-31 04:58:35 -07:00
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef root_child0 = YGNodeNew();
|
2016-10-25 07:33:58 -07:00
|
|
|
int measureCount = 0;
|
2023-08-29 23:27:25 -07:00
|
|
|
root_child0->setContext(&measureCount);
|
|
|
|
root_child0->setMeasureFunc(_measureMin);
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeInsertChild(root, root_child0, 0);
|
2016-08-31 04:58:35 -07:00
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
|
|
|
|
YGNodeCalculateLayout(root, 100, 50, YGDirectionLTR);
|
2016-10-25 07:33:58 -07:00
|
|
|
|
|
|
|
ASSERT_EQ(1, measureCount);
|
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeFreeRecursive(root);
|
2016-10-25 07:33:58 -07:00
|
|
|
}
|
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
TEST(YogaTest, remeasure_with_computed_width_larger_than_needed_height) {
|
|
|
|
const YGNodeRef root = YGNodeNew();
|
|
|
|
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
|
2016-10-25 07:33:58 -07:00
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef root_child0 = YGNodeNew();
|
2016-08-31 04:58:35 -07:00
|
|
|
int measureCount = 0;
|
2023-08-29 23:27:25 -07:00
|
|
|
root_child0->setContext(&measureCount);
|
|
|
|
root_child0->setMeasureFunc(_measureMin);
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeInsertChild(root, root_child0, 0);
|
2016-08-31 04:58:35 -07:00
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);
|
|
|
|
YGNodeStyleSetAlignItems(root, YGAlignStretch);
|
|
|
|
YGNodeCalculateLayout(root, 10, 50, YGDirectionLTR);
|
2016-08-31 04:58:35 -07:00
|
|
|
|
2016-10-25 07:33:58 -07:00
|
|
|
ASSERT_EQ(1, measureCount);
|
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeFreeRecursive(root);
|
2016-10-25 07:33:58 -07:00
|
|
|
}
|
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
TEST(YogaTest, remeasure_with_atmost_computed_width_undefined_height) {
|
|
|
|
const YGNodeRef root = YGNodeNew();
|
|
|
|
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
|
2016-10-25 07:33:58 -07:00
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef root_child0 = YGNodeNew();
|
2016-10-25 07:33:58 -07:00
|
|
|
int measureCount = 0;
|
2023-08-29 23:27:25 -07:00
|
|
|
root_child0->setContext(&measureCount);
|
|
|
|
root_child0->setMeasureFunc(_measureMin);
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeInsertChild(root, root_child0, 0);
|
2016-10-25 07:33:58 -07:00
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeCalculateLayout(root, 100, YGUndefined, YGDirectionLTR);
|
|
|
|
YGNodeCalculateLayout(root, 10, YGUndefined, YGDirectionLTR);
|
2016-08-31 04:58:35 -07:00
|
|
|
|
|
|
|
ASSERT_EQ(1, measureCount);
|
2016-09-29 04:09:56 -07:00
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeFreeRecursive(root);
|
2016-08-31 04:58:35 -07:00
|
|
|
}
|
2017-01-24 18:57:07 -08:00
|
|
|
|
2019-03-25 05:37:36 -07:00
|
|
|
TEST(
|
|
|
|
YogaTest,
|
|
|
|
remeasure_with_already_measured_value_smaller_but_still_float_equal) {
|
2017-01-24 18:57:07 -08:00
|
|
|
int measureCount = 0;
|
|
|
|
|
|
|
|
const YGNodeRef root = YGNodeNew();
|
|
|
|
YGNodeStyleSetWidth(root, 288.f);
|
|
|
|
YGNodeStyleSetHeight(root, 288.f);
|
|
|
|
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
|
2017-01-31 09:28:10 -08:00
|
|
|
|
2017-01-24 18:57:07 -08:00
|
|
|
const YGNodeRef root_child0 = YGNodeNew();
|
|
|
|
YGNodeStyleSetPadding(root_child0, YGEdgeAll, 2.88f);
|
|
|
|
YGNodeStyleSetFlexDirection(root_child0, YGFlexDirectionRow);
|
|
|
|
YGNodeInsertChild(root, root_child0, 0);
|
2017-01-31 09:28:10 -08:00
|
|
|
|
2017-01-24 18:57:07 -08:00
|
|
|
const YGNodeRef root_child0_child0 = YGNodeNew();
|
2023-08-29 23:27:25 -07:00
|
|
|
root_child0_child0->setContext(&measureCount);
|
|
|
|
root_child0_child0->setMeasureFunc(_measure_84_49);
|
2017-01-24 18:57:07 -08:00
|
|
|
YGNodeInsertChild(root_child0, root_child0_child0, 0);
|
2017-01-31 09:28:10 -08:00
|
|
|
|
2017-01-24 18:57:07 -08:00
|
|
|
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
|
2017-01-31 09:28:10 -08:00
|
|
|
|
2017-01-24 18:57:07 -08:00
|
|
|
YGNodeFreeRecursive(root);
|
|
|
|
|
|
|
|
ASSERT_EQ(1, measureCount);
|
|
|
|
}
|