1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtTest module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #if !defined(QTESTKEYBOARD_H)
43 #define QTESTKEYBOARD_H
47 #pragma qt_no_master_include
50 #include <QtTest/qtestassert.h>
51 #include <QtTest/qtest_global.h>
52 #include <QtTest/qtestsystem.h>
53 #include <QtTest/qtestspontaneevent.h>
55 #include <QtCore/qpointer.h>
56 #include <QtGui/qapplication.h>
57 #include <QtGui/qevent.h>
58 #include <QtGui/qwidget.h>
68 enum KeyAction { Press, Release, Click };
70 static void simulateEvent(QWidget *widget, bool press, int code,
71 Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1)
74 extern int Q_TESTLIB_EXPORT defaultKeyDelay();
76 if (delay == -1 || delay < defaultKeyDelay())
77 delay = defaultKeyDelay();
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");
87 static void sendKeyEvent(KeyAction action, QWidget *widget, Qt::Key code,
88 QString text, Qt::KeyboardModifiers modifier, int delay=-1)
93 widget = QWidget::keyboardGrabber();
95 if (QWidget *apw = QApplication::activePopupWidget())
96 widget = apw->focusWidget() ? apw->focusWidget() : apw;
98 widget = QApplication::focusWidget();
101 widget = QApplication::activeWindow();
103 QTEST_ASSERT(widget);
105 if (action == Click) {
106 QPointer<QWidget> ptr(widget);
107 sendKeyEvent(Press, widget, code, text, modifier, delay);
109 // if we send key-events to embedded widgets, they might be destroyed
110 // when the user presses Return
113 sendKeyEvent(Release, widget, code, text, modifier, delay);
119 if (action == Press) {
120 if (modifier & Qt::ShiftModifier)
121 simulateEvent(widget, true, Qt::Key_Shift, 0, QString(), false, delay);
123 if (modifier & Qt::ControlModifier)
124 simulateEvent(widget, true, Qt::Key_Control, modifier & Qt::ShiftModifier, QString(), false, delay);
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);
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);
142 if (modifier & Qt::ControlModifier)
143 simulateEvent(widget, false, Qt::Key_Control,
144 modifier & (Qt::ShiftModifier | Qt::ControlModifier), QString(), false, delay);
146 if (modifier & Qt::ShiftModifier)
147 simulateEvent(widget, false, Qt::Key_Shift, modifier & Qt::ShiftModifier, QString(), false, delay);
151 // Convenience function
152 static void sendKeyEvent(KeyAction action, QWidget *widget, Qt::Key code,
153 char ascii, Qt::KeyboardModifiers modifier, int delay=-1)
157 text = QString(QChar::fromLatin1(ascii));
158 sendKeyEvent(action, widget, code, text, modifier, delay);
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); }
168 inline static void keyClicks(QWidget *widget, const QString &sequence,
169 Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
171 for (int i=0; i < sequence.length(); i++)
172 keyEvent(Click, widget, sequence.at(i).toLatin1(), modifier, delay);
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); }
194 #endif // QTESTKEYBOARD_H