AnimTrackTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzTest/AzTest.h>
  9. #include <AzCore/UnitTest/UnitTest.h>
  10. #include <AnimKey.h>
  11. #include <Maestro/Types/AssetBlendKey.h>
  12. #include <Cinematics/AnimTrack.h>
  13. namespace AnimTrackTest
  14. {
  15. struct ITestKey
  16. : public IKey
  17. {
  18. public:
  19. AZ_RTTI(ITestKey, "{0C84DBF1-C88E-4C76-8282-601EE30D1AFF}", IKey);
  20. ITestKey() {}
  21. ITestKey(bool isSelected, bool isSortMarker)
  22. {
  23. if (isSelected)
  24. {
  25. flags |= AKEY_SELECTED;
  26. }
  27. if (isSortMarker)
  28. {
  29. flags |= AKEY_SORT_MARKER;
  30. }
  31. }
  32. };
  33. class CTestTrack
  34. : public TAnimTrack<ITestKey>
  35. {
  36. public:
  37. AZ_RTTI(CTestTrack, "{78041EDD-872C-4AB9-81DD-605413810C7E}", IAnimTrack);
  38. CTestTrack() {}
  39. void GetKeyInfo([[maybe_unused]] int index, [[maybe_unused]] const char*& description, [[maybe_unused]] float& duration) {}
  40. void SerializeKey([[maybe_unused]] ITestKey& key, [[maybe_unused]] XmlNodeRef& keyNode, [[maybe_unused]] bool bLoading) {};
  41. };
  42. class TAnimTrackTest
  43. : public ::testing::Test
  44. {
  45. public:
  46. TAnimTrackTest() {}
  47. void SetUp() override
  48. {
  49. // Init Test Track A
  50. int keyIndex = 0;
  51. m_testKeyA_0.time = 1.0f;
  52. m_testTrackA.CreateKey(m_testKeyA_0.time);
  53. m_testTrackA.SetKey(keyIndex++, &m_testKeyA_0);
  54. m_testKeyA_1 = ITestKey(/*is selected*/ true, /*is sort marker*/ false);
  55. m_testKeyA_1.time = 2.0f;
  56. m_testTrackA.CreateKey(m_testKeyA_1.time);
  57. m_testTrackA.SetKey(keyIndex++, &m_testKeyA_1);
  58. m_testKeyA_2 = ITestKey(/*is selected*/ false, /*is sort marker*/ true);
  59. m_testKeyA_2.time = 5.0f;
  60. m_testTrackA.CreateKey(m_testKeyA_2.time);
  61. m_testTrackA.SetKey(keyIndex++, &m_testKeyA_2);
  62. }
  63. CTestTrack m_emptyTrack;
  64. CTestTrack m_testTrackA;
  65. ITestKey m_testKeyA_0;
  66. ITestKey m_testKeyA_1;
  67. ITestKey m_testKeyA_2;
  68. };
  69. //------------------------------------------------------------------------------
  70. // Unit Tests
  71. //------------------------------------------------------------------------------
  72. TEST_F(TAnimTrackTest, IsKeySelected_InvalidKey_ExpectAssert)
  73. {
  74. AZ_TEST_START_TRACE_SUPPRESSION;
  75. m_testTrackA.IsKeySelected(-1);
  76. // We are expecting 2 asserts as the function does not early-out, so the bad input asserts in both
  77. // the TrackView code and the AZStd code that gets called. Early-outs should probably be added in the future.
  78. AZ_TEST_STOP_TRACE_SUPPRESSION(2);
  79. }
  80. TEST_F(TAnimTrackTest, IsKeySelected_UnselectedKey_ExpectFalse)
  81. {
  82. bool result = true;
  83. result = m_testTrackA.IsKeySelected(2);
  84. EXPECT_FALSE(result);
  85. }
  86. TEST_F(TAnimTrackTest, IsKeySelected_SelectedKey_ExpectTrue)
  87. {
  88. bool result = true;
  89. result = m_testTrackA.IsKeySelected(1);
  90. EXPECT_TRUE(result);
  91. }
  92. TEST_F(TAnimTrackTest, SelectKey_InvalidKey)
  93. {
  94. AZ_TEST_START_TRACE_SUPPRESSION;
  95. m_testTrackA.SelectKey(-1, true);
  96. // We are expecting 2 asserts as the function does not early-out, so the bad input asserts in both
  97. // the TrackView code and the AZStd code that gets called. Early-outs should probably be added in the future.
  98. AZ_TEST_STOP_TRACE_SUPPRESSION(2);
  99. }
  100. TEST_F(TAnimTrackTest, SelectKey_UnselectedKey_Select)
  101. {
  102. bool result = false;
  103. m_testTrackA.SelectKey(0, true);
  104. result = m_testTrackA.IsKeySelected(0);
  105. EXPECT_TRUE(result);
  106. }
  107. TEST_F(TAnimTrackTest, SelectKey_SelectedKey_Select)
  108. {
  109. bool result = false;
  110. m_testTrackA.SelectKey(1, true);
  111. result = m_testTrackA.IsKeySelected(1);
  112. EXPECT_TRUE(result);
  113. }
  114. TEST_F(TAnimTrackTest, SelectKey_UnselectedKey_UnSelect)
  115. {
  116. bool result = true;
  117. m_testTrackA.SelectKey(0, false);
  118. result = m_testTrackA.IsKeySelected(0);
  119. EXPECT_FALSE(result);
  120. }
  121. TEST_F(TAnimTrackTest, SelectKey_SelectedKey_UnSelect)
  122. {
  123. bool result = true;
  124. m_testTrackA.SelectKey(1, false);
  125. result = m_testTrackA.IsKeySelected(1);
  126. EXPECT_FALSE(result);
  127. }
  128. TEST_F(TAnimTrackTest, IsSortMarkerKey_InvalidKey)
  129. {
  130. AZ_TEST_START_TRACE_SUPPRESSION;
  131. m_testTrackA.IsSortMarkerKey(5);
  132. // We are expecting 2 asserts as the function does not early-out, so the bad input asserts in both
  133. // the TrackView code and the AZStd code that gets called. Early-outs should probably be added in the future.
  134. AZ_TEST_STOP_TRACE_SUPPRESSION(2);
  135. }
  136. TEST_F(TAnimTrackTest, IsSortMarkerKey_UnmarkedKey_ExpectFalse)
  137. {
  138. bool result = true;
  139. result = m_testTrackA.IsSortMarkerKey(0);
  140. EXPECT_FALSE(result);
  141. }
  142. TEST_F(TAnimTrackTest, IsSortMarkerKey_MarkedKey_ExpectTrue)
  143. {
  144. bool result = false;
  145. result = m_testTrackA.IsSortMarkerKey(2);
  146. EXPECT_TRUE(result);
  147. }
  148. TEST_F(TAnimTrackTest, SetSortMarkerKey_InvalidKey)
  149. {
  150. AZ_TEST_START_TRACE_SUPPRESSION;
  151. m_testTrackA.SetSortMarkerKey(5, true);
  152. // We are expecting 2 asserts as the function does not early-out, so the bad input asserts in both
  153. // the TrackView code and the AZStd code that gets called. Early-outs should probably be added in the future.
  154. AZ_TEST_STOP_TRACE_SUPPRESSION(2);
  155. }
  156. TEST_F(TAnimTrackTest, SetSortMarkerKey_UnsetKey_Set)
  157. {
  158. m_testTrackA.SetSortMarkerKey(0, true);
  159. EXPECT_TRUE(m_testTrackA.IsSortMarkerKey(0));
  160. }
  161. TEST_F(TAnimTrackTest, SetSortMarkerKey_UnsetKey_Unset)
  162. {
  163. m_testTrackA.SetSortMarkerKey(0, false);
  164. EXPECT_FALSE(m_testTrackA.IsSortMarkerKey(0));
  165. }
  166. TEST_F(TAnimTrackTest, SetSortMarkerKey_SetKey_Set)
  167. {
  168. m_testTrackA.SetSortMarkerKey(2, true);
  169. EXPECT_TRUE(m_testTrackA.IsSortMarkerKey(2));
  170. }
  171. TEST_F(TAnimTrackTest, SetSortMarkerKey_SetKey_Unset)
  172. {
  173. m_testTrackA.SetSortMarkerKey(2, false);
  174. EXPECT_FALSE(m_testTrackA.IsSortMarkerKey(2));
  175. }
  176. TEST_F(TAnimTrackTest, RemoveKey_RemoveFirstKey)
  177. {
  178. m_testTrackA.RemoveKey(0);
  179. IKey result;
  180. m_testTrackA.GetKey(0, &result);
  181. EXPECT_EQ(m_testTrackA.GetNumKeys(), 2);
  182. EXPECT_EQ(result.time, 2.0f);
  183. }
  184. TEST_F(TAnimTrackTest, RemoveKey_RemoveMiddleKey)
  185. {
  186. m_testTrackA.RemoveKey(1);
  187. EXPECT_EQ(m_testTrackA.GetNumKeys(), 2);
  188. IKey result;
  189. m_testTrackA.GetKey(0, &result);
  190. EXPECT_EQ(result.time, 1.0f);
  191. m_testTrackA.GetKey(1, &result);
  192. EXPECT_EQ(result.time, 5.0f);
  193. }
  194. TEST_F(TAnimTrackTest, RemoveKey_RemoveLastKey)
  195. {
  196. m_testTrackA.RemoveKey(2);
  197. EXPECT_EQ(m_testTrackA.GetNumKeys(), 2);
  198. }
  199. TEST_F(TAnimTrackTest, GetKey_InvalidIndex_ExpectAssert)
  200. {
  201. IKey result;
  202. AZ_TEST_START_TRACE_SUPPRESSION;
  203. m_testTrackA.GetKey(-1, &result);
  204. // We are expecting 2 asserts as the function does not early-out, so the bad input asserts in both
  205. // the TrackView code and the AZStd code that gets called. Early-outs should probably be added in the future.
  206. AZ_TEST_STOP_TRACE_SUPPRESSION(2);
  207. }
  208. TEST_F(TAnimTrackTest, GetKey_ValidInputs_ExpectSuccess)
  209. {
  210. IKey result;
  211. m_testTrackA.GetKey(0, &result);
  212. EXPECT_EQ(result.time, m_testKeyA_0.time);
  213. }
  214. TEST_F(TAnimTrackTest, SetKey_InvalidIndex_ExpectAssert)
  215. {
  216. ITestKey testKey;
  217. AZ_TEST_START_TRACE_SUPPRESSION;
  218. m_testTrackA.SetKey(-1, &testKey);
  219. // We are expecting 2 asserts as the function does not early-out, so the bad input asserts in both
  220. // the TrackView code and the AZStd code that gets called. Early-outs should probably be added in the future.
  221. AZ_TEST_STOP_TRACE_SUPPRESSION(2);
  222. }
  223. TEST_F(TAnimTrackTest, SetKey_ValidKey_ExpectSuccess)
  224. {
  225. ITestKey testKey;
  226. testKey.time = 3.0f;
  227. m_testTrackA.SetKey(2, &testKey);
  228. EXPECT_EQ(m_testTrackA.GetKeyTime(2), 3.0f);
  229. EXPECT_EQ(m_testTrackA.GetNumKeys(), 3);
  230. EXPECT_FALSE(m_testTrackA.IsSortMarkerKey(2));
  231. }
  232. TEST_F(TAnimTrackTest, GetKeyTime_InvalidIndex_ExpectAssert)
  233. {
  234. AZ_TEST_START_TRACE_SUPPRESSION;
  235. m_testTrackA.GetKeyTime(-1);
  236. // We are expecting 2 asserts as the function does not early-out, so the bad input asserts in both
  237. // the TrackView code and the AZStd code that gets called. Early-outs should probably be added in the future.
  238. AZ_TEST_STOP_TRACE_SUPPRESSION(2);
  239. }
  240. TEST_F(TAnimTrackTest, GetKeyTime_ValidInputs_ExpectSuccess)
  241. {
  242. EXPECT_EQ(m_testTrackA.GetKeyTime(0), m_testKeyA_0.time);
  243. EXPECT_EQ(m_testTrackA.GetKeyTime(1), m_testKeyA_1.time);
  244. EXPECT_EQ(m_testTrackA.GetKeyTime(2), m_testKeyA_2.time);
  245. }
  246. TEST_F(TAnimTrackTest, SetKeyTime_InvalidIndex_ExpectAssert)
  247. {
  248. AZ_TEST_START_TRACE_SUPPRESSION;
  249. m_testTrackA.SetKeyTime(-1, 5.0f);
  250. // We are expecting 2 asserts as the function does not early-out, so the bad input asserts in both
  251. // the TrackView code and the AZStd code that gets called. Early-outs should probably be added in the future.
  252. AZ_TEST_STOP_TRACE_SUPPRESSION(2);
  253. }
  254. TEST_F(TAnimTrackTest, SetKeyTime)
  255. {
  256. m_testTrackA.SetKeyTime(2, 6.0f);
  257. EXPECT_EQ(m_testTrackA.GetKeyTime(0), m_testKeyA_0.time);
  258. EXPECT_EQ(m_testTrackA.GetKeyTime(1), m_testKeyA_1.time);
  259. EXPECT_EQ(m_testTrackA.GetKeyTime(2), 6.0f);
  260. }
  261. TEST_F(TAnimTrackTest, FindKey_IncorrectInput_NoKeysFound)
  262. {
  263. int result = m_testTrackA.FindKey(4.0f);
  264. EXPECT_EQ(result, -1);
  265. }
  266. TEST_F(TAnimTrackTest, FindKey_ExactInputs_ExpectKeysFound)
  267. {
  268. int result = m_testTrackA.FindKey(1.0f);
  269. EXPECT_EQ(m_testTrackA.GetKeyTime(result), m_testKeyA_0.time);
  270. result = m_testTrackA.FindKey(2.0f);
  271. EXPECT_EQ(m_testTrackA.GetKeyTime(result), m_testKeyA_1.time);
  272. result = m_testTrackA.FindKey(5.0f);
  273. EXPECT_EQ(m_testTrackA.GetKeyTime(result), m_testKeyA_2.time);
  274. }
  275. TEST_F(TAnimTrackTest, CreateKey)
  276. {
  277. m_testTrackA.CreateKey(7.0f);
  278. EXPECT_EQ(m_testTrackA.GetNumKeys(), 4);
  279. EXPECT_EQ(m_testTrackA.GetKeyTime(3), 7.0f);
  280. }
  281. TEST_F(TAnimTrackTest, CloneKey)
  282. {
  283. m_testTrackA.CloneKey(2);
  284. EXPECT_EQ(m_testTrackA.GetNumKeys(), 4);
  285. EXPECT_EQ(m_testTrackA.GetKeyTime(3), m_testKeyA_2.time);
  286. }
  287. TEST_F(TAnimTrackTest, CopyKey)
  288. {
  289. m_emptyTrack.CopyKey(&m_testTrackA, 1);
  290. EXPECT_EQ(m_emptyTrack.GetNumKeys(), 1);
  291. EXPECT_EQ(m_emptyTrack.GetKeyTime(0), m_testKeyA_1.time);
  292. }
  293. TEST_F(TAnimTrackTest, GetActiveKey_NullKey)
  294. {
  295. int i = m_testTrackA.GetActiveKey(0.0f, NULL);
  296. EXPECT_EQ(i, -1);
  297. }
  298. TEST_F(TAnimTrackTest, GetActiveKey_EmptyTrack)
  299. {
  300. ITestKey tempKey;
  301. int i = m_emptyTrack.GetActiveKey(5.0f, &tempKey);
  302. EXPECT_EQ(i, -1);
  303. }
  304. TEST_F(TAnimTrackTest, GetActiveKey_TimeIsBeforeFirstKey_RegularTrack_ExpectInvalid)
  305. {
  306. ITestKey tempKey;
  307. int i = m_testTrackA.GetActiveKey(0.5f, &tempKey);
  308. EXPECT_EQ(i, -1);
  309. }
  310. TEST_F(TAnimTrackTest, GetActiveKey_TimeIsBeforeCurrentKey_ExpectValid)
  311. {
  312. ITestKey tempKey;
  313. int i = m_testTrackA.GetActiveKey(3.0f, &tempKey);
  314. EXPECT_EQ(i, 1);
  315. i = m_testTrackA.GetActiveKey(1.5f, &tempKey);
  316. EXPECT_EQ(i, 0);
  317. }
  318. TEST_F(TAnimTrackTest, GetActiveKey_TimeIsAfterCurrentKey_ExpectValid)
  319. {
  320. ITestKey tempKey;
  321. int i = m_testTrackA.GetActiveKey(1.5f, &tempKey);
  322. EXPECT_EQ(i, 0);
  323. }
  324. TEST_F(TAnimTrackTest, GetActiveKey_TimeIsAfterLastKey_ExpectValid)
  325. {
  326. ITestKey tempKey;
  327. int i = m_testTrackA.GetActiveKey(6.0f, &tempKey);
  328. EXPECT_EQ(i, 2);
  329. }
  330. } //namespace AnimTrackTest