ComponentAdapterTests.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/AzTest.h>
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzFramework/Components/ComponentAdapter.h>
  11. #include <AzToolsFramework/ToolsComponents/EditorComponentAdapter.h>
  12. #include <CustomSerializeContextTestFixture.h>
  13. namespace UnitTest
  14. {
  15. static bool s_activateCalled = false;
  16. static bool s_deactivateCalled = false;
  17. struct TestConfig
  18. : public AZ::ComponentConfig
  19. {
  20. AZ_RTTI(TestConfig, "{835CF711-77DB-4DF2-A364-936227A7AF5F}", AZ::ComponentConfig);
  21. AZ_CLASS_ALLOCATOR(TestConfig, AZ::SystemAllocator)
  22. uint32_t m_testValue = 0;
  23. };
  24. class TestController
  25. {
  26. public:
  27. AZ_TYPE_INFO(TestController, "{89C1FED9-C306-4B00-9EA4-577862D9277D}");
  28. static void Reflect(AZ::ReflectContext* context)
  29. {
  30. AZ_UNUSED(context);
  31. }
  32. TestController() = default;
  33. TestController(const TestConfig& config):
  34. m_config(config)
  35. {
  36. }
  37. void Activate(AZ::EntityId entityId)
  38. {
  39. AZ_UNUSED(entityId);
  40. s_activateCalled = true;
  41. }
  42. void Deactivate()
  43. {
  44. s_deactivateCalled = true;
  45. }
  46. void SetConfiguration(const TestConfig& config)
  47. {
  48. m_config = config;
  49. }
  50. const TestConfig& GetConfiguration() const
  51. {
  52. return m_config;
  53. }
  54. TestConfig m_config;
  55. };
  56. class TestRuntimeComponent
  57. : public AzFramework::Components::ComponentAdapter<TestController, TestConfig>
  58. {
  59. public:
  60. using BaseClass = AzFramework::Components::ComponentAdapter<TestController, TestConfig>;
  61. AZ_COMPONENT(TestRuntimeComponent, "{136104E4-36A6-4778-AE65-065D33F87E76}", BaseClass);
  62. TestRuntimeComponent() = default;
  63. TestRuntimeComponent(const TestConfig& config)
  64. : BaseClass(config)
  65. {
  66. }
  67. };
  68. class TestEditorComponent
  69. : public AzToolsFramework::Components::EditorComponentAdapter<TestController, TestRuntimeComponent, TestConfig>
  70. {
  71. public:
  72. using BaseClass = AzToolsFramework::Components::EditorComponentAdapter<TestController, TestRuntimeComponent, TestConfig>;
  73. AZ_EDITOR_COMPONENT(TestEditorComponent, "{5FA2B1D6-E2DA-47FB-8419-B6425C37AC80}", BaseClass);
  74. TestEditorComponent() = default;
  75. TestEditorComponent(const TestConfig& config)
  76. : BaseClass(config)
  77. {
  78. }
  79. };
  80. class WrappedComponentTest
  81. : public CustomSerializeContextTestFixture
  82. {
  83. AZStd::unique_ptr<AZ::SerializeContext> m_serializeContext;
  84. AZStd::unique_ptr<AZ::ComponentDescriptor> m_testRuntimeComponentDescriptor;
  85. AZStd::unique_ptr<AZ::ComponentDescriptor> m_testEditorComponentDescriptor;
  86. public:
  87. void SetUp() override
  88. {
  89. CustomSerializeContextTestFixture::SetUp();
  90. s_activateCalled = false;
  91. s_deactivateCalled = false;
  92. m_testRuntimeComponentDescriptor.reset(TestRuntimeComponent::CreateDescriptor());
  93. m_testRuntimeComponentDescriptor->Reflect(&(*m_serializeContext));
  94. m_testEditorComponentDescriptor.reset(TestEditorComponent::CreateDescriptor());
  95. m_testEditorComponentDescriptor->Reflect(&(*m_serializeContext));
  96. }
  97. void TearDown() override
  98. {
  99. m_testEditorComponentDescriptor.reset();
  100. m_testRuntimeComponentDescriptor.reset();
  101. CustomSerializeContextTestFixture::TearDown();
  102. }
  103. };
  104. TEST_F(WrappedComponentTest, RuntimeWrappersWrapCommon)
  105. {
  106. AZ::Entity entity;
  107. TestRuntimeComponent* runtimeComponent = entity.CreateComponent<TestRuntimeComponent>();
  108. entity.Init();
  109. entity.Activate();
  110. EXPECT_TRUE(s_activateCalled);
  111. entity.Deactivate();
  112. EXPECT_TRUE(s_deactivateCalled);
  113. TestConfig config;
  114. config.m_testValue = 100;
  115. EXPECT_TRUE(runtimeComponent->SetConfiguration(config));
  116. TestConfig outConfig;
  117. EXPECT_TRUE(runtimeComponent->GetConfiguration(outConfig));
  118. EXPECT_EQ(config.m_testValue, outConfig.m_testValue);
  119. }
  120. TEST_F(WrappedComponentTest, EditorWrappersWrapCommon)
  121. {
  122. AZ::Entity entity;
  123. TestEditorComponent* editorComponent = entity.CreateComponent<TestEditorComponent>();
  124. entity.Init();
  125. entity.Activate();
  126. EXPECT_TRUE(s_activateCalled);
  127. entity.Deactivate();
  128. EXPECT_TRUE(s_deactivateCalled);
  129. TestConfig config;
  130. config.m_testValue = 100;
  131. EXPECT_TRUE(editorComponent->SetConfiguration(config));
  132. TestConfig outConfig;
  133. EXPECT_TRUE(editorComponent->GetConfiguration(outConfig));
  134. EXPECT_EQ(config.m_testValue, outConfig.m_testValue);
  135. AZ::Entity gameEntity;
  136. editorComponent->BuildGameEntity(&gameEntity);
  137. TestRuntimeComponent* testRuntimeComponent = gameEntity.FindComponent<TestRuntimeComponent>();
  138. EXPECT_NE(testRuntimeComponent, nullptr);
  139. }
  140. }