Files
yoga/java/tests/com/facebook/csslayout/CSSLayoutFlexTest.java
Emil Sjolander a253c6fbb7 Dont measure single flex grow+shrink child
Summary:
If there is a single child which is flex grow and flex shrink then instead of measuring and then shrinking we can just set the flex basis to zero as we know the final result will be that the child take up all remaining space.

This is a re-land of D4147298. I have updated the diff to check explicitly for exact measure mode to also handle at_most case correctly.

Reviewed By: gkassabli

Differential Revision: D4153133

fbshipit-source-id: 2333150a83857cc30078cc8d52761cbd00652830
2016-11-09 11:37:48 -08:00

452 lines
16 KiB
Java

/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/**
* @Generated by gentest/gentest.sh with the following input
*
<div id="flex_basis_flex_grow_column" style="width: 100px; height: 100px;">
<div style="flex-basis: 50px; flex-grow: 1;"></div>
<div style="flex-grow: 1;"></div>
</div>
<div id="flex_basis_flex_grow_row" style="width: 100px; height: 100px; flex-direction: row;">
<div style="flex-basis: 50px; flex-grow: 1;"></div>
<div style="flex-grow: 1;"></div>
</div>
<div id="flex_basis_flex_shrink_column" style="width: 100px; height: 100px;">
<div style="flex-basis: 100px; flex-shrink: 1;"></div>
<div style="flex-basis: 50px;"></div>
</div>
<div id="flex_basis_flex_shrink_row" style="width: 100px; height: 100px; flex-direction: row;">
<div style="flex-basis: 100px; flex-shrink: 1;"></div>
<div style="flex-basis: 50px;"></div>
</div>
<div id="flex_shrink_to_zero" style="height: 75px;">
<div style="width: 50px; height: 50px; flex-shrink:0;"></div>
<div style="width: 50px; height: 50px; flex-shrink:1;"></div>
<div style="width: 50px; height: 50px; flex-shrink:0;"></div>
</div>
<div id="flex_basis_overrides_main_size" style="height: 100px; width: 100px;">
<div style="height: 20px; flex-grow:1; flex-basis:50px;"></div>
<div style="height: 10px; flex-grow:1;"></div>
<div style="height: 10px; flex-grow:1;"></div>
</div>
<div id="flex_grow_shrink_at_most" style="height: 100px; width: 100px;">
<div>
<div style="flex-grow:1; flex-shrink:1;"></div>
</div>
</div>
*
*/
package com.facebook.csslayout;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class CSSLayoutFlexTest {
@Test
public void test_flex_basis_flex_grow_column() {
final CSSNode root = new CSSNode();
root.setStyleWidth(100);
root.setStyleHeight(100);
final CSSNode root_child0 = new CSSNode();
root_child0.setFlexGrow(1);
root_child0.setFlexBasis(50);
root.addChildAt(root_child0, 0);
final CSSNode root_child1 = new CSSNode();
root_child1.setFlexGrow(1);
root.addChildAt(root_child1, 1);
root.setDirection(CSSDirection.LTR);
root.calculateLayout(null);
assertEquals(0, root.getLayoutX(), 0.0f);
assertEquals(0, root.getLayoutY(), 0.0f);
assertEquals(100, root.getLayoutWidth(), 0.0f);
assertEquals(100, root.getLayoutHeight(), 0.0f);
assertEquals(0, root_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0.getLayoutY(), 0.0f);
assertEquals(100, root_child0.getLayoutWidth(), 0.0f);
assertEquals(75, root_child0.getLayoutHeight(), 0.0f);
assertEquals(0, root_child1.getLayoutX(), 0.0f);
assertEquals(75, root_child1.getLayoutY(), 0.0f);
assertEquals(100, root_child1.getLayoutWidth(), 0.0f);
assertEquals(25, root_child1.getLayoutHeight(), 0.0f);
root.setDirection(CSSDirection.RTL);
root.calculateLayout(null);
assertEquals(0, root.getLayoutX(), 0.0f);
assertEquals(0, root.getLayoutY(), 0.0f);
assertEquals(100, root.getLayoutWidth(), 0.0f);
assertEquals(100, root.getLayoutHeight(), 0.0f);
assertEquals(0, root_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0.getLayoutY(), 0.0f);
assertEquals(100, root_child0.getLayoutWidth(), 0.0f);
assertEquals(75, root_child0.getLayoutHeight(), 0.0f);
assertEquals(0, root_child1.getLayoutX(), 0.0f);
assertEquals(75, root_child1.getLayoutY(), 0.0f);
assertEquals(100, root_child1.getLayoutWidth(), 0.0f);
assertEquals(25, root_child1.getLayoutHeight(), 0.0f);
}
@Test
public void test_flex_basis_flex_grow_row() {
final CSSNode root = new CSSNode();
root.setFlexDirection(CSSFlexDirection.ROW);
root.setStyleWidth(100);
root.setStyleHeight(100);
final CSSNode root_child0 = new CSSNode();
root_child0.setFlexGrow(1);
root_child0.setFlexBasis(50);
root.addChildAt(root_child0, 0);
final CSSNode root_child1 = new CSSNode();
root_child1.setFlexGrow(1);
root.addChildAt(root_child1, 1);
root.setDirection(CSSDirection.LTR);
root.calculateLayout(null);
assertEquals(0, root.getLayoutX(), 0.0f);
assertEquals(0, root.getLayoutY(), 0.0f);
assertEquals(100, root.getLayoutWidth(), 0.0f);
assertEquals(100, root.getLayoutHeight(), 0.0f);
assertEquals(0, root_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0.getLayoutY(), 0.0f);
assertEquals(75, root_child0.getLayoutWidth(), 0.0f);
assertEquals(100, root_child0.getLayoutHeight(), 0.0f);
assertEquals(75, root_child1.getLayoutX(), 0.0f);
assertEquals(0, root_child1.getLayoutY(), 0.0f);
assertEquals(25, root_child1.getLayoutWidth(), 0.0f);
assertEquals(100, root_child1.getLayoutHeight(), 0.0f);
root.setDirection(CSSDirection.RTL);
root.calculateLayout(null);
assertEquals(0, root.getLayoutX(), 0.0f);
assertEquals(0, root.getLayoutY(), 0.0f);
assertEquals(100, root.getLayoutWidth(), 0.0f);
assertEquals(100, root.getLayoutHeight(), 0.0f);
assertEquals(25, root_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0.getLayoutY(), 0.0f);
assertEquals(75, root_child0.getLayoutWidth(), 0.0f);
assertEquals(100, root_child0.getLayoutHeight(), 0.0f);
assertEquals(0, root_child1.getLayoutX(), 0.0f);
assertEquals(0, root_child1.getLayoutY(), 0.0f);
assertEquals(25, root_child1.getLayoutWidth(), 0.0f);
assertEquals(100, root_child1.getLayoutHeight(), 0.0f);
}
@Test
public void test_flex_basis_flex_shrink_column() {
final CSSNode root = new CSSNode();
root.setStyleWidth(100);
root.setStyleHeight(100);
final CSSNode root_child0 = new CSSNode();
root_child0.setFlexShrink(1);
root_child0.setFlexBasis(100);
root.addChildAt(root_child0, 0);
final CSSNode root_child1 = new CSSNode();
root_child1.setFlexBasis(50);
root.addChildAt(root_child1, 1);
root.setDirection(CSSDirection.LTR);
root.calculateLayout(null);
assertEquals(0, root.getLayoutX(), 0.0f);
assertEquals(0, root.getLayoutY(), 0.0f);
assertEquals(100, root.getLayoutWidth(), 0.0f);
assertEquals(100, root.getLayoutHeight(), 0.0f);
assertEquals(0, root_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0.getLayoutY(), 0.0f);
assertEquals(100, root_child0.getLayoutWidth(), 0.0f);
assertEquals(50, root_child0.getLayoutHeight(), 0.0f);
assertEquals(0, root_child1.getLayoutX(), 0.0f);
assertEquals(50, root_child1.getLayoutY(), 0.0f);
assertEquals(100, root_child1.getLayoutWidth(), 0.0f);
assertEquals(50, root_child1.getLayoutHeight(), 0.0f);
root.setDirection(CSSDirection.RTL);
root.calculateLayout(null);
assertEquals(0, root.getLayoutX(), 0.0f);
assertEquals(0, root.getLayoutY(), 0.0f);
assertEquals(100, root.getLayoutWidth(), 0.0f);
assertEquals(100, root.getLayoutHeight(), 0.0f);
assertEquals(0, root_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0.getLayoutY(), 0.0f);
assertEquals(100, root_child0.getLayoutWidth(), 0.0f);
assertEquals(50, root_child0.getLayoutHeight(), 0.0f);
assertEquals(0, root_child1.getLayoutX(), 0.0f);
assertEquals(50, root_child1.getLayoutY(), 0.0f);
assertEquals(100, root_child1.getLayoutWidth(), 0.0f);
assertEquals(50, root_child1.getLayoutHeight(), 0.0f);
}
@Test
public void test_flex_basis_flex_shrink_row() {
final CSSNode root = new CSSNode();
root.setFlexDirection(CSSFlexDirection.ROW);
root.setStyleWidth(100);
root.setStyleHeight(100);
final CSSNode root_child0 = new CSSNode();
root_child0.setFlexShrink(1);
root_child0.setFlexBasis(100);
root.addChildAt(root_child0, 0);
final CSSNode root_child1 = new CSSNode();
root_child1.setFlexBasis(50);
root.addChildAt(root_child1, 1);
root.setDirection(CSSDirection.LTR);
root.calculateLayout(null);
assertEquals(0, root.getLayoutX(), 0.0f);
assertEquals(0, root.getLayoutY(), 0.0f);
assertEquals(100, root.getLayoutWidth(), 0.0f);
assertEquals(100, root.getLayoutHeight(), 0.0f);
assertEquals(0, root_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0.getLayoutY(), 0.0f);
assertEquals(50, root_child0.getLayoutWidth(), 0.0f);
assertEquals(100, root_child0.getLayoutHeight(), 0.0f);
assertEquals(50, root_child1.getLayoutX(), 0.0f);
assertEquals(0, root_child1.getLayoutY(), 0.0f);
assertEquals(50, root_child1.getLayoutWidth(), 0.0f);
assertEquals(100, root_child1.getLayoutHeight(), 0.0f);
root.setDirection(CSSDirection.RTL);
root.calculateLayout(null);
assertEquals(0, root.getLayoutX(), 0.0f);
assertEquals(0, root.getLayoutY(), 0.0f);
assertEquals(100, root.getLayoutWidth(), 0.0f);
assertEquals(100, root.getLayoutHeight(), 0.0f);
assertEquals(50, root_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0.getLayoutY(), 0.0f);
assertEquals(50, root_child0.getLayoutWidth(), 0.0f);
assertEquals(100, root_child0.getLayoutHeight(), 0.0f);
assertEquals(0, root_child1.getLayoutX(), 0.0f);
assertEquals(0, root_child1.getLayoutY(), 0.0f);
assertEquals(50, root_child1.getLayoutWidth(), 0.0f);
assertEquals(100, root_child1.getLayoutHeight(), 0.0f);
}
@Test
public void test_flex_shrink_to_zero() {
final CSSNode root = new CSSNode();
root.setStyleHeight(75);
final CSSNode root_child0 = new CSSNode();
root_child0.setStyleWidth(50);
root_child0.setStyleHeight(50);
root.addChildAt(root_child0, 0);
final CSSNode root_child1 = new CSSNode();
root_child1.setFlexShrink(1);
root_child1.setStyleWidth(50);
root_child1.setStyleHeight(50);
root.addChildAt(root_child1, 1);
final CSSNode root_child2 = new CSSNode();
root_child2.setStyleWidth(50);
root_child2.setStyleHeight(50);
root.addChildAt(root_child2, 2);
root.setDirection(CSSDirection.LTR);
root.calculateLayout(null);
assertEquals(0, root.getLayoutX(), 0.0f);
assertEquals(0, root.getLayoutY(), 0.0f);
assertEquals(50, root.getLayoutWidth(), 0.0f);
assertEquals(75, root.getLayoutHeight(), 0.0f);
assertEquals(0, root_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0.getLayoutY(), 0.0f);
assertEquals(50, root_child0.getLayoutWidth(), 0.0f);
assertEquals(50, root_child0.getLayoutHeight(), 0.0f);
assertEquals(0, root_child1.getLayoutX(), 0.0f);
assertEquals(50, root_child1.getLayoutY(), 0.0f);
assertEquals(50, root_child1.getLayoutWidth(), 0.0f);
assertEquals(0, root_child1.getLayoutHeight(), 0.0f);
assertEquals(0, root_child2.getLayoutX(), 0.0f);
assertEquals(50, root_child2.getLayoutY(), 0.0f);
assertEquals(50, root_child2.getLayoutWidth(), 0.0f);
assertEquals(50, root_child2.getLayoutHeight(), 0.0f);
root.setDirection(CSSDirection.RTL);
root.calculateLayout(null);
assertEquals(0, root.getLayoutX(), 0.0f);
assertEquals(0, root.getLayoutY(), 0.0f);
assertEquals(50, root.getLayoutWidth(), 0.0f);
assertEquals(75, root.getLayoutHeight(), 0.0f);
assertEquals(0, root_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0.getLayoutY(), 0.0f);
assertEquals(50, root_child0.getLayoutWidth(), 0.0f);
assertEquals(50, root_child0.getLayoutHeight(), 0.0f);
assertEquals(0, root_child1.getLayoutX(), 0.0f);
assertEquals(50, root_child1.getLayoutY(), 0.0f);
assertEquals(50, root_child1.getLayoutWidth(), 0.0f);
assertEquals(0, root_child1.getLayoutHeight(), 0.0f);
assertEquals(0, root_child2.getLayoutX(), 0.0f);
assertEquals(50, root_child2.getLayoutY(), 0.0f);
assertEquals(50, root_child2.getLayoutWidth(), 0.0f);
assertEquals(50, root_child2.getLayoutHeight(), 0.0f);
}
@Test
public void test_flex_basis_overrides_main_size() {
final CSSNode root = new CSSNode();
root.setStyleWidth(100);
root.setStyleHeight(100);
final CSSNode root_child0 = new CSSNode();
root_child0.setFlexGrow(1);
root_child0.setFlexBasis(50);
root_child0.setStyleHeight(20);
root.addChildAt(root_child0, 0);
final CSSNode root_child1 = new CSSNode();
root_child1.setFlexGrow(1);
root_child1.setStyleHeight(10);
root.addChildAt(root_child1, 1);
final CSSNode root_child2 = new CSSNode();
root_child2.setFlexGrow(1);
root_child2.setStyleHeight(10);
root.addChildAt(root_child2, 2);
root.setDirection(CSSDirection.LTR);
root.calculateLayout(null);
assertEquals(0, root.getLayoutX(), 0.0f);
assertEquals(0, root.getLayoutY(), 0.0f);
assertEquals(100, root.getLayoutWidth(), 0.0f);
assertEquals(100, root.getLayoutHeight(), 0.0f);
assertEquals(0, root_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0.getLayoutY(), 0.0f);
assertEquals(100, root_child0.getLayoutWidth(), 0.0f);
assertEquals(60, root_child0.getLayoutHeight(), 0.0f);
assertEquals(0, root_child1.getLayoutX(), 0.0f);
assertEquals(60, root_child1.getLayoutY(), 0.0f);
assertEquals(100, root_child1.getLayoutWidth(), 0.0f);
assertEquals(20, root_child1.getLayoutHeight(), 0.0f);
assertEquals(0, root_child2.getLayoutX(), 0.0f);
assertEquals(80, root_child2.getLayoutY(), 0.0f);
assertEquals(100, root_child2.getLayoutWidth(), 0.0f);
assertEquals(20, root_child2.getLayoutHeight(), 0.0f);
root.setDirection(CSSDirection.RTL);
root.calculateLayout(null);
assertEquals(0, root.getLayoutX(), 0.0f);
assertEquals(0, root.getLayoutY(), 0.0f);
assertEquals(100, root.getLayoutWidth(), 0.0f);
assertEquals(100, root.getLayoutHeight(), 0.0f);
assertEquals(0, root_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0.getLayoutY(), 0.0f);
assertEquals(100, root_child0.getLayoutWidth(), 0.0f);
assertEquals(60, root_child0.getLayoutHeight(), 0.0f);
assertEquals(0, root_child1.getLayoutX(), 0.0f);
assertEquals(60, root_child1.getLayoutY(), 0.0f);
assertEquals(100, root_child1.getLayoutWidth(), 0.0f);
assertEquals(20, root_child1.getLayoutHeight(), 0.0f);
assertEquals(0, root_child2.getLayoutX(), 0.0f);
assertEquals(80, root_child2.getLayoutY(), 0.0f);
assertEquals(100, root_child2.getLayoutWidth(), 0.0f);
assertEquals(20, root_child2.getLayoutHeight(), 0.0f);
}
@Test
public void test_flex_grow_shrink_at_most() {
final CSSNode root = new CSSNode();
root.setStyleWidth(100);
root.setStyleHeight(100);
final CSSNode root_child0 = new CSSNode();
root.addChildAt(root_child0, 0);
final CSSNode root_child0_child0 = new CSSNode();
root_child0_child0.setFlexGrow(1);
root_child0_child0.setFlexShrink(1);
root_child0.addChildAt(root_child0_child0, 0);
root.setDirection(CSSDirection.LTR);
root.calculateLayout(null);
assertEquals(0, root.getLayoutX(), 0.0f);
assertEquals(0, root.getLayoutY(), 0.0f);
assertEquals(100, root.getLayoutWidth(), 0.0f);
assertEquals(100, root.getLayoutHeight(), 0.0f);
assertEquals(0, root_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0.getLayoutY(), 0.0f);
assertEquals(100, root_child0.getLayoutWidth(), 0.0f);
assertEquals(0, root_child0.getLayoutHeight(), 0.0f);
assertEquals(0, root_child0_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0_child0.getLayoutY(), 0.0f);
assertEquals(100, root_child0_child0.getLayoutWidth(), 0.0f);
assertEquals(0, root_child0_child0.getLayoutHeight(), 0.0f);
root.setDirection(CSSDirection.RTL);
root.calculateLayout(null);
assertEquals(0, root.getLayoutX(), 0.0f);
assertEquals(0, root.getLayoutY(), 0.0f);
assertEquals(100, root.getLayoutWidth(), 0.0f);
assertEquals(100, root.getLayoutHeight(), 0.0f);
assertEquals(0, root_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0.getLayoutY(), 0.0f);
assertEquals(100, root_child0.getLayoutWidth(), 0.0f);
assertEquals(0, root_child0.getLayoutHeight(), 0.0f);
assertEquals(0, root_child0_child0.getLayoutX(), 0.0f);
assertEquals(0, root_child0_child0.getLayoutY(), 0.0f);
assertEquals(100, root_child0_child0.getLayoutWidth(), 0.0f);
assertEquals(0, root_child0_child0.getLayoutHeight(), 0.0f);
}
}