Prefab/unit tests for deletion (#6034)

* Added tests for deleting entity under level and other prefabs

Signed-off-by: srikappa-amzn <82230713+srikappa-amzn@users.noreply.github.com>

* Added class comments and improved variable names

Signed-off-by: srikappa-amzn <82230713+srikappa-amzn@users.noreply.github.com>

* Called the reflect function of PrefabFocusHandler from PrefabSystemComponent

Signed-off-by: srikappa-amzn <82230713+srikappa-amzn@users.noreply.github.com>

* Added couple of unit tests around entity and prefab deletion

Signed-off-by: srikappa-amzn <82230713+srikappa-amzn@users.noreply.github.com>

* Used engine root path from settings registry as fake path for prefab creation

Signed-off-by: srikappa-amzn <82230713+srikappa-amzn@users.noreply.github.com>

* Added 2 unit tests around deleting prefabs

Signed-off-by: srikappa-amzn <82230713+srikappa-amzn@users.noreply.github.com>
monroegm-disable-blank-issue-2
srikappa-amzn 4 years ago committed by GitHub
parent 6150a60e57
commit 625fca71ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -521,21 +521,18 @@ namespace AzToolsFramework
nestedInstanceLink.has_value(),
"A valid link was not found for one of the instances provided as input for the CreatePrefab operation.");
PrefabDom patchesCopyForUndoSupport;
PrefabDomReference nestedInstanceLinkDom = nestedInstanceLink->get().GetLinkDom();
AZ_Assert(
nestedInstanceLinkDom.has_value(),
"A valid DOM was not found for the link corresponding to one of the instances provided as input for the "
"CreatePrefab operation.");
PrefabDomValueReference nestedInstanceLinkPatches =
PrefabDomUtils::FindPrefabDomValue(nestedInstanceLinkDom->get(), PrefabDomUtils::PatchesName);
AZ_Assert(
nestedInstanceLinkPatches.has_value(),
"A valid DOM for patches was not found for the link corresponding to one of the instances provided as input for the "
"CreatePrefab operation.");
if (nestedInstanceLinkDom.has_value())
{
PrefabDomValueReference nestedInstanceLinkPatches =
PrefabDomUtils::FindPrefabDomValue(nestedInstanceLinkDom->get(), PrefabDomUtils::PatchesName);
if (nestedInstanceLinkPatches.has_value())
{
patchesCopyForUndoSupport.CopyFrom(nestedInstanceLinkPatches->get(), patchesCopyForUndoSupport.GetAllocator());
}
}
PrefabDom patchesCopyForUndoSupport;
patchesCopyForUndoSupport.CopyFrom(nestedInstanceLinkPatches->get(), patchesCopyForUndoSupport.GetAllocator());
PrefabUndoHelpers::RemoveLink(
sourceInstance->GetTemplateId(), targetTemplateId, sourceInstance->GetInstanceAlias(), sourceInstance->GetLinkId(),
AZStd::move(patchesCopyForUndoSupport), undoBatch);

