VegetationAreaSystemComponentTest.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <AzTest/AzTest.h>
  9. #include <AzCore/UnitTest/TestTypes.h>
  10. #include <AzCore/Component/ComponentApplication.h>
  11. #include <AzCore/Component/Entity.h>
  12. #include <AzCore/Asset/AssetManagerComponent.h>
  13. #include <AzCore/Asset/AssetManager.h>
  14. #include <AzCore/Jobs/JobManager.h>
  15. #include <AzCore/Jobs/JobContext.h>
  16. #include <AzCore/Memory/PoolAllocator.h>
  17. #include <AzCore/Memory/SystemAllocator.h>
  18. //////////////////////////////////////////////////////////////////////////
  19. #include <Vegetation/Ebuses/AreaSystemRequestBus.h>
  20. #include <VegetationModule.h>
  21. #include <AreaSystemComponent.h>
  22. namespace UnitTest
  23. {
  24. // This component meets all the dependencies required to get the Vegetation system activated:
  25. // - Provides SurfaceData services
  26. // - Starts / stops the Asset Manager
  27. // Note that this will always start before the vegetation components and end after them due
  28. // to the dependency-enforced ordering.
  29. class MockVegetationDependenciesComponent
  30. : public AZ::Component
  31. {
  32. public:
  33. AZ_COMPONENT(MockVegetationDependenciesComponent, "{C93FAEE8-E0C3-41E6-BBD1-89023C5ACB28}");
  34. static void Reflect(AZ::ReflectContext* context)
  35. {
  36. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  37. {
  38. serialize->Class<MockVegetationDependenciesComponent, AZ::Component>()->Version(0);
  39. }
  40. }
  41. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  42. {
  43. provided.push_back(AZ_CRC_CE("SurfaceDataSystemService"));
  44. provided.push_back(AZ_CRC_CE("SurfaceDataProviderService"));
  45. }
  46. static void GetIncompatibleServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& incompatible) {}
  47. static void GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required) {}
  48. static void GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent) {}
  49. protected:
  50. ////////////////////////////////////////////////////////////////////////
  51. // AZ::Component interface implementation
  52. void Init() override {}
  53. void Activate() override
  54. {
  55. // Initialize the job manager with 1 thread for the AssetManager to use.
  56. AZ::JobManagerDesc jobDesc;
  57. AZ::JobManagerThreadDesc threadDesc;
  58. jobDesc.m_workerThreads.push_back(threadDesc);
  59. m_jobManager = aznew AZ::JobManager(jobDesc);
  60. m_jobContext = aznew AZ::JobContext(*m_jobManager);
  61. AZ::JobContext::SetGlobalContext(m_jobContext);
  62. AZ::Data::AssetManager::Descriptor descriptor;
  63. AZ::Data::AssetManager::Create(descriptor);
  64. }
  65. void Deactivate() override
  66. {
  67. AZ::Data::AssetManager::Destroy();
  68. AZ::JobContext::SetGlobalContext(nullptr);
  69. delete m_jobContext;
  70. delete m_jobManager;
  71. }
  72. AZ::JobManager* m_jobManager{ nullptr };
  73. AZ::JobContext* m_jobContext{ nullptr };
  74. };
  75. // Create a mock module to load our mock component that meets all the vegetation system dependencies.
  76. class MockVegetationDependenciesModule
  77. : public AZ::Module
  78. {
  79. public:
  80. AZ_RTTI(MockVegetationDependenciesModule, "{3F7470AD-4FF9-48E6-8FFB-A5314F874F2B}", AZ::Module);
  81. AZ_CLASS_ALLOCATOR(MockVegetationDependenciesModule, AZ::SystemAllocator);
  82. MockVegetationDependenciesModule()
  83. {
  84. m_descriptors.insert(m_descriptors.end(), {
  85. MockVegetationDependenciesComponent::CreateDescriptor()
  86. });
  87. }
  88. AZ::ComponentTypeList GetRequiredSystemComponents() const override
  89. {
  90. return AZ::ComponentTypeList{
  91. azrtti_typeid<MockVegetationDependenciesComponent>()
  92. };
  93. }
  94. };
  95. // Test harness for the vegetation system that starts up / shuts down all the vegetation system components.
  96. class VegetationTestApp
  97. : public UnitTest::LeakDetectionFixture
  98. {
  99. public:
  100. VegetationTestApp()
  101. : m_application()
  102. , m_systemEntity(nullptr)
  103. {
  104. }
  105. void SetUp() override
  106. {
  107. AZ::ComponentApplication::Descriptor appDesc;
  108. appDesc.m_memoryBlocksByteSize = 50 * 1024 * 1024;
  109. appDesc.m_recordingMode = AZ::Debug::AllocationRecords::Mode::RECORD_FULL;
  110. AZ::ComponentApplication::StartupParameters appStartup;
  111. appStartup.m_createStaticModulesCallback =
  112. [](AZStd::vector<AZ::Module*>& modules)
  113. {
  114. modules.emplace_back(new MockVegetationDependenciesModule);
  115. modules.emplace_back(new Vegetation::VegetationModule);
  116. };
  117. m_systemEntity = m_application.Create(appDesc, appStartup);
  118. m_systemEntity->Init();
  119. m_systemEntity->Activate();
  120. }
  121. void TearDown() override
  122. {
  123. m_systemEntity->Deactivate();
  124. m_application.Destroy();
  125. }
  126. AZ::ComponentApplication m_application;
  127. AZ::Entity* m_systemEntity;
  128. };
  129. TEST_F(VegetationTestApp, Vegetation_AreaComponentTest_SuccessfulActivation)
  130. {
  131. // This test simply creates an environment that activates and deactivates the vegetation system components.
  132. // If it runs without asserting / crashing, then it is successful.
  133. }
  134. }