PrefabUndoDeleteTests.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/Prefab/Undo/PrefabUndo.h>
  10. #include <AzToolsFramework/Prefab/Undo/PrefabUndoDelete.h>
  11. #include <AzToolsFramework/Prefab/Undo/PrefabUndoDeleteAsOverride.h>
  12. namespace UnitTest
  13. {
  14. using PrefabUndoDeleteTests = PrefabTestFixture;
  15. TEST_F(PrefabUndoDeleteTests, PrefabUndoDeleteTests_DeleteEntity)
  16. {
  17. // Level
  18. // | Car <-- focused
  19. // | Tire <-- delete
  20. // | Car
  21. // | Tire
  22. const AZStd::string carPrefabName = "Car";
  23. const AZStd::string tireEntityName = "Tire";
  24. AZ::IO::Path engineRootPath;
  25. m_settingsRegistryInterface->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  26. AZ::IO::Path carPrefabFilepath(engineRootPath);
  27. carPrefabFilepath.Append(carPrefabName);
  28. AZ::EntityId tireEntityId = CreateEditorEntity(tireEntityName, GetRootContainerEntityId());
  29. AZ::EntityId carContainerId = CreateEditorPrefab(carPrefabFilepath, { tireEntityId });
  30. EntityAlias tireEntityAlias = FindEntityAliasInInstance(carContainerId, tireEntityName);
  31. ASSERT_FALSE(tireEntityAlias.empty());
  32. AZ::EntityId secondCarContainerId = InstantiateEditorPrefab(carPrefabFilepath, GetRootContainerEntityId());
  33. auto firstCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(carContainerId);
  34. ASSERT_TRUE(firstCarInstance.has_value());
  35. auto secondCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(secondCarContainerId);
  36. ASSERT_TRUE(secondCarInstance.has_value());
  37. // Validate before deletion
  38. ValidateEntityUnderInstance(firstCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
  39. ValidateEntityUnderInstance(secondCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
  40. // Create an undo node
  41. PrefabUndoDeleteEntity undoDeleteNode("Undo Deleting Entity");
  42. EntityOptionalReference parentEntityToUpdate = firstCarInstance->get().GetContainerEntity();
  43. ASSERT_TRUE(parentEntityToUpdate.has_value());
  44. AZ::EntityId firstTireEntityId = firstCarInstance->get().GetEntityId(tireEntityAlias);
  45. ASSERT_TRUE(firstTireEntityId.IsValid());
  46. AZStd::string firstTireEntityAliasPath = m_instanceToTemplateInterface->GenerateEntityAliasPath(firstTireEntityId);
  47. firstCarInstance->get().DetachEntity(firstTireEntityId).reset();
  48. undoDeleteNode.Capture({ firstTireEntityAliasPath }, { &(parentEntityToUpdate->get()) }, firstCarInstance->get());
  49. // Redo
  50. undoDeleteNode.Redo();
  51. PropagateAllTemplateChanges();
  52. ValidateEntityNotUnderInstance(firstCarInstance->get().GetContainerEntityId(), tireEntityAlias);
  53. ValidateEntityNotUnderInstance(secondCarInstance->get().GetContainerEntityId(), tireEntityAlias);
  54. // Undo
  55. undoDeleteNode.Undo();
  56. PropagateAllTemplateChanges();
  57. ValidateEntityUnderInstance(firstCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
  58. ValidateEntityUnderInstance(secondCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
  59. }
  60. TEST_F(PrefabUndoDeleteTests, PrefabUndoDeleteTests_DeleteEntityAsOverride)
  61. {
  62. // Level <-- focused
  63. // | Car
  64. // | Tire <-- delete
  65. // | Car
  66. // | Tire
  67. const AZStd::string carPrefabName = "Car";
  68. const AZStd::string tireEntityName = "Tire";
  69. AZ::IO::Path engineRootPath;
  70. m_settingsRegistryInterface->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  71. AZ::IO::Path carPrefabFilepath(engineRootPath);
  72. carPrefabFilepath.Append(carPrefabName);
  73. AZ::EntityId tireEntityId = CreateEditorEntity(tireEntityName, GetRootContainerEntityId());
  74. AZ::EntityId carContainerId = CreateEditorPrefab(carPrefabFilepath, { tireEntityId });
  75. EntityAlias tireEntityAlias = FindEntityAliasInInstance(carContainerId, tireEntityName);
  76. ASSERT_FALSE(tireEntityAlias.empty());
  77. AZ::EntityId secondCarContainerId = InstantiateEditorPrefab(carPrefabFilepath, GetRootContainerEntityId());
  78. auto firstCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(carContainerId);
  79. ASSERT_TRUE(firstCarInstance.has_value());
  80. auto secondCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(secondCarContainerId);
  81. ASSERT_TRUE(secondCarInstance.has_value());
  82. // Validate before deletion
  83. ValidateEntityUnderInstance(firstCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
  84. ValidateEntityUnderInstance(secondCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
  85. // Create an undo node
  86. PrefabUndoDeleteAsOverride undoDeleteNode("Undo Deleting Entity As Override");
  87. EntityOptionalReference parentEntityToUpdate = firstCarInstance->get().GetContainerEntity();
  88. ASSERT_TRUE(parentEntityToUpdate.has_value());
  89. AZ::EntityId firstTireEntityId = firstCarInstance->get().GetEntityId(tireEntityAlias);
  90. ASSERT_TRUE(firstTireEntityId.IsValid());
  91. AZStd::string firstTireEntityAliasPath = m_instanceToTemplateInterface->GenerateEntityAliasPath(firstTireEntityId);
  92. firstCarInstance->get().DetachEntity(firstTireEntityId).reset();
  93. auto levelRootInstance = m_instanceEntityMapperInterface->FindOwningInstance(GetRootContainerEntityId());
  94. ASSERT_TRUE(levelRootInstance.has_value());
  95. undoDeleteNode.Capture({ firstTireEntityAliasPath }, {}, { &(parentEntityToUpdate->get()) },
  96. firstCarInstance->get(), levelRootInstance->get());
  97. // Redo
  98. undoDeleteNode.Redo();
  99. PropagateAllTemplateChanges();
  100. ValidateEntityNotUnderInstance(firstCarInstance->get().GetContainerEntityId(), tireEntityAlias);
  101. ValidateEntityUnderInstance(secondCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
  102. // Undo
  103. undoDeleteNode.Undo();
  104. PropagateAllTemplateChanges();
  105. ValidateEntityUnderInstance(firstCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
  106. ValidateEntityUnderInstance(secondCarInstance->get().GetContainerEntityId(), tireEntityAlias, tireEntityName);
  107. }
  108. TEST_F(PrefabUndoDeleteTests, PrefabUndoDeleteTests_DeleteNestedInstance)
  109. {
  110. // Level
  111. // | Car <-- focused
  112. // | Wheel <-- delete
  113. // | Tire
  114. // | Car
  115. // | Wheel
  116. // | Tire
  117. const AZStd::string carPrefabName = "Car";
  118. const AZStd::string wheelPrefabName = "Wheel";
  119. const AZStd::string tireEntityName = "Tire";
  120. AZ::IO::Path engineRootPath;
  121. m_settingsRegistryInterface->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  122. AZ::IO::Path carPrefabFilepath(engineRootPath);
  123. carPrefabFilepath.Append(carPrefabName);
  124. AZ::IO::Path wheelPrefabFilepath(engineRootPath);
  125. wheelPrefabFilepath.Append(wheelPrefabName);
  126. AZ::EntityId tireEntityId = CreateEditorEntity(tireEntityName, GetRootContainerEntityId());
  127. AZ::EntityId wheelContainerId = CreateEditorPrefab(wheelPrefabFilepath, { tireEntityId });
  128. AZ::EntityId carContainerId = CreateEditorPrefab(carPrefabFilepath, { wheelContainerId });
  129. InstanceAlias wheelInstanceAlias = FindNestedInstanceAliasInInstance(carContainerId, wheelPrefabName);
  130. ASSERT_FALSE(wheelInstanceAlias.empty());
  131. AZ::EntityId secondCarContainerId = InstantiateEditorPrefab(carPrefabFilepath, GetRootContainerEntityId());
  132. auto firstCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(carContainerId);
  133. ASSERT_TRUE(firstCarInstance.has_value());
  134. auto secondCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(secondCarContainerId);
  135. ASSERT_TRUE(secondCarInstance.has_value());
  136. // Validate before deletion
  137. ValidateNestedInstanceUnderInstance(firstCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
  138. ValidateNestedInstanceUnderInstance(secondCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
  139. // Create an undo node
  140. // Note: Currently for deleting a nested instance, it depends on the PrefabUndoInstanceLink undo node.
  141. PrefabUndoInstanceLink undoDeleteNestedInstance("Undo Delete Nested Instance");
  142. AZStd::unique_ptr<Instance> firstWheelInstance = firstCarInstance->get().DetachNestedInstance(wheelInstanceAlias);
  143. TemplateId wheelTemplateId = firstWheelInstance->GetTemplateId();
  144. LinkId wheelLinkId = firstWheelInstance->GetLinkId();
  145. LinkReference firstWheelLink = m_prefabSystemComponent->FindLink(wheelLinkId);
  146. ASSERT_TRUE(firstWheelLink.has_value());
  147. // Generate link patches needed for redo and undo support.
  148. PrefabDom patchesCopyForUndoSupport;
  149. PrefabDom wheelInstanceLinkDom;
  150. firstWheelLink->get().GetLinkDom(wheelInstanceLinkDom, wheelInstanceLinkDom.GetAllocator());
  151. PrefabDomValueReference wheelInstanceLinkPatches =
  152. PrefabDomUtils::FindPrefabDomValue(wheelInstanceLinkDom, PrefabDomUtils::PatchesName);
  153. if (wheelInstanceLinkPatches.has_value())
  154. {
  155. patchesCopyForUndoSupport.CopyFrom(wheelInstanceLinkPatches->get(), patchesCopyForUndoSupport.GetAllocator());
  156. }
  157. firstWheelInstance.reset();
  158. undoDeleteNestedInstance.Capture(firstCarInstance->get().GetTemplateId(), wheelTemplateId,
  159. wheelInstanceAlias, AZStd::move(patchesCopyForUndoSupport), wheelLinkId);
  160. // Redo
  161. undoDeleteNestedInstance.Redo();
  162. PropagateAllTemplateChanges();
  163. ValidateNestedInstanceNotUnderInstance(firstCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
  164. ValidateNestedInstanceNotUnderInstance(secondCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
  165. // Undo
  166. undoDeleteNestedInstance.Undo();
  167. PropagateAllTemplateChanges();
  168. ValidateNestedInstanceUnderInstance(firstCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
  169. ValidateNestedInstanceUnderInstance(secondCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
  170. }
  171. TEST_F(PrefabUndoDeleteTests, PrefabUndoDeleteTests_DeleteInstanceAsOverride)
  172. {
  173. // Level <-- focused
  174. // | First Car
  175. // | Wheel <-- delete
  176. // | Tire
  177. // | Second Car
  178. // | Wheel
  179. // | Tire
  180. const AZStd::string carPrefabName = "Car";
  181. const AZStd::string wheelPrefabName = "Wheel";
  182. const AZStd::string tireEntityName = "Tire";
  183. AZ::IO::Path engineRootPath;
  184. m_settingsRegistryInterface->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  185. AZ::IO::Path carPrefabFilepath(engineRootPath);
  186. carPrefabFilepath.Append(carPrefabName);
  187. AZ::IO::Path wheelPrefabFilepath(engineRootPath);
  188. wheelPrefabFilepath.Append(wheelPrefabName);
  189. AZ::EntityId tireEntityId = CreateEditorEntity(tireEntityName, GetRootContainerEntityId());
  190. AZ::EntityId wheelContainerId = CreateEditorPrefab(wheelPrefabFilepath, { tireEntityId });
  191. AZ::EntityId carContainerId = CreateEditorPrefab(carPrefabFilepath, { wheelContainerId });
  192. InstanceAlias wheelInstanceAlias = FindNestedInstanceAliasInInstance(carContainerId, wheelPrefabName);
  193. ASSERT_FALSE(wheelInstanceAlias.empty());
  194. AZ::EntityId secondCarContainerId = InstantiateEditorPrefab(carPrefabFilepath, GetRootContainerEntityId());
  195. auto firstCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(carContainerId);
  196. ASSERT_TRUE(firstCarInstance.has_value());
  197. auto secondCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(secondCarContainerId);
  198. ASSERT_TRUE(secondCarInstance.has_value());
  199. // Validate before deletion
  200. ValidateNestedInstanceUnderInstance(firstCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
  201. ValidateNestedInstanceUnderInstance(secondCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
  202. // Create an undo node
  203. PrefabUndoDeleteAsOverride undoDeleteAsOverride("Undo Delete Instance As Override");
  204. auto parentEntityToUpdate = firstCarInstance->get().GetContainerEntity();
  205. ASSERT_TRUE(parentEntityToUpdate.has_value());
  206. const AZStd::string firstWheelInstanceAliasPath = PrefabDomUtils::PathStartingWithInstances + wheelInstanceAlias;
  207. firstCarInstance->get().DetachNestedInstance(wheelInstanceAlias).reset();
  208. auto levelRootInstance = m_instanceEntityMapperInterface->FindOwningInstance(GetRootContainerEntityId());
  209. ASSERT_TRUE(levelRootInstance.has_value());
  210. undoDeleteAsOverride.Capture({}, { firstWheelInstanceAliasPath }, { &(parentEntityToUpdate->get()) },
  211. firstCarInstance->get(), levelRootInstance->get());
  212. // Redo
  213. undoDeleteAsOverride.Redo();
  214. PropagateAllTemplateChanges();
  215. ValidateNestedInstanceNotUnderInstance(firstCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
  216. ValidateNestedInstanceUnderInstance(secondCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
  217. // Undo
  218. undoDeleteAsOverride.Undo();
  219. PropagateAllTemplateChanges();
  220. ValidateNestedInstanceUnderInstance(firstCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
  221. ValidateNestedInstanceUnderInstance(secondCarInstance->get().GetContainerEntityId(), wheelInstanceAlias);
  222. }
  223. } // namespace UnitTest