From 63570613d303e23f88d6de7aa3c1a9d3a536bfd6 Mon Sep 17 00:00:00 2001 From: Sidharth Guglani Date: Wed, 14 Nov 2018 04:00:48 -0800 Subject: [PATCH] added basic java, csharp and javascript tests for reference baseline feature Summary: @public We added a functionality using which child node can tell parent node to use it as a reference baseline. Added some tests for java, csharp, javascript language bindings. Reviewed By: davidaurelio Differential Revision: D12997442 fbshipit-source-id: 4717167d2b3862bf2af87b663bda82f9c1eae33e --- .../Facebook.Yoga/YGAlignBaselineTest.cs | 104 ++++++++++++ .../facebook/yoga/YGAlignBaselineTest.java | 122 ++++++++++++++ .../Facebook.Yoga/YGAlignBaselineTest.js | 156 ++++++++++++++++++ 3 files changed, 382 insertions(+) create mode 100644 csharp/tests/Facebook.Yoga/YGAlignBaselineTest.cs create mode 100644 java/tests/com/facebook/yoga/YGAlignBaselineTest.java create mode 100644 javascript/tests/Facebook.Yoga/YGAlignBaselineTest.js diff --git a/csharp/tests/Facebook.Yoga/YGAlignBaselineTest.cs b/csharp/tests/Facebook.Yoga/YGAlignBaselineTest.cs new file mode 100644 index 00000000..ea62d3ae --- /dev/null +++ b/csharp/tests/Facebook.Yoga/YGAlignBaselineTest.cs @@ -0,0 +1,104 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +using System; +using NUnit.Framework; + +namespace Facebook.Yoga +{ + [TestFixture] + public class YGAlignBaselineTest + { + [Test] + public void Test_align_baseline_parent_using_child_in_column_as_reference() + { + YogaConfig config = new YogaConfig(); + YogaNode root = createYGNode(config, YogaFlexDirection.Row, 1000, 1000, true); + + YogaNode root_child0 = createYGNode(config, YogaFlexDirection.Column, 500, 600, false); + root.Insert(0, root_child0); + + YogaNode root_child1 = createYGNode(config, YogaFlexDirection.Column, 500, 800, false); + root.Insert(1, root_child1); + + YogaNode root_child1_child0 = createYGNode(config, YogaFlexDirection.Column, 500, 300, false); + root_child1.Insert(0, root_child1_child0); + + YogaNode root_child1_child1 = createYGNode(config, YogaFlexDirection.Column, 500, 400, false); + root_child1_child1.SetBaselineFunction((_, width, height) => { + return height / 2; + }); + root_child1_child1.IsReferenceBaseline = true; + root_child1.Insert(1, root_child1_child1); + + root.CalculateLayout(); + + Assert.AreEqual(0f, root_child0.LayoutX); + Assert.AreEqual(0f, root_child0.LayoutY); + + Assert.AreEqual(500f, root_child1.LayoutX); + Assert.AreEqual(100f, root_child1.LayoutY); + + Assert.AreEqual(0f, root_child1_child0.LayoutX); + Assert.AreEqual(0f, root_child1_child0.LayoutY); + + Assert.AreEqual(0f, root_child1_child1.LayoutX); + Assert.AreEqual(300f, root_child1_child1.LayoutY); + } + + [Test] + public void Test_align_baseline_parent_using_child_in_row_as_reference() + { + YogaConfig config = new YogaConfig(); + + YogaNode root = createYGNode(config, YogaFlexDirection.Row, 1000, 1000, true); + + YogaNode root_child0 = createYGNode(config, YogaFlexDirection.Column, 500, 600, false); + root.Insert(0, root_child0); + + YogaNode root_child1 = createYGNode(config, YogaFlexDirection.Row, 500, 800, true); + root.Insert(1, root_child1); + + YogaNode root_child1_child0 = createYGNode(config, YogaFlexDirection.Row, 500, 500, false); + root_child1.Insert(0, root_child1_child0); + + YogaNode root_child1_child1 = createYGNode(config, YogaFlexDirection.Row, 500, 400, false); + root_child1_child1.SetBaselineFunction((_, width, height) => { + return height / 2; + }); + root_child1_child1.IsReferenceBaseline = true; + root_child1.Insert(1, root_child1_child1); + + root.StyleDirection = YogaDirection.LTR; + + root.CalculateLayout(); + + Assert.AreEqual(0f, root_child0.LayoutX); + Assert.AreEqual(0f, root_child0.LayoutY); + + Assert.AreEqual(500f, root_child1.LayoutX); + Assert.AreEqual(100f, root_child1.LayoutY); + + Assert.AreEqual(0f, root_child1_child0.LayoutX); + Assert.AreEqual(0f, root_child1_child0.LayoutY); + + Assert.AreEqual(500f, root_child1_child1.LayoutX); + Assert.AreEqual(300f, root_child1_child1.LayoutY); + } + + private YogaNode createYGNode(YogaConfig config, YogaFlexDirection flexDirection, int width, int height, bool alignBaseline) { + YogaNode node = new YogaNode(config); + node.FlexDirection = flexDirection; + node.Width = width; + node.Height = height; + if (alignBaseline) { + node.AlignItems = YogaAlign.Baseline; + } + return node; + } + } +} diff --git a/java/tests/com/facebook/yoga/YGAlignBaselineTest.java b/java/tests/com/facebook/yoga/YGAlignBaselineTest.java new file mode 100644 index 00000000..46258b41 --- /dev/null +++ b/java/tests/com/facebook/yoga/YGAlignBaselineTest.java @@ -0,0 +1,122 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +package com.facebook.yoga; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class YGAlignBaselineTest { + @Parameterized.Parameters(name = "{0}") + public static Iterable nodeFactories() { + return TestParametrization.nodeFactories(); + } + + @Parameterized.Parameter public TestParametrization.NodeFactory mNodeFactory; + + private YogaBaselineFunction getBaselineFunc() { + return new YogaBaselineFunction() { + @Override + public float baseline(YogaNode node, float width, float height) { + return height / 2; + } + }; + } + + @Test + public void test_align_baseline_parent_using_child_in_column_as_reference() { + YogaConfig config = new YogaConfig(); + + final YogaNode root = createYGNode(config, YogaFlexDirection.ROW, 1000f, 1000f, true); + + final YogaNode root_child0 = createYGNode(config, YogaFlexDirection.COLUMN, 500f, 600f, false); + root.addChildAt(root_child0, 0); + + final YogaNode root_child1 = createYGNode(config, YogaFlexDirection.COLUMN, 500f, 800f, false); + root.addChildAt(root_child1, 1); + + final YogaNode root_child1_child0 = + createYGNode(config, YogaFlexDirection.COLUMN, 500f, 300f, false); + root_child1.addChildAt(root_child1_child0, 0); + + final YogaNode root_child1_child1 = + createYGNode(config, YogaFlexDirection.COLUMN, 500f, 400f, false); + root_child1_child1.setBaselineFunction(getBaselineFunc()); + root_child1_child1.setIsReferenceBaseline(true); + root_child1.addChildAt(root_child1_child1, 1); + + root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); + + assertEquals(0f, root_child0.getLayoutX(), 0.0f); + assertEquals(0f, root_child0.getLayoutY(), 0.0f); + + assertEquals(500f, root_child1.getLayoutX(), 0.0f); + assertEquals(100f, root_child1.getLayoutY(), 0.0f); + + assertEquals(0f, root_child1_child0.getLayoutX(), 0.0f); + assertEquals(0f, root_child1_child0.getLayoutY(), 0.0f); + + assertEquals(0f, root_child1_child1.getLayoutX(), 0.0f); + assertEquals(300f, root_child1_child1.getLayoutY(), 0.0f); + } + + @Test + public void test_align_baseline_parent_using_child_in_row_as_reference() { + YogaConfig config = new YogaConfig(); + + final YogaNode root = createYGNode(config, YogaFlexDirection.ROW, 1000f, 1000f, true); + + final YogaNode root_child0 = createYGNode(config, YogaFlexDirection.COLUMN, 500f, 600f, false); + root.addChildAt(root_child0, 0); + + final YogaNode root_child1 = createYGNode(config, YogaFlexDirection.ROW, 500f, 800f, true); + root.addChildAt(root_child1, 1); + + final YogaNode root_child1_child0 = + createYGNode(config, YogaFlexDirection.COLUMN, 500f, 500f, false); + root_child1.addChildAt(root_child1_child0, 0); + + final YogaNode root_child1_child1 = + createYGNode(config, YogaFlexDirection.COLUMN, 500f, 400f, false); + root_child1_child1.setBaselineFunction(getBaselineFunc()); + root_child1_child1.setIsReferenceBaseline(true); + root_child1.addChildAt(root_child1_child1, 1); + + root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED); + + assertEquals(0f, root_child0.getLayoutX(), 0.0f); + assertEquals(0f, root_child0.getLayoutY(), 0.0f); + + assertEquals(500f, root_child1.getLayoutX(), 0.0f); + assertEquals(100f, root_child1.getLayoutY(), 0.0f); + + assertEquals(0f, root_child1_child0.getLayoutX(), 0.0f); + assertEquals(0f, root_child1_child0.getLayoutY(), 0.0f); + + assertEquals(500f, root_child1_child1.getLayoutX(), 0.0f); + assertEquals(300f, root_child1_child1.getLayoutY(), 0.0f); + } + + private YogaNode createYGNode( + YogaConfig config, + YogaFlexDirection flexDirection, + float width, + float height, + boolean alignBaseline) { + YogaNode node = mNodeFactory.create(config); + node.setFlexDirection(flexDirection); + node.setWidth(width); + node.setHeight(height); + if (alignBaseline) { + node.setAlignItems(YogaAlign.BASELINE); + } + return node; + } +} diff --git a/javascript/tests/Facebook.Yoga/YGAlignBaselineTest.js b/javascript/tests/Facebook.Yoga/YGAlignBaselineTest.js new file mode 100644 index 00000000..34f186f4 --- /dev/null +++ b/javascript/tests/Facebook.Yoga/YGAlignBaselineTest.js @@ -0,0 +1,156 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +var Yoga = Yoga || require("../../sources/entry-" + process.env.TEST_ENTRY); + +it("align_baseline_parent_using_child_in_column_as_reference", function () { + var config = Yoga.Config.create(); + + try { + var root = Yoga.Node.create(config); + root.setFlexDirection(Yoga.FLEX_DIRECTION_ROW); + root.setWidth(1000); + root.setHeight(1000); + root.setAlignItems(Yoga.ALIGN_BASELINE); + + var root_child0 = Yoga.Node.create(config); + root_child0.setFlexDirection(Yoga.FLEX_DIRECTION_COLUMN); + root_child0.setWidth(500); + root_child0.setHeight(600); + root.insertChild(root_child0, 0); + + var root_child1 = Yoga.Node.create(config); + root_child1.setFlexDirection(Yoga.FLEX_DIRECTION_COLUMN); + root_child1.setWidth(500); + root_child1.setHeight(800); + root.insertChild(root_child1, 1); + + var root_child1_child0 = Yoga.Node.create(config); + root_child1_child0.setFlexDirection(Yoga.FLEX_DIRECTION_COLUMN); + root_child1_child0.setWidth(500); + root_child1_child0.setHeight(300); + root_child1.insertChild(root_child1_child0, 0); + + var root_child1_child1 = Yoga.Node.create(config); + root_child1_child1.setFlexDirection(Yoga.FLEX_DIRECTION_COLUMN); + root_child1_child1.setWidth(500); + root_child1_child1.setHeight(400); + root_child1_child1.setIsReferenceBaseline(true); + root_child1.insertChild(root_child1_child1, 1); + + root.calculateLayout(Yoga.UNDEFINED, Yoga.UNDEFINED, Yoga.DIRECTION_LTR); + + console.assert(0 === root_child0.getComputedLeft(), + "0 === root_child0.getComputedLeft() (" + + root_child0.getComputedLeft() + ")"); + console.assert(100 === root_child0.getComputedTop(), + "100 === root_child0.getComputedTop() (" + + root_child0.getComputedTop() + ")"); + + console.assert(500 === root_child1.getComputedLeft(), + "500 === root_child1.getComputedLeft() (" + + root_child1.getComputedLeft() + ")"); + console.assert(0 === root_child1.getComputedTop(), + "0 === root_child1.getComputedTop() (" + + root_child1.getComputedTop() + ")"); + + console.assert(0 === root_child1_child0.getComputedLeft(), + "0 === root_child1_child0.getComputedLeft() (" + + root_child1_child0.getComputedLeft() + ")"); + console.assert(0 === root_child1_child0.getComputedTop(), + "0 === root_child1_child0.getComputedTop() (" + + root_child1_child0.getComputedTop() + ")"); + + console.assert(0 === root_child1_child1.getComputedLeft(), + "0 === root_child1_child1.getComputedLeft() (" + + root_child1_child1.getComputedLeft() + ")"); + console.assert(300 === root_child1_child1.getComputedTop(), + "300 === root_child1_child1.getComputedTop() (" + + root_child1_child1.getComputedTop() + ")"); + + } finally { + if (typeof root !== "undefined") { + root.freeRecursive(); + } + + config.free(); + } +}); + +it("align_baseline_parent_using_child_in_row_as_reference", function () { + var config = Yoga.Config.create(); + + try { + var root = Yoga.Node.create(config); + root.setFlexDirection(Yoga.FLEX_DIRECTION_ROW); + root.setWidth(1000); + root.setHeight(1000); + root.setAlignItems(Yoga.ALIGN_BASELINE); + + var root_child0 = Yoga.Node.create(config); + root_child0.setFlexDirection(Yoga.FLEX_DIRECTION_COLUMN); + root_child0.setWidth(500); + root_child0.setHeight(600); + root.insertChild(root_child0, 0); + + var root_child1 = Yoga.Node.create(config); + root_child1.setFlexDirection(Yoga.FLEX_DIRECTION_ROW); + root_child1.setWidth(500); + root_child1.setHeight(800); + root.insertChild(root_child1, 1); + + var root_child1_child0 = Yoga.Node.create(config); + root_child1_child0.setFlexDirection(Yoga.FLEX_DIRECTION_COLUMN); + root_child1_child0.setWidth(500); + root_child1_child0.setHeight(500); + root_child1.insertChild(root_child1_child0, 0); + + var root_child1_child1 = Yoga.Node.create(config); + root_child1_child1.setFlexDirection(Yoga.FLEX_DIRECTION_COLUMN); + root_child1_child1.setWidth(500); + root_child1_child1.setHeight(400); + root_child1_child1.setIsReferenceBaseline(true); + root_child1.insertChild(root_child1_child1, 1); + + root.calculateLayout(Yoga.UNDEFINED, Yoga.UNDEFINED, Yoga.DIRECTION_LTR); + + console.assert(0 === root_child0.getComputedLeft(), + "0 === root_child0.getComputedLeft() (" + + root_child0.getComputedLeft() + ")"); + console.assert(0 === root_child0.getComputedTop(), + "0 === root_child0.getComputedTop() (" + + root_child0.getComputedTop() + ")"); + + console.assert(500 === root_child1.getComputedLeft(), + "500 === root_child1.getComputedLeft() (" + + root_child1.getComputedLeft() + ")"); + console.assert(200 === root_child1.getComputedTop(), + "200 === root_child1.getComputedTop() (" + + root_child1.getComputedTop() + ")"); + + console.assert(0 === root_child1_child0.getComputedLeft(), + "0 === root_child1_child0.getComputedLeft() (" + + root_child1_child0.getComputedLeft() + ")"); + console.assert(0 === root_child1_child0.getComputedTop(), + "0 === root_child1_child0.getComputedTop() (" + + root_child1_child0.getComputedTop() + ")"); + + console.assert(500 === root_child1_child1.getComputedLeft(), + "500 === root_child1_child1.getComputedLeft() (" + + root_child1_child1.getComputedLeft() + ")"); + console.assert(0 === root_child1_child1.getComputedTop(), + "0 === root_child1_child1.getComputedTop() (" + + root_child1_child1.getComputedTop() + ")"); + + } finally { + if (typeof root !== "undefined") { + root.freeRecursive(); + } + + config.free(); + } +});