InputTests.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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 <AzFramework/Input/Contexts/InputContext.h>
  9. #include <AzFramework/Input/Mappings/InputMapping.h>
  10. #include <AzFramework/Input/Mappings/InputMappingAnd.h>
  11. #include <AzFramework/Input/Mappings/InputMappingOr.h>
  12. #include <AzFramework/Input/Devices/Gamepad/InputDeviceGamepad.h>
  13. #include <AzFramework/Input/Devices/Keyboard/InputDeviceKeyboard.h>
  14. #include <AzFramework/Input/System/InputSystemComponent.h>
  15. #include <AzCore/Interface/Interface.h>
  16. #include <AzCore/std/smart_ptr/make_shared.h>
  17. #include <AzCore/std/smart_ptr/shared_ptr.h>
  18. #include <AzCore/std/smart_ptr/unique_ptr.h>
  19. #include <AzCore/UnitTest/TestTypes.h>
  20. ////////////////////////////////////////////////////////////////////////////////////////////////////
  21. namespace InputUnitTests
  22. {
  23. using namespace AZ;
  24. using namespace AzFramework;
  25. using namespace UnitTest;
  26. ////////////////////////////////////////////////////////////////////////////////////////////////
  27. class InputTest : public LeakDetectionFixture
  28. {
  29. public:
  30. InputTest() : LeakDetectionFixture()
  31. {
  32. // Many input tests are only valid if the GamePad device is supported on this platform.
  33. auto deviceGamepadImplFactory = AZ::Interface<InputDeviceGamepad::ImplementationFactory>::Get();
  34. m_gamepadSupported = (deviceGamepadImplFactory != nullptr) && (deviceGamepadImplFactory->GetMaxSupportedGamepads() > 0);
  35. }
  36. protected:
  37. ////////////////////////////////////////////////////////////////////////////////////////////
  38. void SetUp() override
  39. {
  40. m_inputSystemComponent = AZStd::make_unique<InputSystemComponent>();
  41. m_inputSystemComponent->Activate();
  42. }
  43. ////////////////////////////////////////////////////////////////////////////////////////////
  44. void TearDown() override
  45. {
  46. m_inputSystemComponent->Deactivate();
  47. m_inputSystemComponent.reset();
  48. }
  49. ////////////////////////////////////////////////////////////////////////////////////////////
  50. AZStd::unique_ptr<InputSystemComponent> m_inputSystemComponent;
  51. bool m_gamepadSupported;
  52. };
  53. ////////////////////////////////////////////////////////////////////////////////////////////////
  54. TEST_F(InputTest, InputChannelId_ConstExpression_CopyConstructorSuccessfull)
  55. {
  56. constexpr InputChannelId testInputChannelId1("TestInputChannelId");
  57. constexpr InputChannelId testInputChannelId2(testInputChannelId1);
  58. static_assert(testInputChannelId1 == testInputChannelId2);
  59. EXPECT_EQ(testInputChannelId1, testInputChannelId2);
  60. }
  61. ////////////////////////////////////////////////////////////////////////////////////////////////
  62. TEST_F(InputTest, InputDeviceId_ConstExpression_CopyConstructorSuccessfull)
  63. {
  64. constexpr InputDeviceId testInputDeviceId1("TestInputDeviceId");
  65. constexpr InputDeviceId testInputDeviceId2(testInputDeviceId1);
  66. static_assert(testInputDeviceId1 == testInputDeviceId2);
  67. EXPECT_EQ(testInputDeviceId1, testInputDeviceId2);
  68. }
  69. ////////////////////////////////////////////////////////////////////////////////////////////////
  70. TEST_F(InputTest, InputContext_InitWithDataStruct_InitializationSuccessfull)
  71. {
  72. // Create an input context using an init data struct.
  73. constexpr AZ::s32 testPriority = 9;
  74. InputContext::InitData initData;
  75. initData.priority = testPriority;
  76. InputContext inputContext("TestInputContext", initData);
  77. EXPECT_EQ(inputContext.GetPriority(), testPriority);
  78. }
  79. ////////////////////////////////////////////////////////////////////////////////////////////////
  80. TEST_F(InputTest, InputContext_ActivateDeactivate_Successfull)
  81. {
  82. if (!m_gamepadSupported)
  83. {
  84. #if defined(GTEST_SKIP)
  85. GTEST_SKIP() << "Skipping test InputContext_ActivateDeactivate_Successfull";
  86. #else
  87. SUCCEED() << "Skipping test InputContext_ActivateDeactivate_Successfull";
  88. #endif
  89. return;
  90. }
  91. // Create an input context (they are inactive by default).
  92. InputContext inputContext("TestInputContext");
  93. // Create an input mapping, add a single source input, and add the mapping to the context.
  94. AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMapping"), inputContext);
  95. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
  96. EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
  97. // Validate the initial state of the mapping.
  98. EXPECT_FALSE(inputMapping->IsActive());
  99. EXPECT_TRUE(inputMapping->IsStateIdle());
  100. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  101. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  102. // Simulate a button press, then validate the state of the mapping is unchanged.
  103. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  104. &AzFramework::InputChannelRequests::SimulateRawInput,
  105. 1.0f);
  106. EXPECT_FALSE(inputMapping->IsActive());
  107. EXPECT_TRUE(inputMapping->IsStateIdle());
  108. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  109. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  110. // Reset the button, then validate the state of the mapping is unchanged.
  111. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A, &AzFramework::InputChannelRequests::ResetState);
  112. EXPECT_FALSE(inputMapping->IsActive());
  113. EXPECT_TRUE(inputMapping->IsStateIdle());
  114. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  115. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  116. // Activate the input context, then validate the state of the mapping is unchanged.
  117. inputContext.Activate();
  118. EXPECT_FALSE(inputMapping->IsActive());
  119. EXPECT_TRUE(inputMapping->IsStateIdle());
  120. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  121. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  122. // Simulate a button press, then validate the state of the mapping has changed.
  123. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  124. &AzFramework::InputChannelRequests::SimulateRawInput,
  125. 1.0f);
  126. EXPECT_TRUE(inputMapping->IsActive());
  127. EXPECT_TRUE(inputMapping->IsStateBegan());
  128. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  129. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  130. // Deactivate the input context, then validate the state of the mapping is unchanged.
  131. inputContext.Deactivate();
  132. EXPECT_TRUE(inputMapping->IsActive());
  133. EXPECT_TRUE(inputMapping->IsStateBegan());
  134. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  135. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  136. // Simulate a button release, then validate the state of the mapping is unchanged.
  137. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  138. &AzFramework::InputChannelRequests::SimulateRawInput,
  139. 0.0f);
  140. EXPECT_TRUE(inputMapping->IsActive());
  141. EXPECT_TRUE(inputMapping->IsStateBegan());
  142. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  143. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  144. }
  145. ////////////////////////////////////////////////////////////////////////////////////////////////
  146. TEST_F(InputTest, InputContext_AddRemoveInputMapping_Successfull)
  147. {
  148. if (!m_gamepadSupported)
  149. {
  150. #if defined(GTEST_SKIP)
  151. GTEST_SKIP() << "Skipping test InputContext_AddRemoveInputMapping_Successfull";
  152. #else
  153. SUCCEED() << "Skipping test InputContext_AddRemoveInputMapping_Successfull";
  154. #endif
  155. return;
  156. }
  157. // Create an input context and activate it.
  158. InputContext inputContext("TestInputContext");
  159. inputContext.Activate();
  160. // Create an input mapping and add a single source input.
  161. const InputChannelId inputMappingId = InputChannelId("TestInputMapping");
  162. AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(inputMappingId, inputContext);
  163. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
  164. // Validate the initial state of the mapping.
  165. EXPECT_FALSE(inputMapping->IsActive());
  166. EXPECT_TRUE(inputMapping->IsStateIdle());
  167. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  168. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  169. // Simulate a button press, then validate the state of the mapping is unchanged.
  170. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  171. &AzFramework::InputChannelRequests::SimulateRawInput,
  172. 1.0f);
  173. EXPECT_FALSE(inputMapping->IsActive());
  174. EXPECT_TRUE(inputMapping->IsStateIdle());
  175. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  176. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  177. // Reset the button, then validate the state of the mapping is unchanged.
  178. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A, &AzFramework::InputChannelRequests::ResetState);
  179. EXPECT_FALSE(inputMapping->IsActive());
  180. EXPECT_TRUE(inputMapping->IsStateIdle());
  181. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  182. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  183. // Add the mapping to the context, then validate the state of the mapping is unchanged.
  184. EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
  185. EXPECT_FALSE(inputMapping->IsActive());
  186. EXPECT_TRUE(inputMapping->IsStateIdle());
  187. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  188. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  189. // Simulate a button press, then validate the state of the mapping has changed.
  190. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  191. &AzFramework::InputChannelRequests::SimulateRawInput,
  192. 1.0f);
  193. EXPECT_TRUE(inputMapping->IsActive());
  194. EXPECT_TRUE(inputMapping->IsStateBegan());
  195. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  196. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  197. // Remove the mapping from the context, then validate the state of the mapping is unchanged.
  198. EXPECT_TRUE(inputContext.RemoveInputMapping(inputMappingId));
  199. EXPECT_TRUE(inputMapping->IsActive());
  200. EXPECT_TRUE(inputMapping->IsStateBegan());
  201. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  202. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  203. // Simulate a button release, then validate the state of the mapping is unchanged.
  204. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  205. &AzFramework::InputChannelRequests::SimulateRawInput,
  206. 0.0f);
  207. EXPECT_TRUE(inputMapping->IsActive());
  208. EXPECT_TRUE(inputMapping->IsStateBegan());
  209. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  210. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  211. }
  212. ////////////////////////////////////////////////////////////////////////////////////////////////
  213. TEST_F(InputTest, InputContext_AddSameMappingTwice_Unsuccessful)
  214. {
  215. // Validate that we can't add the same input mapping more than one to the same input context.
  216. InputContext inputContext("TestInputContext");
  217. AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMapping"), inputContext);
  218. EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
  219. EXPECT_FALSE(inputContext.AddInputMapping(inputMapping));
  220. }
  221. ////////////////////////////////////////////////////////////////////////////////////////////////
  222. TEST_F(InputTest, InputContext_RemoveMappingThatHasNotBeenAdded_Unsuccessful)
  223. {
  224. // Validate that we can't remove an input mapping that has not been added to an input context.
  225. InputContext inputContext("TestInputContext");
  226. EXPECT_FALSE(inputContext.RemoveInputMapping(InputChannelId("TestInputMapping")));
  227. }
  228. ////////////////////////////////////////////////////////////////////////////////////////////////
  229. TEST_F(InputTest, InputContext_AddMappingToContextThatIsNotParent_Unsuccessful)
  230. {
  231. // Validate that we can't add a mapping to a context that is not its parent.
  232. InputContext inputContext("TestInputContext");
  233. AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMapping"), inputContext);
  234. InputContext otherInputContext("TestInputContext");
  235. EXPECT_FALSE(otherInputContext.AddInputMapping(inputMapping));
  236. }
  237. ////////////////////////////////////////////////////////////////////////////////////////////////
  238. TEST_F(InputTest, InputContext_AddNullMapping_Unsuccessful)
  239. {
  240. // Validate that we can't add a null mapping to an input context.
  241. InputContext inputContext("TestInputContext");
  242. EXPECT_FALSE(inputContext.AddInputMapping(nullptr));
  243. }
  244. ////////////////////////////////////////////////////////////////////////////////////////////////
  245. TEST_F(InputTest, InputContext_ConsumeProcessedInput_Consumed)
  246. {
  247. if (!m_gamepadSupported)
  248. {
  249. #if defined(GTEST_SKIP)
  250. GTEST_SKIP() << "Skipping test InputContext_ConsumeProcessedInput_Consumed";
  251. #else
  252. SUCCEED() << "Skipping test InputContext_ConsumeProcessedInput_Consumed";
  253. #endif
  254. return;
  255. }
  256. InputContext::InitData initData;
  257. // Create a high priority input context that consumes input processed by any of its mappings.
  258. initData.autoActivate = true;
  259. initData.consumesProcessedInput = true;
  260. initData.priority = InputChannelEventListener::GetPriorityFirst();
  261. InputContext inputContextPriorityHigh("TestInputContextPriorityHigh", initData);
  262. // Create a default priority input context that does not consume any input.
  263. initData.autoActivate = true;
  264. initData.consumesProcessedInput = false;
  265. initData.priority = InputChannelEventListener::GetPriorityDefault();
  266. InputContext inputContextPriorityDefault("TestInputContextPriorityDefault", initData);
  267. // Create a low priority input context that does not consume any input.
  268. initData.autoActivate = true;
  269. initData.consumesProcessedInput = false;
  270. initData.priority = InputChannelEventListener::GetPriorityLast();
  271. InputContext inputContextPriorityLow("TestInputContextPriorityLow", initData);
  272. // Create an input mapping to add to the high priority input context.
  273. AZStd::shared_ptr<InputMappingOr> inputMappingPriorityHigh = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMappingPriorityHigh"), inputContextPriorityHigh);
  274. EXPECT_TRUE(inputMappingPriorityHigh->AddSourceInput(InputDeviceGamepad::Button::A));
  275. EXPECT_TRUE(inputContextPriorityHigh.AddInputMapping(inputMappingPriorityHigh));
  276. // Create the same input mapping to add to the default and low priority input contexts.
  277. AZStd::shared_ptr<InputMappingOr> inputMappingPriorityDefault = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMappingPriorityDefault"), inputContextPriorityDefault);
  278. EXPECT_TRUE(inputMappingPriorityDefault->AddSourceInput(InputDeviceGamepad::Button::A));
  279. EXPECT_TRUE(inputContextPriorityDefault.AddInputMapping(inputMappingPriorityDefault));
  280. AZStd::shared_ptr<InputMappingOr> inputMappingPriorityLow = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMappingPriorityLow"), inputContextPriorityLow);
  281. EXPECT_TRUE(inputMappingPriorityLow->AddSourceInput(InputDeviceGamepad::Button::A));
  282. EXPECT_TRUE(inputContextPriorityLow.AddInputMapping(inputMappingPriorityLow));
  283. // Simulate a button press, then validate that it was consumed by the high priority context.
  284. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  285. &AzFramework::InputChannelRequests::SimulateRawInput,
  286. 1.0f);
  287. EXPECT_TRUE(inputMappingPriorityHigh->IsActive());
  288. EXPECT_TRUE(inputMappingPriorityHigh->IsStateBegan());
  289. EXPECT_EQ(inputMappingPriorityHigh->GetValue(), 1.0f);
  290. EXPECT_EQ(inputMappingPriorityHigh->GetDelta(), 1.0f);
  291. EXPECT_FALSE(inputMappingPriorityDefault->IsActive());
  292. EXPECT_TRUE(inputMappingPriorityDefault->IsStateIdle());
  293. EXPECT_EQ(inputMappingPriorityDefault->GetValue(), 0.0f);
  294. EXPECT_EQ(inputMappingPriorityDefault->GetDelta(), 0.0f);
  295. EXPECT_FALSE(inputMappingPriorityLow->IsActive());
  296. EXPECT_TRUE(inputMappingPriorityLow->IsStateIdle());
  297. EXPECT_EQ(inputMappingPriorityLow->GetValue(), 0.0f);
  298. EXPECT_EQ(inputMappingPriorityLow->GetDelta(), 0.0f);
  299. // Create different input mappings to add to the default and low priority input contexts.
  300. AZStd::shared_ptr<InputMappingOr> otherInputMappingPriorityDefault = AZStd::make_shared<InputMappingOr>(InputChannelId("OtherTestInputMappingPriorityDefault"), inputContextPriorityDefault);
  301. EXPECT_TRUE(otherInputMappingPriorityDefault->AddSourceInput(InputDeviceGamepad::Button::B));
  302. EXPECT_TRUE(inputContextPriorityDefault.AddInputMapping(otherInputMappingPriorityDefault));
  303. AZStd::shared_ptr<InputMappingOr> otherInputMappingPriorityLow = AZStd::make_shared<InputMappingOr>(InputChannelId("OtherTestInputMappingPriorityLow"), inputContextPriorityLow);
  304. EXPECT_TRUE(otherInputMappingPriorityLow->AddSourceInput(InputDeviceGamepad::Button::B));
  305. EXPECT_TRUE(inputContextPriorityLow.AddInputMapping(otherInputMappingPriorityLow));
  306. // Simulate a button press, then validate that it was not consumed.
  307. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::B,
  308. &AzFramework::InputChannelRequests::SimulateRawInput,
  309. 1.0f);
  310. EXPECT_TRUE(otherInputMappingPriorityDefault->IsActive());
  311. EXPECT_TRUE(otherInputMappingPriorityDefault->IsStateBegan());
  312. EXPECT_EQ(otherInputMappingPriorityDefault->GetValue(), 1.0f);
  313. EXPECT_EQ(otherInputMappingPriorityDefault->GetDelta(), 1.0f);
  314. EXPECT_TRUE(otherInputMappingPriorityLow->IsActive());
  315. EXPECT_TRUE(otherInputMappingPriorityLow->IsStateBegan());
  316. EXPECT_EQ(otherInputMappingPriorityLow->GetValue(), 1.0f);
  317. EXPECT_EQ(otherInputMappingPriorityLow->GetDelta(), 1.0f);
  318. }
  319. ////////////////////////////////////////////////////////////////////////////////////////////////
  320. TEST_F(InputTest, InputContext_FilteredInput_Mapped)
  321. {
  322. if (!m_gamepadSupported)
  323. {
  324. #if defined(GTEST_SKIP)
  325. GTEST_SKIP() << "Skipping test InputContext_FilteredInput_Mapped";
  326. #else
  327. SUCCEED() << "Skipping test InputContext_FilteredInput_Mapped";
  328. #endif
  329. return;
  330. }
  331. // Create an input context that initially only listens for keyboard input.
  332. InputContext::InitData initData;
  333. initData.autoActivate = true;
  334. AZStd::shared_ptr<InputChannelEventFilterInclusionList> inclusionFilter = AZStd::make_shared<InputChannelEventFilterInclusionList>();
  335. inclusionFilter->IncludeDeviceName(InputDeviceKeyboard::Id.GetNameCrc32());
  336. initData.filter = inclusionFilter;
  337. InputContext inputContext("TestInputContext", initData);
  338. // Create an input mapping and add multiple source inputs from different input devices.
  339. AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMapping"), inputContext);
  340. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceKeyboard::Key::AlphanumericA));
  341. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
  342. EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
  343. // Validate the initial state of the mapping.
  344. EXPECT_FALSE(inputMapping->IsActive());
  345. EXPECT_TRUE(inputMapping->IsStateIdle());
  346. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  347. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  348. // Simulate a gamepad button press, then validate the state of the mapping is unchanged.
  349. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  350. &AzFramework::InputChannelRequests::SimulateRawInput,
  351. 1.0f);
  352. EXPECT_FALSE(inputMapping->IsActive());
  353. EXPECT_TRUE(inputMapping->IsStateIdle());
  354. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  355. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  356. // Simulate a keyboard key press, then validate the state of the mapping is updated.
  357. AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
  358. &AzFramework::InputChannelRequests::SimulateRawInput,
  359. 1.0f);
  360. EXPECT_TRUE(inputMapping->IsActive());
  361. EXPECT_TRUE(inputMapping->IsStateBegan());
  362. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  363. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  364. // Reset the button and key, then validate the expected state of the mapping.
  365. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A, &AzFramework::InputChannelRequests::ResetState);
  366. AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA, &AzFramework::InputChannelRequests::ResetState);
  367. EXPECT_FALSE(inputMapping->IsActive());
  368. EXPECT_TRUE(inputMapping->IsStateEnded());
  369. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  370. EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
  371. // Tick the input system, then validate the state of the mapping reset.
  372. AzFramework::InputSystemRequestBus::Broadcast(&InputSystemRequests::TickInput);
  373. EXPECT_FALSE(inputMapping->IsActive());
  374. EXPECT_TRUE(inputMapping->IsStateIdle());
  375. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  376. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  377. // Update the filter so it also listens for gamepad input.
  378. inclusionFilter->IncludeDeviceName(InputDeviceGamepad::IdForIndex0.GetNameCrc32());
  379. // Simulate a gamepad button press, then validate the state of the mapping is updated.
  380. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  381. &AzFramework::InputChannelRequests::SimulateRawInput,
  382. 1.0f);
  383. EXPECT_TRUE(inputMapping->IsActive());
  384. EXPECT_TRUE(inputMapping->IsStateBegan());
  385. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  386. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  387. }
  388. ////////////////////////////////////////////////////////////////////////////////////////////////
  389. TEST_F(InputTest, InputMappingOr_AddRemoveSourceInput_Successful)
  390. {
  391. if (!m_gamepadSupported)
  392. {
  393. #if defined(GTEST_SKIP)
  394. GTEST_SKIP() << "Skipping test InputMappingOr_AddRemoveSourceInput_Successful";
  395. #else
  396. SUCCEED() << "Skipping test InputMappingOr_AddRemoveSourceInput_Successful";
  397. #endif
  398. return;
  399. }
  400. // Create an input context and activate it.
  401. InputContext inputContext("TestInputContext");
  402. inputContext.Activate();
  403. // Create an input mapping and add it to the context.
  404. AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMapping"), inputContext);
  405. EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
  406. // Validate the initial state of the mapping.
  407. EXPECT_FALSE(inputMapping->IsActive());
  408. EXPECT_TRUE(inputMapping->IsStateIdle());
  409. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  410. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  411. // Simulate a button press, then validate the state of the mapping is unchanged.
  412. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  413. &AzFramework::InputChannelRequests::SimulateRawInput,
  414. 1.0f);
  415. EXPECT_FALSE(inputMapping->IsActive());
  416. EXPECT_TRUE(inputMapping->IsStateIdle());
  417. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  418. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  419. // Reset the button, then validate the state of the mapping is unchanged.
  420. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A, &AzFramework::InputChannelRequests::ResetState);
  421. EXPECT_FALSE(inputMapping->IsActive());
  422. EXPECT_TRUE(inputMapping->IsStateIdle());
  423. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  424. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  425. // Add a source input to the mapping, then validate the state of the mapping is unchanged.
  426. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
  427. EXPECT_FALSE(inputMapping->IsActive());
  428. EXPECT_TRUE(inputMapping->IsStateIdle());
  429. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  430. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  431. // Simulate a button press, then validate the state of the mapping has changed.
  432. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  433. &AzFramework::InputChannelRequests::SimulateRawInput,
  434. 1.0f);
  435. EXPECT_TRUE(inputMapping->IsActive());
  436. EXPECT_TRUE(inputMapping->IsStateBegan());
  437. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  438. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  439. // Remove the source input from the mapping, then validate the state of the mapping is unchanged.
  440. EXPECT_TRUE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::A));
  441. EXPECT_TRUE(inputMapping->IsActive());
  442. EXPECT_TRUE(inputMapping->IsStateBegan());
  443. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  444. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  445. // Simulate a button release, then validate the state of the mapping is unchanged.
  446. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  447. &AzFramework::InputChannelRequests::SimulateRawInput,
  448. 0.0f);
  449. EXPECT_TRUE(inputMapping->IsActive());
  450. EXPECT_TRUE(inputMapping->IsStateBegan());
  451. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  452. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  453. // Validate that we can't add a source input twice.
  454. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
  455. EXPECT_FALSE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
  456. EXPECT_TRUE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::A));
  457. // Validate that we can't remove a source input that has not been added.
  458. EXPECT_FALSE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::B));
  459. }
  460. ////////////////////////////////////////////////////////////////////////////////////////////////
  461. TEST_F(InputTest, InputMappingOr_SingleSourceInput_Mapped)
  462. {
  463. if (!m_gamepadSupported)
  464. {
  465. #if defined(GTEST_SKIP)
  466. GTEST_SKIP() << "Skipping test InputMappingOr_SingleSourceInput_Mapped";
  467. #else
  468. SUCCEED() << "Skipping test InputMappingOr_SingleSourceInput_Mapped";
  469. #endif
  470. return;
  471. }
  472. // Create an input context and activate it.
  473. InputContext inputContext("TestInputContext");
  474. inputContext.Activate();
  475. // Create an input mapping, add a single source input, and add the mapping to the context.
  476. AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMapping"), inputContext);
  477. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
  478. EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
  479. // Validate the initial state of the mapping.
  480. EXPECT_FALSE(inputMapping->IsActive());
  481. EXPECT_TRUE(inputMapping->IsStateIdle());
  482. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  483. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  484. // Simulate a button press, then validate the expected state of the mapping.
  485. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  486. &AzFramework::InputChannelRequests::SimulateRawInput,
  487. 1.0f);
  488. EXPECT_TRUE(inputMapping->IsActive());
  489. EXPECT_TRUE(inputMapping->IsStateBegan());
  490. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  491. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  492. // Simulate a button hold, then validate the expected state of the mapping.
  493. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  494. &AzFramework::InputChannelRequests::SimulateRawInput,
  495. 1.0f);
  496. EXPECT_TRUE(inputMapping->IsActive());
  497. EXPECT_TRUE(inputMapping->IsStateUpdated());
  498. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  499. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  500. // Simulate a button release, then validate the expected state of the mapping.
  501. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  502. &AzFramework::InputChannelRequests::SimulateRawInput,
  503. 0.0f);
  504. EXPECT_FALSE(inputMapping->IsActive());
  505. EXPECT_TRUE(inputMapping->IsStateEnded());
  506. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  507. EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
  508. // Simulate a tick of the input system, then validate the expected state of the mapping.
  509. AzFramework::InputSystemRequestBus::Broadcast(&InputSystemRequests::TickInput);
  510. EXPECT_FALSE(inputMapping->IsActive());
  511. EXPECT_TRUE(inputMapping->IsStateIdle());
  512. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  513. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  514. // Remove the source input, simulate a button press, then validate the expected state of the mapping.
  515. EXPECT_TRUE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::A));
  516. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  517. &AzFramework::InputChannelRequests::SimulateRawInput,
  518. 1.0f);
  519. EXPECT_FALSE(inputMapping->IsActive());
  520. EXPECT_TRUE(inputMapping->IsStateIdle());
  521. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  522. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  523. }
  524. ////////////////////////////////////////////////////////////////////////////////////////////////
  525. TEST_F(InputTest, InputMappingOr_MultipleSourceInputs_Mapped)
  526. {
  527. if (!m_gamepadSupported)
  528. {
  529. #if defined(GTEST_SKIP)
  530. GTEST_SKIP() << "Skipping test InputMappingOr_MultipleSourceInputs_Mapped";
  531. #else
  532. SUCCEED() << "Skipping test InputMappingOr_MultipleSourceInputs_Mapped";
  533. #endif
  534. return;
  535. }
  536. // Create an input context and activate it.
  537. InputContext inputContext("TestInputContext");
  538. inputContext.Activate();
  539. // Create an input mapping, add multiple source inputs, and add the mapping to the context.
  540. AZStd::shared_ptr<InputMappingOr> inputMapping = AZStd::make_shared<InputMappingOr>(InputChannelId("TestInputMapping"), inputContext);
  541. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
  542. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceKeyboard::Key::AlphanumericA));
  543. EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
  544. // Validate the initial state of the mapping.
  545. EXPECT_FALSE(inputMapping->IsActive());
  546. EXPECT_TRUE(inputMapping->IsStateIdle());
  547. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  548. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  549. // Simulate a button press, then validate the expected state of the mapping.
  550. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  551. &AzFramework::InputChannelRequests::SimulateRawInput,
  552. 1.0f);
  553. EXPECT_TRUE(inputMapping->IsActive());
  554. EXPECT_TRUE(inputMapping->IsStateBegan());
  555. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  556. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  557. // Simulate a button hold, then validate the expected state of the mapping.
  558. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  559. &AzFramework::InputChannelRequests::SimulateRawInput,
  560. 1.0f);
  561. EXPECT_TRUE(inputMapping->IsActive());
  562. EXPECT_TRUE(inputMapping->IsStateUpdated());
  563. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  564. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  565. // Simulate a button release, then validate the expected state of the mapping.
  566. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  567. &AzFramework::InputChannelRequests::SimulateRawInput,
  568. 0.0f);
  569. EXPECT_FALSE(inputMapping->IsActive());
  570. EXPECT_TRUE(inputMapping->IsStateEnded());
  571. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  572. EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
  573. // Simulate a tick of the input system, then validate the expected state of the mapping.
  574. AzFramework::InputSystemRequestBus::Broadcast(&InputSystemRequests::TickInput);
  575. EXPECT_FALSE(inputMapping->IsActive());
  576. EXPECT_TRUE(inputMapping->IsStateIdle());
  577. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  578. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  579. // Simulate a key press, then validate the expected state of the mapping.
  580. AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
  581. &AzFramework::InputChannelRequests::SimulateRawInput,
  582. 1.0f);
  583. EXPECT_TRUE(inputMapping->IsActive());
  584. EXPECT_TRUE(inputMapping->IsStateBegan());
  585. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  586. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  587. // Simulate a key hold, then validate the expected state of the mapping.
  588. AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
  589. &AzFramework::InputChannelRequests::SimulateRawInput,
  590. 1.0f);
  591. EXPECT_TRUE(inputMapping->IsActive());
  592. EXPECT_TRUE(inputMapping->IsStateUpdated());
  593. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  594. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  595. // Simulate a key release, then validate the expected state of the mapping.
  596. AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
  597. &AzFramework::InputChannelRequests::SimulateRawInput,
  598. 0.0f);
  599. EXPECT_FALSE(inputMapping->IsActive());
  600. EXPECT_TRUE(inputMapping->IsStateEnded());
  601. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  602. EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
  603. // Simulate a tick of the input system, then validate the expected state of the mapping.
  604. AzFramework::InputSystemRequestBus::Broadcast(&InputSystemRequests::TickInput);
  605. EXPECT_FALSE(inputMapping->IsActive());
  606. EXPECT_TRUE(inputMapping->IsStateIdle());
  607. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  608. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  609. }
  610. ////////////////////////////////////////////////////////////////////////////////////////////////
  611. TEST_F(InputTest, InputMappingAnd_AddRemoveSourceInput_Successful)
  612. {
  613. if (!m_gamepadSupported)
  614. {
  615. #if defined(GTEST_SKIP)
  616. GTEST_SKIP() << "Skipping test InputMappingAnd_AddRemoveSourceInput_Successful";
  617. #else
  618. SUCCEED() << "Skipping test InputMappingAnd_AddRemoveSourceInput_Successful";
  619. #endif
  620. return;
  621. }
  622. // Create an input context and activate it.
  623. InputContext inputContext("TestInputContext");
  624. inputContext.Activate();
  625. // Create an input mapping and add it to the context.
  626. AZStd::shared_ptr<InputMappingAnd> inputMapping = AZStd::make_shared<InputMappingAnd>(InputChannelId("TestInputMapping"), inputContext);
  627. EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
  628. // Validate the initial state of the mapping.
  629. EXPECT_FALSE(inputMapping->IsActive());
  630. EXPECT_TRUE(inputMapping->IsStateIdle());
  631. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  632. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  633. // Simulate a button press, then validate the state of the mapping is unchanged.
  634. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  635. &AzFramework::InputChannelRequests::SimulateRawInput,
  636. 1.0f);
  637. EXPECT_FALSE(inputMapping->IsActive());
  638. EXPECT_TRUE(inputMapping->IsStateIdle());
  639. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  640. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  641. // Reset the button, then validate the state of the mapping is unchanged.
  642. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A, &AzFramework::InputChannelRequests::ResetState);
  643. EXPECT_FALSE(inputMapping->IsActive());
  644. EXPECT_TRUE(inputMapping->IsStateIdle());
  645. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  646. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  647. // Add a source input to the mapping, then validate the state of the mapping is unchanged.
  648. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
  649. EXPECT_FALSE(inputMapping->IsActive());
  650. EXPECT_TRUE(inputMapping->IsStateIdle());
  651. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  652. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  653. // Simulate a button press, then validate the state of the mapping has changed.
  654. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  655. &AzFramework::InputChannelRequests::SimulateRawInput,
  656. 1.0f);
  657. EXPECT_TRUE(inputMapping->IsActive());
  658. EXPECT_TRUE(inputMapping->IsStateBegan());
  659. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  660. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  661. // Remove the source input from the mapping, then validate the state of the mapping is unchanged.
  662. EXPECT_TRUE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::A));
  663. EXPECT_TRUE(inputMapping->IsActive());
  664. EXPECT_TRUE(inputMapping->IsStateBegan());
  665. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  666. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  667. // Simulate a button release, then validate the state of the mapping is unchanged.
  668. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  669. &AzFramework::InputChannelRequests::SimulateRawInput,
  670. 0.0f);
  671. EXPECT_TRUE(inputMapping->IsActive());
  672. EXPECT_TRUE(inputMapping->IsStateBegan());
  673. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  674. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  675. // Validate that we can't add a source input twice.
  676. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
  677. EXPECT_FALSE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
  678. EXPECT_TRUE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::A));
  679. // Validate that we can't remove a source input that has not been added.
  680. EXPECT_FALSE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::B));
  681. }
  682. ////////////////////////////////////////////////////////////////////////////////////////////////
  683. TEST_F(InputTest, InputMappingAnd_SingleSourceInput_Mapped)
  684. {
  685. if (!m_gamepadSupported)
  686. {
  687. #if defined(GTEST_SKIP)
  688. GTEST_SKIP() << "Skipping test InputMappingAnd_SingleSourceInput_Mapped";
  689. #else
  690. SUCCEED() << "Skipping test InputMappingAnd_SingleSourceInput_Mapped";
  691. #endif
  692. return;
  693. }
  694. // Create an input context and activate it.
  695. InputContext inputContext("TestInputContext");
  696. inputContext.Activate();
  697. // Create an input mapping, add a single source input, and add the mapping to the context.
  698. AZStd::shared_ptr<InputMappingAnd> inputMapping = AZStd::make_shared<InputMappingAnd>(InputChannelId("TestInputMapping"), inputContext);
  699. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
  700. EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
  701. // Validate the initial state of the mapping.
  702. EXPECT_FALSE(inputMapping->IsActive());
  703. EXPECT_TRUE(inputMapping->IsStateIdle());
  704. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  705. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  706. // Simulate a button press, then validate the expected state of the mapping.
  707. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  708. &AzFramework::InputChannelRequests::SimulateRawInput,
  709. 1.0f);
  710. EXPECT_TRUE(inputMapping->IsActive());
  711. EXPECT_TRUE(inputMapping->IsStateBegan());
  712. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  713. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  714. // Simulate a button hold, then validate the expected state of the mapping.
  715. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  716. &AzFramework::InputChannelRequests::SimulateRawInput,
  717. 1.0f);
  718. EXPECT_TRUE(inputMapping->IsActive());
  719. EXPECT_TRUE(inputMapping->IsStateUpdated());
  720. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  721. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  722. // Simulate a button release, then validate the expected state of the mapping.
  723. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  724. &AzFramework::InputChannelRequests::SimulateRawInput,
  725. 0.0f);
  726. EXPECT_FALSE(inputMapping->IsActive());
  727. EXPECT_TRUE(inputMapping->IsStateEnded());
  728. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  729. EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
  730. // Simulate a tick of the input system, then validate the expected state of the mapping.
  731. AzFramework::InputSystemRequestBus::Broadcast(&InputSystemRequests::TickInput);
  732. EXPECT_FALSE(inputMapping->IsActive());
  733. EXPECT_TRUE(inputMapping->IsStateIdle());
  734. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  735. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  736. // Remove the source input, simulate a button press, then validate the expected state of the mapping.
  737. EXPECT_TRUE(inputMapping->RemoveSourceInput(InputDeviceGamepad::Button::A));
  738. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  739. &AzFramework::InputChannelRequests::SimulateRawInput,
  740. 1.0f);
  741. EXPECT_FALSE(inputMapping->IsActive());
  742. EXPECT_TRUE(inputMapping->IsStateIdle());
  743. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  744. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  745. }
  746. ////////////////////////////////////////////////////////////////////////////////////////////////
  747. TEST_F(InputTest, InputMappingAnd_MultipleSourceInputs_Mapped)
  748. {
  749. if (!m_gamepadSupported)
  750. {
  751. #if defined(GTEST_SKIP)
  752. GTEST_SKIP() << "Skipping test InputMappingAnd_MultipleSourceInputs_Mapped";
  753. #else
  754. SUCCEED() << "Skipping test InputMappingAnd_MultipleSourceInputs_Mapped";
  755. #endif
  756. return;
  757. }
  758. // Create an input context and activate it.
  759. InputContext inputContext("TestInputContext");
  760. inputContext.Activate();
  761. // Create an input mapping, add multiple source inputs, and add the mapping to the context.
  762. AZStd::shared_ptr<InputMappingAnd> inputMapping = AZStd::make_shared<InputMappingAnd>(InputChannelId("TestInputMapping"), inputContext);
  763. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Button::A));
  764. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceKeyboard::Key::AlphanumericA));
  765. EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
  766. // Validate the initial state of the mapping.
  767. EXPECT_FALSE(inputMapping->IsActive());
  768. EXPECT_TRUE(inputMapping->IsStateIdle());
  769. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  770. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  771. // Simulate a button press, then validate the expected state of the mapping.
  772. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  773. &AzFramework::InputChannelRequests::SimulateRawInput,
  774. 1.0f);
  775. EXPECT_FALSE(inputMapping->IsActive());
  776. EXPECT_TRUE(inputMapping->IsStateIdle());
  777. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  778. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  779. // Simulate a key press, then validate the expected state of the mapping.
  780. AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
  781. &AzFramework::InputChannelRequests::SimulateRawInput,
  782. 1.0f);
  783. EXPECT_TRUE(inputMapping->IsActive());
  784. EXPECT_TRUE(inputMapping->IsStateBegan());
  785. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  786. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  787. // Simulate a button hold, then validate the expected state of the mapping.
  788. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  789. &AzFramework::InputChannelRequests::SimulateRawInput,
  790. 1.0f);
  791. EXPECT_TRUE(inputMapping->IsActive());
  792. EXPECT_TRUE(inputMapping->IsStateUpdated());
  793. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  794. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  795. // Simulate a button release, then validate the expected state of the mapping.
  796. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  797. &AzFramework::InputChannelRequests::SimulateRawInput,
  798. 0.0f);
  799. EXPECT_FALSE(inputMapping->IsActive());
  800. EXPECT_TRUE(inputMapping->IsStateEnded());
  801. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  802. EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
  803. // Reset the button and key, tick the input system, then validate the state of the mapping reset.
  804. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A, &AzFramework::InputChannelRequests::ResetState);
  805. AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA, &AzFramework::InputChannelRequests::ResetState);
  806. AzFramework::InputSystemRequestBus::Broadcast(&InputSystemRequests::TickInput);
  807. EXPECT_FALSE(inputMapping->IsActive());
  808. EXPECT_TRUE(inputMapping->IsStateIdle());
  809. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  810. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  811. // Simulate a key press, then validate the expected state of the mapping.
  812. AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
  813. &AzFramework::InputChannelRequests::SimulateRawInput,
  814. 1.0f);
  815. EXPECT_FALSE(inputMapping->IsActive());
  816. EXPECT_TRUE(inputMapping->IsStateIdle());
  817. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  818. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  819. // Simulate a button press, then validate the expected state of the mapping.
  820. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A,
  821. &AzFramework::InputChannelRequests::SimulateRawInput,
  822. 1.0f);
  823. EXPECT_TRUE(inputMapping->IsActive());
  824. EXPECT_TRUE(inputMapping->IsStateBegan());
  825. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  826. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  827. // Simulate a key hold, then validate the expected state of the mapping.
  828. AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
  829. &AzFramework::InputChannelRequests::SimulateRawInput,
  830. 1.0f);
  831. EXPECT_TRUE(inputMapping->IsActive());
  832. EXPECT_TRUE(inputMapping->IsStateUpdated());
  833. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  834. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  835. // Simulate a key release, then validate the expected state of the mapping.
  836. AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA,
  837. &AzFramework::InputChannelRequests::SimulateRawInput,
  838. 0.0f);
  839. EXPECT_FALSE(inputMapping->IsActive());
  840. EXPECT_TRUE(inputMapping->IsStateEnded());
  841. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  842. EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
  843. // Reset the button and key, tick the input system, then validate the state of the mapping reset.
  844. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Button::A, &AzFramework::InputChannelRequests::ResetState);
  845. AzFramework::InputChannelRequestBus::Event(InputDeviceKeyboard::Key::AlphanumericA, &AzFramework::InputChannelRequests::ResetState);
  846. AzFramework::InputSystemRequestBus::Broadcast(&InputSystemRequests::TickInput);
  847. EXPECT_FALSE(inputMapping->IsActive());
  848. EXPECT_TRUE(inputMapping->IsStateIdle());
  849. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  850. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  851. }
  852. ////////////////////////////////////////////////////////////////////////////////////////////////
  853. TEST_F(InputTest, InputMappingAnd_MultipleSourceInputsWithDifferentValues_ValuesAveraged)
  854. {
  855. if (!m_gamepadSupported)
  856. {
  857. #if defined(GTEST_SKIP)
  858. GTEST_SKIP() << "Skipping test InputMappingAnd_MultipleSourceInputsWithDifferentValues_ValuesAveraged";
  859. #else
  860. SUCCEED() << "Skipping test InputMappingAnd_MultipleSourceInputsWithDifferentValues_ValuesAveraged";
  861. #endif
  862. return;
  863. }
  864. // Create an input context and activate it.
  865. InputContext inputContext("TestInputContext");
  866. inputContext.Activate();
  867. // Create an input mapping, add multiple source inputs, and add the mapping to the context.
  868. AZStd::shared_ptr<InputMappingAnd> inputMapping = AZStd::make_shared<InputMappingAnd>(InputChannelId("TestInputMapping"), inputContext);
  869. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Trigger::L2));
  870. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Trigger::R2));
  871. EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
  872. // Validate the initial state of the mapping.
  873. EXPECT_FALSE(inputMapping->IsActive());
  874. EXPECT_TRUE(inputMapping->IsStateIdle());
  875. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  876. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  877. // Simulate an L2 trigger press, then validate the expected state of the mapping.
  878. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Trigger::L2,
  879. &AzFramework::InputChannelRequests::SimulateRawInput,
  880. 0.25f);
  881. EXPECT_FALSE(inputMapping->IsActive());
  882. EXPECT_TRUE(inputMapping->IsStateIdle());
  883. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  884. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  885. // Simulate an R2 trigger press, then validate the expected state of the mapping.
  886. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Trigger::R2,
  887. &AzFramework::InputChannelRequests::SimulateRawInput,
  888. 0.75f);
  889. EXPECT_TRUE(inputMapping->IsActive());
  890. EXPECT_TRUE(inputMapping->IsStateBegan());
  891. EXPECT_EQ(inputMapping->GetValue(), 0.5f);
  892. EXPECT_EQ(inputMapping->GetDelta(), 0.5f);
  893. // Simulate an L2 trigger value change, then validate the expected state of the mapping.
  894. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Trigger::L2,
  895. &AzFramework::InputChannelRequests::SimulateRawInput,
  896. 0.75f);
  897. EXPECT_TRUE(inputMapping->IsActive());
  898. EXPECT_TRUE(inputMapping->IsStateUpdated());
  899. EXPECT_EQ(inputMapping->GetValue(), 0.75f);
  900. EXPECT_EQ(inputMapping->GetDelta(), 0.25f);
  901. // Simulate an R2 trigger release, then validate the expected state of the mapping.
  902. AzFramework::InputChannelRequestBus::Event(InputDeviceGamepad::Trigger::R2,
  903. &AzFramework::InputChannelRequests::SimulateRawInput,
  904. 0.0f);
  905. EXPECT_FALSE(inputMapping->IsActive());
  906. EXPECT_TRUE(inputMapping->IsStateEnded());
  907. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  908. EXPECT_EQ(inputMapping->GetDelta(), -0.75f);
  909. }
  910. ////////////////////////////////////////////////////////////////////////////////////////////////
  911. TEST_F(InputTest, InputMappingAnd_MultipleSourceInputsFromTheSameInputDeviceTypeWithDifferentIndicies_NotMapped)
  912. {
  913. if (!m_gamepadSupported)
  914. {
  915. #if defined(GTEST_SKIP)
  916. GTEST_SKIP() << "Skipping test InputMappingAnd_MultipleSourceInputsFromTheSameInputDeviceTypeWithDifferentIndicies_NotMapped";
  917. #else
  918. SUCCEED() << "Skipping test InputMappingAnd_MultipleSourceInputsFromTheSameInputDeviceTypeWithDifferentIndicies_NotMapped";
  919. #endif
  920. return;
  921. }
  922. // Create an input context and activate it.
  923. InputContext inputContext("TestInputContext");
  924. inputContext.Activate();
  925. // Create an input mapping, add multiple source inputs, and add the mapping to the context.
  926. AZStd::shared_ptr<InputMappingAnd> inputMapping = AZStd::make_shared<InputMappingAnd>(InputChannelId("TestInputMapping"), inputContext);
  927. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Trigger::L2));
  928. EXPECT_TRUE(inputMapping->AddSourceInput(InputDeviceGamepad::Trigger::R2));
  929. EXPECT_TRUE(inputContext.AddInputMapping(inputMapping));
  930. // Validate the initial state of the mapping.
  931. EXPECT_FALSE(inputMapping->IsActive());
  932. EXPECT_TRUE(inputMapping->IsStateIdle());
  933. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  934. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  935. // Simulate an L2 trigger press from device index 0, then validate the expected state of the mapping.
  936. AzFramework::InputChannelRequestBus::Event(InputChannelRequests::BusIdType(InputDeviceGamepad::Trigger::L2, 0),
  937. &AzFramework::InputChannelRequests::SimulateRawInput,
  938. 1.0f);
  939. EXPECT_FALSE(inputMapping->IsActive());
  940. EXPECT_TRUE(inputMapping->IsStateIdle());
  941. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  942. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  943. // Simulate an L2 trigger press from device index 1, then validate the expected state of the mapping.
  944. AzFramework::InputChannelRequestBus::Event(InputChannelRequests::BusIdType(InputDeviceGamepad::Trigger::L2, 1),
  945. &AzFramework::InputChannelRequests::SimulateRawInput,
  946. 1.0f);
  947. EXPECT_FALSE(inputMapping->IsActive());
  948. EXPECT_TRUE(inputMapping->IsStateIdle());
  949. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  950. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  951. // Simulate an R2 trigger press from device index 1, then validate the expected state of the mapping.
  952. AzFramework::InputChannelRequestBus::Event(InputChannelRequests::BusIdType(InputDeviceGamepad::Trigger::R2, 1),
  953. &AzFramework::InputChannelRequests::SimulateRawInput,
  954. 1.0f);
  955. EXPECT_FALSE(inputMapping->IsActive());
  956. EXPECT_TRUE(inputMapping->IsStateIdle());
  957. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  958. EXPECT_EQ(inputMapping->GetDelta(), 0.0f);
  959. // Simulate an R2 trigger press from device index 0, then validate the expected state of the mapping.
  960. AzFramework::InputChannelRequestBus::Event(InputChannelRequests::BusIdType(InputDeviceGamepad::Trigger::R2, 0),
  961. &AzFramework::InputChannelRequests::SimulateRawInput,
  962. 1.0f);
  963. EXPECT_TRUE(inputMapping->IsActive());
  964. EXPECT_TRUE(inputMapping->IsStateBegan());
  965. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  966. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  967. // Simulate an L2 trigger release from device index 1, then validate the expected state of the mapping.
  968. AzFramework::InputChannelRequestBus::Event(InputChannelRequests::BusIdType(InputDeviceGamepad::Trigger::L2, 1),
  969. &AzFramework::InputChannelRequests::SimulateRawInput,
  970. 0.0f);
  971. EXPECT_TRUE(inputMapping->IsActive());
  972. EXPECT_TRUE(inputMapping->IsStateBegan());
  973. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  974. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  975. // Simulate an R2 trigger release from device index 1, then validate the expected state of the mapping.
  976. AzFramework::InputChannelRequestBus::Event(InputChannelRequests::BusIdType(InputDeviceGamepad::Trigger::R2, 1),
  977. &AzFramework::InputChannelRequests::SimulateRawInput,
  978. 0.0f);
  979. EXPECT_TRUE(inputMapping->IsActive());
  980. EXPECT_TRUE(inputMapping->IsStateBegan());
  981. EXPECT_EQ(inputMapping->GetValue(), 1.0f);
  982. EXPECT_EQ(inputMapping->GetDelta(), 1.0f);
  983. // Simulate an R2 trigger release from device index 0, then validate the expected state of the mapping.
  984. AzFramework::InputChannelRequestBus::Event(InputChannelRequests::BusIdType(InputDeviceGamepad::Trigger::R2, 0),
  985. &AzFramework::InputChannelRequests::SimulateRawInput,
  986. 0.0f);
  987. EXPECT_FALSE(inputMapping->IsActive());
  988. EXPECT_TRUE(inputMapping->IsStateEnded());
  989. EXPECT_EQ(inputMapping->GetValue(), 0.0f);
  990. EXPECT_EQ(inputMapping->GetDelta(), -1.0f);
  991. }
  992. } // namespace UnitTest