123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include "LyShineTest.h"
- #include <Mocks/ISystemMock.h>
- #include <AzCore/UnitTest/Mocks/MockITime.h>
- #include <AzCore/UserSettings/UserSettingsComponent.h>
- #include <AzFramework/Application/Application.h>
- #include <UiCanvasComponent.h>
- #include <Animation/AnimSequence.h>
- #include <Animation/EventNode.h>
- #include <Animation/TrackEventTrack.h>
- namespace UnitTest
- {
- struct AnimationTestStubTimer : public AZ::StubTimeSystem
- {
- AZ_RTTI(UnitTest::AnimationTestStubTimer, "{541EBC6C-E793-4433-9402-4CAD2F6770E3}", AZ::StubTimeSystem);
- AZ::TimeMs GetElapsedTimeMs() const override
- {
- return AZ::TimeUsToMs(m_timeUs);
- }
- AZ::TimeUs GetElapsedTimeUs() const override
- {
- return m_timeUs;
- }
- void AddFrameTime(float sec)
- {
- m_timeUs += AZ::SecondsToTimeUs(sec);
- }
- AZ::TimeUs m_timeUs = AZ::Time::ZeroTimeUs;
- };
- class TrackEventHandler
- : public UiAnimationNotificationBus::Handler
- {
- public:
- void Connect(AZ::EntityId id)
- {
- m_busId = id;
- UiAnimationNotificationBus::Handler::BusConnect(id);
- }
- ~TrackEventHandler()
- {
- UiAnimationNotificationBus::Handler::BusDisconnect(m_busId);
- }
- void OnUiAnimationEvent([[maybe_unused]] IUiAnimationListener::EUiAnimationEvent uiAnimationEvent, [[maybe_unused]] AZStd::string animSequenceName) {};
- void OnUiTrackEvent(AZStd::string eventName, AZStd::string valueName, AZStd::string animSequenceName)
- {
- m_recievedEvents.push_back(EventInfo{ eventName, valueName, animSequenceName });
- }
- struct EventInfo
- {
- AZStd::string m_event;
- AZStd::string m_value;
- AZStd::string m_sequence;
- };
- AZ::EntityId m_busId;
- AZStd::vector<EventInfo> m_recievedEvents;
- };
- class LyShineAnimationTestApplication : public AzFramework::Application
- {
- public:
- LyShineAnimationTestApplication()
- : AzFramework::Application()
- {
- m_timeSystem.reset();
- m_timeSystem = AZStd::make_unique<UnitTest::AnimationTestStubTimer>();
- }
- UnitTest::AnimationTestStubTimer* GetTimer()
- {
- return azdynamic_cast<UnitTest::AnimationTestStubTimer*>(m_timeSystem.get());
- }
- };
- class LyShineAnimationTest
- : public LyShineTest
- {
- protected:
- LyShineAnimationTest()
- : m_canvasComponent(nullptr)
- {
- }
- void SetupApplication() override
- {
- AZ::ComponentApplication::Descriptor appDesc;
- appDesc.m_memoryBlocksByteSize = 10 * 1024 * 1024;
- appDesc.m_recordingMode = AZ::Debug::AllocationRecords::Mode::RECORD_FULL;
- m_application = aznew LyShineAnimationTestApplication();
- m_systemEntity = m_application->Create(appDesc);
- m_systemEntity->Init();
- m_systemEntity->Activate();
- // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
- // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
- // in the unit tests.
- AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
- }
- void SetupEnvironment() override
- {
- LyShineTest::SetupEnvironment();
- m_canvasComponent = aznew UiCanvasComponent;
- }
- void TearDown() override
- {
- delete m_canvasComponent;
- UiAnimationNotificationBus::ClearQueuedEvents();
- LyShineTest::TearDown();
- }
- UnitTest::AnimationTestStubTimer* GetTimer()
- {
- return static_cast<LyShineAnimationTestApplication*>(m_application)->GetTimer();
- }
- UiCanvasComponent* m_canvasComponent;
- };
- TEST_F(LyShineAnimationTest, Animation_TrackEventTriggered_FT)
- {
- IUiAnimationSystem* animSys = m_canvasComponent->GetAnimationSystem();
- IUiAnimSequence* sequence = animSys->CreateSequence("TestSequence", true);
- CUiAnimEventNode* eventNode = aznew CUiAnimEventNode;
- sequence->AddNode(eventNode);
- sequence->AddTrackEvent("TestTrackEvent");
- eventNode->CreateDefaultTracks();
- IUiAnimTrack* eventTrack = eventNode->GetTrackByIndex(0);
- int keyIndex = eventTrack->CreateKey(0.01f);
- IEventKey key;
- eventTrack->SetKey(keyIndex, &key);
- key.event = "TestTrackEvent";
- key.eventValue = "TestValue";
- eventTrack->SetKey(keyIndex, &key);
- animSys->AddUiAnimationListener(sequence, m_canvasComponent);
- TrackEventHandler eventHandler;
- eventHandler.Connect(m_canvasComponent->GetEntityId());
- animSys->PlaySequence(sequence, nullptr, true, true);
- UnitTest::AnimationTestStubTimer* timer = GetTimer();
- for (int frame = 0; frame < 2; ++frame)
- {
- static float deltaTime = 1.0f / 60.0f;
- animSys->PreUpdate(deltaTime);
- animSys->PostUpdate(deltaTime);
- timer->AddFrameTime(deltaTime);
- }
- UiAnimationNotificationBus::ExecuteQueuedEvents();
- EXPECT_EQ(eventHandler.m_recievedEvents.size(), 1);
- EXPECT_STREQ(eventHandler.m_recievedEvents[0].m_event.c_str(), key.event.c_str());
- EXPECT_STREQ(eventHandler.m_recievedEvents[0].m_value.c_str(), key.eventValue.c_str());
- EXPECT_STREQ(eventHandler.m_recievedEvents[0].m_sequence.c_str(), sequence->GetName());
- }
- } //namespace UnitTest
|