Skip to content

Commit

Permalink
Fix min/max not overriding width/height
Browse files Browse the repository at this point in the history
Summary:
Two bugs:
1. Min/Max width/height should have higher priority than width/height
2. custom measure nodes percentages should be based in parent size like everything else.

Differential Revision: D4537576

fbshipit-source-id: c003f723f424afbca63170d41e54fd5ff837926d
  • Loading branch information
Emil Sjolander authored and facebook-github-bot committed Feb 11, 2017
1 parent a5b94eb commit 240c2dd
Show file tree
Hide file tree
Showing 8 changed files with 631 additions and 71 deletions.
133 changes: 133 additions & 0 deletions csharp/tests/Facebook.Yoga/YGMinMaxDimensionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -675,5 +675,138 @@ public void Test_flex_grow_within_constrained_max_column()
Assert.AreEqual(50f, root_child1.LayoutHeight);
}

[Test]
public void Test_min_width_overrides_width()
{
YogaNode root = new YogaNode();
root.Width = 50;
root.MinWidth = 100;
root.StyleDirection = YogaDirection.LTR;
root.CalculateLayout();

Assert.AreEqual(0f, root.LayoutX);
Assert.AreEqual(0f, root.LayoutY);
Assert.AreEqual(100f, root.LayoutWidth);
Assert.AreEqual(0f, root.LayoutHeight);

root.StyleDirection = YogaDirection.RTL;
root.CalculateLayout();

Assert.AreEqual(0f, root.LayoutX);
Assert.AreEqual(0f, root.LayoutY);
Assert.AreEqual(100f, root.LayoutWidth);
Assert.AreEqual(0f, root.LayoutHeight);
}

[Test]
public void Test_max_width_overrides_width()
{
YogaNode root = new YogaNode();
root.Width = 200;
root.MaxWidth = 100;
root.StyleDirection = YogaDirection.LTR;
root.CalculateLayout();

Assert.AreEqual(0f, root.LayoutX);
Assert.AreEqual(0f, root.LayoutY);
Assert.AreEqual(100f, root.LayoutWidth);
Assert.AreEqual(0f, root.LayoutHeight);

root.StyleDirection = YogaDirection.RTL;
root.CalculateLayout();

Assert.AreEqual(0f, root.LayoutX);
Assert.AreEqual(0f, root.LayoutY);
Assert.AreEqual(100f, root.LayoutWidth);
Assert.AreEqual(0f, root.LayoutHeight);
}

[Test]
public void Test_min_height_overrides_height()
{
YogaNode root = new YogaNode();
root.Height = 50;
root.MinHeight = 100;
root.StyleDirection = YogaDirection.LTR;
root.CalculateLayout();

Assert.AreEqual(0f, root.LayoutX);
Assert.AreEqual(0f, root.LayoutY);
Assert.AreEqual(0f, root.LayoutWidth);
Assert.AreEqual(100f, root.LayoutHeight);

root.StyleDirection = YogaDirection.RTL;
root.CalculateLayout();

Assert.AreEqual(0f, root.LayoutX);
Assert.AreEqual(0f, root.LayoutY);
Assert.AreEqual(0f, root.LayoutWidth);
Assert.AreEqual(100f, root.LayoutHeight);
}

[Test]
public void Test_max_height_overrides_height()
{
YogaNode root = new YogaNode();
root.Height = 200;
root.MaxHeight = 100;
root.StyleDirection = YogaDirection.LTR;
root.CalculateLayout();

Assert.AreEqual(0f, root.LayoutX);
Assert.AreEqual(0f, root.LayoutY);
Assert.AreEqual(0f, root.LayoutWidth);
Assert.AreEqual(100f, root.LayoutHeight);

root.StyleDirection = YogaDirection.RTL;
root.CalculateLayout();

Assert.AreEqual(0f, root.LayoutX);
Assert.AreEqual(0f, root.LayoutY);
Assert.AreEqual(0f, root.LayoutWidth);
Assert.AreEqual(100f, root.LayoutHeight);
}

[Test]
public void Test_min_max_percent_no_width_height()
{
YogaNode root = new YogaNode();
root.AlignItems = YogaAlign.FlexStart;
root.Width = 100;
root.Height = 100;

YogaNode root_child0 = new YogaNode();
root_child0.MinWidth = 10.Percent();
root_child0.MaxWidth = 10.Percent();
root_child0.MinHeight = 10.Percent();
root_child0.MaxHeight = 10.Percent();
root.Insert(0, root_child0);
root.StyleDirection = YogaDirection.LTR;
root.CalculateLayout();

Assert.AreEqual(0f, root.LayoutX);
Assert.AreEqual(0f, root.LayoutY);
Assert.AreEqual(100f, root.LayoutWidth);
Assert.AreEqual(100f, root.LayoutHeight);

Assert.AreEqual(0f, root_child0.LayoutX);
Assert.AreEqual(0f, root_child0.LayoutY);
Assert.AreEqual(10f, root_child0.LayoutWidth);
Assert.AreEqual(10f, root_child0.LayoutHeight);

root.StyleDirection = YogaDirection.RTL;
root.CalculateLayout();

Assert.AreEqual(0f, root.LayoutX);
Assert.AreEqual(0f, root.LayoutY);
Assert.AreEqual(100f, root.LayoutWidth);
Assert.AreEqual(100f, root.LayoutHeight);

Assert.AreEqual(90f, root_child0.LayoutX);
Assert.AreEqual(0f, root_child0.LayoutY);
Assert.AreEqual(10f, root_child0.LayoutWidth);
Assert.AreEqual(10f, root_child0.LayoutHeight);
}

}
}
17 changes: 17 additions & 0 deletions gentest/fixtures/YGMinMaxDimensionTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,20 @@
<div style="flex-shrink:1; flex-basis:100px"></div>
<div style="height: 50px;"></div>
</div>

