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
40#ifndef QTESTEVENT_H
41#define QTESTEVENT_H
42
43#if 0
44// inform syncqt
45#pragma qt_no_master_include
46#endif
47
48#include <QtTest/qttestglobal.h>
49#ifdef QT_GUI_LIB
50#include <QtTest/qtestkeyboard.h>
51#include <QtTest/qtestmouse.h>
52#endif
53#include <QtTest/qtestsystem.h>
54
55#include <QtCore/qlist.h>
56
57#include <stdlib.h>
58
59QT_BEGIN_NAMESPACE
60
61
62class QTestEvent
63{
64public:
65#ifdef QT_WIDGETS_LIB
66 virtual void simulate(QWidget *w) = 0;
67#endif
68 virtual QTestEvent *clone() const = 0;
69
70 virtual ~QTestEvent() {}
71};
72
73#ifdef QT_GUI_LIB
74class QTestKeyEvent: public QTestEvent
75{
76public:
77 inline QTestKeyEvent(QTest::KeyAction action, Qt::Key key, Qt::KeyboardModifiers modifiers, int delay)
78 : _action(action), _delay(delay), _modifiers(modifiers), _ascii(0), _key(key) {}
79 inline QTestKeyEvent(QTest::KeyAction action, char ascii, Qt::KeyboardModifiers modifiers, int delay)
80 : _action(action), _delay(delay), _modifiers(modifiers),
81 _ascii(ascii), _key(Qt::Key_unknown) {}
82 inline QTestEvent *clone() const override { return new QTestKeyEvent(*this); }
83
84#ifdef QT_WIDGETS_LIB
85 inline void simulate(QWidget *w) override
86 {
87 if (_ascii == 0)
88 QTest::keyEvent(_action, w, _key, _modifiers, _delay);
89 else
90 QTest::keyEvent(_action, w, _ascii, _modifiers, _delay);
91 }
92#endif
93
94protected:
95 QTest::KeyAction _action;
96 int _delay;
97 Qt::KeyboardModifiers _modifiers;
98 char _ascii;
99 Qt::Key _key;
100};
101
102class QTestKeyClicksEvent: public QTestEvent
103{
104public:
105 inline QTestKeyClicksEvent(const QString &keys, Qt::KeyboardModifiers modifiers, int delay)
106 : _keys(keys), _modifiers(modifiers), _delay(delay)
107 {
108 Q_UNUSED(_delay) // Silence -Werror,-Wunused-private-field
109 }
110 inline QTestEvent *clone() const override { return new QTestKeyClicksEvent(*this); }
111
112#ifdef QT_WIDGETS_LIB
113 inline void simulate(QWidget *w) override
114 {
115 QTest::keyClicks(w, _keys, _modifiers, _delay);
116 }
117#endif
118
119private:
120 QString _keys;
121 Qt::KeyboardModifiers _modifiers;
122 int _delay;
123};
124
125class QTestMouseEvent: public QTestEvent
126{
127public:
128 inline QTestMouseEvent(QTest::MouseAction action, Qt::MouseButton button,
129 Qt::KeyboardModifiers modifiers, QPoint position, int delay)
130 : _action(action), _button(button), _modifiers(modifiers), _pos(position), _delay(delay)
131 {
132 Q_UNUSED(_action)
133 Q_UNUSED(_button)
134 Q_UNUSED(_delay)
135 }
136 inline QTestEvent *clone() const override { return new QTestMouseEvent(*this); }
137
138#ifdef QT_WIDGETS_LIB
139 inline void simulate(QWidget *w) override
140 {
141 QTest::mouseEvent(_action, w, _button, _modifiers, _pos, _delay);
142 }
143#endif
144
145private:
146 QTest::MouseAction _action;
147 Qt::MouseButton _button;
148 Qt::KeyboardModifiers _modifiers;
149 QPoint _pos;
150 int _delay;
151};
152#endif //QT_GUI_LIB
153
154
155class QTestDelayEvent: public QTestEvent
156{
157public:
158 inline QTestDelayEvent(int msecs): _delay(msecs)
159 {
160 Q_UNUSED(_delay)
161 }
162 inline QTestEvent *clone() const override { return new QTestDelayEvent(*this); }
163
164#ifdef QT_WIDGETS_LIB
165 inline void simulate(QWidget * /*w*/) override { QTest::qWait(_delay); }
166#endif
167
168private:
169 int _delay;
170};
171
172class QTestEventList: public QList<QTestEvent *>
173{
174public:
175 inline QTestEventList() {}
176 inline QTestEventList(const QTestEventList &other): QList<QTestEvent *>()
177 { for (int i = 0; i < other.count(); ++i) append(t: other.at(i)->clone()); }
178 inline ~QTestEventList()
179 { clear(); }
180 inline void clear()
181 { qDeleteAll(c: *this); QList<QTestEvent *>::clear(); }
182
183#ifdef QT_GUI_LIB
184 inline void addKeyClick(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
185 { addKeyEvent(action: QTest::Click, qtKey, modifiers, msecs); }
186 inline void addKeyPress(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
187 { addKeyEvent(action: QTest::Press, qtKey, modifiers, msecs); }
188 inline void addKeyRelease(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
189 { addKeyEvent(action: QTest::Release, qtKey, modifiers, msecs); }
190 inline void addKeyEvent(QTest::KeyAction action, Qt::Key qtKey,
191 Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
192 { append(t: new QTestKeyEvent(action, qtKey, modifiers, msecs)); }
193
194 inline void addKeyClick(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
195 { addKeyEvent(action: QTest::Click, ascii, modifiers, msecs); }
196 inline void addKeyPress(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
197 { addKeyEvent(action: QTest::Press, ascii, modifiers, msecs); }
198 inline void addKeyRelease(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
199 { addKeyEvent(action: QTest::Release, ascii, modifiers, msecs); }
200 inline void addKeyClicks(const QString &keys, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
201 { append(t: new QTestKeyClicksEvent(keys, modifiers, msecs)); }
202 inline void addKeyEvent(QTest::KeyAction action, char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
203 { append(t: new QTestKeyEvent(action, ascii, modifiers, msecs)); }
204
205 inline void addMousePress(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
206 QPoint pos = QPoint(), int delay=-1)
207 { append(t: new QTestMouseEvent(QTest::MousePress, button, stateKey, pos, delay)); }
208 inline void addMouseRelease(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
209 QPoint pos = QPoint(), int delay=-1)
210 { append(t: new QTestMouseEvent(QTest::MouseRelease, button, stateKey, pos, delay)); }
211 inline void addMouseClick(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
212 QPoint pos = QPoint(), int delay=-1)
213 { append(t: new QTestMouseEvent(QTest::MouseClick, button, stateKey, pos, delay)); }
214 inline void addMouseDClick(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(),
215 QPoint pos = QPoint(), int delay=-1)
216 { append(t: new QTestMouseEvent(QTest::MouseDClick, button, stateKey, pos, delay)); }
217 inline void addMouseMove(QPoint pos = QPoint(), int delay=-1)
218 { append(t: new QTestMouseEvent(QTest::MouseMove, Qt::NoButton, Qt::KeyboardModifiers(), pos, delay)); }
219#endif //QT_GUI_LIB
220
221 inline void addDelay(int msecs)
222 { append(t: new QTestDelayEvent(msecs)); }
223
224#ifdef QT_WIDGETS_LIB
225 inline void simulate(QWidget *w)
226 {
227 for (int i = 0; i < count(); ++i)
228 at(i)->simulate(w);
229 }
230#endif
231};
232
233QT_END_NAMESPACE
234
235Q_DECLARE_METATYPE(QTestEventList)
236
237#endif
238

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