QtEventToAzInputMapperTests.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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 <AzToolsFramework/Input/QtEventToAzInputMapper.h>
  9. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  10. #include <AzFramework/Input/Events/InputTextEventListener.h>
  11. #include <AzFramework/Input/Channels/InputChannel.h>
  12. namespace UnitTest
  13. {
  14. static bool IsMouseButton(const AzFramework::InputChannelId& inputChannelId)
  15. {
  16. const auto& buttons = AzFramework::InputDeviceMouse::Button::All;
  17. const auto& it = AZStd::find(buttons.cbegin(), buttons.cend(), inputChannelId);
  18. return it != buttons.cend();
  19. }
  20. class QtEventToAzInputMapperFixture
  21. : public LeakDetectionFixture
  22. , public AzFramework::InputChannelNotificationBus::Handler
  23. , public AzFramework::InputTextNotificationBus::Handler
  24. {
  25. public:
  26. static inline constexpr QSize WidgetSize = QSize(1920, 1080);
  27. static inline constexpr int TestDeviceIdSeed = 4321;
  28. void SetUp() override
  29. {
  30. LeakDetectionFixture::SetUp();
  31. m_rootWidget = AZStd::make_unique<QWidget>();
  32. m_rootWidget->setFixedSize(WidgetSize);
  33. m_rootWidget->move(0, 0);
  34. m_inputChannelMapper = AZStd::make_unique<AzToolsFramework::QtEventToAzInputMapper>(m_rootWidget.get(), TestDeviceIdSeed);
  35. // listen for events signaled from QtEventToAzInputMapper and forward to the controller list
  36. QObject::connect(m_inputChannelMapper.get(), &AzToolsFramework::QtEventToAzInputMapper::InputChannelUpdated, m_rootWidget.get(),
  37. [this]([[maybe_unused]] const AzFramework::InputChannel* inputChannel, QEvent* event)
  38. {
  39. if(event == nullptr)
  40. {
  41. return;
  42. }
  43. const QEvent::Type eventType = event->type();
  44. if (eventType == QEvent::Type::MouseButtonPress ||
  45. eventType == QEvent::Type::MouseButtonRelease ||
  46. eventType == QEvent::Type::MouseButtonDblClick)
  47. {
  48. m_signalEvents.push_back(QtEventInfo(static_cast<QMouseEvent*>(event)));
  49. event->accept();
  50. }
  51. else if (eventType == QEvent::Type::Wheel)
  52. {
  53. m_signalEvents.push_back(QtEventInfo(static_cast<QWheelEvent*>(event)));
  54. event->accept();
  55. }
  56. else if (eventType == QEvent::Type::KeyPress ||
  57. eventType == QEvent::Type::KeyRelease ||
  58. eventType == QEvent::Type::ShortcutOverride)
  59. {
  60. m_signalEvents.push_back(QtEventInfo(static_cast<QKeyEvent*>(event)));
  61. event->accept();
  62. }
  63. });
  64. }
  65. void TearDown() override
  66. {
  67. m_inputChannelMapper.reset();
  68. m_rootWidget.reset();
  69. LeakDetectionFixture::TearDown();
  70. }
  71. void OnInputChannelEvent(const AzFramework::InputChannel& inputChannel, bool& hasBeenConsumed) override
  72. {
  73. AZ_Assert(hasBeenConsumed == false, "Unexpected input event consumed elsewhere during QtEventToAzInputMapper tests");
  74. const AzFramework::InputChannelId& inputChannelId = inputChannel.GetInputChannelId();
  75. const AzFramework::InputDeviceId& inputDeviceId = inputChannel.GetInputDevice().GetInputDeviceId();
  76. if (AzFramework::InputDeviceMouse::IsMouseDevice(inputDeviceId))
  77. {
  78. if (IsMouseButton(inputChannelId))
  79. {
  80. m_azChannelEvents.push_back(AzEventInfo(inputChannel));
  81. hasBeenConsumed = m_captureAzEvents;
  82. }
  83. else if (inputChannelId == AzFramework::InputDeviceMouse::Movement::Z)
  84. {
  85. m_azChannelEvents.push_back(AzEventInfo(inputChannel));
  86. hasBeenConsumed = m_captureAzEvents;
  87. }
  88. else if (inputChannelId == AzFramework::InputDeviceMouse::SystemCursorPosition)
  89. {
  90. m_azCursorPositions.push_back(*inputChannel.GetCustomData<AzFramework::InputChannel::PositionData2D>());
  91. hasBeenConsumed = m_captureAzEvents;
  92. }
  93. }
  94. else if (AzFramework::InputDeviceKeyboard::IsKeyboardDevice(inputDeviceId))
  95. {
  96. m_azChannelEvents.push_back(AzEventInfo(inputChannel));
  97. hasBeenConsumed = m_captureAzEvents;
  98. }
  99. }
  100. void OnInputTextEvent(const AZStd::string& textUtf8, bool& hasBeenConsumed) override
  101. {
  102. AZ_Assert(hasBeenConsumed == false, "Unexpected text event consumed elsewhere during QtEventToAzInputMapper tests");
  103. m_azTextEvents.push_back(textUtf8);
  104. hasBeenConsumed = m_captureTextEvents;
  105. }
  106. // simple structure for caching minimal QtEvent data necessary for testing
  107. struct QtEventInfo
  108. {
  109. explicit QtEventInfo(QMouseEvent* mouseEvent)
  110. : m_eventType(mouseEvent->type())
  111. , m_button(mouseEvent->button())
  112. {
  113. }
  114. explicit QtEventInfo(QWheelEvent* mouseWheelEvent)
  115. : m_eventType(mouseWheelEvent->type())
  116. , m_scrollPhase(mouseWheelEvent->phase())
  117. {
  118. }
  119. explicit QtEventInfo(QKeyEvent* keyEvent)
  120. : m_eventType(keyEvent->type())
  121. , m_key(keyEvent->key())
  122. {
  123. }
  124. QEvent::Type m_eventType{ QEvent::None };
  125. Qt::MouseButton m_button{ Qt::NoButton };
  126. Qt::ScrollPhase m_scrollPhase{ Qt::NoScrollPhase };
  127. int m_key{ 0 };
  128. };
  129. // simple structure for caching minimal AzInput event data necessary for testing
  130. struct AzEventInfo
  131. {
  132. AzEventInfo() = delete;
  133. explicit AzEventInfo(const AzFramework::InputChannel& inputChannel)
  134. : m_inputChannelId(inputChannel.GetInputChannelId())
  135. , m_isActive(inputChannel.IsActive())
  136. {
  137. }
  138. AzFramework::InputChannelId m_inputChannelId;
  139. bool m_isActive;
  140. };
  141. AZStd::unique_ptr<QWidget> m_rootWidget;
  142. AZStd::unique_ptr<AzToolsFramework::QtEventToAzInputMapper> m_inputChannelMapper;
  143. AZStd::vector<QtEventInfo> m_signalEvents;
  144. AZStd::vector<AzEventInfo> m_azChannelEvents;
  145. AZStd::vector<AZStd::string> m_azTextEvents;
  146. AZStd::vector<AzFramework::InputChannel::PositionData2D> m_azCursorPositions;
  147. bool m_captureAzEvents{ false };
  148. bool m_captureTextEvents{ false };
  149. };
  150. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  151. // Qt event forwarding through the internal signal handler test
  152. TEST_F(QtEventToAzInputMapperFixture, MouseWheel_NoAzHandlers_ReceivedThreeSignalAndZeroAzChannelEvents)
  153. {
  154. // setup
  155. const QPoint mouseEventPos = QPoint(WidgetSize.width() / 2, WidgetSize.height() / 2);
  156. const QPoint scrollDelta = QPoint(10, 10);
  157. MouseScroll(m_rootWidget.get(), mouseEventPos, scrollDelta);
  158. // qt validation
  159. ASSERT_EQ(m_signalEvents.size(), 3);
  160. EXPECT_EQ(m_signalEvents[0].m_eventType, QEvent::Type::Wheel);
  161. EXPECT_EQ(m_signalEvents[0].m_scrollPhase, Qt::ScrollBegin);
  162. EXPECT_EQ(m_signalEvents[1].m_eventType, QEvent::Type::Wheel);
  163. EXPECT_EQ(m_signalEvents[1].m_scrollPhase, Qt::ScrollUpdate);
  164. EXPECT_EQ(m_signalEvents[2].m_eventType, QEvent::Type::Wheel);
  165. EXPECT_EQ(m_signalEvents[2].m_scrollPhase, Qt::ScrollEnd);
  166. // az validation
  167. EXPECT_EQ(m_azChannelEvents.size(), 0);
  168. }
  169. // Qt event to AzInput event conversion test
  170. TEST_F(QtEventToAzInputMapperFixture, MouseWheel_AzHandlerNotCaptured_ReceivedThreeSignalAndThreeAzChannelEvents)
  171. {
  172. // setup
  173. const AzFramework::InputChannelId mouseWheelId = AzFramework::InputDeviceMouse::Movement::Z;
  174. const char* mouseWheelChannelName = mouseWheelId.GetName();
  175. AzFramework::InputChannelNotificationBus::Handler::BusConnect();
  176. m_captureAzEvents = false;
  177. const QPoint mouseEventPos = QPoint(WidgetSize.width() / 2, WidgetSize.height() / 2);
  178. const QPoint scrollDelta = QPoint(10, 10);
  179. MouseScroll(m_rootWidget.get(), mouseEventPos, scrollDelta);
  180. // qt validation
  181. ASSERT_EQ(m_signalEvents.size(), 3);
  182. EXPECT_EQ(m_signalEvents[0].m_eventType, QEvent::Type::Wheel);
  183. EXPECT_EQ(m_signalEvents[0].m_scrollPhase, Qt::ScrollBegin);
  184. EXPECT_EQ(m_signalEvents[1].m_eventType, QEvent::Type::Wheel);
  185. EXPECT_EQ(m_signalEvents[1].m_scrollPhase, Qt::ScrollUpdate);
  186. EXPECT_EQ(m_signalEvents[2].m_eventType, QEvent::Type::Wheel);
  187. EXPECT_EQ(m_signalEvents[2].m_scrollPhase, Qt::ScrollEnd);
  188. // az validation
  189. ASSERT_EQ(m_azChannelEvents.size(), 3);
  190. EXPECT_STREQ(m_azChannelEvents[0].m_inputChannelId.GetName(), mouseWheelChannelName);
  191. EXPECT_STREQ(m_azChannelEvents[1].m_inputChannelId.GetName(), mouseWheelChannelName);
  192. EXPECT_STREQ(m_azChannelEvents[2].m_inputChannelId.GetName(), mouseWheelChannelName);
  193. // cleanup
  194. AzFramework::InputChannelNotificationBus::Handler::BusDisconnect();
  195. }
  196. // AzInput event handler consumption test
  197. TEST_F(QtEventToAzInputMapperFixture, MouseWheel_AzHandlerCaptured_ReceivedZeroSignalAndThreeAzChannelEvents)
  198. {
  199. // setup
  200. const AzFramework::InputChannelId mouseWheelId = AzFramework::InputDeviceMouse::Movement::Z;
  201. const char* mouseWheelChannelName = mouseWheelId.GetName();
  202. AzFramework::InputChannelNotificationBus::Handler::BusConnect();
  203. m_captureAzEvents = true;
  204. const QPoint mouseEventPos = QPoint(WidgetSize.width() / 2, WidgetSize.height() / 2);
  205. const QPoint scrollDelta = QPoint(10, 10);
  206. MouseScroll(m_rootWidget.get(), mouseEventPos, scrollDelta);
  207. // qt validation
  208. EXPECT_EQ(m_signalEvents.size(), 0);
  209. // az validation
  210. ASSERT_EQ(m_azChannelEvents.size(), 3);
  211. EXPECT_STREQ(m_azChannelEvents[0].m_inputChannelId.GetName(), mouseWheelChannelName);
  212. EXPECT_STREQ(m_azChannelEvents[1].m_inputChannelId.GetName(), mouseWheelChannelName);
  213. EXPECT_STREQ(m_azChannelEvents[2].m_inputChannelId.GetName(), mouseWheelChannelName);
  214. // cleanup
  215. AzFramework::InputChannelNotificationBus::Handler::BusDisconnect();
  216. }
  217. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  218. struct MouseButtonIdsParam
  219. {
  220. Qt::MouseButton m_qt;
  221. AzFramework::InputChannelId m_az;
  222. };
  223. class MouseButtonParamQtEventToAzInputMapperFixture
  224. : public QtEventToAzInputMapperFixture
  225. , public ::testing::WithParamInterface<MouseButtonIdsParam>
  226. {
  227. };
  228. // Qt event forwarding through the internal signal handler test
  229. TEST_P(MouseButtonParamQtEventToAzInputMapperFixture, MouseClick_NoAzHandlers_ReceivedTwoSignalAndZeroAzChannelEvents)
  230. {
  231. // setup
  232. const MouseButtonIdsParam mouseButtonIds = GetParam();
  233. const QPoint mouseEventPos = QPoint(WidgetSize.width() / 2, WidgetSize.height() / 2);
  234. QTest::mouseClick(m_rootWidget.get(), mouseButtonIds.m_qt, Qt::NoModifier, mouseEventPos);
  235. // qt validation
  236. ASSERT_EQ(m_signalEvents.size(), 2);
  237. EXPECT_EQ(m_signalEvents[0].m_eventType, QEvent::Type::MouseButtonPress);
  238. EXPECT_EQ(m_signalEvents[0].m_button, mouseButtonIds.m_qt);
  239. EXPECT_EQ(m_signalEvents[1].m_eventType, QEvent::Type::MouseButtonRelease);
  240. EXPECT_EQ(m_signalEvents[1].m_button, mouseButtonIds.m_qt);
  241. // az validation
  242. EXPECT_EQ(m_azChannelEvents.size(), 0);
  243. }
  244. // Qt event to AzInput event conversion test
  245. TEST_P(MouseButtonParamQtEventToAzInputMapperFixture, MouseClick_AzHandlerNotCaptured_ReceivedTwoSignalAndTwoAzChannelEvents)
  246. {
  247. // setup
  248. const MouseButtonIdsParam mouseButtonIds = GetParam();
  249. AzFramework::InputChannelNotificationBus::Handler::BusConnect();
  250. m_captureAzEvents = false;
  251. const QPoint mouseEventPos = QPoint(WidgetSize.width() / 2, WidgetSize.height() / 2);
  252. QTest::mouseClick(m_rootWidget.get(), mouseButtonIds.m_qt, Qt::NoModifier, mouseEventPos);
  253. // qt validation
  254. ASSERT_EQ(m_signalEvents.size(), 2);
  255. EXPECT_EQ(m_signalEvents[0].m_eventType, QEvent::Type::MouseButtonPress);
  256. EXPECT_EQ(m_signalEvents[0].m_button, mouseButtonIds.m_qt);
  257. EXPECT_EQ(m_signalEvents[1].m_eventType, QEvent::Type::MouseButtonRelease);
  258. EXPECT_EQ(m_signalEvents[1].m_button, mouseButtonIds.m_qt);
  259. // az validation
  260. ASSERT_EQ(m_azChannelEvents.size(), 2);
  261. EXPECT_STREQ(m_azChannelEvents[0].m_inputChannelId.GetName(), mouseButtonIds.m_az.GetName());
  262. EXPECT_TRUE(m_azChannelEvents[0].m_isActive);
  263. EXPECT_STREQ(m_azChannelEvents[1].m_inputChannelId.GetName(), mouseButtonIds.m_az.GetName());
  264. EXPECT_FALSE(m_azChannelEvents[1].m_isActive);
  265. // cleanup
  266. AzFramework::InputChannelNotificationBus::Handler::BusDisconnect();
  267. }
  268. // AzInput event handler consumption test
  269. TEST_P(MouseButtonParamQtEventToAzInputMapperFixture, MouseClick_AzHandlerCaptured_ReceivedZeroSignalAndTwoAzChannelEvents)
  270. {
  271. // setup
  272. const MouseButtonIdsParam mouseButtonIds = GetParam();
  273. AzFramework::InputChannelNotificationBus::Handler::BusConnect();
  274. m_captureAzEvents = true;
  275. const QPoint mouseEventPos = QPoint(WidgetSize.width() / 2, WidgetSize.height() / 2);
  276. QTest::mouseClick(m_rootWidget.get(), mouseButtonIds.m_qt, Qt::NoModifier, mouseEventPos);
  277. // qt validation
  278. EXPECT_EQ(m_signalEvents.size(), 0);
  279. // az validation
  280. ASSERT_EQ(m_azChannelEvents.size(), 2);
  281. EXPECT_STREQ(m_azChannelEvents[0].m_inputChannelId.GetName(), mouseButtonIds.m_az.GetName());
  282. EXPECT_TRUE(m_azChannelEvents[0].m_isActive);
  283. EXPECT_STREQ(m_azChannelEvents[1].m_inputChannelId.GetName(), mouseButtonIds.m_az.GetName());
  284. EXPECT_FALSE(m_azChannelEvents[1].m_isActive);
  285. // cleanup
  286. AzFramework::InputChannelNotificationBus::Handler::BusDisconnect();
  287. }
  288. INSTANTIATE_TEST_SUITE_P(All, MouseButtonParamQtEventToAzInputMapperFixture,
  289. testing::Values(
  290. MouseButtonIdsParam{ Qt::MouseButton::LeftButton, AzFramework::InputDeviceMouse::Button::Left },
  291. MouseButtonIdsParam{ Qt::MouseButton::RightButton, AzFramework::InputDeviceMouse::Button::Right },
  292. MouseButtonIdsParam{ Qt::MouseButton::MiddleButton, AzFramework::InputDeviceMouse::Button::Middle }
  293. ),
  294. [](const ::testing::TestParamInfo<MouseButtonIdsParam>& info)
  295. {
  296. return info.param.m_az.GetName();
  297. }
  298. );
  299. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  300. struct KeyEventIdsParam
  301. {
  302. Qt::Key m_qt;
  303. AzFramework::InputChannelId m_az;
  304. };
  305. class PrintableKeyEventParamQtEventToAzInputMapperFixture
  306. : public QtEventToAzInputMapperFixture
  307. , public ::testing::WithParamInterface<KeyEventIdsParam>
  308. {
  309. };
  310. // Qt event forwarding through the internal signal handler test
  311. TEST_P(PrintableKeyEventParamQtEventToAzInputMapperFixture, KeyClick_NoAzHandlers_ReceivedTwoSignalAndZeroAzEvents)
  312. {
  313. // setup
  314. const KeyEventIdsParam keyEventIds = GetParam();
  315. const Qt::KeyboardModifiers modifiers = Qt::NoModifier;
  316. QTest::keyClick(m_rootWidget.get(), keyEventIds.m_qt, modifiers);
  317. // qt validation
  318. ASSERT_EQ(m_signalEvents.size(), 2);
  319. EXPECT_EQ(m_signalEvents[0].m_eventType, QEvent::Type::KeyPress);
  320. EXPECT_EQ(m_signalEvents[0].m_key, keyEventIds.m_qt);
  321. EXPECT_EQ(m_signalEvents[1].m_eventType, QEvent::Type::KeyRelease);
  322. EXPECT_EQ(m_signalEvents[1].m_key, keyEventIds.m_qt);
  323. // az validation
  324. EXPECT_EQ(m_azChannelEvents.size(), 0);
  325. EXPECT_EQ(m_azTextEvents.size(), 0);
  326. }
  327. // Qt event to AzInput event conversion test
  328. TEST_P(PrintableKeyEventParamQtEventToAzInputMapperFixture, KeyClick_AzHandlersNotCaptured_ReceivedTwoSignalAndThreeAzEvents)
  329. {
  330. // setup
  331. const KeyEventIdsParam keyEventIds = GetParam();
  332. const Qt::KeyboardModifiers modifiers = Qt::NoModifier;
  333. AZStd::string keyAsText = QtKeyToAzString(keyEventIds.m_qt, modifiers);
  334. AzFramework::InputChannelNotificationBus::Handler::BusConnect();
  335. m_captureAzEvents = false;
  336. AzFramework::InputTextNotificationBus::Handler::BusConnect();
  337. m_captureAzEvents = false;
  338. QTest::keyClick(m_rootWidget.get(), keyEventIds.m_qt, modifiers);
  339. // qt validation
  340. ASSERT_EQ(m_signalEvents.size(), 2);
  341. EXPECT_EQ(m_signalEvents[0].m_eventType, QEvent::Type::KeyPress);
  342. EXPECT_EQ(m_signalEvents[0].m_key, keyEventIds.m_qt);
  343. EXPECT_EQ(m_signalEvents[1].m_eventType, QEvent::Type::KeyRelease);
  344. EXPECT_EQ(m_signalEvents[1].m_key, keyEventIds.m_qt);
  345. // az validation
  346. ASSERT_EQ(m_azTextEvents.size(), 1);
  347. EXPECT_STREQ(m_azTextEvents[0].c_str(), keyAsText.c_str());
  348. ASSERT_EQ(m_azChannelEvents.size(), 2);
  349. EXPECT_STREQ(m_azChannelEvents[0].m_inputChannelId.GetName(), keyEventIds.m_az.GetName());
  350. EXPECT_TRUE(m_azChannelEvents[0].m_isActive);
  351. EXPECT_STREQ(m_azChannelEvents[1].m_inputChannelId.GetName(), keyEventIds.m_az.GetName());
  352. EXPECT_FALSE(m_azChannelEvents[1].m_isActive);
  353. // cleanup
  354. AzFramework::InputTextNotificationBus::Handler::BusDisconnect();
  355. AzFramework::InputChannelNotificationBus::Handler::BusDisconnect();
  356. }
  357. INSTANTIATE_TEST_SUITE_P(All, PrintableKeyEventParamQtEventToAzInputMapperFixture,
  358. testing::Values(
  359. KeyEventIdsParam{ Qt::Key_0, AzFramework::InputDeviceKeyboard::Key::Alphanumeric0 },
  360. KeyEventIdsParam{ Qt::Key_1, AzFramework::InputDeviceKeyboard::Key::Alphanumeric1 },
  361. KeyEventIdsParam{ Qt::Key_2, AzFramework::InputDeviceKeyboard::Key::Alphanumeric2 },
  362. KeyEventIdsParam{ Qt::Key_3, AzFramework::InputDeviceKeyboard::Key::Alphanumeric3 },
  363. KeyEventIdsParam{ Qt::Key_4, AzFramework::InputDeviceKeyboard::Key::Alphanumeric4 },
  364. KeyEventIdsParam{ Qt::Key_5, AzFramework::InputDeviceKeyboard::Key::Alphanumeric5 },
  365. KeyEventIdsParam{ Qt::Key_6, AzFramework::InputDeviceKeyboard::Key::Alphanumeric6 },
  366. KeyEventIdsParam{ Qt::Key_7, AzFramework::InputDeviceKeyboard::Key::Alphanumeric7 },
  367. KeyEventIdsParam{ Qt::Key_8, AzFramework::InputDeviceKeyboard::Key::Alphanumeric8 },
  368. KeyEventIdsParam{ Qt::Key_9, AzFramework::InputDeviceKeyboard::Key::Alphanumeric9 },
  369. KeyEventIdsParam{ Qt::Key_A, AzFramework::InputDeviceKeyboard::Key::AlphanumericA },
  370. KeyEventIdsParam{ Qt::Key_B, AzFramework::InputDeviceKeyboard::Key::AlphanumericB },
  371. KeyEventIdsParam{ Qt::Key_C, AzFramework::InputDeviceKeyboard::Key::AlphanumericC },
  372. KeyEventIdsParam{ Qt::Key_D, AzFramework::InputDeviceKeyboard::Key::AlphanumericD },
  373. KeyEventIdsParam{ Qt::Key_E, AzFramework::InputDeviceKeyboard::Key::AlphanumericE },
  374. KeyEventIdsParam{ Qt::Key_F, AzFramework::InputDeviceKeyboard::Key::AlphanumericF },
  375. KeyEventIdsParam{ Qt::Key_G, AzFramework::InputDeviceKeyboard::Key::AlphanumericG },
  376. KeyEventIdsParam{ Qt::Key_H, AzFramework::InputDeviceKeyboard::Key::AlphanumericH },
  377. KeyEventIdsParam{ Qt::Key_I, AzFramework::InputDeviceKeyboard::Key::AlphanumericI },
  378. KeyEventIdsParam{ Qt::Key_J, AzFramework::InputDeviceKeyboard::Key::AlphanumericJ },
  379. KeyEventIdsParam{ Qt::Key_K, AzFramework::InputDeviceKeyboard::Key::AlphanumericK },
  380. KeyEventIdsParam{ Qt::Key_L, AzFramework::InputDeviceKeyboard::Key::AlphanumericL },
  381. KeyEventIdsParam{ Qt::Key_M, AzFramework::InputDeviceKeyboard::Key::AlphanumericM },
  382. KeyEventIdsParam{ Qt::Key_N, AzFramework::InputDeviceKeyboard::Key::AlphanumericN },
  383. KeyEventIdsParam{ Qt::Key_O, AzFramework::InputDeviceKeyboard::Key::AlphanumericO },
  384. KeyEventIdsParam{ Qt::Key_P, AzFramework::InputDeviceKeyboard::Key::AlphanumericP },
  385. KeyEventIdsParam{ Qt::Key_Q, AzFramework::InputDeviceKeyboard::Key::AlphanumericQ },
  386. KeyEventIdsParam{ Qt::Key_R, AzFramework::InputDeviceKeyboard::Key::AlphanumericR },
  387. KeyEventIdsParam{ Qt::Key_S, AzFramework::InputDeviceKeyboard::Key::AlphanumericS },
  388. KeyEventIdsParam{ Qt::Key_T, AzFramework::InputDeviceKeyboard::Key::AlphanumericT },
  389. KeyEventIdsParam{ Qt::Key_U, AzFramework::InputDeviceKeyboard::Key::AlphanumericU },
  390. KeyEventIdsParam{ Qt::Key_V, AzFramework::InputDeviceKeyboard::Key::AlphanumericV },
  391. KeyEventIdsParam{ Qt::Key_W, AzFramework::InputDeviceKeyboard::Key::AlphanumericW },
  392. KeyEventIdsParam{ Qt::Key_X, AzFramework::InputDeviceKeyboard::Key::AlphanumericX },
  393. KeyEventIdsParam{ Qt::Key_Y, AzFramework::InputDeviceKeyboard::Key::AlphanumericY },
  394. KeyEventIdsParam{ Qt::Key_Z, AzFramework::InputDeviceKeyboard::Key::AlphanumericZ },
  395. // these may need to be special cased due to the printable text conversion
  396. //KeyEventIdsParam{ Qt::Key_Space, AzFramework::InputDeviceKeyboard::Key::EditSpace },
  397. //KeyEventIdsParam{ Qt::Key_Tab, AzFramework::InputDeviceKeyboard::Key::EditTab },
  398. KeyEventIdsParam{ Qt::Key_Apostrophe, AzFramework::InputDeviceKeyboard::Key::PunctuationApostrophe },
  399. KeyEventIdsParam{ Qt::Key_Backslash, AzFramework::InputDeviceKeyboard::Key::PunctuationBackslash },
  400. KeyEventIdsParam{ Qt::Key_BracketLeft, AzFramework::InputDeviceKeyboard::Key::PunctuationBracketL },
  401. KeyEventIdsParam{ Qt::Key_BracketRight, AzFramework::InputDeviceKeyboard::Key::PunctuationBracketR },
  402. KeyEventIdsParam{ Qt::Key_Comma, AzFramework::InputDeviceKeyboard::Key::PunctuationComma },
  403. KeyEventIdsParam{ Qt::Key_Equal, AzFramework::InputDeviceKeyboard::Key::PunctuationEquals },
  404. KeyEventIdsParam{ Qt::Key_hyphen, AzFramework::InputDeviceKeyboard::Key::PunctuationHyphen },
  405. KeyEventIdsParam{ Qt::Key_Period, AzFramework::InputDeviceKeyboard::Key::PunctuationPeriod },
  406. KeyEventIdsParam{ Qt::Key_Semicolon, AzFramework::InputDeviceKeyboard::Key::PunctuationSemicolon },
  407. KeyEventIdsParam{ Qt::Key_Slash, AzFramework::InputDeviceKeyboard::Key::PunctuationSlash },
  408. KeyEventIdsParam{ Qt::Key_QuoteLeft, AzFramework::InputDeviceKeyboard::Key::PunctuationTilde }
  409. ),
  410. [](const ::testing::TestParamInfo<KeyEventIdsParam>& info)
  411. {
  412. return info.param.m_az.GetName();
  413. }
  414. );
  415. // Note that this class is identical to the previous fixture class.
  416. // This is intentional - the test framework appears to gather all TEST_P bodies and execute them for all
  417. // fixtures that use the same class, even if they have different names in INSTANTIATE_TEST_SUITE_P.
  418. // By making a different class here, we can separate them so the above INSTANTIATE_TEST_SUITE_P params
  419. // do not get fed into the below TEST_P. (This happens even if you give the INSTANTIATE_TEST_SUITE_P calls different names.)
  420. class ModifierKeyEventFixture
  421. : public QtEventToAzInputMapperFixture
  422. , public ::testing::WithParamInterface<KeyEventIdsParam>
  423. {
  424. };
  425. // This test makes sure that the keyboard device releases the modifier keys when the application is deactivated.
  426. // It tests a regression where the modifier keys would stick if the application was deactivated while they were held,
  427. // for example, ALT-TAB would cause the ALT key to stick.
  428. // It tests to make sure that the actual keyboard device input channel custom data containing the modifier keys
  429. // has released the modifier key in its custom data, since the application checks that custom data to see if the
  430. // modifiers are present.
  431. TEST_P(ModifierKeyEventFixture, ModifierKey_During_ApplicationStateChange_Causes_ModifierKeys_Reset)
  432. {
  433. // setup phase
  434. const KeyEventIdsParam keyEventIds = GetParam();
  435. const Qt::Key whichKey = keyEventIds.m_qt;
  436. Qt::KeyboardModifiers modifiers = {};
  437. AzFramework::ModifierKeyMask expectedMask = {};
  438. switch (whichKey)
  439. {
  440. case Qt::Key_Alt: expectedMask = AzFramework::ModifierKeyMask::AltAny; modifiers = Qt::KeyboardModifier::AltModifier; break;
  441. case Qt::Key_Control: expectedMask = AzFramework::ModifierKeyMask::CtrlAny; modifiers = Qt::KeyboardModifier::ControlModifier; break;
  442. case Qt::Key_Super_L: expectedMask = AzFramework::ModifierKeyMask::SuperAny; modifiers = Qt::KeyboardModifier::MetaModifier; break;
  443. case Qt::Key_Super_R: expectedMask = AzFramework::ModifierKeyMask::SuperAny; modifiers = Qt::KeyboardModifier::MetaModifier; break;
  444. case Qt::Key_Shift: expectedMask = AzFramework::ModifierKeyMask::ShiftAny; modifiers = Qt::KeyboardModifier::ShiftModifier; break;
  445. };
  446. AzFramework::InputChannelNotificationBus::Handler::BusConnect();
  447. AzFramework::InputTextNotificationBus::Handler::BusConnect();
  448. // This is still the setup phase - we want to get into a state where the application
  449. // believes that the modifier key is pressed as the starting point.
  450. QTest::keyPress(m_rootWidget.get(), whichKey, modifiers);
  451. // note that because Qt sends (up to) 4 events whenever it gets a single modifier keypress, we can expect
  452. // this to have at least 1 but probably 4 events - for example, it sends out a ShortCut event in addition to the
  453. // usual key press event because modifiers like ALT can trigger shorcuts. Some modifiers will only output 1 event
  454. // so here we only check that there is at least 1, and then work with that one.
  455. ASSERT_GT(m_azChannelEvents.size(), 0);
  456. EXPECT_TRUE(m_azChannelEvents[0].m_isActive); // it should consider it as being active now since it was pressed
  457. EXPECT_STREQ(m_azChannelEvents[0].m_inputChannelId.GetName(), keyEventIds.m_az.GetName()); // it should be the expected key
  458. // get the input channel directly, so that its extra class-specific data can be captured, in this case, we expect
  459. // keyboard events to have a modifier key state special data object attached:
  460. AzFramework::InputDeviceId deviceId = AzToolsFramework::GetSyntheticKeyboardDeviceId(QtEventToAzInputMapperFixture::TestDeviceIdSeed);
  461. const AzFramework::InputChannel* inputChannel = nullptr;
  462. AzFramework::InputChannelRequestBus::EventResult(inputChannel, {m_azChannelEvents[0].m_inputChannelId, deviceId.GetIndex()}, &AzFramework::InputChannelRequests::GetInputChannel);
  463. ASSERT_NE(inputChannel, nullptr);
  464. // At this point, we can expect the mask to be set to the modifier key actually pressed pressed.
  465. // This would indicate the device has consumed the event and now thinks that the modifier key is held.
  466. const AzFramework::ModifierKeyStates* modifierKeyStatesBefore = inputChannel->GetCustomData<AzFramework::ModifierKeyStates>();
  467. ASSERT_NE(modifierKeyStatesBefore, nullptr);
  468. // we are about to do bitwise masking operations on this, so it must be static cast like it is elsewhere in the code.
  469. int activeModifierKeys = static_cast<int>(modifierKeyStatesBefore->GetActiveModifierKeys());
  470. EXPECT_NE(activeModifierKeys & static_cast<int>(expectedMask), 0);
  471. // Testing phase - trigger the event we are interested in seeing the outcome for.
  472. // Tell the application that it has gone inactive. It should respond by resetting any modifier keys,
  473. // even though no keypress events have occurred since.
  474. QApplicationStateChangeEvent event(Qt::ApplicationState::ApplicationInactive);
  475. QCoreApplication::sendEvent(m_rootWidget.get(), &event);
  476. // Get the active modifier keys (if any) of the input event. Will only exist for keyboard keys.
  477. const AzFramework::ModifierKeyStates* modifierKeyStatesAfter = inputChannel->GetCustomData<AzFramework::ModifierKeyStates>();
  478. ASSERT_NE(modifierKeyStatesAfter, nullptr);
  479. EXPECT_EQ(modifierKeyStatesAfter->GetActiveModifierKeys(), AzFramework::ModifierKeyMask::None);
  480. // cleanup
  481. AzFramework::InputTextNotificationBus::Handler::BusDisconnect();
  482. AzFramework::InputChannelNotificationBus::Handler::BusDisconnect();
  483. }
  484. // Test case exercises each modifier key and makes sure it doesn't "stick" when the application
  485. // is deactivated.
  486. INSTANTIATE_TEST_SUITE_P(All, ModifierKeyEventFixture,
  487. testing::Values(
  488. KeyEventIdsParam{ Qt::Key_Alt, AzFramework::InputDeviceKeyboard::Key::ModifierAltL },
  489. KeyEventIdsParam{ Qt::Key_Shift, AzFramework::InputDeviceKeyboard::Key::ModifierShiftL },
  490. KeyEventIdsParam{ Qt::Key_Control, AzFramework::InputDeviceKeyboard::Key::ModifierCtrlL },
  491. KeyEventIdsParam{ Qt::Key_Super_L, AzFramework::InputDeviceKeyboard::Key::ModifierSuperL },
  492. KeyEventIdsParam{ Qt::Key_Super_R, AzFramework::InputDeviceKeyboard::Key::ModifierSuperR }
  493. ),
  494. [](const ::testing::TestParamInfo<KeyEventIdsParam>& info)
  495. {
  496. return info.param.m_az.GetName();
  497. }
  498. );
  499. struct MouseMoveParam
  500. {
  501. AzToolsFramework::CursorInputMode mode;
  502. int iterations;
  503. QPoint startPos;
  504. QPoint deltaPos;
  505. QPoint expectedPos;
  506. const char* name;
  507. };
  508. class MoveMoveWrapParamQtEventToAzInputMapperFixture
  509. : public QtEventToAzInputMapperFixture
  510. , public ::testing::WithParamInterface<MouseMoveParam>
  511. {
  512. };
  513. TEST_P(MoveMoveWrapParamQtEventToAzInputMapperFixture, DISABLED_MouseMove_NoAzHandlers_VerifyMouseMovementViewport)
  514. {
  515. // setup
  516. const MouseMoveParam mouseMoveParam = GetParam();
  517. AzFramework::InputChannelNotificationBus::Handler::BusConnect();
  518. m_captureAzEvents = true;
  519. m_rootWidget->move(100, 100);
  520. QScreen* screen = m_rootWidget->screen();
  521. MouseMove(m_rootWidget.get(), mouseMoveParam.startPos, QPoint(0, 0));
  522. // given
  523. m_inputChannelMapper->SetCursorMode(mouseMoveParam.mode);
  524. m_azCursorPositions.clear();
  525. for (float i = 0; i < mouseMoveParam.iterations; i++)
  526. {
  527. MouseMove(m_rootWidget.get(), m_rootWidget->mapFromGlobal(QCursor::pos(screen)), (mouseMoveParam.deltaPos / mouseMoveParam.iterations));
  528. }
  529. AZ::Vector2 accumulatedPosition(0.0f,0.0f);
  530. for(const auto& pos: m_azCursorPositions) {
  531. accumulatedPosition += (pos.m_normalizedPositionDelta * AZ::Vector2(aznumeric_cast<float>(WidgetSize.width()), aznumeric_cast<float>(WidgetSize.height())));
  532. }
  533. // validate
  534. const QPoint endPosition = m_rootWidget->mapFromGlobal(QCursor::pos(screen));
  535. EXPECT_NEAR(endPosition.x(), mouseMoveParam.expectedPos.x(), 1.0f);
  536. EXPECT_NEAR(endPosition.y(), mouseMoveParam.expectedPos.y(), 1.0f);
  537. EXPECT_NEAR(accumulatedPosition.GetX(), mouseMoveParam.deltaPos.x(), 1.0f);
  538. EXPECT_NEAR(accumulatedPosition.GetY(), mouseMoveParam.deltaPos.y(), 1.0f);
  539. // cleanup
  540. m_rootWidget->move(0, 0);
  541. m_inputChannelMapper->SetCursorMode(AzToolsFramework::CursorInputMode::CursorModeNone);
  542. AzFramework::InputChannelNotificationBus::Handler::BusDisconnect();
  543. }
  544. INSTANTIATE_TEST_SUITE_P(All, MoveMoveWrapParamQtEventToAzInputMapperFixture,
  545. testing::Values(
  546. // verify CursorModeWrappedX wrapping
  547. MouseMoveParam {AzToolsFramework::CursorInputMode::CursorModeWrappedX,
  548. 40,
  549. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() - 20, QtEventToAzInputMapperFixture::WidgetSize.height() / 2),
  550. QPoint(40, 0),
  551. QPoint(20, QtEventToAzInputMapperFixture::WidgetSize.height() / 2),
  552. "CursorModeWrappedX_Test_Right"
  553. },
  554. MouseMoveParam {AzToolsFramework::CursorInputMode::CursorModeWrappedX,
  555. 40,
  556. QPoint(20, QtEventToAzInputMapperFixture::WidgetSize.height() / 2),
  557. QPoint(-40, 0),
  558. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() - 20, QtEventToAzInputMapperFixture::WidgetSize.height()/2),
  559. "CursorModeWrappedX_Test_Left"
  560. },
  561. MouseMoveParam {AzToolsFramework::CursorInputMode::CursorModeWrappedX,
  562. 40,
  563. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() / 2, 20),
  564. QPoint(0, -40),
  565. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() / 2, -20),
  566. "CursorModeWrappedX_Test_Top"
  567. },
  568. MouseMoveParam {AzToolsFramework::CursorInputMode::CursorModeWrappedX,
  569. 40,
  570. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() / 2, QtEventToAzInputMapperFixture::WidgetSize.height() - 20),
  571. QPoint(0, 40),
  572. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() / 2, QtEventToAzInputMapperFixture::WidgetSize.height() + 20),
  573. "CursorModeWrappedX_Test_Bottom"
  574. },
  575. // verify CursorModeWrappedY wrapping
  576. MouseMoveParam {AzToolsFramework::CursorInputMode::CursorModeWrappedY,
  577. 40,
  578. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() - 20, QtEventToAzInputMapperFixture::WidgetSize.height()/2),
  579. QPoint(40, 0),
  580. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() + 20, QtEventToAzInputMapperFixture::WidgetSize.height()/2),
  581. "CursorModeWrappedY_Test_Right"
  582. },
  583. MouseMoveParam {AzToolsFramework::CursorInputMode::CursorModeWrappedY,
  584. 40,
  585. QPoint(20, QtEventToAzInputMapperFixture::WidgetSize.height() / 2),
  586. QPoint(-40, 0),
  587. QPoint(-20, QtEventToAzInputMapperFixture::WidgetSize.height() / 2),
  588. "CursorModeWrappedY_Test_Left"
  589. },
  590. MouseMoveParam {AzToolsFramework::CursorInputMode::CursorModeWrappedY,
  591. 40,
  592. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() / 2, 20),
  593. QPoint(0, -40),
  594. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() / 2, QtEventToAzInputMapperFixture::WidgetSize.height() - 20),
  595. "CursorModeWrappedY_Test_Top"
  596. },
  597. MouseMoveParam {AzToolsFramework::CursorInputMode::CursorModeWrappedY,
  598. 40,
  599. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() / 2, QtEventToAzInputMapperFixture::WidgetSize.height() - 20),
  600. QPoint(0, 40),
  601. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() / 2, 20),
  602. "CursorModeWrappedY_Test_Bottom"
  603. },
  604. // verify CursorModeWrapped wrapping
  605. MouseMoveParam {AzToolsFramework::CursorInputMode::CursorModeWrapped,
  606. 40,
  607. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() - 20, QtEventToAzInputMapperFixture::WidgetSize.height()/2),
  608. QPoint(40, 0),
  609. QPoint(20, QtEventToAzInputMapperFixture::WidgetSize.height()/2),
  610. "CursorModeWrapped_Test_Right"
  611. },
  612. MouseMoveParam {AzToolsFramework::CursorInputMode::CursorModeWrapped,
  613. 40,
  614. QPoint(20, QtEventToAzInputMapperFixture::WidgetSize.height() / 2),
  615. QPoint(-40, 0),
  616. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() - 20, QtEventToAzInputMapperFixture::WidgetSize.height()/2),
  617. "CursorModeWrapped_Test_Left"
  618. },
  619. MouseMoveParam {AzToolsFramework::CursorInputMode::CursorModeWrapped,
  620. 40,
  621. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() / 2, 20),
  622. QPoint(0, -40),
  623. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() / 2, QtEventToAzInputMapperFixture::WidgetSize.height() - 20),
  624. "CursorModeWrapped_Test_Top"
  625. },
  626. MouseMoveParam {AzToolsFramework::CursorInputMode::CursorModeWrapped,
  627. 40,
  628. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() / 2, QtEventToAzInputMapperFixture::WidgetSize.height() - 20),
  629. QPoint(0, 40),
  630. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() / 2, 20),
  631. "CursorModeWrapped_Test_Bottom"
  632. },
  633. // verify CursorModeCaptured
  634. MouseMoveParam {AzToolsFramework::CursorInputMode::CursorModeCaptured,
  635. 40,
  636. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() / 2, QtEventToAzInputMapperFixture::WidgetSize.height() / 2),
  637. QPoint(0, 40),
  638. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() / 2, QtEventToAzInputMapperFixture::WidgetSize.height() / 2),
  639. "CursorModeCaptured"
  640. },
  641. // verify CursorModeNone
  642. MouseMoveParam {AzToolsFramework::CursorInputMode::CursorModeNone,
  643. 40,
  644. QPoint(QtEventToAzInputMapperFixture::WidgetSize.width() / 2, QtEventToAzInputMapperFixture::WidgetSize.height() / 2),
  645. QPoint(40, 0),
  646. QPoint((QtEventToAzInputMapperFixture::WidgetSize.width() / 2) + 40, (QtEventToAzInputMapperFixture::WidgetSize.height() / 2)),
  647. "CursorModeNone"
  648. }
  649. ),
  650. [](const ::testing::TestParamInfo<MouseMoveParam>& info)
  651. {
  652. return info.param.name;
  653. }
  654. );
  655. } // namespace UnitTest