| 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 | QT_WARNING_PUSH |
| 32 | QT_WARNING_DISABLE_CLANG("-Wweak-vtables" ) // QTBUG-1044486 |
| 33 | |
| 34 | class QTestEvent |
| 35 | { |
| 36 | public: |
| 37 | #ifdef QT_WIDGETS_LIB |
| 38 | virtual void simulate(QWidget *w) = 0; |
| 39 | #endif |
| 40 | virtual QTestEvent *clone() const = 0; |
| 41 | |
| 42 | virtual ~QTestEvent() = default; // ### FIXME: weak vtable (QTBUG-104486) |
| 43 | }; |
| 44 | |
| 45 | #ifdef QT_GUI_LIB |
| 46 | class QTestKeyEvent: public QTestEvent |
| 47 | { |
| 48 | public: |
| 49 | ~QTestKeyEvent() override = default; // ### FIXME: weak vtable (QTBUG-104486) |
| 50 | |
| 51 | inline QTestKeyEvent(QTest::KeyAction action, Qt::Key key, Qt::KeyboardModifiers modifiers, int delay) |
| 52 | : _action(action), _delay(delay), _modifiers(modifiers), _ascii(0), _key(key) {} |
| 53 | inline QTestKeyEvent(QTest::KeyAction action, char ascii, Qt::KeyboardModifiers modifiers, int delay) |
| 54 | : _action(action), _delay(delay), _modifiers(modifiers), |
| 55 | _ascii(ascii), _key(Qt::Key_unknown) {} |
| 56 | inline QTestEvent *clone() const override { return new QTestKeyEvent(*this); } |
| 57 | |
| 58 | #ifdef QT_WIDGETS_LIB |
| 59 | inline void simulate(QWidget *w) override |
| 60 | { |
| 61 | if (_ascii == 0) |
| 62 | QTest::keyEvent(_action, w, _key, _modifiers, _delay); |
| 63 | else |
| 64 | QTest::keyEvent(_action, w, _ascii, _modifiers, _delay); |
| 65 | } |
| 66 | #endif |
| 67 | |
| 68 | protected: |
| 69 | QTest::KeyAction _action; |
| 70 | int _delay; |
| 71 | Qt::KeyboardModifiers _modifiers; |
| 72 | char _ascii; |
| 73 | Qt::Key _key; |
| 74 | }; |
| 75 | |
| 76 | class QTestKeyClicksEvent: public QTestEvent |
| 77 | { |
| 78 | public: |
| 79 | ~QTestKeyClicksEvent() override = default; // ### FIXME: weak vtables (QTBUG-104486) |
| 80 | |
| 81 | inline QTestKeyClicksEvent(const QString &keys, Qt::KeyboardModifiers modifiers, int delay) |
| 82 | : _keys(keys), _modifiers(modifiers), _delay(delay) {} |
| 83 | inline QTestEvent *clone() const override { return new QTestKeyClicksEvent(*this); } |
| 84 | |
| 85 | #ifdef QT_WIDGETS_LIB |
| 86 | inline void simulate(QWidget *w) override |
| 87 | { |
| 88 | QTest::keyClicks(w, _keys, _modifiers, _delay); |
| 89 | } |
| 90 | #endif |
| 91 | |
| 92 | private: |
| 93 | QT_ONLY_WIDGETLIB_USES QString _keys; |
| 94 | QT_ONLY_WIDGETLIB_USES Qt::KeyboardModifiers _modifiers; |
| 95 | QT_ONLY_WIDGETLIB_USES int _delay; |
| 96 | }; |
| 97 | |
| 98 | class QTestMouseEvent: public QTestEvent |
| 99 | { |
| 100 | public: |
| 101 | ~QTestMouseEvent() override = default; // ### FIXME: weak vtables (QTBUG-104486) |
| 102 | |
| 103 | inline QTestMouseEvent(QTest::MouseAction action, Qt::MouseButton button, |
| 104 | Qt::KeyboardModifiers modifiers, QPoint position, int delay) |
| 105 | : _action(action), _button(button), _modifiers(modifiers), _pos(position), _delay(delay) {} |
| 106 | inline QTestEvent *clone() const override { return new QTestMouseEvent(*this); } |
| 107 | |
| 108 | #ifdef QT_WIDGETS_LIB |
| 109 | inline void simulate(QWidget *w) override |
| 110 | { |
| 111 | QTest::mouseEvent(_action, w, _button, _modifiers, _pos, _delay); |
| 112 | } |
| 113 | #endif |
| 114 | |
| 115 | private: |
| 116 | QT_ONLY_WIDGETLIB_USES QTest::MouseAction _action; |
| 117 | QT_ONLY_WIDGETLIB_USES Qt::MouseButton _button; |
| 118 | QT_ONLY_WIDGETLIB_USES Qt::KeyboardModifiers _modifiers; |
| 119 | QT_ONLY_WIDGETLIB_USES QPoint _pos; |
| 120 | QT_ONLY_WIDGETLIB_USES int _delay; |
| 121 | }; |
| 122 | #endif //QT_GUI_LIB |
| 123 | |
| 124 | |
| 125 | class QTestDelayEvent: public QTestEvent |
| 126 | { |
| 127 | public: |
| 128 | inline QTestDelayEvent(int msecs): _delay(msecs) {} |
| 129 | ~QTestDelayEvent() override = default; // ### FIXME: weak vtables (QTBUG-104486) |
| 130 | |
| 131 | inline QTestEvent *clone() const override { return new QTestDelayEvent(*this); } |
| 132 | |
| 133 | #ifdef QT_WIDGETS_LIB |
| 134 | inline void simulate(QWidget * /*w*/) override { QTest::qWait(_delay); } |
| 135 | #endif |
| 136 | |
| 137 | private: |
| 138 | QT_ONLY_WIDGETLIB_USES int _delay; |
| 139 | }; |
| 140 | |
| 141 | class QTestEventList: public QList<QTestEvent *> |
| 142 | { |
| 143 | public: |
| 144 | inline QTestEventList() {} |
| 145 | inline QTestEventList(const QTestEventList &other): QList<QTestEvent *>() |
| 146 | { for (int i = 0; i < other.size(); ++i) append(t: other.at(i)->clone()); } |
| 147 | inline ~QTestEventList() |
| 148 | { clear(); } |
| 149 | inline void clear() |
| 150 | { qDeleteAll(c: *this); QList<QTestEvent *>::clear(); } |
| 151 | |
| 152 | #ifdef QT_GUI_LIB |
| 153 | inline void addKeyClick(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
| 154 | { addKeyEvent(action: QTest::Click, qtKey, modifiers, msecs); } |
| 155 | inline void addKeyPress(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
| 156 | { addKeyEvent(action: QTest::Press, qtKey, modifiers, msecs); } |
| 157 | inline void addKeyRelease(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
| 158 | { addKeyEvent(action: QTest::Release, qtKey, modifiers, msecs); } |
| 159 | inline void addKeyEvent(QTest::KeyAction action, Qt::Key qtKey, |
| 160 | Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
| 161 | { append(t: new QTestKeyEvent(action, qtKey, modifiers, msecs)); } |
| 162 | |
| 163 | inline void addKeyClick(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
| 164 | { addKeyEvent(action: QTest::Click, ascii, modifiers, msecs); } |
| 165 | inline void addKeyPress(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
| 166 | { addKeyEvent(action: QTest::Press, ascii, modifiers, msecs); } |
| 167 | inline void addKeyRelease(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
| 168 | { addKeyEvent(action: QTest::Release, ascii, modifiers, msecs); } |
| 169 | inline void addKeyClicks(const QString &keys, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
| 170 | { append(t: new QTestKeyClicksEvent(keys, modifiers, msecs)); } |
| 171 | inline void addKeyEvent(QTest::KeyAction action, char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
| 172 | { append(t: new QTestKeyEvent(action, ascii, modifiers, msecs)); } |
| 173 | |
| 174 | inline void addMousePress(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), |
| 175 | QPoint pos = QPoint(), int delay=-1) |
| 176 | { append(t: new QTestMouseEvent(QTest::MousePress, button, stateKey, pos, delay)); } |
| 177 | inline void addMouseRelease(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), |
| 178 | QPoint pos = QPoint(), int delay=-1) |
| 179 | { append(t: new QTestMouseEvent(QTest::MouseRelease, button, stateKey, pos, delay)); } |
| 180 | inline void addMouseClick(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), |
| 181 | QPoint pos = QPoint(), int delay=-1) |
| 182 | { append(t: new QTestMouseEvent(QTest::MouseClick, button, stateKey, pos, delay)); } |
| 183 | inline void addMouseDClick(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), |
| 184 | QPoint pos = QPoint(), int delay=-1) |
| 185 | { append(t: new QTestMouseEvent(QTest::MouseDClick, button, stateKey, pos, delay)); } |
| 186 | inline void addMouseMove(QPoint pos = QPoint(), int delay=-1) |
| 187 | { append(t: new QTestMouseEvent(QTest::MouseMove, Qt::NoButton, Qt::KeyboardModifiers(), pos, delay)); } |
| 188 | #endif //QT_GUI_LIB |
| 189 | |
| 190 | inline void addDelay(int msecs) |
| 191 | { append(t: new QTestDelayEvent(msecs)); } |
| 192 | |
| 193 | #ifdef QT_WIDGETS_LIB |
| 194 | inline void simulate(QWidget *w) |
| 195 | { |
| 196 | for (int i = 0; i < size(); ++i) |
| 197 | at(i)->simulate(w); |
| 198 | } |
| 199 | #endif |
| 200 | }; |
| 201 | |
| 202 | #undef QT_ONLY_WIDGETLIB_USES |
| 203 | |
| 204 | QT_WARNING_POP // Clang -Wweak-vtables |
| 205 | |
| 206 | QT_END_NAMESPACE |
| 207 | |
| 208 | Q_DECLARE_METATYPE(QTestEventList) |
| 209 | |
| 210 | #endif |
| 211 | |