VegetationTest.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #pragma once
  9. #include <Vegetation/Ebuses/AreaRequestBus.h>
  10. #include <AzCore/Math/Random.h>
  11. #include <AzCore/Component/ComponentApplication.h>
  12. #include <AzCore/Asset/AssetManagerComponent.h>
  13. #include <AzCore/UnitTest/TestTypes.h>
  14. #include <AzCore/std/functional.h>
  15. /*
  16. * testing fixtures for vegetation testing
  17. */
  18. namespace UnitTest
  19. {
  20. class VegetationComponentTests
  21. : public LeakDetectionFixture
  22. {
  23. protected:
  24. AZ::ComponentApplication m_app;
  25. virtual void RegisterComponentDescriptors() {}
  26. void SetUp() override
  27. {
  28. if (AZ::Debug::AllocationRecords* records = AZ::AllocatorInstance<AZ::SystemAllocator>::Get().GetRecords();
  29. records != nullptr)
  30. {
  31. records->SetMode(AZ::Debug::AllocationRecords::Mode::RECORD_NO_RECORDS);
  32. }
  33. AZ::ComponentApplication::StartupParameters startupParameters;
  34. startupParameters.m_loadSettingsRegistry = false;
  35. m_app.Create({}, startupParameters);
  36. RegisterComponentDescriptors();
  37. }
  38. void TearDown() override
  39. {
  40. m_app.Destroy();
  41. }
  42. int m_createdCallbackCount = 0;
  43. int m_existedCallbackCount = 0;
  44. bool m_existedCallbackOutput = false;
  45. template <const AZStd::size_t CountX = 1, const AZStd::size_t CountY = 1>
  46. Vegetation::ClaimContext CreateContext(AZ::Vector3 startValue, float stepValue = 1.0f)
  47. {
  48. Vegetation::ClaimContext claimContext;
  49. for (auto x = 0; x < CountX; ++x)
  50. {
  51. for (auto y = 0; y < CountY; ++y)
  52. {
  53. AZ::Vector3 value = startValue + AZ::Vector3(x * stepValue, y * stepValue, 0.0f);
  54. size_t hash = 0;
  55. AZStd::hash_combine<float>(hash, value.GetX());
  56. AZStd::hash_combine<float>(hash, value.GetY());
  57. Vegetation::ClaimPoint pt;
  58. pt.m_position = value;
  59. pt.m_handle = hash;
  60. claimContext.m_availablePoints.push_back(pt);
  61. }
  62. }
  63. claimContext.m_createdCallback = [this](const Vegetation::ClaimPoint&, const Vegetation::InstanceData&)
  64. {
  65. ++m_createdCallbackCount;
  66. };
  67. claimContext.m_existedCallback = [this](const Vegetation::ClaimPoint&, const Vegetation::InstanceData&)
  68. {
  69. return m_existedCallbackOutput;
  70. };
  71. return claimContext;
  72. }
  73. template <typename Component>
  74. AZStd::unique_ptr<AZ::Entity> CreateEntity(Component** ppComponent)
  75. {
  76. m_app.RegisterComponentDescriptor(Component::CreateDescriptor());
  77. auto entity = AZStd::make_unique<AZ::Entity>();
  78. if (ppComponent)
  79. {
  80. *ppComponent = entity->CreateComponent<Component>();
  81. }
  82. else
  83. {
  84. entity->CreateComponent<Component>();
  85. }
  86. entity->Init();
  87. EXPECT_EQ(AZ::Entity::State::Init, entity->GetState());
  88. entity->Activate();
  89. EXPECT_EQ(AZ::Entity::State::Active, entity->GetState());
  90. return entity;
  91. }
  92. template <typename Component, typename Configuration>
  93. AZStd::unique_ptr<AZ::Entity> CreateEntity(const Configuration& config, Component** ppComponent)
  94. {
  95. m_app.RegisterComponentDescriptor(Component::CreateDescriptor());
  96. auto entity = AZStd::make_unique<AZ::Entity>();
  97. if (ppComponent)
  98. {
  99. *ppComponent = entity->CreateComponent<Component>(config);
  100. }
  101. else
  102. {
  103. entity->CreateComponent<Component>(config);
  104. }
  105. entity->Init();
  106. EXPECT_EQ(AZ::Entity::State::Init, entity->GetState());
  107. entity->Activate();
  108. EXPECT_EQ(AZ::Entity::State::Active, entity->GetState());
  109. return entity;
  110. }
  111. using CreateAdditionalComponents = AZStd::function<void(AZ::Entity*)>;
  112. template <typename Component, typename Configuration>
  113. AZStd::unique_ptr<AZ::Entity> CreateEntity(const Configuration& config, Component** ppComponent, CreateAdditionalComponents fnCreateAdditionalComponents)
  114. {
  115. m_app.RegisterComponentDescriptor(Component::CreateDescriptor());
  116. auto entity = AZStd::make_unique<AZ::Entity>();
  117. if (ppComponent)
  118. {
  119. *ppComponent = entity->CreateComponent<Component>(config);
  120. }
  121. else
  122. {
  123. entity->CreateComponent<Component>(config);
  124. }
  125. fnCreateAdditionalComponents(entity.get());
  126. entity->Init();
  127. EXPECT_EQ(AZ::Entity::State::Init, entity->GetState());
  128. entity->Activate();
  129. EXPECT_EQ(AZ::Entity::State::Active, entity->GetState());
  130. return entity;
  131. }
  132. void DestroyEntity(AZ::Entity* entity)
  133. {
  134. entity->Deactivate();
  135. delete entity;
  136. }
  137. };
  138. }