UiScrollBarComponentTest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "UiScrollBarComponent.h"
  11. #include "UiElementComponent.h"
  12. #include "UiTransform2dComponent.h"
  13. #include "UiCanvasComponent.h"
  14. #include "UiImageComponent.h"
  15. #include <AzFramework/Application/Application.h>
  16. #include <AzFramework/Entity/GameEntityContextComponent.h>
  17. #include <AzFramework/Asset/AssetSystemComponent.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 UiScrollBarTestApplication
  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(UiScrollBarComponent::CreateDescriptor());
  51. RegisterComponentDescriptor(UiImageComponent::CreateDescriptor());
  52. RegisterComponentDescriptor(UiCanvasComponent::CreateDescriptor());
  53. }
  54. };
  55. class UiScrollBarComponentTest
  56. : public UnitTest::LeakDetectionFixture
  57. {
  58. protected:
  59. void SetUp() override
  60. {
  61. // start application
  62. AZ::ComponentApplication::Descriptor appDescriptor;
  63. appDescriptor.m_useExistingAllocator = true;
  64. m_application = aznew UiScrollBarTestApplication();
  65. AZ::ComponentApplication::StartupParameters startupParameters;
  66. startupParameters.m_loadSettingsRegistry = false;
  67. m_application->Start(appDescriptor, startupParameters);
  68. }
  69. void TearDown() override
  70. {
  71. m_application->Stop();
  72. delete m_application;
  73. m_application = nullptr;
  74. }
  75. static AZStd::tuple<UiCanvasComponent*, UiScrollBarComponent*> CreateUiCanvasWithScrollBar()
  76. {
  77. // create a canvas
  78. UiGameEntityContext* entityContext = new UiGameEntityContext(); //< UiCanvasComponent takes ownership of this pointer and will free this when exiting
  79. UiCanvasComponent* uiCanvasComponent = UiCanvasComponent::CreateCanvasInternal(entityContext, false);
  80. // add scroll bar to the canvas
  81. AZ::Entity* uiScrollBarEntity = uiCanvasComponent->CreateChildElement("Ui Scroll Bar");
  82. uiScrollBarEntity->Deactivate(); //< deactivate so that we can add components
  83. uiScrollBarEntity->CreateComponent<UiTransform2dComponent>(); //< required by UiScrollBarComponent
  84. uiScrollBarEntity->CreateComponent<UiImageComponent>(); //< required by UiScrollBarComponent
  85. auto uiScrollBarComponent = uiScrollBarEntity->CreateComponent<UiScrollBarComponent>();
  86. uiScrollBarEntity->Activate();
  87. // create the handle entity
  88. AZ::Entity* handleEntity = uiScrollBarEntity->FindComponent<UiElementComponent>()->CreateChildElement("Handle");
  89. handleEntity->Deactivate(); // deactivate to add component
  90. handleEntity->CreateComponent<UiTransform2dComponent>();
  91. handleEntity->CreateComponent<UiImageComponent>();
  92. handleEntity->Activate();
  93. // give the handle a size otherwise scroll box items won't be spawned (fill the whole canvas)
  94. uiScrollBarComponent->SetHandleEntity(handleEntity->GetId());
  95. return AZStd::make_tuple(uiCanvasComponent, uiScrollBarComponent);
  96. }
  97. private:
  98. UiScrollBarTestApplication* m_application = nullptr;
  99. };
  100. TEST_F(UiScrollBarComponentTest, UiScrollBarComponent_WillFadeAfterDelay)
  101. {
  102. UiCanvasComponent* uiCanvasComponent;
  103. UiScrollBarComponent* uiScrollBarComponent;
  104. std::tie(uiCanvasComponent, uiScrollBarComponent) = CreateUiCanvasWithScrollBar();
  105. AZ::Entity* uiScrollBarEntity = uiScrollBarComponent->GetEntity();
  106. // test: move the scrollbar, wait 2 seconds and see if the alpha has faded to 0
  107. UiScrollBarBus::Event(uiScrollBarEntity->GetId(), &UiScrollBarBus::Events::SetAutoFadeEnabled, true);
  108. uiScrollBarComponent->SetValue(0.5f); // move the scrollbar
  109. uiScrollBarComponent->Update(2.0f);// wait two seconds
  110. EXPECT_EQ(uiScrollBarEntity->FindComponent<UiImageComponent>()->GetAlpha(), 0.0f);
  111. // clean up the canvas
  112. delete uiCanvasComponent->GetEntity();
  113. }
  114. } //namespace UnitTest