2016-10-06 06:04:57 -07:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using NUnit.Framework;
|
2016-10-07 11:07:50 -07:00
|
|
|
using System;
|
2016-10-06 06:04:57 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for {@link CSSNode}.
|
|
|
|
*/
|
|
|
|
namespace Facebook.CSSLayout
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class CSSNodeTest
|
|
|
|
{
|
|
|
|
[Test]
|
|
|
|
public void TestAddChildGetParent()
|
|
|
|
{
|
|
|
|
CSSNode parent = new CSSNode();
|
|
|
|
CSSNode child = new CSSNode();
|
2016-10-27 10:52:09 -07:00
|
|
|
|
2016-10-06 06:04:57 -07:00
|
|
|
Assert.IsNull(child.Parent);
|
|
|
|
Assert.AreEqual(0, parent.Count);
|
2016-10-27 10:52:09 -07:00
|
|
|
|
2016-10-06 06:04:57 -07:00
|
|
|
parent.Insert(0, child);
|
2016-10-27 10:52:09 -07:00
|
|
|
|
2016-10-06 06:04:57 -07:00
|
|
|
Assert.AreEqual(1, parent.Count);
|
|
|
|
Assert.AreEqual(child, parent[0]);
|
|
|
|
Assert.AreEqual(parent, child.Parent);
|
2016-10-27 10:52:09 -07:00
|
|
|
|
2016-10-06 06:04:57 -07:00
|
|
|
parent.RemoveAt(0);
|
2016-10-27 10:52:09 -07:00
|
|
|
|
2016-10-06 06:04:57 -07:00
|
|
|
Assert.IsNull(child.Parent);
|
|
|
|
Assert.AreEqual(0, parent.Count);
|
|
|
|
}
|
|
|
|
|
2016-10-26 06:51:39 -07:00
|
|
|
[Test]
|
|
|
|
public void TestChildren()
|
|
|
|
{
|
|
|
|
CSSNode parent = new CSSNode();
|
|
|
|
foreach (CSSNode node in parent) {
|
|
|
|
}
|
|
|
|
|
|
|
|
CSSNode child0 = new CSSNode();
|
|
|
|
Assert.AreEqual(-1, parent.IndexOf(child0));
|
|
|
|
parent.Insert(0, child0);
|
|
|
|
foreach (CSSNode node in parent) {
|
|
|
|
Assert.AreEqual(0, parent.IndexOf(node));
|
|
|
|
}
|
|
|
|
|
|
|
|
CSSNode child1 = new CSSNode();
|
|
|
|
parent.Insert(1, child1);
|
|
|
|
int index = 0;
|
|
|
|
foreach (CSSNode 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
[ExpectedException("System.NullReferenceException")]
|
|
|
|
public void TestRemoveAtFromEmpty()
|
|
|
|
{
|
|
|
|
CSSNode parent = new CSSNode();
|
|
|
|
parent.RemoveAt(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
[ExpectedException("System.ArgumentOutOfRangeException")]
|
|
|
|
public void TestRemoveAtOutOfRange()
|
|
|
|
{
|
|
|
|
CSSNode parent = new CSSNode();
|
|
|
|
CSSNode child = new CSSNode();
|
|
|
|
parent.Insert(0, child);
|
|
|
|
parent.RemoveAt(1);
|
|
|
|
}
|
|
|
|
|
2016-10-07 12:31:06 -07:00
|
|
|
[Test]
|
|
|
|
[ExpectedException("System.InvalidOperationException")]
|
|
|
|
public void TestCannotAddChildToMultipleParents()
|
|
|
|
{
|
|
|
|
CSSNode parent1 = new CSSNode();
|
|
|
|
CSSNode parent2 = new CSSNode();
|
|
|
|
CSSNode child = new CSSNode();
|
|
|
|
|
|
|
|
parent1.Insert(0, child);
|
|
|
|
parent2.Insert(0, child);
|
|
|
|
}
|
|
|
|
|
2016-10-06 06:04:57 -07:00
|
|
|
[Test]
|
2016-10-25 07:38:06 -07:00
|
|
|
public void TestReset()
|
2016-10-06 06:04:57 -07:00
|
|
|
{
|
2016-10-14 07:00:50 -07:00
|
|
|
int instanceCount = CSSNode.GetInstanceCount();
|
2016-10-06 06:04:57 -07:00
|
|
|
CSSNode node = new CSSNode();
|
2016-10-14 07:00:50 -07:00
|
|
|
Assert.AreEqual(instanceCount + 1, CSSNode.GetInstanceCount());
|
2016-10-25 07:38:06 -07:00
|
|
|
node.Reset();
|
|
|
|
Assert.AreEqual(instanceCount + 1, CSSNode.GetInstanceCount());
|
2016-10-07 11:07:51 -07:00
|
|
|
}
|
|
|
|
|
2016-10-13 07:52:44 -07:00
|
|
|
[Test]
|
|
|
|
[ExpectedException("System.InvalidOperationException")]
|
2016-10-25 07:38:06 -07:00
|
|
|
public void TestResetParent()
|
2016-10-13 07:52:44 -07:00
|
|
|
{
|
|
|
|
CSSNode parent = new CSSNode();
|
|
|
|
CSSNode child = new CSSNode();
|
|
|
|
parent.Insert(0, child);
|
2016-10-25 07:38:06 -07:00
|
|
|
parent.Reset();
|
2016-10-13 07:52:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
[ExpectedException("System.InvalidOperationException")]
|
2016-10-25 07:38:06 -07:00
|
|
|
public void TestResetChild()
|
2016-10-13 07:52:44 -07:00
|
|
|
{
|
|
|
|
CSSNode parent = new CSSNode();
|
|
|
|
CSSNode child = new CSSNode();
|
|
|
|
parent.Insert(0, child);
|
2016-10-25 07:38:06 -07:00
|
|
|
child.Reset();
|
2016-10-13 07:52:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2016-10-25 07:38:06 -07:00
|
|
|
public void TestClear()
|
2016-10-13 07:52:44 -07:00
|
|
|
{
|
2016-10-14 07:00:50 -07:00
|
|
|
int instanceCount = CSSNode.GetInstanceCount();
|
2016-10-13 07:52:44 -07:00
|
|
|
CSSNode parent = new CSSNode();
|
2016-10-25 07:38:06 -07:00
|
|
|
Assert.AreEqual(instanceCount + 1, CSSNode.GetInstanceCount());
|
|
|
|
CSSNode child = new CSSNode();
|
2016-10-14 07:00:50 -07:00
|
|
|
Assert.AreEqual(instanceCount + 2, CSSNode.GetInstanceCount());
|
2016-10-25 07:38:06 -07:00
|
|
|
parent.Insert(0, child);
|
2016-10-13 07:52:44 -07:00
|
|
|
Assert.AreEqual(1, parent.Count);
|
2016-10-25 07:38:06 -07:00
|
|
|
Assert.AreEqual(parent, child.Parent);
|
|
|
|
parent.Clear();
|
|
|
|
Assert.AreEqual(0, parent.Count);
|
|
|
|
Assert.IsNull(child.Parent);
|
|
|
|
Assert.AreEqual(instanceCount + 2, CSSNode.GetInstanceCount());
|
2016-10-13 07:52:44 -07:00
|
|
|
}
|
|
|
|
|
2016-10-13 07:52:46 -07:00
|
|
|
[Test]
|
|
|
|
public void TestMeasureFunc()
|
|
|
|
{
|
|
|
|
CSSNode node = new CSSNode();
|
2016-10-27 10:52:09 -07:00
|
|
|
node.SetMeasureFunction((_, width, widthMode, height, heightMode) => {
|
|
|
|
return MeasureOutput.Make(100, 150);
|
2016-10-13 07:52:46 -07:00
|
|
|
});
|
|
|
|
node.CalculateLayout();
|
|
|
|
Assert.AreEqual(100, (int)node.LayoutWidth);
|
|
|
|
Assert.AreEqual(150, (int)node.LayoutHeight);
|
|
|
|
}
|
|
|
|
|
2016-10-19 11:01:24 -07:00
|
|
|
[Test]
|
|
|
|
public void TestPrint()
|
|
|
|
{
|
|
|
|
CSSNode parent = new CSSNode();
|
|
|
|
parent.StyleWidth = 100;
|
|
|
|
parent.StyleHeight = 120;
|
|
|
|
CSSNode child0 = new CSSNode();
|
|
|
|
child0.StyleWidth = 30;
|
|
|
|
child0.StyleHeight = 40;
|
|
|
|
CSSNode child1 = new CSSNode();
|
|
|
|
child1.StyleWidth = 35;
|
|
|
|
child1.StyleHeight = 45;
|
|
|
|
parent.Insert(0, child0);
|
|
|
|
parent.Insert(0, child1);
|
|
|
|
parent.CalculateLayout();
|
|
|
|
Assert.AreEqual(parent.Print(), "{layout: {width: 100, height: 120, top: 0, left: 0}, flexDirection: 'column', alignItems: 'stretch', flexGrow: 0, flexShrink: 0, overflow: 'visible', width: 100, height: 120, children: [\n {layout: {width: 35, height: 45, top: 0, left: 0}, flexDirection: 'column', alignItems: 'stretch', flexGrow: 0, flexShrink: 0, overflow: 'visible', width: 35, height: 45, },\n {layout: {width: 30, height: 40, top: 45, left: 0}, flexDirection: 'column', alignItems: 'stretch', flexGrow: 0, flexShrink: 0, overflow: 'visible', width: 30, height: 40, },\n]},\n");
|
|
|
|
}
|
|
|
|
|
2016-10-07 11:07:50 -07:00
|
|
|
private void ForceGC()
|
|
|
|
{
|
|
|
|
GC.Collect(GC.MaxGeneration);
|
|
|
|
GC.WaitForPendingFinalizers();
|
2016-10-06 06:04:57 -07:00
|
|
|
}
|
2016-10-14 07:00:50 -07:00
|
|
|
|
|
|
|
#if !UNITY_EDITOR
|
|
|
|
[Test]
|
|
|
|
public void TestDestructor()
|
|
|
|
{
|
|
|
|
ForceGC();
|
|
|
|
int instanceCount = CSSNode.GetInstanceCount();
|
|
|
|
TestDestructorForGC(instanceCount);
|
|
|
|
ForceGC();
|
|
|
|
Assert.AreEqual(instanceCount, CSSNode.GetInstanceCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void TestDestructorForGC(int instanceCount)
|
|
|
|
{
|
|
|
|
CSSNode node = new CSSNode();
|
|
|
|
Assert.IsNotNull(node);
|
|
|
|
Assert.AreEqual(instanceCount + 1, CSSNode.GetInstanceCount());
|
|
|
|
node = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestDestructorWithChildren()
|
|
|
|
{
|
|
|
|
ForceGC();
|
|
|
|
int instanceCount = CSSNode.GetInstanceCount();
|
|
|
|
TestDestructorWithChildrenForGC1(instanceCount);
|
|
|
|
ForceGC();
|
|
|
|
Assert.AreEqual(instanceCount, CSSNode.GetInstanceCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void TestDestructorWithChildrenForGC1(int instanceCount)
|
|
|
|
{
|
|
|
|
CSSNode node = new CSSNode();
|
|
|
|
Assert.AreEqual(instanceCount + 1, CSSNode.GetInstanceCount());
|
|
|
|
|
|
|
|
TestDestructorWithChildrenForGC2(node, instanceCount + 1);
|
|
|
|
ForceGC();
|
|
|
|
Assert.AreEqual(instanceCount + 2, CSSNode.GetInstanceCount());
|
|
|
|
|
|
|
|
TestDestructorWithChildrenForGC2(node, instanceCount + 2);
|
|
|
|
ForceGC();
|
|
|
|
Assert.AreEqual(instanceCount + 3, CSSNode.GetInstanceCount());
|
|
|
|
|
|
|
|
node = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void TestDestructorWithChildrenForGC2(CSSNode parent, int instanceCount)
|
|
|
|
{
|
|
|
|
CSSNode child = new CSSNode();
|
|
|
|
Assert.AreEqual(instanceCount + 1, CSSNode.GetInstanceCount());
|
|
|
|
|
|
|
|
parent.Insert(0, child);
|
|
|
|
child = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2016-10-25 07:38:06 -07:00
|
|
|
public void TestParentDestructor()
|
2016-10-14 07:00:50 -07:00
|
|
|
{
|
|
|
|
ForceGC();
|
|
|
|
int instanceCount = CSSNode.GetInstanceCount();
|
2016-10-25 07:38:06 -07:00
|
|
|
CSSNode child = new CSSNode();
|
|
|
|
Assert.AreEqual(instanceCount + 1, CSSNode.GetInstanceCount());
|
|
|
|
|
|
|
|
TestParentDestructorForGC(child, instanceCount + 1);
|
|
|
|
ForceGC();
|
|
|
|
|
|
|
|
Assert.IsNull(child.Parent);
|
|
|
|
Assert.AreEqual(instanceCount + 1, CSSNode.GetInstanceCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void TestParentDestructorForGC(CSSNode child, int instanceCount)
|
|
|
|
{
|
2016-10-14 07:00:50 -07:00
|
|
|
CSSNode parent = new CSSNode();
|
|
|
|
Assert.AreEqual(instanceCount + 1, CSSNode.GetInstanceCount());
|
2016-10-25 07:38:06 -07:00
|
|
|
parent.Insert(0, child);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestClearWithChildDestructor()
|
|
|
|
{
|
|
|
|
ForceGC();
|
|
|
|
int instanceCount = CSSNode.GetInstanceCount();
|
|
|
|
CSSNode node = new CSSNode();
|
|
|
|
Assert.AreEqual(instanceCount + 1, CSSNode.GetInstanceCount());
|
|
|
|
TestClearWithChildDestructorForGC(node, instanceCount + 1);
|
2016-10-14 07:00:50 -07:00
|
|
|
ForceGC();
|
|
|
|
Assert.AreEqual(instanceCount + 2, CSSNode.GetInstanceCount());
|
2016-10-25 07:38:06 -07:00
|
|
|
node.Clear();
|
|
|
|
Assert.AreEqual(0, node.Count);
|
2016-10-14 07:00:50 -07:00
|
|
|
ForceGC();
|
2016-10-25 07:38:06 -07:00
|
|
|
Assert.AreEqual(instanceCount + 1, CSSNode.GetInstanceCount());
|
2016-10-14 07:00:50 -07:00
|
|
|
}
|
|
|
|
|
2016-10-25 07:38:06 -07:00
|
|
|
private void TestClearWithChildDestructorForGC(CSSNode parent, int instanceCount)
|
2016-10-14 07:00:50 -07:00
|
|
|
{
|
|
|
|
CSSNode child = new CSSNode();
|
|
|
|
Assert.AreEqual(instanceCount + 1, CSSNode.GetInstanceCount());
|
|
|
|
parent.Insert(0, child);
|
|
|
|
}
|
2016-10-26 06:51:39 -07:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestMeasureFuncWithDestructor()
|
|
|
|
{
|
|
|
|
ForceGC();
|
|
|
|
int instanceCount = CSSNode.GetInstanceCount();
|
|
|
|
CSSNode parent = new CSSNode();
|
|
|
|
Assert.AreEqual(instanceCount + 1, CSSNode.GetInstanceCount());
|
|
|
|
TestMeasureFuncWithDestructorForGC(parent);
|
|
|
|
ForceGC();
|
|
|
|
Assert.AreEqual(instanceCount + 2, CSSNode.GetInstanceCount());
|
|
|
|
parent.CalculateLayout();
|
|
|
|
Assert.AreEqual(120, (int)parent.LayoutWidth);
|
|
|
|
Assert.AreEqual(130, (int)parent.LayoutHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void TestMeasureFuncWithDestructorForGC(CSSNode parent)
|
|
|
|
{
|
|
|
|
CSSNode child = new CSSNode();
|
|
|
|
parent.Insert(0, child);
|
|
|
|
child.SetMeasureFunction((_, width, widthMode, height, heightMode, measureResult) => {
|
|
|
|
measureResult.Width = 120;
|
|
|
|
measureResult.Height = 130;
|
|
|
|
});
|
|
|
|
}
|
2016-10-14 07:00:50 -07:00
|
|
|
#endif
|
2016-10-06 06:04:57 -07:00
|
|
|
}
|
|
|
|
}
|