EntityContext.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/Math/Uuid.h>
  9. #include <AzCore/Component/ComponentApplication.h>
  10. #include <AzCore/Component/Entity.h>
  11. #include <AzCore/Component/TickBus.h>
  12. #include <AzCore/Slice/SliceComponent.h>
  13. #include <AzCore/Slice/SliceAssetHandler.h>
  14. #include <AzCore/Asset/AssetManager.h>
  15. #include <AzCore/Memory/PoolAllocator.h>
  16. #include <AzCore/UnitTest/TestTypes.h>
  17. #include <AzFramework/Entity/EntityContext.h>
  18. namespace UnitTest
  19. {
  20. using namespace AZ;
  21. using namespace AzFramework;
  22. class EntityContextBasicTest
  23. : public LeakDetectionFixture
  24. , public EntityContextEventBus::Handler
  25. {
  26. public:
  27. void SetUp() override
  28. {
  29. Data::AssetManager::Descriptor desc;
  30. Data::AssetManager::Create(desc);
  31. }
  32. void TearDown() override
  33. {
  34. Data::AssetManager::Destroy();
  35. }
  36. void run()
  37. {
  38. ComponentApplication app;
  39. ComponentApplication::Descriptor desc;
  40. desc.m_useExistingAllocator = true;
  41. AZ::ComponentApplication::StartupParameters startupParameters;
  42. startupParameters.m_loadSettingsRegistry = false;
  43. app.Create(desc, startupParameters);
  44. Data::AssetManager::Instance().RegisterHandler(aznew SliceAssetHandler(app.GetSerializeContext()), AZ::AzTypeInfo<AZ::SliceAsset>::Uuid());
  45. AZ::Uuid entityContextId = AZ::Uuid::CreateRandom();
  46. AZStd::unique_ptr<SliceEntityOwnershipService> m_entityOwnershipService =
  47. AZStd::make_unique<AzFramework::SliceEntityOwnershipService>(entityContextId, app.GetSerializeContext());
  48. EntityContext context(entityContextId, AZStd::move(m_entityOwnershipService), app.GetSerializeContext());
  49. context.InitContext();
  50. EntityContextEventBus::Handler::BusConnect(context.GetContextId());
  51. AZ::Entity* entity = context.CreateEntity("MyEntity");
  52. AZ_TEST_ASSERT(entity); // Should have created the entity.
  53. AZ_TEST_ASSERT(m_createEntityEvents == 1);
  54. AZ::Uuid contextId = AZ::Uuid::CreateNull();
  55. EntityIdContextQueryBus::EventResult(contextId, entity->GetId(), &EntityIdContextQueryBus::Events::GetOwningContextId);
  56. AZ_TEST_ASSERT(contextId == context.GetContextId()); // Context properly associated with entity?
  57. AZ_TEST_ASSERT(context.DestroyEntity(entity));
  58. AZ_TEST_ASSERT(m_destroyEntityEvents == 1);
  59. AZ::Entity* sliceEntity = aznew AZ::Entity();
  60. AZ::SliceComponent* sliceComponent = sliceEntity->CreateComponent<AZ::SliceComponent>();
  61. sliceComponent->SetSerializeContext(app.GetSerializeContext());
  62. sliceComponent->AddEntity(aznew AZ::Entity());
  63. Data::Asset<SliceAsset> sliceAssetHolder = Data::AssetManager::Instance().CreateAsset<SliceAsset>(Data::AssetId(Uuid::CreateRandom()));
  64. SliceAsset* sliceAsset = sliceAssetHolder.Get();
  65. sliceAsset->SetData(sliceEntity, sliceComponent);
  66. EntityContextEventBus::Handler::BusDisconnect(context.GetContextId());
  67. app.Destroy();
  68. }
  69. void OnEntityContextCreateEntity(AZ::Entity& entity) override
  70. {
  71. (void)entity;
  72. ++m_createEntityEvents;
  73. }
  74. void OnEntityContextDestroyEntity(const AZ::EntityId& entity) override
  75. {
  76. (void)entity;
  77. ++m_destroyEntityEvents;
  78. }
  79. size_t m_createEntityEvents = 0;
  80. size_t m_destroyEntityEvents = 0;
  81. };
  82. TEST_F(EntityContextBasicTest, Test)
  83. {
  84. run();
  85. }
  86. }