FbxAnimCurveTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. using NUnit.Framework;
  8. using System.Collections.Generic;
  9. using Autodesk.Fbx;
  10. namespace Autodesk.Fbx.UnitTests
  11. {
  12. public class FbxAnimCurveTest : Base<FbxAnimCurve>
  13. {
  14. Dictionary<FbxManager, FbxScene> m_scenes = new Dictionary<FbxManager, FbxScene>();
  15. public override FbxAnimCurve CreateObject(FbxManager mgr, string name = "") {
  16. if (mgr == null) { throw new System.ArgumentNullException(); }
  17. /* Creating in a manager doesn't work for AnimCurves, but for the benefit of
  18. testing, just fudge it by creating a scene for the manager. */
  19. FbxScene scene;
  20. if (!m_scenes.TryGetValue(mgr, out scene)) {
  21. scene = FbxScene.Create(mgr, "__testscene");
  22. m_scenes.Add(mgr, scene);
  23. }
  24. return FbxAnimCurve.Create(scene, name);
  25. }
  26. public override FbxAnimCurve CreateObject(FbxObject container, string name = "") {
  27. if (container == null) { throw new System.ArgumentNullException(); }
  28. if (container is FbxScene) {
  29. /* Probably should have cast to a scene already... but ok. */
  30. return FbxAnimCurve.Create((FbxScene)container, name);
  31. } else {
  32. /* This create call doesn't do what you want. Use the manager's scene instead. */
  33. return CreateObject(container.GetFbxManager(), name);
  34. }
  35. }
  36. protected override void TestSceneContainer()
  37. {
  38. // The base test tries to make FbxAnimCurve with an FbxAnimCurve as
  39. // parent; that doesn't work for some reason. So simplify it.
  40. using(var scene = FbxScene.Create(Manager, "thescene")) {
  41. var obj = CreateObject(scene, "scene_object");
  42. Assert.AreEqual(scene, obj.GetScene());
  43. }
  44. {
  45. // The base test assumes that if there's no scene, the object
  46. // won't be in a scene. But FbxAnimCurve synthesizes a test
  47. // scene.
  48. var obj = CreateObject(Manager, "not_scene_object");
  49. Assert.AreNotEqual(null, obj.GetScene());
  50. }
  51. }
  52. [Test]
  53. public void TestBasics ()
  54. {
  55. var scene = FbxScene.Create(Manager, "scene");
  56. using (FbxAnimCurve curve = FbxAnimCurve.Create(scene, "curve")) {
  57. // test KeyModifyBegin (make sure it doesn't crash)
  58. curve.KeyModifyBegin ();
  59. // test KeyAdd
  60. int last = 0;
  61. int index = curve.KeyAdd (FbxTime.FromFrame (5), ref last);
  62. Assert.GreaterOrEqual (index, 0);
  63. // test KeyAdd null FbxTime
  64. Assert.That (() => { curve.KeyAdd(null); }, Throws.Exception.TypeOf<System.ArgumentNullException>());
  65. Assert.That (() => { curve.KeyAdd(null, ref last); }, Throws.Exception.TypeOf<System.ArgumentNullException>());
  66. // test KeySet
  67. FbxTime keyTime = FbxTime.FromSecondDouble(3);
  68. curve.KeySet(index, keyTime, 5);
  69. // test KeyGetValue, KeyGetTime, KeyGetCount
  70. Assert.AreEqual (5, curve.KeyGetValue (index));
  71. Assert.AreEqual (keyTime, curve.KeyGetTime (index));
  72. Assert.AreEqual (1, curve.KeyGetCount ());
  73. // test don't crash
  74. FbxAnimCurveKey key = curve.KeyGet(index);
  75. Assert.That(key, Is.Not.Null);
  76. // make sure none of the variations crash
  77. curve.KeySet (index, new FbxTime (), 5, FbxAnimCurveDef.EInterpolationType.eInterpolationConstant
  78. );
  79. curve.KeySet (index, new FbxTime (), 0,
  80. FbxAnimCurveDef.EInterpolationType.eInterpolationCubic,
  81. FbxAnimCurveDef.ETangentMode.eTangentAuto
  82. );
  83. curve.KeySet (index, new FbxTime (), 0,
  84. FbxAnimCurveDef.EInterpolationType.eInterpolationCubic,
  85. FbxAnimCurveDef.ETangentMode.eTangentAuto, 4
  86. );
  87. curve.KeySet (index, new FbxTime (), 0,
  88. FbxAnimCurveDef.EInterpolationType.eInterpolationCubic,
  89. FbxAnimCurveDef.ETangentMode.eTangentAuto,
  90. 0, 3);
  91. curve.KeySet (index, new FbxTime (), 0,
  92. FbxAnimCurveDef.EInterpolationType.eInterpolationCubic,
  93. FbxAnimCurveDef.ETangentMode.eTangentAuto,
  94. 0, 0, FbxAnimCurveDef.EWeightedMode.eWeightedAll);
  95. curve.KeySet (index, new FbxTime (), 0,
  96. FbxAnimCurveDef.EInterpolationType.eInterpolationCubic,
  97. FbxAnimCurveDef.ETangentMode.eTangentAuto,
  98. 0, 0, FbxAnimCurveDef.EWeightedMode.eWeightedAll, 0);
  99. curve.KeySet (index, new FbxTime (), 0,
  100. FbxAnimCurveDef.EInterpolationType.eInterpolationCubic,
  101. FbxAnimCurveDef.ETangentMode.eTangentAuto,
  102. 0, 0, FbxAnimCurveDef.EWeightedMode.eWeightedAll, 0, 0);
  103. curve.KeySet (index, new FbxTime (), 0,
  104. FbxAnimCurveDef.EInterpolationType.eInterpolationCubic,
  105. FbxAnimCurveDef.ETangentMode.eTangentAuto,
  106. 0, 0, FbxAnimCurveDef.EWeightedMode.eWeightedAll, 0, 0, 0);
  107. curve.KeySet (index, new FbxTime (), 0,
  108. FbxAnimCurveDef.EInterpolationType.eInterpolationCubic,
  109. FbxAnimCurveDef.ETangentMode.eTangentAuto,
  110. 0, 0, FbxAnimCurveDef.EWeightedMode.eWeightedAll, 0, 0, 0, 0);
  111. // more setter test
  112. curve.KeySetTangentMode (index, FbxAnimCurveDef.ETangentMode.eTangentUser);
  113. Assert.That(curve.KeyGetTangentMode (index), Is.EqualTo (FbxAnimCurveDef.ETangentMode.eTangentUser));
  114. curve.KeySetTangentMode (index, FbxAnimCurveDef.ETangentMode.eTangentGenericBreak);
  115. Assert.That(curve.KeyGetTangentMode (index), Is.EqualTo (FbxAnimCurveDef.ETangentMode.eTangentGenericBreak));
  116. // test settings key parameters
  117. key.SetTangentMode (FbxAnimCurveDef.ETangentMode.eTangentUser);
  118. // Set break is only meaningful if tangent is eTangentAuto or eTangentUser
  119. key.SetBreak (true);
  120. Assert.True (key.GetBreak ());
  121. key.SetDataFloat (FbxAnimCurveDef.EDataIndex.eRightSlope, 1.0f);
  122. Assert.That (key.GetDataFloat (FbxAnimCurveDef.EDataIndex.eRightSlope), Is.EqualTo (1.0f).Within (float.Epsilon));
  123. key.SetBreak (false);
  124. Assert.False (key.GetBreak ());
  125. //
  126. key.SetTangentWeightMode (FbxAnimCurveDef.EWeightedMode.eWeightedAll);
  127. key.SetTangentWeightMode (FbxAnimCurveDef.EWeightedMode.eWeightedAll, FbxAnimCurveDef.EWeightedMode.eWeightedAll);
  128. Assert.That(key.GetTangentWeightMode (), Is.EqualTo(FbxAnimCurveDef.EWeightedMode.eWeightedAll));
  129. //
  130. key.SetBreak (true);
  131. key.SetTangentWeightAndAdjustTangent (FbxAnimCurveDef.EDataIndex.eRightSlope, 1.0);
  132. key.SetTangentWeightAndAdjustTangent (FbxAnimCurveDef.EDataIndex.eNextLeftSlope, 1.0);
  133. key.SetTangentWeightAndAdjustTangent (FbxAnimCurveDef.EDataIndex.eWeights, 1.0);
  134. key.SetTangentWeightAndAdjustTangent (FbxAnimCurveDef.EDataIndex.eRightWeight, 1.0);
  135. key.SetTangentWeightAndAdjustTangent (FbxAnimCurveDef.EDataIndex.eNextLeftWeight, 1.0);
  136. key.SetBreak (false);
  137. //
  138. key.SetTangentVelocityMode (FbxAnimCurveDef.EVelocityMode.eVelocityAll);
  139. key.SetTangentVelocityMode (FbxAnimCurveDef.EVelocityMode.eVelocityAll, FbxAnimCurveDef.EVelocityMode.eVelocityAll);
  140. Assert.That (key.GetTangentVelocityMode (), Is.EqualTo (FbxAnimCurveDef.EVelocityMode.eVelocityAll));
  141. //
  142. key.SetTangentVisibility(FbxAnimCurveDef.ETangentVisibility.eTangentShowLeft);
  143. Assert.That (key.GetTangentVisibility (), Is.EqualTo (FbxAnimCurveDef.ETangentVisibility.eTangentShowLeft));
  144. // test KeyModifyEnd (make sure it doesn't crash)
  145. curve.KeyModifyEnd ();
  146. }
  147. // Also test that the AnimCurveBase can't be created.
  148. Assert.That(() => FbxAnimCurveBase.Create(Manager, ""), Throws.Exception.TypeOf<System.NotImplementedException>());
  149. Assert.That(() => FbxAnimCurveBase.Create(FbxObject.Create(Manager, ""), ""), Throws.Exception.TypeOf<System.NotImplementedException>());
  150. }
  151. [Test]
  152. public override void TestDisposeDestroy()
  153. {
  154. // Because we can't build a recursive structure of anim curves,
  155. // this test is much simpler than the usual FbxObject test.
  156. var curve = CreateObject("a");
  157. DisposeTester.TestDispose(curve);
  158. using (CreateObject("b"));
  159. curve = CreateObject("c");
  160. curve.Destroy();
  161. Assert.That(() => curve.GetName(), Throws.Exception.TypeOf<System.ArgumentNullException>());
  162. // we can't destroy recursively, but we still get the flag
  163. curve = CreateObject("d");
  164. curve.Destroy(false);
  165. }
  166. [Test]
  167. public void TestKeyAddBeforeKeyModifyBegin()
  168. {
  169. using (FbxAnimCurve curve = CreateObject ("curve")) {
  170. curve.KeyAdd (new FbxTime ());
  171. curve.KeyModifyBegin ();
  172. }
  173. }
  174. [Test]
  175. public void TestKeyModifyEndBeforeKeyModifyBegin()
  176. {
  177. using (FbxAnimCurve curve = CreateObject ("curve")) {
  178. curve.KeyModifyEnd ();
  179. curve.KeyModifyBegin ();
  180. }
  181. }
  182. }
  183. public class FbxAnimCurveDefTest /* testing a static class, so we can't derive from TestBase */
  184. {
  185. #if ENABLE_COVERAGE_TEST
  186. [Test]
  187. public virtual void TestCoverage() { CoverageTester.TestCoverage(typeof(FbxAnimCurveDef), this.GetType()); }
  188. #endif
  189. [Test]
  190. public void TestBasics()
  191. {
  192. Assert.AreEqual(0.0, FbxAnimCurveDef.sDEFAULT_VELOCITY);
  193. Assert.AreNotEqual(0.0, FbxAnimCurveDef.sDEFAULT_WEIGHT);
  194. Assert.That(FbxAnimCurveDef.sMIN_WEIGHT, Is.GreaterThan(0.0f));
  195. Assert.That(FbxAnimCurveDef.sMAX_WEIGHT, Is.LessThan(1.0f));
  196. }
  197. }
  198. }