From 3ea76f8a9b066ee9b67447bb99fa23f4224a7134 Mon Sep 17 00:00:00 2001 From: Kazuki Sakamoto Date: Mon, 10 Apr 2017 14:22:24 -0700 Subject: [PATCH] Add YogaConfig unit tests Summary: - depends on #497 Closes https://github.com/facebook/yoga/pull/498 Reviewed By: emilsjolander Differential Revision: D4796199 Pulled By: splhack fbshipit-source-id: c63f23da11a719b36c0d49e954b29c0016cad8c7 --- .../Facebook.Yoga.Shared.Tests.projitems | 1 + csharp/tests/Facebook.Yoga/YogaConfigTest.cs | 136 ++++++++++++++++++ csharp/tests/Facebook.Yoga/YogaNodeTest.cs | 2 +- 3 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 csharp/tests/Facebook.Yoga/YogaConfigTest.cs diff --git a/csharp/tests/Facebook.Yoga/Facebook.Yoga.Shared.Tests.projitems b/csharp/tests/Facebook.Yoga/Facebook.Yoga.Shared.Tests.projitems index 0afe5e63..3bbfd970 100644 --- a/csharp/tests/Facebook.Yoga/Facebook.Yoga.Shared.Tests.projitems +++ b/csharp/tests/Facebook.Yoga/Facebook.Yoga.Shared.Tests.projitems @@ -22,6 +22,7 @@ + diff --git a/csharp/tests/Facebook.Yoga/YogaConfigTest.cs b/csharp/tests/Facebook.Yoga/YogaConfigTest.cs new file mode 100644 index 00000000..0f678da6 --- /dev/null +++ b/csharp/tests/Facebook.Yoga/YogaConfigTest.cs @@ -0,0 +1,136 @@ +/** + * 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; +using System; + +/** + * Tests for {@link YogaConfig}. + */ +namespace Facebook.Yoga +{ + [TestFixture] + public class YogaConfigTest + { + [Test] + public void TestUseWebDefaults() + { + YogaNode node0 = new YogaNode(new YogaConfig{UseWebDefaults = true}); + Assert.AreEqual(YogaFlexDirection.Row, node0.FlexDirection); + + node0.Reset(); + Assert.AreEqual(YogaFlexDirection.Row, node0.FlexDirection); + + YogaConfig config = new YogaConfig(); + config.UseWebDefaults = true; + YogaNode node1 = new YogaNode(config); + Assert.AreEqual(YogaFlexDirection.Row, node1.FlexDirection); + + node1.Reset(); + Assert.AreEqual(YogaFlexDirection.Row, node1.FlexDirection); + } + + [Test] + public void TestDefaultConfig() + { + YogaNode node0 = new YogaNode(); + Assert.AreEqual(YogaFlexDirection.Column, node0.FlexDirection); + + YogaNode node1 = new YogaNode(new YogaConfig()); + Assert.AreEqual(YogaFlexDirection.Column, node1.FlexDirection); + } + + [Test] + public void TestCopyConstructor() + { + YogaNode srcNode = new YogaNode(new YogaConfig{UseWebDefaults = true}); + YogaNode node0 = new YogaNode(srcNode); + Assert.AreEqual(YogaFlexDirection.Row, node0.FlexDirection); + + node0.FlexDirection = YogaFlexDirection.Column; + Assert.AreEqual(YogaFlexDirection.Column, node0.FlexDirection); + + node0.Reset(); + Assert.AreEqual(YogaFlexDirection.Row, node0.FlexDirection); + + YogaNode node1 = new YogaNode(srcNode) + { + FlexDirection = YogaFlexDirection.Column + }; + Assert.AreEqual(YogaFlexDirection.Column, node1.FlexDirection); + + node1.Reset(); + Assert.AreEqual(YogaFlexDirection.Row, node1.FlexDirection); + } + + public static void ForceGC() + { + YogaNodeTest.ForceGC(); + } + + [Test] + public void TestDestructor() + { + ForceGC(); + int instanceCount = YogaConfig.GetInstanceCount(); + TestDestructorForGC(instanceCount); + ForceGC(); + Assert.AreEqual(instanceCount, YogaConfig.GetInstanceCount()); + } + + private void TestDestructorForGC(int instanceCount) + { + YogaConfig config = new YogaConfig(); + Assert.IsNotNull(config); + Assert.AreEqual(instanceCount + 1, YogaConfig.GetInstanceCount()); + config = null; + } + + [Test] + public void TestRetainConfig() + { + ForceGC(); + int nodeInstanceCount = YogaNode.GetInstanceCount(); + int configInstanceCount = YogaConfig.GetInstanceCount(); + TestRetainConfigForGC(nodeInstanceCount, configInstanceCount); + ForceGC(); + + Assert.AreEqual(nodeInstanceCount, YogaNode.GetInstanceCount()); + Assert.AreEqual(configInstanceCount, YogaConfig.GetInstanceCount()); + } + + private void TestRetainConfigForGC(int nodeInstanceCount, int configInstanceCount) + { + ForceGC(); + Assert.AreEqual(nodeInstanceCount, YogaNode.GetInstanceCount()); + Assert.AreEqual(configInstanceCount, YogaConfig.GetInstanceCount()); + YogaNode node = TestRetainConfigForGC2(nodeInstanceCount, configInstanceCount); + ForceGC(); + Assert.IsNotNull(node); + Assert.AreEqual(configInstanceCount + 1, YogaConfig.GetInstanceCount()); + Assert.AreEqual(nodeInstanceCount + 1, YogaNode.GetInstanceCount()); + node = null; + } + + private YogaNode TestRetainConfigForGC2(int nodeInstanceCount, int configInstanceCount) + { + YogaConfig config = new YogaConfig(); + Assert.IsNotNull(config); + Assert.AreEqual(configInstanceCount + 1, YogaConfig.GetInstanceCount()); + + YogaNode node = new YogaNode(config); + Assert.IsNotNull(node); + Assert.AreEqual(nodeInstanceCount + 1, YogaNode.GetInstanceCount()); + + config = null; + + return node; + } + } +} diff --git a/csharp/tests/Facebook.Yoga/YogaNodeTest.cs b/csharp/tests/Facebook.Yoga/YogaNodeTest.cs index ca62195f..9c604d0d 100644 --- a/csharp/tests/Facebook.Yoga/YogaNodeTest.cs +++ b/csharp/tests/Facebook.Yoga/YogaNodeTest.cs @@ -301,7 +301,7 @@ namespace Facebook.Yoga Assert.AreEqual(90.Pt(), node4.MaxHeight); } - private void ForceGC() + public static void ForceGC() { GC.Collect(GC.MaxGeneration); GC.WaitForPendingFinalizers();