PrefabUndoComponentPropertyTestFixture.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <AzCore/DOM/Backends/JSON/JsonSerializationUtils.h>
  10. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  11. #include <AzToolsFramework/Prefab/PrefabDomUtils.h>
  12. #include <AzToolsFramework/ToolsComponents/TransformComponent.h>
  13. namespace UnitTest
  14. {
  15. void PrefabUndoComponentPropertyTestFixture::SetUpEditorFixtureImpl()
  16. {
  17. PrefabTestFixture::SetUpEditorFixtureImpl();
  18. m_prefabOverridePublicInterface = AZ::Interface<PrefabOverridePublicInterface>::Get();
  19. ASSERT_TRUE(m_prefabOverridePublicInterface);
  20. }
  21. void PrefabUndoComponentPropertyTestFixture::CreateWheelEntityHierarchy(EntityInfo& wheelEntityInfo)
  22. {
  23. AZ::EntityId wheelEntityId = CreateEditorEntityUnderRoot(WheelEntityName);
  24. ASSERT_TRUE(wheelEntityId.IsValid());
  25. EntityAlias wheelEntityAlias = FindEntityAliasInInstance(GetRootContainerEntityId(), WheelEntityName);
  26. ASSERT_FALSE(wheelEntityAlias.empty());
  27. wheelEntityInfo = { wheelEntityId, wheelEntityAlias };
  28. }
  29. void PrefabUndoComponentPropertyTestFixture::CreateCarPrefabHierarchy(
  30. InstanceInfo& carInstanceInfo, EntityInfo& wheelEntityInfo)
  31. {
  32. AZ::IO::Path engineRootPath;
  33. m_settingsRegistryInterface->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  34. AZ::IO::Path carPrefabFilepath = engineRootPath / CarPrefabName;
  35. // Create the Car prefab
  36. AZ::EntityId wheelEntityId = CreateEditorEntityUnderRoot(WheelEntityName);
  37. ASSERT_TRUE(wheelEntityId.IsValid());
  38. AZ::EntityId carContainerId = CreateEditorPrefab(carPrefabFilepath, { wheelEntityId });
  39. ASSERT_TRUE(carContainerId.IsValid());
  40. InstanceAlias carInstanceAlias = FindNestedInstanceAliasInInstance(GetRootContainerEntityId(), CarPrefabName);
  41. ASSERT_FALSE(carInstanceAlias.empty());
  42. EntityAlias wheelEntityAlias = FindEntityAliasInInstance(carContainerId, WheelEntityName);
  43. ASSERT_FALSE(wheelEntityAlias.empty());
  44. // Retrieve the Wheel entity id after adding it to Car instance
  45. InstanceOptionalReference carInstance = m_instanceEntityMapperInterface->FindOwningInstance(carContainerId);
  46. ASSERT_TRUE(carInstance.has_value());
  47. EntityOptionalReference wheelEntity = carInstance->get().GetEntity(wheelEntityAlias);
  48. ASSERT_TRUE(wheelEntity.has_value());
  49. wheelEntityId = wheelEntity->get().GetId();
  50. ASSERT_TRUE(wheelEntityId.IsValid());
  51. carInstanceInfo = { carContainerId, carInstanceAlias };
  52. wheelEntityInfo = { wheelEntityId, wheelEntityAlias };
  53. }
  54. void PrefabUndoComponentPropertyTestFixture::CreateSuperCarPrefabHierarchy(
  55. InstanceInfo& superCarInstanceInfo, InstanceInfo& carInstanceInfo, EntityInfo& wheelEntityInfo)
  56. {
  57. // Create the Car prefab
  58. CreateCarPrefabHierarchy(carInstanceInfo, wheelEntityInfo);
  59. AZ::IO::Path engineRootPath;
  60. m_settingsRegistryInterface->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  61. AZ::IO::Path superCarPrefabFilepath = engineRootPath / SuperCarPrefabName;
  62. // Create the SuperCar prefab
  63. AZ::EntityId superCarContainerId = CreateEditorPrefab(superCarPrefabFilepath, { carInstanceInfo.m_containerEntityId });
  64. ASSERT_TRUE(superCarContainerId.IsValid());
  65. InstanceAlias superCarInstanceAlias = FindNestedInstanceAliasInInstance(GetRootContainerEntityId(), SuperCarPrefabName);
  66. ASSERT_FALSE(superCarInstanceAlias.empty());
  67. superCarInstanceInfo = { superCarContainerId, superCarInstanceAlias };
  68. // Retrieve the Car instance info after adding it to SuperCar instance
  69. InstanceAlias newCarInstanceAlias = FindNestedInstanceAliasInInstance(superCarContainerId, CarPrefabName);
  70. ASSERT_FALSE(newCarInstanceAlias.empty());
  71. InstanceOptionalReference superCarInstance = m_instanceEntityMapperInterface->FindOwningInstance(superCarContainerId);
  72. ASSERT_TRUE(superCarInstance.has_value());
  73. InstanceOptionalReference carInstance = superCarInstance->get().FindNestedInstance(newCarInstanceAlias);
  74. ASSERT_TRUE(carInstance.has_value());
  75. carInstanceInfo = { carInstance->get().GetContainerEntityId(), newCarInstanceAlias };
  76. // Retrieve the Wheel entity id after adding the Car instance to SuperCar instance
  77. EntityOptionalReference wheelEntity = carInstance->get().GetEntity(wheelEntityInfo.m_entityAlias);
  78. ASSERT_TRUE(wheelEntity.has_value());
  79. wheelEntityInfo.m_entityId = wheelEntity->get().GetId();
  80. ASSERT_TRUE(wheelEntityInfo.m_entityId.IsValid());
  81. }
  82. auto PrefabUndoComponentPropertyTestFixture::MakeTransformTranslationPropertyChangePatch(
  83. const AZ::EntityId entityId, const AZ::Vector3& translation) -> PropertyChangePatch
  84. {
  85. AZ::Entity* entity = AzToolsFramework::GetEntity(entityId);
  86. EXPECT_TRUE(entity) << "MakeTransformTranslationPropertyChangePatch - Cannot retrieve the entity that is provided.";
  87. const auto transformComponent = entity->FindComponent<AzToolsFramework::Components::TransformComponent>();
  88. EXPECT_TRUE(transformComponent) << "MakeTransformTranslationPropertyChangePatch - Cannot get the transform component.";
  89. AZ::Dom::Value propertyValue;
  90. propertyValue.SetArray();
  91. propertyValue.ArrayPushBack(AZ::Dom::Value(translation.GetX()));
  92. propertyValue.ArrayPushBack(AZ::Dom::Value(translation.GetY()));
  93. propertyValue.ArrayPushBack(AZ::Dom::Value(translation.GetZ()));
  94. const AZStd::string componentAlias = transformComponent->GetSerializedIdentifier();
  95. EXPECT_FALSE(componentAlias.empty()) << "MakeTransformTranslationPropertyChangePatch - Component alias is empty.";
  96. AZ::Dom::Path pathToProperty;
  97. pathToProperty /= PrefabDomUtils::ComponentsName;
  98. pathToProperty /= componentAlias;
  99. pathToProperty /= AZ::Dom::Path("/Transform Data/Translate");
  100. return { pathToProperty, propertyValue };
  101. }
  102. auto PrefabUndoComponentPropertyTestFixture::MakeTransformStaticPropertyChangePatch(
  103. const AZ::EntityId entityId, const float isStatic) -> PropertyChangePatch
  104. {
  105. AZ::Entity* entity = AzToolsFramework::GetEntity(entityId);
  106. EXPECT_TRUE(entity) << "MakeTransformStaticPropertyChangePatch - Cannot retrieve the entity that is provided.";
  107. const auto transformComponent = entity->FindComponent<AzToolsFramework::Components::TransformComponent>();
  108. EXPECT_TRUE(transformComponent) << "MakeTransformStaticPropertyChangePatch - Cannot get the transform component.";
  109. AZ::Dom::Value propertyValue;
  110. propertyValue.SetBool(isStatic);
  111. const AZStd::string componentAlias = transformComponent->GetSerializedIdentifier();
  112. EXPECT_FALSE(componentAlias.empty()) << "MakeTransformStaticPropertyChangePatch - Component alias is empty.";
  113. AZ::Dom::Path pathToProperty;
  114. pathToProperty /= PrefabDomUtils::ComponentsName;
  115. pathToProperty /= componentAlias;
  116. pathToProperty /= AZ::Dom::Path("/IsStatic");
  117. return { pathToProperty, propertyValue };
  118. }
  119. void PrefabUndoComponentPropertyTestFixture::ConvertToPrefabDomValue(
  120. PrefabDom& outputDom, const AZ::Dom::Value& domValue)
  121. {
  122. auto convertToRapidJsonOutcome = AZ::Dom::Json::WriteToRapidJsonDocument(
  123. [&domValue](AZ::Dom::Visitor& visitor)
  124. {
  125. return domValue.Accept(visitor, false);
  126. });
  127. EXPECT_TRUE(convertToRapidJsonOutcome.IsSuccess());
  128. outputDom = convertToRapidJsonOutcome.TakeValue();
  129. }
  130. } // namespace UnitTest