TerrainWorldComponentTests.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #include <Components/TerrainWorldComponent.h>
  9. #include <Components/TerrainSystemComponent.h>
  10. #include <AzTest/AzTest.h>
  11. #include <Tests/Mocks/Terrain/MockTerrainDataRequestBus.h>
  12. #include <Terrain/MockTerrain.h>
  13. #include <TerrainTestFixtures.h>
  14. class TerrainWorldComponentTest
  15. : public UnitTest::TerrainSystemTestFixture
  16. {
  17. protected:
  18. AZStd::unique_ptr<AZ::Entity> CreateAndActivateTerrainWorldComponent(const Terrain::TerrainWorldConfig& config)
  19. {
  20. auto entity = CreateEntity();
  21. entity->CreateComponent<Terrain::TerrainWorldComponent>(config);
  22. ActivateEntity(entity.get());
  23. // Run for one tick so that the terrain system has a chance to refresh all of its settings.
  24. AZ::TickBus::Broadcast(&AZ::TickBus::Events::OnTick, 0.f, AZ::ScriptTimePoint{});
  25. return entity;
  26. }
  27. };
  28. TEST_F(TerrainWorldComponentTest, ComponentActivatesSuccessfully)
  29. {
  30. auto entity = CreateAndActivateTerrainWorldComponent(Terrain::TerrainWorldConfig());
  31. EXPECT_EQ(entity->GetState(), AZ::Entity::State::Active);
  32. entity.reset();
  33. }
  34. TEST_F(TerrainWorldComponentTest, ComponentCreatesAndActivatesTerrainSystem)
  35. {
  36. // Verify that activation of the Terrain World component causes the Terrain System to get created/activated,
  37. // and deactivation of the Terrain World component causes the Terrain System to get destroyed/deactivated.
  38. using ::testing::NiceMock;
  39. using ::testing::AtLeast;
  40. NiceMock<UnitTest::MockTerrainDataNotificationListener> mockTerrainListener;
  41. EXPECT_CALL(mockTerrainListener, OnTerrainDataCreateBegin()).Times(AtLeast(1));
  42. EXPECT_CALL(mockTerrainListener, OnTerrainDataCreateEnd()).Times(AtLeast(1));
  43. EXPECT_CALL(mockTerrainListener, OnTerrainDataDestroyBegin()).Times(AtLeast(1));
  44. EXPECT_CALL(mockTerrainListener, OnTerrainDataDestroyEnd()).Times(AtLeast(1));
  45. auto entity = CreateAndActivateTerrainWorldComponent(Terrain::TerrainWorldConfig());
  46. entity.reset();
  47. }
  48. TEST_F(TerrainWorldComponentTest, WorldMinAndMaxAffectTerrainSystem)
  49. {
  50. // Verify that the Z component of the Terrain World Component's World Min and World Max set the Terrain System's min/max.
  51. // The z min/max should be returned with GetTerrainHeightBounds, and since there are no terrain areas, the aabb returned
  52. // from worldBounds should be invalid.
  53. Terrain::TerrainWorldConfig config;
  54. config.m_minHeight = -345.0f;
  55. config.m_maxHeight = 678.0f;
  56. auto entity = CreateAndActivateTerrainWorldComponent(config);
  57. AzFramework::Terrain::FloatRange heightBounds;
  58. AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(
  59. heightBounds, &AzFramework::Terrain::TerrainDataRequestBus::Events::GetTerrainHeightBounds);
  60. AZ::Aabb worldBounds = AZ::Aabb::CreateNull();
  61. AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(
  62. worldBounds, &AzFramework::Terrain::TerrainDataRequestBus::Events::GetTerrainAabb);
  63. EXPECT_NEAR(config.m_minHeight, heightBounds.m_min, 0.001f);
  64. EXPECT_NEAR(config.m_maxHeight, heightBounds.m_max, 0.001f);
  65. EXPECT_FALSE(worldBounds.IsValid());
  66. entity.reset();
  67. }
  68. TEST_F(TerrainWorldComponentTest, QueryResolutionsAffectTerrainSystem)
  69. {
  70. // Verify that the Height Query Resolution and Surface Data Query Resolution on the Terrain World Component set the query
  71. // resolutions in the Terrain System.
  72. Terrain::TerrainWorldConfig config;
  73. config.m_heightQueryResolution = 123.0f;
  74. config.m_surfaceDataQueryResolution = 456.0f;
  75. auto entity = CreateAndActivateTerrainWorldComponent(config);
  76. float heightQueryResolution = 0.0f;
  77. AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(
  78. heightQueryResolution, &AzFramework::Terrain::TerrainDataRequestBus::Events::GetTerrainHeightQueryResolution);
  79. float surfaceQueryResolution = 0.0f;
  80. AzFramework::Terrain::TerrainDataRequestBus::BroadcastResult(
  81. surfaceQueryResolution, &AzFramework::Terrain::TerrainDataRequestBus::Events::GetTerrainSurfaceDataQueryResolution);
  82. EXPECT_NEAR(config.m_heightQueryResolution, heightQueryResolution, 0.001f);
  83. EXPECT_NEAR(config.m_surfaceDataQueryResolution, surfaceQueryResolution, 0.001f);
  84. entity.reset();
  85. }