123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- /*
- * 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
- *
- */
- #pragma once
- #include <Vegetation/Ebuses/AreaRequestBus.h>
- #include <AzCore/Math/Random.h>
- #include <AzCore/Component/ComponentApplication.h>
- #include <AzCore/Asset/AssetManagerComponent.h>
- #include <AzCore/UnitTest/TestTypes.h>
- #include <AzCore/std/functional.h>
- /*
- * testing fixtures for vegetation testing
- */
- namespace UnitTest
- {
- class VegetationComponentTests
- : public LeakDetectionFixture
- {
- protected:
- AZ::ComponentApplication m_app;
- virtual void RegisterComponentDescriptors() {}
- void SetUp() override
- {
- if (AZ::Debug::AllocationRecords* records = AZ::AllocatorInstance<AZ::SystemAllocator>::Get().GetRecords();
- records != nullptr)
- {
- records->SetMode(AZ::Debug::AllocationRecords::Mode::RECORD_NO_RECORDS);
- }
- AZ::ComponentApplication::StartupParameters startupParameters;
- startupParameters.m_loadSettingsRegistry = false;
- m_app.Create({}, startupParameters);
- RegisterComponentDescriptors();
- }
- void TearDown() override
- {
- m_app.Destroy();
- }
- int m_createdCallbackCount = 0;
- int m_existedCallbackCount = 0;
- bool m_existedCallbackOutput = false;
- template <const AZStd::size_t CountX = 1, const AZStd::size_t CountY = 1>
- Vegetation::ClaimContext CreateContext(AZ::Vector3 startValue, float stepValue = 1.0f)
- {
- Vegetation::ClaimContext claimContext;
- for (auto x = 0; x < CountX; ++x)
- {
- for (auto y = 0; y < CountY; ++y)
- {
- AZ::Vector3 value = startValue + AZ::Vector3(x * stepValue, y * stepValue, 0.0f);
- size_t hash = 0;
- AZStd::hash_combine<float>(hash, value.GetX());
- AZStd::hash_combine<float>(hash, value.GetY());
- Vegetation::ClaimPoint pt;
- pt.m_position = value;
- pt.m_handle = hash;
- claimContext.m_availablePoints.push_back(pt);
- }
- }
- claimContext.m_createdCallback = [this](const Vegetation::ClaimPoint&, const Vegetation::InstanceData&)
- {
- ++m_createdCallbackCount;
- };
- claimContext.m_existedCallback = [this](const Vegetation::ClaimPoint&, const Vegetation::InstanceData&)
- {
- return m_existedCallbackOutput;
- };
- return claimContext;
- }
- template <typename Component>
- AZStd::unique_ptr<AZ::Entity> CreateEntity(Component** ppComponent)
- {
- m_app.RegisterComponentDescriptor(Component::CreateDescriptor());
- auto entity = AZStd::make_unique<AZ::Entity>();
- if (ppComponent)
- {
- *ppComponent = entity->CreateComponent<Component>();
- }
- else
- {
- entity->CreateComponent<Component>();
- }
- entity->Init();
- EXPECT_EQ(AZ::Entity::State::Init, entity->GetState());
- entity->Activate();
- EXPECT_EQ(AZ::Entity::State::Active, entity->GetState());
- return entity;
- }
- template <typename Component, typename Configuration>
- AZStd::unique_ptr<AZ::Entity> CreateEntity(const Configuration& config, Component** ppComponent)
- {
- m_app.RegisterComponentDescriptor(Component::CreateDescriptor());
- auto entity = AZStd::make_unique<AZ::Entity>();
- if (ppComponent)
- {
- *ppComponent = entity->CreateComponent<Component>(config);
- }
- else
- {
- entity->CreateComponent<Component>(config);
- }
- entity->Init();
- EXPECT_EQ(AZ::Entity::State::Init, entity->GetState());
- entity->Activate();
- EXPECT_EQ(AZ::Entity::State::Active, entity->GetState());
- return entity;
- }
- using CreateAdditionalComponents = AZStd::function<void(AZ::Entity*)>;
- template <typename Component, typename Configuration>
- AZStd::unique_ptr<AZ::Entity> CreateEntity(const Configuration& config, Component** ppComponent, CreateAdditionalComponents fnCreateAdditionalComponents)
- {
- m_app.RegisterComponentDescriptor(Component::CreateDescriptor());
- auto entity = AZStd::make_unique<AZ::Entity>();
- if (ppComponent)
- {
- *ppComponent = entity->CreateComponent<Component>(config);
- }
- else
- {
- entity->CreateComponent<Component>(config);
- }
- fnCreateAdditionalComponents(entity.get());
- entity->Init();
- EXPECT_EQ(AZ::Entity::State::Init, entity->GetState());
- entity->Activate();
- EXPECT_EQ(AZ::Entity::State::Active, entity->GetState());
- return entity;
- }
- void DestroyEntity(AZ::Entity* entity)
- {
- entity->Deactivate();
- delete entity;
- }
- };
- }
|