PrefabUndoComponentPropertyOverrideTests.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/PrefabUndoComponentPropertyTestFixture.h>
  9. #include <AzToolsFramework/Prefab/Undo/PrefabUndoComponentPropertyOverride.h>
  10. namespace UnitTest
  11. {
  12. using PrefabUndoComponentPropertyOverrideTests = PrefabUndoComponentPropertyTestFixture;
  13. TEST_F(PrefabUndoComponentPropertyOverrideTests, EditTransformStaticSucceeds)
  14. {
  15. InstanceInfo carInstanceInfo;
  16. EntityInfo wheelEntityInfo;
  17. CreateCarPrefabHierarchy(carInstanceInfo, wheelEntityInfo);
  18. const bool defaultStaticValue = false;
  19. const bool overriddenStaticValue = true;
  20. // Modify the IsStatic property in transform component as override
  21. // Note: Entity needs to be modified manually since we would update cached instance DOM in undo node to avoid reloading
  22. AZ::TransformBus::Event(wheelEntityInfo.m_entityId, &AZ::TransformInterface::SetIsStaticTransform, overriddenStaticValue);
  23. PropertyChangePatch changePatch = MakeTransformStaticPropertyChangePatch(wheelEntityInfo.m_entityId, overriddenStaticValue);
  24. PrefabDom propertyDomValue;
  25. ConvertToPrefabDomValue(propertyDomValue, changePatch.m_propertyValue);
  26. AZStd::string wheelEntityAliasPath = m_instanceToTemplateInterface->GenerateEntityAliasPath(wheelEntityInfo.m_entityId);
  27. AZ::Dom::Path pathToPropertyFromOwningPrefab = AZ::Dom::Path(wheelEntityAliasPath) / changePatch.m_pathToPropertyFromOwningEntity;
  28. // Create an undo node
  29. PrefabUndoComponentPropertyOverride undoNode("Modify transform static");
  30. InstanceOptionalReference carInstance = m_instanceEntityMapperInterface->FindOwningInstance(carInstanceInfo.m_containerEntityId);
  31. EXPECT_TRUE(carInstance.has_value());
  32. // Redo
  33. undoNode.CaptureAndRedo(carInstance->get(), pathToPropertyFromOwningPrefab, propertyDomValue);
  34. EXPECT_TRUE(undoNode.Changed());
  35. PropagateAllTemplateChanges();
  36. bool currentStaticValue = defaultStaticValue; // default to opposite value
  37. AZ::TransformBus::EventResult(currentStaticValue, wheelEntityInfo.m_entityId, &AZ::TransformBus::Events::IsStaticTransform);
  38. EXPECT_EQ(currentStaticValue, overriddenStaticValue);
  39. EXPECT_TRUE(m_prefabOverridePublicInterface->AreOverridesPresent(
  40. wheelEntityInfo.m_entityId, changePatch.m_pathToPropertyFromOwningEntity.ToString()));
  41. // Undo
  42. undoNode.Undo();
  43. PropagateAllTemplateChanges();
  44. currentStaticValue = overriddenStaticValue; // reset to opposite value
  45. AZ::TransformBus::EventResult(currentStaticValue, wheelEntityInfo.m_entityId, &AZ::TransformBus::Events::IsStaticTransform);
  46. EXPECT_EQ(currentStaticValue, defaultStaticValue);
  47. EXPECT_FALSE(m_prefabOverridePublicInterface->AreOverridesPresent(
  48. wheelEntityInfo.m_entityId, changePatch.m_pathToPropertyFromOwningEntity.ToString()));
  49. // Redo
  50. undoNode.Redo();
  51. PropagateAllTemplateChanges();
  52. currentStaticValue = defaultStaticValue; // reset to opposite value
  53. AZ::TransformBus::EventResult(currentStaticValue, wheelEntityInfo.m_entityId, &AZ::TransformBus::Events::IsStaticTransform);
  54. EXPECT_EQ(currentStaticValue, overriddenStaticValue);
  55. EXPECT_TRUE(m_prefabOverridePublicInterface->AreOverridesPresent(
  56. wheelEntityInfo.m_entityId, changePatch.m_pathToPropertyFromOwningEntity.ToString()));
  57. }
  58. TEST_F(PrefabUndoComponentPropertyOverrideTests, EditTransformStaticNoOverrideForUnchangedDefaultValue)
  59. {
  60. // This test validates that changing from default value 'false' to 'false' would not incur an override edit.
  61. InstanceInfo carInstanceInfo;
  62. EntityInfo wheelEntityInfo;
  63. CreateCarPrefabHierarchy(carInstanceInfo, wheelEntityInfo);
  64. const bool defaultStaticValue = false;
  65. // Modify the IsStatic property in transform component as override
  66. AZ::TransformBus::Event(wheelEntityInfo.m_entityId, &AZ::TransformInterface::SetIsStaticTransform, defaultStaticValue);
  67. PropertyChangePatch changePatch = MakeTransformStaticPropertyChangePatch(wheelEntityInfo.m_entityId, defaultStaticValue);
  68. PrefabDom propertyDomValue;
  69. ConvertToPrefabDomValue(propertyDomValue, changePatch.m_propertyValue);
  70. AZStd::string wheelEntityAliasPath = m_instanceToTemplateInterface->GenerateEntityAliasPath(wheelEntityInfo.m_entityId);
  71. AZ::Dom::Path pathToPropertyFromOwningPrefab = AZ::Dom::Path(wheelEntityAliasPath) / changePatch.m_pathToPropertyFromOwningEntity;
  72. // Create an undo node
  73. PrefabUndoComponentPropertyOverride undoNode("Modify transform static to default value");
  74. InstanceOptionalReference carInstance = m_instanceEntityMapperInterface->FindOwningInstance(carInstanceInfo.m_containerEntityId);
  75. EXPECT_TRUE(carInstance.has_value());
  76. // Redo
  77. undoNode.CaptureAndRedo(carInstance->get(), pathToPropertyFromOwningPrefab, propertyDomValue);
  78. EXPECT_FALSE(undoNode.Changed());
  79. PropagateAllTemplateChanges();
  80. bool currentStaticValue = true; // default to opposite value
  81. AZ::TransformBus::EventResult(currentStaticValue, wheelEntityInfo.m_entityId, &AZ::TransformBus::Events::IsStaticTransform);
  82. EXPECT_EQ(currentStaticValue, defaultStaticValue);
  83. // Validate that there is no override created.
  84. EXPECT_FALSE(m_prefabOverridePublicInterface->AreOverridesPresent(
  85. wheelEntityInfo.m_entityId, changePatch.m_pathToPropertyFromOwningEntity.ToString()));
  86. // Undo
  87. undoNode.Undo();
  88. PropagateAllTemplateChanges();
  89. currentStaticValue = true; // reset to opposite value
  90. AZ::TransformBus::EventResult(currentStaticValue, wheelEntityInfo.m_entityId, &AZ::TransformBus::Events::IsStaticTransform);
  91. EXPECT_EQ(currentStaticValue, defaultStaticValue);
  92. // Validate that there is no override created.
  93. EXPECT_FALSE(m_prefabOverridePublicInterface->AreOverridesPresent(
  94. wheelEntityInfo.m_entityId, changePatch.m_pathToPropertyFromOwningEntity.ToString()));
  95. // Redo
  96. undoNode.Redo();
  97. PropagateAllTemplateChanges();
  98. currentStaticValue = true; // reset to opposite value
  99. AZ::TransformBus::EventResult(currentStaticValue, wheelEntityInfo.m_entityId, &AZ::TransformBus::Events::IsStaticTransform);
  100. EXPECT_EQ(currentStaticValue, defaultStaticValue);
  101. // Validate that there is no override created.
  102. EXPECT_FALSE(m_prefabOverridePublicInterface->AreOverridesPresent(
  103. wheelEntityInfo.m_entityId, changePatch.m_pathToPropertyFromOwningEntity.ToString()));
  104. }
  105. TEST_F(PrefabUndoComponentPropertyOverrideTests, EditTranslationSucceeds)
  106. {
  107. InstanceInfo carInstanceInfo;
  108. EntityInfo wheelEntityInfo;
  109. CreateCarPrefabHierarchy(carInstanceInfo, wheelEntityInfo);
  110. const AZ::Vector3 defaultTranslation(0.0f, 0.0f, 0.0f);
  111. const AZ::Vector3 overriddenTranslation(10.0f, 20.0f, 0.0f);
  112. // Modify the local translation property in transform component as override
  113. // Note: Entity needs to be modified manually since we would update cached instance DOM in undo node to avoid reloading
  114. AZ::TransformBus::Event(wheelEntityInfo.m_entityId, &AZ::TransformInterface::SetLocalTranslation, overriddenTranslation);
  115. PropertyChangePatch changePatch = MakeTransformTranslationPropertyChangePatch(wheelEntityInfo.m_entityId, overriddenTranslation);
  116. PrefabDom propertyDomValue;
  117. ConvertToPrefabDomValue(propertyDomValue, changePatch.m_propertyValue);
  118. AZStd::string wheelEntityAliasPath = m_instanceToTemplateInterface->GenerateEntityAliasPath(wheelEntityInfo.m_entityId);
  119. AZ::Dom::Path pathToPropertyFromOwningPrefab = AZ::Dom::Path(wheelEntityAliasPath) / changePatch.m_pathToPropertyFromOwningEntity;
  120. // Create an undo node
  121. PrefabUndoComponentPropertyOverride undoNode("Modify transform translation");
  122. InstanceOptionalReference carInstance = m_instanceEntityMapperInterface->FindOwningInstance(carInstanceInfo.m_containerEntityId);
  123. EXPECT_TRUE(carInstance.has_value());
  124. // Redo
  125. undoNode.CaptureAndRedo(carInstance->get(), pathToPropertyFromOwningPrefab, propertyDomValue);
  126. EXPECT_TRUE(undoNode.Changed());
  127. PropagateAllTemplateChanges();
  128. AZ::Vector3 currentWheelTranslate(-1.0f); // init to other value.
  129. AZ::TransformBus::EventResult(currentWheelTranslate, wheelEntityInfo.m_entityId, &AZ::TransformBus::Events::GetLocalTranslation);
  130. EXPECT_EQ(currentWheelTranslate, overriddenTranslation);
  131. EXPECT_TRUE(m_prefabOverridePublicInterface->AreOverridesPresent(
  132. wheelEntityInfo.m_entityId, changePatch.m_pathToPropertyFromOwningEntity.ToString()));
  133. // Undo
  134. undoNode.Undo();
  135. PropagateAllTemplateChanges();
  136. currentWheelTranslate = AZ::Vector3(-1.0f); // reset
  137. AZ::TransformBus::EventResult(currentWheelTranslate, wheelEntityInfo.m_entityId, &AZ::TransformBus::Events::GetLocalTranslation);
  138. EXPECT_EQ(currentWheelTranslate, defaultTranslation);
  139. EXPECT_FALSE(m_prefabOverridePublicInterface->AreOverridesPresent(
  140. wheelEntityInfo.m_entityId, changePatch.m_pathToPropertyFromOwningEntity.ToString()));
  141. // Redo
  142. undoNode.Redo();
  143. PropagateAllTemplateChanges();
  144. currentWheelTranslate = AZ::Vector3(-1.0f); // reset
  145. AZ::TransformBus::EventResult(currentWheelTranslate, wheelEntityInfo.m_entityId, &AZ::TransformBus::Events::GetLocalTranslation);
  146. EXPECT_EQ(currentWheelTranslate, overriddenTranslation);
  147. EXPECT_TRUE(m_prefabOverridePublicInterface->AreOverridesPresent(
  148. wheelEntityInfo.m_entityId, changePatch.m_pathToPropertyFromOwningEntity.ToString()));
  149. }
  150. TEST_F(PrefabUndoComponentPropertyOverrideTests, EditTranslationOnEntityInNestedPrefabSucceeds)
  151. {
  152. const AZ::Vector3 defaultTranslation(0.0f, 0.0f, 0.0f);
  153. const AZ::Vector3 overriddenTranslation(5.0f, 10.0f, 15.0f);
  154. InstanceInfo superCarInstanceInfo;
  155. InstanceInfo carInstanceInfo;
  156. EntityInfo wheelEntityInfo;
  157. CreateSuperCarPrefabHierarchy(superCarInstanceInfo, carInstanceInfo, wheelEntityInfo);
  158. // Modify the transform component as override
  159. AZ::TransformBus::Event(wheelEntityInfo.m_entityId, &AZ::TransformInterface::SetLocalTranslation, overriddenTranslation);
  160. PropertyChangePatch changePatch = MakeTransformTranslationPropertyChangePatch(wheelEntityInfo.m_entityId, overriddenTranslation);
  161. PrefabDom propertyDomValue;
  162. ConvertToPrefabDomValue(propertyDomValue, changePatch.m_propertyValue);
  163. AZStd::string wheelEntityAliasPath = m_instanceToTemplateInterface->GenerateEntityAliasPath(wheelEntityInfo.m_entityId);
  164. AZ::Dom::Path pathToPropertyFromOwningPrefab = AZ::Dom::Path(wheelEntityAliasPath) / changePatch.m_pathToPropertyFromOwningEntity;
  165. // Create an undo node
  166. PrefabUndoComponentPropertyOverride undoNode("Modify transform translation");
  167. InstanceOptionalReference carInstance = m_instanceEntityMapperInterface->FindOwningInstance(carInstanceInfo.m_containerEntityId);
  168. EXPECT_TRUE(carInstance.has_value());
  169. // Redo
  170. undoNode.CaptureAndRedo(carInstance->get(), pathToPropertyFromOwningPrefab, propertyDomValue);
  171. EXPECT_TRUE(undoNode.Changed());
  172. PropagateAllTemplateChanges();
  173. AZ::Vector3 currentWheelTranslate(-1.0f); // init to other value
  174. AZ::TransformBus::EventResult(currentWheelTranslate, wheelEntityInfo.m_entityId, &AZ::TransformBus::Events::GetLocalTranslation);
  175. EXPECT_EQ(currentWheelTranslate, overriddenTranslation);
  176. EXPECT_TRUE(m_prefabOverridePublicInterface->AreOverridesPresent(
  177. wheelEntityInfo.m_entityId, changePatch.m_pathToPropertyFromOwningEntity.ToString()));
  178. // Undo
  179. undoNode.Undo();
  180. PropagateAllTemplateChanges();
  181. currentWheelTranslate = AZ::Vector3(-1.0f); // reset
  182. AZ::TransformBus::EventResult(currentWheelTranslate, wheelEntityInfo.m_entityId, &AZ::TransformBus::Events::GetLocalTranslation);
  183. EXPECT_EQ(currentWheelTranslate, defaultTranslation);
  184. EXPECT_FALSE(m_prefabOverridePublicInterface->AreOverridesPresent(
  185. wheelEntityInfo.m_entityId, changePatch.m_pathToPropertyFromOwningEntity.ToString()));
  186. // Redo
  187. undoNode.Redo();
  188. PropagateAllTemplateChanges();
  189. currentWheelTranslate = AZ::Vector3(-1.0f); // reset
  190. AZ::TransformBus::EventResult(currentWheelTranslate, wheelEntityInfo.m_entityId, &AZ::TransformBus::Events::GetLocalTranslation);
  191. EXPECT_EQ(currentWheelTranslate, overriddenTranslation);
  192. EXPECT_TRUE(m_prefabOverridePublicInterface->AreOverridesPresent(
  193. wheelEntityInfo.m_entityId, changePatch.m_pathToPropertyFromOwningEntity.ToString()));
  194. }
  195. TEST_F(PrefabUndoComponentPropertyOverrideTests, EditTranslationOverridePersistsAfterChangingBackToDefault)
  196. {
  197. // This test validates that override edit should persist when changing an overridden value '[10, 20, 0]' back
  198. // to its default value '[0, 0, 0]'.
  199. InstanceInfo carInstanceInfo;
  200. EntityInfo wheelEntityInfo;
  201. CreateCarPrefabHierarchy(carInstanceInfo, wheelEntityInfo);
  202. const AZ::Vector3 defaultTranslation(0.0f, 0.0f, 0.0f);
  203. const AZ::Vector3 overriddenTranslation(10.0f, 20.0f, 0.0f);
  204. // Modify the transform component as override
  205. AZ::TransformBus::Event(wheelEntityInfo.m_entityId, &AZ::TransformInterface::SetLocalTranslation, overriddenTranslation);
  206. PropertyChangePatch changePatch = MakeTransformTranslationPropertyChangePatch(wheelEntityInfo.m_entityId, overriddenTranslation);
  207. PrefabDom propertyDomValue;
  208. ConvertToPrefabDomValue(propertyDomValue, changePatch.m_propertyValue);
  209. AZStd::string wheelEntityAliasPath = m_instanceToTemplateInterface->GenerateEntityAliasPath(wheelEntityInfo.m_entityId);
  210. AZ::Dom::Path pathToPropertyFromOwningPrefab = AZ::Dom::Path(wheelEntityAliasPath) / changePatch.m_pathToPropertyFromOwningEntity;
  211. // Create an undo node
  212. PrefabUndoComponentPropertyOverride undoNode("Modify transform translation to non-default value");
  213. InstanceOptionalReference carInstance = m_instanceEntityMapperInterface->FindOwningInstance(carInstanceInfo.m_containerEntityId);
  214. EXPECT_TRUE(carInstance.has_value());
  215. // Redo
  216. undoNode.CaptureAndRedo(carInstance->get(), pathToPropertyFromOwningPrefab, propertyDomValue);
  217. EXPECT_TRUE(undoNode.Changed());
  218. PropagateAllTemplateChanges();
  219. AZ::Vector3 currentWheelTranslate(-1.0f); // init to other value
  220. AZ::TransformBus::EventResult(currentWheelTranslate, wheelEntityInfo.m_entityId, &AZ::TransformBus::Events::GetLocalTranslation);
  221. EXPECT_EQ(currentWheelTranslate, overriddenTranslation);
  222. EXPECT_TRUE(m_prefabOverridePublicInterface->AreOverridesPresent(
  223. wheelEntityInfo.m_entityId, changePatch.m_pathToPropertyFromOwningEntity.ToString()));
  224. // Change back to default value
  225. AZ::TransformBus::Event(wheelEntityInfo.m_entityId, &AZ::TransformInterface::SetLocalTranslation, defaultTranslation);
  226. PropertyChangePatch changePatchToDefault =
  227. MakeTransformTranslationPropertyChangePatch(wheelEntityInfo.m_entityId, defaultTranslation);
  228. PrefabDom defaultPropertyDomValue;
  229. ConvertToPrefabDomValue(defaultPropertyDomValue, changePatchToDefault.m_propertyValue);
  230. PrefabUndoComponentPropertyOverride undoNodeToDefault("Modify transform translation to default value");
  231. undoNodeToDefault.CaptureAndRedo(
  232. carInstance->get(), changePatchToDefault.m_pathToPropertyFromOwningEntity, defaultPropertyDomValue);
  233. PropagateAllTemplateChanges();
  234. currentWheelTranslate = AZ::Vector3(-1.0f); // reset
  235. AZ::TransformBus::EventResult(currentWheelTranslate, wheelEntityInfo.m_entityId, &AZ::TransformBus::Events::GetLocalTranslation);
  236. // Validate the value changes back to default and the override patch still persists.
  237. EXPECT_EQ(currentWheelTranslate, defaultTranslation);
  238. EXPECT_TRUE(m_prefabOverridePublicInterface->AreOverridesPresent(
  239. wheelEntityInfo.m_entityId, changePatch.m_pathToPropertyFromOwningEntity.ToString()));
  240. }
  241. } // namespace UnitTest