From 4b5f4042f201c35c94f1a60c82975342670972d9 Mon Sep 17 00:00:00 2001 From: amzn-sj Date: Wed, 19 Jan 2022 16:05:51 -0800 Subject: [PATCH] Move common code used by multiple tests into functions to reduce code duplication. Signed-off-by: amzn-sj --- .../Tests/TerrainPhysicsColliderTests.cpp | 117 ++++++-------- Gems/Terrain/Code/Tests/TerrainSystemTest.cpp | 149 ++++++++---------- 2 files changed, 111 insertions(+), 155 deletions(-) diff --git a/Gems/Terrain/Code/Tests/TerrainPhysicsColliderTests.cpp b/Gems/Terrain/Code/Tests/TerrainPhysicsColliderTests.cpp index 859e618983..2d0933c367 100644 --- a/Gems/Terrain/Code/Tests/TerrainPhysicsColliderTests.cpp +++ b/Gems/Terrain/Code/Tests/TerrainPhysicsColliderTests.cpp @@ -69,6 +69,46 @@ protected: m_colliderComponent = m_entity->CreateComponent(Terrain::TerrainPhysicsColliderConfig()); m_app.RegisterComponentDescriptor(m_colliderComponent->CreateDescriptor()); } + + void ProcessRegionLoop(const AZ::Aabb& inRegion, const AZ::Vector2& stepSize, + AzFramework::Terrain::SurfacePointRegionFillCallback perPositionCallback, + AzFramework::Terrain::TerrainDataRequests::Sampler sampleFilter, + AzFramework::SurfaceData::SurfaceTagWeightList* surfaceTags, + float mockHeight) + { + if (!perPositionCallback) + { + return; + } + + const size_t numSamplesX = aznumeric_cast(ceil(inRegion.GetExtents().GetX() / stepSize.GetX())); + const size_t numSamplesY = aznumeric_cast(ceil(inRegion.GetExtents().GetY() / stepSize.GetY())); + + AzFramework::SurfaceData::SurfacePoint surfacePoint; + for (size_t y = 0; y < numSamplesY; y++) + { + float fy = aznumeric_cast(inRegion.GetMin().GetY() + (y * stepSize.GetY())); + for (size_t x = 0; x < numSamplesX; x++) + { + bool terrainExists = false; + float fx = aznumeric_cast(inRegion.GetMin().GetX() + (x * stepSize.GetX())); + surfacePoint.m_position.Set(fx, fy, mockHeight); + if (surfaceTags) + { + surfacePoint.m_surfaceTags.clear(); + if (fy < 128.0) + { + surfacePoint.m_surfaceTags.push_back(surfaceTags->at(0)); + } + else + { + surfacePoint.m_surfaceTags.push_back(surfaceTags->at(1)); + } + } + perPositionCallback(x, y, surfacePoint, terrainExists); + } + } + } }; TEST_F(TerrainPhysicsColliderComponentTest, ActivateEntityActivateSuccess) @@ -239,30 +279,11 @@ TEST_F(TerrainPhysicsColliderComponentTest, TerrainPhysicsColliderGetHeightsRetu NiceMock terrainListener; ON_CALL(terrainListener, GetTerrainHeightQueryResolution).WillByDefault(Return(mockHeightResolution)); ON_CALL(terrainListener, ProcessHeightsFromRegion).WillByDefault( - [](const AZ::Aabb& inRegion, const AZ::Vector2& stepSize, + [this](const AZ::Aabb& inRegion, const AZ::Vector2& stepSize, AzFramework::Terrain::SurfacePointRegionFillCallback perPositionCallback, AzFramework::Terrain::TerrainDataRequests::Sampler sampleFilter) { - if (!perPositionCallback) - { - return; - } - - const size_t numSamplesX = aznumeric_cast(ceil(inRegion.GetExtents().GetX() / stepSize.GetX())); - const size_t numSamplesY = aznumeric_cast(ceil(inRegion.GetExtents().GetY() / stepSize.GetY())); - - AzFramework::SurfaceData::SurfacePoint surfacePoint; - for (size_t y = 0; y < numSamplesY; y++) - { - float fy = aznumeric_cast(inRegion.GetMin().GetY() + (y * stepSize.GetY())); - for (size_t x = 0; x < numSamplesX; x++) - { - bool terrainExists = false; - float fx = aznumeric_cast(inRegion.GetMin().GetX() + (x * stepSize.GetX())); - surfacePoint.m_position.Set(fx, fy, 0.0f); - perPositionCallback(x, y, surfacePoint, terrainExists); - } - } + ProcessRegionLoop(inRegion, stepSize, perPositionCallback, sampleFilter, nullptr, 0.0f); } ); @@ -300,30 +321,11 @@ TEST_F(TerrainPhysicsColliderComponentTest, TerrainPhysicsColliderReturnsRelativ NiceMock terrainListener; ON_CALL(terrainListener, GetTerrainHeightQueryResolution).WillByDefault(Return(mockHeightResolution)); ON_CALL(terrainListener, ProcessHeightsFromRegion).WillByDefault( - [mockHeight](const AZ::Aabb& inRegion, const AZ::Vector2& stepSize, + [this, mockHeight](const AZ::Aabb& inRegion, const AZ::Vector2& stepSize, AzFramework::Terrain::SurfacePointRegionFillCallback perPositionCallback, AzFramework::Terrain::TerrainDataRequests::Sampler sampleFilter) { - if (!perPositionCallback) - { - return; - } - - const size_t numSamplesX = aznumeric_cast(ceil(inRegion.GetExtents().GetX() / stepSize.GetX())); - const size_t numSamplesY = aznumeric_cast(ceil(inRegion.GetExtents().GetY() / stepSize.GetY())); - - AzFramework::SurfaceData::SurfacePoint surfacePoint; - for (size_t y = 0; y < numSamplesY; y++) - { - float fy = aznumeric_cast(inRegion.GetMin().GetY() + (y * stepSize.GetY())); - for (size_t x = 0; x < numSamplesX; x++) - { - bool terrainExists = false; - float fx = aznumeric_cast(inRegion.GetMin().GetX() + (x * stepSize.GetX())); - surfacePoint.m_position.Set(fx, fy, mockHeight); - perPositionCallback(x, y, surfacePoint, terrainExists); - } - } + ProcessRegionLoop(inRegion, stepSize, perPositionCallback, sampleFilter, nullptr, mockHeight); } ); @@ -467,39 +469,16 @@ TEST_F(TerrainPhysicsColliderComponentTest, TerrainPhysicsColliderGetHeightsAndM return2.m_surfaceType = tag2; return2.m_weight = 1.0f; + AzFramework::SurfaceData::SurfaceTagWeightList surfaceTags = { return1, return2 }; + NiceMock terrainListener; ON_CALL(terrainListener, GetTerrainHeightQueryResolution).WillByDefault(Return(mockHeightResolution)); ON_CALL(terrainListener, ProcessSurfacePointsFromRegion).WillByDefault( - [mockHeight, return1, return2](const AZ::Aabb& inRegion, const AZ::Vector2& stepSize, + [this, mockHeight, &surfaceTags](const AZ::Aabb& inRegion, const AZ::Vector2& stepSize, AzFramework::Terrain::SurfacePointRegionFillCallback perPositionCallback, AzFramework::Terrain::TerrainDataRequests::Sampler sampleFilter) { - if (!perPositionCallback) - { - return; - } - - const size_t numSamplesX = aznumeric_cast(ceil(inRegion.GetExtents().GetX() / stepSize.GetX())); - const size_t numSamplesY = aznumeric_cast(ceil(inRegion.GetExtents().GetY() / stepSize.GetY())); - - AzFramework::SurfaceData::SurfacePoint surfacePoint; - for (size_t y = 0; y < numSamplesY; y++) - { - float fy = aznumeric_cast(inRegion.GetMin().GetY() + (y * stepSize.GetY())); - for (size_t x = 0; x < numSamplesX; x++) - { - surfacePoint.m_surfaceTags.clear(); - bool terrainExists = false; - float fx = aznumeric_cast(inRegion.GetMin().GetX() + (x * stepSize.GetX())); - surfacePoint.m_position.Set(fx, fy, mockHeight); - if (fy < 128.0) - { - surfacePoint.m_surfaceTags.push_back(return1); - } - surfacePoint.m_surfaceTags.push_back(return2); - perPositionCallback(x, y, surfacePoint, terrainExists); - } - } + ProcessRegionLoop(inRegion, stepSize, perPositionCallback, sampleFilter, &surfaceTags, mockHeight); } ); diff --git a/Gems/Terrain/Code/Tests/TerrainSystemTest.cpp b/Gems/Terrain/Code/Tests/TerrainSystemTest.cpp index 2eebc1b824..ab0847e634 100644 --- a/Gems/Terrain/Code/Tests/TerrainSystemTest.cpp +++ b/Gems/Terrain/Code/Tests/TerrainSystemTest.cpp @@ -68,6 +68,7 @@ namespace UnitTest AZStd::unique_ptr> m_boxShapeRequests; AZStd::unique_ptr> m_shapeRequests; AZStd::unique_ptr> m_terrainAreaHeightRequests; + AZStd::unique_ptr> m_terrainAreaSurfaceRequests; void SetUp() override { @@ -84,6 +85,7 @@ namespace UnitTest m_boxShapeRequests.reset(); m_shapeRequests.reset(); m_terrainAreaHeightRequests.reset(); + m_terrainAreaSurfaceRequests.reset(); m_app.Destroy(); } @@ -160,6 +162,49 @@ namespace UnitTest ActivateEntity(entity.get()); return entity; } + + void SetupSurfaceWeightMocks(AZ::Entity* entity, AzFramework::SurfaceData::SurfaceTagWeightList& expectedTags) + { + const SurfaceData::SurfaceTag tag1 = SurfaceData::SurfaceTag("tag1"); + const SurfaceData::SurfaceTag tag2 = SurfaceData::SurfaceTag("tag2"); + const SurfaceData::SurfaceTag tag3 = SurfaceData::SurfaceTag("tag3"); + + AzFramework::SurfaceData::SurfaceTagWeight tagWeight1; + tagWeight1.m_surfaceType = tag1; + tagWeight1.m_weight = 1.0f; + expectedTags.push_back(tagWeight1); + + AzFramework::SurfaceData::SurfaceTagWeight tagWeight2; + tagWeight2.m_surfaceType = tag2; + tagWeight2.m_weight = 0.7f; + expectedTags.push_back(tagWeight2); + + AzFramework::SurfaceData::SurfaceTagWeight tagWeight3; + tagWeight3.m_surfaceType = tag3; + tagWeight3.m_weight = 0.3f; + expectedTags.push_back(tagWeight3); + + m_terrainAreaSurfaceRequests = AZStd::make_unique>(entity->GetId()); + ON_CALL(*m_terrainAreaSurfaceRequests, GetSurfaceWeights).WillByDefault( + [tagWeight1, tagWeight2, tagWeight3](const AZ::Vector3& position, AzFramework::SurfaceData::SurfaceTagWeightList& surfaceWeights) + { + surfaceWeights.clear(); + float absYPos = fabsf(position.GetY()); + if (absYPos < 1.0f) + { + surfaceWeights.push_back(tagWeight1); + } + else if(absYPos < 2.0f) + { + surfaceWeights.push_back(tagWeight2); + } + else + { + surfaceWeights.push_back(tagWeight3); + } + } + ); + } }; TEST_F(TerrainSystemTest, TrivialCreateDestroy) @@ -921,62 +966,28 @@ namespace UnitTest const AZ::Aabb testRegionBox = AZ::Aabb::CreateFromMinMaxValues(-3.0f, -3.0f, -1.0f, 3.0f, 3.0f, 1.0f); const AZ::Vector2 stepSize(1.0f); - const SurfaceData::SurfaceTag tag1 = SurfaceData::SurfaceTag("tag1"); - const SurfaceData::SurfaceTag tag2 = SurfaceData::SurfaceTag("tag2"); - const SurfaceData::SurfaceTag tag3 = SurfaceData::SurfaceTag("tag3"); + AzFramework::SurfaceData::SurfaceTagWeightList expectedTags; + SetupSurfaceWeightMocks(entity.get(), expectedTags); - AzFramework::SurfaceData::SurfaceTagWeight tagWeight1; - tagWeight1.m_surfaceType = tag1; - tagWeight1.m_weight = 1.0f; - - AzFramework::SurfaceData::SurfaceTagWeight tagWeight2; - tagWeight2.m_surfaceType = tag2; - tagWeight2.m_weight = 0.7f; - - AzFramework::SurfaceData::SurfaceTagWeight tagWeight3; - tagWeight3.m_surfaceType = tag3; - tagWeight3.m_weight = 0.3f; - - NiceMock mockSurfaceRequests(entity->GetId()); - ON_CALL(mockSurfaceRequests, GetSurfaceWeights).WillByDefault( - [&tagWeight1, &tagWeight2, &tagWeight3](const AZ::Vector3& position, AzFramework::SurfaceData::SurfaceTagWeightList& surfaceWeights) - { - surfaceWeights.clear(); - float absYPos = fabsf(position.GetY()); - if (absYPos < 1.0f) - { - surfaceWeights.push_back(tagWeight1); - } - else if(absYPos < 2.0f) - { - surfaceWeights.push_back(tagWeight2); - } - else - { - surfaceWeights.push_back(tagWeight3); - } - } - ); - - auto perPositionCallback = [&tagWeight1, &tagWeight2, &tagWeight3](size_t xIndex, size_t yIndex, + auto perPositionCallback = [&expectedTags](size_t xIndex, size_t yIndex, const AzFramework::SurfaceData::SurfacePoint& surfacePoint, [[maybe_unused]] bool terrainExists) { constexpr float epsilon = 0.0001f; float absYPos = fabsf(surfacePoint.m_position.GetY()); if (absYPos < 1.0f) { - EXPECT_EQ(surfacePoint.m_surfaceTags[0].m_surfaceType, tagWeight1.m_surfaceType); - EXPECT_NEAR(surfacePoint.m_surfaceTags[0].m_weight, tagWeight1.m_weight, epsilon); + EXPECT_EQ(surfacePoint.m_surfaceTags[0].m_surfaceType, expectedTags[0].m_surfaceType); + EXPECT_NEAR(surfacePoint.m_surfaceTags[0].m_weight, expectedTags[0].m_weight, epsilon); } else if(absYPos < 2.0f) { - EXPECT_EQ(surfacePoint.m_surfaceTags[0].m_surfaceType, tagWeight2.m_surfaceType); - EXPECT_NEAR(surfacePoint.m_surfaceTags[0].m_weight, tagWeight2.m_weight, epsilon); + EXPECT_EQ(surfacePoint.m_surfaceTags[0].m_surfaceType, expectedTags[1].m_surfaceType); + EXPECT_NEAR(surfacePoint.m_surfaceTags[0].m_weight, expectedTags[1].m_weight, epsilon); } else { - EXPECT_EQ(surfacePoint.m_surfaceTags[0].m_surfaceType, tagWeight3.m_surfaceType); - EXPECT_NEAR(surfacePoint.m_surfaceTags[0].m_weight, tagWeight3.m_weight, epsilon); + EXPECT_EQ(surfacePoint.m_surfaceTags[0].m_surfaceType, expectedTags[2].m_surfaceType); + EXPECT_NEAR(surfacePoint.m_surfaceTags[0].m_weight, expectedTags[2].m_weight, epsilon); } }; @@ -1001,44 +1012,10 @@ namespace UnitTest const AZ::Aabb testRegionBox = AZ::Aabb::CreateFromMinMaxValues(-3.0f, -3.0f, -1.0f, 3.0f, 3.0f, 1.0f); const AZ::Vector2 stepSize(1.0f); - const SurfaceData::SurfaceTag tag1 = SurfaceData::SurfaceTag("tag1"); - const SurfaceData::SurfaceTag tag2 = SurfaceData::SurfaceTag("tag2"); - const SurfaceData::SurfaceTag tag3 = SurfaceData::SurfaceTag("tag3"); + AzFramework::SurfaceData::SurfaceTagWeightList expectedTags; + SetupSurfaceWeightMocks(entity.get(), expectedTags); - AzFramework::SurfaceData::SurfaceTagWeight tagWeight1; - tagWeight1.m_surfaceType = tag1; - tagWeight1.m_weight = 1.0f; - - AzFramework::SurfaceData::SurfaceTagWeight tagWeight2; - tagWeight2.m_surfaceType = tag2; - tagWeight2.m_weight = 0.7f; - - AzFramework::SurfaceData::SurfaceTagWeight tagWeight3; - tagWeight3.m_surfaceType = tag3; - tagWeight3.m_weight = 0.3f; - - NiceMock mockSurfaceRequests(entity->GetId()); - ON_CALL(mockSurfaceRequests, GetSurfaceWeights).WillByDefault( - [&tagWeight1, &tagWeight2, &tagWeight3](const AZ::Vector3& position, AzFramework::SurfaceData::SurfaceTagWeightList& surfaceWeights) - { - surfaceWeights.clear(); - float absYPos = fabsf(position.GetY()); - if (absYPos < 1.0f) - { - surfaceWeights.push_back(tagWeight1); - } - else if(absYPos < 2.0f) - { - surfaceWeights.push_back(tagWeight2); - } - else - { - surfaceWeights.push_back(tagWeight3); - } - } - ); - - auto perPositionCallback = [&tagWeight1, &tagWeight2, &tagWeight3](size_t xIndex, size_t yIndex, + auto perPositionCallback = [&expectedTags](size_t xIndex, size_t yIndex, const AzFramework::SurfaceData::SurfacePoint& surfacePoint, [[maybe_unused]] bool terrainExists) { constexpr float epsilon = 0.0001f; @@ -1049,18 +1026,18 @@ namespace UnitTest float absYPos = fabsf(surfacePoint.m_position.GetY()); if (absYPos < 1.0f) { - EXPECT_EQ(surfacePoint.m_surfaceTags[0].m_surfaceType, tagWeight1.m_surfaceType); - EXPECT_NEAR(surfacePoint.m_surfaceTags[0].m_weight, tagWeight1.m_weight, epsilon); + EXPECT_EQ(surfacePoint.m_surfaceTags[0].m_surfaceType, expectedTags[0].m_surfaceType); + EXPECT_NEAR(surfacePoint.m_surfaceTags[0].m_weight, expectedTags[0].m_weight, epsilon); } else if(absYPos < 2.0f) { - EXPECT_EQ(surfacePoint.m_surfaceTags[0].m_surfaceType, tagWeight2.m_surfaceType); - EXPECT_NEAR(surfacePoint.m_surfaceTags[0].m_weight, tagWeight2.m_weight, epsilon); + EXPECT_EQ(surfacePoint.m_surfaceTags[0].m_surfaceType, expectedTags[1].m_surfaceType); + EXPECT_NEAR(surfacePoint.m_surfaceTags[0].m_weight, expectedTags[1].m_weight, epsilon); } else { - EXPECT_EQ(surfacePoint.m_surfaceTags[0].m_surfaceType, tagWeight3.m_surfaceType); - EXPECT_NEAR(surfacePoint.m_surfaceTags[0].m_weight, tagWeight3.m_weight, epsilon); + EXPECT_EQ(surfacePoint.m_surfaceTags[0].m_surfaceType, expectedTags[2].m_surfaceType); + EXPECT_NEAR(surfacePoint.m_surfaceTags[0].m_weight, expectedTags[2].m_weight, epsilon); } };