<div id="min_width_overrides_width" style="min-width: 100px; width: 50px;">
</div>

<div id="max_width_overrides_width" style="max-width: 100px; width: 200px;">
</div>

<div id="min_height_overrides_height" style="min-height: 100px; height: 50px;">
</div>

<div id="max_height_overrides_height" style="max-height: 100px; height: 200px;">
</div>

<div id="min_max_percent_no_width_height" style="width: 100px; height: 100px; align-items: flex-start;">
<div style="min-width: 10%; max-width: 10%; min-height: 10%; max-height: 10%;">
</div>
</div>
128 changes: 128 additions & 0 deletions java/tests/com/facebook/yoga/YGMinMaxDimensionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -661,4 +661,132 @@ public void test_flex_grow_within_constrained_max_column() {
assertEquals(50f, root_child1.getLayoutHeight(), 0.0f);
}

@Test
public void test_min_width_overrides_width() {
final YogaNode root = new YogaNode();
root.setWidth(50f);
root.setMinWidth(100f);
root.setDirection(YogaDirection.LTR);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(100f, root.getLayoutWidth(), 0.0f);
assertEquals(0f, root.getLayoutHeight(), 0.0f);

root.setDirection(YogaDirection.RTL);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(100f, root.getLayoutWidth(), 0.0f);
assertEquals(0f, root.getLayoutHeight(), 0.0f);
}

@Test
public void test_max_width_overrides_width() {
final YogaNode root = new YogaNode();
root.setWidth(200f);
root.setMaxWidth(100f);
root.setDirection(YogaDirection.LTR);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(100f, root.getLayoutWidth(), 0.0f);
assertEquals(0f, root.getLayoutHeight(), 0.0f);

root.setDirection(YogaDirection.RTL);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(100f, root.getLayoutWidth(), 0.0f);
assertEquals(0f, root.getLayoutHeight(), 0.0f);
}

@Test
public void test_min_height_overrides_height() {
final YogaNode root = new YogaNode();
root.setHeight(50f);
root.setMinHeight(100f);
root.setDirection(YogaDirection.LTR);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(0f, root.getLayoutWidth(), 0.0f);
assertEquals(100f, root.getLayoutHeight(), 0.0f);

root.setDirection(YogaDirection.RTL);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(0f, root.getLayoutWidth(), 0.0f);
assertEquals(100f, root.getLayoutHeight(), 0.0f);
}

@Test
public void test_max_height_overrides_height() {
final YogaNode root = new YogaNode();
root.setHeight(200f);
root.setMaxHeight(100f);
root.setDirection(YogaDirection.LTR);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(0f, root.getLayoutWidth(), 0.0f);
assertEquals(100f, root.getLayoutHeight(), 0.0f);

root.setDirection(YogaDirection.RTL);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(0f, root.getLayoutWidth(), 0.0f);
assertEquals(100f, root.getLayoutHeight(), 0.0f);
}

@Test
public void test_min_max_percent_no_width_height() {
final YogaNode root = new YogaNode();
root.setAlignItems(YogaAlign.FLEX_START);
root.setWidth(100f);
root.setHeight(100f);

final YogaNode root_child0 = new YogaNode();
root_child0.setMinWidthPercent(10f);
root_child0.setMaxWidthPercent(10f);
root_child0.setMinHeightPercent(10f);
root_child0.setMaxHeightPercent(10f);
root.addChildAt(root_child0, 0);
root.setDirection(YogaDirection.LTR);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(100f, root.getLayoutWidth(), 0.0f);
assertEquals(100f, root.getLayoutHeight(), 0.0f);

assertEquals(0f, root_child0.getLayoutX(), 0.0f);
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
assertEquals(10f, root_child0.getLayoutWidth(), 0.0f);
assertEquals(10f, root_child0.getLayoutHeight(), 0.0f);

root.setDirection(YogaDirection.RTL);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(100f, root.getLayoutWidth(), 0.0f);
assertEquals(100f, root.getLayoutHeight(), 0.0f);

assertEquals(90f, root_child0.getLayoutX(), 0.0f);
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
assertEquals(10f, root_child0.getLayoutWidth(), 0.0f);
assertEquals(10f, root_child0.getLayoutHeight(), 0.0f);
}

}
Loading

0 comments on commit 240c2dd

Please sign in to comment.