SpawnerComponentTest.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 "LmbrCentralReflectionTest.h"
  9. #include <AzCore/Asset/AssetManager.h>
  10. #include <AzCore/Asset/AssetManagerComponent.h>
  11. #include <AzCore/Jobs/JobManagerComponent.h>
  12. #include <AzCore/IO/Streamer/StreamerComponent.h>
  13. #include <AzCore/Slice/SliceComponent.h>
  14. #include <AzCore/Slice/SliceSystemComponent.h>
  15. #include <AzFramework/Application/Application.h>
  16. #include <AzFramework/Asset/AssetSystemComponent.h>
  17. #include <AzFramework/Components/TransformComponent.h>
  18. #include <AzFramework/Entity/GameEntityContextComponent.h>
  19. #include <AzCore/UnitTest/TestTypes.h>
  20. // LmbrCentral/Source
  21. #include "Scripting/SpawnerComponent.h"
  22. #include "LmbrCentral.h"
  23. #ifdef LMBR_CENTRAL_EDITOR
  24. #include <AzToolsFramework/Application/ToolsApplication.h>
  25. #include <AzToolsFramework/ToolsComponents/GenericComponentWrapper.h>
  26. #include "LmbrCentralEditor.h"
  27. #include "Scripting/EditorSpawnerComponent.h"
  28. #endif
  29. // Tracks SpawnerComponentNotificationBus events
  30. class SpawnWatcher
  31. : public LmbrCentral::SpawnerComponentNotificationBus::Handler
  32. {
  33. public:
  34. AZ_CLASS_ALLOCATOR(SpawnWatcher, AZ::SystemAllocator);
  35. SpawnWatcher(AZ::EntityId spawnerEntityId)
  36. {
  37. LmbrCentral::SpawnerComponentNotificationBus::Handler::BusConnect(spawnerEntityId);
  38. }
  39. void OnSpawnBegin(const AzFramework::SliceInstantiationTicket& ticket) override
  40. {
  41. m_tickets[ticket].m_onSpawnBegin = true;
  42. }
  43. void OnSpawnEnd(const AzFramework::SliceInstantiationTicket& ticket) override
  44. {
  45. m_tickets[ticket].m_onSpawnEnd = true;
  46. }
  47. void OnEntitySpawned(const AzFramework::SliceInstantiationTicket& ticket, const AZ::EntityId& spawnedEntity) override
  48. {
  49. m_tickets[ticket].m_onEntitySpawned.emplace_back(spawnedEntity);
  50. }
  51. void OnEntitiesSpawned(const AzFramework::SliceInstantiationTicket& ticket, const AZStd::vector<AZ::EntityId>& spawnedEntities) override
  52. {
  53. m_tickets[ticket].m_onEntitiesSpawned = spawnedEntities;
  54. }
  55. void OnSpawnedSliceDestroyed(const AzFramework::SliceInstantiationTicket& ticket) override
  56. {
  57. m_tickets[ticket].m_onSpawnedSliceDestroyed = true;
  58. }
  59. // Which events have fired for a particular ticket
  60. struct TicketInfo
  61. {
  62. bool m_onSpawnBegin = false;
  63. bool m_onSpawnEnd = false;
  64. AZStd::vector<AZ::EntityId> m_onEntitySpawned;
  65. AZStd::vector<AZ::EntityId> m_onEntitiesSpawned;
  66. bool m_onSpawnedSliceDestroyed = false;
  67. };
  68. AZStd::unordered_map<AzFramework::SliceInstantiationTicket, TicketInfo> m_tickets;
  69. };
  70. // Simplified version of AzFramework::Application
  71. class SpawnerApplication
  72. : public AzFramework::Application
  73. {
  74. // override and only include system components required for spawner tests.
  75. AZ::ComponentTypeList GetRequiredSystemComponents() const override
  76. {
  77. return AZ::ComponentTypeList{
  78. azrtti_typeid<AZ::JobManagerComponent>(),
  79. azrtti_typeid<AZ::StreamerComponent>(),
  80. azrtti_typeid<AZ::AssetManagerComponent>(),
  81. azrtti_typeid<AZ::SliceSystemComponent>(),
  82. azrtti_typeid<AzFramework::GameEntityContextComponent>(),
  83. azrtti_typeid<AzFramework::AssetSystem::AssetSystemComponent>(),
  84. };
  85. }
  86. void RegisterCoreComponents() override
  87. {
  88. AzFramework::Application::RegisterCoreComponents();
  89. RegisterComponentDescriptor(LmbrCentral::SpawnerComponent::CreateDescriptor());
  90. }
  91. };
  92. class SpawnerComponentTest
  93. : public UnitTest::LeakDetectionFixture
  94. {
  95. public:
  96. void SetUp() override
  97. {
  98. // start application
  99. AZ::ComponentApplication::Descriptor appDescriptor;
  100. appDescriptor.m_useExistingAllocator = true;
  101. m_application = aznew SpawnerApplication();
  102. AZ::ComponentApplication::StartupParameters startupParameters;
  103. startupParameters.m_loadSettingsRegistry = false;
  104. m_application->Start(appDescriptor, startupParameters);
  105. // create a dynamic slice in the asset system
  106. AZ::Entity* sliceAssetEntity = aznew AZ::Entity();
  107. AZ::SliceComponent* sliceAssetComponent = sliceAssetEntity->CreateComponent<AZ::SliceComponent>();
  108. sliceAssetComponent->SetSerializeContext(m_application->GetSerializeContext());
  109. sliceAssetEntity->Init();
  110. sliceAssetEntity->Activate();
  111. AZ::Entity* entityInSlice1 = aznew AZ::Entity("spawned entity 1");
  112. entityInSlice1->CreateComponent<AzFramework::TransformComponent>();
  113. sliceAssetComponent->AddEntity(entityInSlice1);
  114. AZ::Entity* entityInSlice2 = aznew AZ::Entity("spawned entity 2");
  115. entityInSlice2->CreateComponent<AzFramework::TransformComponent>();
  116. sliceAssetComponent->AddEntity(entityInSlice2);
  117. m_sliceAssetRef = AZ::Data::AssetManager::Instance().CreateAsset<AZ::SliceAsset>(AZ::Data::AssetId("{E47E78B1-FF5E-4191-BE72-A06428D324F3}"), AZ::Data::AssetLoadBehavior::Default);
  118. m_sliceAssetRef.Get()->SetData(sliceAssetEntity, sliceAssetComponent);
  119. // create an entity with a spawner component
  120. AZ::Entity* spawnerEntity = aznew AZ::Entity("spawner");
  121. m_spawnerComponent = spawnerEntity->CreateComponent<LmbrCentral::SpawnerComponent>();
  122. spawnerEntity->Init();
  123. spawnerEntity->Activate();
  124. // create class that will watch for spawner component notifications
  125. m_spawnWatcher = aznew SpawnWatcher(m_spawnerComponent->GetEntityId());
  126. }
  127. void TearDown() override
  128. {
  129. delete m_spawnWatcher;
  130. m_spawnWatcher = nullptr;
  131. delete m_application->FindEntity(m_spawnerComponent->GetEntityId());
  132. m_spawnerComponent = nullptr;
  133. // reset game context (delete any spawned slices and their entities)
  134. AzFramework::GameEntityContextRequestBus::Broadcast(&AzFramework::GameEntityContextRequestBus::Events::ResetGameContext);
  135. m_sliceAssetRef = AZ::Data::Asset<AZ::SliceAsset>();
  136. m_application->Stop();
  137. delete m_application;
  138. m_application = nullptr;
  139. }
  140. // Tick application until 'condition' function returns true.
  141. // If 'maxTicks' elapse without condition passing, return false.
  142. bool TickUntil(AZStd::function<bool()> condition, size_t maxTicks=100)
  143. {
  144. for (size_t tickI = 0; tickI < maxTicks; ++tickI)
  145. {
  146. if (condition())
  147. {
  148. return true;
  149. }
  150. m_application->Tick();
  151. }
  152. return false;
  153. }
  154. // Common test operation: Spawn m_sliceAssetRef and tick application until OnSpawnEnd fires.
  155. AzFramework::SliceInstantiationTicket SpawnDefaultSlice()
  156. {
  157. AzFramework::SliceInstantiationTicket ticket = m_spawnerComponent->SpawnSlice(m_sliceAssetRef);
  158. bool onSpawnEndFired = TickUntil([this, ticket]() { return m_spawnWatcher->m_tickets[ticket].m_onSpawnEnd; });
  159. EXPECT_TRUE(onSpawnEndFired); // sanity check
  160. return ticket;
  161. }
  162. // Common test operation: Spawn m_sliceAssetRef many times and tick application until OnSpawnEnd fires for each spawn.
  163. AZStd::vector<AzFramework::SliceInstantiationTicket> SpawnManyDefaultSlices()
  164. {
  165. AZStd::vector<AzFramework::SliceInstantiationTicket> tickets;
  166. for (int i = 0; i < 10; ++i)
  167. {
  168. tickets.emplace_back(m_spawnerComponent->SpawnSlice(m_sliceAssetRef));
  169. }
  170. bool onSpawnEndFiredForAll = TickUntil(
  171. [this, &tickets]()
  172. {
  173. for (AzFramework::SliceInstantiationTicket& ticket : tickets)
  174. {
  175. if (!m_spawnWatcher->m_tickets[ticket].m_onSpawnEnd)
  176. {
  177. return false;
  178. }
  179. }
  180. return true;
  181. });
  182. EXPECT_TRUE(onSpawnEndFiredForAll); // sanity check
  183. return tickets;
  184. }
  185. SpawnerApplication* m_application = nullptr;
  186. AZ::Data::Asset<AZ::SliceAsset> m_sliceAssetRef; // a slice asset to spawn
  187. LmbrCentral::SpawnerComponent* m_spawnerComponent = nullptr; // a spawner component to test
  188. SpawnWatcher* m_spawnWatcher = nullptr; // tracks events from the spawner component
  189. };
  190. const size_t kEntitiesInSlice = 2; // number of entities in asset we're testing with
  191. TEST_F(SpawnerComponentTest, SanityCheck)
  192. {
  193. // Tests that Setup/TearDown work as expected
  194. }
  195. TEST_F(SpawnerComponentTest, SpawnSlice_OnSpawnEnd_Fires)
  196. {
  197. // First test the helper function, which checks for OnSpawnEnd
  198. SpawnDefaultSlice();
  199. }
  200. TEST_F(SpawnerComponentTest, SpawnSlice_OnSpawnBegin_Fires)
  201. {
  202. AzFramework::SliceInstantiationTicket ticket = SpawnDefaultSlice();
  203. EXPECT_TRUE(m_spawnWatcher->m_tickets[ticket].m_onSpawnBegin);
  204. }
  205. TEST_F(SpawnerComponentTest, SpawnSlice_OnEntitySpawned_FiresOncePerEntity)
  206. {
  207. AzFramework::SliceInstantiationTicket ticket = SpawnDefaultSlice();
  208. EXPECT_EQ(kEntitiesInSlice, m_spawnWatcher->m_tickets[ticket].m_onEntitySpawned.size());
  209. }
  210. TEST_F(SpawnerComponentTest, SpawnSlice_OnEntitiesSpawned_FiresWithAllEntities)
  211. {
  212. AzFramework::SliceInstantiationTicket ticket = SpawnDefaultSlice();
  213. EXPECT_EQ(kEntitiesInSlice, m_spawnWatcher->m_tickets[ticket].m_onEntitiesSpawned.size());
  214. }
  215. TEST_F(SpawnerComponentTest, OnSpawnedSliceDestroyed_FiresAfterEntitiesDeleted)
  216. {
  217. AzFramework::SliceInstantiationTicket ticket = SpawnDefaultSlice();
  218. for (AZ::EntityId spawnedEntityId : m_spawnWatcher->m_tickets[ticket].m_onEntitiesSpawned)
  219. {
  220. AzFramework::GameEntityContextRequestBus::Broadcast(&AzFramework::GameEntityContextRequestBus::Events::DestroyGameEntity, spawnedEntityId);
  221. }
  222. bool spawnDestructionFired = TickUntil([this, ticket]() { return m_spawnWatcher->m_tickets[ticket].m_onSpawnedSliceDestroyed; });
  223. EXPECT_TRUE(spawnDestructionFired);
  224. }
  225. TEST_F(SpawnerComponentTest, DISABLED_OnSpawnedSliceDestroyed_FiresWhenSpawningBadAssets) // disabled because AZ_TEST_START_TRACE_SUPPRESSION isn't currently suppressing the asserts
  226. {
  227. // ID is made up and not registered with asset manager
  228. auto nonexistentAsset = AZ::Data::Asset<AZ::SliceAsset>(AZ::Data::AssetId("{9E3862CC-B6DF-485F-A9D8-5F4A966DE88B}"), AZ::AzTypeInfo<AZ::SliceAsset>::Uuid());
  229. AzFramework::SliceInstantiationTicket ticket = m_spawnerComponent->SpawnSlice(nonexistentAsset);
  230. AZ_TEST_START_TRACE_SUPPRESSION;
  231. bool spawnDestructionFired = TickUntil([this, ticket]() { return m_spawnWatcher->m_tickets[ticket].m_onSpawnedSliceDestroyed; });
  232. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  233. EXPECT_TRUE(spawnDestructionFired);
  234. }
  235. TEST_F(SpawnerComponentTest, DestroySpawnedSlice_EntitiesFromSpawn_AreDeleted)
  236. {
  237. AzFramework::SliceInstantiationTicket ticket = SpawnDefaultSlice();
  238. m_spawnerComponent->DestroySpawnedSlice(ticket);
  239. bool entitiesRemoved = TickUntil(
  240. [this, ticket]()
  241. {
  242. for (AZ::EntityId entityId : m_spawnWatcher->m_tickets[ticket].m_onEntitiesSpawned)
  243. {
  244. if (m_application->FindEntity(entityId))
  245. {
  246. return false;
  247. }
  248. }
  249. return true;
  250. });
  251. EXPECT_TRUE(entitiesRemoved);
  252. }
  253. TEST_F(SpawnerComponentTest, DestroySpawnedSlice_OnSpawnedSliceDestroyed_Fires)
  254. {
  255. AzFramework::SliceInstantiationTicket ticket = SpawnDefaultSlice();
  256. m_spawnerComponent->DestroySpawnedSlice(ticket);
  257. bool onSpawnedSliceDestroyed = TickUntil([this, ticket](){ return m_spawnWatcher->m_tickets[ticket].m_onSpawnedSliceDestroyed; });
  258. EXPECT_TRUE(onSpawnedSliceDestroyed);
  259. }
  260. TEST_F(SpawnerComponentTest, DestroySpawnedSlice_BeforeOnSpawnBegin_PreventsInstantiation)
  261. {
  262. AzFramework::SliceInstantiationTicket ticket = m_spawnerComponent->SpawnSlice(m_sliceAssetRef);
  263. m_spawnerComponent->DestroySpawnedSlice(ticket);
  264. // wait a long time, just to be sure no queued entity instantiation takes place
  265. for (int i = 0; i < 100; ++i)
  266. {
  267. m_application->Tick();
  268. }
  269. EXPECT_FALSE(m_spawnWatcher->m_tickets[ticket].m_onSpawnBegin);
  270. EXPECT_TRUE(m_spawnWatcher->m_tickets[ticket].m_onSpawnedSliceDestroyed);
  271. }
  272. class GameEntityContextWatcher
  273. : public AzFramework::SliceGameEntityOwnershipServiceNotificationBus::Handler
  274. {
  275. public:
  276. GameEntityContextWatcher()
  277. {
  278. AzFramework::SliceGameEntityOwnershipServiceNotificationBus::Handler::BusConnect();
  279. }
  280. void OnSliceInstantiated(const AZ::Data::AssetId& /*sliceAssetId*/, const AZ::SliceComponent::SliceInstanceAddress& /*instance*/, const AzFramework::SliceInstantiationTicket& ticket) override
  281. {
  282. m_onSliceInstantiatedTickets.emplace(ticket);
  283. }
  284. void OnSliceInstantiationFailed(const AZ::Data::AssetId& /*sliceAssetId*/, const AzFramework::SliceInstantiationTicket& ticket) override
  285. {
  286. m_onSliceInstantiationFailedTickets.emplace(ticket);
  287. }
  288. AZStd::unordered_set<AzFramework::SliceInstantiationTicket> m_onSliceInstantiatedTickets;
  289. AZStd::unordered_set<AzFramework::SliceInstantiationTicket> m_onSliceInstantiationFailedTickets;
  290. };
  291. TEST_F(SpawnerComponentTest, DestroySpawnedSlice_BeforeOnSpawnBegin_ContextFiresOnSliceInstantiationFailed)
  292. {
  293. // context should send out instantiation failure message, even if ticket is explicitly cancelled.
  294. // others might be listening to the context and not know about the cancellation.
  295. GameEntityContextWatcher contextWatcher;
  296. AzFramework::SliceInstantiationTicket ticket = m_spawnerComponent->SpawnSlice(m_sliceAssetRef);
  297. m_spawnerComponent->DestroySpawnedSlice(ticket);
  298. TickUntil([this, ticket]() { return m_spawnWatcher->m_tickets[ticket].m_onSpawnedSliceDestroyed; });
  299. bool onSliceInstantiationFailed = contextWatcher.m_onSliceInstantiationFailedTickets.count(ticket) > 0;
  300. EXPECT_TRUE(onSliceInstantiationFailed);
  301. bool onSliceInstantiated = contextWatcher.m_onSliceInstantiatedTickets.count(ticket) > 0;
  302. EXPECT_FALSE(onSliceInstantiated);
  303. }
  304. TEST_F(SpawnerComponentTest, DestroySpawnedSlice_WhenManySpawnsInProgress_DoesntAffectOtherSpawns)
  305. {
  306. AZStd::vector<AzFramework::SliceInstantiationTicket> tickets;
  307. for (int i = 0; i < 10; ++i)
  308. {
  309. tickets.emplace_back(m_spawnerComponent->SpawnSlice(m_sliceAssetRef));
  310. }
  311. m_spawnerComponent->DestroySpawnedSlice(tickets[0]);
  312. // check that other slices finish spawning
  313. bool entitiesSpawnedInOtherSlices = TickUntil(
  314. [this, &tickets]()
  315. {
  316. for (size_t i = 1; i < tickets.size(); ++i)
  317. {
  318. if (m_spawnWatcher->m_tickets[tickets[i]].m_onEntitiesSpawned.size() > 0)
  319. {
  320. return false;
  321. }
  322. }
  323. return true;
  324. });
  325. EXPECT_TRUE(entitiesSpawnedInOtherSlices);
  326. // check that one slice destroyed
  327. bool sliceDestroyed = TickUntil([this, &tickets]()
  328. {
  329. return m_spawnWatcher->m_tickets[tickets[0]].m_onSpawnedSliceDestroyed;
  330. });
  331. EXPECT_TRUE(sliceDestroyed);
  332. // make sure no other slice get destroyed
  333. bool anyOtherSliceDestroyed = false;
  334. for (size_t i = 1; i < tickets.size(); ++i)
  335. {
  336. if (m_spawnWatcher->m_tickets[tickets[i]].m_onSpawnedSliceDestroyed)
  337. {
  338. anyOtherSliceDestroyed = true;
  339. }
  340. }
  341. EXPECT_FALSE(anyOtherSliceDestroyed);
  342. }
  343. TEST_F(SpawnerComponentTest, DestroyAllSpawnedSlices_AllSpawnedEntities_AreDestroyed)
  344. {
  345. AZStd::vector<AzFramework::SliceInstantiationTicket> tickets = SpawnManyDefaultSlices();
  346. m_spawnerComponent->DestroyAllSpawnedSlices();
  347. bool allEntitiesDestroyed = TickUntil(
  348. [this, &tickets]()
  349. {
  350. for (AzFramework::SliceInstantiationTicket& ticket : tickets)
  351. {
  352. for (AZ::EntityId spawnedEntityId : m_spawnWatcher->m_tickets[ticket].m_onEntitiesSpawned)
  353. {
  354. if (m_application->FindEntity(spawnedEntityId))
  355. {
  356. return false;
  357. }
  358. }
  359. }
  360. return true;
  361. });
  362. EXPECT_TRUE(allEntitiesDestroyed);
  363. }
  364. TEST_F(SpawnerComponentTest, DestroyAllSpawnedSlices_OnSpawnedSliceDestroyed_FiresForAll)
  365. {
  366. AZStd::vector<AzFramework::SliceInstantiationTicket> tickets = SpawnManyDefaultSlices();
  367. m_spawnerComponent->DestroyAllSpawnedSlices();
  368. bool onSpawnedSliceDestroyedFiresForAll = TickUntil(
  369. [this, &tickets]()
  370. {
  371. for (AzFramework::SliceInstantiationTicket& ticket : tickets)
  372. {
  373. if (!m_spawnWatcher->m_tickets[ticket].m_onSpawnedSliceDestroyed)
  374. {
  375. return false;
  376. }
  377. }
  378. return true;
  379. });
  380. EXPECT_TRUE(onSpawnedSliceDestroyedFiresForAll);
  381. }
  382. TEST_F(SpawnerComponentTest, DestroyAllSpawnedSlices_BeforeOnSpawnBegin_PreventsInstantiation)
  383. {
  384. AZStd::vector<AzFramework::SliceInstantiationTicket> tickets;
  385. for (int i = 0; i < 10; ++i)
  386. {
  387. tickets.emplace_back(m_spawnerComponent->SpawnSlice(m_sliceAssetRef));
  388. }
  389. m_spawnerComponent->DestroyAllSpawnedSlices();
  390. // wait a long time, to ensure no queued activity results in an instantiation
  391. for (int i = 0; i < 100; ++i)
  392. {
  393. m_application->Tick();
  394. }
  395. bool anyOnSpawnBegan = false;
  396. bool allOnSpawnedSliceDestroyed = true;
  397. for (AzFramework::SliceInstantiationTicket& ticket : tickets)
  398. {
  399. anyOnSpawnBegan |= m_spawnWatcher->m_tickets[ticket].m_onSpawnBegin;
  400. allOnSpawnedSliceDestroyed &= m_spawnWatcher->m_tickets[ticket].m_onSpawnedSliceDestroyed;
  401. }
  402. EXPECT_FALSE(anyOnSpawnBegan);
  403. EXPECT_TRUE(allOnSpawnedSliceDestroyed);
  404. }
  405. TEST_F(SpawnerComponentTest, GetCurrentEntitiesFromSpawnedSlice_ReturnsEntities)
  406. {
  407. AzFramework::SliceInstantiationTicket ticket = SpawnDefaultSlice();
  408. AZStd::vector<AZ::EntityId> entities = m_spawnerComponent->GetCurrentEntitiesFromSpawnedSlice(ticket);
  409. EXPECT_EQ(m_spawnWatcher->m_tickets[ticket].m_onEntitiesSpawned.size(), entities.size());
  410. }
  411. TEST_F(SpawnerComponentTest, GetCurrentEntitiesFromSpawnedSlice_WithEntityDeleted_DoesNotReturnDeletedEntity)
  412. {
  413. AzFramework::SliceInstantiationTicket ticket = SpawnDefaultSlice();
  414. AZStd::vector<AZ::EntityId>& entitiesBeforeDelete = m_spawnWatcher->m_tickets[ticket].m_onEntitiesSpawned;
  415. AZ::EntityId entityToDelete = entitiesBeforeDelete[0];
  416. delete m_application->FindEntity(entityToDelete);
  417. AZStd::vector<AZ::EntityId> entitiesAfterDelete = m_spawnerComponent->GetCurrentEntitiesFromSpawnedSlice(ticket);
  418. EXPECT_EQ(entitiesBeforeDelete.size() - 1, entitiesAfterDelete.size());
  419. bool deletedEntityPresent = AZStd::find(entitiesAfterDelete.begin(), entitiesAfterDelete.end(), entityToDelete) != entitiesAfterDelete.end();
  420. EXPECT_FALSE(deletedEntityPresent);
  421. }
  422. TEST_F(SpawnerComponentTest, GetAllCurrentlySpawnedEntities_ReturnsEntities)
  423. {
  424. AZStd::vector<AzFramework::SliceInstantiationTicket> tickets = SpawnManyDefaultSlices();
  425. AZStd::vector<AZ::EntityId> entities = m_spawnerComponent->GetAllCurrentlySpawnedEntities();
  426. bool allEntitiesFound = true;
  427. size_t numEntities = 0;
  428. // compare against entities from OnEntitiesSpawned event
  429. for (auto& ticketInfoPair : m_spawnWatcher->m_tickets)
  430. {
  431. for (AZ::EntityId spawnedEntity : ticketInfoPair.second.m_onEntitiesSpawned)
  432. {
  433. ++numEntities;
  434. allEntitiesFound &= AZStd::find(entities.begin(), entities.end(), spawnedEntity) != entities.end();
  435. }
  436. }
  437. EXPECT_EQ(numEntities, entities.size());
  438. EXPECT_TRUE(allEntitiesFound);
  439. }
  440. // Legacy SpawnerComponent from game data
  441. // Should get converted into modern SpawnerComponent
  442. const char kWrappedGameSpawnerComponent[] =
  443. R"DELIMITER(<ObjectStream version="3">
  444. <Class name="SpawnerComponent" field="element" version="1" type="{8022A627-FA7D-4516-A155-657A0927A3CA}">
  445. <Class name="AZ::Component" field="BaseClass1" type="{EDFCB2CF-F75D-43BE-B26B-F35821B29247}">
  446. <Class name="AZ::u64" field="Id" value="8317941343245109563" type="{D6597933-47CD-4FC8-B911-63F3E2B0993A}"/>
  447. </Class>
  448. <Class name="Asset" field="Slice" value="id={6F11134F-84C9-559F-AABA-3D1778656707}:2,type={78802ABF-9595-463A-8D2B-D022F906F9B1},hint={slices/particle_electrical_damage.dynamicslice}" version="1" type="{77A19D40-8731-4D3C-9041-1B43047366A4}"/>
  449. <Class name="bool" field="SpawnOnActivate" value="true" type="{A0CA880C-AFE4-43CB-926C-59AC48496112}"/>
  450. <Class name="bool" field="DestroyOnDeactivate" value="true" type="{A0CA880C-AFE4-43CB-926C-59AC48496112}"/>
  451. </Class>
  452. </ObjectStream>)DELIMITER";
  453. class LoadSpawnerComponentFromLegacyGameData
  454. : public LoadReflectedObjectTest<AzFramework::Application, LmbrCentral::LmbrCentralModule, LmbrCentral::SpawnerComponent>
  455. {
  456. public:
  457. const char* GetSourceDataBuffer() const { return kWrappedGameSpawnerComponent; }
  458. void SetUp()
  459. {
  460. LoadReflectedObjectTest::SetUp();
  461. if (m_object)
  462. {
  463. m_readConfigSuccess = m_object->GetConfiguration(m_spawnerConfig);
  464. }
  465. }
  466. LmbrCentral::SpawnerConfig m_spawnerConfig;
  467. bool m_readConfigSuccess = false;
  468. };
  469. TEST_F(LoadSpawnerComponentFromLegacyGameData, Fixture_SanityCheck)
  470. {
  471. EXPECT_NE(nullptr, GetApplication());
  472. }
  473. TEST_F(LoadSpawnerComponentFromLegacyGameData, SpawnerComponent_LoadsFromData)
  474. {
  475. EXPECT_NE(nullptr, m_object.get());
  476. }
  477. TEST_F(LoadSpawnerComponentFromLegacyGameData, ComponentId_ValuePreserved)
  478. {
  479. EXPECT_EQ(AZ::ComponentId(8317941343245109563ULL), m_object->GetId());
  480. }
  481. TEST_F(LoadSpawnerComponentFromLegacyGameData, SliceAsset_ValuePreserved)
  482. {
  483. EXPECT_EQ(AZ::Uuid("{6F11134F-84C9-559F-AABA-3D1778656707}"), m_spawnerConfig.m_sliceAsset.GetId().m_guid);
  484. }
  485. TEST_F(LoadSpawnerComponentFromLegacyGameData, SpawnOnActivate_ValuePreserved)
  486. {
  487. EXPECT_TRUE(m_spawnerConfig.m_spawnOnActivate);
  488. }
  489. TEST_F(LoadSpawnerComponentFromLegacyGameData, DestroyOnDeactivate_ValuePreserved)
  490. {
  491. EXPECT_TRUE(m_spawnerConfig.m_destroyOnDeactivate);
  492. }
  493. #ifdef LMBR_CENTRAL_EDITOR
  494. // Legacy SpawnerComponent from editor data (wrapped inside a GenericComponentWrapper)
  495. // Should get converted into EditorSpawnerComponent
  496. const char kWrappedLegacySpawnerComponent[] =
  497. R"DELIMITER(<ObjectStream version="3">
  498. <Class name="GenericComponentWrapper" field="element" type="{68D358CA-89B9-4730-8BA6-E181DEA28FDE}">
  499. <Class name="EditorComponentBase" field="BaseClass1" version="1" type="{D5346BD4-7F20-444E-B370-327ACD03D4A0}">
  500. <Class name="AZ::Component" field="BaseClass1" type="{EDFCB2CF-F75D-43BE-B26B-F35821B29247}">
  501. <Class name="AZ::u64" field="Id" value="6866719809809621109" type="{D6597933-47CD-4FC8-B911-63F3E2B0993A}"/>
  502. </Class>
  503. </Class>
  504. <Class name="SpawnerComponent" field="m_template" version="1" type="{8022A627-FA7D-4516-A155-657A0927A3CA}">
  505. <Class name="AZ::Component" field="BaseClass1" type="{EDFCB2CF-F75D-43BE-B26B-F35821B29247}">
  506. <Class name="AZ::u64" field="Id" value="0" type="{D6597933-47CD-4FC8-B911-63F3E2B0993A}"/>
  507. </Class>
  508. <Class name="Asset" field="Slice" value="id={3987FC80-0CF5-5A22-BE55-1EEDF382909E}:2,type={78802ABF-9595-463A-8D2B-D022F906F9B1},hint={slices/ai_walker.dynamicslice}" version="1" type="{77A19D40-8731-4D3C-9041-1B43047366A4}"/>
  509. <Class name="bool" field="SpawnOnActivate" value="true" type="{A0CA880C-AFE4-43CB-926C-59AC48496112}"/>
  510. <Class name="bool" field="DestroyOnDeactivate" value="true" type="{A0CA880C-AFE4-43CB-926C-59AC48496112}"/>
  511. </Class>
  512. </Class>
  513. </ObjectStream>)DELIMITER";
  514. class LoadSpawnerComponentFromLegacyEditorData
  515. : public LoadReflectedObjectTest<AzToolsFramework::ToolsApplication, LmbrCentral::LmbrCentralEditorModule, AzToolsFramework::Components::GenericComponentWrapper>
  516. {
  517. public:
  518. const char* GetSourceDataBuffer() const override { return kWrappedLegacySpawnerComponent; }
  519. void SetUp() override
  520. {
  521. LoadReflectedObjectTest::SetUp();
  522. // reset values from previous run
  523. m_editorSpawnerComponent = nullptr;
  524. m_readConfigSuccess = false;
  525. if (m_object)
  526. {
  527. m_editorSpawnerComponent = azrtti_cast<LmbrCentral::EditorSpawnerComponent*>(m_object->GetTemplate());
  528. if (m_editorSpawnerComponent)
  529. {
  530. m_readConfigSuccess = m_editorSpawnerComponent->GetConfiguration(m_spawnerConfig);
  531. }
  532. }
  533. }
  534. LmbrCentral::EditorSpawnerComponent* m_editorSpawnerComponent;
  535. LmbrCentral::SpawnerConfig m_spawnerConfig;
  536. bool m_readConfigSuccess;
  537. };
  538. TEST_F(LoadSpawnerComponentFromLegacyEditorData, Fixture_SanityCheck)
  539. {
  540. EXPECT_NE(nullptr, GetApplication());
  541. }
  542. TEST_F(LoadSpawnerComponentFromLegacyEditorData, ObjectStream_LoadsComponents)
  543. {
  544. EXPECT_NE(nullptr, m_object.get());
  545. }
  546. TEST_F(LoadSpawnerComponentFromLegacyEditorData, LegacySpawnerComponent_TurnedIntoEditorSpawnerComponent)
  547. {
  548. EXPECT_NE(nullptr, m_editorSpawnerComponent);
  549. }
  550. TEST_F(LoadSpawnerComponentFromLegacyEditorData, SpawnerConfig_SuccessfullyRead)
  551. {
  552. EXPECT_TRUE(m_readConfigSuccess);
  553. }
  554. TEST_F(LoadSpawnerComponentFromLegacyEditorData, SliceAsset_ValuePreserved)
  555. {
  556. EXPECT_EQ(AZ::Uuid("{3987FC80-0CF5-5A22-BE55-1EEDF382909E}"), m_spawnerConfig.m_sliceAsset.GetId().m_guid);
  557. }
  558. TEST_F(LoadSpawnerComponentFromLegacyEditorData, SpawnOnActivate_ValuePreserved)
  559. {
  560. EXPECT_TRUE(m_spawnerConfig.m_spawnOnActivate);
  561. }
  562. TEST_F(LoadSpawnerComponentFromLegacyEditorData, DestroyOnDeactivate_ValuePreserved)
  563. {
  564. EXPECT_TRUE(m_spawnerConfig.m_destroyOnDeactivate);
  565. }
  566. #endif // LMBR_CENTRAL_EDITOR