Skip to content

Commit 25f14a1

Browse files
woehrl01facebook-github-bot
authored andcommitted
Fix min constraint incorrectly reducing available space
Summary: If a min constraint exists. It incorrectly reduces the available space by that amount. This adds a test and fix for this. Closes #501 Differential Revision: D4867146 Pulled By: emilsjolander fbshipit-source-id: ceafe070bfe7f501929d316656ac44c4e1753059
1 parent e992737 commit 25f14a1

File tree

5 files changed

+442
-4
lines changed

5 files changed

+442
-4
lines changed

csharp/tests/Facebook.Yoga/YGMinMaxDimensionTest.cs

+108
Original file line numberDiff line numberDiff line change
@@ -957,5 +957,113 @@ public void Test_min_max_percent_no_width_height()
957957
Assert.AreEqual(10f, root_child0.LayoutHeight);
958958
}
959959

960+
[Test]
961+
public void Test_min_width_in_flex_distribution()
962+
{
963+
YogaConfig config = new YogaConfig();
964+
965+
YogaNode root = new YogaNode(config);
966+
root.FlexDirection = YogaFlexDirection.Row;
967+
root.Width = 300;
968+
root.Height = 300;
969+
970+
YogaNode root_child0 = new YogaNode(config);
971+
root_child0.FlexGrow = 2;
972+
root_child0.FlexShrink = 1;
973+
root_child0.FlexBasis = 0.Percent();
974+
root_child0.MinWidth = 100;
975+
root_child0.MaxWidth = 200;
976+
root.Insert(0, root_child0);
977+
978+
YogaNode root_child1 = new YogaNode(config);
979+
root_child1.FlexGrow = 1;
980+
root_child1.FlexShrink = 1;
981+
root_child1.FlexBasis = 0.Percent();
982+
root.Insert(1, root_child1);
983+
984+
YogaNode root_child2 = new YogaNode(config);
985+
root_child2.FlexGrow = 1;
986+
root_child2.FlexShrink = 1;
987+
root_child2.FlexBasis = 0.Percent();
988+
root.Insert(2, root_child2);
989+
990+
YogaNode root_child3 = new YogaNode(config);
991+
root_child3.FlexGrow = 1;
992+
root_child3.FlexShrink = 1;
993+
root_child3.FlexBasis = 0.Percent();
994+
root.Insert(3, root_child3);
995+
996+
YogaNode root_child4 = new YogaNode(config);
997+
root_child4.FlexGrow = 1;
998+
root_child4.FlexShrink = 1;
999+
root_child4.FlexBasis = 0.Percent();
1000+
root.Insert(4, root_child4);
1001+
root.StyleDirection = YogaDirection.LTR;
1002+
root.CalculateLayout();
1003+
1004+
Assert.AreEqual(0f, root.LayoutX);
1005+
Assert.AreEqual(0f, root.LayoutY);
1006+
Assert.AreEqual(300f, root.LayoutWidth);
1007+
Assert.AreEqual(300f, root.LayoutHeight);
1008+
1009+
Assert.AreEqual(0f, root_child0.LayoutX);
1010+
Assert.AreEqual(0f, root_child0.LayoutY);
1011+
Assert.AreEqual(100f, root_child0.LayoutWidth);
1012+
Assert.AreEqual(300f, root_child0.LayoutHeight);
1013+
1014+
Assert.AreEqual(100f, root_child1.LayoutX);
1015+
Assert.AreEqual(0f, root_child1.LayoutY);
1016+
Assert.AreEqual(50f, root_child1.LayoutWidth);
1017+
Assert.AreEqual(300f, root_child1.LayoutHeight);
1018+
1019+
Assert.AreEqual(150f, root_child2.LayoutX);
1020+
Assert.AreEqual(0f, root_child2.LayoutY);
1021+
Assert.AreEqual(50f, root_child2.LayoutWidth);
1022+
Assert.AreEqual(300f, root_child2.LayoutHeight);
1023+
1024+
Assert.AreEqual(200f, root_child3.LayoutX);
1025+
Assert.AreEqual(0f, root_child3.LayoutY);
1026+
Assert.AreEqual(50f, root_child3.LayoutWidth);
1027+
Assert.AreEqual(300f, root_child3.LayoutHeight);
1028+
1029+
Assert.AreEqual(250f, root_child4.LayoutX);
1030+
Assert.AreEqual(0f, root_child4.LayoutY);
1031+
Assert.AreEqual(50f, root_child4.LayoutWidth);
1032+
Assert.AreEqual(300f, root_child4.LayoutHeight);
1033+
1034+
root.StyleDirection = YogaDirection.RTL;
1035+
root.CalculateLayout();
1036+
1037+
Assert.AreEqual(0f, root.LayoutX);
1038+
Assert.AreEqual(0f, root.LayoutY);
1039+
Assert.AreEqual(300f, root.LayoutWidth);
1040+
Assert.AreEqual(300f, root.LayoutHeight);
1041+
1042+
Assert.AreEqual(200f, root_child0.LayoutX);
1043+
Assert.AreEqual(0f, root_child0.LayoutY);
1044+
Assert.AreEqual(100f, root_child0.LayoutWidth);
1045+
Assert.AreEqual(300f, root_child0.LayoutHeight);
1046+
1047+
Assert.AreEqual(150f, root_child1.LayoutX);
1048+
Assert.AreEqual(0f, root_child1.LayoutY);
1049+
Assert.AreEqual(50f, root_child1.LayoutWidth);
1050+
Assert.AreEqual(300f, root_child1.LayoutHeight);
1051+
1052+
Assert.AreEqual(100f, root_child2.LayoutX);
1053+
Assert.AreEqual(0f, root_child2.LayoutY);
1054+
Assert.AreEqual(50f, root_child2.LayoutWidth);
1055+
Assert.AreEqual(300f, root_child2.LayoutHeight);
1056+
1057+
Assert.AreEqual(50f, root_child3.LayoutX);
1058+
Assert.AreEqual(0f, root_child3.LayoutY);
1059+
Assert.AreEqual(50f, root_child3.LayoutWidth);
1060+
Assert.AreEqual(300f, root_child3.LayoutHeight);
1061+
1062+
Assert.AreEqual(0f, root_child4.LayoutX);
1063+
Assert.AreEqual(0f, root_child4.LayoutY);
1064+
Assert.AreEqual(50f, root_child4.LayoutWidth);
1065+
Assert.AreEqual(300f, root_child4.LayoutHeight);
1066+
}
1067+
9601068
}
9611069
}

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

