1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtTest module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
47 #pragma qt_no_master_include
50 #include <QtTest/qtest_global.h>
52 #include <QtTest/qtestkeyboard.h>
53 #include <QtTest/qtestmouse.h>
55 #include <QtTest/qtestsystem.h>
57 #include <QtCore/qlist.h>
70 virtual void simulate(QWidget *w) = 0;
72 virtual QTestEvent *clone() const = 0;
74 virtual ~QTestEvent() {}
78 class QTestKeyEvent: public QTestEvent
81 inline QTestKeyEvent(QTest::KeyAction action, Qt::Key key, Qt::KeyboardModifiers modifiers, int delay)
82 : _action(action), _delay(delay), _modifiers(modifiers), _ascii(0), _key(key) {}
83 inline QTestKeyEvent(QTest::KeyAction action, char ascii, Qt::KeyboardModifiers modifiers, int delay)
84 : _action(action), _delay(delay), _modifiers(modifiers),
85 _ascii(ascii), _key(Qt::Key_unknown) {}
86 inline QTestEvent *clone() const { return new QTestKeyEvent(*this); }
89 inline void simulate(QWidget *w)
92 QTest::keyEvent(_action, w, _key, _modifiers, _delay);
94 QTest::keyEvent(_action, w, _ascii, _modifiers, _delay);
99 QTest::KeyAction _action;
101 Qt::KeyboardModifiers _modifiers;
106 class QTestKeyClicksEvent: public QTestEvent
109 inline QTestKeyClicksEvent(const QString &keys, Qt::KeyboardModifiers modifiers, int delay)
110 : _keys(keys), _modifiers(modifiers), _delay(delay) {}
111 inline QTestEvent *clone() const { return new QTestKeyClicksEvent(*this); }
113 #ifdef QT_WIDGETS_LIB
114 inline void simulate(QWidget *w)
116 QTest::keyClicks(w, _keys, _modifiers, _delay);
122 Qt::KeyboardModifiers _modifiers;
126 class QTestMouseEvent: public QTestEvent
129 inline QTestMouseEvent(QTest::MouseAction action, Qt::MouseButton button,
130 Qt::KeyboardModifiers modifiers, QPoint position, int delay)
131 : _action(action), _button(button), _modifiers(modifiers), _pos(position), _delay(delay) {}
132 inline QTestEvent *clone() const { return new QTestMouseEvent(*this); }
134 #ifdef QT_WIDGETS_LIB
135 inline void simulate(QWidget *w)
137 QTest::mouseEvent(_action, w, _button, _modifiers, _pos, _delay);
142 QTest::MouseAction _action;
143 Qt::MouseButton _button;
144 Qt::KeyboardModifiers _modifiers;
151 class QTestDelayEvent: public QTestEvent
154 inline QTestDelayEvent(int msecs): _delay(msecs) {}
155 inline QTestEvent *clone() const { return new QTestDelayEvent(*this); }
157 #ifdef QT_WIDGETS_LIB
158 inline void simulate(QWidget * /*w*/) { QTest::qWait(_delay); }
165 class QTestEventList: public QList<QTestEvent *>
168 inline QTestEventList() {}
169 inline QTestEventList(const QTestEventList &other): QList<QTestEvent *>()
170 { for (int i = 0; i < other.count(); ++i) append(other.at(i)->clone()); }
171 inline ~QTestEventList()
174 { qDeleteAll(*this); QList<QTestEvent *>::clear(); }
177 inline void addKeyClick(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
178 { addKeyEvent(QTest::Click, qtKey, modifiers, msecs); }
179 inline void addKeyPress(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
180 { addKeyEvent(QTest::Press, qtKey, modifiers, msecs); }
181 inline void addKeyRelease(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
182 { addKeyEvent(QTest::Release, qtKey, modifiers, msecs); }
183 inline void addKeyEvent(QTest::KeyAction action, Qt::Key qtKey,
184 Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
185 { append(new QTestKeyEvent(action, qtKey, modifiers, msecs)); }
187 inline void addKeyClick(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
188 { addKeyEvent(QTest::Click, ascii, modifiers, msecs); }
189 inline void addKeyPress(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
190 { addKeyEvent(QTest::Press, ascii, modifiers, msecs); }
191 inline void addKeyRelease(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
192 { addKeyEvent(QTest::Release, ascii, modifiers, msecs); }
193 inline void addKeyClicks(const QString &keys, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
194 { append(new QTestKeyClicksEvent(keys, modifiers, msecs)); }
195 inline void addKeyEvent(QTest::KeyAction action, char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
196 { append(new QTestKeyEvent(action, ascii, modifiers, msecs)); }
198 inline void addMousePress(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
199 QPoint pos = QPoint(), int delay=-1)
200 { append(new QTestMouseEvent(QTest::MousePress, button, stateKey, pos, delay)); }
201 inline void addMouseRelease(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
202 QPoint pos = QPoint(), int delay=-1)
203 { append(new QTestMouseEvent(QTest::MouseRelease, button, stateKey, pos, delay)); }
204 inline void addMouseClick(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
205 QPoint pos = QPoint(), int delay=-1)
206 { append(new QTestMouseEvent(QTest::MouseClick, button, stateKey, pos, delay)); }
207 inline void addMouseDClick(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
208 QPoint pos = QPoint(), int delay=-1)
209 { append(new QTestMouseEvent(QTest::MouseDClick, button, stateKey, pos, delay)); }
210 inline void addMouseMove(QPoint pos = QPoint(), int delay=-1)
211 { append(new QTestMouseEvent(QTest::MouseMove, Qt::NoButton, 0, pos, delay)); }
214 inline void addDelay(int msecs)
215 { append(new QTestDelayEvent(msecs)); }
217 #ifdef QT_WIDGETS_LIB
218 inline void simulate(QWidget *w)
220 for (int i = 0; i < count(); ++i)
228 Q_DECLARE_METATYPE(QTestEventList)