/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #include "LyShine_precompiled.h" #include "LyShineTest.h" #include "UiGameEntityContext.h" #include "UiScrollBarComponent.h" #include "UiElementComponent.h" #include "UiTransform2dComponent.h" #include "UiCanvasComponent.h" #include "UiImageComponent.h" #include #include #include #include #include #include #include #include namespace UnitTest { class UiScrollBarTestApplication : public AzFramework::Application { void Reflect(AZ::ReflectContext* context) override { AzFramework::Application::Reflect(context); UiSerialize::ReflectUiTypes(context); //< needed to serialize ui Anchor and Offset } // override and only include system components required for tests. AZ::ComponentTypeList GetRequiredSystemComponents() const override { return AZ::ComponentTypeList{ azrtti_typeid(), azrtti_typeid(), azrtti_typeid(), azrtti_typeid(), azrtti_typeid(), azrtti_typeid(), }; } void RegisterCoreComponents() override { AzFramework::Application::RegisterCoreComponents(); RegisterComponentDescriptor(UiTransform2dComponent::CreateDescriptor()); RegisterComponentDescriptor(UiElementComponent::CreateDescriptor()); RegisterComponentDescriptor(UiScrollBarComponent::CreateDescriptor()); RegisterComponentDescriptor(UiImageComponent::CreateDescriptor()); RegisterComponentDescriptor(UiCanvasComponent::CreateDescriptor()); } }; class UiScrollBarComponentTest : public testing::Test { protected: void SetUp() override { // start application AZ::AllocatorInstance::Create(AZ::SystemAllocator::Descriptor()); AZ::ComponentApplication::Descriptor appDescriptor; appDescriptor.m_useExistingAllocator = true; m_application = aznew UiScrollBarTestApplication(); m_application->Start(appDescriptor, AZ::ComponentApplication::StartupParameters()); } void TearDown() override { m_application->Stop(); delete m_application; m_application = nullptr; AZ::AllocatorInstance::Destroy(); } static AZStd::tuple CreateUiCanvasWithScrollBar() { // create a canvas UiGameEntityContext* entityContext = new UiGameEntityContext(); //< UiCanvasComponent takes ownership of this pointer and will free this when exiting UiCanvasComponent* uiCanvasComponent = UiCanvasComponent::CreateCanvasInternal(entityContext, false); // add scroll bar to the canvas AZ::Entity* uiScrollBarEntity = uiCanvasComponent->CreateChildElement("Ui Scroll Bar"); uiScrollBarEntity->Deactivate(); //< deactivate so that we can add components uiScrollBarEntity->CreateComponent(); //< required by UiScrollBarComponent uiScrollBarEntity->CreateComponent(); //< required by UiScrollBarComponent auto uiScrollBarComponent = uiScrollBarEntity->CreateComponent(); uiScrollBarEntity->Activate(); // create the handle entity AZ::Entity* handleEntity = uiScrollBarEntity->FindComponent()->CreateChildElement("Handle"); handleEntity->Deactivate(); // deactivate to add component auto handleTransform = handleEntity->CreateComponent(); handleEntity->CreateComponent(); handleEntity->Activate(); // give the handle a size otherwise scroll box items won't be spawned (fill the whole canvas) uiScrollBarComponent->SetHandleEntity(handleEntity->GetId()); return AZStd::make_tuple(uiCanvasComponent, uiScrollBarComponent); } private: UiScrollBarTestApplication* m_application = nullptr; }; TEST_F(UiScrollBarComponentTest, UiScrollBarComponent_WillFadeAfterDelay) { UiCanvasComponent* uiCanvasComponent; UiScrollBarComponent* uiScrollBarComponent; std::tie(uiCanvasComponent, uiScrollBarComponent) = CreateUiCanvasWithScrollBar(); AZ::Entity* uiScrollBarEntity = uiScrollBarComponent->GetEntity(); // test: move the scrollbar, wait 2 seconds and see if the alpha has faded to 0 UiScrollBarBus::Event(uiScrollBarEntity->GetId(), &UiScrollBarBus::Events::SetAutoFadeEnabled, true); uiScrollBarComponent->SetValue(0.5f); // move the scrollbar uiScrollBarComponent->Update(2.0f);// wait two seconds EXPECT_EQ(uiScrollBarEntity->FindComponent()->GetAlpha(), 0.0f); // clean up the canvas delete uiCanvasComponent->GetEntity(); } } //namespace UnitTest