+107
Original file line numberDiff line numberDiff line change
@@ -936,4 +936,111 @@ public void test_min_max_percent_no_width_height() {
936936
assertEquals(10f, root_child0.getLayoutHeight(), 0.0f);
937937
}
938938

939+
@Test
940+
public void test_min_width_in_flex_distribution() {
941+
YogaConfig config = new YogaConfig();
942+
943+
final YogaNode root = new YogaNode(config);
944+
root.setFlexDirection(YogaFlexDirection.ROW);
945+
root.setWidth(300f);
946+
root.setHeight(300f);
947+
948+
final YogaNode root_child0 = new YogaNode(config);
949+
root_child0.setFlexGrow(2f);
950+
root_child0.setFlexShrink(1f);
951+
root_child0.setFlexBasisPercent(0f);
952+
root_child0.setMinWidth(100f);
953+
root_child0.setMaxWidth(200f);
954+
root.addChildAt(root_child0, 0);
955+
956+
final YogaNode root_child1 = new YogaNode(config);
957+
root_child1.setFlexGrow(1f);
958+
root_child1.setFlexShrink(1f);
959+
root_child1.setFlexBasisPercent(0f);
960+
root.addChildAt(root_child1, 1);
961+
962+
final YogaNode root_child2 = new YogaNode(config);
963+
root_child2.setFlexGrow(1f);
964+
root_child2.setFlexShrink(1f);
965+
root_child2.setFlexBasisPercent(0f);
966+
root.addChildAt(root_child2, 2);
967+
968+
final YogaNode root_child3 = new YogaNode(config);
969+
root_child3.setFlexGrow(1f);
970+
root_child3.setFlexShrink(1f);
971+
root_child3.setFlexBasisPercent(0f);
972+
root.addChildAt(root_child3, 3);
973+
974+
final YogaNode root_child4 = new YogaNode(config);
975+
root_child4.setFlexGrow(1f);
976+
root_child4.setFlexShrink(1f);
977+
root_child4.setFlexBasisPercent(0f);
978+
root.addChildAt(root_child4, 4);
979+
root.setDirection(YogaDirection.LTR);
980+
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
981+
982+
assertEquals(0f, root.getLayoutX(), 0.0f);
983+
assertEquals(0f, root.getLayoutY(), 0.0f);
984+
assertEquals(300f, root.getLayoutWidth(), 0.0f);
985+
assertEquals(300f, root.getLayoutHeight(), 0.0f);
986+
987+
assertEquals(0f, root_child0.getLayoutX(), 0.0f);
988+
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
989+
assertEquals(100f, root_child0.getLayoutWidth(), 0.0f);
990+
assertEquals(300f, root_child0.getLayoutHeight(), 0.0f);
991+
992+
assertEquals(100f, root_child1.getLayoutX(), 0.0f);
993+
assertEquals(0f, root_child1.getLayoutY(), 0.0f);
994+
assertEquals(50f, root_child1.getLayoutWidth(), 0.0f);
995+
assertEquals(300f, root_child1.getLayoutHeight(), 0.0f);
996+
997+
assertEquals(150f, root_child2.getLayoutX(), 0.0f);
998+
assertEquals(0f, root_child2.getLayoutY(), 0.0f);
999+
assertEquals(50f, root_child2.getLayoutWidth(), 0.0f);
1000+
assertEquals(300f, root_child2.getLayoutHeight(), 0.0f);
1001+
1002+
assertEquals(200f, root_child3.getLayoutX(), 0.0f);
1003+
assertEquals(0f, root_child3.getLayoutY(), 0.0f);
1004+
assertEquals(50f, root_child3.getLayoutWidth(), 0.0f);
1005+
assertEquals(300f, root_child3.getLayoutHeight(), 0.0f);
1006+
1007+
assertEquals(250f, root_child4.getLayoutX(), 0.0f);
1008+
assertEquals(0f, root_child4.getLayoutY(), 0.0f);
1009+
assertEquals(50f, root_child4.getLayoutWidth(), 0.0f);
1010+
assertEquals(300f, root_child4.getLayoutHeight(), 0.0f);
1011+
1012+
root.setDirection(YogaDirection.RTL);
1013+
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
1014+
1015+
assertEquals(0f, root.getLayoutX(), 0.0f);
1016+
assertEquals(0f, root.getLayoutY(), 0.0f);
1017+
assertEquals(300f, root.getLayoutWidth(), 0.0f);
1018+
assertEquals(300f, root.getLayoutHeight(), 0.0f);
1019+
1020+
assertEquals(200f, root_child0.getLayoutX(), 0.0f);
1021+
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
1022+
assertEquals(100f, root_child0.getLayoutWidth(), 0.0f);
1023+
assertEquals(300f, root_child0.getLayoutHeight(), 0.0f);
1024+
1025+
assertEquals(150f, root_child1.getLayoutX(), 0.0f);
1026+
assertEquals(0f, root_child1.getLayoutY(), 0.0f);
1027+
assertEquals(50f, root_child1.getLayoutWidth(), 0.0f);
1028+
assertEquals(300f, root_child1.getLayoutHeight(), 0.0f);
1029+
1030+
assertEquals(100f, root_child2.getLayoutX(), 0.0f);
1031+
assertEquals(0f, root_child2.getLayoutY(), 0.0f);
1032+
assertEquals(50f, root_child2.getLayoutWidth(), 0.0f);
1033+
assertEquals(300f, root_child2.getLayoutHeight(), 0.0f);
1034+
1035+
assertEquals(50f, root_child3.getLayoutX(), 0.0f);
1036+
assertEquals(0f, root_child3.getLayoutY(), 0.0f);
1037+
assertEquals(50f, root_child3.getLayoutWidth(), 0.0f);
1038+
assertEquals(300f, root_child3.getLayoutHeight(), 0.0f);
1039+
1040+
assertEquals(0f, root_child4.getLayoutX(), 0.0f);
1041+
assertEquals(0f, root_child4.getLayoutY(), 0.0f);
1042+
assertEquals(50f, root_child4.getLayoutWidth(), 0.0f);
1043+
assertEquals(300f, root_child4.getLayoutHeight(), 0.0f);
1044+
}
1045+
9391046
}

