diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp index 1640ac1017..e5530b49f4 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp @@ -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); diff --git a/Code/Framework/AzToolsFramework/Tests/Prefab/PrefabDeleteTests.cpp b/Code/Framework/AzToolsFramework/Tests/Prefab/PrefabDeleteTests.cpp new file mode 100644 index 0000000000..0ef328b2ca --- /dev/null +++ b/Code/Framework/AzToolsFramework/Tests/Prefab/PrefabDeleteTests.cpp @@ -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 +#include +#include + +#include +#include +#include + +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 diff --git a/Code/Framework/AzToolsFramework/Tests/Prefab/PrefabTestFixture.cpp b/Code/Framework/AzToolsFramework/Tests/Prefab/PrefabTestFixture.cpp index ed30de09c4..494f635a9f 100644 --- a/Code/Framework/AzToolsFramework/Tests/Prefab/PrefabTestFixture.cpp +++ b/Code/Framework/AzToolsFramework/Tests/Prefab/PrefabTestFixture.cpp @@ -57,6 +57,11 @@ namespace UnitTest return AZStd::make_unique("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(); + } } diff --git a/Code/Framework/AzToolsFramework/Tests/Prefab/PrefabTestFixture.h b/Code/Framework/AzToolsFramework/Tests/Prefab/PrefabTestFixture.h index 0a78ded1a6..e7fe771610 100644 --- a/Code/Framework/AzToolsFramework/Tests/Prefab/PrefabTestFixture.h +++ b/Code/Framework/AzToolsFramework/Tests/Prefab/PrefabTestFixture.h @@ -52,6 +52,8 @@ namespace UnitTest AZStd::unique_ptr 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; diff --git a/Code/Framework/AzToolsFramework/Tests/UI/EntityOutlinerTests.cpp b/Code/Framework/AzToolsFramework/Tests/UI/EntityOutlinerTests.cpp index 78cdcf9d38..eb7d87fb78 100644 --- a/Code/Framework/AzToolsFramework/Tests/UI/EntityOutlinerTests.cpp +++ b/Code/Framework/AzToolsFramework/Tests/UI/EntityOutlinerTests.cpp @@ -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(); diff --git a/Code/Framework/AzToolsFramework/Tests/aztoolsframeworktests_files.cmake b/Code/Framework/AzToolsFramework/Tests/aztoolsframeworktests_files.cmake index b2b50ca7c6..1a8819b188 100644 --- a/Code/Framework/AzToolsFramework/Tests/aztoolsframeworktests_files.cmake +++ b/Code/Framework/AzToolsFramework/Tests/aztoolsframeworktests_files.cmake @@ -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