@ -0,0 +1,150 @@
/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <AzCore/Settings/SettingsRegistryMergeUtils.h>
#include <AzToolsFramework/Entity/EditorEntityHelpers.h>
#include <AzToolsFramework/ToolsComponents/TransformComponent.h>
#include <Prefab/PrefabTestComponent.h>
#include <Prefab/PrefabTestDomUtils.h>
#include <Prefab/PrefabTestFixture.h>
namespace UnitTest
{
using PrefabDeleteTest = PrefabTestFixture;
TEST_F(PrefabDeleteTest, DeleteEntitiesInInstance_DeleteSingleEntitySucceeds)
{
PrefabEntityResult createEntityResult = m_prefabPublicInterface->CreateEntity(AZ::EntityId(), AZ::Vector3());
// Verify that a valid entity is created.
AZ::EntityId testEntityId = createEntityResult.GetValue();
ASSERT_TRUE(testEntityId.IsValid());
AZ::Entity* testEntity = AzToolsFramework::GetEntityById(testEntityId);
ASSERT_TRUE(testEntity != nullptr);
m_prefabPublicInterface->DeleteEntitiesInInstance(AzToolsFramework::EntityIdList{ testEntityId });
// Verify that entity can't be found after deletion.
testEntity = AzToolsFramework::GetEntityById(testEntityId);
EXPECT_TRUE(testEntity == nullptr);
}
TEST_F(PrefabDeleteTest, DeleteEntitiesInInstance_DeleteSinglePrefabSucceeds)
{
PrefabEntityResult createEntityResult = m_prefabPublicInterface->CreateEntity(AZ::EntityId(), AZ::Vector3());
// Verify that a valid entity is created.
AZ::EntityId createdEntityId = createEntityResult.GetValue();
ASSERT_TRUE(createdEntityId.IsValid());
AZ::Entity* createdEntity = AzToolsFramework::GetEntityById(createdEntityId);
ASSERT_TRUE(createdEntity != nullptr);
// Rather than hardcode a path, use a path from settings registry since that will work on all platforms.
AZ::SettingsRegistryInterface* registry = AZ::SettingsRegistry::Get();
AZ::IO::FixedMaxPath path;
registry->Get(path.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
CreatePrefabResult createPrefabResult =
m_prefabPublicInterface->CreatePrefabInMemory(AzToolsFramework::EntityIdList{ createdEntityId }, path);
AZ::EntityId createdPrefabContainerId = createPrefabResult.GetValue();
ASSERT_TRUE(createdPrefabContainerId.IsValid());
AZ::Entity* prefabContainerEntity = AzToolsFramework::GetEntityById(createdPrefabContainerId);
ASSERT_TRUE(prefabContainerEntity != nullptr);
// Verify that the prefab container entity and the entity within are deleted.
m_prefabPublicInterface->DeleteEntitiesInInstance(AzToolsFramework::EntityIdList{ createdPrefabContainerId });
prefabContainerEntity = AzToolsFramework::GetEntityById(createdPrefabContainerId);
EXPECT_TRUE(prefabContainerEntity == nullptr);
createdEntity = AzToolsFramework::GetEntityById(createdEntityId);
EXPECT_TRUE(createdEntity == nullptr);
}
TEST_F(PrefabDeleteTest, DeleteEntitiesAndAllDescendantsInInstance_DeletingEntityDeletesChildEntityToo)
{
PrefabEntityResult parentEntityCreationResult = m_prefabPublicInterface->CreateEntity(AZ::EntityId(), AZ::Vector3());
// Verify that valid parent entity is created.
AZ::EntityId parentEntityId = parentEntityCreationResult.GetValue();
ASSERT_TRUE(parentEntityId.IsValid());
AZ::Entity* parentEntity = AzToolsFramework::GetEntityById(parentEntityId);
ASSERT_TRUE(parentEntity != nullptr);
// Verify that valid child entity is created.
PrefabEntityResult childEntityCreationResult = m_prefabPublicInterface->CreateEntity(parentEntityId, AZ::Vector3());
AZ::EntityId childEntityId = childEntityCreationResult.GetValue();
ASSERT_TRUE(childEntityId.IsValid());
AZ::Entity* childEntity = AzToolsFramework::GetEntityById(childEntityId);
ASSERT_TRUE(childEntity != nullptr);
// PrefabTestFixture won't add required editor components by default. Hence we add them here.
AddRequiredEditorComponents(childEntity);
AddRequiredEditorComponents(parentEntity);
// Parent the child entity under the parent entity.
AZ::TransformBus::Event(childEntityId, &AZ::TransformBus::Events::SetParent, parentEntityId);
// Delete parent entity and its children.
m_prefabPublicInterface->DeleteEntitiesAndAllDescendantsInInstance(AzToolsFramework::EntityIdList{ parentEntityId });
// Verify that both the parent and child entities are deleted.
parentEntity = AzToolsFramework::GetEntityById(parentEntityId);
EXPECT_TRUE(parentEntity == nullptr);
childEntity = AzToolsFramework::GetEntityById(childEntityId);
EXPECT_TRUE(childEntity == nullptr);
}
TEST_F(PrefabDeleteTest, DeleteEntitiesAndAllDescendantsInInstance_DeletingEntityDeletesChildPrefabToo)
{
PrefabEntityResult entityToBePutUnderPrefabResult = m_prefabPublicInterface->CreateEntity(AZ::EntityId(), AZ::Vector3());
// Verify that a valid entity is created that will be put in a prefab later.
AZ::EntityId entityToBePutUnderPrefabId = entityToBePutUnderPrefabResult.GetValue();
ASSERT_TRUE(entityToBePutUnderPrefabId.IsValid());
AZ::Entity* entityToBePutUnderPrefab = AzToolsFramework::GetEntityById(entityToBePutUnderPrefabId);
ASSERT_TRUE(entityToBePutUnderPrefab != nullptr);
// Verify that a valid parent entity is created.
PrefabEntityResult parentEntityCreationResult = m_prefabPublicInterface->CreateEntity(AZ::EntityId(), AZ::Vector3());
AZ::EntityId parentEntityId = parentEntityCreationResult.GetValue();
ASSERT_TRUE(parentEntityId.IsValid());
AZ::Entity* parentEntity = AzToolsFramework::GetEntityById(parentEntityId);
ASSERT_TRUE(parentEntity != nullptr);
// Rather than hardcode a path, use a path from settings registry since that will work on all platforms.
AZ::SettingsRegistryInterface* registry = AZ::SettingsRegistry::Get();
AZ::IO::FixedMaxPath path;
registry->Get(path.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
CreatePrefabResult createPrefabResult =
m_prefabPublicInterface->CreatePrefabInMemory(AzToolsFramework::EntityIdList{ entityToBePutUnderPrefabId }, path);
// Verify that a valid prefab container entity is created.
AZ::EntityId createdPrefabContainerId = createPrefabResult.GetValue();
ASSERT_TRUE(createdPrefabContainerId.IsValid());
AZ::Entity* prefabContainerEntity = AzToolsFramework::GetEntityById(createdPrefabContainerId);
ASSERT_TRUE(prefabContainerEntity != nullptr);
// PrefabTestFixture won't add required editor components by default. Hence we add them here.
AddRequiredEditorComponents(parentEntity);
AddRequiredEditorComponents(prefabContainerEntity);
// Parent the prefab under the parent entity.
AZ::TransformBus::Event(createdPrefabContainerId, &AZ::TransformBus::Events::SetParent, parentEntityId);
// Delete the parent entity.
m_prefabPublicInterface->DeleteEntitiesAndAllDescendantsInInstance(AzToolsFramework::EntityIdList{ parentEntityId });
// Validate that the parent and the prefab under it and the entity inside the prefab are all deleted.
parentEntity = AzToolsFramework::GetEntityById(parentEntityId);
ASSERT_TRUE(parentEntity == nullptr);
entityToBePutUnderPrefab = AzToolsFramework::GetEntityById(entityToBePutUnderPrefabId);
ASSERT_TRUE(entityToBePutUnderPrefab == nullptr);
prefabContainerEntity = AzToolsFramework::GetEntityById(createdPrefabContainerId);
EXPECT_TRUE(prefabContainerEntity == nullptr);
}
} // namespace UnitTest

@ -57,6 +57,11 @@ namespace UnitTest
return AZStd::make_unique<PrefabTestToolsApplication>("PrefabTestApplication");
}
void PrefabTestFixture::PropagateAllTemplateChanges()
{
m_prefabSystemComponent->OnSystemTick();
}
AZ::Entity* PrefabTestFixture::CreateEntity(const char* entityName, const bool shouldActivate)
{
// Circumvent the EntityContext system and generate a new entity with a transformcomponent
@ -125,4 +130,13 @@ namespace UnitTest
EXPECT_EQ(entityInInstance->GetState(), AZ::Entity::State::Active);
}
}
void PrefabTestFixture::AddRequiredEditorComponents(AZ::Entity* entity)
{
ASSERT_TRUE(entity != nullptr);
entity->Deactivate();
AzToolsFramework::EditorEntityContextRequestBus::Broadcast(
&AzToolsFramework::EditorEntityContextRequests::AddRequiredComponents, *entity);
entity->Activate();
}
}

