ProceduralPrefabAssetTests.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <AzToolsFramework/ToolsComponents/TransformComponent.h>
  9. #include <Entity/EntityUtilityComponent.h>
  10. #include <Prefab/PrefabTestComponent.h>
  11. #include <Prefab/PrefabTestDomUtils.h>
  12. #include <Prefab/PrefabTestFixture.h>
  13. #include <Prefab/Procedural/ProceduralPrefabAsset.h>
  14. #include <AzCore/Serialization/Json/RegistrationContext.h>
  15. namespace UnitTest
  16. {
  17. class ProceduralPrefabAssetTest
  18. : public PrefabTestFixture
  19. {
  20. void SetUpEditorFixtureImpl() override
  21. {
  22. AZ::ComponentApplicationRequests* componentApplicationRequests = AZ::Interface<AZ::ComponentApplicationRequests>::Get();
  23. ASSERT_NE(componentApplicationRequests, nullptr);
  24. auto* behaviorContext = componentApplicationRequests->GetBehaviorContext();
  25. ASSERT_NE(behaviorContext, nullptr);
  26. auto* jsonRegistrationContext = componentApplicationRequests->GetJsonRegistrationContext();
  27. ASSERT_NE(jsonRegistrationContext, nullptr);
  28. auto* serializeContext = componentApplicationRequests->GetSerializeContext();
  29. ASSERT_NE(serializeContext, nullptr);
  30. AZ::Prefab::ProceduralPrefabAsset::Reflect(serializeContext);
  31. AZ::Prefab::ProceduralPrefabAsset::Reflect(behaviorContext);
  32. AZ::Prefab::ProceduralPrefabAsset::Reflect(jsonRegistrationContext);
  33. }
  34. void TearDownEditorFixtureImpl() override
  35. {
  36. AZ::ComponentApplicationRequests* componentApplicationRequests = AZ::Interface<AZ::ComponentApplicationRequests>::Get();
  37. componentApplicationRequests->GetJsonRegistrationContext()->EnableRemoveReflection();
  38. AZ::Prefab::ProceduralPrefabAsset::Reflect(componentApplicationRequests->GetJsonRegistrationContext());
  39. }
  40. };
  41. TEST_F(ProceduralPrefabAssetTest, ReflectContext_AccessMethods_Works)
  42. {
  43. AZ::ComponentApplicationRequests* componentApplicationRequests = AZ::Interface<AZ::ComponentApplicationRequests>::Get();
  44. auto* serializeContext = componentApplicationRequests->GetSerializeContext();
  45. EXPECT_TRUE(!serializeContext->CreateAny(azrtti_typeid<AZ::Prefab::ProceduralPrefabAsset>()).empty());
  46. EXPECT_TRUE(!serializeContext->CreateAny(azrtti_typeid<AZ::Prefab::PrefabDomData>()).empty());
  47. auto* jsonRegistrationContext = componentApplicationRequests->GetJsonRegistrationContext();
  48. EXPECT_TRUE(jsonRegistrationContext->GetSerializerForSerializerType(azrtti_typeid<AZ::Prefab::PrefabDomDataJsonSerializer>()));
  49. }
  50. TEST_F(ProceduralPrefabAssetTest, ProceduralPrefabAsset_AccessMethods_Works)
  51. {
  52. const auto templateId = TemplateId(1);
  53. const auto prefabString = "fake.prefab";
  54. AZ::Prefab::ProceduralPrefabAsset asset{};
  55. asset.SetTemplateId(templateId);
  56. EXPECT_EQ(asset.GetTemplateId(), templateId);
  57. asset.SetTemplateName(prefabString);
  58. EXPECT_EQ(asset.GetTemplateName(), prefabString);
  59. }
  60. TEST_F(ProceduralPrefabAssetTest, PrefabDomData_AccessMethods_Works)
  61. {
  62. AzToolsFramework::Prefab::PrefabDom dom;
  63. dom.SetObject();
  64. dom.AddMember("boolValue", true, dom.GetAllocator());
  65. AZ::Prefab::PrefabDomData prefabDomData;
  66. prefabDomData.CopyValue(dom);
  67. const AzToolsFramework::Prefab::PrefabDom& result = prefabDomData.GetValue();
  68. EXPECT_TRUE(result.HasMember("boolValue"));
  69. EXPECT_TRUE(result.FindMember("boolValue")->value.GetBool());
  70. }
  71. TEST_F(ProceduralPrefabAssetTest, PrefabDomDataJsonSerializer_Load_Works)
  72. {
  73. AZ::Prefab::PrefabDomData prefabDomData;
  74. AzToolsFramework::Prefab::PrefabDom dom;
  75. dom.SetObject();
  76. dom.AddMember("member", "value", dom.GetAllocator());
  77. AZ::Prefab::PrefabDomDataJsonSerializer prefabDomDataJsonSerializer;
  78. AZ::JsonDeserializerSettings settings;
  79. settings.m_reporting = [](auto, auto, auto)
  80. {
  81. AZ::JsonSerializationResult::ResultCode result(AZ::JsonSerializationResult::Tasks::ReadField);
  82. return result;
  83. };
  84. AZ::JsonDeserializerContext context{ settings };
  85. auto result = prefabDomDataJsonSerializer.Load(&prefabDomData, azrtti_typeid(prefabDomData), dom, context);
  86. EXPECT_EQ(result.GetResultCode().GetOutcome(), AZ::JsonSerializationResult::Outcomes::DefaultsUsed);
  87. EXPECT_TRUE(prefabDomData.GetValue().HasMember("member"));
  88. EXPECT_STREQ(prefabDomData.GetValue().FindMember("member")->value.GetString(), "value");
  89. }
  90. TEST_F(ProceduralPrefabAssetTest, PrefabDomDataJsonSerializer_Store_Works)
  91. {
  92. AzToolsFramework::Prefab::PrefabDom dom;
  93. dom.SetObject();
  94. dom.AddMember("member", "value", dom.GetAllocator());
  95. AZ::Prefab::PrefabDomData prefabDomData;
  96. prefabDomData.CopyValue(dom);
  97. AZ::Prefab::PrefabDomDataJsonSerializer prefabDomDataJsonSerializer;
  98. AzToolsFramework::Prefab::PrefabDom outputValue;
  99. AZ::JsonSerializerSettings settings;
  100. settings.m_reporting = [](auto, auto, auto)
  101. {
  102. AZ::JsonSerializationResult::ResultCode result(AZ::JsonSerializationResult::Tasks::WriteValue);
  103. return result;
  104. };
  105. AZ::JsonSerializerContext context{ settings, outputValue.GetAllocator() };
  106. auto result = prefabDomDataJsonSerializer.Store(outputValue, &prefabDomData, nullptr, azrtti_typeid(prefabDomData), context);
  107. EXPECT_EQ(result.GetResultCode().GetOutcome(), AZ::JsonSerializationResult::Outcomes::DefaultsUsed);
  108. EXPECT_TRUE(outputValue.HasMember("member"));
  109. EXPECT_STREQ(outputValue.FindMember("member")->value.GetString(), "value");
  110. }
  111. }