Warning: That file was not part of the compilation database. It may have many parsing errors.
1 | /**************************************************************************** |
---|---|
2 | ** |
3 | ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). |
4 | ** Contact: http://www.qt-project.org/legal |
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 Digia. For licensing terms and |
14 | ** conditions see http://qt.digia.com/licensing. For further information |
15 | ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
24 | ** |
25 | ** In addition, as a special exception, Digia gives you certain additional |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
28 | ** |
29 | ** GNU General Public License Usage |
30 | ** Alternatively, this file may be used under the terms of the GNU |
31 | ** General Public License version 3.0 as published by the Free Software |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the |
33 | ** packaging of this file. Please review the following information to |
34 | ** ensure the GNU General Public License version 3.0 requirements will be |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. |
36 | ** |
37 | ** |
38 | ** $QT_END_LICENSE$ |
39 | ** |
40 | ****************************************************************************/ |
41 | |
42 | #if !defined(QTESTKEYBOARD_H) |
43 | #define QTESTKEYBOARD_H |
44 | |
45 | #if 0 |
46 | // inform syncqt |
47 | #pragma qt_no_master_include |
48 | #endif |
49 | |
50 | #include <QtTest/qtestassert.h> |
51 | #include <QtTest/qtest_global.h> |
52 | #include <QtTest/qtestsystem.h> |
53 | #include <QtTest/qtestspontaneevent.h> |
54 | |
55 | #include <QtCore/qpointer.h> |
56 | #include <QtGui/qapplication.h> |
57 | #include <QtGui/qevent.h> |
58 | #include <QtGui/qwidget.h> |
59 | |
60 | QT_BEGIN_HEADER |
61 | |
62 | QT_BEGIN_NAMESPACE |
63 | |
64 | QT_MODULE(Test) |
65 | |
66 | namespace QTest |
67 | { |
68 | enum KeyAction { Press, Release, Click }; |
69 | |
70 | static void simulateEvent(QWidget *widget, bool press, int code, |
71 | Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1) |
72 | { |
73 | QTEST_ASSERT(widget); |
74 | extern int Q_TESTLIB_EXPORT defaultKeyDelay(); |
75 | |
76 | if (delay == -1 || delay < defaultKeyDelay()) |
77 | delay = defaultKeyDelay(); |
78 | if(delay > 0) |
79 | QTest::qWait(delay); |
80 | |
81 | QKeyEvent a(press ? QEvent::KeyPress : QEvent::KeyRelease, code, modifier, text, repeat); |
82 | QSpontaneKeyEvent::setSpontaneous(&a); |
83 | if (!qApp->notify(widget, &a)) |
84 | QTest::qWarn("Keyboard event not accepted by receiving widget"); |
85 | } |
86 | |
87 | static void sendKeyEvent(KeyAction action, QWidget *widget, Qt::Key code, |
88 | QString text, Qt::KeyboardModifiers modifier, int delay=-1) |
89 | { |
90 | QTEST_ASSERT(qApp); |
91 | |
92 | if (!widget) |
93 | widget = QWidget::keyboardGrabber(); |
94 | if (!widget) { |
95 | if (QWidget *apw = QApplication::activePopupWidget()) |
96 | widget = apw->focusWidget() ? apw->focusWidget() : apw; |
97 | else |
98 | widget = QApplication::focusWidget(); |
99 | } |
100 | if (!widget) |
101 | widget = QApplication::activeWindow(); |
102 | |
103 | QTEST_ASSERT(widget); |
104 | |
105 | if (action == Click) { |
106 | QPointer<QWidget> ptr(widget); |
107 | sendKeyEvent(Press, widget, code, text, modifier, delay); |
108 | if (!ptr) { |
109 | // if we send key-events to embedded widgets, they might be destroyed |
110 | // when the user presses Return |
111 | return; |
112 | } |
113 | sendKeyEvent(Release, widget, code, text, modifier, delay); |
114 | return; |
115 | } |
116 | |
117 | bool repeat = false; |
118 | |
119 | if (action == Press) { |
120 | if (modifier & Qt::ShiftModifier) |
121 | simulateEvent(widget, true, Qt::Key_Shift, 0, QString(), false, delay); |
122 | |
123 | if (modifier & Qt::ControlModifier) |
124 | simulateEvent(widget, true, Qt::Key_Control, modifier & Qt::ShiftModifier, QString(), false, delay); |
125 | |
126 | if (modifier & Qt::AltModifier) |
127 | simulateEvent(widget, true, Qt::Key_Alt, |
128 | modifier & (Qt::ShiftModifier | Qt::ControlModifier), QString(), false, delay); |
129 | if (modifier & Qt::MetaModifier) |
130 | simulateEvent(widget, true, Qt::Key_Meta, modifier & (Qt::ShiftModifier |
131 | | Qt::ControlModifier | Qt::AltModifier), QString(), false, delay); |
132 | simulateEvent(widget, true, code, modifier, text, repeat, delay); |
133 | } else if (action == Release) { |
134 | simulateEvent(widget, false, code, modifier, text, repeat, delay); |
135 | |
136 | if (modifier & Qt::MetaModifier) |
137 | simulateEvent(widget, false, Qt::Key_Meta, modifier, QString(), false, delay); |
138 | if (modifier & Qt::AltModifier) |
139 | simulateEvent(widget, false, Qt::Key_Alt, modifier & |
140 | (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier), QString(), false, delay); |
141 | |
142 | if (modifier & Qt::ControlModifier) |
143 | simulateEvent(widget, false, Qt::Key_Control, |
144 | modifier & (Qt::ShiftModifier | Qt::ControlModifier), QString(), false, delay); |
145 | |
146 | if (modifier & Qt::ShiftModifier) |
147 | simulateEvent(widget, false, Qt::Key_Shift, modifier & Qt::ShiftModifier, QString(), false, delay); |
148 | } |
149 | } |
150 | |
151 | // Convenience function |
152 | static void sendKeyEvent(KeyAction action, QWidget *widget, Qt::Key code, |
153 | char ascii, Qt::KeyboardModifiers modifier, int delay=-1) |
154 | { |
155 | QString text; |
156 | if (ascii) |
157 | text = QString(QChar::fromLatin1(ascii)); |
158 | sendKeyEvent(action, widget, code, text, modifier, delay); |
159 | } |
160 | |
161 | inline static void keyEvent(KeyAction action, QWidget *widget, char ascii, |
162 | Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
163 | { sendKeyEvent(action, widget, asciiToKey(ascii), ascii, modifier, delay); } |
164 | inline static void keyEvent(KeyAction action, QWidget *widget, Qt::Key key, |
165 | Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
166 | { sendKeyEvent(action, widget, key, keyToAscii(key), modifier, delay); } |
167 | |
168 | inline static void keyClicks(QWidget *widget, const QString &sequence, |
169 | Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
170 | { |
171 | for (int i=0; i < sequence.length(); i++) |
172 | keyEvent(Click, widget, sequence.at(i).toLatin1(), modifier, delay); |
173 | } |
174 | |
175 | inline static void keyPress(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
176 | { keyEvent(Press, widget, key, modifier, delay); } |
177 | inline static void keyRelease(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
178 | { keyEvent(Release, widget, key, modifier, delay); } |
179 | inline static void keyClick(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
180 | { keyEvent(Click, widget, key, modifier, delay); } |
181 | inline static void keyPress(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
182 | { keyEvent(Press, widget, key, modifier, delay); } |
183 | inline static void keyRelease(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
184 | { keyEvent(Release, widget, key, modifier, delay); } |
185 | inline static void keyClick(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
186 | { keyEvent(Click, widget, key, modifier, delay); } |
187 | |
188 | } |
189 | |
190 | QT_END_NAMESPACE |
191 | |
192 | QT_END_HEADER |
193 | |
194 | #endif // QTESTKEYBOARD_H |
195 |
Warning: That file was not part of the compilation database. It may have many parsing errors.