SceneScriptRuleTests.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <AzTest/AzTest.h>
  9. #include <SceneAPI/SceneCore/Containers/SceneManifest.h>
  10. #include <SceneAPI/SceneCore/Containers/Scene.h>
  11. #include <SceneAPI/SceneCore/DataTypes/Rules/IScriptProcessorRule.h>
  12. #include <SceneAPI/SceneCore/Containers/Utilities/Filters.h>
  13. #include <SceneAPI/SceneData/ReflectionRegistrar.h>
  14. #include <SceneAPI/SceneData/Rules/CoordinateSystemRule.h>
  15. #include <SceneAPI/SceneData/Behaviors/ScriptProcessorRuleBehavior.h>
  16. #include <SceneAPI/SceneCore/Events/ExportProductList.h>
  17. #include <SceneAPI/SceneCore/Mocks/MockBehaviorUtils.h>
  18. #include <AzCore/Math/MathReflection.h>
  19. #include <AzCore/Math/Quaternion.h>
  20. #include <AzCore/Name/NameDictionary.h>
  21. #include <AzCore/RTTI/BehaviorContext.h>
  22. #include <AzCore/RTTI/ReflectContext.h>
  23. #include <AzCore/RTTI/ReflectionManager.h>
  24. #include <AzCore/Script/ScriptContextAttributes.h>
  25. #include <AzCore/Serialization/Json/JsonSystemComponent.h>
  26. #include <AzCore/Serialization/Json/JsonUtils.h>
  27. #include <AzCore/Serialization/Json/RegistrationContext.h>
  28. #include <AzCore/std/smart_ptr/make_shared.h>
  29. #include <AzCore/std/smart_ptr/shared_ptr.h>
  30. #include <AzCore/UnitTest/Mocks/MockSettingsRegistry.h>
  31. #include <AzCore/UnitTest/TestTypes.h>
  32. extern "C" AZ_DLL_EXPORT void CleanUpSceneCoreGenericClassInfo();
  33. extern "C" AZ_DLL_EXPORT void CleanUpSceneDataGenericClassInfo();
  34. namespace Testing
  35. {
  36. struct SceneScriptTest
  37. : public UnitTest::LeakDetectionFixture
  38. {
  39. static void TestExpectTrue(bool value)
  40. {
  41. EXPECT_TRUE(value);
  42. }
  43. static void TestEqualNumbers(AZ::s64 lhs, AZ::s64 rhs)
  44. {
  45. EXPECT_EQ(lhs, rhs);
  46. }
  47. static void TestEqualStrings(AZStd::string_view lhs, AZStd::string_view rhs)
  48. {
  49. EXPECT_STRCASEEQ(lhs.data(), rhs.data());
  50. }
  51. void ExpectExecute(AZStd::string_view script)
  52. {
  53. EXPECT_TRUE(m_scriptContext->Execute(script.data()));
  54. }
  55. void ReflectTypes(AZ::ReflectContext* context)
  56. {
  57. AZ::SceneAPI::Containers::Scene::Reflect(context);
  58. AZ::SceneAPI::Containers::SceneManifest::Reflect(context);
  59. AZ::SceneAPI::DataTypes::IManifestObject::Reflect(context);
  60. AZ::SceneAPI::Behaviors::ScriptProcessorRuleBehavior::Reflect(context);
  61. AZ::SceneAPI::Events::ExportProductList::Reflect(context);
  62. }
  63. void SetUp() override
  64. {
  65. UnitTest::LeakDetectionFixture::SetUp();
  66. AZ::NameDictionary::Create();
  67. m_data.reset(new DataMembers);
  68. m_serializeContext = AZStd::make_unique<AZ::SerializeContext>();
  69. m_serializeContext->Class<AZ::SceneAPI::DataTypes::IRule, AZ::SceneAPI::DataTypes::IManifestObject>()->Version(1);
  70. AZ::SceneAPI::RegisterDataTypeReflection(m_serializeContext.get());
  71. ReflectTypes(m_serializeContext.get());
  72. m_behaviorContext = AZStd::make_unique<AZ::BehaviorContext>();
  73. ReflectTypes(m_behaviorContext.get());
  74. AZ::MathReflect(m_behaviorContext.get());
  75. m_behaviorContext->Method("TestExpectTrue", &TestExpectTrue);
  76. m_behaviorContext->Method("TestEqualNumbers", &TestEqualNumbers);
  77. m_behaviorContext->Method("TestEqualStrings", &TestEqualStrings);
  78. UnitTest::ScopeForUnitTest(m_behaviorContext->m_classes.find("Scene")->second->m_attributes);
  79. UnitTest::ScopeForUnitTest(m_behaviorContext->m_ebuses.find("ScriptBuildingNotificationBus")->second->m_attributes);
  80. m_scriptContext = AZStd::make_unique<AZ::ScriptContext>();
  81. m_scriptContext->BindTo(m_behaviorContext.get());
  82. using FixedValueString = AZ::SettingsRegistryInterface::FixedValueString;
  83. ON_CALL(m_data->m_settings, Get(::testing::Matcher<FixedValueString&>(::testing::_), testing::_))
  84. .WillByDefault([](FixedValueString& value, AZStd::string_view) -> bool
  85. {
  86. value = "mock_path";
  87. return true;
  88. });
  89. AZ::SettingsRegistry::Register(&m_data->m_settings);
  90. }
  91. void TearDown() override
  92. {
  93. m_scriptContext.reset();
  94. m_serializeContext.reset();
  95. m_behaviorContext.reset();
  96. AZ::SettingsRegistry::Unregister(&m_data->m_settings);
  97. m_data.reset();
  98. CleanUpSceneDataGenericClassInfo();
  99. CleanUpSceneCoreGenericClassInfo();
  100. AZ::NameDictionary::Destroy();
  101. UnitTest::LeakDetectionFixture::TearDown();
  102. }
  103. struct DataMembers
  104. {
  105. int m_count = 0;
  106. AZ::NiceSettingsRegistrySimpleMock m_settings;
  107. };
  108. AZStd::unique_ptr<AZ::SerializeContext> m_serializeContext;
  109. AZStd::unique_ptr<AZ::ScriptContext> m_scriptContext;
  110. AZStd::unique_ptr<AZ::BehaviorContext> m_behaviorContext;
  111. AZStd::unique_ptr<DataMembers> m_data;
  112. };
  113. TEST_F(SceneScriptTest, Scene_ScriptBuildingNotificationBus_Exists)
  114. {
  115. ExpectExecute("TestExpectTrue(ScriptBuildingNotificationBus ~= nil)");
  116. ExpectExecute("self = {}");
  117. ExpectExecute("self.handler = ScriptBuildingNotificationBus.Connect(self)");
  118. ExpectExecute("TestExpectTrue(self.handler ~= nil)");
  119. }
  120. TEST_F(SceneScriptTest, Scene_ScriptBuildingNotificationBus_OnUpdateManifestCalled)
  121. {
  122. const char* handlerScript = R"LUA(
  123. local ScriptSample = {
  124. OnUpdateManifest = function (self, scene)
  125. TestEqualStrings(scene.name, 'test')
  126. return ''
  127. end
  128. }
  129. scene = Scene('test')
  130. ScriptSample.handler = ScriptBuildingNotificationBus.Connect(ScriptSample)
  131. manifest = ScriptBuildingNotificationBus.Broadcast.OnUpdateManifest(scene)
  132. ScriptSample.handler:Disconnect()
  133. )LUA";
  134. ExpectExecute(handlerScript);
  135. }
  136. TEST_F(SceneScriptTest, Scene_ScriptBuildingNotificationBus_OnUpdateManifestClearsHandler)
  137. {
  138. const char* handlerScript = R"LUA(
  139. local ScriptSample = {
  140. OnUpdateManifest = function (self, scene)
  141. TestEqualStrings(scene.name, 'test')
  142. self.handler:Disconnect()
  143. self.handler = nil
  144. return ''
  145. end
  146. }
  147. scene = Scene('test')
  148. ScriptSample.handler = ScriptBuildingNotificationBus.Connect(ScriptSample)
  149. manifest = ScriptBuildingNotificationBus.Broadcast.OnUpdateManifest(scene)
  150. )LUA";
  151. ExpectExecute(handlerScript);
  152. AZ::SceneAPI::Events::AssetPostImportRequestBus::ExecuteQueuedEvents();
  153. }
  154. }