ScriptAutomationApplicationFixture.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 <ScriptAutomationApplicationFixture.h>
  9. #include <ScriptAutomation/ScriptAutomationBus.h>
  10. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  11. #include <AzCore/UserSettings/UserSettingsComponent.h>
  12. #include <AzFramework/IO/LocalFileIO.h>
  13. namespace UnitTest
  14. {
  15. void ScriptAutomationApplicationFixture::TearDown()
  16. {
  17. if (m_application)
  18. {
  19. DestroyApplication();
  20. }
  21. LeakDetectionFixture::TearDown();
  22. }
  23. AzFramework::Application* ScriptAutomationApplicationFixture::CreateApplication(const char* scriptPath, bool exitOnFinish)
  24. {
  25. // The ScriptAutomation gem uses the AssetManager to load the script assets. The AssetManager will try to make the asset path relative to
  26. // the cache root folder. If we pass in an abosolute path to the asset, the AssetManager ends up removing the leading slash on MacOS
  27. // and Linux in the call to Application::MakePathRelative. So we need to override the cache path for these tests. First, we override
  28. // the asset platform folder since we're going to be reading from the gem tests folder for all platforms.
  29. const auto cachePlatformOverride =
  30. AZ::StringFunc::Path::FixedString::format("--regset=%s/%s_assets=.", AZ::SettingsRegistryMergeUtils::BootstrapSettingsRootKey, AZ_TRAIT_OS_PLATFORM_CODENAME_LOWER);
  31. if (scriptPath)
  32. {
  33. m_args.push_back(cachePlatformOverride.c_str());
  34. m_args.push_back("--run-automation-suite ");
  35. m_args.push_back(scriptPath);
  36. if (exitOnFinish)
  37. {
  38. m_args.push_back("--exit-on-automation-end");
  39. }
  40. }
  41. int argc = aznumeric_cast<int>(m_args.size());
  42. char** argv = const_cast<char**>(m_args.data());
  43. m_application = aznew AzFramework::Application(&argc, &argv);
  44. // ensure the ScriptAutomation gem is active
  45. AZ::Test::AddActiveGem("ScriptAutomation", *AZ::SettingsRegistry::Get(), AZ::IO::FileIOBase::GetInstance());
  46. AZ::ComponentApplication::Descriptor appDesc;
  47. appDesc.m_useExistingAllocator = true;
  48. AZ::DynamicModuleDescriptor dynamicModuleDescriptor;
  49. dynamicModuleDescriptor.m_dynamicLibraryPath = "ScriptAutomation";
  50. appDesc.m_modules.push_back(dynamicModuleDescriptor);
  51. m_application->Start(appDesc);
  52. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  53. return m_application;
  54. }
  55. void ScriptAutomationApplicationFixture::DestroyApplication()
  56. {
  57. m_application->Stop();
  58. delete m_application;
  59. m_application = nullptr;
  60. }
  61. } // namespace UnitTest