TerrainTestFixtures.h 5.9 KB

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