123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <AzTest/AzTest.h>
- #include <AzCore/UnitTest/TestTypes.h>
- #include <AzCore/Component/ComponentApplication.h>
- #include <AzCore/Component/Entity.h>
- #include <AzCore/Asset/AssetManagerComponent.h>
- #include <AzCore/Asset/AssetManager.h>
- #include <AzCore/Jobs/JobManager.h>
- #include <AzCore/Jobs/JobContext.h>
- #include <AzCore/Memory/PoolAllocator.h>
- #include <AzCore/Memory/SystemAllocator.h>
- //////////////////////////////////////////////////////////////////////////
- #include <Vegetation/Ebuses/AreaSystemRequestBus.h>
- #include <VegetationModule.h>
- #include <AreaSystemComponent.h>
- namespace UnitTest
- {
- // This component meets all the dependencies required to get the Vegetation system activated:
- // - Provides SurfaceData services
- // - Starts / stops the Asset Manager
- // Note that this will always start before the vegetation components and end after them due
- // to the dependency-enforced ordering.
- class MockVegetationDependenciesComponent
- : public AZ::Component
- {
- public:
- AZ_COMPONENT(MockVegetationDependenciesComponent, "{C93FAEE8-E0C3-41E6-BBD1-89023C5ACB28}");
- static void Reflect(AZ::ReflectContext* context)
- {
- if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
- {
- serialize->Class<MockVegetationDependenciesComponent, AZ::Component>()->Version(0);
- }
- }
- static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
- {
- provided.push_back(AZ_CRC_CE("SurfaceDataSystemService"));
- provided.push_back(AZ_CRC_CE("SurfaceDataProviderService"));
- }
- static void GetIncompatibleServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& incompatible) {}
- static void GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required) {}
- static void GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent) {}
- protected:
- ////////////////////////////////////////////////////////////////////////
- // AZ::Component interface implementation
- void Init() override {}
- void Activate() override
- {
- // Initialize the job manager with 1 thread for the AssetManager to use.
- AZ::JobManagerDesc jobDesc;
- AZ::JobManagerThreadDesc threadDesc;
- jobDesc.m_workerThreads.push_back(threadDesc);
- m_jobManager = aznew AZ::JobManager(jobDesc);
- m_jobContext = aznew AZ::JobContext(*m_jobManager);
- AZ::JobContext::SetGlobalContext(m_jobContext);
- AZ::Data::AssetManager::Descriptor descriptor;
- AZ::Data::AssetManager::Create(descriptor);
- }
- void Deactivate() override
- {
- AZ::Data::AssetManager::Destroy();
- AZ::JobContext::SetGlobalContext(nullptr);
- delete m_jobContext;
- delete m_jobManager;
- }
- AZ::JobManager* m_jobManager{ nullptr };
- AZ::JobContext* m_jobContext{ nullptr };
- };
- // Create a mock module to load our mock component that meets all the vegetation system dependencies.
- class MockVegetationDependenciesModule
- : public AZ::Module
- {
- public:
- AZ_RTTI(MockVegetationDependenciesModule, "{3F7470AD-4FF9-48E6-8FFB-A5314F874F2B}", AZ::Module);
- AZ_CLASS_ALLOCATOR(MockVegetationDependenciesModule, AZ::SystemAllocator);
- MockVegetationDependenciesModule()
- {
- m_descriptors.insert(m_descriptors.end(), {
- MockVegetationDependenciesComponent::CreateDescriptor()
- });
- }
- AZ::ComponentTypeList GetRequiredSystemComponents() const override
- {
- return AZ::ComponentTypeList{
- azrtti_typeid<MockVegetationDependenciesComponent>()
- };
- }
- };
- // Test harness for the vegetation system that starts up / shuts down all the vegetation system components.
- class VegetationTestApp
- : public UnitTest::LeakDetectionFixture
- {
- public:
- VegetationTestApp()
- : m_application()
- , m_systemEntity(nullptr)
- {
- }
- void SetUp() override
- {
- AZ::ComponentApplication::Descriptor appDesc;
- appDesc.m_memoryBlocksByteSize = 50 * 1024 * 1024;
- appDesc.m_recordingMode = AZ::Debug::AllocationRecords::Mode::RECORD_FULL;
- AZ::ComponentApplication::StartupParameters appStartup;
- appStartup.m_createStaticModulesCallback =
- [](AZStd::vector<AZ::Module*>& modules)
- {
- modules.emplace_back(new MockVegetationDependenciesModule);
- modules.emplace_back(new Vegetation::VegetationModule);
- };
- m_systemEntity = m_application.Create(appDesc, appStartup);
- m_systemEntity->Init();
- m_systemEntity->Activate();
- }
- void TearDown() override
- {
- m_systemEntity->Deactivate();
- m_application.Destroy();
- }
- AZ::ComponentApplication m_application;
- AZ::Entity* m_systemEntity;
- };
- TEST_F(VegetationTestApp, Vegetation_AreaComponentTest_SuccessfulActivation)
- {
- // This test simply creates an environment that activates and deactivates the vegetation system components.
- // If it runs without asserting / crashing, then it is successful.
- }
- }
|