1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QTESTKEYBOARD_H
5#define QTESTKEYBOARD_H
6
7#if 0
8// inform syncqt
9#pragma qt_no_master_include
10#endif
11
12#include <QtTest/qtestassert.h>
13#include <QtTest/qttestglobal.h>
14#include <QtTest/qtestsystem.h>
15#include <QtTest/qtestspontaneevent.h>
16
17#include <QtCore/qpointer.h>
18#include <QtGui/qguiapplication.h>
19#include <QtGui/qwindow.h>
20#include <QtGui/qevent.h>
21#if QT_CONFIG(shortcut)
22# include <QtGui/qkeysequence.h>
23#endif
24
25#ifdef QT_WIDGETS_LIB
26#include <QtWidgets/qwidget.h>
27#include <QtWidgets/qapplication.h>
28#endif
29
30QT_BEGIN_NAMESPACE
31
32Q_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);
33Q_GUI_EXPORT bool qt_sendShortcutOverrideEvent(QObject *o, ulong timestamp, int k, Qt::KeyboardModifiers mods, const QString &text = QString(), bool autorep = false, ushort count = 1);
34
35namespace QTest
36{
37 enum KeyAction { Press, Release, Click, Shortcut };
38
39 static void simulateEvent(QWindow *window, bool press, int code,
40 Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1)
41 {
42 QEvent::Type type;
43 type = press ? QEvent::KeyPress : QEvent::KeyRelease;
44 qt_handleKeyEvent(w: window, t: type, k: code, mods: modifier, text, autorep: repeat, count: delay);
45 qApp->processEvents();
46 }
47
48 static void sendKeyEvent(KeyAction action, QWindow *window, Qt::Key code,
49 QString text, Qt::KeyboardModifiers modifier, int delay=-1)
50 {
51 QTEST_ASSERT(qApp);
52
53 if (!window)
54 window = QGuiApplication::focusWindow();
55
56 QTEST_ASSERT(window);
57
58
59 if (action == Click) {
60 QPointer<QWindow> ptr(window);
61 sendKeyEvent(action: Press, window, code, text, modifier, delay);
62 if (!ptr)
63 return;
64 sendKeyEvent(action: Release, window, code, text, modifier, delay);
65 return;
66 }
67
68 bool repeat = false;
69
70 if (action == Shortcut) {
71 int timestamp = 0;
72 qt_sendShortcutOverrideEvent(o: window, timestamp, k: code, mods: modifier, text, autorep: repeat);
73 return;
74 }
75
76 if (action == Press) {
77 if (modifier & Qt::ShiftModifier)
78 simulateEvent(window, press: true, code: Qt::Key_Shift, modifier: Qt::KeyboardModifiers(), text: QString(), repeat: false, delay);
79
80 if (modifier & Qt::ControlModifier)
81 simulateEvent(window, press: true, code: Qt::Key_Control, modifier: modifier & Qt::ShiftModifier, text: QString(), repeat: false, delay);
82
83 if (modifier & Qt::AltModifier)
84 simulateEvent(window, press: true, code: Qt::Key_Alt,
85 modifier: modifier & (Qt::ShiftModifier | Qt::ControlModifier), text: QString(), repeat: false, delay);
86 if (modifier & Qt::MetaModifier)
87 simulateEvent(window, press: true, code: Qt::Key_Meta, modifier: modifier & (Qt::ShiftModifier
88 | Qt::ControlModifier | Qt::AltModifier), text: QString(), repeat: false, delay);
89 simulateEvent(window, press: true, code, modifier, text, repeat, delay);
90 } else if (action == Release) {
91 simulateEvent(window, press: false, code, modifier, text, repeat, delay);
92
93 if (modifier & Qt::MetaModifier)
94 simulateEvent(window, press: false, code: Qt::Key_Meta, modifier, text: QString(), repeat: false, delay);
95 if (modifier & Qt::AltModifier)
96 simulateEvent(window, press: false, code: Qt::Key_Alt, modifier: modifier &
97 (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier), text: QString(), repeat: false, delay);
98
99 if (modifier & Qt::ControlModifier)
100 simulateEvent(window, press: false, code: Qt::Key_Control,
101 modifier: modifier & (Qt::ShiftModifier | Qt::ControlModifier), text: QString(), repeat: false, delay);
102
103 if (modifier & Qt::ShiftModifier)
104 simulateEvent(window, press: false, code: Qt::Key_Shift, modifier: modifier & Qt::ShiftModifier, text: QString(), repeat: false, delay);
105 }
106 }
107
108 // Convenience function
109 static void sendKeyEvent(KeyAction action, QWindow *window, Qt::Key code,
110 char ascii, Qt::KeyboardModifiers modifier, int delay=-1)
111 {
112 QString text;
113 if (ascii)
114 text = QString(QChar::fromLatin1(c: ascii));
115 sendKeyEvent(action, window, code, text, modifier, delay);
116 }
117
118 inline static void keyEvent(KeyAction action, QWindow *window, char ascii,
119 Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
120 { sendKeyEvent(action, window, code: asciiToKey(ascii), ascii, modifier, delay); }
121 inline static void keyEvent(KeyAction action, QWindow *window, Qt::Key key,
122 Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
123 { sendKeyEvent(action, window, code: key, ascii: keyToAscii(key), modifier, delay); }
124
125 [[maybe_unused]] inline static void keyClick(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
126 { keyEvent(action: Click, window, key, modifier, delay); }
127 [[maybe_unused]] inline static void keyClick(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
128 { keyEvent(action: Click, window, ascii: key, modifier, delay); }
129 [[maybe_unused]] inline static void keyRelease(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
130 { keyEvent(action: Release, window, ascii: key, modifier, delay); }
131 [[maybe_unused]] inline static void keyRelease(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
132 { keyEvent(action: Release, window, key, modifier, delay); }
133 [[maybe_unused]] inline static void keyPress(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
134 { keyEvent(action: Press, window, ascii: key, modifier, delay); }
135 [[maybe_unused]] inline static void keyPress(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
136 { keyEvent(action: Press, window, key, modifier, delay); }
137
138#if QT_CONFIG(shortcut)
139 [[maybe_unused]] inline static void keySequence(QWindow *window, const QKeySequence &keySequence)
140 {
141 for (int i = 0; i < keySequence.count(); ++i) {
142 const Qt::Key key = keySequence[i].key();
143 const Qt::KeyboardModifiers modifiers = keySequence[i].keyboardModifiers();
144 keyClick(window, key, modifier: modifiers);
145 }
146 }
147#endif
148
149#ifdef QT_WIDGETS_LIB
150 static void simulateEvent(QWidget *widget, bool press, int code,
151 Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1)
152 {
153 QTEST_ASSERT(widget);
154 extern int Q_TESTLIB_EXPORT defaultKeyDelay();
155
156 if (delay == -1 || delay < defaultKeyDelay())
157 delay = defaultKeyDelay();
158 if (delay > 0)
159 QTest::qWait(delay);
160
161 QKeyEvent a(press ? QEvent::KeyPress : QEvent::KeyRelease, code, modifier, text, repeat);
162 QSpontaneKeyEvent::setSpontaneous(&a);
163
164 if (press && qt_sendShortcutOverrideEvent(widget, a.timestamp(), code, modifier, text, repeat))
165 return;
166 if (!qApp->notify(widget, &a))
167 qWarning("Keyboard event not accepted by receiving widget");
168 }
169
170 static void sendKeyEvent(KeyAction action, QWidget *widget, Qt::Key code,
171 QString text, Qt::KeyboardModifiers modifier, int delay=-1)
172 {
173 QTEST_ASSERT(qApp);
174
175 if (!widget)
176 widget = QWidget::keyboardGrabber();
177 if (!widget) {
178 // Popup widgets stealthily steal the keyboard grab
179 if (QWidget *apw = QApplication::activePopupWidget())
180 widget = apw->focusWidget() ? apw->focusWidget() : apw;
181 }
182 if (!widget) {
183 QWindow *window = QGuiApplication::focusWindow();
184 if (window) {
185 sendKeyEvent(action, window, code, text, modifier, delay);
186 return;
187 }
188 }
189 if (!widget)
190 widget = QApplication::focusWidget();
191 if (!widget)
192 widget = QApplication::activeWindow();
193
194 QTEST_ASSERT(widget);
195
196 if (action == Click) {
197 QPointer<QWidget> ptr(widget);
198 sendKeyEvent(Press, widget, code, text, modifier, delay);
199 if (!ptr) {
200 // if we send key-events to embedded widgets, they might be destroyed
201 // when the user presses Return
202 return;
203 }
204 sendKeyEvent(Release, widget, code, text, modifier, delay);
205 return;
206 }
207
208 bool repeat = false;
209
210 if (action == Press) {
211 if (modifier & Qt::ShiftModifier)
212 simulateEvent(widget, true, Qt::Key_Shift, Qt::KeyboardModifiers(), QString(), false, delay);
213
214 if (modifier & Qt::ControlModifier)
215 simulateEvent(widget, true, Qt::Key_Control, modifier & Qt::ShiftModifier, QString(), false, delay);
216
217 if (modifier & Qt::AltModifier)
218 simulateEvent(widget, true, Qt::Key_Alt,
219 modifier & (Qt::ShiftModifier | Qt::ControlModifier), QString(), false, delay);
220 if (modifier & Qt::MetaModifier)
221 simulateEvent(widget, true, Qt::Key_Meta, modifier & (Qt::ShiftModifier
222 | Qt::ControlModifier | Qt::AltModifier), QString(), false, delay);
223 simulateEvent(widget, true, code, modifier, text, repeat, delay);
224 } else if (action == Release) {
225 simulateEvent(widget, false, code, modifier, text, repeat, delay);
226
227 if (modifier & Qt::MetaModifier)
228 simulateEvent(widget, false, Qt::Key_Meta, modifier, QString(), false, delay);
229 if (modifier & Qt::AltModifier)
230 simulateEvent(widget, false, Qt::Key_Alt, modifier &
231 (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier), QString(), false, delay);
232
233 if (modifier & Qt::ControlModifier)
234 simulateEvent(widget, false, Qt::Key_Control,
235 modifier & (Qt::ShiftModifier | Qt::ControlModifier), QString(), false, delay);
236
237 if (modifier & Qt::ShiftModifier)
238 simulateEvent(widget, false, Qt::Key_Shift, modifier & Qt::ShiftModifier, QString(), false, delay);
239 }
240 }
241
242 // Convenience function
243 static void sendKeyEvent(KeyAction action, QWidget *widget, Qt::Key code,
244 char ascii, Qt::KeyboardModifiers modifier, int delay=-1)
245 {
246 QString text;
247 if (ascii)
248 text = QString(QChar::fromLatin1(ascii));
249 sendKeyEvent(action, widget, code, text, modifier, delay);
250 }
251
252 inline static void keyEvent(KeyAction action, QWidget *widget, char ascii,
253 Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
254 { sendKeyEvent(action, widget, asciiToKey(ascii), ascii, modifier, delay); }
255 inline static void keyEvent(KeyAction action, QWidget *widget, Qt::Key key,
256 Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
257 { sendKeyEvent(action, widget, key, keyToAscii(key), modifier, delay); }
258
259 inline static void keyClicks(QWidget *widget, const QString &sequence,
260 Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
261 {
262 for (int i=0; i < sequence.size(); i++)
263 keyEvent(Click, widget, sequence.at(i).toLatin1(), modifier, delay);
264 }
265
266 inline static void keyPress(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
267 { keyEvent(Press, widget, key, modifier, delay); }
268 inline static void keyRelease(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
269 { keyEvent(Release, widget, key, modifier, delay); }
270 inline static void keyClick(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
271 { keyEvent(Click, widget, key, modifier, delay); }
272 inline static void keyPress(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
273 { keyEvent(Press, widget, key, modifier, delay); }
274 inline static void keyRelease(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
275 { keyEvent(Release, widget, key, modifier, delay); }
276 inline static void keyClick(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
277 { keyEvent(Click, widget, key, modifier, delay); }
278
279#if QT_CONFIG(shortcut)
280 inline static void keySequence(QWidget *widget, const QKeySequence &keySequence)
281 {
282 for (int i = 0; i < keySequence.count(); ++i) {
283 const Qt::Key key = keySequence[i].key();
284 const Qt::KeyboardModifiers modifiers = keySequence[i].keyboardModifiers();
285 keyClick(widget, key, modifiers);
286 }
287 }
288#endif
289
290#endif // QT_WIDGETS_LIB
291
292}
293
294QT_END_NAMESPACE
295
296#endif // QTESTKEYBOARD_H
297

source code of qtbase/src/testlib/qtestkeyboard.h