InputTests.cpp 61 KB

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