BuilderTestFixture.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <AzCore/Asset/AssetManager.h>
  9. #include <AzCore/Asset/AssetManagerComponent.h>
  10. #include <AzCore/IO/Streamer/StreamerComponent.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/PlatformIncl.h>
  13. #include <AzCore/Name/NameDictionary.h>
  14. #include <AzCore/Utils/Utils.h>
  15. #include <AzFramework/IO/LocalFileIO.h>
  16. #include <Atom/RHI.Reflect/ReflectSystemComponent.h>
  17. #include <Atom/RPI.Public/RPISystem.h>
  18. #include <BuilderComponent.h>
  19. #include <Tests.Builders/BuilderTestFixture.h>
  20. namespace UnitTest
  21. {
  22. using namespace AZ;
  23. // Expose AssetManagerComponent::Reflect function for testing
  24. class MyAssetManagerComponent : public AssetManagerComponent
  25. {
  26. public:
  27. static void Reflect(ReflectContext* reflection)
  28. {
  29. AssetManagerComponent::Reflect(reflection);
  30. }
  31. };
  32. SerializeContext* BuilderTestFixture::GetSerializeContext()
  33. {
  34. return m_context.get();
  35. }
  36. JsonRegistrationContext* BuilderTestFixture::GetJsonRegistrationContext()
  37. {
  38. return m_jsonRegistrationContext.get();
  39. }
  40. void BuilderTestFixture::Reflect(ReflectContext* context)
  41. {
  42. RHI::ReflectSystemComponent::Reflect(context);
  43. RPI::RPISystem::Reflect(context);
  44. RPI::BuilderComponent::Reflect(context);
  45. AZ::Name::Reflect(context);
  46. MyAssetManagerComponent::Reflect(context);
  47. }
  48. void BuilderTestFixture::SetUp()
  49. {
  50. LeakDetectionFixture::SetUp();
  51. //prepare reflection
  52. m_context = AZStd::make_unique<SerializeContext>();
  53. m_jsonRegistrationContext = AZStd::make_unique<AZ::JsonRegistrationContext>();
  54. m_jsonSystemComponent = AZStd::make_unique<AZ::JsonSystemComponent>();
  55. m_jsonSystemComponent->Reflect(m_jsonRegistrationContext.get());
  56. // Adding this handler to allow utility functions access the serialize context
  57. ComponentApplicationBus::Handler::BusConnect();
  58. AZ::Interface<AZ::ComponentApplicationRequests>::Register(this);
  59. // Startup default local FileIO (hits OSAllocator) if not already setup.
  60. if (IO::FileIOBase::GetInstance() == nullptr)
  61. {
  62. IO::FileIOBase::SetInstance(aznew IO::LocalFileIO());
  63. }
  64. NameDictionary::Create();
  65. Reflect(m_context.get());
  66. Reflect(m_jsonRegistrationContext.get());
  67. m_streamer = AZStd::make_unique<AZ::IO::Streamer>(AZStd::thread_desc{}, AZ::StreamerComponent::CreateStreamerStack());
  68. Interface<AZ::IO::IStreamer>::Register(m_streamer.get());
  69. Data::AssetManager::Descriptor desc;
  70. Data::AssetManager::Create(desc);
  71. AZ::Utils::GetExecutableDirectory(m_currentDir, AZ_MAX_PATH_LEN);
  72. }
  73. void BuilderTestFixture::TearDown()
  74. {
  75. Data::AssetManager::Destroy();
  76. Interface<AZ::IO::IStreamer>::Unregister(m_streamer.get());
  77. m_streamer.reset();
  78. delete IO::FileIOBase::GetInstance();
  79. IO::FileIOBase::SetInstance(nullptr);
  80. AZ::Interface<AZ::ComponentApplicationRequests>::Unregister(this);
  81. ComponentApplicationBus::Handler::BusDisconnect();
  82. m_jsonRegistrationContext->EnableRemoveReflection();
  83. m_jsonSystemComponent->Reflect(m_jsonRegistrationContext.get());
  84. Reflect(m_jsonRegistrationContext.get());
  85. m_jsonRegistrationContext->DisableRemoveReflection();
  86. m_jsonRegistrationContext.reset();
  87. m_jsonSystemComponent.reset();
  88. NameDictionary::Destroy();
  89. m_context.reset();
  90. LeakDetectionFixture::TearDown();
  91. }
  92. } // namespace UnitTest