NvClothEditorTestEnvironment.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/GemTestEnvironment.h>
  9. #include <AzCore/UserSettings/UserSettingsComponent.h>
  10. #include <AzFramework/IO/LocalFileIO.h>
  11. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  12. #include <AzToolsFramework/UnitTest/ToolsTestApplication.h>
  13. #include <System/FabricCooker.h>
  14. #include <System/TangentSpaceHelper.h>
  15. #include <System/SystemComponent.h>
  16. #include <Components/ClothComponent.h>
  17. #include <Editor/EditorSystemComponent.h>
  18. #include <Components/EditorClothComponent.h>
  19. #include <Pipeline/SceneAPIExt/ClothRuleBehavior.h>
  20. namespace UnitTest
  21. {
  22. class NvClothToolsTestApplication
  23. : public ToolsTestApplication
  24. {
  25. public:
  26. explicit NvClothToolsTestApplication(AZStd::string applicationName)
  27. : ToolsTestApplication(applicationName)
  28. {
  29. }
  30. };
  31. //! Sets up gem test environment, required components, and shared objects used by cloth (e.g. FabricCooker) for all test cases.
  32. class NvClothEditorTestEnvironment
  33. : public AZ::Test::GemTestEnvironment
  34. {
  35. public:
  36. // AZ::Test::GemTestEnvironment overrides ...
  37. void AddGemsAndComponents() override;
  38. void PreCreateApplication() override;
  39. void PostSystemEntityActivate() override;
  40. void PostDestroyApplication() override;
  41. AZ::ComponentApplication* CreateApplicationInstance() override;
  42. private:
  43. AZStd::unique_ptr<NvCloth::FabricCooker> m_fabricCooker;
  44. AZStd::unique_ptr<NvCloth::TangentSpaceHelper> m_tangentSpaceHelper;
  45. AZStd::unique_ptr<AZ::IO::LocalFileIO> m_fileIO;
  46. };
  47. void NvClothEditorTestEnvironment::AddGemsAndComponents()
  48. {
  49. AddDynamicModulePaths({
  50. "LmbrCentral.Editor",
  51. "EMotionFX.Editor"
  52. });
  53. AddComponentDescriptors({
  54. NvCloth::SystemComponent::CreateDescriptor(),
  55. NvCloth::ClothComponent::CreateDescriptor(),
  56. NvCloth::EditorSystemComponent::CreateDescriptor(),
  57. NvCloth::EditorClothComponent::CreateDescriptor(),
  58. NvCloth::Pipeline::ClothRuleBehavior::CreateDescriptor(),
  59. });
  60. AddRequiredComponents({
  61. NvCloth::SystemComponent::TYPEINFO_Uuid(),
  62. NvCloth::EditorSystemComponent::TYPEINFO_Uuid(),
  63. });
  64. }
  65. void NvClothEditorTestEnvironment::PreCreateApplication()
  66. {
  67. NvCloth::SystemComponent::InitializeNvClothLibrary(); // SystemAllocator creation must come before this call.
  68. m_fabricCooker = AZStd::make_unique<NvCloth::FabricCooker>();
  69. m_tangentSpaceHelper = AZStd::make_unique<NvCloth::TangentSpaceHelper>();
  70. // EMotionFX SystemComponent activation requires a valid AZ::IO::LocalFileIO
  71. m_fileIO = AZStd::make_unique<AZ::IO::LocalFileIO>();
  72. AZ::IO::FileIOBase::SetInstance(m_fileIO.get());
  73. }
  74. void NvClothEditorTestEnvironment::PostSystemEntityActivate()
  75. {
  76. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  77. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  78. // in the unit tests.
  79. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  80. }
  81. void NvClothEditorTestEnvironment::PostDestroyApplication()
  82. {
  83. AZ::IO::FileIOBase::SetInstance(nullptr);
  84. m_fileIO.reset();
  85. m_tangentSpaceHelper.reset();
  86. m_fabricCooker.reset();
  87. NvCloth::SystemComponent::TearDownNvClothLibrary(); // SystemAllocator destruction must come after this call.
  88. }
  89. AZ::ComponentApplication* NvClothEditorTestEnvironment::CreateApplicationInstance()
  90. {
  91. return aznew NvClothToolsTestApplication("NvClothEditorTests");
  92. }
  93. } // namespace UnitTest
  94. AZ_TOOLS_UNIT_TEST_HOOK(new UnitTest::NvClothEditorTestEnvironment);