InitSceneAPIFixture.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/std/containers/vector.h>
  10. #include <AzCore/std/smart_ptr/unique_ptr.h>
  11. #include <AzCore/Module/DynamicModuleHandle.h>
  12. #include "SystemComponentFixture.h"
  13. namespace AZ { class DynamicModuleHandle; }
  14. namespace EMotionFX
  15. {
  16. template<class... Components>
  17. class InitSceneAPIFixture : public ComponentFixture<Components...>
  18. {
  19. using DynamicModuleHandlePtr = AZStd::unique_ptr<AZ::DynamicModuleHandle>;
  20. protected:
  21. InitSceneAPIFixture() = default;
  22. AZ_DEFAULT_COPY_MOVE(InitSceneAPIFixture);
  23. void PreStart() override
  24. {
  25. const AZStd::vector<AZStd::string> moduleNames {"SceneCore", "SceneData"};
  26. for (const AZStd::string& moduleName : moduleNames)
  27. {
  28. AZStd::unique_ptr<AZ::DynamicModuleHandle> module = AZ::DynamicModuleHandle::Create(moduleName.c_str());
  29. ASSERT_TRUE(module) << "EMotionFX Editor unit tests failed to create " << moduleName.c_str() << " module.";
  30. const bool loaded = module->Load();
  31. ASSERT_TRUE(loaded) << "EMotionFX Editor unit tests failed to load " << moduleName.c_str() << " module.";
  32. auto init = module->GetFunction<AZ::InitializeDynamicModuleFunction>(AZ::InitializeDynamicModuleFunctionName);
  33. ASSERT_TRUE(init) << "EMotionFX Editor unit tests failed to find the initialization function the " << moduleName.c_str() << " module.";
  34. (*init)();
  35. m_modules.emplace_back(AZStd::move(module));
  36. }
  37. ComponentFixture<Components...>::PreStart();
  38. }
  39. ~InitSceneAPIFixture() override
  40. {
  41. // Deactivate the system entity first, releasing references to SceneAPI
  42. if (this->GetSystemEntity()->GetState() == AZ::Entity::State::Active)
  43. {
  44. this->GetSystemEntity()->Deactivate();
  45. }
  46. // Remove SceneAPI components before the DLL is uninitialized
  47. (([&]() {
  48. auto component = this->GetSystemEntity()->template FindComponent<Components>();
  49. if (component)
  50. {
  51. this->GetSystemEntity()->RemoveComponent(component);
  52. delete component;
  53. }
  54. })(), ...);
  55. // Now tear down SceneAPI
  56. for (DynamicModuleHandlePtr& module : m_modules)
  57. {
  58. auto uninit = module->template GetFunction<AZ::UninitializeDynamicModuleFunction>(AZ::UninitializeDynamicModuleFunctionName);
  59. (*uninit)();
  60. module.reset();
  61. }
  62. m_modules.clear();
  63. m_modules.shrink_to_fit();
  64. }
  65. private:
  66. AZStd::vector<DynamicModuleHandlePtr> m_modules;
  67. };
  68. } // namespace EMotionFX