|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 #include <QtTest/QSignalSpy> |
|
42 #include <qtest.h> |
|
43 #include <QtDeclarative/qdeclarativeengine.h> |
|
44 #include <QtDeclarative/qdeclarativecomponent.h> |
|
45 #include <private/qdeclarativetimer_p.h> |
|
46 #include <QtDeclarative/qdeclarativeitem.h> |
|
47 #include <QDebug> |
|
48 |
|
49 class tst_qdeclarativetimer : public QObject |
|
50 { |
|
51 Q_OBJECT |
|
52 public: |
|
53 tst_qdeclarativetimer(); |
|
54 |
|
55 private slots: |
|
56 void notRepeating(); |
|
57 void notRepeatingStart(); |
|
58 void repeat(); |
|
59 void noTriggerIfNotRunning(); |
|
60 void triggeredOnStart(); |
|
61 void triggeredOnStartRepeat(); |
|
62 void changeDuration(); |
|
63 void restart(); |
|
64 void parentProperty(); |
|
65 }; |
|
66 |
|
67 class TimerHelper : public QObject |
|
68 { |
|
69 Q_OBJECT |
|
70 public: |
|
71 TimerHelper() : QObject(), count(0) |
|
72 { |
|
73 } |
|
74 |
|
75 int count; |
|
76 |
|
77 public slots: |
|
78 void timeout() { |
|
79 ++count; |
|
80 } |
|
81 }; |
|
82 |
|
83 #if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86) |
|
84 // Increase wait as emulator startup can cause unexpected delays |
|
85 #define TIMEOUT_TIMEOUT 2000 |
|
86 #else |
|
87 #define TIMEOUT_TIMEOUT 200 |
|
88 #endif |
|
89 |
|
90 tst_qdeclarativetimer::tst_qdeclarativetimer() |
|
91 { |
|
92 } |
|
93 |
|
94 void tst_qdeclarativetimer::notRepeating() |
|
95 { |
|
96 QDeclarativeEngine engine; |
|
97 QDeclarativeComponent component(&engine); |
|
98 component.setData(QByteArray("import Qt 4.7\nTimer { interval: 100; running: true }"), QUrl::fromLocalFile("")); |
|
99 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create()); |
|
100 QVERIFY(timer != 0); |
|
101 QVERIFY(timer->isRunning()); |
|
102 QVERIFY(!timer->isRepeating()); |
|
103 QCOMPARE(timer->interval(), 100); |
|
104 |
|
105 TimerHelper helper; |
|
106 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout())); |
|
107 |
|
108 QTest::qWait(TIMEOUT_TIMEOUT); |
|
109 QCOMPARE(helper.count, 1); |
|
110 QTest::qWait(TIMEOUT_TIMEOUT); |
|
111 QCOMPARE(helper.count, 1); |
|
112 QVERIFY(timer->isRunning() == false); |
|
113 } |
|
114 |
|
115 void tst_qdeclarativetimer::notRepeatingStart() |
|
116 { |
|
117 QDeclarativeEngine engine; |
|
118 QDeclarativeComponent component(&engine); |
|
119 component.setData(QByteArray("import Qt 4.7\nTimer { interval: 100 }"), QUrl::fromLocalFile("")); |
|
120 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create()); |
|
121 QVERIFY(timer != 0); |
|
122 QVERIFY(!timer->isRunning()); |
|
123 |
|
124 TimerHelper helper; |
|
125 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout())); |
|
126 |
|
127 QTest::qWait(TIMEOUT_TIMEOUT); |
|
128 QCOMPARE(helper.count, 0); |
|
129 |
|
130 timer->start(); |
|
131 QTest::qWait(TIMEOUT_TIMEOUT); |
|
132 QCOMPARE(helper.count, 1); |
|
133 QTest::qWait(TIMEOUT_TIMEOUT); |
|
134 QCOMPARE(helper.count, 1); |
|
135 QVERIFY(timer->isRunning() == false); |
|
136 |
|
137 delete timer; |
|
138 } |
|
139 |
|
140 void tst_qdeclarativetimer::repeat() |
|
141 { |
|
142 QDeclarativeEngine engine; |
|
143 QDeclarativeComponent component(&engine); |
|
144 component.setData(QByteArray("import Qt 4.7\nTimer { interval: 100; repeat: true; running: true }"), QUrl::fromLocalFile("")); |
|
145 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create()); |
|
146 QVERIFY(timer != 0); |
|
147 |
|
148 TimerHelper helper; |
|
149 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout())); |
|
150 QCOMPARE(helper.count, 0); |
|
151 |
|
152 QTest::qWait(TIMEOUT_TIMEOUT); |
|
153 QVERIFY(helper.count > 0); |
|
154 int oldCount = helper.count; |
|
155 |
|
156 QTest::qWait(TIMEOUT_TIMEOUT); |
|
157 QVERIFY(helper.count > oldCount); |
|
158 QVERIFY(timer->isRunning()); |
|
159 |
|
160 oldCount = helper.count; |
|
161 timer->stop(); |
|
162 |
|
163 QTest::qWait(TIMEOUT_TIMEOUT); |
|
164 QVERIFY(helper.count == oldCount); |
|
165 QVERIFY(timer->isRunning() == false); |
|
166 |
|
167 QSignalSpy spy(timer, SIGNAL(repeatChanged())); |
|
168 |
|
169 timer->setRepeating(false); |
|
170 QVERIFY(!timer->isRepeating()); |
|
171 QCOMPARE(spy.count(),1); |
|
172 |
|
173 timer->setRepeating(false); |
|
174 QCOMPARE(spy.count(),1); |
|
175 |
|
176 timer->setRepeating(true); |
|
177 QCOMPARE(spy.count(),2); |
|
178 |
|
179 delete timer; |
|
180 } |
|
181 |
|
182 void tst_qdeclarativetimer::triggeredOnStart() |
|
183 { |
|
184 QDeclarativeEngine engine; |
|
185 QDeclarativeComponent component(&engine); |
|
186 component.setData(QByteArray("import Qt 4.7\nTimer { interval: 100; running: true; triggeredOnStart: true }"), QUrl::fromLocalFile("")); |
|
187 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create()); |
|
188 QVERIFY(timer != 0); |
|
189 QVERIFY(timer->triggeredOnStart()); |
|
190 |
|
191 TimerHelper helper; |
|
192 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout())); |
|
193 QTest::qWait(1); |
|
194 QCOMPARE(helper.count, 1); |
|
195 |
|
196 QTest::qWait(TIMEOUT_TIMEOUT); |
|
197 QCOMPARE(helper.count, 2); |
|
198 QTest::qWait(TIMEOUT_TIMEOUT); |
|
199 QCOMPARE(helper.count, 2); |
|
200 QVERIFY(timer->isRunning() == false); |
|
201 |
|
202 QSignalSpy spy(timer, SIGNAL(triggeredOnStartChanged())); |
|
203 |
|
204 timer->setTriggeredOnStart(false); |
|
205 QVERIFY(!timer->triggeredOnStart()); |
|
206 QCOMPARE(spy.count(),1); |
|
207 |
|
208 timer->setTriggeredOnStart(false); |
|
209 QCOMPARE(spy.count(),1); |
|
210 |
|
211 timer->setTriggeredOnStart(true); |
|
212 QCOMPARE(spy.count(),2); |
|
213 |
|
214 delete timer; |
|
215 } |
|
216 |
|
217 void tst_qdeclarativetimer::triggeredOnStartRepeat() |
|
218 { |
|
219 QDeclarativeEngine engine; |
|
220 QDeclarativeComponent component(&engine); |
|
221 component.setData(QByteArray("import Qt 4.7\nTimer { interval: 100; running: true; triggeredOnStart: true; repeat: true }"), QUrl::fromLocalFile("")); |
|
222 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create()); |
|
223 QVERIFY(timer != 0); |
|
224 |
|
225 TimerHelper helper; |
|
226 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout())); |
|
227 QTest::qWait(1); |
|
228 QCOMPARE(helper.count, 1); |
|
229 |
|
230 QTest::qWait(TIMEOUT_TIMEOUT); |
|
231 QVERIFY(helper.count > 1); |
|
232 int oldCount = helper.count; |
|
233 QTest::qWait(TIMEOUT_TIMEOUT); |
|
234 QVERIFY(helper.count > oldCount); |
|
235 QVERIFY(timer->isRunning()); |
|
236 |
|
237 delete timer; |
|
238 } |
|
239 |
|
240 void tst_qdeclarativetimer::noTriggerIfNotRunning() |
|
241 { |
|
242 QDeclarativeEngine engine; |
|
243 QDeclarativeComponent component(&engine); |
|
244 component.setData(QByteArray( |
|
245 "import Qt 4.7\n" |
|
246 "Item { property bool ok: true\n" |
|
247 "Timer { id: t1; interval: 100; repeat: true; running: true; onTriggered: if (!running) ok=false }" |
|
248 "Timer { interval: 10; running: true; onTriggered: t1.running=false }" |
|
249 "}" |
|
250 ), QUrl::fromLocalFile("")); |
|
251 QObject *item = component.create(); |
|
252 QVERIFY(item != 0); |
|
253 QTest::qWait(TIMEOUT_TIMEOUT); |
|
254 QCOMPARE(item->property("ok").toBool(), true); |
|
255 |
|
256 delete item; |
|
257 } |
|
258 |
|
259 void tst_qdeclarativetimer::changeDuration() |
|
260 { |
|
261 QDeclarativeEngine engine; |
|
262 QDeclarativeComponent component(&engine); |
|
263 component.setData(QByteArray("import Qt 4.7\nTimer { interval: 200; repeat: true; running: true }"), QUrl::fromLocalFile("")); |
|
264 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create()); |
|
265 QVERIFY(timer != 0); |
|
266 |
|
267 TimerHelper helper; |
|
268 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout())); |
|
269 QCOMPARE(helper.count, 0); |
|
270 |
|
271 QTest::qWait(500); |
|
272 QCOMPARE(helper.count, 2); |
|
273 |
|
274 timer->setInterval(500); |
|
275 |
|
276 QTest::qWait(600); |
|
277 QCOMPARE(helper.count, 3); |
|
278 QVERIFY(timer->isRunning()); |
|
279 |
|
280 QSignalSpy spy(timer, SIGNAL(intervalChanged())); |
|
281 |
|
282 timer->setInterval(200); |
|
283 QCOMPARE(timer->interval(), 200); |
|
284 QCOMPARE(spy.count(),1); |
|
285 |
|
286 timer->setInterval(200); |
|
287 QCOMPARE(spy.count(),1); |
|
288 |
|
289 timer->setInterval(300); |
|
290 QCOMPARE(spy.count(),2); |
|
291 |
|
292 delete timer; |
|
293 } |
|
294 |
|
295 void tst_qdeclarativetimer::restart() |
|
296 { |
|
297 QDeclarativeEngine engine; |
|
298 QDeclarativeComponent component(&engine); |
|
299 component.setData(QByteArray("import Qt 4.7\nTimer { interval: 500; repeat: true; running: true }"), QUrl::fromLocalFile("")); |
|
300 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create()); |
|
301 QVERIFY(timer != 0); |
|
302 |
|
303 TimerHelper helper; |
|
304 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout())); |
|
305 QCOMPARE(helper.count, 0); |
|
306 |
|
307 QTest::qWait(600); |
|
308 QCOMPARE(helper.count, 1); |
|
309 |
|
310 QTest::qWait(300); |
|
311 |
|
312 timer->restart(); |
|
313 |
|
314 QTest::qWait(700); |
|
315 |
|
316 QCOMPARE(helper.count, 2); |
|
317 QVERIFY(timer->isRunning()); |
|
318 |
|
319 delete timer; |
|
320 } |
|
321 |
|
322 void tst_qdeclarativetimer::parentProperty() |
|
323 { |
|
324 QDeclarativeEngine engine; |
|
325 QDeclarativeComponent component(&engine); |
|
326 component.setData(QByteArray("import Qt 4.7\nItem { Timer { objectName: \"timer\"; running: parent.visible } }"), QUrl::fromLocalFile("")); |
|
327 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create()); |
|
328 QVERIFY(item != 0); |
|
329 QDeclarativeTimer *timer = item->findChild<QDeclarativeTimer*>("timer"); |
|
330 QVERIFY(timer != 0); |
|
331 |
|
332 QVERIFY(timer->isRunning()); |
|
333 |
|
334 delete timer; |
|
335 } |
|
336 |
|
337 QTEST_MAIN(tst_qdeclarativetimer) |
|
338 |
|
339 #include "tst_qdeclarativetimer.moc" |