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