-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Layout breaks when the first element has a different minWidth rest #2021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+87
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
tests/YGFirstChildMinWidthTest.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| // Regression test for https://github.com/react/yoga/issues/2006 | ||
| // A row of three growable children (flexGrow/flexShrink 1, maxWidth 180) inside | ||
| // a 540px container. When the *first* child has a larger minWidth than the | ||
| // rest, the first free-space pass used to freeze every item at its max while | ||
| // also draining the remaining free space to exactly zero, leaving the second | ||
| // pass with nothing to distribute. The children then collapsed to their | ||
| // minWidths (60/30/30) instead of growing to fill the row (180/180/180). | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <yoga/Yoga.h> | ||
|
|
||
| namespace { | ||
|
|
||
| // Lay out a 540px row containing three children, each with flexGrow/ | ||
| // flexShrink of 1, maxWidth of `maxW` and the given minWidths, and assert | ||
| // every child grows to maxWidth (the row exactly fills the container). | ||
| void expectAllGrowToMax(float minWidth0, float minWidth1, float minWidth2) { | ||
| YGConfigRef config = YGConfigNew(); | ||
| YGNodeRef root = YGNodeNewWithConfig(config); | ||
| YGNodeStyleSetWidth(root, 540.0f); | ||
| YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow); | ||
|
|
||
| float minWidths[] = {minWidth0, minWidth1, minWidth2}; | ||
| for (size_t i = 0; i < 3; i++) { | ||
| YGNodeRef child = YGNodeNewWithConfig(config); | ||
| YGNodeStyleSetFlexGrow(child, 1.0f); | ||
| YGNodeStyleSetFlexShrink(child, 1.0f); | ||
| YGNodeStyleSetMaxWidth(child, 180.0f); | ||
| YGNodeStyleSetHeight(child, 30.0f); | ||
| YGNodeStyleSetMinWidth(child, minWidths[i]); | ||
| YGNodeInsertChild(root, child, i); | ||
| } | ||
|
|
||
| YGNodeCalculateLayout(root, 540.0f, 30.0f, YGDirectionLTR); | ||
|
|
||
| for (size_t i = 0; i < 3; i++) { | ||
| YGNodeRef child = YGNodeGetChild(root, i); | ||
| EXPECT_NEAR(180.0f, YGNodeLayoutGetWidth(child), 1e-3f) | ||
| << "child[" << i | ||
| << "] should grow to maxWidth, not collapse to its minWidth"; | ||
| } | ||
|
|
||
| YGNodeFreeRecursive(root); | ||
| YGConfigFree(config); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST(YGFirstChildMinWidth, first_child_larger_minwidth_row) { | ||
| // The originally reported case: the first child has a larger minWidth than | ||
| // the other two, which used to break the whole row. | ||
| expectAllGrowToMax(60.0f, 30.0f, 30.0f); | ||
| } | ||
|
|
||
| TEST(YGFirstChildMinWidth, other_positions_still_work) { | ||
| // Sanity: placing the outlier minWidth on the second or third child already | ||
| // worked and must keep working. | ||
| expectAllGrowToMax(30.0f, 60.0f, 30.0f); | ||
| expectAllGrowToMax(30.0f, 30.0f, 60.0f); | ||
| } | ||
|
|
||
| TEST(YGFirstChildMinWidth, uniform_minwidth_row) { | ||
| // All children share the same minWidth: no issue either way. | ||
| expectAllGrowToMax(30.0f, 30.0f, 30.0f); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.