PythonTestingUtility.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #pragma once
  9. #include <AzCore/Component/ComponentApplication.h>
  10. #include <AzCore/Memory/AllocationRecords.h>
  11. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  12. #include <AzFramework/IO/LocalFileIO.h>
  13. #include <AzFramework/Application/Application.h>
  14. #include <AzFramework/CommandLine/CommandRegistrationBus.h>
  15. #include <AzQtComponents/Utilities/QtPluginPaths.h>
  16. #include <AzTest/AzTest.h>
  17. #include <AzCore/UnitTest/TestTypes.h>
  18. #include <AzFramework/API/ApplicationAPI.h>
  19. #include <AzToolsFramework/API/EditorPythonConsoleBus.h>
  20. #include <Source/PythonSystemComponent.h>
  21. #include <Source/PythonReflectionComponent.h>
  22. #include <Source/PythonMarshalComponent.h>
  23. namespace UnitTest
  24. {
  25. struct CommandRegistrationBusSupression
  26. : public AzFramework::CommandRegistrationBus::Handler
  27. {
  28. CommandRegistrationBusSupression()
  29. {
  30. BusConnect();
  31. }
  32. ~CommandRegistrationBusSupression()
  33. {
  34. BusDisconnect();
  35. }
  36. bool RegisterCommand(AZStd::string_view, AZStd::string_view, AZ::u32, AzFramework::CommandFunction) override
  37. {
  38. return true;
  39. }
  40. bool UnregisterCommand(AZStd::string_view) override
  41. {
  42. return true;
  43. }
  44. };
  45. struct PythonTestingFixture
  46. : public ::testing::Test
  47. , protected AzFramework::ApplicationRequests::Bus::Handler
  48. {
  49. class FileIOHelper
  50. {
  51. public:
  52. AZ::IO::LocalFileIO m_fileIO;
  53. AZ::IO::FileIOBase* m_prevFileIO;
  54. FileIOHelper()
  55. {
  56. m_prevFileIO = AZ::IO::FileIOBase::GetInstance();
  57. AZ::IO::FileIOBase::SetInstance(&m_fileIO);
  58. }
  59. ~FileIOHelper()
  60. {
  61. AZ::IO::FileIOBase::SetInstance(m_prevFileIO);
  62. }
  63. };
  64. void SetUp() override
  65. {
  66. // fetch the Engine Root folder
  67. if (auto settingsRegistry = AZ::SettingsRegistry::Get(); settingsRegistry != nullptr)
  68. {
  69. settingsRegistry->Get(m_engineRoot.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  70. }
  71. m_fileIOHelper = AZStd::make_unique<FileIOHelper>();
  72. m_fileIOHelper->m_fileIO.SetAlias("@engroot@", m_engineRoot.c_str());
  73. AzFramework::Application::Descriptor appDesc;
  74. appDesc.m_recordingMode = AZ::Debug::AllocationRecords::Mode::RECORD_NO_RECORDS;
  75. AZ::ComponentApplication::StartupParameters startupParameters;
  76. startupParameters.m_loadSettingsRegistry = false;
  77. m_app.Create(appDesc, startupParameters);
  78. AzFramework::ApplicationRequests::Bus::Handler::BusConnect();
  79. }
  80. void TearDown() override
  81. {
  82. AzFramework::ApplicationRequests::Bus::Handler::BusDisconnect();
  83. m_commandRegistrationBusSupression.reset();
  84. m_fileIOHelper.reset();
  85. m_app.Destroy();
  86. }
  87. void SimulateEditorBecomingInitialized(bool useCommandRegistrationBusSupression = true)
  88. {
  89. if (useCommandRegistrationBusSupression)
  90. {
  91. m_commandRegistrationBusSupression = AZStd::make_unique<CommandRegistrationBusSupression>();
  92. }
  93. auto editorPythonEventsInterface = AZ::Interface<AzToolsFramework::EditorPythonEventsInterface>::Get();
  94. if (editorPythonEventsInterface)
  95. {
  96. editorPythonEventsInterface->StartPython();
  97. }
  98. }
  99. void RegisterComponentDescriptors()
  100. {
  101. m_app.RegisterComponentDescriptor(EditorPythonBindings::PythonSystemComponent::CreateDescriptor());
  102. m_app.RegisterComponentDescriptor(EditorPythonBindings::PythonReflectionComponent::CreateDescriptor());
  103. m_app.RegisterComponentDescriptor(EditorPythonBindings::PythonMarshalComponent::CreateDescriptor());
  104. }
  105. void Activate(AZ::Entity& e)
  106. {
  107. e.CreateComponent<EditorPythonBindings::PythonSystemComponent>();
  108. e.CreateComponent<EditorPythonBindings::PythonReflectionComponent>();
  109. e.CreateComponent<EditorPythonBindings::PythonMarshalComponent>();
  110. e.Init();
  111. e.Activate();
  112. }
  113. //////////////////////////////////////////////////////////////////////////
  114. // AzFramework::ApplicationRequests::Bus::Handler
  115. // required pure virtual overrides
  116. void NormalizePath(AZStd::string& ) override {}
  117. void NormalizePathKeepCase(AZStd::string& ) override {}
  118. void CalculateBranchTokenForEngineRoot(AZStd::string& ) const override {}
  119. AZ::ComponentApplication m_app;
  120. AZStd::unique_ptr<FileIOHelper> m_fileIOHelper;
  121. AZStd::unique_ptr<CommandRegistrationBusSupression> m_commandRegistrationBusSupression;
  122. AZ::IO::FixedMaxPath m_engineRoot;
  123. };
  124. }