AnimationTest.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "LyShineTest.h"
  9. #include <Mocks/ISystemMock.h>
  10. #include <AzCore/UnitTest/Mocks/MockITime.h>
  11. #include <AzCore/UserSettings/UserSettingsComponent.h>
  12. #include <AzFramework/Application/Application.h>
  13. #include <UiCanvasComponent.h>
  14. #include <Animation/AnimSequence.h>
  15. #include <Animation/EventNode.h>
  16. #include <Animation/TrackEventTrack.h>
  17. namespace UnitTest
  18. {
  19. struct AnimationTestStubTimer : public AZ::StubTimeSystem
  20. {
  21. AZ_RTTI(UnitTest::AnimationTestStubTimer, "{541EBC6C-E793-4433-9402-4CAD2F6770E3}", AZ::StubTimeSystem);
  22. AZ::TimeMs GetElapsedTimeMs() const override
  23. {
  24. return AZ::TimeUsToMs(m_timeUs);
  25. }
  26. AZ::TimeUs GetElapsedTimeUs() const override
  27. {
  28. return m_timeUs;
  29. }
  30. void AddFrameTime(float sec)
  31. {
  32. m_timeUs += AZ::SecondsToTimeUs(sec);
  33. }
  34. AZ::TimeUs m_timeUs = AZ::Time::ZeroTimeUs;
  35. };
  36. class TrackEventHandler
  37. : public UiAnimationNotificationBus::Handler
  38. {
  39. public:
  40. void Connect(AZ::EntityId id)
  41. {
  42. m_busId = id;
  43. UiAnimationNotificationBus::Handler::BusConnect(id);
  44. }
  45. ~TrackEventHandler()
  46. {
  47. UiAnimationNotificationBus::Handler::BusDisconnect(m_busId);
  48. }
  49. void OnUiAnimationEvent([[maybe_unused]] IUiAnimationListener::EUiAnimationEvent uiAnimationEvent, [[maybe_unused]] AZStd::string animSequenceName) {};
  50. void OnUiTrackEvent(AZStd::string eventName, AZStd::string valueName, AZStd::string animSequenceName)
  51. {
  52. m_recievedEvents.push_back(EventInfo{ eventName, valueName, animSequenceName });
  53. }
  54. struct EventInfo
  55. {
  56. AZStd::string m_event;
  57. AZStd::string m_value;
  58. AZStd::string m_sequence;
  59. };
  60. AZ::EntityId m_busId;
  61. AZStd::vector<EventInfo> m_recievedEvents;
  62. };
  63. class LyShineAnimationTestApplication : public AzFramework::Application
  64. {
  65. public:
  66. LyShineAnimationTestApplication()
  67. : AzFramework::Application()
  68. {
  69. m_timeSystem.reset();
  70. m_timeSystem = AZStd::make_unique<UnitTest::AnimationTestStubTimer>();
  71. }
  72. UnitTest::AnimationTestStubTimer* GetTimer()
  73. {
  74. return azdynamic_cast<UnitTest::AnimationTestStubTimer*>(m_timeSystem.get());
  75. }
  76. };
  77. class LyShineAnimationTest
  78. : public LyShineTest
  79. {
  80. protected:
  81. LyShineAnimationTest()
  82. : m_canvasComponent(nullptr)
  83. {
  84. }
  85. void SetupApplication() override
  86. {
  87. AZ::ComponentApplication::Descriptor appDesc;
  88. appDesc.m_memoryBlocksByteSize = 10 * 1024 * 1024;
  89. appDesc.m_recordingMode = AZ::Debug::AllocationRecords::Mode::RECORD_FULL;
  90. m_application = aznew LyShineAnimationTestApplication();
  91. m_systemEntity = m_application->Create(appDesc);
  92. m_systemEntity->Init();
  93. m_systemEntity->Activate();
  94. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  95. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  96. // in the unit tests.
  97. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  98. }
  99. void SetupEnvironment() override
  100. {
  101. LyShineTest::SetupEnvironment();
  102. m_canvasComponent = aznew UiCanvasComponent;
  103. }
  104. void TearDown() override
  105. {
  106. delete m_canvasComponent;
  107. UiAnimationNotificationBus::ClearQueuedEvents();
  108. LyShineTest::TearDown();
  109. }
  110. UnitTest::AnimationTestStubTimer* GetTimer()
  111. {
  112. return static_cast<LyShineAnimationTestApplication*>(m_application)->GetTimer();
  113. }
  114. UiCanvasComponent* m_canvasComponent;
  115. };
  116. TEST_F(LyShineAnimationTest, Animation_TrackEventTriggered_FT)
  117. {
  118. IUiAnimationSystem* animSys = m_canvasComponent->GetAnimationSystem();
  119. IUiAnimSequence* sequence = animSys->CreateSequence("TestSequence", true);
  120. CUiAnimEventNode* eventNode = aznew CUiAnimEventNode;
  121. sequence->AddNode(eventNode);
  122. sequence->AddTrackEvent("TestTrackEvent");
  123. eventNode->CreateDefaultTracks();
  124. IUiAnimTrack* eventTrack = eventNode->GetTrackByIndex(0);
  125. int keyIndex = eventTrack->CreateKey(0.01f);
  126. IEventKey key;
  127. eventTrack->SetKey(keyIndex, &key);
  128. key.event = "TestTrackEvent";
  129. key.eventValue = "TestValue";
  130. eventTrack->SetKey(keyIndex, &key);
  131. animSys->AddUiAnimationListener(sequence, m_canvasComponent);
  132. TrackEventHandler eventHandler;
  133. eventHandler.Connect(m_canvasComponent->GetEntityId());
  134. animSys->PlaySequence(sequence, nullptr, true, true);
  135. UnitTest::AnimationTestStubTimer* timer = GetTimer();
  136. for (int frame = 0; frame < 2; ++frame)
  137. {
  138. static float deltaTime = 1.0f / 60.0f;
  139. animSys->PreUpdate(deltaTime);
  140. animSys->PostUpdate(deltaTime);
  141. timer->AddFrameTime(deltaTime);
  142. }
  143. UiAnimationNotificationBus::ExecuteQueuedEvents();
  144. EXPECT_EQ(eventHandler.m_recievedEvents.size(), 1);
  145. EXPECT_STREQ(eventHandler.m_recievedEvents[0].m_event.c_str(), key.event.c_str());
  146. EXPECT_STREQ(eventHandler.m_recievedEvents[0].m_value.c_str(), key.eventValue.c_str());
  147. EXPECT_STREQ(eventHandler.m_recievedEvents[0].m_sequence.c_str(), sequence->GetName());
  148. }
  149. } //namespace UnitTest