NvClothTestEnvironment.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <AzFramework/Components/TransformComponent.h>
  12. #include <System/FabricCooker.h>
  13. #include <System/TangentSpaceHelper.h>
  14. #include <System/SystemComponent.h>
  15. #include <Components/ClothComponent.h>
  16. namespace UnitTest
  17. {
  18. //! Sets up gem test environment, required components, and shared objects used by cloth (e.g. FabricCooker) for all test cases.
  19. class NvClothTestEnvironment
  20. : public AZ::Test::GemTestEnvironment
  21. {
  22. public:
  23. // AZ::Test::GemTestEnvironment overrides ...
  24. void AddGemsAndComponents() override;
  25. void PreCreateApplication() override;
  26. void PostSystemEntityActivate() override;
  27. void PostDestroyApplication() override;
  28. private:
  29. AZStd::unique_ptr<NvCloth::FabricCooker> m_fabricCooker;
  30. AZStd::unique_ptr<NvCloth::TangentSpaceHelper> m_tangentSpaceHelper;
  31. AZStd::unique_ptr<AZ::IO::LocalFileIO> m_fileIO;
  32. };
  33. void NvClothTestEnvironment::AddGemsAndComponents()
  34. {
  35. AddDynamicModulePaths({
  36. "LmbrCentral",
  37. "EMotionFX"
  38. });
  39. AddComponentDescriptors({
  40. AzFramework::TransformComponent::CreateDescriptor(),
  41. NvCloth::SystemComponent::CreateDescriptor(),
  42. NvCloth::ClothComponent::CreateDescriptor()
  43. });
  44. AddRequiredComponents({
  45. NvCloth::SystemComponent::TYPEINFO_Uuid()
  46. });
  47. }
  48. void NvClothTestEnvironment::PreCreateApplication()
  49. {
  50. NvCloth::SystemComponent::InitializeNvClothLibrary(); // SystemAllocator creation must come before this call.
  51. m_fabricCooker = AZStd::make_unique<NvCloth::FabricCooker>();
  52. m_tangentSpaceHelper = AZStd::make_unique<NvCloth::TangentSpaceHelper>();
  53. // EMotionFX SystemComponent activation requires a valid AZ::IO::LocalFileIO
  54. m_fileIO = AZStd::make_unique<AZ::IO::LocalFileIO>();
  55. AZ::IO::FileIOBase::SetInstance(m_fileIO.get());
  56. }
  57. void NvClothTestEnvironment::PostSystemEntityActivate()
  58. {
  59. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  60. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  61. // in the unit tests.
  62. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  63. }
  64. void NvClothTestEnvironment::PostDestroyApplication()
  65. {
  66. AZ::IO::FileIOBase::SetInstance(nullptr);
  67. m_fileIO.reset();
  68. m_tangentSpaceHelper.reset();
  69. m_fabricCooker.reset();
  70. NvCloth::SystemComponent::TearDownNvClothLibrary(); // SystemAllocator destruction must come after this call.
  71. }
  72. } // namespace UnitTest
  73. AZ_UNIT_TEST_HOOK(new UnitTest::NvClothTestEnvironment);