TextInputComponentTest.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "LyShineTest.h"
  9. #include "UiGameEntityContext.h"
  10. #include "UiElementComponent.h"
  11. #include "UiTransform2dComponent.h"
  12. #include "UiCanvasComponent.h"
  13. #include "UiTextInputComponent.h"
  14. #include <AzFramework/Application/Application.h>
  15. #include <AzFramework/Entity/GameEntityContextComponent.h>
  16. #include <AzFramework/Asset/AssetSystemComponent.h>
  17. #include <AzFramework/Input/Buses/Requests/InputTextEntryRequestBus.h>
  18. #include <AzCore/Component/Entity.h>
  19. #include <AzCore/Asset/AssetManagerComponent.h>
  20. #include <AzCore/IO/Streamer/StreamerComponent.h>
  21. #include <AzCore/Jobs/JobManagerComponent.h>
  22. #include <AzCore/Slice/SliceSystemComponent.h>
  23. namespace UnitTest
  24. {
  25. class UiTextInputTestApplication
  26. : public AzFramework::Application
  27. {
  28. void Reflect(AZ::ReflectContext* context) override
  29. {
  30. AzFramework::Application::Reflect(context);
  31. UiSerialize::ReflectUiTypes(context); //< needed to serialize ui Anchor and Offset
  32. }
  33. // override and only include system components required for tests.
  34. AZ::ComponentTypeList GetRequiredSystemComponents() const override
  35. {
  36. return AZ::ComponentTypeList{
  37. azrtti_typeid<AZ::AssetManagerComponent>(),
  38. azrtti_typeid<AZ::JobManagerComponent>(),
  39. azrtti_typeid<AZ::StreamerComponent>(),
  40. azrtti_typeid<AZ::SliceSystemComponent>(),
  41. azrtti_typeid<AzFramework::GameEntityContextComponent>(),
  42. azrtti_typeid<AzFramework::AssetSystem::AssetSystemComponent>(),
  43. };
  44. }
  45. void RegisterCoreComponents() override
  46. {
  47. AzFramework::Application::RegisterCoreComponents();
  48. RegisterComponentDescriptor(UiTransform2dComponent::CreateDescriptor());
  49. RegisterComponentDescriptor(UiElementComponent::CreateDescriptor());
  50. RegisterComponentDescriptor(UiTextInputComponent::CreateDescriptor());
  51. RegisterComponentDescriptor(UiCanvasComponent::CreateDescriptor());
  52. }
  53. };
  54. class InputDeviceMock
  55. : public AzFramework::InputTextEntryRequestBus::Handler
  56. {
  57. public:
  58. InputDeviceMock()
  59. : m_textEntryStarted(false)
  60. , m_mockId("MockInputDevice")
  61. {
  62. AzFramework::InputTextEntryRequestBus::Handler::BusConnect(m_mockId);
  63. }
  64. ~InputDeviceMock()
  65. {
  66. AzFramework::InputTextEntryRequestBus::Handler::BusDisconnect(m_mockId);
  67. }
  68. bool HasTextEntryStarted() const override
  69. {
  70. return m_textEntryStarted;
  71. }
  72. void TextEntryStart([[maybe_unused]] const VirtualKeyboardOptions& options) override
  73. {
  74. m_textEntryStarted = true;
  75. }
  76. void TextEntryStop() override
  77. {
  78. m_textEntryStarted = false;
  79. }
  80. private:
  81. bool m_textEntryStarted;
  82. AzFramework::InputDeviceId m_mockId;
  83. };
  84. class UiTextInputComponentTest
  85. : public UnitTest::LeakDetectionFixture
  86. {
  87. protected:
  88. void SetUp() override
  89. {
  90. // Start application
  91. AZ::ComponentApplication::Descriptor appDescriptor;
  92. appDescriptor.m_useExistingAllocator = true;
  93. m_applicationPtr = aznew UiTextInputTestApplication();
  94. m_applicationPtr->Start(appDescriptor, AZ::ComponentApplication::StartupParameters());
  95. // Create mock gEnv for mock renderer
  96. m_env = AZStd::make_unique<StubEnv>();
  97. memset(&m_env->m_stubEnv, 0, sizeof(SSystemGlobalEnvironment));
  98. m_priorEnv = gEnv;
  99. gEnv = &m_env->m_stubEnv;
  100. }
  101. void TearDown() override
  102. {
  103. m_env.reset();
  104. gEnv = m_priorEnv;
  105. m_applicationPtr->Stop();
  106. delete m_applicationPtr;
  107. m_applicationPtr = nullptr;
  108. }
  109. private:
  110. struct StubEnv
  111. {
  112. SSystemGlobalEnvironment m_stubEnv;
  113. };
  114. AZStd::unique_ptr<StubEnv> m_env;
  115. SSystemGlobalEnvironment* m_priorEnv = nullptr;
  116. UiTextInputTestApplication* m_applicationPtr = nullptr;
  117. };
  118. TEST_F(UiTextInputComponentTest, UiTextInputComponent_CanForceFocus_FT)
  119. {
  120. // create a canvas
  121. UiGameEntityContext* entityContext = new UiGameEntityContext(); //< UiCanvasComponent takes ownership of this pointer and will free this when exiting
  122. UiCanvasComponent* uiCanvasComponent = UiCanvasComponent::CreateCanvasInternal(entityContext, false);
  123. // add text input to the canvas
  124. AZ::Entity* uiTextInputEntity = uiCanvasComponent->CreateChildElement("Ui Text Input");
  125. uiTextInputEntity->Deactivate(); //< deactivate so that we can add components
  126. uiTextInputEntity->CreateComponent<UiTransform2dComponent>(); //< required by UiTextInputComponent
  127. uiTextInputEntity->CreateComponent<UiTextInputComponent>();
  128. uiTextInputEntity->Activate();
  129. AZ::EntityId uiTextInputEntityId = uiTextInputEntity->GetId();
  130. InputDeviceMock mockInputDevice = InputDeviceMock();
  131. // Make sure text entry has not already started
  132. bool textEntryStarted = mockInputDevice.HasTextEntryStarted();
  133. EXPECT_FALSE(textEntryStarted);
  134. uiCanvasComponent->ForceFocusInteractable(uiTextInputEntityId);
  135. // Make sure Text Input is active and requesting input
  136. textEntryStarted = mockInputDevice.HasTextEntryStarted();
  137. EXPECT_TRUE(textEntryStarted);
  138. // clean up the canvas
  139. delete uiCanvasComponent->GetEntity();
  140. }
  141. } //namespace UnitTest