FbxLayerElementTestBase.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // ***********************************************************************
  2. // Copyright (c) 2017 Unity Technologies. All rights reserved.
  3. //
  4. // Licensed under the ##LICENSENAME##.
  5. // See LICENSE.md file in the project root for full license information.
  6. // ***********************************************************************
  7. // This file contains Tests for FbxLayerElement and all its derived classes.
  8. using NUnit.Framework;
  9. using System.Collections;
  10. using Autodesk.Fbx;
  11. /*
  12. * Convenience class for testing all derived classes of FbxLayerElement.
  13. *
  14. * FbxLayerElement itself has no public constructor or Create function, so we can
  15. * only test its functions from its derived classes (which do have Create functions).
  16. */
  17. namespace Autodesk.Fbx.UnitTests
  18. {
  19. public abstract class FbxLayerElementTestBase<T> where T: Autodesk.Fbx.FbxLayerElement
  20. {
  21. // Note: Create function is only present in derived classes (those which are actually used).
  22. // Included it here so that we can test FbxLayerElement functions, and because they
  23. // are all of the same format (avoid copy pasting into each derived class test class).
  24. // T.Create(FbxLayerContainer, string)
  25. static System.Reflection.MethodInfo s_createFromLayerContainerAndName;
  26. static System.Reflection.MethodInfo s_getDirectArray;
  27. static System.Reflection.MethodInfo s_getIndexArray;
  28. static FbxLayerElementTestBase() {
  29. s_createFromLayerContainerAndName = typeof(T).GetMethod("Create", new System.Type[] {typeof(FbxLayerContainer), typeof(string)});
  30. s_getDirectArray = typeof(T).GetMethod ("GetDirectArray");
  31. s_getIndexArray = typeof(T).GetMethod ("GetIndexArray");
  32. #if ENABLE_COVERAGE_TEST
  33. // Register the calls we make through reflection.
  34. // We use reflection in CreateObject(FbxLayerContainer, string)
  35. if (s_createFromLayerContainerAndName != null) {
  36. var createFromLayerContainerAndName = typeof(FbxLayerElementTestBase<T>).GetMethod("CreateObject", new System.Type[] {typeof(FbxLayerContainer), typeof(string)});
  37. CoverageTester.RegisterReflectionCall(createFromLayerContainerAndName, s_createFromLayerContainerAndName);
  38. }
  39. if(s_getDirectArray != null){
  40. var getDirectArray = typeof(FbxLayerElementTestBase<T>).GetMethod("GetDirectArray");
  41. CoverageTester.RegisterReflectionCall(getDirectArray, s_getDirectArray);
  42. }
  43. if(s_getIndexArray != null){
  44. var getIndexArray = typeof(FbxLayerElementTestBase<T>).GetMethod("GetIndexArray");
  45. CoverageTester.RegisterReflectionCall(getIndexArray, s_getIndexArray);
  46. }
  47. #endif
  48. }
  49. protected FbxManager m_fbxManager;
  50. protected FbxLayerContainer LayerContainer {
  51. get;
  52. private set;
  53. }
  54. /* Create an object with the default manager. */
  55. public T CreateObject (string name = "") {
  56. return CreateObject(LayerContainer, name);
  57. }
  58. #if ENABLE_COVERAGE_TEST
  59. [Test]
  60. public void TestCoverage() { CoverageTester.TestCoverage(typeof(T), this.GetType()); }
  61. #endif
  62. /* Test all the equality functions we can find. */
  63. [Test]
  64. public virtual void TestEquality() {
  65. var a = CreateObject("a");
  66. var b = CreateObject("b");
  67. var acopy = a; // TODO: copy the proxy
  68. EqualityTester<T>.TestEquality(a, b, acopy);
  69. }
  70. /* Create an object with another layer container. Default implementation uses
  71. * reflection to call T.Create(...); override if reflection is wrong. */
  72. public virtual T CreateObject (FbxLayerContainer layerContainer, string name = "") {
  73. return Invoker.InvokeStatic<T>(s_createFromLayerContainerAndName, layerContainer, name);
  74. }
  75. public virtual FbxLayerElementArray GetDirectArray(T layerElement)
  76. {
  77. return Invoker.Invoke<FbxLayerElementArray> (s_getDirectArray, layerElement);
  78. }
  79. public virtual FbxLayerElementArrayTemplateInt GetIndexArray(T layerElement)
  80. {
  81. return Invoker.Invoke<FbxLayerElementArrayTemplateInt> (s_getIndexArray, layerElement);
  82. }
  83. [SetUp]
  84. public virtual void Init ()
  85. {
  86. m_fbxManager = FbxManager.Create ();
  87. LayerContainer = FbxLayerContainer.Create (m_fbxManager, "layer container");
  88. }
  89. [TearDown]
  90. public virtual void Term ()
  91. {
  92. try {
  93. m_fbxManager.Destroy ();
  94. }
  95. catch (System.ArgumentNullException) {
  96. }
  97. }
  98. [Test]
  99. public void TestCreate()
  100. {
  101. var obj = CreateObject("MyObject");
  102. Assert.IsInstanceOf<T> (obj);
  103. // test null container
  104. Assert.That (() => { CreateObject((FbxLayerContainer)null, "MyObject"); }, Throws.Exception.TypeOf<System.ArgumentNullException>());
  105. // test null name
  106. CreateObject((string)null);
  107. // test zombie
  108. var layerContainer = FbxLayerContainer.Create(m_fbxManager, "");
  109. layerContainer.Destroy();
  110. Assert.That (() => { CreateObject(layerContainer, "MyObject"); }, Throws.Exception.TypeOf<System.ArgumentNullException>());
  111. }
  112. [Test]
  113. public void TestDispose ()
  114. {
  115. var layerElement = CreateObject ("element");
  116. layerElement.Dispose ();
  117. Assert.That (() => { layerElement.SetMappingMode (FbxLayerElement.EMappingMode.eAllSame); }, Throws.Exception.TypeOf<System.ArgumentNullException>());
  118. T element;
  119. using (element = CreateObject ("element2")) {
  120. element.SetMappingMode (FbxLayerElement.EMappingMode.eAllSame); // should be fine
  121. }
  122. Assert.That (() => { element.SetMappingMode (FbxLayerElement.EMappingMode.eAllSame); }, Throws.Exception.TypeOf<System.ArgumentNullException>());
  123. }
  124. [Test]
  125. public void TestSetMappingMode ()
  126. {
  127. var layerElement = CreateObject ("element");
  128. layerElement.SetMappingMode (FbxLayerElement.EMappingMode.eByControlPoint);
  129. Assert.AreEqual (layerElement.GetMappingMode (), FbxLayerElement.EMappingMode.eByControlPoint);
  130. }
  131. [Test]
  132. public void TestSetReferenceMode ()
  133. {
  134. var layerElement = CreateObject ("element");
  135. layerElement.SetReferenceMode (FbxLayerElement.EReferenceMode.eDirect);
  136. Assert.AreEqual (layerElement.GetReferenceMode (), FbxLayerElement.EReferenceMode.eDirect);
  137. }
  138. [Test]
  139. public void TestGetDirectArray() {
  140. var layerElement = CreateObject ("element");
  141. // make sure this doesn't crash
  142. GetDirectArray (layerElement);
  143. }
  144. [Test]
  145. public void TestGetIndexArray() {
  146. var layerElement = CreateObject ("element");
  147. // make sure this doesn't crash
  148. GetIndexArray (layerElement);
  149. }
  150. }
  151. /*
  152. * Tests for the classes derived from the FbxLayerElementTemplate classes.
  153. */
  154. public class FbxLayerElementUVTest : FbxLayerElementTestBase<FbxLayerElementUV>
  155. {}
  156. public class FbxLayerElementVertexColorTest : FbxLayerElementTestBase<FbxLayerElementVertexColor>
  157. {}
  158. public class FbxLayerElementNormalTest : FbxLayerElementTestBase<FbxLayerElementNormal>
  159. {}
  160. public class FbxLayerElementBinormalTest : FbxLayerElementTestBase<FbxLayerElementBinormal>
  161. {}
  162. public class FbxLayerElementTangentTest : FbxLayerElementTestBase<FbxLayerElementTangent>
  163. {}
  164. public class FbxLayerElementMaterialTest : FbxLayerElementTestBase<FbxLayerElementMaterial>
  165. {}
  166. }