1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QTESTEVENT_H |
5 | #define QTESTEVENT_H |
6 | |
7 | #if 0 |
8 | // inform syncqt |
9 | #pragma qt_no_master_include |
10 | #endif |
11 | |
12 | #include <QtTest/qttestglobal.h> |
13 | #ifdef QT_GUI_LIB |
14 | #include <QtTest/qtestkeyboard.h> |
15 | #include <QtTest/qtestmouse.h> |
16 | #endif |
17 | #include <QtTest/qtestsystem.h> |
18 | |
19 | #include <QtCore/qlist.h> |
20 | |
21 | #include <stdlib.h> |
22 | |
23 | QT_BEGIN_NAMESPACE |
24 | |
25 | #ifdef QT_WIDGETS_LIB |
26 | # define QT_ONLY_WIDGETLIB_USES |
27 | #else |
28 | # define QT_ONLY_WIDGETLIB_USES Q_DECL_UNUSED_MEMBER |
29 | #endif |
30 | |
31 | class QTestEvent |
32 | { |
33 | public: |
34 | #ifdef QT_WIDGETS_LIB |
35 | virtual void simulate(QWidget *w) = 0; |
36 | #endif |
37 | virtual QTestEvent *clone() const = 0; |
38 | |
39 | virtual ~QTestEvent() {} |
40 | }; |
41 | |
42 | #ifdef QT_GUI_LIB |
43 | class QTestKeyEvent: public QTestEvent |
44 | { |
45 | public: |
46 | inline QTestKeyEvent(QTest::KeyAction action, Qt::Key key, Qt::KeyboardModifiers modifiers, int delay) |
47 | : _action(action), _delay(delay), _modifiers(modifiers), _ascii(0), _key(key) {} |
48 | inline QTestKeyEvent(QTest::KeyAction action, char ascii, Qt::KeyboardModifiers modifiers, int delay) |
49 | : _action(action), _delay(delay), _modifiers(modifiers), |
50 | _ascii(ascii), _key(Qt::Key_unknown) {} |
51 | inline QTestEvent *clone() const override { return new QTestKeyEvent(*this); } |
52 | |
53 | #ifdef QT_WIDGETS_LIB |
54 | inline void simulate(QWidget *w) override |
55 | { |
56 | if (_ascii == 0) |
57 | QTest::keyEvent(_action, w, _key, _modifiers, _delay); |
58 | else |
59 | QTest::keyEvent(_action, w, _ascii, _modifiers, _delay); |
60 | } |
61 | #endif |
62 | |
63 | protected: |
64 | QTest::KeyAction _action; |
65 | int _delay; |
66 | Qt::KeyboardModifiers _modifiers; |
67 | char _ascii; |
68 | Qt::Key _key; |
69 | }; |
70 | |
71 | class QTestKeyClicksEvent: public QTestEvent |
72 | { |
73 | public: |
74 | inline QTestKeyClicksEvent(const QString &keys, Qt::KeyboardModifiers modifiers, int delay) |
75 | : _keys(keys), _modifiers(modifiers), _delay(delay) {} |
76 | inline QTestEvent *clone() const override { return new QTestKeyClicksEvent(*this); } |
77 | |
78 | #ifdef QT_WIDGETS_LIB |
79 | inline void simulate(QWidget *w) override |
80 | { |
81 | QTest::keyClicks(w, _keys, _modifiers, _delay); |
82 | } |
83 | #endif |
84 | |
85 | private: |
86 | QT_ONLY_WIDGETLIB_USES QString _keys; |
87 | QT_ONLY_WIDGETLIB_USES Qt::KeyboardModifiers _modifiers; |
88 | QT_ONLY_WIDGETLIB_USES int _delay; |
89 | }; |
90 | |
91 | class QTestMouseEvent: public QTestEvent |
92 | { |
93 | public: |
94 | inline QTestMouseEvent(QTest::MouseAction action, Qt::MouseButton button, |
95 | Qt::KeyboardModifiers modifiers, QPoint position, int delay) |
96 | : _action(action), _button(button), _modifiers(modifiers), _pos(position), _delay(delay) {} |
97 | inline QTestEvent *clone() const override { return new QTestMouseEvent(*this); } |
98 | |
99 | #ifdef QT_WIDGETS_LIB |
100 | inline void simulate(QWidget *w) override |
101 | { |
102 | QTest::mouseEvent(_action, w, _button, _modifiers, _pos, _delay); |
103 | } |
104 | #endif |
105 | |
106 | private: |
107 | QT_ONLY_WIDGETLIB_USES QTest::MouseAction _action; |
108 | QT_ONLY_WIDGETLIB_USES Qt::MouseButton _button; |
109 | QT_ONLY_WIDGETLIB_USES Qt::KeyboardModifiers _modifiers; |
110 | QT_ONLY_WIDGETLIB_USES QPoint _pos; |
111 | QT_ONLY_WIDGETLIB_USES int _delay; |
112 | }; |
113 | #endif //QT_GUI_LIB |
114 | |
115 | |
116 | class QTestDelayEvent: public QTestEvent |
117 | { |
118 | public: |
119 | inline QTestDelayEvent(int msecs): _delay(msecs) {} |
120 | inline QTestEvent *clone() const override { return new QTestDelayEvent(*this); } |
121 | |
122 | #ifdef QT_WIDGETS_LIB |
123 | inline void simulate(QWidget * /*w*/) override { QTest::qWait(_delay); } |
124 | #endif |
125 | |
126 | private: |
127 | QT_ONLY_WIDGETLIB_USES int _delay; |
128 | }; |
129 | |
130 | class QTestEventList: public QList<QTestEvent *> |
131 | { |
132 | public: |
133 | inline QTestEventList() {} |
134 | inline QTestEventList(const QTestEventList &other): QList<QTestEvent *>() |
135 | { for (int i = 0; i < other.size(); ++i) append(t: other.at(i)->clone()); } |
136 | inline ~QTestEventList() |
137 | { clear(); } |
138 | inline void clear() |
139 | { qDeleteAll(c: *this); QList<QTestEvent *>::clear(); } |
140 | |
141 | #ifdef QT_GUI_LIB |
142 | inline void addKeyClick(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
143 | { addKeyEvent(action: QTest::Click, qtKey, modifiers, msecs); } |
144 | inline void addKeyPress(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
145 | { addKeyEvent(action: QTest::Press, qtKey, modifiers, msecs); } |
146 | inline void addKeyRelease(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
147 | { addKeyEvent(action: QTest::Release, qtKey, modifiers, msecs); } |
148 | inline void addKeyEvent(QTest::KeyAction action, Qt::Key qtKey, |
149 | Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
150 | { append(t: new QTestKeyEvent(action, qtKey, modifiers, msecs)); } |
151 | |
152 | inline void addKeyClick(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
153 | { addKeyEvent(action: QTest::Click, ascii, modifiers, msecs); } |
154 | inline void addKeyPress(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
155 | { addKeyEvent(action: QTest::Press, ascii, modifiers, msecs); } |
156 | inline void addKeyRelease(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
157 | { addKeyEvent(action: QTest::Release, ascii, modifiers, msecs); } |
158 | inline void addKeyClicks(const QString &keys, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
159 | { append(t: new QTestKeyClicksEvent(keys, modifiers, msecs)); } |
160 | inline void addKeyEvent(QTest::KeyAction action, char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
161 | { append(t: new QTestKeyEvent(action, ascii, modifiers, msecs)); } |
162 | |
163 | inline void addMousePress(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), |
164 | QPoint pos = QPoint(), int delay=-1) |
165 | { append(t: new QTestMouseEvent(QTest::MousePress, button, stateKey, pos, delay)); } |
166 | inline void addMouseRelease(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), |
167 | QPoint pos = QPoint(), int delay=-1) |
168 | { append(t: new QTestMouseEvent(QTest::MouseRelease, button, stateKey, pos, delay)); } |
169 | inline void addMouseClick(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), |
170 | QPoint pos = QPoint(), int delay=-1) |
171 | { append(t: new QTestMouseEvent(QTest::MouseClick, button, stateKey, pos, delay)); } |
172 | inline void addMouseDClick(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), |
173 | QPoint pos = QPoint(), int delay=-1) |
174 | { append(t: new QTestMouseEvent(QTest::MouseDClick, button, stateKey, pos, delay)); } |
175 | inline void addMouseMove(QPoint pos = QPoint(), int delay=-1) |
176 | { append(t: new QTestMouseEvent(QTest::MouseMove, Qt::NoButton, Qt::KeyboardModifiers(), pos, delay)); } |
177 | #endif //QT_GUI_LIB |
178 | |
179 | inline void addDelay(int msecs) |
180 | { append(t: new QTestDelayEvent(msecs)); } |
181 | |
182 | #ifdef QT_WIDGETS_LIB |
183 | inline void simulate(QWidget *w) |
184 | { |
185 | for (int i = 0; i < size(); ++i) |
186 | at(i)->simulate(w); |
187 | } |
188 | #endif |
189 | }; |
190 | |
191 | #undef QT_ONLY_WIDGETLIB_USES |
192 | |
193 | QT_END_NAMESPACE |
194 | |
195 | Q_DECLARE_METATYPE(QTestEventList) |
196 | |
197 | #endif |
198 | |