FrameworkApplicationFixture.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/typetraits/alignment_of.h>
  10. #include <AzCore/std/typetraits/aligned_storage.h>
  11. #include <AzFramework/Application/Application.h>
  12. #include <AzCore/UserSettings/UserSettingsComponent.h>
  13. #include <AzCore/IO/SystemFile.h>
  14. #include <AzCore/UnitTest/TestTypes.h>
  15. namespace AZ
  16. {
  17. class Entity;
  18. }
  19. namespace UnitTest
  20. {
  21. /**
  22. * Test fixture that starts up an AzFramework::Application.
  23. */
  24. class FrameworkApplicationFixture
  25. : public UnitTest::LeakDetectionFixture
  26. {
  27. protected:
  28. // HACK: Special Application that excludes UserSettingsComponent.
  29. // For some reason unit tests for different branches will read/write the same UserSettings.xml file,
  30. // but those tests may have different versions of serialization code for writing UserSettings.xml, thus
  31. // causing version conflicts. Ideally unit tests should not interact with physical files on disk, after
  32. // we fix this problem NoUserSettingsApplication should be removed, and we can use AzFramework::Application directly.
  33. class NoUserSettingsApplication
  34. : public AzFramework::Application
  35. {
  36. AZ::ComponentTypeList GetRequiredSystemComponents() const override
  37. {
  38. AZ::ComponentTypeList components = AzFramework::Application::GetRequiredSystemComponents();
  39. AZ::ComponentTypeList::iterator componentItr = AZStd::find(components.begin(), components.end(), azrtti_typeid<AZ::UserSettingsComponent>());
  40. if (componentItr != components.end())
  41. {
  42. components.erase(componentItr);
  43. }
  44. return components;
  45. }
  46. };
  47. void SetUp() override
  48. {
  49. m_appDescriptor.m_allocationRecordsSaveNames = true;
  50. m_appDescriptor.m_recordingMode = AZ::Debug::AllocationRecords::Mode::RECORD_FULL;
  51. m_application = new (AZStd::addressof(m_applicationBuffer)) NoUserSettingsApplication();
  52. AZ::ComponentApplication::StartupParameters startupParameters;
  53. m_appStartupParams.m_loadSettingsRegistry = false;
  54. m_application->Start(m_appDescriptor, m_appStartupParams);
  55. }
  56. void TearDown() override
  57. {
  58. m_application->~Application();
  59. m_application = nullptr;
  60. // Reset so next test can assume blank slate.
  61. m_appStartupParams = AzFramework::Application::StartupParameters();
  62. m_appDescriptor = AzFramework::Application::Descriptor();
  63. }
  64. // Customize the descriptor before SetUp() to affect the application's startup.
  65. AzFramework::Application::Descriptor m_appDescriptor;
  66. // Customize the startup params before SetUp() to affect the application's startup.
  67. AzFramework::Application::StartupParameters m_appStartupParams;
  68. // Can't store on the stack because the object must be properly destroyed on shutdown.
  69. // Can't use unique_ptr yet because the allocators aren't up yet.
  70. AZStd::aligned_storage<sizeof(NoUserSettingsApplication), AZStd::alignment_of<NoUserSettingsApplication>::value>::type m_applicationBuffer;
  71. AzFramework::Application* m_application;
  72. };
  73. }