|
| 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 | +// Regression test for https://github.com/react/yoga/issues/2006 |
| 9 | +// A row of three growable children (flexGrow/flexShrink 1, maxWidth 180) inside |
| 10 | +// a 540px container. When the *first* child has a larger minWidth than the |
| 11 | +// rest, the first free-space pass used to freeze every item at its max while |
| 12 | +// also draining the remaining free space to exactly zero, leaving the second |
| 13 | +// pass with nothing to distribute. The children then collapsed to their |
| 14 | +// minWidths (60/30/30) instead of growing to fill the row (180/180/180). |
| 15 | +// |
| 16 | +// The fix is gated on clearing YGErrataFlexFirstPassUsesRunningTotals, which |
| 17 | +// new configs set by default, so both behaviors are pinned here. |
| 18 | + |
| 19 | +#include <array> |
| 20 | + |
| 21 | +#include <gtest/gtest.h> |
| 22 | +#include <yoga/Yoga.h> |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +// A config opted into the spec-correct single distribution. |
| 27 | +YGConfigRef makeFixedConfig() { |
| 28 | + YGConfigRef config = YGConfigNew(); |
| 29 | + const YGErrata errata = YGConfigGetErrata(config); |
| 30 | + YGConfigSetErrata( |
| 31 | + config, |
| 32 | + static_cast<YGErrata>(errata & ~YGErrataFlexFirstPassUsesRunningTotals)); |
| 33 | + return config; |
| 34 | +} |
| 35 | + |
| 36 | +// Lay out a 540px row containing three children, each with flexGrow/ |
| 37 | +// flexShrink of 1, maxWidth of 180 and the given minWidths, and return the |
| 38 | +// resolved width of each child. |
| 39 | +std::array<float, 3> layoutRow( |
| 40 | + YGConfigRef config, |
| 41 | + float minWidth0, |
| 42 | + float minWidth1, |
| 43 | + float minWidth2) { |
| 44 | + YGNodeRef root = YGNodeNewWithConfig(config); |
| 45 | + YGNodeStyleSetWidth(root, 540.0f); |
| 46 | + YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow); |
| 47 | + |
| 48 | + const float minWidths[] = {minWidth0, minWidth1, minWidth2}; |
| 49 | + for (size_t i = 0; i < 3; i++) { |
| 50 | + YGNodeRef child = YGNodeNewWithConfig(config); |
| 51 | + YGNodeStyleSetFlexGrow(child, 1.0f); |
| 52 | + YGNodeStyleSetFlexShrink(child, 1.0f); |
| 53 | + YGNodeStyleSetMaxWidth(child, 180.0f); |
| 54 | + YGNodeStyleSetHeight(child, 30.0f); |
| 55 | + YGNodeStyleSetMinWidth(child, minWidths[i]); |
| 56 | + YGNodeInsertChild(root, child, i); |
| 57 | + } |
| 58 | + |
| 59 | + YGNodeCalculateLayout(root, 540.0f, 30.0f, YGDirectionLTR); |
| 60 | + |
| 61 | + std::array<float, 3> widths{}; |
| 62 | + for (size_t i = 0; i < 3; i++) { |
| 63 | + widths[i] = YGNodeLayoutGetWidth(YGNodeGetChild(root, i)); |
| 64 | + } |
| 65 | + |
| 66 | + YGNodeFreeRecursive(root); |
| 67 | + return widths; |
| 68 | +} |
| 69 | + |
| 70 | +// Assert every child grows to maxWidth (the row exactly fills the container). |
| 71 | +void expectAllGrowToMax(float minWidth0, float minWidth1, float minWidth2) { |
| 72 | + YGConfigRef config = makeFixedConfig(); |
| 73 | + const auto widths = layoutRow(config, minWidth0, minWidth1, minWidth2); |
| 74 | + |
| 75 | + for (size_t i = 0; i < 3; i++) { |
| 76 | + EXPECT_NEAR(180.0f, widths[i], 1e-3f) |
| 77 | + << "child[" << i |
| 78 | + << "] should grow to maxWidth, not collapse to its minWidth"; |
| 79 | + } |
| 80 | + |
| 81 | + YGConfigFree(config); |
| 82 | +} |
| 83 | + |
| 84 | +} // namespace |
| 85 | + |
| 86 | +TEST(YGFirstChildMinWidth, first_child_larger_minwidth_row) { |
| 87 | + // The originally reported case: the first child has a larger minWidth than |
| 88 | + // the other two, which used to break the whole row. |
| 89 | + expectAllGrowToMax(60.0f, 30.0f, 30.0f); |
| 90 | +} |
| 91 | + |
| 92 | +TEST(YGFirstChildMinWidth, other_positions_still_work) { |
| 93 | + // Sanity: placing the outlier minWidth on the second or third child already |
| 94 | + // worked and must keep working. |
| 95 | + expectAllGrowToMax(30.0f, 60.0f, 30.0f); |
| 96 | + expectAllGrowToMax(30.0f, 30.0f, 60.0f); |
| 97 | +} |
| 98 | + |
| 99 | +TEST(YGFirstChildMinWidth, uniform_minwidth_row) { |
| 100 | + // All children share the same minWidth: no issue either way. |
| 101 | + expectAllGrowToMax(30.0f, 30.0f, 30.0f); |
| 102 | +} |
| 103 | + |
| 104 | +TEST(YGFirstChildMinWidth, errata_preserves_prefix_geometry_by_default) { |
| 105 | + // A default config keeps the pre-fix collapse, so existing layouts do not |
| 106 | + // shift when this change lands. Removing the errata bit from the defaults is |
| 107 | + // what makes this test fail. |
| 108 | + YGConfigRef config = YGConfigNew(); |
| 109 | + ASSERT_NE( |
| 110 | + YGConfigGetErrata(config) & YGErrataFlexFirstPassUsesRunningTotals, 0); |
| 111 | + |
| 112 | + const auto widths = layoutRow(config, 60.0f, 30.0f, 30.0f); |
| 113 | + EXPECT_NEAR(60.0f, widths[0], 1e-3f); |
| 114 | + EXPECT_NEAR(30.0f, widths[1], 1e-3f); |
| 115 | + EXPECT_NEAR(30.0f, widths[2], 1e-3f); |
| 116 | + |
| 117 | + YGConfigFree(config); |
| 118 | +} |
| 119 | + |
| 120 | +TEST(YGFirstChildMinWidth, errata_bit_round_trips) { |
| 121 | + YGConfigRef config = YGConfigNew(); |
| 122 | + EXPECT_NE( |
| 123 | + YGConfigGetErrata(config) & YGErrataFlexFirstPassUsesRunningTotals, 0); |
| 124 | + |
| 125 | + YGConfigSetErrata( |
| 126 | + config, |
| 127 | + static_cast<YGErrata>( |
| 128 | + YGConfigGetErrata(config) & ~YGErrataFlexFirstPassUsesRunningTotals)); |
| 129 | + EXPECT_EQ( |
| 130 | + YGConfigGetErrata(config) & YGErrataFlexFirstPassUsesRunningTotals, 0); |
| 131 | + |
| 132 | + YGConfigFree(config); |
| 133 | +} |
0 commit comments