javascript/tests/Facebook.Yoga/YGMinMaxDimensionTest.js

+111
Original file line numberDiff line numberDiff line change
@@ -1013,3 +1013,114 @@ it("min_max_percent_no_width_height", function () {
10131013
config.free();
10141014
}
10151015
});
1016+
it("min_width_in_flex_distribution", function () {
1017+
var config = Yoga.Config.create();
1018+
1019+
try {
1020+
var root = Yoga.Node.create(config);
1021+
root.setFlexDirection(Yoga.FLEX_DIRECTION_ROW);
1022+
root.setWidth(300);
1023+
root.setHeight(300);
1024+
1025+
var root_child0 = Yoga.Node.create(config);
1026+
root_child0.setFlexGrow(2);
1027+
root_child0.setFlexShrink(1);
1028+
root_child0.setFlexBasis("0%");
1029+
root_child0.setMinWidth(100);
1030+
root_child0.setMaxWidth(200);
1031+
root.insertChild(root_child0, 0);
1032+
1033+
var root_child1 = Yoga.Node.create(config);
1034+
root_child1.setFlexGrow(1);
1035+
root_child1.setFlexShrink(1);
1036+
root_child1.setFlexBasis("0%");
1037+
root.insertChild(root_child1, 1);
1038+
1039+
var root_child2 = Yoga.Node.create(config);
1040+
root_child2.setFlexGrow(1);
1041+
root_child2.setFlexShrink(1);
1042+
root_child2.setFlexBasis("0%");
1043+
root.insertChild(root_child2, 2);
1044+
1045+
var root_child3 = Yoga.Node.create(config);
1046+
root_child3.setFlexGrow(1);
1047+
root_child3.setFlexShrink(1);
1048+
root_child3.setFlexBasis("0%");
1049+
root.insertChild(root_child3, 3);
1050+
1051+
var root_child4 = Yoga.Node.create(config);
1052+
root_child4.setFlexGrow(1);
1053+
root_child4.setFlexShrink(1);
1054+
root_child4.setFlexBasis("0%");
1055+
root.insertChild(root_child4, 4);
1056+
root.calculateLayout(Yoga.UNDEFINED, Yoga.UNDEFINED, Yoga.DIRECTION_LTR);
1057+
1058+
console.assert(0 === root.getComputedLeft(), "0 === root.getComputedLeft() (" + root.getComputedLeft() + ")");
1059+
console.assert(0 === root.getComputedTop(), "0 === root.getComputedTop() (" + root.getComputedTop() + ")");
1060+
console.assert(300 === root.getComputedWidth(), "300 === root.getComputedWidth() (" + root.getComputedWidth() + ")");
1061+
console.assert(300 === root.getComputedHeight(), "300 === root.getComputedHeight() (" + root.getComputedHeight() + ")");
1062+
1063+
console.assert(0 === root_child0.getComputedLeft(), "0 === root_child0.getComputedLeft() (" + root_child0.getComputedLeft() + ")");
1064+
console.assert(0 === root_child0.getComputedTop(), "0 === root_child0.getComputedTop() (" + root_child0.getComputedTop() + ")");
1065+
console.assert(100 === root_child0.getComputedWidth(), "100 === root_child0.getComputedWidth() (" + root_child0.getComputedWidth() + ")");
1066+
console.assert(300 === root_child0.getComputedHeight(), "300 === root_child0.getComputedHeight() (" + root_child0.getComputedHeight() + ")");
1067+
1068+
console.assert(100 === root_child1.getComputedLeft(), "100 === root_child1.getComputedLeft() (" + root_child1.getComputedLeft() + ")");
1069+
console.assert(0 === root_child1.getComputedTop(), "0 === root_child1.getComputedTop() (" + root_child1.getComputedTop() + ")");
1070+
console.assert(50 === root_child1.getComputedWidth(), "50 === root_child1.getComputedWidth() (" + root_child1.getComputedWidth() + ")");
1071+
console.assert(300 === root_child1.getComputedHeight(), "300 === root_child1.getComputedHeight() (" + root_child1.getComputedHeight() + ")");
1072+
1073+
console.assert(150 === root_child2.getComputedLeft(), "150 === root_child2.getComputedLeft() (" + root_child2.getComputedLeft() + ")");
1074+
console.assert(0 === root_child2.getComputedTop(), "0 === root_child2.getComputedTop() (" + root_child2.getComputedTop() + ")");
1075+
console.assert(50 === root_child2.getComputedWidth(), "50 === root_child2.getComputedWidth() (" + root_child2.getComputedWidth() + ")");
1076+
console.assert(300 === root_child2.getComputedHeight(), "300 === root_child2.getComputedHeight() (" + root_child2.getComputedHeight() + ")");
1077+
1078+
console.assert(200 === root_child3.getComputedLeft(), "200 === root_child3.getComputedLeft() (" + root_child3.getComputedLeft() + ")");
1079+
console.assert(0 === root_child3.getComputedTop(), "0 === root_child3.getComputedTop() (" + root_child3.getComputedTop() + ")");
1080+
console.assert(50 === root_child3.getComputedWidth(), "50 === root_child3.getComputedWidth() (" + root_child3.getComputedWidth() + ")");
1081+
console.assert(300 === root_child3.getComputedHeight(), "300 === root_child3.getComputedHeight() (" + root_child3.getComputedHeight() + ")");
1082+
1083+
console.assert(250 === root_child4.getComputedLeft(), "250 === root_child4.getComputedLeft() (" + root_child4.getComputedLeft() + ")");
1084+
console.assert(0 === root_child4.getComputedTop(), "0 === root_child4.getComputedTop() (" + root_child4.getComputedTop() + ")");
1085+
console.assert(50 === root_child4.getComputedWidth(), "50 === root_child4.getComputedWidth() (" + root_child4.getComputedWidth() + ")");
1086+
console.assert(300 === root_child4.getComputedHeight(), "300 === root_child4.getComputedHeight() (" + root_child4.getComputedHeight() + ")");
1087+
1088+
root.calculateLayout(Yoga.UNDEFINED, Yoga.UNDEFINED, Yoga.DIRECTION_RTL);
1089+
1090+
console.assert(0 === root.getComputedLeft(), "0 === root.getComputedLeft() (" + root.getComputedLeft() + ")");
1091+
console.assert(0 === root.getComputedTop(), "0 === root.getComputedTop() (" + root.getComputedTop() + ")");
1092+
console.assert(300 === root.getComputedWidth(), "300 === root.getComputedWidth() (" + root.getComputedWidth() + ")");
1093+
console.assert(300 === root.getComputedHeight(), "300 === root.getComputedHeight() (" + root.getComputedHeight() + ")");
1094+
1095+
console.assert(200 === root_child0.getComputedLeft(), "200 === root_child0.getComputedLeft() (" + root_child0.getComputedLeft() + ")");
1096+
console.assert(0 === root_child0.getComputedTop(), "0 === root_child0.getComputedTop() (" + root_child0.getComputedTop() + ")");
1097+
console.assert(100 === root_child0.getComputedWidth(), "100 === root_child0.getComputedWidth() (" + root_child0.getComputedWidth() + ")");
1098+
console.assert(300 === root_child0.getComputedHeight(), "300 === root_child0.getComputedHeight() (" + root_child0.getComputedHeight() + ")");
1099+
1100+
console.assert(150 === root_child1.getComputedLeft(), "150 === root_child1.getComputedLeft() (" + root_child1.getComputedLeft() + ")");
1101+
console.assert(0 === root_child1.getComputedTop(), "0 === root_child1.getComputedTop() (" + root_child1.getComputedTop() + ")");
1102+
console.assert(50 === root_child1.getComputedWidth(), "50 === root_child1.getComputedWidth() (" + root_child1.getComputedWidth() + ")");
1103+
console.assert(300 === root_child1.getComputedHeight(), "300 === root_child1.getComputedHeight() (" + root_child1.getComputedHeight() + ")");
1104+
1105+
console.assert(100 === root_child2.getComputedLeft(), "100 === root_child2.getComputedLeft() (" + root_child2.getComputedLeft() + ")");
1106+
console.assert(0 === root_child2.getComputedTop(), "0 === root_child2.getComputedTop() (" + root_child2.getComputedTop() + ")");
1107+
console.assert(50 === root_child2.getComputedWidth(), "50 === root_child2.getComputedWidth() (" + root_child2.getComputedWidth() + ")");
1108+
console.assert(300 === root_child2.getComputedHeight(), "300 === root_child2.getComputedHeight() (" + root_child2.getComputedHeight() + ")");
1109+
1110+
console.assert(50 === root_child3.getComputedLeft(), "50 === root_child3.getComputedLeft() (" + root_child3.getComputedLeft() + ")");
1111+
console.assert(0 === root_child3.getComputedTop(), "0 === root_child3.getComputedTop() (" + root_child3.getComputedTop() + ")");
1112+
console.assert(50 === root_child3.getComputedWidth(), "50 === root_child3.getComputedWidth() (" + root_child3.getComputedWidth() + ")");
1113+
console.assert(300 === root_child3.getComputedHeight(), "300 === root_child3.getComputedHeight() (" + root_child3.getComputedHeight() + ")");
1114+
1115+
console.assert(0 === root_child4.getComputedLeft(), "0 === root_child4.getComputedLeft() (" + root_child4.getComputedLeft() + ")");
1116+
console.assert(0 === root_child4.getComputedTop(), "0 === root_child4.getComputedTop() (" + root_child4.getComputedTop() + ")");
1117+
console.assert(50 === root_child4.getComputedWidth(), "50 === root_child4.getComputedWidth() (" + root_child4.getComputedWidth() + ")");
1118+
console.assert(300 === root_child4.getComputedHeight(), "300 === root_child4.getComputedHeight() (" + root_child4.getComputedHeight() + ")");
1119+
} finally {
1120+
if (typeof root !== "undefined") {
1121+
root.freeRecursive();
1122+
}
1123+
1124+
config.free();
1125+
}
1126+
});

0 commit comments

Comments
 (0)