SpawnableScriptMediatorTests.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 <AzCore/UnitTest/TestTypes.h>
  9. #include <AzCore/UserSettings/UserSettingsComponent.h>
  10. #include <AzFramework/Application/Application.h>
  11. #include <AzFramework/Components/TransformComponent.h>
  12. #include <AzFramework/Spawnable/SpawnableAssetHandler.h>
  13. #include <AzFramework/Spawnable/SpawnableEntitiesManager.h>
  14. #include <AzFramework/Spawnable/Script/SpawnableScriptAssetRef.h>
  15. #include <AzFramework/Spawnable/Script/SpawnableScriptBus.h>
  16. #include <AzFramework/Spawnable/Script/SpawnableScriptMediator.h>
  17. #include <AzTest/AzTest.h>
  18. namespace UnitTest
  19. {
  20. class SpawnableScriptMediatorTests
  21. : public LeakDetectionFixture
  22. , public AzFramework::Scripts::SpawnableScriptNotificationsBus::MultiHandler
  23. {
  24. public:
  25. using TicketToEntityIdsPair = AZStd::pair<AzFramework::EntitySpawnTicket, AZStd::vector<AZ::EntityId>>;
  26. constexpr static AZ::u64 EntityIdStartId = 40;
  27. AZStd::vector<TicketToEntityIdsPair> m_spawnedTicketAndEntitiesPairs ;
  28. void OnSpawn(AzFramework::EntitySpawnTicket spawnTicket, AZStd::vector<AZ::EntityId> entityList) override
  29. {
  30. m_spawnedTicketAndEntitiesPairs .push_back({ spawnTicket, AZStd::move(entityList) });
  31. }
  32. virtual void OnDespawn(AzFramework::EntitySpawnTicket spawnTicket) override
  33. {
  34. AZStd::erase_if(
  35. m_spawnedTicketAndEntitiesPairs ,
  36. [&spawnTicket](const TicketToEntityIdsPair& pair) -> bool
  37. {
  38. return pair.first == spawnTicket;
  39. });
  40. }
  41. void SetUp() override
  42. {
  43. LeakDetectionFixture::SetUp();
  44. m_application = new AzFramework::Application();
  45. AZ::ComponentApplication::Descriptor descriptor;
  46. AZ::ComponentApplication::StartupParameters startupParameters;
  47. startupParameters.m_loadSettingsRegistry = false;
  48. m_application->Start(descriptor, startupParameters);
  49. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  50. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  51. // in the unit tests.
  52. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  53. auto managerInterface = AzFramework::SpawnableEntitiesInterface::Get();
  54. m_manager = azrtti_cast<AzFramework::SpawnableEntitiesManager*>(managerInterface);
  55. }
  56. void TearDown() override
  57. {
  58. AzFramework::Scripts::SpawnableScriptNotificationsBus::MultiHandler::BusDisconnect();
  59. // One more tick on the spawnable entities manager in order to delete the ticket fully.
  60. m_manager->ProcessQueue(
  61. AzFramework::SpawnableEntitiesManager::CommandQueuePriority::High |
  62. AzFramework::SpawnableEntitiesManager::CommandQueuePriority::Regular);
  63. m_spawnedTicketAndEntitiesPairs = {};
  64. delete m_application;
  65. m_application = nullptr;
  66. LeakDetectionFixture::TearDown();
  67. }
  68. void WaitForResponse(AzFramework::Scripts::SpawnableScriptMediator& mediator)
  69. {
  70. m_manager->ProcessQueue(
  71. AzFramework::SpawnableEntitiesManager::CommandQueuePriority::High |
  72. AzFramework::SpawnableEntitiesManager::CommandQueuePriority::Regular);
  73. // force an additional tick on mediator to synchronize spawn callbacks and dispatch EBus notifications
  74. mediator.OnTick(0, AZ::ScriptTimePoint());
  75. }
  76. AzFramework::Scripts::SpawnableScriptAssetRef CreateSpawnable(size_t numElements)
  77. {
  78. AZ::Data::AssetId spawnableAssetId(AZ::Uuid::Create(), 0);
  79. auto spawnable = aznew AzFramework::Spawnable(spawnableAssetId, AZ::Data::AssetData::AssetStatus::Ready);
  80. AzFramework::Spawnable::EntityList& entities = spawnable->GetEntities();
  81. entities.reserve(numElements);
  82. for (size_t i = 0; i < numElements; ++i)
  83. {
  84. auto entry = AZStd::make_unique<AZ::Entity>();
  85. entry->SetId(AZ::EntityId(EntityIdStartId + i));
  86. AzFramework::TransformComponent* transformComponent = aznew AzFramework::TransformComponent();
  87. entry->AddComponent(transformComponent);
  88. entities.push_back(AZStd::move(entry));
  89. }
  90. auto spawnableAsset = AZ::Data::Asset<AzFramework::Spawnable>(spawnable, AZ::Data::AssetLoadBehavior::Default);
  91. AzFramework::Scripts::SpawnableScriptAssetRef spawnableScriptAssetRef;
  92. spawnableScriptAssetRef.SetAsset(spawnableAsset);
  93. return spawnableScriptAssetRef;
  94. }
  95. protected:
  96. AzFramework::SpawnableEntitiesManager* m_manager { nullptr };
  97. AzFramework::Application* m_application{ nullptr };
  98. };
  99. TEST_F(SpawnableScriptMediatorTests, CreateSpawnTicket_Works)
  100. {
  101. using namespace AzFramework::Scripts;
  102. auto spawnable = CreateSpawnable(1);
  103. SpawnableScriptMediator mediator;
  104. auto ticket = mediator.CreateSpawnTicket(spawnable);
  105. EXPECT_TRUE(ticket.IsValid());
  106. }
  107. TEST_F(SpawnableScriptMediatorTests, SpawnAndDespawn_Works)
  108. {
  109. using namespace AzFramework::Scripts;
  110. auto spawnable = CreateSpawnable(1);
  111. SpawnableScriptMediator mediator;
  112. const auto ticket = mediator.CreateSpawnTicket(spawnable);
  113. SpawnableScriptNotificationsBus::MultiHandler::BusConnect(ticket.GetId());
  114. mediator.Spawn(ticket);
  115. WaitForResponse(mediator);
  116. EXPECT_EQ(m_spawnedTicketAndEntitiesPairs .size(), 1);
  117. EXPECT_EQ(m_spawnedTicketAndEntitiesPairs [0].first, ticket);
  118. EXPECT_EQ(m_spawnedTicketAndEntitiesPairs [0].second.size(), 1);
  119. mediator.Despawn(ticket);
  120. WaitForResponse(mediator);
  121. EXPECT_TRUE(m_spawnedTicketAndEntitiesPairs .empty());
  122. }
  123. TEST_F(SpawnableScriptMediatorTests, SpawnMultipleAtOnce_Works)
  124. {
  125. using namespace AzFramework::Scripts;
  126. auto spawnable1 = CreateSpawnable(1);
  127. auto spawnable2 = CreateSpawnable(2);
  128. SpawnableScriptMediator mediator;
  129. auto ticket1 = mediator.CreateSpawnTicket(spawnable1);
  130. auto ticket2 = mediator.CreateSpawnTicket(spawnable2);
  131. SpawnableScriptNotificationsBus::MultiHandler::BusConnect(ticket1.GetId());
  132. SpawnableScriptNotificationsBus::MultiHandler::BusConnect(ticket2.GetId());
  133. mediator.Spawn(ticket1);
  134. mediator.Spawn(ticket2);
  135. WaitForResponse(mediator);
  136. auto it1 = AZStd::find_if(
  137. m_spawnedTicketAndEntitiesPairs .begin(), m_spawnedTicketAndEntitiesPairs .end(),
  138. [&ticket1](const TicketToEntityIdsPair& pair) -> bool
  139. {
  140. return pair.first == ticket1;
  141. });
  142. EXPECT_FALSE(it1 == m_spawnedTicketAndEntitiesPairs .end());
  143. EXPECT_EQ(it1->second.size(), 1);
  144. auto it2 = AZStd::find_if(
  145. m_spawnedTicketAndEntitiesPairs .begin(), m_spawnedTicketAndEntitiesPairs .end(),
  146. [&ticket2](const TicketToEntityIdsPair& pair) -> bool
  147. {
  148. return pair.first == ticket2;
  149. });
  150. EXPECT_FALSE(it2 == m_spawnedTicketAndEntitiesPairs .end());
  151. EXPECT_EQ(it2->second.size(), 2);
  152. mediator.Despawn(ticket1);
  153. mediator.Despawn(ticket2);
  154. WaitForResponse(mediator);
  155. EXPECT_TRUE(m_spawnedTicketAndEntitiesPairs .empty());
  156. }
  157. TEST_F(SpawnableScriptMediatorTests, SpawnAndParent_Works)
  158. {
  159. using namespace AzFramework::Scripts;
  160. auto spawnable = CreateSpawnable(1);
  161. SpawnableScriptMediator mediator;
  162. auto ticket = mediator.CreateSpawnTicket(spawnable);
  163. SpawnableScriptNotificationsBus::MultiHandler::BusConnect(ticket.GetId());
  164. AZ::EntityId parentId = AZ::Entity::MakeId();
  165. AZ::Entity parentEntity(parentId);
  166. AzFramework::TransformComponent parentTransformComponent;
  167. parentEntity.AddComponent(&parentTransformComponent);
  168. parentEntity.Init();
  169. parentEntity.ApplyEffectiveActiveState();
  170. mediator.SpawnAndParent(ticket, parentEntity.GetId());
  171. WaitForResponse(mediator);
  172. AZStd::vector<AZ::EntityId> descendantIds;
  173. AZ::TransformBus::EventResult(descendantIds, parentId, &AZ::TransformBus::Events::GetAllDescendants);
  174. EXPECT_EQ(descendantIds.size(), 1);
  175. EXPECT_EQ(m_spawnedTicketAndEntitiesPairs .size(), 1);
  176. EXPECT_EQ(m_spawnedTicketAndEntitiesPairs [0].second.size(), 1);
  177. EXPECT_EQ(descendantIds[0], m_spawnedTicketAndEntitiesPairs [0].second[0]);
  178. mediator.Despawn(ticket);
  179. WaitForResponse(mediator);
  180. // Wait for entities to actually be destroyed
  181. AZ::TickBus::ExecuteQueuedEvents();
  182. AZ::TransformBus::EventResult(descendantIds, parentId, &AZ::TransformBus::Events::GetAllDescendants);
  183. EXPECT_TRUE(descendantIds.empty());
  184. parentEntity.SetEntityActive(false);
  185. parentEntity.ApplyEffectiveActiveState();
  186. }
  187. TEST_F(SpawnableScriptMediatorTests, SpawnAndParentAndTransform_Works)
  188. {
  189. using namespace AzFramework::Scripts;
  190. auto spawnable = CreateSpawnable(1);
  191. SpawnableScriptMediator mediator;
  192. auto ticket = mediator.CreateSpawnTicket(spawnable);
  193. SpawnableScriptNotificationsBus::MultiHandler::BusConnect(ticket.GetId());
  194. AZ::EntityId parentId = AZ::Entity::MakeId();
  195. AZ::Entity parentEntity(parentId);
  196. AzFramework::TransformComponent parentTransformComponent;
  197. parentEntity.AddComponent(&parentTransformComponent);
  198. parentEntity.Init();
  199. parentEntity.Activate();
  200. AZ::Vector3 translation(5, 0, 0);
  201. AZ::Vector3 rotation(90, 0, 0);
  202. float scale = 2.0f;
  203. mediator.SpawnAndParentAndTransform(ticket, parentEntity.GetId(), translation, rotation, scale);
  204. WaitForResponse(mediator);
  205. AZStd::vector<AZ::EntityId> descendantIds;
  206. AZ::TransformBus::EventResult(descendantIds, parentId, &AZ::TransformBus::Events::GetAllDescendants);
  207. EXPECT_EQ(descendantIds.size(), 1);
  208. auto it = AZStd::find_if(
  209. m_spawnedTicketAndEntitiesPairs .begin(), m_spawnedTicketAndEntitiesPairs .end(),
  210. [&ticket](const TicketToEntityIdsPair& pair) -> bool
  211. {
  212. return pair.first.GetId() == ticket.GetId();
  213. });
  214. EXPECT_FALSE(it == m_spawnedTicketAndEntitiesPairs .end());
  215. EXPECT_FALSE(it->second.empty());
  216. AZ::EntityId entityId = it->second[0];
  217. AZ::Vector3 spawnedTranslation = {};
  218. AZ::Quaternion spawnedRotation = {};
  219. float spawnedScale;
  220. AZ::TransformBus::EventResult(spawnedTranslation, entityId, &AZ::TransformBus::Events::GetLocalTranslation);
  221. AZ::TransformBus::EventResult(spawnedRotation, entityId, &AZ::TransformBus::Events::GetLocalRotationQuaternion);
  222. AZ::TransformBus::EventResult(spawnedScale, entityId, &AZ::TransformBus::Events::GetLocalUniformScale);
  223. EXPECT_EQ(translation, spawnedTranslation);
  224. EXPECT_EQ(AZ::Quaternion::CreateFromEulerAnglesDegrees(rotation), spawnedRotation);
  225. EXPECT_EQ(scale, spawnedScale);
  226. mediator.Despawn(ticket);
  227. WaitForResponse(mediator);
  228. // Wait for entities to actually be destroyed
  229. AZ::TickBus::ExecuteQueuedEvents();
  230. AZ::TransformBus::EventResult(descendantIds, parentId, &AZ::TransformBus::Events::GetAllDescendants);
  231. EXPECT_TRUE(descendantIds.empty());
  232. parentEntity.Deactivate();
  233. }
  234. } // namespace UnitTest