LmbrCentralReflectionTest.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <AzTest/AzTest.h>
  10. #include <AzCore/Memory/AllocationRecords.h>
  11. #include <AzCore/Serialization/Utils.h>
  12. #include <AzCore/Component/ComponentApplication.h>
  13. #include <AzCore/UnitTest/TestTypes.h>
  14. #include <AzFramework/Application/Application.h>
  15. #ifdef LMBR_CENTRAL_EDITOR
  16. #include <AzToolsFramework/Application/ToolsApplication.h>
  17. #include "LmbrCentralEditor.h"
  18. #endif
  19. /**
  20. * Fixture class for tests that require a module to have been reflected.
  21. * An AZ application is created to handle the reflection. The application
  22. * starts up and shuts down only once for all tests using this fixture.
  23. * \tparam ApplicationT Type of AZ::ComponentApplication to use.
  24. * \tparam ModuleT Type of AZ::Module to reflect. It must be defined within this library.
  25. */
  26. template<class ApplicationT, class ModuleT>
  27. class ModuleReflectionTest
  28. : public UnitTest::LeakDetectionFixture
  29. {
  30. public:
  31. static ApplicationT* GetApplication() { return s_application.get(); }
  32. protected:
  33. void SetUp() override;
  34. void TearDown() override;
  35. private:
  36. // We need reflection from ApplicationT and nothing more.
  37. // This class lets us simplify the application that we run for tests.
  38. class InternalApplication : public ApplicationT
  39. {
  40. public:
  41. // Unhide these core startup/shutdown functions (They're 'protected' in AzFramework::Application)
  42. using ApplicationT::Create;
  43. using ApplicationT::Destroy;
  44. // Don't create any system components.
  45. AZ::ComponentTypeList GetRequiredSystemComponents() const override { return AZ::ComponentTypeList(); }
  46. };
  47. static AZStd::unique_ptr<InternalApplication> s_application;
  48. static AZ::Entity* s_systemEntity;
  49. };
  50. /**
  51. * Fixture class for tests that load an object whose class is reflected within a module.
  52. * Upon setup, the object is loaded from a source data buffer.
  53. * \tparam ApplicationT Type of AZ::ComponentApplication to start.
  54. * \tparam ModuleT Type of AZ::Module to reflect. It must be defined within this library.
  55. * \tparam ObjectT Type of object to load from buffer.
  56. */
  57. template<class ApplicationT, class ModuleT, class ObjectT>
  58. class LoadReflectedObjectTest
  59. : public ModuleReflectionTest<ApplicationT, ModuleT>
  60. {
  61. typedef ModuleReflectionTest<ApplicationT, ModuleT> BaseType;
  62. protected:
  63. void SetUp() override;
  64. void TearDown() override;
  65. virtual const char* GetSourceDataBuffer() const = 0;
  66. AZStd::unique_ptr<ObjectT> m_object;
  67. };
  68. #ifdef LMBR_CENTRAL_EDITOR
  69. /**
  70. * Creates, registers a dummy transform component for Editor Component Tests
  71. * Manages an entity for the editor component
  72. */
  73. template<class ComponentT>
  74. class LoadEditorComponentTest
  75. : public LoadReflectedObjectTest<AzToolsFramework::ToolsApplication, LmbrCentral::LmbrCentralEditorModule, ComponentT>
  76. {
  77. public:
  78. using LoadReflectedObjectTestBase = LoadReflectedObjectTest<AzToolsFramework::ToolsApplication, LmbrCentral::LmbrCentralEditorModule, ComponentT>;
  79. void SetUp() override;
  80. void TearDown() override;
  81. protected:
  82. // simply fulfills the transform component dependency on editor components
  83. class DummyTransformComponent
  84. : public AZ::Component
  85. {
  86. public:
  87. AZ_COMPONENT(DummyTransformComponent, "{971C64A3-C9FB-4ADB-B122-BC579A889CD4}", AZ::Component);
  88. void Activate() override {};
  89. void Deactivate() override {};
  90. protected:
  91. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  92. {
  93. provided.push_back(AZ_CRC_CE("TransformService"));
  94. }
  95. static void Reflect(AZ::ReflectContext* reflection)
  96. {
  97. AZ_UNUSED(reflection);
  98. }
  99. };
  100. AZStd::unique_ptr<AZ::Entity> m_entity;
  101. AZ::ComponentDescriptor* m_transformComponentDescriptor{};
  102. };
  103. #endif
  104. template<class ApplicationT, class ModuleT>
  105. void ModuleReflectionTest<ApplicationT, ModuleT>::SetUp()
  106. {
  107. s_application.reset(new ModuleReflectionTest::InternalApplication);
  108. AZ::ComponentApplication::Descriptor appDescriptor;
  109. appDescriptor.m_useExistingAllocator = true;
  110. appDescriptor.m_recordingMode = AZ::Debug::AllocationRecords::Mode::RECORD_FULL;
  111. AZ::ComponentApplication::StartupParameters appStartup;
  112. // ModuleT is declared within this dll.
  113. // Therefore, we can treat it like a statically linked module.
  114. appStartup.m_createStaticModulesCallback =
  115. [](AZStd::vector<AZ::Module*>& modules)
  116. {
  117. modules.emplace_back(new ModuleT);
  118. };
  119. // Create() starts the application and returns the system entity.
  120. s_systemEntity = s_application->Create(appDescriptor, appStartup);
  121. }
  122. template<class ApplicationT, class ModuleT>
  123. void ModuleReflectionTest<ApplicationT, ModuleT>::TearDown()
  124. {
  125. s_application->GetSerializeContext()->DestroyEditContext();
  126. s_systemEntity = nullptr;
  127. s_application->Destroy();
  128. s_application.reset();
  129. }
  130. template<class ApplicationT, class ModuleT>
  131. AZStd::unique_ptr<typename ModuleReflectionTest<ApplicationT, ModuleT>::InternalApplication> ModuleReflectionTest<ApplicationT, ModuleT>::s_application;
  132. template<class ApplicationT, class ModuleT>
  133. AZ::Entity* ModuleReflectionTest<ApplicationT, ModuleT>::s_systemEntity = nullptr;
  134. template<class ApplicationT, class ModuleT, class ObjectT>
  135. void LoadReflectedObjectTest<ApplicationT, ModuleT, ObjectT>::SetUp()
  136. {
  137. BaseType::SetUp();
  138. const char* buffer = GetSourceDataBuffer();
  139. if (buffer)
  140. {
  141. // don't load any assets referenced from the data
  142. AZ::ObjectStream::FilterDescriptor filter;
  143. filter.m_assetCB = AZ::Data::AssetFilterNoAssetLoading;
  144. m_object.reset(AZ::Utils::LoadObjectFromBuffer<ObjectT>(buffer, strlen(buffer) + 1, this->GetApplication()->GetSerializeContext(), filter));
  145. }
  146. }
  147. template<class ApplicationT, class ModuleT, class ObjectT>
  148. void LoadReflectedObjectTest<ApplicationT, ModuleT, ObjectT>::TearDown()
  149. {
  150. m_object.reset();
  151. BaseType::TearDown();
  152. }
  153. #ifdef LMBR_CENTRAL_EDITOR
  154. template<class ComponentT>
  155. void LoadEditorComponentTest<ComponentT>::SetUp()
  156. {
  157. this->m_transformComponentDescriptor = DummyTransformComponent::CreateDescriptor();
  158. AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationRequests::RegisterComponentDescriptor, this->m_transformComponentDescriptor);
  159. m_entity = AZStd::make_unique<AZ::Entity>("LoadEditorComponentTestEntity");
  160. m_entity->Init();
  161. LoadReflectedObjectTestBase::SetUp();
  162. m_entity->AddComponent(aznew DummyTransformComponent());
  163. if (this->m_object)
  164. {
  165. m_entity->AddComponent(this->m_object.get());
  166. }
  167. m_entity->Activate();
  168. }
  169. template<class ComponentT>
  170. void LoadEditorComponentTest<ComponentT>::TearDown()
  171. {
  172. m_entity->Deactivate();
  173. if (this->m_object)
  174. {
  175. m_entity->RemoveComponent(this->m_object.get());
  176. }
  177. LoadReflectedObjectTestBase::TearDown();
  178. m_entity.reset();
  179. AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationRequests::UnregisterComponentDescriptor, this->m_transformComponentDescriptor);
  180. this->m_transformComponentDescriptor = nullptr;
  181. }
  182. #endif