PrefabInstanceDomGeneratorTestFixture.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/PrefabInstanceDomGeneratorTestFixture.h>
  9. #include <Prefab/PrefabTestDomUtils.h>
  10. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  11. namespace UnitTest
  12. {
  13. void PrefabInstanceDomGeneratorTestFixture::SetUpEditorFixtureImpl()
  14. {
  15. PrefabTestFixture::SetUpEditorFixtureImpl();
  16. m_instanceDomGeneratorInterface = AZ::Interface<InstanceDomGeneratorInterface>::Get();
  17. ASSERT_TRUE(m_instanceDomGeneratorInterface);
  18. m_prefabFocusPublicInterface = AZ::Interface<PrefabFocusPublicInterface>::Get();
  19. ASSERT_TRUE(m_prefabFocusPublicInterface);
  20. SetUpInstanceHierarchy();
  21. }
  22. void PrefabInstanceDomGeneratorTestFixture::SetUpInstanceHierarchy()
  23. {
  24. // Level <-- override WorldX of Tire and WorldX of Wheel container
  25. // | Car <-- override WorldX of Tire
  26. // | Wheel
  27. // | Tire
  28. const AZStd::string carPrefabName = "CarPrefab";
  29. const AZStd::string wheelPrefabName = "WheelPrefab";
  30. const AZStd::string tireEntityName = "Tire";
  31. AZ::IO::Path engineRootPath;
  32. m_settingsRegistryInterface->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  33. AZ::IO::Path carPrefabFilepath = engineRootPath / carPrefabName;
  34. AZ::IO::Path wheelPrefabFilepath = engineRootPath / wheelPrefabName;
  35. // Create the car hierarchy
  36. AZ::EntityId tireEntityId = CreateEditorEntityUnderRoot(tireEntityName);
  37. AZ::EntityId wheelContainerId = CreateEditorPrefab(wheelPrefabFilepath, { tireEntityId });
  38. m_wheelInstance = m_instanceEntityMapperInterface->FindOwningInstance(wheelContainerId);
  39. ASSERT_TRUE(m_wheelInstance.has_value());
  40. // Save the tire alias for further testing
  41. AZStd::vector<EntityAlias> entityAliases = m_wheelInstance->get().GetEntityAliases();
  42. ASSERT_TRUE(!entityAliases.empty());
  43. m_tireAlias = entityAliases[0];
  44. // Save the Wheel instance alias before the Car prefab is created
  45. InstanceAlias wheelInstanceAlias = m_wheelInstance->get().GetInstanceAlias();
  46. AZ::EntityId carContainerId = CreateEditorPrefab(carPrefabFilepath, { wheelContainerId });
  47. m_carInstance = m_instanceEntityMapperInterface->FindOwningInstance(carContainerId);
  48. ASSERT_TRUE(m_carInstance.has_value());
  49. // Reassign the Wheel instance now that it's been recreated by propagation
  50. m_wheelInstance = m_carInstance->get().FindNestedInstance(wheelInstanceAlias);
  51. ASSERT_TRUE(m_wheelInstance.has_value());
  52. // Activate the container entity of the Wheel instance
  53. m_wheelInstance->get().ActivateContainerEntity();
  54. InitializePrefabTemplates();
  55. }
  56. void PrefabInstanceDomGeneratorTestFixture::InitializePrefabTemplates()
  57. {
  58. const Instance& rootInstance = *m_prefabEditorEntityOwnershipInterface->GetRootPrefabInstance();
  59. // Generate a patch that will alter the Tire
  60. PrefabDom entityPatch;
  61. GenerateWorldXEntityPatch(m_tireAlias, m_entityOverrideValueOnLevel, *m_wheelInstance, entityPatch);
  62. // Apply the Tire patch to the Root template (level)
  63. ApplyEntityPatchToTemplate(entityPatch, m_tireAlias, *m_wheelInstance, rootInstance);
  64. // Update the Tire patch and apply it to the Car template
  65. UpdateWorldXEntityPatch(entityPatch, m_entityOverrideValueOnCar);
  66. ApplyEntityPatchToTemplate(entityPatch, m_tireAlias, *m_wheelInstance, m_carInstance->get());
  67. // Update the Tire patch and apply it to the Wheel template
  68. UpdateWorldXEntityPatch(entityPatch, m_entityValueOnWheel);
  69. ApplyEntityPatchToTemplate(entityPatch, m_tireAlias, *m_wheelInstance, *m_wheelInstance);
  70. // Generate a patch that will alter the Wheel's container entity
  71. PrefabDom containerPatch;
  72. GenerateWorldXEntityPatch("", m_wheelContainerOverrideValueOnLevel, *m_wheelInstance, containerPatch);
  73. // Apply the Wheel container patch to the Root template (level)
  74. ApplyEntityPatchToTemplate(containerPatch, "", *m_wheelInstance, rootInstance);
  75. }
  76. void PrefabInstanceDomGeneratorTestFixture::GenerateWorldXEntityPatch(
  77. const EntityAlias& entityAlias,
  78. float updatedXValue,
  79. Instance& owningInstance,
  80. PrefabDom& patchOut)
  81. {
  82. EntityOptionalReference childEntity = entityAlias.empty() ? owningInstance.GetContainerEntity() : owningInstance.GetEntity(entityAlias);
  83. ASSERT_TRUE(childEntity.has_value());
  84. // Create document with before change snapshot
  85. PrefabDom entityDomBeforeUpdate;
  86. m_instanceToTemplateInterface->GenerateEntityDomBySerializing(entityDomBeforeUpdate, *childEntity);
  87. // Change the entity
  88. float prevXValue = 0.0f;
  89. AZ::TransformBus::EventResult(prevXValue, childEntity->get().GetId(), &AZ::TransformInterface::GetWorldX);
  90. AZ::TransformBus::Event(childEntity->get().GetId(), &AZ::TransformInterface::SetWorldX, updatedXValue);
  91. float curXValue = 0.0f;
  92. AZ::TransformBus::EventResult(curXValue, childEntity->get().GetId(), &AZ::TransformInterface::GetWorldX);
  93. EXPECT_EQ(curXValue, updatedXValue);
  94. // Create document with after change snapshot
  95. PrefabDom entityDomAfterUpdate;
  96. m_instanceToTemplateInterface->GenerateEntityDomBySerializing(entityDomAfterUpdate, *childEntity);
  97. // Generate patch
  98. m_instanceToTemplateInterface->GeneratePatch(patchOut, entityDomBeforeUpdate, entityDomAfterUpdate);
  99. // Revert the change to the entity
  100. AZ::TransformBus::Event(childEntity->get().GetId(), &AZ::TransformInterface::SetWorldX, prevXValue);
  101. }
  102. void PrefabInstanceDomGeneratorTestFixture::UpdateWorldXEntityPatch(
  103. PrefabDom& patch,
  104. double newValue)
  105. {
  106. PrefabDomValue::MemberIterator patchEntryIterator = patch[0].FindMember("value");
  107. ASSERT_TRUE(patchEntryIterator != patch[0].MemberEnd());
  108. patchEntryIterator->value.SetDouble(newValue);
  109. }
  110. void PrefabInstanceDomGeneratorTestFixture::ApplyEntityPatchToTemplate(
  111. PrefabDom& patch,
  112. const EntityAlias& entityAlias,
  113. const Instance& owningInstance,
  114. const Instance& targetInstance)
  115. {
  116. // We need to generate a patch prefix so that the path of the patches correctly reflects the hierarchy path
  117. // from the entity to the instance where the patch needs to be added.
  118. AZStd::string patchPrefix;
  119. if (entityAlias.empty())
  120. {
  121. patchPrefix.insert(0, "/ContainerEntity");
  122. }
  123. else
  124. {
  125. patchPrefix.insert(0, "/Entities/" + entityAlias);
  126. }
  127. const Instance* curInstance = &owningInstance;
  128. while (curInstance != &targetInstance)
  129. {
  130. patchPrefix.insert(0, "/Instances/" + curInstance->GetInstanceAlias());
  131. InstanceOptionalConstReference parentInstance = curInstance->GetParentInstance();
  132. ASSERT_TRUE(parentInstance.has_value());
  133. curInstance = &parentInstance->get();
  134. }
  135. PrefabDomValueReference patchPathReference = PrefabDomUtils::FindPrefabDomValue(patch[0], "path");
  136. ASSERT_TRUE(patchPathReference.has_value());
  137. AZStd::string patchPathStringBefore = patchPathReference->get().GetString();
  138. AZStd::string patchPathString = patchPathStringBefore;
  139. patchPathString.insert(0, patchPrefix);
  140. patchPathReference->get().SetString(patchPathString.c_str(), patch.GetAllocator());
  141. // Apply the patch
  142. TemplateId targetTemplateId = targetInstance.GetTemplateId();
  143. PrefabDom& targetTemplateDomReference = m_prefabSystemComponent->FindTemplateDom(targetTemplateId);
  144. AZ::JsonSerializationResult::ResultCode result =
  145. PrefabDomUtils::ApplyPatches(targetTemplateDomReference, targetTemplateDomReference.GetAllocator(), patch);
  146. ASSERT_TRUE(result.GetOutcome() == AZ::JsonSerializationResult::Outcomes::Success);
  147. // Revert back to previous path
  148. patchPathReference->get().SetString(patchPathStringBefore.c_str(), patch.GetAllocator());
  149. }
  150. void PrefabInstanceDomGeneratorTestFixture::GenerateAndValidateInstanceDom(
  151. const Instance& instance, EntityAlias entityAlias, float expectedValue)
  152. {
  153. // Gets a copy of an instance DOM for the provided instance
  154. PrefabDom instanceDomFromTemplate;
  155. m_instanceDomGeneratorInterface->GetInstanceDomFromTemplate(instanceDomFromTemplate, instance);
  156. // Create an instance from the generated prefab DOM for validation
  157. Instance instanceFromDom;
  158. ASSERT_TRUE(PrefabDomUtils::LoadInstanceFromPrefabDom(
  159. instanceFromDom, instanceDomFromTemplate, PrefabDomUtils::LoadFlags::UseSelectiveDeserialization));
  160. // Verify that the worldX value of the provided child entity is coming from the correct template
  161. EntityOptionalReference childEntity = FindEntityInInstanceHierarchy(instanceFromDom, entityAlias);
  162. ASSERT_TRUE(childEntity.has_value());
  163. childEntity->get().Init();
  164. childEntity->get().Activate(); // to connect to buses such as transform bus
  165. float curXValue = 0.0f;
  166. AZ::TransformBus::EventResult(curXValue, childEntity->get().GetId(), &AZ::TransformInterface::GetWorldX);
  167. EXPECT_EQ(curXValue, expectedValue);
  168. // Verify that the parent of the container entity is a valid entity
  169. EntityOptionalReference containerEntity = instanceFromDom.GetContainerEntity();
  170. ASSERT_TRUE(containerEntity.has_value());
  171. containerEntity->get().Init();
  172. containerEntity->get().Activate(); // to connect to buses such as transform bus
  173. AZ::EntityId parentEntityId;
  174. AZ::TransformBus::EventResult(parentEntityId, containerEntity->get().GetId(), &AZ::TransformInterface::GetParentId);
  175. EXPECT_TRUE(parentEntityId.IsValid());
  176. }
  177. EntityOptionalReference PrefabInstanceDomGeneratorTestFixture::FindEntityInInstanceHierarchy(
  178. Instance& instance,
  179. EntityAlias entityAlias)
  180. {
  181. AZStd::queue<InstanceOptionalReference> instanceQueue;
  182. instanceQueue.push(instance);
  183. EntityOptionalReference foundEntity;
  184. while (!instanceQueue.empty())
  185. {
  186. InstanceOptionalReference currentInstance = instanceQueue.front();
  187. instanceQueue.pop();
  188. if (currentInstance.has_value())
  189. {
  190. foundEntity = currentInstance->get().GetEntity(entityAlias);
  191. if (foundEntity.has_value())
  192. {
  193. break;
  194. }
  195. currentInstance->get().GetNestedInstances(
  196. [&](AZStd::unique_ptr<Instance>& nestedInstance)
  197. {
  198. instanceQueue.push(*nestedInstance.get());
  199. });
  200. }
  201. }
  202. return foundEntity;
  203. }
  204. void PrefabInstanceDomGeneratorTestFixture::GenerateAndValidateEntityDom(const AZ::Entity& entity, float expectedValue)
  205. {
  206. // Generate an entity DOM for the provided entity
  207. PrefabDom generatedEntityDom;
  208. m_instanceDomGeneratorInterface->GetEntityDomFromTemplate(generatedEntityDom, entity);
  209. EXPECT_TRUE(generatedEntityDom.IsObject());
  210. // Create an entity from the generated entity DOM for validation
  211. AZ::Entity entityFromDom;
  212. AZ::JsonSerializationResult::ResultCode result = AZ::JsonSerialization::Load(entityFromDom, generatedEntityDom);
  213. ASSERT_TRUE(result.GetProcessing() != AZ::JsonSerializationResult::Processing::Halted);
  214. // Verify that the worldX value is coming from the correct template
  215. entityFromDom.Init();
  216. entityFromDom.Activate(); // to connect to buses such as transform bus
  217. float curXValue = 0.0f;
  218. AZ::TransformBus::EventResult(curXValue, entityFromDom.GetId(), &AZ::TransformInterface::GetWorldX);
  219. EXPECT_EQ(curXValue, expectedValue);
  220. }
  221. } // namespace UnitTest