123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- /*
- * 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 <Prefab/PrefabTestFixture.h>
- #include <AzToolsFramework/Prefab/Undo/PrefabUndo.h>
- #include <AzToolsFramework/Prefab/Undo/PrefabUndoDelete.h>
- #include <AzToolsFramework/Prefab/Undo/PrefabUndoDeleteAsOverride.h>
- namespace UnitTest
- {
- using PrefabUndoDeleteTests = PrefabTestFixture;
- TEST_F(PrefabUndoDeleteTests, PrefabUndoDeleteTests_DeleteEntity)
- {
- // Level
- // | Car <-- focused
- // | Tire <-- delete
- // | Car
- // | Tire
- const AZStd::string carPrefabName = "Car";
- const AZStd::string tireEntityName = "Tire";
- AZ::IO::Path engineRootPath;
- m_settingsRegistryInterface->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
- AZ::IO::Path carPrefabFilepath(engineRootPath);
- carPrefabFilepath.Append(carPrefabName);
- AZ::EntityId tireEntityId = CreateEditorEntity(tireEntityName, GetRootContainerEntityId());
- AZ::EntityId carContainerId = CreateEditorPrefab(carPrefabFilepath, { tireEntityId });
-
- EntityAlias tireEntityAlias = FindEntityAliasInInstance(carContainerId, tireEntityName);
- ASSERT_FALSE(tireEntityAlias.empty());
- AZ::EntityId secondCarContainerId = InstantiateEditorPrefab(carPrefabFilepath, GetRootContainerEntityId());
- auto firstCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(carContainerId);
- ASSERT_TRUE(firstCarInstance.has_value());
- auto secondCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(secondCarContainerId);
- ASSERT_TRUE(secondCarInstance.has_value());
- // Validate before deletion
- ValidateEntityUnderInstance(firstCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
- ValidateEntityUnderInstance(secondCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
- // Create an undo node
- PrefabUndoDeleteEntity undoDeleteNode("Undo Deleting Entity");
- EntityOptionalReference parentEntityToUpdate = firstCarInstance->get().GetContainerEntity();
- ASSERT_TRUE(parentEntityToUpdate.has_value());
- AZ::EntityId firstTireEntityId = firstCarInstance->get().GetEntityId(tireEntityAlias);
- ASSERT_TRUE(firstTireEntityId.IsValid());
- AZStd::string firstTireEntityAliasPath = m_instanceToTemplateInterface->GenerateEntityAliasPath(firstTireEntityId);
- firstCarInstance->get().DetachEntity(firstTireEntityId).reset();
- undoDeleteNode.Capture({ firstTireEntityAliasPath }, { &(parentEntityToUpdate->get()) }, firstCarInstance->get());
- // Redo
- undoDeleteNode.Redo();
- PropagateAllTemplateChanges();
-
- ValidateEntityNotUnderInstance(firstCarInstance->get().GetContainerEntityId(), tireEntityAlias);
- ValidateEntityNotUnderInstance(secondCarInstance->get().GetContainerEntityId(), tireEntityAlias);
- // Undo
- undoDeleteNode.Undo();
- PropagateAllTemplateChanges();
- ValidateEntityUnderInstance(firstCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
- ValidateEntityUnderInstance(secondCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
- }
- TEST_F(PrefabUndoDeleteTests, PrefabUndoDeleteTests_DeleteEntityAsOverride)
- {
- // Level <-- focused
- // | Car
- // | Tire <-- delete
- // | Car
- // | Tire
- const AZStd::string carPrefabName = "Car";
- const AZStd::string tireEntityName = "Tire";
- AZ::IO::Path engineRootPath;
- m_settingsRegistryInterface->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
- AZ::IO::Path carPrefabFilepath(engineRootPath);
- carPrefabFilepath.Append(carPrefabName);
- AZ::EntityId tireEntityId = CreateEditorEntity(tireEntityName, GetRootContainerEntityId());
- AZ::EntityId carContainerId = CreateEditorPrefab(carPrefabFilepath, { tireEntityId });
- EntityAlias tireEntityAlias = FindEntityAliasInInstance(carContainerId, tireEntityName);
- ASSERT_FALSE(tireEntityAlias.empty());
- AZ::EntityId secondCarContainerId = InstantiateEditorPrefab(carPrefabFilepath, GetRootContainerEntityId());
- auto firstCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(carContainerId);
- ASSERT_TRUE(firstCarInstance.has_value());
- auto secondCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(secondCarContainerId);
- ASSERT_TRUE(secondCarInstance.has_value());
- // Validate before deletion
- ValidateEntityUnderInstance(firstCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
- ValidateEntityUnderInstance(secondCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
- // Create an undo node
- PrefabUndoDeleteAsOverride undoDeleteNode("Undo Deleting Entity As Override");
- EntityOptionalReference parentEntityToUpdate = firstCarInstance->get().GetContainerEntity();
- ASSERT_TRUE(parentEntityToUpdate.has_value());
- AZ::EntityId firstTireEntityId = firstCarInstance->get().GetEntityId(tireEntityAlias);
- ASSERT_TRUE(firstTireEntityId.IsValid());
- AZStd::string firstTireEntityAliasPath = m_instanceToTemplateInterface->GenerateEntityAliasPath(firstTireEntityId);
- firstCarInstance->get().DetachEntity(firstTireEntityId).reset();
- auto levelRootInstance = m_instanceEntityMapperInterface->FindOwningInstance(GetRootContainerEntityId());
- ASSERT_TRUE(levelRootInstance.has_value());
- undoDeleteNode.Capture({ firstTireEntityAliasPath }, {}, { &(parentEntityToUpdate->get()) },
- firstCarInstance->get(), levelRootInstance->get());
- // Redo
- undoDeleteNode.Redo();
- PropagateAllTemplateChanges();
- ValidateEntityNotUnderInstance(firstCarInstance->get().GetContainerEntityId(), tireEntityAlias);
- ValidateEntityUnderInstance(secondCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
- // Undo
- undoDeleteNode.Undo();
- PropagateAllTemplateChanges();
-
- ValidateEntityUnderInstance(firstCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
- ValidateEntityUnderInstance(secondCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
- }
- TEST_F(PrefabUndoDeleteTests, PrefabUndoDeleteTests_DeleteNestedInstance)
- {
- // Level
- // | Car <-- focused
- // | Wheel <-- delete
- // | Tire
- // | Car
- // | Wheel
- // | Tire
- const AZStd::string carPrefabName = "Car";
- const AZStd::string wheelPrefabName = "Wheel";
- const AZStd::string tireEntityName = "Tire";
- AZ::IO::Path engineRootPath;
- m_settingsRegistryInterface->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
- AZ::IO::Path carPrefabFilepath(engineRootPath);
- carPrefabFilepath.Append(carPrefabName);
- AZ::IO::Path wheelPrefabFilepath(engineRootPath);
- wheelPrefabFilepath.Append(wheelPrefabName);
- AZ::EntityId tireEntityId = CreateEditorEntity(tireEntityName, GetRootContainerEntityId());
- AZ::EntityId wheelContainerId = CreateEditorPrefab(wheelPrefabFilepath, { tireEntityId });
- AZ::EntityId carContainerId = CreateEditorPrefab(carPrefabFilepath, { wheelContainerId });
- InstanceAlias wheelInstanceAlias = FindNestedInstanceAliasInInstance(carContainerId, wheelPrefabName);
- ASSERT_FALSE(wheelInstanceAlias.empty());
- AZ::EntityId secondCarContainerId = InstantiateEditorPrefab(carPrefabFilepath, GetRootContainerEntityId());
- auto firstCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(carContainerId);
- ASSERT_TRUE(firstCarInstance.has_value());
- auto secondCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(secondCarContainerId);
- ASSERT_TRUE(secondCarInstance.has_value());
- // Validate before deletion
- ValidateNestedInstanceUnderInstance(firstCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
- ValidateNestedInstanceUnderInstance(secondCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
- // Create an undo node
- // Note: Currently for deleting a nested instance, it depends on the PrefabUndoInstanceLink undo node.
- PrefabUndoInstanceLink undoDeleteNestedInstance("Undo Delete Nested Instance");
- AZStd::unique_ptr<Instance> firstWheelInstance = firstCarInstance->get().DetachNestedInstance(wheelInstanceAlias);
- TemplateId wheelTemplateId = firstWheelInstance->GetTemplateId();
- LinkId wheelLinkId = firstWheelInstance->GetLinkId();
- LinkReference firstWheelLink = m_prefabSystemComponent->FindLink(wheelLinkId);
- ASSERT_TRUE(firstWheelLink.has_value());
- // Generate link patches needed for redo and undo support.
- PrefabDom patchesCopyForUndoSupport;
- PrefabDom wheelInstanceLinkDom;
- firstWheelLink->get().GetLinkDom(wheelInstanceLinkDom, wheelInstanceLinkDom.GetAllocator());
- PrefabDomValueReference wheelInstanceLinkPatches =
- PrefabDomUtils::FindPrefabDomValue(wheelInstanceLinkDom, PrefabDomUtils::PatchesName);
- if (wheelInstanceLinkPatches.has_value())
- {
- patchesCopyForUndoSupport.CopyFrom(wheelInstanceLinkPatches->get(), patchesCopyForUndoSupport.GetAllocator());
- }
- firstWheelInstance.reset();
- undoDeleteNestedInstance.Capture(firstCarInstance->get().GetTemplateId(), wheelTemplateId,
- wheelInstanceAlias, AZStd::move(patchesCopyForUndoSupport), wheelLinkId);
- // Redo
- undoDeleteNestedInstance.Redo();
- PropagateAllTemplateChanges();
- ValidateNestedInstanceNotUnderInstance(firstCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
- ValidateNestedInstanceNotUnderInstance(secondCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
- // Undo
- undoDeleteNestedInstance.Undo();
- PropagateAllTemplateChanges();
- ValidateNestedInstanceUnderInstance(firstCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
- ValidateNestedInstanceUnderInstance(secondCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
- }
- TEST_F(PrefabUndoDeleteTests, PrefabUndoDeleteTests_DeleteInstanceAsOverride)
- {
- // Level <-- focused
- // | First Car
- // | Wheel <-- delete
- // | Tire
- // | Second Car
- // | Wheel
- // | Tire
- const AZStd::string carPrefabName = "Car";
- const AZStd::string wheelPrefabName = "Wheel";
- const AZStd::string tireEntityName = "Tire";
- AZ::IO::Path engineRootPath;
- m_settingsRegistryInterface->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
- AZ::IO::Path carPrefabFilepath(engineRootPath);
- carPrefabFilepath.Append(carPrefabName);
- AZ::IO::Path wheelPrefabFilepath(engineRootPath);
- wheelPrefabFilepath.Append(wheelPrefabName);
- AZ::EntityId tireEntityId = CreateEditorEntity(tireEntityName, GetRootContainerEntityId());
- AZ::EntityId wheelContainerId = CreateEditorPrefab(wheelPrefabFilepath, { tireEntityId });
- AZ::EntityId carContainerId = CreateEditorPrefab(carPrefabFilepath, { wheelContainerId });
- InstanceAlias wheelInstanceAlias = FindNestedInstanceAliasInInstance(carContainerId, wheelPrefabName);
- ASSERT_FALSE(wheelInstanceAlias.empty());
- AZ::EntityId secondCarContainerId = InstantiateEditorPrefab(carPrefabFilepath, GetRootContainerEntityId());
- auto firstCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(carContainerId);
- ASSERT_TRUE(firstCarInstance.has_value());
- auto secondCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(secondCarContainerId);
- ASSERT_TRUE(secondCarInstance.has_value());
- // Validate before deletion
- ValidateNestedInstanceUnderInstance(firstCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
- ValidateNestedInstanceUnderInstance(secondCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
- // Create an undo node
- PrefabUndoDeleteAsOverride undoDeleteAsOverride("Undo Delete Instance As Override");
- auto parentEntityToUpdate = firstCarInstance->get().GetContainerEntity();
- ASSERT_TRUE(parentEntityToUpdate.has_value());
- const AZStd::string firstWheelInstanceAliasPath = PrefabDomUtils::PathStartingWithInstances + wheelInstanceAlias;
- firstCarInstance->get().DetachNestedInstance(wheelInstanceAlias).reset();
- auto levelRootInstance = m_instanceEntityMapperInterface->FindOwningInstance(GetRootContainerEntityId());
- ASSERT_TRUE(levelRootInstance.has_value());
- undoDeleteAsOverride.Capture({}, { firstWheelInstanceAliasPath }, { &(parentEntityToUpdate->get()) },
- firstCarInstance->get(), levelRootInstance->get());
- // Redo
- undoDeleteAsOverride.Redo();
- PropagateAllTemplateChanges();
- ValidateNestedInstanceNotUnderInstance(firstCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
- ValidateNestedInstanceUnderInstance(secondCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
- // Undo
- undoDeleteAsOverride.Undo();
- PropagateAllTemplateChanges();
- ValidateNestedInstanceUnderInstance(firstCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
- ValidateNestedInstanceUnderInstance(secondCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
- }
- } // namespace UnitTest
|