Qt 4.8
qtestevent.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 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.
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 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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QTESTEVENT_H
43 #define QTESTEVENT_H
44 
45 #if 0
46 // inform syncqt
47 #pragma qt_no_master_include
48 #endif
49 
50 #include <QtTest/qtest_global.h>
51 #ifdef QT_GUI_LIB
52 #include <QtTest/qtestkeyboard.h>
53 #include <QtTest/qtestmouse.h>
54 #endif
55 #include <QtTest/qtestsystem.h>
56 
57 #include <QtCore/qlist.h>
58 
59 #include <stdlib.h>
60 
62 
64 
65 QT_MODULE(Test)
66 
68 {
69 public:
70  virtual void simulate(QWidget *w) = 0;
71  virtual QTestEvent *clone() const = 0;
72 
73  virtual ~QTestEvent() {}
74 };
75 
76 #ifdef QT_GUI_LIB
77 class QTestKeyEvent: public QTestEvent
78 {
79 public:
80  inline QTestKeyEvent(QTest::KeyAction action, Qt::Key key, Qt::KeyboardModifiers modifiers, int delay)
81  : _action(action), _delay(delay), _modifiers(modifiers), _ascii(0), _key(key) {}
82  inline QTestKeyEvent(QTest::KeyAction action, char ascii, Qt::KeyboardModifiers modifiers, int delay)
83  : _action(action), _delay(delay), _modifiers(modifiers),
84  _ascii(ascii), _key(Qt::Key_unknown) {}
85  inline QTestEvent *clone() const { return new QTestKeyEvent(*this); }
86 
87  inline void simulate(QWidget *w)
88  {
89  if (_ascii == 0)
90  QTest::keyEvent(_action, w, _key, _modifiers, _delay);
91  else
92  QTest::keyEvent(_action, w, _ascii, _modifiers, _delay);
93  }
94 
95 protected:
96  QTest::KeyAction _action;
97  int _delay;
98  Qt::KeyboardModifiers _modifiers;
99  char _ascii;
100  Qt::Key _key;
101 };
102 
103 class QTestKeyClicksEvent: public QTestEvent
104 {
105 public:
106  inline QTestKeyClicksEvent(const QString &keys, Qt::KeyboardModifiers modifiers, int delay)
107  : _keys(keys), _modifiers(modifiers), _delay(delay) {}
108  inline QTestEvent *clone() const { return new QTestKeyClicksEvent(*this); }
109 
110  inline void simulate(QWidget *w)
111  {
112  QTest::keyClicks(w, _keys, _modifiers, _delay);
113  }
114 
115 private:
116  QString _keys;
117  Qt::KeyboardModifiers _modifiers;
118  int _delay;
119 };
120 
121 class QTestMouseEvent: public QTestEvent
122 {
123 public:
124  inline QTestMouseEvent(QTest::MouseAction action, Qt::MouseButton button,
125  Qt::KeyboardModifiers modifiers, QPoint position, int delay)
126  : _action(action), _button(button), _modifiers(modifiers), _pos(position), _delay(delay) {}
127  inline QTestEvent *clone() const { return new QTestMouseEvent(*this); }
128 
129  inline void simulate(QWidget *w)
130  {
131  QTest::mouseEvent(_action, w, _button, _modifiers, _pos, _delay);
132  }
133 
134 private:
135  QTest::MouseAction _action;
136  Qt::MouseButton _button;
137  Qt::KeyboardModifiers _modifiers;
138  QPoint _pos;
139  int _delay;
140 };
141 #endif //QT_GUI_LIB
142 
143 
145 {
146 public:
147  inline QTestDelayEvent(int msecs): _delay(msecs) {}
148  inline QTestEvent *clone() const { return new QTestDelayEvent(*this); }
149 
150  inline void simulate(QWidget * /*w*/) { QTest::qWait(_delay); }
151 
152 private:
153  int _delay;
154 };
155 
156 class QTestEventList: public QList<QTestEvent *>
157 {
158 public:
159  inline QTestEventList() {}
160  inline QTestEventList(const QTestEventList &other): QList<QTestEvent *>()
161  { for (int i = 0; i < other.count(); ++i) append(other.at(i)->clone()); }
163  { clear(); }
164  inline void clear()
166 
167 #ifdef QT_GUI_LIB
168  inline void addKeyClick(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
169  { addKeyEvent(QTest::Click, qtKey, modifiers, msecs); }
170  inline void addKeyPress(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
171  { addKeyEvent(QTest::Press, qtKey, modifiers, msecs); }
172  inline void addKeyRelease(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
173  { addKeyEvent(QTest::Release, qtKey, modifiers, msecs); }
174  inline void addKeyEvent(QTest::KeyAction action, Qt::Key qtKey,
175  Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
176  { append(new QTestKeyEvent(action, qtKey, modifiers, msecs)); }
177 
178  inline void addKeyClick(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
179  { addKeyEvent(QTest::Click, ascii, modifiers, msecs); }
180  inline void addKeyPress(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
181  { addKeyEvent(QTest::Press, ascii, modifiers, msecs); }
182  inline void addKeyRelease(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
183  { addKeyEvent(QTest::Release, ascii, modifiers, msecs); }
184  inline void addKeyClicks(const QString &keys, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
185  { append(new QTestKeyClicksEvent(keys, modifiers, msecs)); }
186  inline void addKeyEvent(QTest::KeyAction action, char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
187  { append(new QTestKeyEvent(action, ascii, modifiers, msecs)); }
188 
189  inline void addMousePress(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
190  QPoint pos = QPoint(), int delay=-1)
191  { append(new QTestMouseEvent(QTest::MousePress, button, stateKey, pos, delay)); }
192  inline void addMouseRelease(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
193  QPoint pos = QPoint(), int delay=-1)
194  { append(new QTestMouseEvent(QTest::MouseRelease, button, stateKey, pos, delay)); }
195  inline void addMouseClick(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
196  QPoint pos = QPoint(), int delay=-1)
197  { append(new QTestMouseEvent(QTest::MouseClick, button, stateKey, pos, delay)); }
198  inline void addMouseDClick(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
199  QPoint pos = QPoint(), int delay=-1)
200  { append(new QTestMouseEvent(QTest::MouseDClick, button, stateKey, pos, delay)); }
201  inline void addMouseMove(QPoint pos = QPoint(), int delay=-1)
202  { append(new QTestMouseEvent(QTest::MouseMove, Qt::NoButton, 0, pos, delay)); }
203 #endif //QT_GUI_LIB
204 
205  inline void addDelay(int msecs)
206  { append(new QTestDelayEvent(msecs)); }
207 
208  inline void simulate(QWidget *w)
209  {
210  for (int i = 0; i < count(); ++i)
211  at(i)->simulate(w);
212  }
213 };
214 
216 
218 
220 
221 #endif
void simulate(QWidget *w)
Definition: qtestevent.h:208
MouseAction
This enum describes possible actions for mouse handling.
Definition: qtestmouse.h:69
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define QT_MODULE(x)
Definition: qglobal.h:2783
static void keyEvent(KeyAction action, QWidget *widget, char ascii, Qt::KeyboardModifiers modifier=Qt::NoModifier, int delay=-1)
static void keyClicks(QWidget *widget, const QString &sequence, Qt::KeyboardModifiers modifier=Qt::NoModifier, int delay=-1)
Simulates clicking a sequence of keys on a widget.
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
#define at(className, varName)
QTestEvent * clone() const
Definition: qtestevent.h:148
static qreal position(QGraphicsObject *item, QDeclarativeAnchorLine::AnchorLine anchorLine)
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
static void clear(QVariant::Private *d)
Definition: qvariant.cpp:197
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
QTestDelayEvent(int msecs)
Definition: qtestevent.h:147
QStringList keys
virtual void simulate(QWidget *w)=0
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QTestEventList(const QTestEventList &other)
Definition: qtestevent.h:160
void clear()
Removes all items from the list.
Definition: qlist.h:764
void simulate(QWidget *)
Definition: qtestevent.h:150
#define Q_DECLARE_METATYPE(TYPE)
This macro makes the type Type known to QMetaType as long as it provides a public default constructor...
Definition: qmetatype.h:265
static void qWait(int ms)
Waits for ms milliseconds.
Definition: qtestsystem.h:62
int key
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
static void mouseEvent(MouseAction action, QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
Definition: qtestmouse.h:71
virtual ~QTestEvent()
Definition: qtestevent.h:73
KeyAction
This enum describes possible actions for key handling.
Definition: qtestkeyboard.h:68
void addDelay(int msecs)
Definition: qtestevent.h:205
static QString qtKey(CFStringRef cfkey)
#define class
#define QT_END_HEADER
Definition: qglobal.h:137
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
static void clone(QMetaObjectBuilder &builder, const QMetaObject *mo, const QMetaObject *ignoreStart, const QMetaObject *ignoreEnd)
virtual QTestEvent * clone() const =0
MouseButton
Definition: qnamespace.h:150