EntityOwnershipServiceTestFixture.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "EntityOwnershipServiceTestFixture.h"
  9. #include <AzCore/UserSettings/UserSettingsComponent.h>
  10. #include <AzFramework/Components/AzFrameworkConfigurationSystemComponent.h>
  11. #include <AzToolsFramework/Prefab/PrefabSystemComponent.h>
  12. namespace UnitTest
  13. {
  14. void EntityOwnershipServiceTestFixture::SetUpEntityOwnershipServiceTest()
  15. {
  16. LeakDetectionFixture::SetUp();
  17. AZ::ComponentApplication::Descriptor componentApplicationDescriptor;
  18. componentApplicationDescriptor.m_useExistingAllocator = true;
  19. m_app = AZStd::make_unique<EntityOwnershipServiceApplication>();
  20. AZ::ComponentApplication::StartupParameters startupParameters;
  21. startupParameters.m_loadSettingsRegistry = false;
  22. m_app->Start(componentApplicationDescriptor, startupParameters);
  23. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  24. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  25. // in the unit tests.
  26. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  27. }
  28. void EntityOwnershipServiceTestFixture::TearDownEntityOwnershipServiceTest()
  29. {
  30. m_app.reset();
  31. LeakDetectionFixture::TearDown();
  32. }
  33. AZ::ComponentTypeList EntityOwnershipServiceTestFixture::EntityOwnershipServiceApplication::GetRequiredSystemComponents() const
  34. {
  35. AZ::ComponentTypeList defaultRequiredComponents = AzFramework::Application::GetRequiredSystemComponents();
  36. defaultRequiredComponents.emplace_back(azrtti_typeid<AzToolsFramework::Prefab::PrefabSystemComponent>());
  37. auto findComponentIterator = AZStd::find(defaultRequiredComponents.begin(), defaultRequiredComponents.end(),
  38. azrtti_typeid<AzFramework::GameEntityContextComponent>());
  39. if (findComponentIterator != defaultRequiredComponents.end())
  40. {
  41. defaultRequiredComponents.erase(findComponentIterator);
  42. }
  43. findComponentIterator = AZStd::find(defaultRequiredComponents.begin(), defaultRequiredComponents.end(),
  44. azrtti_typeid<AzFramework::AzFrameworkConfigurationSystemComponent>());
  45. if (findComponentIterator != defaultRequiredComponents.end())
  46. {
  47. defaultRequiredComponents.erase(findComponentIterator);
  48. }
  49. return defaultRequiredComponents;
  50. }
  51. AzFramework::RootSliceAsset EntityOwnershipServiceTestFixture::GetRootSliceAsset()
  52. {
  53. AzFramework::RootSliceAsset rootSliceAsset;
  54. AzFramework::SliceEntityOwnershipServiceRequestBus::BroadcastResult(rootSliceAsset,
  55. &AzFramework::SliceEntityOwnershipServiceRequestBus::Events::GetRootAsset);
  56. return rootSliceAsset;
  57. }
  58. void EntityOwnershipServiceTestFixture::HandleEntitiesAdded(const AzFramework::EntityList& entityList)
  59. {
  60. m_entitiesAddedCallbackTriggered = true;
  61. for (AZ::Entity* entity : entityList)
  62. {
  63. // If the entities are not initialized, they won't be removed from ComponentApplication during Entity destruction.
  64. if (entity->GetState() != AZ::Entity::State::Init)
  65. {
  66. entity->Init();
  67. }
  68. }
  69. }
  70. void EntityOwnershipServiceTestFixture::HandleEntitiesRemoved(const AzFramework::EntityIdList&)
  71. {
  72. m_entityRemovedCallbackTriggered = true;
  73. }
  74. bool EntityOwnershipServiceTestFixture::ValidateEntities(const AzFramework::EntityList&)
  75. {
  76. m_validateEntitiesCallbackTriggered = true;
  77. return m_areEntitiesValidForContext;
  78. }
  79. AzFramework::SliceInstantiationTicket EntityOwnershipServiceTestFixture::AddSlice(const EntityList& entityList,
  80. const bool isAsynchronous)
  81. {
  82. AZ::Data::Asset<AZ::SliceAsset> sliceAsset;
  83. sliceAsset.Create(AZ::Data::AssetId(AZ::Uuid::CreateRandom()), false);
  84. return AddSlice(entityList, isAsynchronous, sliceAsset);
  85. }
  86. AzFramework::SliceInstantiationTicket EntityOwnershipServiceTestFixture::AddSlice(const EntityList& entityList,
  87. const bool isAsynchronous, AZ::Data::Asset<AZ::SliceAsset>& sliceAsset)
  88. {
  89. AddSliceComponentToAsset(sliceAsset, entityList);
  90. AzFramework::SliceInstantiationTicket sliceInstantiationTicket;
  91. AzFramework::SliceEntityOwnershipServiceRequestBus::BroadcastResult(sliceInstantiationTicket,
  92. &AzFramework::SliceEntityOwnershipServiceRequestBus::Events::InstantiateSlice, sliceAsset, nullptr, nullptr);
  93. if (!isAsynchronous)
  94. {
  95. AZ::TickBus::ExecuteQueuedEvents();
  96. }
  97. return sliceInstantiationTicket;
  98. }
  99. void EntityOwnershipServiceTestFixture::AddSliceComponentToAsset(AZ::Data::Asset<AZ::SliceAsset>& sliceAsset,
  100. const EntityList& entityList)
  101. {
  102. AZ::Entity* sliceEntity = aznew AZ::Entity();
  103. AZ::SliceComponent* sliceComponent = sliceEntity->CreateComponent<AZ::SliceComponent>();
  104. sliceComponent->SetSerializeContext(m_app->GetSerializeContext());
  105. for (AZ::Entity* entity : entityList)
  106. {
  107. sliceComponent->AddEntity(entity);
  108. }
  109. sliceAsset->SetData(sliceEntity, sliceComponent);
  110. }
  111. }