TerrainTestFixtures.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 <AzTest/GemTestEnvironment.h>
  10. #include <TerrainSystem/TerrainSystem.h>
  11. #include <Components/TerrainSurfaceGradientListComponent.h>
  12. #include <Atom/RPI.Public/RPISystem.h>
  13. #include <Atom/RPI.Public/Image/ImageSystem.h>
  14. #include <Common/RHI/Factory.h>
  15. #include <Common/RHI/Stubs.h>
  16. #include <AzCore/UnitTest/Mocks/MockFileIOBase.h>
  17. #include <Tests/FileIOBaseTestTypes.h>
  18. namespace UnitTest
  19. {
  20. // The Terrain unit tests need to use the GemTestEnvironment to load LmbrCentral, SurfaceData, and GradientSignal Gems so that these
  21. // systems can be used in the unit tests.
  22. class TerrainTestEnvironment
  23. : public AZ::Test::GemTestEnvironment
  24. {
  25. public:
  26. void AddGemsAndComponents() override;
  27. void PostCreateApplication() override;
  28. };
  29. #ifdef HAVE_BENCHMARK
  30. //! The Benchmark environment is used for one time setup and tear down of shared resources
  31. class TerrainBenchmarkEnvironment
  32. : public AZ::Test::BenchmarkEnvironmentBase
  33. , public TerrainTestEnvironment
  34. {
  35. protected:
  36. void SetUpBenchmark() override
  37. {
  38. SetupEnvironment();
  39. }
  40. void TearDownBenchmark() override
  41. {
  42. TeardownEnvironment();
  43. }
  44. };
  45. #endif
  46. // Base test fixture used for Terrain unit tests and benchmark tests
  47. class TerrainBaseFixture
  48. {
  49. public:
  50. void SetupCoreSystems();
  51. void TearDownCoreSystems();
  52. AZStd::unique_ptr<AZ::Entity> CreateEntity() const
  53. {
  54. return AZStd::make_unique<AZ::Entity>();
  55. }
  56. void ActivateEntity(AZ::Entity* entity) const
  57. {
  58. entity->Init();
  59. entity->Activate();
  60. }
  61. // Create an entity with a box shape and a transform.
  62. AZStd::unique_ptr<AZ::Entity> CreateTestBoxEntity(float boxHalfBounds) const;
  63. // Create an entity with a box shape and a transform.
  64. AZStd::unique_ptr<AZ::Entity> CreateTestBoxEntity(const AZ::Aabb& box) const;
  65. // Create an entity with a sphere shape and a transform.
  66. AZStd::unique_ptr<AZ::Entity> CreateTestSphereEntity(float shapeRadius) const;
  67. AZStd::unique_ptr<AZ::Entity> CreateTestSphereEntity(float shapeRadius, const AZ::Vector3& center) const;
  68. // Create and activate an entity with a gradient component of the requested type, initialized with test data.
  69. AZStd::unique_ptr<AZ::Entity> CreateAndActivateTestRandomGradient(const AZ::Aabb& spawnerBox, uint32_t randomSeed) const;
  70. AZStd::unique_ptr<AZ::Entity> CreateTestLayerSpawnerEntity(
  71. const AZ::Aabb& spawnerBox,
  72. const AZ::EntityId& heightGradientEntityId,
  73. const Terrain::TerrainSurfaceGradientListConfig& surfaceConfig) const;
  74. // Create a terrain system with reasonable defaults for testing, but with the ability to override the defaults
  75. // on a test-by-test basis.
  76. AZStd::unique_ptr<Terrain::TerrainSystem> CreateAndActivateTerrainSystem(
  77. float queryResolution = 1.0f,
  78. AzFramework::Terrain::FloatRange heightBounds = {-128.0f, 128.0f}) const;
  79. AZStd::unique_ptr<Terrain::TerrainSystem> CreateAndActivateTerrainSystem(
  80. float heightQueryResolution, float surfaceQueryResolution, const AzFramework::Terrain::FloatRange& heightBounds) const;
  81. void CreateTestTerrainSystem(const AZ::Aabb& worldBounds, float queryResolution, uint32_t numSurfaces);
  82. void CreateTestTerrainSystemWithSurfaceGradients(const AZ::Aabb& worldBounds, float queryResolution);
  83. void DestroyTestTerrainSystem();
  84. private:
  85. // State data for a full test terrain system setup.
  86. AZStd::vector<AZStd::unique_ptr<AZ::Entity>> m_heightGradientEntities;
  87. AZStd::vector<AZStd::unique_ptr<AZ::Entity>> m_surfaceGradientEntities;
  88. AZStd::unique_ptr<AZ::Entity> m_terrainLayerSpawnerEntity;
  89. AZStd::unique_ptr<Terrain::TerrainSystem> m_terrainSystem;
  90. };
  91. class TerrainTestFixture
  92. : public TerrainBaseFixture
  93. , public ::testing::Test
  94. {
  95. protected:
  96. void SetUp() override
  97. {
  98. SetupCoreSystems();
  99. }
  100. void TearDown() override
  101. {
  102. TearDownCoreSystems();
  103. }
  104. };
  105. // This test fixture initializes and destroys both the Atom RPI and the Terrain System Component as a part of setup and teardown.
  106. // It's useful for creating unit tests that use or test the terrain level components.
  107. class TerrainSystemTestFixture : public UnitTest::TerrainTestFixture
  108. {
  109. protected:
  110. TerrainSystemTestFixture();
  111. void SetUp() override;
  112. void TearDown() override;
  113. private:
  114. AZStd::unique_ptr<UnitTest::StubRHI::Factory> m_rhiFactory;
  115. AZStd::unique_ptr<AZ::RPI::RPISystem> m_rpiSystem;
  116. AZStd::unique_ptr<AZ::RPI::ImageSystem> m_imageSystem;
  117. UnitTest::SetRestoreFileIOBaseRAII m_restoreFileIO;
  118. ::testing::NiceMock<AZ::IO::MockFileIOBase> m_fileIOMock;
  119. AZStd::unique_ptr<AZ::Entity> m_systemEntity;
  120. };
  121. #ifdef HAVE_BENCHMARK
  122. class TerrainBenchmarkFixture
  123. : public TerrainBaseFixture
  124. , public ::benchmark::Fixture
  125. {
  126. public:
  127. void internalSetUp()
  128. {
  129. SetupCoreSystems();
  130. }
  131. void internalTearDown()
  132. {
  133. TearDownCoreSystems();
  134. }
  135. protected:
  136. void SetUp([[maybe_unused]] const benchmark::State& state) override
  137. {
  138. internalSetUp();
  139. }
  140. void SetUp([[maybe_unused]] benchmark::State& state) override
  141. {
  142. internalSetUp();
  143. }
  144. void TearDown([[maybe_unused]] const benchmark::State& state) override
  145. {
  146. internalTearDown();
  147. }
  148. void TearDown([[maybe_unused]] benchmark::State& state) override
  149. {
  150. internalTearDown();
  151. }
  152. };
  153. #endif
  154. }