PrefabUpdateWithPatchesTests.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/PrefabDomUtils.h>
  9. #include <Prefab/PrefabTestComponent.h>
  10. #include <Prefab/PrefabTestDomUtils.h>
  11. #include <Prefab/PrefabTestFixture.h>
  12. #include <AzCore/Component/ComponentApplicationBus.h>
  13. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  14. namespace UnitTest
  15. {
  16. using PrefabUpdateWithPatchesTest = PrefabTestFixture;
  17. /*
  18. The below tests use an example of car->axle->wheel templates to test that change propagation works correctly within templates.
  19. The car template will have axle templates nested under it and the axle template will have wheel templates nested under it.
  20. Because of the complexity that arises from multiple levels of prefab nesting, it's easier to write tests using an example scenario
  21. than use generic nesting terminology.
  22. */
  23. TEST_F(PrefabUpdateWithPatchesTest, ApplyPatchesToInstance_ComponentUpdated_PatchAppliedCorrectly)
  24. {
  25. // Create a single entity wheel instance with a PrefabTestComponent and create a template out of it.
  26. AZ::Entity* wheelEntity = CreateEntity("WheelEntity1");
  27. AddRequiredEditorComponents({ wheelEntity->GetId() });
  28. wheelEntity->Deactivate();
  29. PrefabTestComponent* prefabTestComponent = aznew PrefabTestComponent(true);
  30. wheelEntity->AddComponent(prefabTestComponent);
  31. AZ::ComponentId prefabTestComponentId = prefabTestComponent->GetId();
  32. const int expectedComponentCount = 10; // 9 of them are from AddRequiredEditorComponents.
  33. wheelEntity->Activate();
  34. AZStd::unique_ptr<Instance> wheelIsolatedInstance = m_prefabSystemComponent->CreatePrefab({ wheelEntity },
  35. {}, WheelPrefabMockFilePath);
  36. const TemplateId wheelTemplateId = wheelIsolatedInstance->GetTemplateId();
  37. PrefabDom& wheelTemplateDom = m_prefabSystemComponent->FindTemplateDom(wheelTemplateId);
  38. AZStd::vector<EntityAlias> wheelTemplateEntityAliases = wheelIsolatedInstance->GetEntityAliases();
  39. // Validate that the wheel template has the same entities(1) as the instance it was created from.
  40. ASSERT_EQ(wheelTemplateEntityAliases.size(), 1);
  41. // Validate that the wheel entity has 2 components. One of them is added through HandleEntitiesAdded() in EditorEntityContext.
  42. EntityAlias wheelEntityAlias = wheelTemplateEntityAliases.front();
  43. PrefabDomValue* wheelEntityComponents =
  44. PrefabTestDomUtils::GetPrefabDomComponentsPath(wheelEntityAlias).Get(wheelTemplateDom);
  45. ASSERT_TRUE(wheelEntityComponents != nullptr && wheelEntityComponents->IsObject());
  46. EXPECT_EQ(wheelEntityComponents->MemberCount(), expectedComponentCount);
  47. // Extract the component id of the entity in wheel template and verify that it matches with the component id of the wheel instance.
  48. PrefabTestDomUtils::ValidateComponentsDomHasId(
  49. *wheelEntityComponents, prefabTestComponent->RTTI_GetTypeName(), prefabTestComponentId);
  50. // Create an axle with 0 entities and 1 wheel instance.
  51. AZStd::unique_ptr<Instance> wheel1UnderAxle = m_prefabSystemComponent->InstantiatePrefab(wheelTemplateId);
  52. const AZStd::vector<EntityAlias> wheelEntityAliasesUnderAxle = wheel1UnderAxle->GetEntityAliases();
  53. AZStd::unique_ptr<Instance> axleInstance = m_prefabSystemComponent->CreatePrefab({},
  54. MakeInstanceList(AZStd::move(wheel1UnderAxle)), AxlePrefabMockFilePath);
  55. const TemplateId axleTemplateId = axleInstance->GetTemplateId();
  56. PrefabDom& axleTemplateDom = m_prefabSystemComponent->FindTemplateDom(axleTemplateId);
  57. const AZStd::vector<InstanceAlias> wheelInstanceAliasesUnderAxle = axleInstance->GetNestedInstanceAliases(wheelTemplateId);
  58. ASSERT_EQ(wheelInstanceAliasesUnderAxle.size(), 1);
  59. // Create a car with 0 entities and 1 axle instance.
  60. AZStd::unique_ptr<Instance> axleUnderCar = m_prefabSystemComponent->InstantiatePrefab(axleTemplateId);
  61. AZStd::unique_ptr<Instance> carInstance = m_prefabSystemComponent->CreatePrefab({},
  62. MakeInstanceList(AZStd::move(axleUnderCar)), CarPrefabMockFilePath);
  63. const TemplateId carTemplateId = carInstance->GetTemplateId();
  64. const AZStd::vector<InstanceAlias> axleInstanceAliasesUnderCar = carInstance->GetNestedInstanceAliases(axleTemplateId);
  65. PrefabDom& carTemplateDom = m_prefabSystemComponent->FindTemplateDom(carTemplateId);
  66. InstanceOptionalReference nestedWheelInstanceRef = axleInstance->FindNestedInstance(wheelInstanceAliasesUnderAxle[0]);
  67. ASSERT_TRUE(nestedWheelInstanceRef);
  68. //get the entity id
  69. AZStd::vector<AZ::EntityId> entityIdVector;
  70. axleInstance->GetAllEntityIdsInHierarchy([&entityIdVector](AZ::EntityId entityId)
  71. {
  72. entityIdVector.push_back(entityId);
  73. return true;
  74. });
  75. ASSERT_EQ(entityIdVector.size(), 3);
  76. AZ::EntityId wheelEntityIdUnderAxle = nestedWheelInstanceRef->get().GetEntityId(wheelEntityAlias);
  77. // Retrieve the entity pointer from the component application bus.
  78. AZ::Entity* wheelEntityUnderAxle = nullptr;
  79. axleInstance->GetAllEntitiesInHierarchy([&wheelEntityUnderAxle, wheelEntityIdUnderAxle](AZStd::unique_ptr<AZ::Entity>& entity)
  80. {
  81. if (entity->GetId() == wheelEntityIdUnderAxle)
  82. {
  83. wheelEntityUnderAxle = entity.get();
  84. return false;
  85. }
  86. else
  87. {
  88. return true;
  89. }
  90. });
  91. ASSERT_NE(nullptr, wheelEntityUnderAxle);
  92. //create document with before change snapshot
  93. PrefabDom entityDomBefore;
  94. m_instanceToTemplateInterface->GenerateEntityDomBySerializing(entityDomBefore, *wheelEntityUnderAxle);
  95. PrefabTestComponent* axlewheelComponent = wheelEntityUnderAxle->FindComponent<PrefabTestComponent>();
  96. // Change the bool property of the component from Wheel instance and use it to update the wheel template.
  97. axlewheelComponent->m_boolProperty = false;
  98. //create document with after change snapshot
  99. PrefabDom entityDomAfter;
  100. m_instanceToTemplateInterface->GenerateEntityDomBySerializing(entityDomAfter, *wheelEntityUnderAxle);
  101. InstanceOptionalReference topMostInstanceInHierarchy = m_instanceToTemplateInterface->GetTopMostInstanceInHierarchy(wheelEntityIdUnderAxle);
  102. ASSERT_TRUE(topMostInstanceInHierarchy);
  103. PrefabDom patches;
  104. InstanceOptionalReference wheelInstanceUnderAxle = axleInstance->FindNestedInstance(wheelInstanceAliasesUnderAxle.front());
  105. m_instanceToTemplateInterface->GeneratePatchForLink(patches, entityDomBefore, entityDomAfter, wheelInstanceUnderAxle->get().GetLinkId());
  106. m_instanceToTemplateInterface->ApplyPatchesToInstance(wheelEntityIdUnderAxle, patches, topMostInstanceInHierarchy->get());
  107. m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
  108. // Validate that the prefabTestComponent in the wheel instance under axle doesn't have a BoolProperty.
  109. // Even though we changed the property to false, it won't be serialized out because it's a default value.
  110. PrefabDomValue* wheelInstanceDomUnderAxle =
  111. PrefabTestDomUtils::GetPrefabDomInstancePath(wheelInstanceAliasesUnderAxle.front()).Get(axleTemplateDom);
  112. wheelEntityComponents = PrefabTestDomUtils::GetPrefabDomComponentsPath(wheelEntityAlias).Get(*wheelInstanceDomUnderAxle);
  113. ASSERT_TRUE(wheelEntityComponents != nullptr);
  114. PrefabDomValueReference wheelEntityComponentValue = PrefabDomUtils::FindPrefabDomValue(*wheelEntityComponents, prefabTestComponent->RTTI_GetTypeName());
  115. ASSERT_TRUE(wheelEntityComponentValue);
  116. PrefabDomValueReference wheelEntityComponentBoolPropertyValue =
  117. PrefabDomUtils::FindPrefabDomValue(wheelEntityComponentValue->get(), PrefabTestDomUtils::BoolPropertyName);
  118. ASSERT_TRUE(wheelEntityComponentBoolPropertyValue.has_value());
  119. ASSERT_TRUE(wheelEntityComponentBoolPropertyValue->get().IsBool());
  120. ASSERT_FALSE(wheelEntityComponentBoolPropertyValue->get().GetBool());
  121. // Validate that the axles under the car have the same DOM as the axle template.
  122. PrefabTestDomUtils::ValidatePrefabDomInstances(axleInstanceAliasesUnderCar, carTemplateDom, axleTemplateDom);
  123. }
  124. }