/* * Copyright (c) Meta Platforms, Inc. and 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 Microsoft.VisualStudio.TestPlatform.UnitTestFramework; using Facebook.Yoga; namespace Facebook.Yoga.Universal.Tests { [TestClass] public class YogaNodeTest { [TestMethod] public void TestAddChildGetParent() { YogaNode parent = new YogaNode(); YogaNode child = new YogaNode(); Assert.IsNull(child.Parent); Assert.AreEqual(0, parent.Count); parent.Insert(0, child); Assert.AreEqual(1, parent.Count); Assert.AreEqual(child, parent[0]); Assert.AreEqual(parent, child.Parent); parent.RemoveAt(0); Assert.IsNull(child.Parent); Assert.AreEqual(0, parent.Count); } [TestMethod] public void TestChildren() { YogaNode parent = new YogaNode(); foreach (YogaNode node in parent) { Assert.Fail(node.ToString()); } YogaNode child0 = new YogaNode(); Assert.AreEqual(-1, parent.IndexOf(child0)); parent.Insert(0, child0); foreach (YogaNode node in parent) { Assert.AreEqual(0, parent.IndexOf(node)); } YogaNode child1 = new YogaNode(); parent.Insert(1, child1); int index = 0; foreach (YogaNode node in parent) { Assert.AreEqual(index++, parent.IndexOf(node)); } parent.RemoveAt(0); Assert.AreEqual(-1, parent.IndexOf(child0)); Assert.AreEqual(0, parent.IndexOf(child1)); parent.Clear(); Assert.AreEqual(0, parent.Count); parent.Clear(); Assert.AreEqual(0, parent.Count); } [TestMethod] public void TestRemoveAtFromEmpty() { YogaNode parent = new YogaNode(); try { parent.RemoveAt(0); } catch (System.NullReferenceException) { return; } Assert.Fail("Excepted exception of type 'System.NullReferenceException'."); } [TestMethod] public void TestRemoveAtOutOfRange() { YogaNode parent = new YogaNode(); YogaNode child = new YogaNode(); parent.Insert(0, child); try { parent.RemoveAt(1); } catch (System.ArgumentOutOfRangeException) { return; } Assert.Fail("Excepted exception of type 'System.ArgumentOutOfRangeException'."); } [TestMethod] public void TestCannotAddChildToMultipleParents() { YogaNode parent1 = new YogaNode(); YogaNode parent2 = new YogaNode(); YogaNode child = new YogaNode(); parent1.Insert(0, child); try { parent2.Insert(0, child); } catch (System.InvalidOperationException) { return; } Assert.Fail("Excepted exception of type 'System.InvalidOperationException'."); } [TestMethod] public void TestReset() { int instanceCount = YogaNode.GetInstanceCount(); YogaNode node = new YogaNode(); Assert.AreEqual(instanceCount + 1, YogaNode.GetInstanceCount()); node.Reset(); Assert.AreEqual(instanceCount + 1, YogaNode.GetInstanceCount()); } [TestMethod] public void TestResetParent() { YogaNode parent = new YogaNode(); YogaNode child = new YogaNode(); parent.Insert(0, child); try { parent.Reset(); } catch (System.InvalidOperationException) { return; } Assert.Fail("Excepted exception of type 'System.InvalidOperationException'."); } [TestMethod] public void TestResetChild() { YogaNode parent = new YogaNode(); YogaNode child = new YogaNode(); parent.Insert(0, child); try { child.Reset(); } catch (System.InvalidOperationException) { return; } Assert.Fail("Excepted exception of type 'System.InvalidOperationException'."); } [TestMethod] public void TestClear() { int instanceCount = YogaNode.GetInstanceCount(); YogaNode parent = new YogaNode(); Assert.AreEqual(instanceCount + 1, YogaNode.GetInstanceCount()); YogaNode child = new YogaNode(); Assert.AreEqual(instanceCount + 2, YogaNode.GetInstanceCount()); parent.Insert(0, child); Assert.AreEqual(1, parent.Count); Assert.AreEqual(parent, child.Parent); parent.Clear(); Assert.AreEqual(0, parent.Count); Assert.IsNull(child.Parent); Assert.AreEqual(instanceCount + 2, YogaNode.GetInstanceCount()); } [TestMethod] public void TestMeasureFunc() { YogaNode node = new YogaNode(); node.SetMeasureFunction((_, width, widthMode, height, heightMode) => { return MeasureOutput.Make(100, 150); }); node.CalculateLayout(); Assert.AreEqual(100, node.LayoutWidth); Assert.AreEqual(150, node.LayoutHeight); } [TestMethod] public void TestMeasureFuncWithFloat() { YogaNode node = new YogaNode(); node.SetMeasureFunction((_, width, widthMode, height, heightMode) => { return MeasureOutput.Make(123.4f, 81.7f); }); node.CalculateLayout(); Assert.AreEqual(124, node.LayoutWidth); Assert.AreEqual(82, node.LayoutHeight); } [TestMethod] public void TestChildWithMeasureFunc() { YogaNode node = new YogaNode(); node.SetMeasureFunction((_, width, widthMode, height, heightMode) => { return MeasureOutput.Make(100, 150); }); YogaNode child = new YogaNode(); try { node.Insert(0, child); } catch (System.InvalidOperationException) { return; } Assert.Fail("Excepted exception of type 'System.InvalidOperationException'."); } [TestMethod] public void TestMeasureFuncWithChild() { YogaNode node = new YogaNode(); YogaNode child = new YogaNode(); node.Insert(0, child); try { node.SetMeasureFunction((_, width, widthMode, height, heightMode) => { return MeasureOutput.Make(100, 150); }); } catch (System.InvalidOperationException) { return; } Assert.Fail("Excepted exception of type 'System.InvalidOperationException'."); } [TestMethod] public void TestPrint() { YogaNode parent = new YogaNode(); parent.Width = 100; parent.Height = 120; YogaNode child0 = new YogaNode(); child0.Width = 30; child0.Height = 40; YogaNode child1 = new YogaNode(); child1.Width = 35; child1.Height = 45; parent.Insert(0, child0); parent.Insert(0, child1); parent.CalculateLayout(); Assert.AreEqual(parent.Print(), "