Skip to content

Commit 85c2e40

Browse files
Emil Sjolanderfacebook-github-bot
authored andcommitted
Fix flex basis not accounting for max size constraint
Summary: Fix flex basis not being constraint to the max size in the main direction. Previously this caused the added test to fail due to NaN in child dimensions. Reviewed By: gkassabli Differential Revision: D5044314 fbshipit-source-id: d9f9db832e4943a57a89c9d162ff6077b709795a
1 parent dcf57d2 commit 85c2e40

File tree

8 files changed

+305
-48
lines changed

8 files changed

+305
-48
lines changed

csharp/tests/Facebook.Yoga/YGMinMaxDimensionTest.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,64 @@ public void Test_flex_grow_within_constrained_max_column()
905905
Assert.AreEqual(50f, root_child1.LayoutHeight);
906906
}
907907

908+
[Test]
909+
public void Test_child_min_max_width_flexing()
910+
{
911+
YogaConfig config = new YogaConfig();
912+
913+
YogaNode root = new YogaNode(config);
914+
root.FlexDirection = YogaFlexDirection.Row;
915+
root.Width = 120;
916+
root.Height = 50;
917+
918+
YogaNode root_child0 = new YogaNode(config);
919+
root_child0.FlexGrow = 1;
920+
root_child0.FlexBasis = 0;
921+
root_child0.MinWidth = 60;
922+
root.Insert(0, root_child0);
923+
924+
YogaNode root_child1 = new YogaNode(config);
925+
root_child1.FlexGrow = 1;
926+
root_child1.FlexBasis = 50.Percent();
927+
root_child1.MaxWidth = 20;
928+
root.Insert(1, root_child1);
929+
root.StyleDirection = YogaDirection.LTR;
930+
root.CalculateLayout();
931+
932+
Assert.AreEqual(0f, root.LayoutX);
933+
Assert.AreEqual(0f, root.LayoutY);
934+
Assert.AreEqual(120f, root.LayoutWidth);
935+
Assert.AreEqual(50f, root.LayoutHeight);
936+
937+
Assert.AreEqual(0f, root_child0.LayoutX);
938+
Assert.AreEqual(0f, root_child0.LayoutY);
939+
Assert.AreEqual(100f, root_child0.LayoutWidth);
940+
Assert.AreEqual(50f, root_child0.LayoutHeight);
941+
942+
Assert.AreEqual(100f, root_child1.LayoutX);
943+
Assert.AreEqual(0f, root_child1.LayoutY);
944+
Assert.AreEqual(20f, root_child1.LayoutWidth);
945+
Assert.AreEqual(50f, root_child1.LayoutHeight);
946+
947+
root.StyleDirection = YogaDirection.RTL;
948+
root.CalculateLayout();
949+
950+
Assert.AreEqual(0f, root.LayoutX);
951+
Assert.AreEqual(0f, root.LayoutY);
952+
Assert.AreEqual(120f, root.LayoutWidth);
953+
Assert.AreEqual(50f, root.LayoutHeight);
954+
955+
Assert.AreEqual(20f, root_child0.LayoutX);
956+
Assert.AreEqual(0f, root_child0.LayoutY);
957+
Assert.AreEqual(100f, root_child0.LayoutWidth);
958+
Assert.AreEqual(50f, root_child0.LayoutHeight);
959+
960+
Assert.AreEqual(0f, root_child1.LayoutX);
961+
Assert.AreEqual(0f, root_child1.LayoutY);
962+
Assert.AreEqual(20f, root_child1.LayoutWidth);
963+
Assert.AreEqual(50f, root_child1.LayoutHeight);
964+
}
965+
908966
[Test]
909967
public void Test_min_width_overrides_width()
910968
{

gentest/fixtures/YGMinMaxDimensionTest.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@
8484
<div style="height: 50px;"></div>
8585
</div>
8686

87+
<div id="child_min_max_width_flexing" style="width: 120px; height: 50px; flex-direction: row; align-items:stretch">
88+
<div style="min-width: 60px; flex-grow:1; flex-shrink:0; flex-basis: 0px"></div>
89+
<div style="max-width: 20px; flex-grow:1; flex-shrink:0; flex-basis: 50%"></div>
90+
</div>
91+
8792
<div id="min_width_overrides_width" style="min-width: 100px; width: 50px;">
8893
</div>
8994

java/jni/YGJNI.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ struct JYogaLogLevel : public JavaClass<JYogaLogLevel> {
153153
};
154154

155155
static int YGJNILogFunc(const YGConfigRef config,
156-
const YGNodeRef node,
157-
YGLogLevel level,
158-
const char *format,
159-
va_list args) {
156+
const YGNodeRef node,
157+
YGLogLevel level,
158+
const char *format,
159+
va_list args) {
160160
char buffer[256];
161161
int result = vsnprintf(buffer, sizeof(buffer), format, args);
162162

java/tests/com/facebook/yoga/YGMinMaxDimensionTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,63 @@ public void test_flex_grow_within_constrained_max_column() {
887887
assertEquals(50f, root_child1.getLayoutHeight(), 0.0f);
888888
}
889889

890+
@Test
891+
public void test_child_min_max_width_flexing() {
892+
YogaConfig config = new YogaConfig();
893+
894+
final YogaNode root = new YogaNode(config);
895+
root.setFlexDirection(YogaFlexDirection.ROW);
896+
root.setWidth(120f);
897+
root.setHeight(50f);
898+
899+
final YogaNode root_child0 = new YogaNode(config);
900+
root_child0.setFlexGrow(1f);
901+
root_child0.setFlexBasis(0f);
902+
root_child0.setMinWidth(60f);
903+
root.addChildAt(root_child0, 0);
904+
905+
final YogaNode root_child1 = new YogaNode(config);
906+
root_child1.setFlexGrow(1f);
907+
root_child1.setFlexBasisPercent(50f);
908+
root_child1.setMaxWidth(20f);
909+
root.addChildAt(root_child1, 1);
910+
root.setDirection(YogaDirection.LTR);
911+
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
912+
913+
assertEquals(0f, root.getLayoutX(), 0.0f);
914+
assertEquals(0f, root.getLayoutY(), 0.0f);
915+
assertEquals(120f, root.getLayoutWidth(), 0.0f);
916+
assertEquals(50f, root.getLayoutHeight(), 0.0f);
917+
918+
assertEquals(0f, root_child0.getLayoutX(), 0.0f);
919+
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
920+
assertEquals(100f, root_child0.getLayoutWidth(), 0.0f);
921+
assertEquals(50f, root_child0.getLayoutHeight(), 0.0f);
922+
923+
assertEquals(100f, root_child1.getLayoutX(), 0.0f);
924+
assertEquals(0f, root_child1.getLayoutY(), 0.0f);
925+
assertEquals(20f, root_child1.getLayoutWidth(), 0.0f);
926+
assertEquals(50f, root_child1.getLayoutHeight(), 0.0f);
927+
928+
root.setDirection(YogaDirection.RTL);
929+
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
930+
931+
assertEquals(0f, root.getLayoutX(), 0.0f);
932+
assertEquals(0f, root.getLayoutY(), 0.0f);
933+
assertEquals(120f, root.getLayoutWidth(), 0.0f);
934+
assertEquals(50f, root.getLayoutHeight(), 0.0f);
935+
936+
assertEquals(20f, root_child0.getLayoutX(), 0.0f);
937+
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
938+
assertEquals(100f, root_child0.getLayoutWidth(), 0.0f);
939+
assertEquals(50f, root_child0.getLayoutHeight(), 0.0f);
940+
941+
assertEquals(0f, root_child1.getLayoutX(), 0.0f);
942+
assertEquals(0f, root_child1.getLayoutY(), 0.0f);
943+
assertEquals(20f, root_child1.getLayoutWidth(), 0.0f);
944+
assertEquals(50f, root_child1.getLayoutHeight(), 0.0f);
945+
}
946+
890947
@Test
891948
public void test_min_width_overrides_width() {
892949
YogaConfig config = new YogaConfig();

javascript/tests/Facebook.Yoga/YGMinMaxDimensionTest.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,67 @@ it("flex_grow_within_constrained_max_column", function () {
950950
config.free();
951951
}
952952
});
953+
it("child_min_max_width_flexing", function () {
954+
var config = Yoga.Config.create();
955+
956+
try {
957+
var root = Yoga.Node.create(config);
958+
root.setFlexDirection(Yoga.FLEX_DIRECTION_ROW);
959+
root.setWidth(120);
960+
root.setHeight(50);
961+
962+
var root_child0 = Yoga.Node.create(config);
963+
root_child0.setFlexGrow(1);
964+
root_child0.setFlexBasis(0);
965+
root_child0.setMinWidth(60);
966+
root.insertChild(root_child0, 0);
967+
968+
var root_child1 = Yoga.Node.create(config);
969+
root_child1.setFlexGrow(1);
970+
root_child1.setFlexBasis("50%");
971+
root_child1.setMaxWidth(20);
972+
root.insertChild(root_child1, 1);
973+
root.calculateLayout(Yoga.UNDEFINED, Yoga.UNDEFINED, Yoga.DIRECTION_LTR);
974+
975+
console.assert(0 === root.getComputedLeft(), "0 === root.getComputedLeft() (" + root.getComputedLeft() + ")");
976+
console.assert(0 === root.getComputedTop(), "0 === root.getComputedTop() (" + root.getComputedTop() + ")");
977+
console.assert(120 === root.getComputedWidth(), "120 === root.getComputedWidth() (" + root.getComputedWidth() + ")");
978+
console.assert(50 === root.getComputedHeight(), "50 === root.getComputedHeight() (" + root.getComputedHeight() + ")");
979+
980+
console.assert(0 === root_child0.getComputedLeft(), "0 === root_child0.getComputedLeft() (" + root_child0.getComputedLeft() + ")");
981+
console.assert(0 === root_child0.getComputedTop(), "0 === root_child0.getComputedTop() (" + root_child0.getComputedTop() + ")");
982+
console.assert(100 === root_child0.getComputedWidth(), "100 === root_child0.getComputedWidth() (" + root_child0.getComputedWidth() + ")");
983+
console.assert(50 === root_child0.getComputedHeight(), "50 === root_child0.getComputedHeight() (" + root_child0.getComputedHeight() + ")");
984+
985+
console.assert(100 === root_child1.getComputedLeft(), "100 === root_child1.getComputedLeft() (" + root_child1.getComputedLeft() + ")");
986+
console.assert(0 === root_child1.getComputedTop(), "0 === root_child1.getComputedTop() (" + root_child1.getComputedTop() + ")");
987+
console.assert(20 === root_child1.getComputedWidth(), "20 === root_child1.getComputedWidth() (" + root_child1.getComputedWidth() + ")");
988+
console.assert(50 === root_child1.getComputedHeight(), "50 === root_child1.getComputedHeight() (" + root_child1.getComputedHeight() + ")");
989+
990+
root.calculateLayout(Yoga.UNDEFINED, Yoga.UNDEFINED, Yoga.DIRECTION_RTL);
991+
992+
console.assert(0 === root.getComputedLeft(), "0 === root.getComputedLeft() (" + root.getComputedLeft() + ")");
993+
console.assert(0 === root.getComputedTop(), "0 === root.getComputedTop() (" + root.getComputedTop() + ")");
994+
console.assert(120 === root.getComputedWidth(), "120 === root.getComputedWidth() (" + root.getComputedWidth() + ")");
995+
console.assert(50 === root.getComputedHeight(), "50 === root.getComputedHeight() (" + root.getComputedHeight() + ")");
996+
997+
console.assert(20 === root_child0.getComputedLeft(), "20 === root_child0.getComputedLeft() (" + root_child0.getComputedLeft() + ")");
998+
console.assert(0 === root_child0.getComputedTop(), "0 === root_child0.getComputedTop() (" + root_child0.getComputedTop() + ")");
999+
console.assert(100 === root_child0.getComputedWidth(), "100 === root_child0.getComputedWidth() (" + root_child0.getComputedWidth() + ")");
1000+
console.assert(50 === root_child0.getComputedHeight(), "50 === root_child0.getComputedHeight() (" + root_child0.getComputedHeight() + ")");
1001+
1002+
console.assert(0 === root_child1.getComputedLeft(), "0 === root_child1.getComputedLeft() (" + root_child1.getComputedLeft() + ")");
1003+
console.assert(0 === root_child1.getComputedTop(), "0 === root_child1.getComputedTop() (" + root_child1.getComputedTop() + ")");
1004+
console.assert(20 === root_child1.getComputedWidth(), "20 === root_child1.getComputedWidth() (" + root_child1.getComputedWidth() + ")");
1005+
console.assert(50 === root_child1.getComputedHeight(), "50 === root_child1.getComputedHeight() (" + root_child1.getComputedHeight() + ")");
1006+
} finally {
1007+
if (typeof root !== "undefined") {
1008+
root.freeRecursive();
1009+
}
1010+
1011+
config.free();
1012+
}
1013+
});
9531014
it("min_width_overrides_width", function () {
9541015
var config = Yoga.Config.create();
9551016

tests/YGAspectRatioTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ TEST(YogaTest, aspect_ratio_with_max_main_defined) {
276276

277277
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
278278
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
279-
ASSERT_EQ(50, YGNodeLayoutGetWidth(root_child0));
279+
ASSERT_EQ(40, YGNodeLayoutGetWidth(root_child0));
280280
ASSERT_EQ(40, YGNodeLayoutGetHeight(root_child0));
281281

282282
YGNodeFreeRecursive(root);
@@ -320,7 +320,7 @@ TEST(YogaTest, aspect_ratio_with_min_main_defined) {
320320

321321
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
322322
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
323-
ASSERT_EQ(30, YGNodeLayoutGetWidth(root_child0));
323+
ASSERT_EQ(40, YGNodeLayoutGetWidth(root_child0));
324324
ASSERT_EQ(40, YGNodeLayoutGetHeight(root_child0));
325325

326326
YGNodeFreeRecursive(root);

tests/YGMinMaxDimensionTest.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,64 @@ TEST(YogaTest, flex_grow_within_constrained_max_column) {
900900
YGConfigFree(config);
901901
}
902902

903+
TEST(YogaTest, child_min_max_width_flexing) {
904+
const YGConfigRef config = YGConfigNew();
905+
906+
const YGNodeRef root = YGNodeNewWithConfig(config);
907+
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
908+
YGNodeStyleSetWidth(root, 120);
909+
YGNodeStyleSetHeight(root, 50);
910+
911+
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
912+
YGNodeStyleSetFlexGrow(root_child0, 1);
913+
YGNodeStyleSetFlexBasis(root_child0, 0);
914+
YGNodeStyleSetMinWidth(root_child0, 60);
915+
YGNodeInsertChild(root, root_child0, 0);
916+
917+
const YGNodeRef root_child1 = YGNodeNewWithConfig(config);
918+
YGNodeStyleSetFlexGrow(root_child1, 1);
919+
YGNodeStyleSetFlexBasisPercent(root_child1, 50);
920+
YGNodeStyleSetMaxWidth(root_child1, 20);
921+
YGNodeInsertChild(root, root_child1, 1);
922+
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
923+
924+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root));
925+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root));
926+
ASSERT_FLOAT_EQ(120, YGNodeLayoutGetWidth(root));
927+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetHeight(root));
928+
929+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0));
930+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0));
931+
ASSERT_FLOAT_EQ(100, YGNodeLayoutGetWidth(root_child0));
932+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetHeight(root_child0));
933+
934+
ASSERT_FLOAT_EQ(100, YGNodeLayoutGetLeft(root_child1));
935+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child1));
936+
ASSERT_FLOAT_EQ(20, YGNodeLayoutGetWidth(root_child1));
937+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetHeight(root_child1));
938+
939+
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionRTL);
940+
941+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root));
942+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root));
943+
ASSERT_FLOAT_EQ(120, YGNodeLayoutGetWidth(root));
944+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetHeight(root));
945+
946+
ASSERT_FLOAT_EQ(20, YGNodeLayoutGetLeft(root_child0));
947+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0));
948+
ASSERT_FLOAT_EQ(100, YGNodeLayoutGetWidth(root_child0));
949+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetHeight(root_child0));
950+
951+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child1));
952+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child1));
953+
ASSERT_FLOAT_EQ(20, YGNodeLayoutGetWidth(root_child1));
954+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetHeight(root_child1));
955+
956+
YGNodeFreeRecursive(root);
957+
958+
YGConfigFree(config);
959+
}
960+
903961
TEST(YogaTest, min_width_overrides_width) {
904962
const YGConfigRef config = YGConfigNew();
905963

0 commit comments

Comments
 (0)