| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <AzFramework/Input/Contexts/InputContext.h>
- #include <AzFramework/Input/Mappings/InputMapping.h>
- #include <AzFramework/Input/Mappings/InputMappingAnd.h>
- #include <AzFramework/Input/Mappings/InputMappingOr.h>
- #include <AzFramework/Input/Devices/Gamepad/InputDeviceGamepad.h>
- #include <AzFramework/Input/Devices/Keyboard/InputDeviceKeyboard.h>
- #include <AzFramework/Input/System/InputSystemComponent.h>
- #include <AzCore/Interface/Interface.h>
- #include <AzCore/std/smart_ptr/make_shared.h>
- #include <AzCore/std/smart_ptr/shared_ptr.h>
- #include <AzCore/std/smart_ptr/unique_ptr.h>
- #include <AzCore/UnitTest/TestTypes.h>
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- namespace InputUnitTests
- {
- using namespace AZ;
- using namespace AzFramework;
- using namespace UnitTest;
- ////////////////////////////////////////////////////////////////////////////////////////////////
- class InputTest : public LeakDetectionFixture
- {
- public:
- InputTest() : LeakDetectionFixture()
- {
- // Many input tests are only valid if the GamePad device is supported on this platform.
- auto deviceGamepadImplFactory = AZ::Interface<InputDeviceGamepad::ImplementationFactory>::Get();
- m_gamepadSupported = (deviceGamepadImplFactory != nullptr) && (deviceGamepadImplFactory->GetMaxSupportedGamepads() > 0);
- }
- protected:
- ////////////////////////////////////////////////////////////////////////////////////////////
- void SetUp() override
- {
- m_inputSystemComponent = AZStd::make_unique<InputSystemComponent>();
- m_inputSystemComponent->Activate();
- }
- ////////////////////////////////////////////////////////////////////////////////////////////
- void TearDown() override
- {
- m_inputSystemComponent->Deactivate();
- m_inputSystemComponent.reset();
- }
-
- ////////////////////////////////////////////////////////////////////////////////////////////
- AZStd::unique_ptr<InputSystemComponent> m_inputSystemComponent;
- bool m_gamepadSupported;
- };
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputChannelId_ConstExpression_CopyConstructorSuccessfull)
- {
- constexpr InputChannelId testInputChannelId1("TestInputChannelId");
- constexpr InputChannelId testInputChannelId2(testInputChannelId1);
- static_assert(testInputChannelId1 == testInputChannelId2);
- EXPECT_EQ(testInputChannelId1, testInputChannelId2);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputDeviceId_ConstExpression_CopyConstructorSuccessfull)
- {
- constexpr InputDeviceId testInputDeviceId1("TestInputDeviceId");
- constexpr InputDeviceId testInputDeviceId2(testInputDeviceId1);
- static_assert(testInputDeviceId1 == testInputDeviceId2);
- EXPECT_EQ(testInputDeviceId1, testInputDeviceId2);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputContext_InitWithDataStruct_InitializationSuccessfull)
- {
- // Create an input context using an init data struct.
- constexpr AZ::s32 testPriority = 9;
- InputContext::InitData initData;
- initData.priority = testPriority;
- InputContext inputContext("TestInputContext", initData);
- EXPECT_EQ(inputContext.GetPriority(), testPriority);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputContext_ActivateDeactivate_Successfull)
- {
- if (!m_gamepadSupported)
- {
- #if defined(GTEST_SKIP)
- GTEST_SKIP() << "Skipping test InputContext_ActivateDeactivate_Successfull";
- #else
- SUCCEED() << "Skipping test InputContext_ActivateDeactivate_Successfull";
- #endif
- return;
- }
- // Create an input context (they are inactive by default).
- InputContext inputContext("TestInputContext");
- // Create an input mapping, add a single source input, and add the mapping to the context.
- AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMapping"), inputContext);
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
- // Validate the initial state of the mapping.
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button press, then validate the state of the mapping is unchanged.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Reset the button, then validate the state of the mapping is unchanged.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A, &AzFramework::InputChannelRequests::ResetState);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Activate the input context, then validate the state of the mapping is unchanged.
- inputContext.Activate();
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button press, then validate the state of the mapping has changed.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Deactivate the input context, then validate the state of the mapping is unchanged.
- inputContext.Deactivate();
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Simulate a button release, then validate the state of the mapping is unchanged.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputContext_AddRemoveInputMapping_Successfull)
- {
- if (!m_gamepadSupported)
- {
- #if defined(GTEST_SKIP)
- GTEST_SKIP() << "Skipping test InputContext_AddRemoveInputMapping_Successfull";
- #else
- SUCCEED() << "Skipping test InputContext_AddRemoveInputMapping_Successfull";
- #endif
- return;
- }
- // Create an input context and activate it.
- InputContext inputContext("TestInputContext");
- inputContext.Activate();
- // Create an input mapping and add a single source input.
- const InputChannelId inputMappingId = InputChannelId("TestInputMapping");
- AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(inputMappingId, inputContext);
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
- // Validate the initial state of the mapping.
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button press, then validate the state of the mapping is unchanged.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Reset the button, then validate the state of the mapping is unchanged.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A, &AzFramework::InputChannelRequests::ResetState);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Add the mapping to the context, then validate the state of the mapping is unchanged.
- EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button press, then validate the state of the mapping has changed.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Remove the mapping from the context, then validate the state of the mapping is unchanged.
- EXPECT_TRUE(inputContext.RemoveInputMapping(inputMappingId));
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Simulate a button release, then validate the state of the mapping is unchanged.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputContext_AddSameMappingTwice_Unsuccessful)
- {
- // Validate that we can't add the same input mapping more than one to the same input context.
- InputContext inputContext("TestInputContext");
- AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMapping"), inputContext);
- EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
- EXPECT_FALSE(inputContext.AddInputMapping(inputMapping));
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputContext_RemoveMappingThatHasNotBeenAdded_Unsuccessful)
- {
- // Validate that we can't remove an input mapping that has not been added to an input context.
- InputContext inputContext("TestInputContext");
- EXPECT_FALSE(inputContext.RemoveInputMapping(InputChannelId("TestInputMapping")));
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputContext_AddMappingToContextThatIsNotParent_Unsuccessful)
- {
- // Validate that we can't add a mapping to a context that is not its parent.
- InputContext inputContext("TestInputContext");
- AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMapping"), inputContext);
- InputContext otherInputContext("TestInputContext");
- EXPECT_FALSE(otherInputContext.AddInputMapping(inputMapping));
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputContext_AddNullMapping_Unsuccessful)
- {
- // Validate that we can't add a null mapping to an input context.
- InputContext inputContext("TestInputContext");
- EXPECT_FALSE(inputContext.AddInputMapping(nullptr));
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputContext_ConsumeProcessedInput_Consumed)
- {
- if (!m_gamepadSupported)
- {
- #if defined(GTEST_SKIP)
- GTEST_SKIP() << "Skipping test InputContext_ConsumeProcessedInput_Consumed";
- #else
- SUCCEED() << "Skipping test InputContext_ConsumeProcessedInput_Consumed";
- #endif
- return;
- }
- InputContext::InitData initData;
- // Create a high priority input context that consumes input processed by any of its mappings.
- initData.autoActivate = true;
- initData.consumesProcessedInput = true;
- initData.priority = InputChannelEventListener::GetPriorityFirst();
- InputContext inputContextPriorityHigh("TestInputContextPriorityHigh", initData);
- // Create a default priority input context that does not consume any input.
- initData.autoActivate = true;
- initData.consumesProcessedInput = false;
- initData.priority = InputChannelEventListener::GetPriorityDefault();
- InputContext inputContextPriorityDefault("TestInputContextPriorityDefault", initData);
- // Create a low priority input context that does not consume any input.
- initData.autoActivate = true;
- initData.consumesProcessedInput = false;
- initData.priority = InputChannelEventListener::GetPriorityLast();
- InputContext inputContextPriorityLow("TestInputContextPriorityLow", initData);
- // Create an input mapping to add to the high priority input context.
- AZStd::shared_ptr<InputMappingOr> inputMappingPriorityHigh = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMappingPriorityHigh"), inputContextPriorityHigh);
- EXPECT_TRUE(inputMappingPriorityHigh->AddSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_TRUE(inputContextPriorityHigh.AddInputMapping(inputMappingPriorityHigh));
- // Create the same input mapping to add to the default and low priority input contexts.
- AZStd::shared_ptr<InputMappingOr> inputMappingPriorityDefault = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMappingPriorityDefault"), inputContextPriorityDefault);
- EXPECT_TRUE(inputMappingPriorityDefault->AddSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_TRUE(inputContextPriorityDefault.AddInputMapping(inputMappingPriorityDefault));
- AZStd::shared_ptr<InputMappingOr> inputMappingPriorityLow = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMappingPriorityLow"), inputContextPriorityLow);
- EXPECT_TRUE(inputMappingPriorityLow->AddSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_TRUE(inputContextPriorityLow.AddInputMapping(inputMappingPriorityLow));
- // Simulate a button press, then validate that it was consumed by the high priority context.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMappingPriorityHigh->IsActive());
- EXPECT_TRUE(inputMappingPriorityHigh->IsStateBegan());
- EXPECT_EQ(inputMappingPriorityHigh->GetValue(), 1.0f);
- EXPECT_EQ(inputMappingPriorityHigh->GetDelta(), 1.0f);
- EXPECT_FALSE(inputMappingPriorityDefault->IsActive());
- EXPECT_TRUE(inputMappingPriorityDefault->IsStateIdle());
- EXPECT_EQ(inputMappingPriorityDefault->GetValue(), 0.0f);
- EXPECT_EQ(inputMappingPriorityDefault->GetDelta(), 0.0f);
- EXPECT_FALSE(inputMappingPriorityLow->IsActive());
- EXPECT_TRUE(inputMappingPriorityLow->IsStateIdle());
- EXPECT_EQ(inputMappingPriorityLow->GetValue(), 0.0f);
- EXPECT_EQ(inputMappingPriorityLow->GetDelta(), 0.0f);
- // Create different input mappings to add to the default and low priority input contexts.
- AZStd::shared_ptr<InputMappingOr> otherInputMappingPriorityDefault = AZStd::make_shared<InputMappingOr>(InputChannelId("OtherTestInputMappingPriorityDefault"), inputContextPriorityDefault);
- EXPECT_TRUE(otherInputMappingPriorityDefault->AddSourceInput(InputDeviceGamepad::Button::B));
- EXPECT_TRUE(inputContextPriorityDefault.AddInputMapping(otherInputMappingPriorityDefault));
- AZStd::shared_ptr<InputMappingOr> otherInputMappingPriorityLow = AZStd::make_shared<InputMappingOr>(InputChannelId("OtherTestInputMappingPriorityLow"), inputContextPriorityLow);
- EXPECT_TRUE(otherInputMappingPriorityLow->AddSourceInput(InputDeviceGamepad::Button::B));
- EXPECT_TRUE(inputContextPriorityLow.AddInputMapping(otherInputMappingPriorityLow));
- // Simulate a button press, then validate that it was not consumed.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::B,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(otherInputMappingPriorityDefault->IsActive());
- EXPECT_TRUE(otherInputMappingPriorityDefault->IsStateBegan());
- EXPECT_EQ(otherInputMappingPriorityDefault->GetValue(), 1.0f);
- EXPECT_EQ(otherInputMappingPriorityDefault->GetDelta(), 1.0f);
- EXPECT_TRUE(otherInputMappingPriorityLow->IsActive());
- EXPECT_TRUE(otherInputMappingPriorityLow->IsStateBegan());
- EXPECT_EQ(otherInputMappingPriorityLow->GetValue(), 1.0f);
- EXPECT_EQ(otherInputMappingPriorityLow->GetDelta(), 1.0f);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputContext_FilteredInput_Mapped)
- {
- if (!m_gamepadSupported)
- {
- #if defined(GTEST_SKIP)
- GTEST_SKIP() << "Skipping test InputContext_FilteredInput_Mapped";
- #else
- SUCCEED() << "Skipping test InputContext_FilteredInput_Mapped";
- #endif
- return;
- }
- // Create an input context that initially only listens for keyboard input.
- InputContext::InitData initData;
- initData.autoActivate = true;
- AZStd::shared_ptr<InputChannelEventFilterInclusionList> inclusionFilter = AZStd::make_shared<InputChannelEventFilterInclusionList>();
- inclusionFilter->IncludeDeviceName(InputDeviceKeyboard::Id.GetNameCrc32());
- initData.filter = inclusionFilter;
- InputContext inputContext("TestInputContext", initData);
- // Create an input mapping and add multiple source inputs from different input devices.
- AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMapping"), inputContext);
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceKeyboard::Key::AlphanumericA));
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
- // Validate the initial state of the mapping.
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a gamepad button press, then validate the state of the mapping is unchanged.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a keyboard key press, then validate the state of the mapping is updated.
- AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Reset the button and key, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A, &AzFramework::InputChannelRequests::ResetState);
- AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA, &AzFramework::InputChannelRequests::ResetState);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateEnded());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
- // Tick the input system, then validate the state of the mapping reset.
- AzFramework::InputSystemRequestBus::Broadcast(&InputSystemRequests::TickInput);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Update the filter so it also listens for gamepad input.
- inclusionFilter->IncludeDeviceName(InputDeviceGamepad::IdForIndex0.GetNameCrc32());
- // Simulate a gamepad button press, then validate the state of the mapping is updated.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputMappingOr_AddRemoveSourceInput_Successful)
- {
- if (!m_gamepadSupported)
- {
- #if defined(GTEST_SKIP)
- GTEST_SKIP() << "Skipping test InputMappingOr_AddRemoveSourceInput_Successful";
- #else
- SUCCEED() << "Skipping test InputMappingOr_AddRemoveSourceInput_Successful";
- #endif
- return;
- }
- // Create an input context and activate it.
- InputContext inputContext("TestInputContext");
- inputContext.Activate();
- // Create an input mapping and add it to the context.
- AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMapping"), inputContext);
- EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
- // Validate the initial state of the mapping.
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button press, then validate the state of the mapping is unchanged.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Reset the button, then validate the state of the mapping is unchanged.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A, &AzFramework::InputChannelRequests::ResetState);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Add a source input to the mapping, then validate the state of the mapping is unchanged.
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button press, then validate the state of the mapping has changed.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Remove the source input from the mapping, then validate the state of the mapping is unchanged.
- EXPECT_TRUE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Simulate a button release, then validate the state of the mapping is unchanged.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Validate that we can't add a source input twice.
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_FALSE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_TRUE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::A));
- // Validate that we can't remove a source input that has not been added.
- EXPECT_FALSE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::B));
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputMappingOr_SingleSourceInput_Mapped)
- {
- if (!m_gamepadSupported)
- {
- #if defined(GTEST_SKIP)
- GTEST_SKIP() << "Skipping test InputMappingOr_SingleSourceInput_Mapped";
- #else
- SUCCEED() << "Skipping test InputMappingOr_SingleSourceInput_Mapped";
- #endif
- return;
- }
- // Create an input context and activate it.
- InputContext inputContext("TestInputContext");
- inputContext.Activate();
- // Create an input mapping, add a single source input, and add the mapping to the context.
- AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMapping"), inputContext);
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
- // Validate the initial state of the mapping.
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button press, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Simulate a button hold, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateUpdated());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button release, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateEnded());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
- // Simulate a tick of the input system, then validate the expected state of the mapping.
- AzFramework::InputSystemRequestBus::Broadcast(&InputSystemRequests::TickInput);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Remove the source input, simulate a button press, then validate the expected state of the mapping.
- EXPECT_TRUE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::A));
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputMappingOr_MultipleSourceInputs_Mapped)
- {
- if (!m_gamepadSupported)
- {
- #if defined(GTEST_SKIP)
- GTEST_SKIP() << "Skipping test InputMappingOr_MultipleSourceInputs_Mapped";
- #else
- SUCCEED() << "Skipping test InputMappingOr_MultipleSourceInputs_Mapped";
- #endif
- return;
- }
- // Create an input context and activate it.
- InputContext inputContext("TestInputContext");
- inputContext.Activate();
- // Create an input mapping, add multiple source inputs, and add the mapping to the context.
- AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMapping"), inputContext);
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceKeyboard::Key::AlphanumericA));
- EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
- // Validate the initial state of the mapping.
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button press, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Simulate a button hold, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateUpdated());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button release, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateEnded());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
- // Simulate a tick of the input system, then validate the expected state of the mapping.
- AzFramework::InputSystemRequestBus::Broadcast(&InputSystemRequests::TickInput);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a key press, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Simulate a key hold, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateUpdated());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a key release, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateEnded());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
- // Simulate a tick of the input system, then validate the expected state of the mapping.
- AzFramework::InputSystemRequestBus::Broadcast(&InputSystemRequests::TickInput);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputMappingAnd_AddRemoveSourceInput_Successful)
- {
- if (!m_gamepadSupported)
- {
- #if defined(GTEST_SKIP)
- GTEST_SKIP() << "Skipping test InputMappingAnd_AddRemoveSourceInput_Successful";
- #else
- SUCCEED() << "Skipping test InputMappingAnd_AddRemoveSourceInput_Successful";
- #endif
- return;
- }
- // Create an input context and activate it.
- InputContext inputContext("TestInputContext");
- inputContext.Activate();
- // Create an input mapping and add it to the context.
- AZStd::shared_ptr<InputMappingAnd> inputMapping = AZStd::make_shared<InputMappingAnd>(InputChannelId("TestInputMapping"), inputContext);
- EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
- // Validate the initial state of the mapping.
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button press, then validate the state of the mapping is unchanged.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Reset the button, then validate the state of the mapping is unchanged.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A, &AzFramework::InputChannelRequests::ResetState);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Add a source input to the mapping, then validate the state of the mapping is unchanged.
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button press, then validate the state of the mapping has changed.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Remove the source input from the mapping, then validate the state of the mapping is unchanged.
- EXPECT_TRUE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Simulate a button release, then validate the state of the mapping is unchanged.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Validate that we can't add a source input twice.
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_FALSE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_TRUE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::A));
- // Validate that we can't remove a source input that has not been added.
- EXPECT_FALSE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::B));
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputMappingAnd_SingleSourceInput_Mapped)
- {
- if (!m_gamepadSupported)
- {
- #if defined(GTEST_SKIP)
- GTEST_SKIP() << "Skipping test InputMappingAnd_SingleSourceInput_Mapped";
- #else
- SUCCEED() << "Skipping test InputMappingAnd_SingleSourceInput_Mapped";
- #endif
- return;
- }
- // Create an input context and activate it.
- InputContext inputContext("TestInputContext");
- inputContext.Activate();
- // Create an input mapping, add a single source input, and add the mapping to the context.
- AZStd::shared_ptr<InputMappingAnd> inputMapping = AZStd::make_shared<InputMappingAnd>(InputChannelId("TestInputMapping"), inputContext);
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
- // Validate the initial state of the mapping.
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button press, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Simulate a button hold, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateUpdated());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button release, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateEnded());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
- // Simulate a tick of the input system, then validate the expected state of the mapping.
- AzFramework::InputSystemRequestBus::Broadcast(&InputSystemRequests::TickInput);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Remove the source input, simulate a button press, then validate the expected state of the mapping.
- EXPECT_TRUE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::A));
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputMappingAnd_MultipleSourceInputs_Mapped)
- {
- if (!m_gamepadSupported)
- {
- #if defined(GTEST_SKIP)
- GTEST_SKIP() << "Skipping test InputMappingAnd_MultipleSourceInputs_Mapped";
- #else
- SUCCEED() << "Skipping test InputMappingAnd_MultipleSourceInputs_Mapped";
- #endif
- return;
- }
- // Create an input context and activate it.
- InputContext inputContext("TestInputContext");
- inputContext.Activate();
- // Create an input mapping, add multiple source inputs, and add the mapping to the context.
- AZStd::shared_ptr<InputMappingAnd> inputMapping = AZStd::make_shared<InputMappingAnd>(InputChannelId("TestInputMapping"), inputContext);
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceKeyboard::Key::AlphanumericA));
- EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
- // Validate the initial state of the mapping.
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button press, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a key press, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Simulate a button hold, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateUpdated());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button release, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateEnded());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
- // Reset the button and key, tick the input system, then validate the state of the mapping reset.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A, &AzFramework::InputChannelRequests::ResetState);
- AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA, &AzFramework::InputChannelRequests::ResetState);
- AzFramework::InputSystemRequestBus::Broadcast(&InputSystemRequests::TickInput);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a key press, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a button press, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Simulate a key hold, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateUpdated());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate a key release, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateEnded());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
- // Reset the button and key, tick the input system, then validate the state of the mapping reset.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A, &AzFramework::InputChannelRequests::ResetState);
- AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA, &AzFramework::InputChannelRequests::ResetState);
- AzFramework::InputSystemRequestBus::Broadcast(&InputSystemRequests::TickInput);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputMappingAnd_MultipleSourceInputsWithDifferentValues_ValuesAveraged)
- {
- if (!m_gamepadSupported)
- {
- #if defined(GTEST_SKIP)
- GTEST_SKIP() << "Skipping test InputMappingAnd_MultipleSourceInputsWithDifferentValues_ValuesAveraged";
- #else
- SUCCEED() << "Skipping test InputMappingAnd_MultipleSourceInputsWithDifferentValues_ValuesAveraged";
- #endif
- return;
- }
- // Create an input context and activate it.
- InputContext inputContext("TestInputContext");
- inputContext.Activate();
- // Create an input mapping, add multiple source inputs, and add the mapping to the context.
- AZStd::shared_ptr<InputMappingAnd> inputMapping = AZStd::make_shared<InputMappingAnd>(InputChannelId("TestInputMapping"), inputContext);
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Trigger::L2));
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Trigger::R2));
- EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
- // Validate the initial state of the mapping.
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate an L2 trigger press, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Trigger::L2,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.25f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate an R2 trigger press, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Trigger::R2,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.75f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 0.5f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.5f);
- // Simulate an L2 trigger value change, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Trigger::L2,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.75f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateUpdated());
- EXPECT_EQ(inputMapping->GetValue(), 0.75f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.25f);
- // Simulate an R2 trigger release, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Trigger::R2,
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateEnded());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), -0.75f);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- TEST_F(InputTest, InputMappingAnd_MultipleSourceInputsFromTheSameInputDeviceTypeWithDifferentIndicies_NotMapped)
- {
- if (!m_gamepadSupported)
- {
- #if defined(GTEST_SKIP)
- GTEST_SKIP() << "Skipping test InputMappingAnd_MultipleSourceInputsFromTheSameInputDeviceTypeWithDifferentIndicies_NotMapped";
- #else
- SUCCEED() << "Skipping test InputMappingAnd_MultipleSourceInputsFromTheSameInputDeviceTypeWithDifferentIndicies_NotMapped";
- #endif
- return;
- }
- // Create an input context and activate it.
- InputContext inputContext("TestInputContext");
- inputContext.Activate();
- // Create an input mapping, add multiple source inputs, and add the mapping to the context.
- AZStd::shared_ptr<InputMappingAnd> inputMapping = AZStd::make_shared<InputMappingAnd>(InputChannelId("TestInputMapping"), inputContext);
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Trigger::L2));
- EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Trigger::R2));
- EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
- // Validate the initial state of the mapping.
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate an L2 trigger press from device index 0, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputChannelRequests::BusIdType(InputDeviceGamepad::Trigger::L2, 0),
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate an L2 trigger press from device index 1, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputChannelRequests::BusIdType(InputDeviceGamepad::Trigger::L2, 1),
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate an R2 trigger press from device index 1, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputChannelRequests::BusIdType(InputDeviceGamepad::Trigger::R2, 1),
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateIdle());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
- // Simulate an R2 trigger press from device index 0, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputChannelRequests::BusIdType(InputDeviceGamepad::Trigger::R2, 0),
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 1.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Simulate an L2 trigger release from device index 1, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputChannelRequests::BusIdType(InputDeviceGamepad::Trigger::L2, 1),
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Simulate an R2 trigger release from device index 1, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputChannelRequests::BusIdType(InputDeviceGamepad::Trigger::R2, 1),
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.0f);
- EXPECT_TRUE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateBegan());
- EXPECT_EQ(inputMapping->GetValue(), 1.0f);
- EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
- // Simulate an R2 trigger release from device index 0, then validate the expected state of the mapping.
- AzFramework::InputChannelRequestBus::Event(InputChannelRequests::BusIdType(InputDeviceGamepad::Trigger::R2, 0),
- &AzFramework::InputChannelRequests::SimulateRawInput,
- 0.0f);
- EXPECT_FALSE(inputMapping->IsActive());
- EXPECT_TRUE(inputMapping->IsStateEnded());
- EXPECT_EQ(inputMapping->GetValue(), 0.0f);
- EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
- }
- } // namespace UnitTest
|