qtestkeyboard.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the QtTest module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #ifndef QTESTKEYBOARD_H
  40. #define QTESTKEYBOARD_H
  41. #if 0
  42. // inform syncqt
  43. #pragma qt_no_master_include
  44. #endif
  45. #include <QtTest/qtestassert.h>
  46. #include <QtTest/qtest_global.h>
  47. #include <QtTest/qtestsystem.h>
  48. #include <QtTest/qtestspontaneevent.h>
  49. #include <QtCore/qpointer.h>
  50. #include <QtGui/qguiapplication.h>
  51. #include <QtGui/qwindow.h>
  52. #include <QtGui/qevent.h>
  53. #ifdef QT_WIDGETS_LIB
  54. #include <QtWidgets/qwidget.h>
  55. #include <QtWidgets/qapplication.h>
  56. #endif
  57. QT_BEGIN_NAMESPACE
  58. Q_GUI_EXPORT void qt_handleKeyEvent(QWindow *w, QEvent::Type t, int k, Qt::KeyboardModifiers mods, const QString & text = QString(), bool autorep = false, ushort count = 1);
  59. Q_GUI_EXPORT bool qt_sendShortcutOverrideEvent(QObject *o, ulong timestamp, int k, Qt::KeyboardModifiers mods, const QString &text = QString(), bool autorep = false, ushort count = 1);
  60. namespace QTest
  61. {
  62. enum KeyAction { Press, Release, Click, Shortcut };
  63. static void simulateEvent(QWindow *window, bool press, int code,
  64. Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1)
  65. {
  66. QEvent::Type type;
  67. type = press ? QEvent::KeyPress : QEvent::KeyRelease;
  68. qt_handleKeyEvent(window, type, code, modifier, text, repeat, delay);
  69. qApp->processEvents();
  70. }
  71. static void sendKeyEvent(KeyAction action, QWindow *window, Qt::Key code,
  72. QString text, Qt::KeyboardModifiers modifier, int delay=-1)
  73. {
  74. QTEST_ASSERT(qApp);
  75. if (!window)
  76. window = QGuiApplication::focusWindow();
  77. QTEST_ASSERT(window);
  78. if (action == Click) {
  79. sendKeyEvent(Press, window, code, text, modifier, delay);
  80. sendKeyEvent(Release, window, code, text, modifier, delay);
  81. return;
  82. }
  83. bool repeat = false;
  84. if (action == Shortcut) {
  85. int timestamp = 0;
  86. qt_sendShortcutOverrideEvent(window, timestamp, code, modifier, text, repeat);
  87. return;
  88. }
  89. if (action == Press) {
  90. if (modifier & Qt::ShiftModifier)
  91. simulateEvent(window, true, Qt::Key_Shift, Qt::KeyboardModifiers(), QString(), false, delay);
  92. if (modifier & Qt::ControlModifier)
  93. simulateEvent(window, true, Qt::Key_Control, modifier & Qt::ShiftModifier, QString(), false, delay);
  94. if (modifier & Qt::AltModifier)
  95. simulateEvent(window, true, Qt::Key_Alt,
  96. modifier & (Qt::ShiftModifier | Qt::ControlModifier), QString(), false, delay);
  97. if (modifier & Qt::MetaModifier)
  98. simulateEvent(window, true, Qt::Key_Meta, modifier & (Qt::ShiftModifier
  99. | Qt::ControlModifier | Qt::AltModifier), QString(), false, delay);
  100. simulateEvent(window, true, code, modifier, text, repeat, delay);
  101. } else if (action == Release) {
  102. simulateEvent(window, false, code, modifier, text, repeat, delay);
  103. if (modifier & Qt::MetaModifier)
  104. simulateEvent(window, false, Qt::Key_Meta, modifier, QString(), false, delay);
  105. if (modifier & Qt::AltModifier)
  106. simulateEvent(window, false, Qt::Key_Alt, modifier &
  107. (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier), QString(), false, delay);
  108. if (modifier & Qt::ControlModifier)
  109. simulateEvent(window, false, Qt::Key_Control,
  110. modifier & (Qt::ShiftModifier | Qt::ControlModifier), QString(), false, delay);
  111. if (modifier & Qt::ShiftModifier)
  112. simulateEvent(window, false, Qt::Key_Shift, modifier & Qt::ShiftModifier, QString(), false, delay);
  113. }
  114. }
  115. // Convenience function
  116. static void sendKeyEvent(KeyAction action, QWindow *window, Qt::Key code,
  117. char ascii, Qt::KeyboardModifiers modifier, int delay=-1)
  118. {
  119. QString text;
  120. if (ascii)
  121. text = QString(QChar::fromLatin1(ascii));
  122. sendKeyEvent(action, window, code, text, modifier, delay);
  123. }
  124. inline static void keyEvent(KeyAction action, QWindow *window, char ascii,
  125. Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  126. { sendKeyEvent(action, window, asciiToKey(ascii), ascii, modifier, delay); }
  127. inline static void keyEvent(KeyAction action, QWindow *window, Qt::Key key,
  128. Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  129. { sendKeyEvent(action, window, key, keyToAscii(key), modifier, delay); }
  130. Q_DECL_UNUSED inline static void keyClick(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  131. { keyEvent(Click, window, key, modifier, delay); }
  132. Q_DECL_UNUSED inline static void keyClick(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  133. { keyEvent(Click, window, key, modifier, delay); }
  134. Q_DECL_UNUSED inline static void keyRelease(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  135. { keyEvent(Release, window, key, modifier, delay); }
  136. Q_DECL_UNUSED inline static void keyRelease(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  137. { keyEvent(Release, window, key, modifier, delay); }
  138. Q_DECL_UNUSED inline static void keyPress(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  139. { keyEvent(Press, window, key, modifier, delay); }
  140. Q_DECL_UNUSED inline static void keyPress(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  141. { keyEvent(Press, window, key, modifier, delay); }
  142. #ifdef QT_WIDGETS_LIB
  143. static void simulateEvent(QWidget *widget, bool press, int code,
  144. Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1)
  145. {
  146. QTEST_ASSERT(widget);
  147. extern int Q_TESTLIB_EXPORT defaultKeyDelay();
  148. if (delay == -1 || delay < defaultKeyDelay())
  149. delay = defaultKeyDelay();
  150. if (delay > 0)
  151. QTest::qWait(delay);
  152. QKeyEvent a(press ? QEvent::KeyPress : QEvent::KeyRelease, code, modifier, text, repeat);
  153. QSpontaneKeyEvent::setSpontaneous(&a);
  154. if (press && qt_sendShortcutOverrideEvent(widget, a.timestamp(), code, modifier, text, repeat))
  155. return;
  156. if (!qApp->notify(widget, &a))
  157. QTest::qWarn("Keyboard event not accepted by receiving widget");
  158. }
  159. static void sendKeyEvent(KeyAction action, QWidget *widget, Qt::Key code,
  160. QString text, Qt::KeyboardModifiers modifier, int delay=-1)
  161. {
  162. QTEST_ASSERT(qApp);
  163. if (!widget)
  164. widget = QWidget::keyboardGrabber();
  165. if (!widget) {
  166. // Popup widgets stealthily steal the keyboard grab
  167. if (QWidget *apw = QApplication::activePopupWidget())
  168. widget = apw->focusWidget() ? apw->focusWidget() : apw;
  169. }
  170. if (!widget) {
  171. QWindow *window = QGuiApplication::focusWindow();
  172. if (window) {
  173. sendKeyEvent(action, window, code, text, modifier, delay);
  174. return;
  175. }
  176. }
  177. if (!widget)
  178. widget = QApplication::focusWidget();
  179. if (!widget)
  180. widget = QApplication::activeWindow();
  181. QTEST_ASSERT(widget);
  182. if (action == Click) {
  183. QPointer<QWidget> ptr(widget);
  184. sendKeyEvent(Press, widget, code, text, modifier, delay);
  185. if (!ptr) {
  186. // if we send key-events to embedded widgets, they might be destroyed
  187. // when the user presses Return
  188. return;
  189. }
  190. sendKeyEvent(Release, widget, code, text, modifier, delay);
  191. return;
  192. }
  193. bool repeat = false;
  194. if (action == Press) {
  195. if (modifier & Qt::ShiftModifier)
  196. simulateEvent(widget, true, Qt::Key_Shift, Qt::KeyboardModifiers(), QString(), false, delay);
  197. if (modifier & Qt::ControlModifier)
  198. simulateEvent(widget, true, Qt::Key_Control, modifier & Qt::ShiftModifier, QString(), false, delay);
  199. if (modifier & Qt::AltModifier)
  200. simulateEvent(widget, true, Qt::Key_Alt,
  201. modifier & (Qt::ShiftModifier | Qt::ControlModifier), QString(), false, delay);
  202. if (modifier & Qt::MetaModifier)
  203. simulateEvent(widget, true, Qt::Key_Meta, modifier & (Qt::ShiftModifier
  204. | Qt::ControlModifier | Qt::AltModifier), QString(), false, delay);
  205. simulateEvent(widget, true, code, modifier, text, repeat, delay);
  206. } else if (action == Release) {
  207. simulateEvent(widget, false, code, modifier, text, repeat, delay);
  208. if (modifier & Qt::MetaModifier)
  209. simulateEvent(widget, false, Qt::Key_Meta, modifier, QString(), false, delay);
  210. if (modifier & Qt::AltModifier)
  211. simulateEvent(widget, false, Qt::Key_Alt, modifier &
  212. (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier), QString(), false, delay);
  213. if (modifier & Qt::ControlModifier)
  214. simulateEvent(widget, false, Qt::Key_Control,
  215. modifier & (Qt::ShiftModifier | Qt::ControlModifier), QString(), false, delay);
  216. if (modifier & Qt::ShiftModifier)
  217. simulateEvent(widget, false, Qt::Key_Shift, modifier & Qt::ShiftModifier, QString(), false, delay);
  218. }
  219. }
  220. // Convenience function
  221. static void sendKeyEvent(KeyAction action, QWidget *widget, Qt::Key code,
  222. char ascii, Qt::KeyboardModifiers modifier, int delay=-1)
  223. {
  224. QString text;
  225. if (ascii)
  226. text = QString(QChar::fromLatin1(ascii));
  227. sendKeyEvent(action, widget, code, text, modifier, delay);
  228. }
  229. inline static void keyEvent(KeyAction action, QWidget *widget, char ascii,
  230. Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  231. { sendKeyEvent(action, widget, asciiToKey(ascii), ascii, modifier, delay); }
  232. inline static void keyEvent(KeyAction action, QWidget *widget, Qt::Key key,
  233. Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  234. { sendKeyEvent(action, widget, key, keyToAscii(key), modifier, delay); }
  235. inline static void keyClicks(QWidget *widget, const QString &sequence,
  236. Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  237. {
  238. for (int i=0; i < sequence.length(); i++)
  239. keyEvent(Click, widget, sequence.at(i).toLatin1(), modifier, delay);
  240. }
  241. inline static void keyPress(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  242. { keyEvent(Press, widget, key, modifier, delay); }
  243. inline static void keyRelease(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  244. { keyEvent(Release, widget, key, modifier, delay); }
  245. inline static void keyClick(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  246. { keyEvent(Click, widget, key, modifier, delay); }
  247. inline static void keyPress(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  248. { keyEvent(Press, widget, key, modifier, delay); }
  249. inline static void keyRelease(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  250. { keyEvent(Release, widget, key, modifier, delay); }
  251. inline static void keyClick(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
  252. { keyEvent(Click, widget, key, modifier, delay); }
  253. #endif // QT_WIDGETS_LIB
  254. }
  255. QT_END_NAMESPACE
  256. #endif // QTESTKEYBOARD_H