PrefabGroupTests.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <PrefabBuilderTests.h>
  9. #include <PrefabGroup/IPrefabGroup.h>
  10. #include <PrefabGroup/PrefabGroup.h>
  11. #include <AzCore/Serialization/Json/JsonSystemComponent.h>
  12. #include <AzCore/Serialization/Json/RegistrationContext.h>
  13. #include <AzFramework/FileFunc/FileFunc.h>
  14. namespace UnitTest
  15. {
  16. TEST_F(PrefabBuilderTests, PrefabGroup_FindsRequiredReflection_True)
  17. {
  18. using namespace AZ::SceneAPI;
  19. auto* serializeContext = m_app.GetSerializeContext();
  20. ASSERT_NE(nullptr, serializeContext);
  21. SceneData::PrefabGroup::Reflect(serializeContext);
  22. SceneData::PrefabGroup::Reflect(m_app.GetJsonRegistrationContext());
  23. ASSERT_NE(nullptr, serializeContext->FindClassData(azrtti_typeid<DataTypes::IPrefabGroup>()));
  24. ASSERT_NE(nullptr, serializeContext->FindClassData(azrtti_typeid<SceneData::PrefabGroup>()));
  25. auto findElementWithName = [](const AZ::SerializeContext::ClassData* classData, const char* name)
  26. {
  27. auto it = AZStd::find_if(classData->m_elements.begin(), classData->m_elements.end(), [name](const auto& element)
  28. {
  29. return strcmp(element.m_name, name) == 0;
  30. });
  31. return it != classData->m_elements.end();
  32. };
  33. auto* prefabGroupClassData = serializeContext->FindClassData(azrtti_typeid<SceneData::PrefabGroup>());
  34. EXPECT_TRUE(findElementWithName(prefabGroupClassData, "name"));
  35. EXPECT_TRUE(findElementWithName(prefabGroupClassData, "nodeSelectionList"));
  36. EXPECT_TRUE(findElementWithName(prefabGroupClassData, "rules"));
  37. EXPECT_TRUE(findElementWithName(prefabGroupClassData, "id"));
  38. EXPECT_TRUE(findElementWithName(prefabGroupClassData, "prefabDomData"));
  39. m_app.GetJsonRegistrationContext()->EnableRemoveReflection();
  40. SceneData::PrefabGroup::Reflect(m_app.GetJsonRegistrationContext());
  41. }
  42. TEST_F(PrefabBuilderTests, PrefabGroup_JsonWithPrefabArbitraryPrefab_Works)
  43. {
  44. namespace JSR = AZ::JsonSerializationResult;
  45. using namespace AZ::SceneAPI;
  46. auto* serializeContext = m_app.GetSerializeContext();
  47. ASSERT_NE(nullptr, serializeContext);
  48. SceneData::PrefabGroup::Reflect(serializeContext);
  49. AZ::Prefab::ProceduralPrefabAsset::Reflect(serializeContext);
  50. SceneData::PrefabGroup::Reflect(m_app.GetJsonRegistrationContext());
  51. AZ::Prefab::ProceduralPrefabAsset::Reflect(m_app.GetJsonRegistrationContext());
  52. // fill out a PrefabGroup using JSON
  53. AZStd::string_view input = R"JSON(
  54. {
  55. "name" : "tester",
  56. "id" : "{49698DBC-B447-49EF-9B56-25BB29342AFB}",
  57. "prefabDomData" : {"foo": "bar"}
  58. })JSON";
  59. rapidjson::Document document;
  60. document.Parse<rapidjson::kParseCommentsFlag>(input.data(), input.size());
  61. ASSERT_FALSE(document.HasParseError());
  62. SceneData::PrefabGroup instancePrefabGroup;
  63. EXPECT_EQ(AZ::JsonSerialization::Load(instancePrefabGroup, document).GetOutcome(), JSR::Outcomes::PartialDefaults);
  64. auto prefabDomRef = instancePrefabGroup.GetPrefabDomRef();
  65. ASSERT_TRUE(prefabDomRef.has_value());
  66. const auto& dom = prefabDomRef.value().get();
  67. EXPECT_TRUE(dom.IsObject());
  68. EXPECT_TRUE(dom.GetObject().HasMember("foo"));
  69. EXPECT_STREQ(dom.GetObject().FindMember("foo")->value.GetString(), "bar");
  70. EXPECT_STREQ(instancePrefabGroup.GetName().c_str(), "tester");
  71. EXPECT_STREQ(instancePrefabGroup.GetId().ToString<AZStd::string>().c_str(), "{49698DBC-B447-49EF-9B56-25BB29342AFB}");
  72. m_app.GetJsonRegistrationContext()->EnableRemoveReflection();
  73. SceneData::PrefabGroup::Reflect(m_app.GetJsonRegistrationContext());
  74. AZ::Prefab::ProceduralPrefabAsset::Reflect(m_app.GetJsonRegistrationContext());
  75. }
  76. TEST_F(PrefabBuilderTests, PrefabGroup_InvalidPrefabJson_Detected)
  77. {
  78. using namespace AZ::SceneAPI;
  79. AZStd::string_view input = R"JSON(
  80. {
  81. bad json that will not parse
  82. })JSON";
  83. rapidjson::Document document;
  84. document.Parse<rapidjson::kParseCommentsFlag>(input.data(), input.size());
  85. SceneData::PrefabGroup prefabGroup;
  86. prefabGroup.SetId(AZ::Uuid::CreateRandom());
  87. prefabGroup.SetName("tester");
  88. prefabGroup.SetPrefabDom(AZStd::move(document));
  89. auto prefabDomRef = prefabGroup.GetPrefabDomRef();
  90. ASSERT_TRUE(prefabDomRef.has_value());
  91. const auto& dom = prefabDomRef.value().get();
  92. EXPECT_TRUE(dom.IsNull());
  93. EXPECT_STREQ("tester", prefabGroup.GetName().c_str());
  94. }
  95. struct PrefabBuilderBehaviorTests
  96. : public PrefabBuilderTests
  97. {
  98. void SetUp() override
  99. {
  100. using namespace AZ::SceneAPI;
  101. PrefabBuilderTests::SetUp();
  102. SceneData::PrefabGroup::Reflect(m_app.GetSerializeContext());
  103. SceneData::PrefabGroup::Reflect(m_app.GetBehaviorContext());
  104. SceneData::PrefabGroup::Reflect(m_app.GetJsonRegistrationContext());
  105. m_scriptContext = AZStd::make_unique<AZ::ScriptContext>();
  106. m_scriptContext->BindTo(m_app.GetBehaviorContext());
  107. }
  108. void TearDown() override
  109. {
  110. using namespace AZ::SceneAPI;
  111. m_app.GetJsonRegistrationContext()->EnableRemoveReflection();
  112. SceneData::PrefabGroup::Reflect(m_app.GetJsonRegistrationContext());
  113. m_scriptContext.reset();
  114. PrefabBuilderTests::TearDown();
  115. }
  116. void ExpectExecute(AZStd::string_view script)
  117. {
  118. EXPECT_TRUE(m_scriptContext->Execute(script.data()));
  119. }
  120. AZStd::unique_ptr<AZ::ScriptContext> m_scriptContext;
  121. };
  122. TEST_F(PrefabBuilderBehaviorTests, PrefabGroup_PrefabGroupClass_Exists)
  123. {
  124. ExpectExecute("group = PrefabGroup()");
  125. ExpectExecute("assert(group)");
  126. ExpectExecute("assert(group.name)");
  127. ExpectExecute("assert(group.id)");
  128. ExpectExecute("assert(group.prefabDomData)");
  129. }
  130. TEST_F(PrefabBuilderBehaviorTests, PrefabGroup_PrefabGroupAssignment_Works)
  131. {
  132. ExpectExecute("group = PrefabGroup()");
  133. ExpectExecute("group.name = 'tester'");
  134. ExpectExecute("group.id = Uuid.CreateString('{AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE}')");
  135. ExpectExecute("group.prefabDomData = '{\"foo\": \"bar\"}'");
  136. ExpectExecute("assert(group.name == 'tester')");
  137. ExpectExecute("assert(tostring(group.id) == '{AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE}')");
  138. ExpectExecute("assert(group.prefabDomData == '{\\n \"foo\": \"bar\"\\n}')");
  139. }
  140. }