Merge pull request #5543 from aws-lumberyard-dev/terrain/sphrose/lost_unit_tests

Redoing tests that got lost when TerrainPhysicsManager was merged.
monroegm-disable-blank-issue-2
sphrose 4 years ago committed by GitHub
commit fac3d0b1bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,7 +19,6 @@
#include <Terrain/MockTerrain.h>
using ::testing::_;
using ::testing::AtLeast;
using ::testing::Mock;
using ::testing::NiceMock;
using ::testing::Return;
@ -29,8 +28,6 @@ class TerrainHeightGradientListComponentTest : public ::testing::Test
protected:
AZ::ComponentApplication m_app;
AZStd::unique_ptr<AZ::Entity> m_entity;
void SetUp() override
{
AZ::ComponentApplication::Descriptor appDesc;
@ -46,47 +43,70 @@ protected:
m_app.Destroy();
}
void CreateEntity()
AZStd::unique_ptr<AZ::Entity> CreateEntity()
{
m_entity = AZStd::make_unique<AZ::Entity>();
ASSERT_TRUE(m_entity);
// Create the required box component.
UnitTest::MockAxisAlignedBoxShapeComponent* boxComponent = m_entity->CreateComponent<UnitTest::MockAxisAlignedBoxShapeComponent>();
m_app.RegisterComponentDescriptor(boxComponent->CreateDescriptor());
auto entity = AZStd::make_unique<AZ::Entity>();
entity->Init();
return entity;
}
Terrain::TerrainHeightGradientListComponent* AddHeightGradientListToEntity(AZ::Entity* entity)
{
// Create the TerrainHeightGradientListComponent with an entity in its configuration.
Terrain::TerrainHeightGradientListConfig config;
config.m_gradientEntities.push_back(m_entity->GetId());
config.m_gradientEntities.push_back(entity->GetId());
Terrain::TerrainHeightGradientListComponent* heightGradientListComponent = m_entity->CreateComponent<Terrain::TerrainHeightGradientListComponent>(config);
auto heightGradientListComponent = entity->CreateComponent<Terrain::TerrainHeightGradientListComponent>(config);
m_app.RegisterComponentDescriptor(heightGradientListComponent->CreateDescriptor());
return heightGradientListComponent;
}
void AddRequiredComponetsToEntity(AZ::Entity* entity)
{
// Create the required box component.
UnitTest::MockAxisAlignedBoxShapeComponent* boxComponent = entity->CreateComponent<UnitTest::MockAxisAlignedBoxShapeComponent>();
m_app.RegisterComponentDescriptor(boxComponent->CreateDescriptor());
// Create a MockTerrainLayerSpawnerComponent to provide the required TerrainAreaService.
UnitTest::MockTerrainLayerSpawnerComponent* layerSpawner = m_entity->CreateComponent<UnitTest::MockTerrainLayerSpawnerComponent>();
UnitTest::MockTerrainLayerSpawnerComponent* layerSpawner = entity->CreateComponent<UnitTest::MockTerrainLayerSpawnerComponent>();
m_app.RegisterComponentDescriptor(layerSpawner->CreateDescriptor());
m_entity->Init();
}
};
TEST_F(TerrainHeightGradientListComponentTest, MissingRequiredComponentsActivateFailure)
{
auto entity = CreateEntity();
AddHeightGradientListToEntity(entity.get());
const AZ::Entity::DependencySortOutcome sortOutcome = entity->EvaluateDependenciesGetDetails();
EXPECT_FALSE(sortOutcome.IsSuccess());
}
TEST_F(TerrainHeightGradientListComponentTest, ActivateEntityActivateSuccess)
{
// Check that the entity activates.
CreateEntity();
auto entity = CreateEntity();
AddHeightGradientListToEntity(entity.get());
m_entity->Activate();
EXPECT_EQ(m_entity->GetState(), AZ::Entity::State::Active);
AddRequiredComponetsToEntity(entity.get());
m_entity.reset();
entity->Activate();
EXPECT_EQ(entity->GetState(), AZ::Entity::State::Active);
}
TEST_F(TerrainHeightGradientListComponentTest, TerrainHeightGradientRefreshesTerrainSystem)
{
// Check that the HeightGradientListComponent informs the TerrainSystem when the composition changes.
CreateEntity();
auto entity = CreateEntity();
m_entity->Activate();
AddHeightGradientListToEntity(entity.get());
AddRequiredComponetsToEntity(entity.get());
entity->Activate();
NiceMock<UnitTest::MockTerrainSystemService> terrainSystem;
@ -95,32 +115,34 @@ TEST_F(TerrainHeightGradientListComponentTest, TerrainHeightGradientRefreshesTer
// and once when the HeightGradientListComponent gets the OnCompositionChanged directly through the DependencyNotificationBus.
EXPECT_CALL(terrainSystem, RefreshArea(_, _)).Times(2);
LmbrCentral::DependencyNotificationBus::Event(m_entity->GetId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
LmbrCentral::DependencyNotificationBus::Event(entity->GetId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
// Stop the EXPECT_CALL check now, as OnCompositionChanged will get called twice again during the reset.
Mock::VerifyAndClearExpectations(&terrainSystem);
m_entity.reset();
}
TEST_F(TerrainHeightGradientListComponentTest, TerrainHeightGradientListReturnsHeights)
{
// Check that the HeightGradientListComponent returns expected height values.
CreateEntity();
auto entity = CreateEntity();
NiceMock<UnitTest::MockTerrainAreaHeightRequests> heightfieldRequestBus(m_entity->GetId());
AddHeightGradientListToEntity(entity.get());
m_entity->Activate();
AddRequiredComponetsToEntity(entity.get());
NiceMock<UnitTest::MockTerrainAreaHeightRequests> heightfieldRequestBus(entity->GetId());
entity->Activate();
const float mockGradientValue = 0.25f;
NiceMock<UnitTest::MockGradientRequests> gradientRequests(m_entity->GetId());
NiceMock<UnitTest::MockGradientRequests> gradientRequests(entity->GetId());
ON_CALL(gradientRequests, GetValue).WillByDefault(Return(mockGradientValue));
// Setup a mock to provide the encompassing Aabb to the HeightGradientListComponent.
const float min = 0.0f;
const float max = 1000.0f;
const AZ::Aabb aabb = AZ::Aabb::CreateFromMinMax(AZ::Vector3(min), AZ::Vector3(max));
NiceMock<UnitTest::MockShapeComponentRequests> mockShapeRequests(m_entity->GetId());
NiceMock<UnitTest::MockShapeComponentRequests> mockShapeRequests(entity->GetId());
ON_CALL(mockShapeRequests, GetEncompassingAabb).WillByDefault(Return(aabb));
const float worldMax = 10000.0f;
@ -130,17 +152,16 @@ TEST_F(TerrainHeightGradientListComponentTest, TerrainHeightGradientListReturnsH
ON_CALL(mockterrainDataRequests, GetTerrainAabb).WillByDefault(Return(worldAabb));
// Ensure the cached values in the HeightGradientListComponent are up to date.
LmbrCentral::DependencyNotificationBus::Event(m_entity->GetId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
LmbrCentral::DependencyNotificationBus::Event(entity->GetId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
const AZ::Vector3 inPosition = AZ::Vector3::CreateZero();
AZ::Vector3 outPosition = AZ::Vector3::CreateZero();
bool terrainExists = false;
Terrain::TerrainAreaHeightRequestBus::Event(m_entity->GetId(), &Terrain::TerrainAreaHeightRequestBus::Events::GetHeight, inPosition, outPosition, terrainExists);
Terrain::TerrainAreaHeightRequestBus::Event(
entity->GetId(), &Terrain::TerrainAreaHeightRequestBus::Events::GetHeight, inPosition, outPosition, terrainExists);
const float height = outPosition.GetZ();
EXPECT_NEAR(height, mockGradientValue * max, 0.01f);
m_entity.reset();
}

@ -11,8 +11,6 @@
#include <GradientSignal/Ebuses/MockGradientRequestBus.h>
using ::testing::NiceMock;
using ::testing::AtLeast;
using ::testing::_;
using ::testing::Return;
namespace UnitTest
@ -22,10 +20,6 @@ namespace UnitTest
protected:
AZ::ComponentApplication m_app;
AZStd::unique_ptr<AZ::Entity> m_entity;
UnitTest::MockTerrainLayerSpawnerComponent* m_layerSpawnerComponent = nullptr;
AZStd::unique_ptr<AZ::Entity> m_gradientEntity1, m_gradientEntity2;
const AZStd::string surfaceTag1 = "testtag1";
const AZStd::string surfaceTag2 = "testtag2";
@ -37,81 +31,76 @@ namespace UnitTest
appDesc.m_stackRecordLevels = 20;
m_app.Create(appDesc);
CreateEntities();
}
void TearDown() override
{
m_gradientEntity2.reset();
m_gradientEntity1.reset();
m_entity.reset();
m_app.Destroy();
}
void CreateEntities()
AZStd::unique_ptr<AZ::Entity> CreateEntity()
{
m_entity = AZStd::make_unique<AZ::Entity>();
ASSERT_TRUE(m_entity);
auto entity = AZStd::make_unique<AZ::Entity>();
entity->Init();
return entity;
}
m_entity->Init();
UnitTest::MockTerrainLayerSpawnerComponent* AddRequiredComponentsToEntity(AZ::Entity* entity)
{
auto layerSpawnerComponent = entity->CreateComponent<UnitTest::MockTerrainLayerSpawnerComponent>();
m_app.RegisterComponentDescriptor(layerSpawnerComponent->CreateDescriptor());
m_gradientEntity1 = AZStd::make_unique<AZ::Entity>();
ASSERT_TRUE(m_gradientEntity1);
return layerSpawnerComponent;
}
};
m_gradientEntity1->Init();
TEST_F(TerrainSurfaceGradientListTest, SurfaceGradientMissingRequirementsActivateFails)
{
auto entity = CreateEntity();
m_gradientEntity2 = AZStd::make_unique<AZ::Entity>();
ASSERT_TRUE(m_gradientEntity2);
auto terrainSurfaceGradientListComponent = entity->CreateComponent<Terrain::TerrainSurfaceGradientListComponent>();
m_app.RegisterComponentDescriptor(terrainSurfaceGradientListComponent->CreateDescriptor());
m_gradientEntity2->Init();
const AZ::Entity::DependencySortOutcome sortOutcome = entity->EvaluateDependenciesGetDetails();
EXPECT_FALSE(sortOutcome.IsSuccess());
}
void AddSurfaceGradientListToEntities()
TEST_F(TerrainSurfaceGradientListTest, SurfaceGradientActivateSuccess)
{
m_layerSpawnerComponent = m_entity->CreateComponent<UnitTest::MockTerrainLayerSpawnerComponent>();
m_app.RegisterComponentDescriptor(m_layerSpawnerComponent->CreateDescriptor());
auto entity = CreateEntity();
Terrain::TerrainSurfaceGradientListConfig config;
AddRequiredComponentsToEntity(entity.get());
Terrain::TerrainSurfaceGradientMapping mapping1;
mapping1.m_gradientEntityId = m_gradientEntity1->GetId();
mapping1.m_surfaceTag = SurfaceData::SurfaceTag(surfaceTag1);
config.m_gradientSurfaceMappings.emplace_back(mapping1);
auto terrainSurfaceGradientListComponent = entity->CreateComponent<Terrain::TerrainSurfaceGradientListComponent>();
m_app.RegisterComponentDescriptor(terrainSurfaceGradientListComponent->CreateDescriptor());
Terrain::TerrainSurfaceGradientMapping mapping2;
mapping2.m_gradientEntityId = m_gradientEntity2->GetId();
mapping2.m_surfaceTag = SurfaceData::SurfaceTag(surfaceTag2);
config.m_gradientSurfaceMappings.emplace_back(mapping2);
entity->Activate();
Terrain::TerrainSurfaceGradientListComponent* terrainSurfaceGradientListComponent =
m_entity->CreateComponent<Terrain::TerrainSurfaceGradientListComponent>(config);
m_app.RegisterComponentDescriptor(terrainSurfaceGradientListComponent->CreateDescriptor());
EXPECT_EQ(entity->GetState(), AZ::Entity::State::Active);
}
};
TEST_F(TerrainSurfaceGradientListTest, SurfaceGradientReturnsSurfaceWeights)
{
// When there is more than one surface/weight defined and added to the component, they should all
// be returned. The component isn't required to return them in descending order.
AddSurfaceGradientListToEntities();
auto entity = CreateEntity();
AddRequiredComponentsToEntity(entity.get());
m_entity->Activate();
m_gradientEntity1->Activate();
m_gradientEntity2->Activate();
auto gradientEntity1 = CreateEntity();
auto gradientEntity2 = CreateEntity();
const float gradient1Value = 0.3f;
NiceMock<UnitTest::MockGradientRequests> mockGradientRequests1(m_gradientEntity1->GetId());
NiceMock<UnitTest::MockGradientRequests> mockGradientRequests1(gradientEntity1->GetId());
ON_CALL(mockGradientRequests1, GetValue).WillByDefault(Return(gradient1Value));
const float gradient2Value = 1.0f;
NiceMock<UnitTest::MockGradientRequests> mockGradientRequests2(m_gradientEntity2->GetId());
NiceMock<UnitTest::MockGradientRequests> mockGradientRequests2(gradientEntity2->GetId());
ON_CALL(mockGradientRequests2, GetValue).WillByDefault(Return(gradient2Value));
AzFramework::SurfaceData::SurfaceTagWeightList weightList;
Terrain::TerrainAreaSurfaceRequestBus::Event(
m_entity->GetId(), &Terrain::TerrainAreaSurfaceRequestBus::Events::GetSurfaceWeights, AZ::Vector3::CreateZero(), weightList);
entity->GetId(), &Terrain::TerrainAreaSurfaceRequestBus::Events::GetSurfaceWeights, AZ::Vector3::CreateZero(), weightList);
AZ::Crc32 expectedCrcList[] = { AZ::Crc32(surfaceTag1), AZ::Crc32(surfaceTag2) };
const float expectedWeightList[] = { gradient1Value, gradient2Value };

Loading…
Cancel
Save