Move common code used by multiple tests into functions to reduce code duplication.

Signed-off-by: amzn-sj <srikkant@amazon.com>
This commit is contained in:
amzn-sj
2022-01-19 16:05:51 -08:00
parent 392d08e2f0
commit 4b5f4042f2
2 changed files with 111 additions and 155 deletions
@@ -69,6 +69,46 @@ protected:
m_colliderComponent = m_entity->CreateComponent<Terrain::TerrainPhysicsColliderComponent>(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<size_t>(ceil(inRegion.GetExtents().GetX() / stepSize.GetX()));
const size_t numSamplesY = aznumeric_cast<size_t>(ceil(inRegion.GetExtents().GetY() / stepSize.GetY()));
AzFramework::SurfaceData::SurfacePoint surfacePoint;
for (size_t y = 0; y < numSamplesY; y++)
{
float fy = aznumeric_cast<float>(inRegion.GetMin().GetY() + (y * stepSize.GetY()));
for (size_t x = 0; x < numSamplesX; x++)
{
bool terrainExists = false;
float fx = aznumeric_cast<float>(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<UnitTest::MockTerrainDataRequests> 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<size_t>(ceil(inRegion.GetExtents().GetX() / stepSize.GetX()));
const size_t numSamplesY = aznumeric_cast<size_t>(ceil(inRegion.GetExtents().GetY() / stepSize.GetY()));
AzFramework::SurfaceData::SurfacePoint surfacePoint;
for (size_t y = 0; y < numSamplesY; y++)
{
float fy = aznumeric_cast<float>(inRegion.GetMin().GetY() + (y * stepSize.GetY()));
for (size_t x = 0; x < numSamplesX; x++)
{
bool terrainExists = false;
float fx = aznumeric_cast<float>(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<UnitTest::MockTerrainDataRequests> 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<size_t>(ceil(inRegion.GetExtents().GetX() / stepSize.GetX()));
const size_t numSamplesY = aznumeric_cast<size_t>(ceil(inRegion.GetExtents().GetY() / stepSize.GetY()));
AzFramework::SurfaceData::SurfacePoint surfacePoint;
for (size_t y = 0; y < numSamplesY; y++)
{
float fy = aznumeric_cast<float>(inRegion.GetMin().GetY() + (y * stepSize.GetY()));
for (size_t x = 0; x < numSamplesX; x++)
{
bool terrainExists = false;
float fx = aznumeric_cast<float>(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<UnitTest::MockTerrainDataRequests> 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<size_t>(ceil(inRegion.GetExtents().GetX() / stepSize.GetX()));
const size_t numSamplesY = aznumeric_cast<size_t>(ceil(inRegion.GetExtents().GetY() / stepSize.GetY()));
AzFramework::SurfaceData::SurfacePoint surfacePoint;
for (size_t y = 0; y < numSamplesY; y++)
{
float fy = aznumeric_cast<float>(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<float>(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);
}
);
+63 -86
View File
@@ -68,6 +68,7 @@ namespace UnitTest
AZStd::unique_ptr<NiceMock<UnitTest::MockBoxShapeComponentRequests>> m_boxShapeRequests;
AZStd::unique_ptr<NiceMock<UnitTest::MockShapeComponentRequests>> m_shapeRequests;
AZStd::unique_ptr<NiceMock<UnitTest::MockTerrainAreaHeightRequests>> m_terrainAreaHeightRequests;
AZStd::unique_ptr<NiceMock<UnitTest::MockTerrainAreaSurfaceRequestBus>> 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<NiceMock<UnitTest::MockTerrainAreaSurfaceRequestBus>>(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<UnitTest::MockTerrainAreaSurfaceRequestBus> 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<UnitTest::MockTerrainAreaSurfaceRequestBus> 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);
}
};