3
0

SliceStabilityPushTests.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 <Tests/SliceStabilityTests/SliceStabilityTestFramework.h>
  9. namespace UnitTest
  10. {
  11. TEST_F(SliceStabilityTest, PushToSlice_PushSingleEntityToSlice_EntityStateRemainsTheSame_FT)
  12. {
  13. AUTO_RESULT_IF_SETTING_TRUE(UnitTest::prefabSystemSetting, true)
  14. // Create an entity to be used in a slice
  15. AzToolsFramework::EntityIdList liveEntityIds;
  16. AZ::EntityId sliceEntity = CreateEditorEntity("SliceEntity", liveEntityIds);
  17. ASSERT_TRUE(sliceEntity.IsValid());
  18. // Capture current entity state
  19. EXPECT_TRUE(m_validator.Capture(liveEntityIds));
  20. // Create a slice from the entity
  21. AZ::SliceComponent::SliceInstanceAddress sliceInstanceAddress;
  22. ASSERT_TRUE(CreateSlice("NewSlice", liveEntityIds, sliceInstanceAddress).IsValid());
  23. // Compare the generated slice instance against the captured entity state
  24. EXPECT_TRUE(m_validator.Compare(sliceInstanceAddress));
  25. m_validator.Reset();
  26. // Create an entity to be pushed to slice and set its parent to be the first SliceEntity
  27. AZ::EntityId addedEntity = CreateEditorEntity("AddedEntity", liveEntityIds, sliceEntity);
  28. ASSERT_TRUE(addedEntity.IsValid());
  29. // Capture current entity state
  30. EXPECT_TRUE(m_validator.Capture(liveEntityIds));
  31. // Push AddedEntity to the existing slice instance
  32. ASSERT_TRUE(PushEntitiesToSlice(sliceInstanceAddress, liveEntityIds));
  33. // Compare the updated slice instance against the captured entity state
  34. EXPECT_TRUE(m_validator.Compare(sliceInstanceAddress));
  35. }
  36. TEST_F(SliceStabilityTest, PushToSlice_PushSingleParentEntityWithChildEntity_EntityStateRemainsTheSame_FT)
  37. {
  38. AUTO_RESULT_IF_SETTING_TRUE(UnitTest::prefabSystemSetting, true)
  39. // Create an entity to be used as a slice root
  40. AzToolsFramework::EntityIdList liveEntityIds;
  41. AZ::EntityId sliceEntity = CreateEditorEntity("SliceEntity", liveEntityIds);
  42. ASSERT_TRUE(sliceEntity.IsValid());
  43. // Capture current entity state
  44. EXPECT_TRUE(m_validator.Capture(liveEntityIds));
  45. // Create a slice from the current entity state
  46. AZ::SliceComponent::SliceInstanceAddress sliceInstanceAddress;
  47. ASSERT_TRUE(CreateSlice("NewSlice", liveEntityIds, sliceInstanceAddress).IsValid());
  48. // Compare the generated slice instance against the captured entity state
  49. EXPECT_TRUE(m_validator.Compare(sliceInstanceAddress));
  50. m_validator.Reset();
  51. // Create a parent and child entity to be pushed to the slice
  52. // Set AddedParent's parent to be SliceEntity
  53. // Set AddedChild's parent to be AddedParent
  54. AZ::EntityId addedParent = CreateEditorEntity("AddedParent", liveEntityIds, sliceEntity);
  55. ASSERT_TRUE(addedParent.IsValid());
  56. AZ::EntityId addedChild = CreateEditorEntity("AddedChild", liveEntityIds, addedParent);
  57. ASSERT_TRUE(addedChild.IsValid());
  58. // Capture the current entity state
  59. EXPECT_TRUE(m_validator.Capture(liveEntityIds));
  60. // Push AddedParent and AddedChild to the existing slice instance
  61. ASSERT_TRUE(PushEntitiesToSlice(sliceInstanceAddress, liveEntityIds));
  62. // Compare the updated slice instance against the captured entity state
  63. EXPECT_TRUE(m_validator.Compare(sliceInstanceAddress));
  64. }
  65. // Disabled in SPEC-3077
  66. TEST_F(SliceStabilityTest, DISABLED_PushToSlice_PushGrandparentParentChildHierarchy_EntityStateRemainsTheSame_FT)
  67. {
  68. AUTO_RESULT_IF_SETTING_TRUE(UnitTest::prefabSystemSetting, true)
  69. // Create an entity to be used as a slice root
  70. AzToolsFramework::EntityIdList liveEntityIds;
  71. AZ::EntityId sliceEntity = CreateEditorEntity("SliceEntity", liveEntityIds);
  72. ASSERT_TRUE(sliceEntity.IsValid());
  73. // Capture current entity state
  74. EXPECT_TRUE(m_validator.Capture(liveEntityIds));
  75. // Create a slice from current entity state
  76. AZ::SliceComponent::SliceInstanceAddress sliceInstanceAddress;
  77. ASSERT_TRUE(CreateSlice("NewSlice", liveEntityIds, sliceInstanceAddress).IsValid());
  78. // Compare the generated slice instance against the captured entity state
  79. EXPECT_TRUE(m_validator.Compare(sliceInstanceAddress));
  80. m_validator.Reset();
  81. // Create a grandparent->parent->child to be pushed to the slice and connect their parent hierarchy accordingly
  82. AZ::EntityId addedGrandparent = CreateEditorEntity("AddedGrandParent", liveEntityIds, sliceEntity);
  83. ASSERT_TRUE(addedGrandparent.IsValid());
  84. AZ::EntityId addedParent = CreateEditorEntity("AddedParent", liveEntityIds, addedGrandparent);
  85. ASSERT_TRUE(addedParent.IsValid());
  86. AZ::EntityId addedChild = CreateEditorEntity("AddedChild", liveEntityIds, addedParent);
  87. ASSERT_TRUE(addedChild.IsValid());
  88. // Capture current entity state
  89. EXPECT_TRUE(m_validator.Capture(liveEntityIds));
  90. // Push grandparent, parent, and child to slice
  91. ASSERT_TRUE(PushEntitiesToSlice(sliceInstanceAddress, liveEntityIds));
  92. // Compare the updated slice instance against the captured entity state
  93. EXPECT_TRUE(m_validator.Compare(sliceInstanceAddress));
  94. }
  95. TEST_F(SliceStabilityTest, PushToSlice_Push10DeepParentChildHierarchy_EntityStateRemainsTheSame_FT)
  96. {
  97. AUTO_RESULT_IF_SETTING_TRUE(UnitTest::prefabSystemSetting, true)
  98. // Create an entity to be used as a slice root
  99. AzToolsFramework::EntityIdList liveEntityIds;
  100. AZ::EntityId sliceEntity = CreateEditorEntity("SliceEntity", liveEntityIds);
  101. ASSERT_TRUE(sliceEntity.IsValid());
  102. // Capture current entity state
  103. EXPECT_TRUE(m_validator.Capture(liveEntityIds));
  104. // Create a slice from current entity state
  105. AZ::SliceComponent::SliceInstanceAddress sliceInstanceAddress;
  106. ASSERT_TRUE(CreateSlice("NewSlice", liveEntityIds, sliceInstanceAddress).IsValid());
  107. // Compare the generated slice instance against the captured entity state
  108. EXPECT_TRUE(m_validator.Compare(sliceInstanceAddress));
  109. m_validator.Reset();
  110. // Generate 10 new entities and set each entity's parent to be the entity generated before them
  111. // This creates a 10 child deep hierarchy that we will push to slice
  112. AZ::EntityId parent = sliceEntity;
  113. for (size_t entityCounter = 0; entityCounter < 10; ++entityCounter)
  114. {
  115. parent = CreateEditorEntity(AZStd::string::format("Added Entity Level %zu", entityCounter).c_str(), liveEntityIds, parent);
  116. ASSERT_TRUE(parent.IsValid());
  117. }
  118. // Capture the current entity state
  119. EXPECT_TRUE(m_validator.Capture(liveEntityIds));
  120. // Push the newly created entities into the existing slice
  121. ASSERT_TRUE(PushEntitiesToSlice(sliceInstanceAddress, liveEntityIds));
  122. // Compare the updated slice instance against the captured entity state
  123. EXPECT_TRUE(m_validator.Compare(sliceInstanceAddress));
  124. }
  125. TEST_F(SliceStabilityTest, PushToSlice_Push10Children_EntityStateRemainsTheSame_FT)
  126. {
  127. AUTO_RESULT_IF_SETTING_TRUE(UnitTest::prefabSystemSetting, true)
  128. // Create an entity to be used as a slice root
  129. AzToolsFramework::EntityIdList liveEntityIds;
  130. AZ::EntityId sliceEntity = CreateEditorEntity("SliceEntity", liveEntityIds);
  131. ASSERT_TRUE(sliceEntity.IsValid());
  132. // Capture current entity state
  133. EXPECT_TRUE(m_validator.Capture(liveEntityIds));
  134. // Create a slice from current entity state
  135. AZ::SliceComponent::SliceInstanceAddress sliceInstanceAddress;
  136. ASSERT_TRUE(CreateSlice("NewSlice", liveEntityIds, sliceInstanceAddress).IsValid());
  137. // Compare the generated slice instance against the captured entity state
  138. EXPECT_TRUE(m_validator.Compare(sliceInstanceAddress));
  139. m_validator.Reset();
  140. AZ::EntityId addedEntity;
  141. AzToolsFramework::EntityIdList entitiesToPush;
  142. for (size_t childEntityCounter = 0; childEntityCounter < 10; ++childEntityCounter)
  143. {
  144. // Generate a set of children who share the same parent (SliceEntity) and add them to the list of entities to push
  145. addedEntity = CreateEditorEntity(AZStd::string::format("Child #%zu", childEntityCounter).c_str(), liveEntityIds, sliceEntity);
  146. ASSERT_TRUE(addedEntity.IsValid());
  147. entitiesToPush.emplace_back(addedEntity);
  148. }
  149. // Capture current entity state
  150. EXPECT_TRUE(m_validator.Capture(liveEntityIds));
  151. // Push the created child entities to the existing slice
  152. ASSERT_TRUE(PushEntitiesToSlice(sliceInstanceAddress, liveEntityIds));
  153. // Compare the updated slice instance against the captured entity state
  154. EXPECT_TRUE(m_validator.Compare(sliceInstanceAddress));
  155. }
  156. TEST_F(SliceStabilityTest, PushToSlice_PushNestedSliceOfDifferentType_EntityStateRemainsTheSame_FT)
  157. {
  158. AUTO_RESULT_IF_SETTING_TRUE(UnitTest::prefabSystemSetting, true)
  159. // Create an entity to be used for Slice1's root
  160. AzToolsFramework::EntityIdList slice1Entities;
  161. AZ::EntityId slice1Root = CreateEditorEntity("slice1Root", slice1Entities);
  162. ASSERT_TRUE(slice1Root.IsValid());
  163. // Capture entity state for slice1Root
  164. EXPECT_TRUE(m_validator.Capture(slice1Entities));
  165. // Create a slice from slice1Root
  166. AZ::SliceComponent::SliceInstanceAddress slice1Instance;
  167. ASSERT_TRUE(CreateSlice("Slice1", slice1Entities, slice1Instance).IsValid());
  168. // Compare the state of slice1Instance to the captured state of slice1Root
  169. EXPECT_TRUE(m_validator.Compare(slice1Instance));
  170. m_validator.Reset();
  171. // Create an entity to be used for Slice2's root and make its parent slice1Root
  172. AzToolsFramework::EntityIdList slice2Entities;
  173. AZ::EntityId slice2Root = CreateEditorEntity("Slice2Root", slice2Entities);
  174. ASSERT_TRUE(slice2Root.IsValid());
  175. // Provide Slice2Root a child entity to confirm all entities in Slice2 are included in the push
  176. ASSERT_TRUE(CreateEditorEntity("Slice2Child", slice2Entities, slice2Root).IsValid());
  177. // Capture entity state for Slice2Root
  178. EXPECT_TRUE(m_validator.Capture(slice2Entities));
  179. // Create a slice from slice2Root
  180. AZ::SliceComponent::SliceInstanceAddress slice2Instance;
  181. ASSERT_TRUE(CreateSlice("Slice2", slice2Entities, slice2Instance).IsValid());
  182. // Compare the state of slice2Instance to the captured state of slice2Root
  183. EXPECT_TRUE(m_validator.Compare(slice2Instance));
  184. m_validator.Reset();
  185. // Parent slice2Root under slice1Root to prepare for the push
  186. ReparentEntity(slice2Root, slice1Root);
  187. // Combine the current entity lists
  188. AzToolsFramework::EntityIdList totalEntities = slice1Entities;
  189. totalEntities.insert(totalEntities.end(), slice2Entities.begin(), slice2Entities.end());
  190. // Capture the total entity hierarchy state
  191. EXPECT_TRUE(m_validator.Capture(totalEntities));
  192. // Push the slice2Root entity into slice1Instance
  193. ASSERT_TRUE(PushEntitiesToSlice(slice1Instance, totalEntities));
  194. // Compare the updated slice instance against the captured entity state
  195. EXPECT_TRUE(m_validator.Compare(slice1Instance));
  196. }
  197. TEST_F(SliceStabilityTest, PushToSliceAndCreateSlice_ValidateCombinationOfPushCreateOperations_EntityStateRemainsTheSame_FT)
  198. {
  199. AUTO_RESULT_IF_SETTING_TRUE(UnitTest::prefabSystemSetting, true)
  200. // Create Slice1 root
  201. AzToolsFramework::EntityIdList slice1Entities;
  202. AZ::EntityId slice1Root = CreateEditorEntity("Slice1Root", slice1Entities);
  203. ASSERT_TRUE(slice1Root.IsValid());
  204. EXPECT_TRUE(m_validator.Capture(slice1Entities));
  205. // Create Slice1 from Slice1 root
  206. AZ::SliceComponent::SliceInstanceAddress slice1Instance;
  207. AZ::Data::AssetId slice1Asset = CreateSlice("Slice1", slice1Entities, slice1Instance);
  208. ASSERT_TRUE(slice1Asset.IsValid());
  209. // Validate that Slice1 instance did not change the structure of Slice1 root
  210. EXPECT_TRUE(m_validator.Compare(slice1Instance));
  211. m_validator.Reset();
  212. // Create Slice1 child and make Slice1 root its parent
  213. AZ::EntityId slice1Child = CreateEditorEntity("Slice1Child", slice1Entities, slice1Root);
  214. ASSERT_TRUE(slice1Child.IsValid());
  215. EXPECT_TRUE(m_validator.Capture(slice1Entities));
  216. // Push Slice1 child to Slice1
  217. ASSERT_TRUE(PushEntitiesToSlice(slice1Instance, slice1Entities));
  218. // Validate that Slice1 root and child did not change during push
  219. EXPECT_TRUE(m_validator.Compare(slice1Instance));
  220. m_validator.Reset();
  221. // Instantiate a second instance of Slice1 and make the original Slice 1 child its parent
  222. AzToolsFramework::EntityIdList secondSlice1InstanceEntities;
  223. ASSERT_TRUE(InstantiateEditorSlice(slice1Asset, secondSlice1InstanceEntities, slice1Child).IsValid());
  224. // Slice 2 entities will be the combination of both Slice1 instances
  225. AzToolsFramework::EntityIdList slice2Entities = slice1Entities;
  226. slice2Entities.insert(slice2Entities.end(), secondSlice1InstanceEntities.begin(), secondSlice1InstanceEntities.end());
  227. EXPECT_TRUE(m_validator.Capture(slice2Entities));
  228. // Create slice 2
  229. AZ::SliceComponent::SliceInstanceAddress slice2Instance;
  230. ASSERT_TRUE(CreateSlice("Slice2", slice2Entities, slice2Instance).IsValid());
  231. // Validate that entities in the Slice 2 instance are structurally the same as the input entities in its creation
  232. EXPECT_TRUE(m_validator.Compare(slice2Instance));
  233. }
  234. }