DefaultProceduralPrefabGroupTests.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/tests/PrefabBehaviorTestMocks.h>
  10. #include <PrefabGroup/IPrefabGroup.h>
  11. #include <PrefabGroup/PrefabGroup.h>
  12. #include <PrefabGroup/PrefabGroupBehavior.h>
  13. #include <PrefabGroup/DefaultProceduralPrefab.h>
  14. #include <AzCore/Serialization/Json/JsonSystemComponent.h>
  15. #include <AzCore/Serialization/Json/RegistrationContext.h>
  16. #include <AzFramework/FileFunc/FileFunc.h>
  17. #include <SceneAPI/SceneCore/Mocks/DataTypes/MockIGraphObject.h>
  18. #include <SceneAPI/SceneCore/Containers/Scene.h>
  19. #include <SceneAPI/SceneCore/DataTypes/Groups/IMeshGroup.h>
  20. #include <SceneAPI/SceneData/GraphData/MeshData.h>
  21. namespace UnitTest
  22. {
  23. struct DefaultProceduralPrefabGroupTests
  24. : public PrefabBuilderTests
  25. {
  26. void SetUp() override
  27. {
  28. using namespace AZ::SceneAPI;
  29. PrefabBuilderTests::SetUp();
  30. SceneData::PrefabGroup::Reflect(m_app.GetSerializeContext());
  31. SceneData::PrefabGroup::Reflect(m_app.GetBehaviorContext());
  32. DefaultProceduralPrefabGroup::Reflect(m_app.GetBehaviorContext());
  33. m_app.GetBehaviorContext()->Method("TestExpectTrue", &TestExpectTrue);
  34. m_app.GetBehaviorContext()->Method("TestEqualNumbers", &TestEqualNumbers);
  35. m_app.GetBehaviorContext()->Method("TestEqualStrings", &TestEqualStrings);
  36. ScopeForUnitTest(m_app.GetBehaviorContext()->m_ebuses.find("PrefabGroupNotificationBus")->second->m_attributes);
  37. ScopeForUnitTest(m_app.GetBehaviorContext()->m_ebuses.find("PrefabGroupEventBus")->second->m_attributes);
  38. m_editorMeshComponentHelper = AZStd::make_unique<AZ::Render::EditorMeshComponentHelper>();
  39. m_editorMeshComponentHelper->Reflect(m_app.GetSerializeContext());
  40. m_editorMeshComponentHelper->Reflect(m_app.GetBehaviorContext());
  41. AZ::SceneAPI::Containers::Scene::Reflect(m_app.GetBehaviorContext());
  42. m_scriptContext = AZStd::make_unique<AZ::ScriptContext>();
  43. m_scriptContext->BindTo(m_app.GetBehaviorContext());
  44. }
  45. void TearDown() override
  46. {
  47. m_editorMeshComponentHelper.reset();
  48. m_scriptContext.reset();
  49. PrefabBuilderTests::TearDown();
  50. }
  51. void ScopeForUnitTest(AZ::AttributeArray& attributes)
  52. {
  53. using namespace AZ::Script;
  54. attributes.erase(AZStd::remove_if(attributes.begin(), attributes.end(), [](const AZ::AttributePair& pair)
  55. {
  56. return pair.first == Attributes::Scope;
  57. }));
  58. auto* attributeData = aznew AZ::Edit::AttributeData<Attributes::ScopeFlags>(Attributes::ScopeFlags::Common);
  59. attributes.push_back(AZStd::make_pair(Attributes::Scope, attributeData));
  60. }
  61. void ExpectExecute(AZStd::string_view script)
  62. {
  63. EXPECT_TRUE(m_scriptContext->Execute(script.data()));
  64. }
  65. static void TestExpectTrue(bool value)
  66. {
  67. EXPECT_TRUE(value);
  68. }
  69. static void TestEqualNumbers(AZ::s64 lhs, AZ::s64 rhs)
  70. {
  71. EXPECT_EQ(lhs, rhs);
  72. }
  73. static void TestEqualStrings(AZStd::string_view lhs, AZStd::string_view rhs)
  74. {
  75. EXPECT_STRCASEEQ(lhs.data(), rhs.data());
  76. }
  77. AZStd::unique_ptr<AZ::ScriptContext> m_scriptContext;
  78. AZStd::unique_ptr<AZ::Render::EditorMeshComponentHelper> m_editorMeshComponentHelper;
  79. };
  80. TEST_F(DefaultProceduralPrefabGroupTests, ScripContext_PrefabGroupNotificationBus_ClassExists)
  81. {
  82. ExpectExecute("handler = PrefabGroupNotificationBus.Connect({})");
  83. ExpectExecute("TestExpectTrue(handler ~= nil)");
  84. }
  85. TEST_F(DefaultProceduralPrefabGroupTests, ScripContext_PrefabGroupEventBus_ClassApiExists)
  86. {
  87. ExpectExecute("TestExpectTrue(PrefabGroupEventBus.Broadcast.GeneratePrefabGroupManifestUpdates ~= nil)");
  88. }
  89. TEST_F(DefaultProceduralPrefabGroupTests, PrefabGroupEventBus_GeneratePrefabGroupManifestUpdates_HasProceduralMeshGroupRule)
  90. {
  91. AZ::SceneAPI::DefaultProceduralPrefabGroup defaultProceduralPrefabGroup;
  92. auto scene = CreateMockScene();
  93. AZStd::optional<AZ::SceneAPI::PrefabGroupRequests::ManifestUpdates> manifestUpdates;
  94. AZ::SceneAPI::PrefabGroupEventBus::BroadcastResult(
  95. manifestUpdates,
  96. &AZ::SceneAPI::PrefabGroupEventBus::Events::GeneratePrefabGroupManifestUpdates,
  97. *scene.get());
  98. ASSERT_TRUE(manifestUpdates.has_value());
  99. bool hasProceduralMeshGroupRule = false;
  100. for (const auto& manifestUpdate : manifestUpdates.value())
  101. {
  102. auto* meshGroup = azrtti_cast<AZ::SceneAPI::DataTypes::IMeshGroup*>(manifestUpdate.get());
  103. if (meshGroup)
  104. {
  105. const auto proceduralMeshGroupRule =
  106. meshGroup->GetRuleContainer().FindFirstByType<AZ::SceneAPI::SceneData::ProceduralMeshGroupRule>();
  107. if (proceduralMeshGroupRule)
  108. {
  109. hasProceduralMeshGroupRule = true;
  110. break;
  111. }
  112. }
  113. }
  114. EXPECT_TRUE(hasProceduralMeshGroupRule);
  115. }
  116. }