/* * 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 #include #include #include #include #include #include /* * testing fixtures for vegetation testing */ namespace UnitTest { class VegetationComponentTests : public ScopedAllocatorSetupFixture { protected: VegetationComponentTests() : ScopedAllocatorSetupFixture( []() { AZ::SystemAllocator::Descriptor desc; desc.m_heap.m_fixedMemoryBlocksByteSize[0] = 20 * 1024 * 1024; desc.m_stackRecordLevels = 20; return desc; }() ) { } AZ::ComponentApplication m_app; virtual void RegisterComponentDescriptors() {} void SetUp() override { if (AZ::Debug::AllocationRecords* records = AZ::AllocatorInstance::GetAllocator().GetRecords(); records != nullptr) { records->SetMode(AZ::Debug::AllocationRecords::RECORD_NO_RECORDS); } m_app.Create({}); RegisterComponentDescriptors(); } void TearDown() override { m_app.Destroy(); } int m_createdCallbackCount = 0; int m_existedCallbackCount = 0; bool m_existedCallbackOutput = false; template 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(hash, value.GetX()); AZStd::hash_combine(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 AZStd::unique_ptr CreateEntity(Component** ppComponent) { m_app.RegisterComponentDescriptor(Component::CreateDescriptor()); auto entity = AZStd::make_unique(); if (ppComponent) { *ppComponent = entity->CreateComponent(); } else { entity->CreateComponent(); } entity->Init(); EXPECT_EQ(AZ::Entity::State::Init, entity->GetState()); entity->Activate(); EXPECT_EQ(AZ::Entity::State::Active, entity->GetState()); return entity; } template AZStd::unique_ptr CreateEntity(const Configuration& config, Component** ppComponent) { m_app.RegisterComponentDescriptor(Component::CreateDescriptor()); auto entity = AZStd::make_unique(); if (ppComponent) { *ppComponent = entity->CreateComponent(config); } else { entity->CreateComponent(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; template AZStd::unique_ptr CreateEntity(const Configuration& config, Component** ppComponent, CreateAdditionalComponents fnCreateAdditionalComponents) { m_app.RegisterComponentDescriptor(Component::CreateDescriptor()); auto entity = AZStd::make_unique(); if (ppComponent) { *ppComponent = entity->CreateComponent(config); } else { entity->CreateComponent(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; } }; }