|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #ifndef HBAUTOTEST_H |
|
19 #define HBAUTOTEST_H |
|
20 |
|
21 #include <hbnamespace.h> |
|
22 #include <hbmainwindow.h> |
|
23 #include <QtTest/QtTest> |
|
24 |
|
25 class HbMainWindow; |
|
26 class HbWidget; |
|
27 class HbAutoTestMainWindow; |
|
28 class HbAutoTest; |
|
29 class HbAutoTestMouseEvent; |
|
30 /* |
|
31 INSTRUCTIONS: |
|
32 The class HbAutoTest is meant to be used with Orbit applications auto testing instead of GUI testing APIs of QTestLib. |
|
33 |
|
34 The functions of this class is to used similarily to the related QTestLib functions. |
|
35 |
|
36 Use HbAutoTestMainWindow (defined below) instead of HbMainWindow to enble filtering. |
|
37 Filterin filters out UI events that are not sent by function defined in HbAutoTest class. |
|
38 |
|
39 */ |
|
40 |
|
41 class HbAutoTestMouseEvent : public QMouseEvent |
|
42 { |
|
43 public: |
|
44 HbAutoTestMouseEvent(Type type, const QPoint & pos, const QPoint & globalPos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers ) |
|
45 : QMouseEvent(type,pos,globalPos,button,buttons,modifiers){} |
|
46 }; |
|
47 |
|
48 class HbAutoTestKeyEvent : public QKeyEvent |
|
49 { |
|
50 public: |
|
51 HbAutoTestKeyEvent(Type type, int key, Qt::KeyboardModifiers modifiers, const QString& text = QString(), |
|
52 bool autorep = false, ushort count = 1 ) |
|
53 : QKeyEvent(type, key, modifiers, text, autorep, count){} |
|
54 }; |
|
55 |
|
56 class HbAutoTest |
|
57 { |
|
58 public: |
|
59 |
|
60 static void mouseMove (HbAutoTestMainWindow *window, HbWidget *widget, QPointF pos = QPointF(), int delay = -1 ); |
|
61 static void mousePress (HbAutoTestMainWindow *window, HbWidget *widget, QPointF pos = QPointF(), int delay = -1); |
|
62 static void mouseRelease (HbAutoTestMainWindow *window, HbWidget *widget, QPointF pos = QPointF(), int delay = -1); |
|
63 static void mouseClick (HbAutoTestMainWindow *window, const HbWidget *widget, QPointF pos = QPointF(), int delay = -1); |
|
64 |
|
65 private: |
|
66 static void drag(HbAutoTestMainWindow *window, QPointF targetPoint); |
|
67 static QPointF middlePointOfWidget( const HbWidget* widget); |
|
68 |
|
69 static bool pointerPressed; |
|
70 static QPointF pressPoint; |
|
71 |
|
72 //Key event Part: copy-pasted from QTestLib and modified to support HbAutoTestKeyEvent to enable filtering. |
|
73 //see HbAutoTestMainWindow below. |
|
74 public: |
|
75 |
|
76 static Qt::Key asciiToKey(char ascii); |
|
77 static char keyToAscii(Qt::Key key); |
|
78 |
|
79 static void simulateEvent(QWidget *widget, bool press, int code, |
|
80 Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1); |
|
81 |
|
82 static void sendKeyEvent(QTest::KeyAction action, QWidget *widget, Qt::Key code, |
|
83 QString text, Qt::KeyboardModifiers modifier, int delay=-1); |
|
84 |
|
85 static void sendKeyEvent(QTest::KeyAction action, QWidget *widget, Qt::Key code, |
|
86 char ascii, Qt::KeyboardModifiers modifier, int delay=-1); |
|
87 |
|
88 static void keyEvent(QTest::KeyAction action, QWidget *widget, char ascii, |
|
89 Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1); |
|
90 |
|
91 static void keyEvent(QTest::KeyAction action, QWidget *widget, Qt::Key key, |
|
92 Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1); |
|
93 |
|
94 static void keyClicks(QWidget *widget, const QString &sequence, |
|
95 Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1); |
|
96 |
|
97 static void keyPress(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1); |
|
98 |
|
99 static void keyRelease(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1); |
|
100 |
|
101 static void keyClick(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1); |
|
102 |
|
103 static void keyPress(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1); |
|
104 |
|
105 static void keyRelease(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1); |
|
106 |
|
107 static void keyClick(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1); |
|
108 }; |
|
109 |
|
110 class HbTestEvent |
|
111 { |
|
112 public: |
|
113 virtual void simulate(QWidget *w) = 0; |
|
114 virtual HbTestEvent *clone() const = 0; |
|
115 |
|
116 virtual ~HbTestEvent() {} |
|
117 }; |
|
118 |
|
119 class HbTestKeyEvent: public HbTestEvent |
|
120 { |
|
121 public: |
|
122 inline HbTestKeyEvent(QTest::KeyAction action, Qt::Key key, Qt::KeyboardModifiers modifiers, int delay) |
|
123 : _action(action), _delay(delay), _modifiers(modifiers), _ascii(0), _key(key) {} |
|
124 inline HbTestKeyEvent(QTest::KeyAction action, char ascii, Qt::KeyboardModifiers modifiers, int delay) |
|
125 : _action(action), _delay(delay), _modifiers(modifiers), |
|
126 _ascii(ascii), _key(Qt::Key_unknown) {} |
|
127 inline HbTestEvent *clone() const { return new HbTestKeyEvent(*this); } |
|
128 |
|
129 inline void simulate(QWidget *w) |
|
130 { |
|
131 if (_ascii == 0) |
|
132 HbAutoTest::keyEvent(_action, w, _key, _modifiers, _delay); |
|
133 else |
|
134 HbAutoTest::keyEvent(_action, w, _ascii, _modifiers, _delay); |
|
135 } |
|
136 |
|
137 protected: |
|
138 QTest::KeyAction _action; |
|
139 int _delay; |
|
140 Qt::KeyboardModifiers _modifiers; |
|
141 char _ascii; |
|
142 Qt::Key _key; |
|
143 }; |
|
144 |
|
145 class HbTestKeyClicksEvent: public HbTestEvent |
|
146 { |
|
147 public: |
|
148 inline HbTestKeyClicksEvent(const QString &keys, Qt::KeyboardModifiers modifiers, int delay) |
|
149 : _keys(keys), _modifiers(modifiers), _delay(delay) {} |
|
150 inline HbTestEvent *clone() const { return new HbTestKeyClicksEvent(*this); } |
|
151 |
|
152 inline void simulate(QWidget *w) |
|
153 { |
|
154 HbAutoTest::keyClicks(w, _keys, _modifiers, _delay); |
|
155 } |
|
156 |
|
157 private: |
|
158 QString _keys; |
|
159 Qt::KeyboardModifiers _modifiers; |
|
160 int _delay; |
|
161 }; |
|
162 |
|
163 class HbTestDelayEvent: public HbTestEvent |
|
164 { |
|
165 public: |
|
166 inline HbTestDelayEvent(int msecs): _delay(msecs) {} |
|
167 inline HbTestEvent *clone() const { return new HbTestDelayEvent(*this); } |
|
168 |
|
169 inline void simulate(QWidget * /*w*/) { QTest::qWait(_delay); } |
|
170 |
|
171 private: |
|
172 int _delay; |
|
173 }; |
|
174 |
|
175 class HbTestEventList: public QList<HbTestEvent *> |
|
176 { |
|
177 public: |
|
178 inline HbTestEventList() {} |
|
179 inline HbTestEventList(const HbTestEventList &other): QList<HbTestEvent *>() |
|
180 { for (int i = 0; i < other.count(); ++i) append(other.at(i)->clone()); } |
|
181 inline ~HbTestEventList() |
|
182 { clear(); } |
|
183 inline void clear() |
|
184 { qDeleteAll(*this); QList<HbTestEvent *>::clear(); } |
|
185 |
|
186 inline void addKeyClick(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
|
187 { addKeyEvent(QTest::Click, qtKey, modifiers, msecs); } |
|
188 inline void addKeyPress(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
|
189 { addKeyEvent(QTest::Press, qtKey, modifiers, msecs); } |
|
190 inline void addKeyRelease(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
|
191 { addKeyEvent(QTest::Release, qtKey, modifiers, msecs); } |
|
192 inline void addKeyEvent(QTest::KeyAction action, Qt::Key qtKey, |
|
193 Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
|
194 { append(new HbTestKeyEvent(action, qtKey, modifiers, msecs)); } |
|
195 |
|
196 inline void addKeyClick(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
|
197 { addKeyEvent(QTest::Click, ascii, modifiers, msecs); } |
|
198 inline void addKeyPress(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
|
199 { addKeyEvent(QTest::Press, ascii, modifiers, msecs); } |
|
200 inline void addKeyRelease(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
|
201 { addKeyEvent(QTest::Release, ascii, modifiers, msecs); } |
|
202 inline void addKeyClicks(const QString &keys, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
|
203 { append(new HbTestKeyClicksEvent(keys, modifiers, msecs)); } |
|
204 inline void addKeyEvent(QTest::KeyAction action, char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1) |
|
205 { append(new HbTestKeyEvent(action, ascii, modifiers, msecs)); } |
|
206 |
|
207 inline void addDelay(int msecs) |
|
208 { append(new HbTestDelayEvent(msecs)); } |
|
209 |
|
210 inline void simulate(QWidget *w) |
|
211 { |
|
212 for (int i = 0; i < count(); ++i) |
|
213 at(i)->simulate(w); |
|
214 } |
|
215 }; |
|
216 |
|
217 class HbAutoTestMainWindow : public HbMainWindow |
|
218 { |
|
219 public: |
|
220 HbAutoTestMainWindow() : HbMainWindow() {} |
|
221 |
|
222 void mousePressEvent(QMouseEvent *event) |
|
223 { |
|
224 qDebug( |
|
225 "HbAutoTestMainWindow::mousePressEvent: x(%d) y(%d)", |
|
226 event->x(), |
|
227 event->y()); |
|
228 if ( dynamic_cast<HbAutoTestMouseEvent *>(event) ) { |
|
229 HbMainWindow::mousePressEvent(event); |
|
230 } else { |
|
231 ;//Do nothing |
|
232 } |
|
233 } |
|
234 |
|
235 void mouseMoveEvent(QMouseEvent *event) |
|
236 { |
|
237 if ( dynamic_cast<HbAutoTestMouseEvent *>(event) ) { |
|
238 HbMainWindow::mouseMoveEvent(event); |
|
239 } else { |
|
240 ;//Do nothing |
|
241 } |
|
242 } |
|
243 |
|
244 void mouseReleaseEvent(QMouseEvent *event) |
|
245 { |
|
246 if ( dynamic_cast<HbAutoTestMouseEvent *>(event) ) { |
|
247 HbMainWindow::mouseReleaseEvent(event); |
|
248 } else { |
|
249 ;//Do nothing |
|
250 } |
|
251 } |
|
252 |
|
253 void keyPressEvent(QKeyEvent *event) |
|
254 { |
|
255 if ( dynamic_cast<HbAutoTestKeyEvent *>(event) ) { |
|
256 HbMainWindow::keyPressEvent(event); |
|
257 } else { |
|
258 ;//Do nothing |
|
259 } |
|
260 } |
|
261 void keyReleaseEvent(QKeyEvent *event) |
|
262 { |
|
263 if ( dynamic_cast<HbAutoTestKeyEvent *>(event) ) { |
|
264 HbMainWindow::keyReleaseEvent(event); |
|
265 } else { |
|
266 ;//Do nothing |
|
267 } |
|
268 } |
|
269 |
|
270 void mouseDoubleClickEvent(QMouseEvent *event) |
|
271 { |
|
272 Q_UNUSED(event); |
|
273 //Just ignore, not supported in Orbit |
|
274 } |
|
275 }; |
|
276 |
|
277 |
|
278 Q_DECLARE_METATYPE(HbTestEventList) |
|
279 #endif //HBAUTOTEST_H |