FbxTimeTest.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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;
  9. using Autodesk.Fbx;
  10. namespace Autodesk.Fbx.UnitTests
  11. {
  12. public class FbxTimeTest : TestBase<FbxTime>
  13. {
  14. [Test]
  15. public void TestComparison ()
  16. {
  17. var a = FbxTime.FromSecondDouble(5);
  18. var b = FbxTime.FromSecondDouble(6);
  19. var acopy = FbxTime.FromSecondDouble(5);
  20. // Test equality.
  21. EqualityTester<FbxTime>.TestEquality(a, b, acopy);
  22. // Test inequality.
  23. Assert.IsTrue(a.CompareTo(b) < 0);
  24. Assert.IsTrue(b.CompareTo(a) > 0);
  25. Assert.IsTrue(a.CompareTo(acopy) == 0);
  26. Assert.IsTrue(a.CompareTo((object)null) > 0);
  27. Assert.That(() => a.CompareTo("a string"), Throws.Exception.TypeOf<System.ArgumentException>());
  28. Assert.IsTrue(a < b);
  29. Assert.IsTrue(a <= b);
  30. Assert.IsFalse(a >= b);
  31. Assert.IsFalse(a > b);
  32. Assert.IsTrue((FbxTime)null < b);
  33. Assert.IsFalse(a < (FbxTime)null);
  34. Assert.IsFalse((FbxTime)null < (FbxTime)null);
  35. }
  36. [Test]
  37. public void TestBasics ()
  38. {
  39. // try the static functions
  40. var mode = FbxTime.GetGlobalTimeMode();
  41. FbxTime.SetGlobalTimeMode(FbxTime.EMode.ePAL);
  42. Assert.AreEqual(FbxTime.EMode.ePAL, FbxTime.GetGlobalTimeMode());
  43. FbxTime.SetGlobalTimeMode(mode);
  44. var protocol = FbxTime.GetGlobalTimeProtocol();
  45. FbxTime.SetGlobalTimeProtocol(FbxTime.EProtocol.eSMPTE);
  46. Assert.AreEqual(FbxTime.EProtocol.eSMPTE, FbxTime.GetGlobalTimeProtocol());
  47. FbxTime.SetGlobalTimeProtocol(protocol);
  48. Assert.AreEqual(24, FbxTime.GetFrameRate(FbxTime.EMode.eFrames24));
  49. Assert.AreEqual(FbxTime.EMode.eFrames24, FbxTime.ConvertFrameRateToTimeMode(24));
  50. Assert.AreEqual(FbxTime.EMode.eFrames24, FbxTime.ConvertFrameRateToTimeMode(24.01, 0.1));
  51. Assert.AreEqual(FbxTime.EMode.eDefaultMode, FbxTime.ConvertFrameRateToTimeMode(24.1, 0.01));
  52. TestGetter(FbxTime.GetOneFrameValue());
  53. TestGetter(FbxTime.GetOneFrameValue(FbxTime.EMode.ePAL));
  54. Assert.IsFalse(FbxTime.IsDropFrame());
  55. Assert.IsTrue(FbxTime.IsDropFrame(FbxTime.EMode.eNTSCDropFrame));
  56. // just make sure it doesn't crash
  57. new FbxTime();
  58. // test dispose
  59. DisposeTester.TestDispose(new FbxTime());
  60. using (new FbxTime ()) {}
  61. // try the extension constructors
  62. Assert.AreEqual(5, FbxTime.FromRaw(5).GetRaw());
  63. Assert.AreEqual(5, FbxTime.FromMilliSeconds(5000).GetSecondDouble());
  64. Assert.AreEqual(5, FbxTime.FromSecondDouble(5).GetSecondDouble());
  65. Assert.AreEqual(126210.02, FbxTime.FromTime(pSecond:7, pHour:1, pMinute:10, pResidual:2).GetFrameCountPrecise());
  66. Assert.AreEqual(5, FbxTime.FromFrame(5).GetFrameCountPrecise());
  67. Assert.AreEqual(5.125, FbxTime.FromFramePrecise(5.125).GetFrameCountPrecise());
  68. Assert.AreEqual(5, FbxTime.FromRaw(5).GetRaw());
  69. Assert.AreEqual(126211.2, FbxTime.FromFrame(105176, FbxTime.EMode.ePAL).GetFrameCountPrecise());
  70. Assert.AreEqual(126211.8, FbxTime.FromFramePrecise(105176.5, FbxTime.EMode.ePAL).GetFrameCountPrecise());
  71. Assert.AreEqual(126211.8, FbxTime.FromTimeString("105176.5", FbxTime.EMode.ePAL).GetFrameCountPrecise());
  72. // try breaking a time down
  73. var t = FbxTime.FromTime(pSecond:7, pHour:1, pMinute:10, pField:4, pResidual:2);
  74. Assert.AreEqual(1, t.GetHourCount());
  75. Assert.AreEqual(70, t.GetMinuteCount());
  76. Assert.AreEqual(4207, t.GetSecondCount());
  77. Assert.AreEqual(4207067, t.GetMilliSeconds());
  78. Assert.AreEqual(126212, t.GetFrameCount());
  79. Assert.AreEqual(105176, t.GetFrameCount(FbxTime.EMode.ePAL));
  80. Assert.AreEqual(252424.04, t.GetFrameCountPrecise(FbxTime.EMode.eFrames60));
  81. Assert.AreEqual(252424, t.GetFieldCount());
  82. Assert.AreEqual(210353, t.GetFieldCount(FbxTime.EMode.ePAL));
  83. Assert.AreEqual(2, t.GetResidual());
  84. Assert.AreEqual(68, t.GetResidual(FbxTime.EMode.ePAL));
  85. Assert.AreEqual(':', t.GetFrameSeparator());
  86. Assert.AreEqual(':', t.GetFrameSeparator(FbxTime.EMode.ePAL));
  87. int h, m, s, frame, field, residual;
  88. t.GetTime(out h, out m, out s, out frame, out field, out residual);
  89. Assert.AreEqual(1, h);
  90. Assert.AreEqual(10, m);
  91. Assert.AreEqual(2, frame);
  92. Assert.AreEqual(0, field);
  93. Assert.AreEqual(2, residual);
  94. t.GetTime(out h, out m, out s, out frame, out field, out residual, FbxTime.EMode.ePAL);
  95. Assert.AreEqual(1, h);
  96. Assert.AreEqual(10, m);
  97. Assert.AreEqual(1, frame);
  98. Assert.AreEqual(1, field);
  99. Assert.AreEqual(68, residual);
  100. Assert.AreEqual("126212*", t.GetTimeString());
  101. Assert.AreEqual("126212*", t.GetTimeString(FbxTime.EElement.eSeconds));
  102. Assert.AreEqual("001:10:07", t.GetTimeString(pEnd: FbxTime.EElement.eSeconds, pTimeFormat: FbxTime.EProtocol.eSMPTE));
  103. Assert.AreEqual("126212", t.GetFramedTime().GetTimeString());
  104. Assert.AreEqual("126212", t.GetFramedTime(false).GetTimeString());
  105. }
  106. }
  107. public class FbxTimeSpanTest : TestBase<FbxTimeSpan>
  108. {
  109. [Test]
  110. public void TestBasics ()
  111. {
  112. // just make sure it doesn't crash
  113. new FbxTimeSpan();
  114. new FbxTimeSpan (FbxTime.FromFrame(1), FbxTime.FromFrame(2));
  115. // test dispose
  116. DisposeTester.TestDispose(new FbxTimeSpan());
  117. using (new FbxTimeSpan (FbxTime.FromFrame(1), FbxTime.FromFrame(2))) { }
  118. Assert.That (() => { new FbxTimeSpan(null, null); }, Throws.Exception.TypeOf<System.ArgumentNullException>());
  119. // test Set/Get
  120. FbxTimeSpan timeSpan = new FbxTimeSpan();
  121. timeSpan.Set (FbxTime.FromFrame(2), FbxTime.FromFrame(3));
  122. Assert.AreEqual(FbxTime.FromFrame(2), timeSpan.GetStart());
  123. Assert.AreEqual(FbxTime.FromFrame(3), timeSpan.GetStop());
  124. Assert.That (() => { timeSpan.Set(null, null); }, Throws.Exception.TypeOf<System.ArgumentNullException>());
  125. timeSpan.SetStart(FbxTime.FromFrame(1));
  126. Assert.AreEqual(FbxTime.FromFrame(1), timeSpan.GetStart());
  127. timeSpan.SetStop(FbxTime.FromFrame(4));
  128. Assert.AreEqual(FbxTime.FromFrame(4), timeSpan.GetStop());
  129. // test other functions
  130. Assert.AreEqual(FbxTime.FromFrame(3), timeSpan.GetDuration());
  131. Assert.AreEqual(FbxTime.FromFrame(3), timeSpan.GetSignedDuration());
  132. Assert.AreEqual(1, timeSpan.GetDirection());
  133. Assert.IsTrue(timeSpan.IsInside(FbxTime.FromFrame(2)));
  134. var timeSpan2 = new FbxTimeSpan(FbxTime.FromFrame(2), FbxTime.FromFrame(10));
  135. Assert.AreEqual(new FbxTimeSpan(FbxTime.FromFrame(2), FbxTime.FromFrame(4)), timeSpan.Intersect(timeSpan2));
  136. timeSpan.UnionAssignment(timeSpan2);
  137. Assert.AreEqual(new FbxTimeSpan(FbxTime.FromFrame(1), FbxTime.FromFrame(10)), timeSpan);
  138. new FbxTimeSpan(FbxTime.FromFrame(0), FbxTime.FromFrame(1)).UnionAssignment(timeSpan2, 1);
  139. }
  140. }
  141. }