PrefabDeleteTests.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Prefab/PrefabTestFixture.h>
  9. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  10. namespace UnitTest
  11. {
  12. using PrefabDeleteTests = PrefabTestFixture;
  13. TEST_F(PrefabDeleteTests, DeleteEntitiesAndAllDescendantsInInstance_DeleteSingleEntitySucceeds)
  14. {
  15. const AZStd::string entityToDeleteName = "EntityToDelete";
  16. AZ::EntityId entityId = CreateEditorEntityUnderRoot(entityToDeleteName);
  17. EntityAlias entityAlias = FindEntityAliasInInstance(GetRootContainerEntityId(), entityToDeleteName);
  18. PrefabOperationResult result = m_prefabPublicInterface->DeleteEntitiesAndAllDescendantsInInstance({ entityId });
  19. ASSERT_TRUE(result.IsSuccess());
  20. // Verify that entity can't be found after deletion.
  21. AZ::Entity* entityToDelete = AzToolsFramework::GetEntityById(entityId);
  22. EXPECT_TRUE(entityToDelete == nullptr);
  23. ValidateEntityNotUnderInstance(GetRootContainerEntityId(), entityAlias);
  24. }
  25. TEST_F(PrefabDeleteTests, DeleteEntitiesAndAllDescendantsInInstance_DeleteSinglePrefabSucceeds)
  26. {
  27. const AZStd::string entityName = "EntityUnderPrefab";
  28. const AZStd::string prefabName = "PrefabToDelete";
  29. AZ::IO::Path engineRootPath;
  30. m_settingsRegistryInterface->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  31. AZ::IO::Path prefabFilepath(engineRootPath);
  32. prefabFilepath.Append(prefabName);
  33. AZ::EntityId entityId = CreateEditorEntityUnderRoot(entityName);
  34. AZ::EntityId containerEntityId = CreateEditorPrefab(prefabFilepath, { entityId });
  35. InstanceAlias prefabInstanceAlias = FindNestedInstanceAliasInInstance(GetRootContainerEntityId(), prefabName);
  36. // Verify that the prefab container entity and the entity within are deleted.
  37. PrefabOperationResult result = m_prefabPublicInterface->DeleteEntitiesAndAllDescendantsInInstance({ containerEntityId });
  38. ASSERT_TRUE(result.IsSuccess());
  39. AZ::Entity* containerEntity = AzToolsFramework::GetEntityById(containerEntityId);
  40. EXPECT_TRUE(containerEntity == nullptr);
  41. ValidateNestedInstanceNotUnderInstance(GetRootContainerEntityId(), prefabInstanceAlias);
  42. }
  43. TEST_F(PrefabDeleteTests, DeleteEntitiesAndAllDescendantsInInstance_DeletingEntityDeletesChildEntityToo)
  44. {
  45. const AZStd::string parentEntityName = "ParentEntity";
  46. const AZStd::string childEntityName = "ChildEntity";
  47. AZ::EntityId parentEntityId = CreateEditorEntityUnderRoot(parentEntityName);
  48. AZ::EntityId childEntityId = CreateEditorEntity(childEntityName, parentEntityId);
  49. EntityAlias parentEntityAlias = FindEntityAliasInInstance(GetRootContainerEntityId(), parentEntityName);
  50. EntityAlias childEntityAlias = FindEntityAliasInInstance(GetRootContainerEntityId(), childEntityName);
  51. // Delete the parent entity.
  52. PrefabOperationResult result = m_prefabPublicInterface->DeleteEntitiesAndAllDescendantsInInstance({ parentEntityId });
  53. ASSERT_TRUE(result.IsSuccess());
  54. // Verify that both the parent and its child entity are deleted.
  55. AZ::Entity* parentEntity = AzToolsFramework::GetEntityById(parentEntityId);
  56. EXPECT_TRUE(parentEntity == nullptr);
  57. AZ::Entity* childEntity = AzToolsFramework::GetEntityById(childEntityId);
  58. EXPECT_TRUE(childEntity == nullptr);
  59. ValidateEntityNotUnderInstance(GetRootContainerEntityId(), parentEntityAlias);
  60. ValidateEntityNotUnderInstance(GetRootContainerEntityId(), childEntityAlias);
  61. }
  62. TEST_F(PrefabDeleteTests, DeleteEntitiesAndAllDescendantsInInstance_DeletingEntityDeletesChildPrefabToo)
  63. {
  64. const AZStd::string parentEntityName = "ParentEntity";
  65. const AZStd::string childEntityName = "ChildEntity";
  66. const AZStd::string childPrefabName = "ChildPrefab";
  67. AZ::IO::Path engineRootPath;
  68. m_settingsRegistryInterface->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  69. AZ::IO::Path prefabFilepath(engineRootPath);
  70. prefabFilepath.Append(childPrefabName);
  71. AZ::EntityId parentEntityId = CreateEditorEntityUnderRoot(parentEntityName);
  72. EntityAlias parentEntityAlias = FindEntityAliasInInstance(GetRootContainerEntityId(), parentEntityName);
  73. AZ::EntityId childEntityId = CreateEditorEntity(childEntityName, parentEntityId);
  74. AZ::EntityId childContainerId = CreateEditorPrefab(prefabFilepath, { childEntityId });
  75. InstanceAlias childInstanceAlias = FindNestedInstanceAliasInInstance(GetRootContainerEntityId(), childPrefabName);
  76. // Delete the parent entity.
  77. PrefabOperationResult result = m_prefabPublicInterface->DeleteEntitiesAndAllDescendantsInInstance({ parentEntityId });
  78. ASSERT_TRUE(result.IsSuccess());
  79. // Validate that the parent and the prefab instance under it are deleted.
  80. AZ::Entity* parentEntity = AzToolsFramework::GetEntityById(parentEntityId);
  81. EXPECT_TRUE(parentEntity == nullptr);
  82. AZ::Entity* childContainerEntity = AzToolsFramework::GetEntityById(childContainerId);
  83. EXPECT_TRUE(childContainerEntity == nullptr);
  84. ValidateEntityNotUnderInstance(GetRootContainerEntityId(), parentEntityAlias);
  85. ValidateNestedInstanceNotUnderInstance(GetRootContainerEntityId(), childPrefabName);
  86. }
  87. } // namespace UnitTest