@ -52,6 +52,8 @@ namespace UnitTest
AZStd::unique_ptr<ToolsTestApplication> CreateTestApplication() override;
void PropagateAllTemplateChanges();
AZ::Entity* CreateEntity(const char* entityName, const bool shouldActivate = true);
void CompareInstances(const Instance& instanceA, const Instance& instanceB, bool shouldCompareLinkIds = true,
@ -62,6 +64,8 @@ namespace UnitTest
//! Validates that all entities within a prefab instance are in 'Active' state.
void ValidateInstanceEntitiesActive(Instance& instance);
void AddRequiredEditorComponents(AZ::Entity* entity);
PrefabSystemComponent* m_prefabSystemComponent = nullptr;
PrefabLoaderInterface* m_prefabLoaderInterface = nullptr;
PrefabPublicInterface* m_prefabPublicInterface = nullptr;

@ -128,7 +128,7 @@ namespace UnitTest
void ProcessDeferredUpdates()
{
// Force a prefab propagation for updates that are deferred to the next tick.
m_prefabSystemComponent->OnSystemTick();
PropagateAllTemplateChanges();
// Ensure the model process its entity update queue
m_model->ProcessEntityUpdates();

@ -69,6 +69,7 @@ set(FILES
Prefab/PrefabFocus/PrefabFocusTests.cpp
Prefab/MockPrefabFileIOActionValidator.cpp
Prefab/MockPrefabFileIOActionValidator.h
Prefab/PrefabDeleteTests.cpp
Prefab/PrefabDuplicateTests.cpp
Prefab/PrefabEntityAliasTests.cpp
Prefab/PrefabInstanceToTemplatePropagatorTests.cpp

Loading…
Cancel
Save