|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 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/Yoga.h> |
| 10 | +#include <algorithm> |
| 11 | + |
| 12 | +static YGSize measureTextLike( |
| 13 | + YGNodeConstRef /*node*/, |
| 14 | + float width, |
| 15 | + YGMeasureMode widthMode, |
| 16 | + float /*height*/, |
| 17 | + YGMeasureMode /*heightMode*/) { |
| 18 | + float measuredWidth = 200.0f; |
| 19 | + if (widthMode == YGMeasureModeAtMost) { |
| 20 | + measuredWidth = std::min(measuredWidth, width); |
| 21 | + } |
| 22 | + return YGSize{.width = measuredWidth, .height = 20.0f}; |
| 23 | +} |
| 24 | + |
| 25 | +class YGFlexBasisFitContentTest : public testing::TestWithParam<bool> { |
| 26 | + protected: |
| 27 | + void SetUp() override { |
| 28 | + config_ = YGConfigNew(); |
| 29 | + YGConfigSetExperimentalFeatureEnabled( |
| 30 | + config_, YGExperimentalFeatureFixFlexBasisFitContent, GetParam()); |
| 31 | + } |
| 32 | + |
| 33 | + void TearDown() override { |
| 34 | + if (root_ != nullptr) { |
| 35 | + YGNodeFreeRecursive(root_); |
| 36 | + } |
| 37 | + YGConfigFree(config_); |
| 38 | + } |
| 39 | + |
| 40 | + YGConfigRef config_ = nullptr; |
| 41 | + YGNodeRef root_ = nullptr; |
| 42 | +}; |
| 43 | + |
| 44 | +// Auto-height container with a percentage-height child produces the same |
| 45 | +// layout regardless of feature state, because Check 3 preserves percentage |
| 46 | +// resolution when availableInnerHeight is NaN. |
| 47 | +TEST_P(YGFlexBasisFitContentTest, percentage_height_converges) { |
| 48 | + root_ = YGNodeNewWithConfig(config_); |
| 49 | + YGNodeStyleSetHeight(root_, 300); |
| 50 | + YGNodeStyleSetWidth(root_, 100); |
| 51 | + |
| 52 | + YGNodeRef container = YGNodeNewWithConfig(config_); |
| 53 | + YGNodeInsertChild(root_, container, 0); |
| 54 | + |
| 55 | + YGNodeRef child = YGNodeNewWithConfig(config_); |
| 56 | + YGNodeStyleSetHeightPercent(child, 50); |
| 57 | + YGNodeInsertChild(container, child, 0); |
| 58 | + |
| 59 | + YGNodeCalculateLayout(root_, YGUndefined, YGUndefined, YGDirectionLTR); |
| 60 | + |
| 61 | + ASSERT_FLOAT_EQ(75, YGNodeLayoutGetHeight(child)); |
| 62 | + ASSERT_FLOAT_EQ(150, YGNodeLayoutGetHeight(container)); |
| 63 | +} |
| 64 | + |
| 65 | +// Two auto-height containers with percentage children and flexGrow:1 produce |
| 66 | +// the same layout regardless of feature state. |
| 67 | +TEST_P(YGFlexBasisFitContentTest, percentage_with_flex_grow_converges) { |
| 68 | + root_ = YGNodeNewWithConfig(config_); |
| 69 | + YGNodeStyleSetHeight(root_, 400); |
| 70 | + YGNodeStyleSetWidth(root_, 100); |
| 71 | + |
| 72 | + YGNodeRef containerA = YGNodeNewWithConfig(config_); |
| 73 | + YGNodeStyleSetFlexGrow(containerA, 1); |
| 74 | + YGNodeInsertChild(root_, containerA, 0); |
| 75 | + |
| 76 | + YGNodeRef childA = YGNodeNewWithConfig(config_); |
| 77 | + YGNodeStyleSetHeightPercent(childA, 25); |
| 78 | + YGNodeInsertChild(containerA, childA, 0); |
| 79 | + |
| 80 | + YGNodeRef containerB = YGNodeNewWithConfig(config_); |
| 81 | + YGNodeStyleSetFlexGrow(containerB, 1); |
| 82 | + YGNodeInsertChild(root_, containerB, 1); |
| 83 | + |
| 84 | + YGNodeRef childB = YGNodeNewWithConfig(config_); |
| 85 | + YGNodeStyleSetHeightPercent(childB, 50); |
| 86 | + YGNodeInsertChild(containerB, childB, 0); |
| 87 | + |
| 88 | + YGNodeCalculateLayout(root_, YGUndefined, YGUndefined, YGDirectionLTR); |
| 89 | + |
| 90 | + ASSERT_FLOAT_EQ(150, YGNodeLayoutGetHeight(containerA)); |
| 91 | + ASSERT_FLOAT_EQ(250, YGNodeLayoutGetHeight(containerB)); |
| 92 | +} |
| 93 | + |
| 94 | +// Auto-height container with flexShrink and a percentage child causing |
| 95 | +// overflow produces the same layout regardless of feature state. |
| 96 | +TEST_P(YGFlexBasisFitContentTest, flex_shrink_overflow_converges) { |
| 97 | + root_ = YGNodeNewWithConfig(config_); |
| 98 | + YGNodeStyleSetHeight(root_, 200); |
| 99 | + YGNodeStyleSetWidth(root_, 100); |
| 100 | + |
| 101 | + YGNodeRef container = YGNodeNewWithConfig(config_); |
| 102 | + YGNodeStyleSetFlexShrink(container, 1); |
| 103 | + YGNodeInsertChild(root_, container, 0); |
| 104 | + |
| 105 | + YGNodeRef child = YGNodeNewWithConfig(config_); |
| 106 | + YGNodeStyleSetHeightPercent(child, 100); |
| 107 | + YGNodeInsertChild(container, child, 0); |
| 108 | + |
| 109 | + YGNodeRef fixed = YGNodeNewWithConfig(config_); |
| 110 | + YGNodeStyleSetHeight(fixed, 150); |
| 111 | + YGNodeInsertChild(root_, fixed, 1); |
| 112 | + |
| 113 | + YGNodeCalculateLayout(root_, YGUndefined, YGUndefined, YGDirectionLTR); |
| 114 | + |
| 115 | + ASSERT_FLOAT_EQ(50, YGNodeLayoutGetHeight(container)); |
| 116 | + ASSERT_FLOAT_EQ(150, YGNodeLayoutGetHeight(fixed)); |
| 117 | +} |
| 118 | + |
| 119 | +// In a scroll container (column), changing a sibling's height does not cause |
| 120 | +// re-measurement of unaffected subtrees when the feature is enabled. |
| 121 | +TEST_P(YGFlexBasisFitContentTest, scroll_avoids_remeasure) { |
| 122 | + static uint32_t measureCount = 0; |
| 123 | + auto measureFunc = [](YGNodeConstRef /*node*/, |
| 124 | + float /*width*/, |
| 125 | + YGMeasureMode /*widthMode*/, |
| 126 | + float /*height*/, |
| 127 | + YGMeasureMode /*heightMode*/) { |
| 128 | + measureCount++; |
| 129 | + return YGSize{.width = 50.0f, .height = 50.0f}; |
| 130 | + }; |
| 131 | + |
| 132 | + measureCount = 0; |
| 133 | + |
| 134 | + root_ = YGNodeNewWithConfig(config_); |
| 135 | + YGNodeStyleSetOverflow(root_, YGOverflowScroll); |
| 136 | + YGNodeStyleSetWidth(root_, 100); |
| 137 | + YGNodeStyleSetHeight(root_, 500); |
| 138 | + |
| 139 | + YGNodeRef sibling = YGNodeNewWithConfig(config_); |
| 140 | + YGNodeStyleSetHeight(sibling, 100); |
| 141 | + YGNodeInsertChild(root_, sibling, 0); |
| 142 | + |
| 143 | + YGNodeRef wrapper = YGNodeNewWithConfig(config_); |
| 144 | + YGNodeInsertChild(root_, wrapper, 1); |
| 145 | + |
| 146 | + YGNodeRef inner = YGNodeNewWithConfig(config_); |
| 147 | + YGNodeInsertChild(wrapper, inner, 0); |
| 148 | + |
| 149 | + YGNodeRef leaf = YGNodeNewWithConfig(config_); |
| 150 | + YGNodeSetMeasureFunc(leaf, measureFunc); |
| 151 | + YGNodeInsertChild(inner, leaf, 0); |
| 152 | + |
| 153 | + YGNodeCalculateLayout(root_, YGUndefined, YGUndefined, YGDirectionLTR); |
| 154 | + uint32_t firstPassCount = measureCount; |
| 155 | + |
| 156 | + YGNodeStyleSetHeight(sibling, 200); |
| 157 | + YGNodeCalculateLayout(root_, YGUndefined, YGUndefined, YGDirectionLTR); |
| 158 | + uint32_t secondPassCount = measureCount - firstPassCount; |
| 159 | + |
| 160 | + ASSERT_FLOAT_EQ(50, YGNodeLayoutGetHeight(leaf)); |
| 161 | + |
| 162 | + EXPECT_EQ(0, secondPassCount); |
| 163 | +} |
| 164 | + |
| 165 | +// Row direction is unaffected by the optimization. Width FitContent is always |
| 166 | +// preserved to support text wrapping through container nodes. |
| 167 | +TEST_P(YGFlexBasisFitContentTest, row_direction_unchanged) { |
| 168 | + root_ = YGNodeNewWithConfig(config_); |
| 169 | + YGNodeStyleSetWidth(root_, 100); |
| 170 | + YGNodeStyleSetHeight(root_, 100); |
| 171 | + |
| 172 | + YGNodeRef container = YGNodeNewWithConfig(config_); |
| 173 | + YGNodeInsertChild(root_, container, 0); |
| 174 | + |
| 175 | + YGNodeRef text = YGNodeNewWithConfig(config_); |
| 176 | + YGNodeSetMeasureFunc(text, measureTextLike); |
| 177 | + YGNodeInsertChild(container, text, 0); |
| 178 | + |
| 179 | + YGNodeCalculateLayout(root_, YGUndefined, YGUndefined, YGDirectionLTR); |
| 180 | + |
| 181 | + ASSERT_FLOAT_EQ(100, YGNodeLayoutGetWidth(text)); |
| 182 | +} |
| 183 | + |
| 184 | +// Scroll container in row direction: width FitContent is skipped for the |
| 185 | +// main axis (row) in scroll containers, matching legacy behavior. |
| 186 | +TEST_P(YGFlexBasisFitContentTest, row_scroll_skips_width) { |
| 187 | + root_ = YGNodeNewWithConfig(config_); |
| 188 | + YGNodeStyleSetFlexDirection(root_, YGFlexDirectionRow); |
| 189 | + YGNodeStyleSetOverflow(root_, YGOverflowScroll); |
| 190 | + YGNodeStyleSetWidth(root_, 100); |
| 191 | + YGNodeStyleSetHeight(root_, 100); |
| 192 | + |
| 193 | + YGNodeRef text = YGNodeNewWithConfig(config_); |
| 194 | + YGNodeSetMeasureFunc(text, measureTextLike); |
| 195 | + YGNodeInsertChild(root_, text, 0); |
| 196 | + |
| 197 | + YGNodeCalculateLayout(root_, YGUndefined, YGUndefined, YGDirectionLTR); |
| 198 | + |
| 199 | + ASSERT_FLOAT_EQ(200, YGNodeLayoutGetWidth(text)); |
| 200 | +} |
| 201 | + |
| 202 | +INSTANTIATE_TEST_SUITE_P( |
| 203 | + YogaTest, |
| 204 | + YGFlexBasisFitContentTest, |
| 205 | + testing::Values(false, true)); |
| 206 | + |
| 207 | +// Feature toggle invalidates layout cache. |
| 208 | +TEST(YogaTest, flex_basis_fit_content_feature_change_invalidates_cache) { |
| 209 | + YGConfigRef config = YGConfigNew(); |
| 210 | + YGConfigSetExperimentalFeatureEnabled( |
| 211 | + config, YGExperimentalFeatureFixFlexBasisFitContent, false); |
| 212 | + |
| 213 | + YGNodeRef root = YGNodeNewWithConfig(config); |
| 214 | + YGNodeStyleSetHeight(root, 300); |
| 215 | + YGNodeStyleSetWidth(root, 100); |
| 216 | + |
| 217 | + YGNodeRef container = YGNodeNewWithConfig(config); |
| 218 | + YGNodeStyleSetFlexGrow(container, 1); |
| 219 | + YGNodeInsertChild(root, container, 0); |
| 220 | + |
| 221 | + YGNodeRef child = YGNodeNewWithConfig(config); |
| 222 | + YGNodeStyleSetHeightPercent(child, 50); |
| 223 | + YGNodeInsertChild(container, child, 0); |
| 224 | + |
| 225 | + YGNodeRef fixed = YGNodeNewWithConfig(config); |
| 226 | + YGNodeStyleSetHeight(fixed, 100); |
| 227 | + YGNodeInsertChild(root, fixed, 1); |
| 228 | + |
| 229 | + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); |
| 230 | + float heightBefore = YGNodeLayoutGetHeight(container); |
| 231 | + |
| 232 | + YGConfigSetExperimentalFeatureEnabled( |
| 233 | + config, YGExperimentalFeatureFixFlexBasisFitContent, true); |
| 234 | + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); |
| 235 | + float heightAfter = YGNodeLayoutGetHeight(container); |
| 236 | + |
| 237 | + ASSERT_FLOAT_EQ(heightBefore, heightAfter); |
| 238 | + |
| 239 | + YGNodeFreeRecursive(root); |
| 240 | + YGConfigFree(config); |
| 241 | +} |
0 commit comments