author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 17 Sep 2010 08:34:18 +0300 | |
changeset 36 | ef0373b55136 |
parent 30 | 5dc02b23752f |
child 37 | 758a864f9613 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
||
42 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
||
45 |
||
46 |
#include <qtimer.h> |
|
47 |
#include <qthread.h> |
|
48 |
||
49 |
#if defined Q_OS_UNIX |
|
50 |
#include <unistd.h> |
|
51 |
#endif |
|
52 |
||
53 |
||
54 |
//TESTED_CLASS= |
|
55 |
//TESTED_FILES= |
|
56 |
||
57 |
class tst_QTimer : public QObject |
|
58 |
{ |
|
59 |
Q_OBJECT |
|
60 |
||
61 |
public: |
|
62 |
tst_QTimer(); |
|
63 |
virtual ~tst_QTimer(); |
|
64 |
||
65 |
||
66 |
public slots: |
|
67 |
void initTestCase(); |
|
68 |
void cleanupTestCase(); |
|
69 |
void init(); |
|
70 |
void cleanup(); |
|
71 |
private slots: |
|
72 |
void zeroTimer(); |
|
73 |
void singleShotTimeout(); |
|
74 |
void timeout(); |
|
75 |
void livelock_data(); |
|
76 |
void livelock(); |
|
77 |
void timerInfiniteRecursion_data(); |
|
78 |
void timerInfiniteRecursion(); |
|
79 |
void recurringTimer_data(); |
|
80 |
void recurringTimer(); |
|
81 |
void deleteLaterOnQTimer(); // long name, don't want to shadow QObject::deleteLater() |
|
82 |
void moveToThread(); |
|
83 |
void restartedTimerFiresTooSoon(); |
|
84 |
void timerFiresOnlyOncePerProcessEvents_data(); |
|
85 |
void timerFiresOnlyOncePerProcessEvents(); |
|
86 |
void timerIdPersistsAfterThreadExit(); |
|
87 |
void cancelLongTimer(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
88 |
void singleShotStaticFunctionZeroTimeout(); |
0 | 89 |
}; |
90 |
||
91 |
class TimerHelper : public QObject |
|
92 |
{ |
|
93 |
Q_OBJECT |
|
94 |
public: |
|
95 |
TimerHelper() : QObject(), count(0) |
|
96 |
{ |
|
97 |
} |
|
98 |
||
99 |
int count; |
|
100 |
||
101 |
public slots: |
|
102 |
void timeout(); |
|
103 |
}; |
|
104 |
||
105 |
void TimerHelper::timeout() |
|
106 |
{ |
|
107 |
++count; |
|
108 |
} |
|
109 |
||
110 |
tst_QTimer::tst_QTimer() |
|
111 |
{ |
|
112 |
} |
|
113 |
||
114 |
tst_QTimer::~tst_QTimer() |
|
115 |
{ |
|
116 |
} |
|
117 |
||
118 |
void tst_QTimer::initTestCase() |
|
119 |
{ |
|
120 |
} |
|
121 |
||
122 |
void tst_QTimer::cleanupTestCase() |
|
123 |
{ |
|
124 |
} |
|
125 |
||
126 |
void tst_QTimer::init() |
|
127 |
{ |
|
128 |
} |
|
129 |
||
130 |
void tst_QTimer::cleanup() |
|
131 |
{ |
|
132 |
} |
|
133 |
||
134 |
void tst_QTimer::zeroTimer() |
|
135 |
{ |
|
136 |
TimerHelper helper; |
|
137 |
QTimer timer; |
|
138 |
timer.setInterval(0); |
|
139 |
timer.start(); |
|
140 |
||
141 |
connect(&timer, SIGNAL(timeout()), &helper, SLOT(timeout())); |
|
142 |
||
143 |
QCoreApplication::processEvents(); |
|
144 |
||
145 |
QCOMPARE(helper.count, 1); |
|
146 |
} |
|
147 |
||
148 |
void tst_QTimer::singleShotTimeout() |
|
149 |
{ |
|
150 |
TimerHelper helper; |
|
151 |
QTimer timer; |
|
152 |
timer.setSingleShot(true); |
|
153 |
||
154 |
connect(&timer, SIGNAL(timeout()), &helper, SLOT(timeout())); |
|
155 |
timer.start(100); |
|
156 |
||
157 |
QTest::qWait(500); |
|
158 |
QCOMPARE(helper.count, 1); |
|
159 |
QTest::qWait(500); |
|
160 |
QCOMPARE(helper.count, 1); |
|
161 |
} |
|
162 |
||
163 |
#if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86) |
|
164 |
// Increase wait as emulator startup can cause unexpected delays |
|
165 |
#define TIMEOUT_TIMEOUT 2000 |
|
166 |
#else |
|
167 |
#define TIMEOUT_TIMEOUT 200 |
|
168 |
#endif |
|
169 |
||
170 |
void tst_QTimer::timeout() |
|
171 |
{ |
|
172 |
TimerHelper helper; |
|
173 |
QTimer timer; |
|
174 |
||
175 |
connect(&timer, SIGNAL(timeout()), &helper, SLOT(timeout())); |
|
176 |
timer.start(100); |
|
177 |
||
178 |
QCOMPARE(helper.count, 0); |
|
179 |
||
180 |
QTest::qWait(TIMEOUT_TIMEOUT); |
|
181 |
QVERIFY(helper.count > 0); |
|
182 |
int oldCount = helper.count; |
|
183 |
||
184 |
QTest::qWait(TIMEOUT_TIMEOUT); |
|
185 |
QVERIFY(helper.count > oldCount); |
|
186 |
} |
|
187 |
||
188 |
||
189 |
void tst_QTimer::livelock_data() |
|
190 |
{ |
|
191 |
QTest::addColumn<int>("interval"); |
|
192 |
QTest::newRow("zero timer") << 0; |
|
193 |
QTest::newRow("non-zero timer") << 1; |
|
194 |
QTest::newRow("longer than sleep") << 20; |
|
195 |
} |
|
196 |
||
197 |
/*! |
|
198 |
* |
|
199 |
* DO NOT "FIX" THIS TEST! it is written like this for a reason, do |
|
200 |
* not *change it without first dicussing it with its maintainers. |
|
201 |
* |
|
202 |
*/ |
|
203 |
class LiveLockTester : public QObject |
|
204 |
{ |
|
205 |
public: |
|
206 |
LiveLockTester(int i) |
|
207 |
: interval(i), |
|
208 |
timeoutsForFirst(0), timeoutsForExtra(0), timeoutsForSecond(0), |
|
209 |
postEventAtRightTime(false) |
|
210 |
{ |
|
211 |
firstTimerId = startTimer(interval); |
|
212 |
extraTimerId = startTimer(interval + 80); |
|
213 |
secondTimerId = -1; // started later |
|
214 |
} |
|
215 |
||
216 |
bool event(QEvent *e) { |
|
217 |
if (e->type() == 4002) { |
|
218 |
// got the posted event |
|
219 |
if (timeoutsForFirst == 1 && timeoutsForSecond == 0) |
|
220 |
postEventAtRightTime = true; |
|
221 |
return true; |
|
222 |
} |
|
223 |
return QObject::event(e); |
|
224 |
} |
|
225 |
||
226 |
void timerEvent(QTimerEvent *te) { |
|
227 |
if (te->timerId() == firstTimerId) { |
|
228 |
if (++timeoutsForFirst == 1) { |
|
229 |
killTimer(extraTimerId); |
|
230 |
extraTimerId = -1; |
|
231 |
QCoreApplication::postEvent(this, new QEvent(static_cast<QEvent::Type>(4002))); |
|
232 |
secondTimerId = startTimer(interval); |
|
233 |
} |
|
234 |
} else if (te->timerId() == secondTimerId) { |
|
235 |
++timeoutsForSecond; |
|
236 |
} else if (te->timerId() == extraTimerId) { |
|
237 |
++timeoutsForExtra; |
|
238 |
} |
|
239 |
||
240 |
// sleep for 2ms |
|
241 |
QTest::qSleep(2); |
|
242 |
killTimer(te->timerId()); |
|
243 |
} |
|
244 |
||
245 |
const int interval; |
|
246 |
int firstTimerId; |
|
247 |
int secondTimerId; |
|
248 |
int extraTimerId; |
|
249 |
int timeoutsForFirst; |
|
250 |
int timeoutsForExtra; |
|
251 |
int timeoutsForSecond; |
|
252 |
bool postEventAtRightTime; |
|
253 |
}; |
|
254 |
||
255 |
void tst_QTimer::livelock() |
|
256 |
{ |
|
257 |
/* |
|
258 |
New timers created in timer event handlers should not be sent |
|
259 |
until the next iteration of the eventloop. Note: this test |
|
260 |
depends on the fact that we send posted events before timer |
|
261 |
events (since new posted events are not sent until the next |
|
262 |
iteration of the eventloop either). |
|
263 |
*/ |
|
264 |
QFETCH(int, interval); |
|
265 |
LiveLockTester tester(interval); |
|
266 |
QTest::qWait(180); // we have to use wait here, since we're testing timers with a non-zero timeout |
|
267 |
QCOMPARE(tester.timeoutsForFirst, 1); |
|
268 |
QCOMPARE(tester.timeoutsForExtra, 0); |
|
269 |
QCOMPARE(tester.timeoutsForSecond, 1); |
|
270 |
#if defined(Q_OS_MAC) |
|
271 |
QEXPECT_FAIL("zero timer", "Posted events source are handled AFTER timers", Continue); |
|
272 |
QEXPECT_FAIL("non-zero timer", "Posted events source are handled AFTER timers", Continue); |
|
273 |
#elif defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN) |
|
274 |
QEXPECT_FAIL("zero timer", "", Continue); |
|
275 |
QEXPECT_FAIL("non-zero timer", "", Continue); |
|
276 |
#elif defined(Q_OS_WIN) && !defined(Q_OS_WINCE) |
|
277 |
if (QSysInfo::WindowsVersion < QSysInfo::WV_XP) |
|
278 |
QEXPECT_FAIL("non-zero timer", "Multimedia timers are not available on Windows 2000", Continue); |
|
279 |
#elif defined(Q_OS_WINCE) |
|
280 |
QEXPECT_FAIL("non-zero timer", "Windows CE devices often too slow", Continue); |
|
281 |
#endif |
|
282 |
QVERIFY(tester.postEventAtRightTime); |
|
283 |
} |
|
284 |
||
285 |
class TimerInfiniteRecursionObject : public QObject |
|
286 |
{ |
|
287 |
public: |
|
288 |
bool inTimerEvent; |
|
289 |
bool timerEventRecursed; |
|
290 |
int interval; |
|
291 |
||
292 |
TimerInfiniteRecursionObject(int interval) |
|
293 |
: inTimerEvent(false), timerEventRecursed(false), interval(interval) |
|
294 |
{ } |
|
295 |
||
296 |
void timerEvent(QTimerEvent *timerEvent) |
|
297 |
{ |
|
298 |
timerEventRecursed = inTimerEvent; |
|
299 |
if (timerEventRecursed) { |
|
300 |
// bug detected! |
|
301 |
return; |
|
302 |
} |
|
303 |
||
304 |
inTimerEvent = true; |
|
305 |
||
306 |
QEventLoop eventLoop; |
|
307 |
QTimer::singleShot(qMax(100, interval * 2), &eventLoop, SLOT(quit())); |
|
308 |
eventLoop.exec(); |
|
309 |
||
310 |
inTimerEvent = false; |
|
311 |
||
312 |
killTimer(timerEvent->timerId()); |
|
313 |
} |
|
314 |
}; |
|
315 |
||
316 |
void tst_QTimer::timerInfiniteRecursion_data() |
|
317 |
{ |
|
318 |
QTest::addColumn<int>("interval"); |
|
319 |
QTest::newRow("zero timer") << 0; |
|
320 |
QTest::newRow("non-zero timer") << 1; |
|
321 |
QTest::newRow("10ms timer") << 10; |
|
322 |
QTest::newRow("11ms timer") << 11; |
|
323 |
QTest::newRow("100ms timer") << 100; |
|
324 |
QTest::newRow("1s timer") << 1000; |
|
325 |
} |
|
326 |
||
327 |
||
328 |
void tst_QTimer::timerInfiniteRecursion() |
|
329 |
{ |
|
330 |
QFETCH(int, interval); |
|
331 |
TimerInfiniteRecursionObject object(interval); |
|
332 |
(void) object.startTimer(interval); |
|
333 |
||
334 |
QEventLoop eventLoop; |
|
335 |
QTimer::singleShot(qMax(100, interval * 2), &eventLoop, SLOT(quit())); |
|
336 |
eventLoop.exec(); |
|
337 |
||
338 |
QVERIFY(!object.timerEventRecursed); |
|
339 |
} |
|
340 |
||
341 |
class RecurringTimerObject : public QObject |
|
342 |
{ |
|
343 |
Q_OBJECT |
|
344 |
public: |
|
345 |
int times; |
|
346 |
int target; |
|
347 |
bool recurse; |
|
348 |
||
349 |
RecurringTimerObject(int target) |
|
350 |
: times(0), target(target), recurse(false) |
|
351 |
{ } |
|
352 |
||
353 |
void timerEvent(QTimerEvent *timerEvent) |
|
354 |
{ |
|
355 |
if (++times == target) { |
|
356 |
killTimer(timerEvent->timerId()); |
|
357 |
emit done(); |
|
358 |
} if (recurse) { |
|
359 |
QEventLoop eventLoop; |
|
360 |
QTimer::singleShot(100, &eventLoop, SLOT(quit())); |
|
361 |
eventLoop.exec(); |
|
362 |
} |
|
363 |
} |
|
364 |
||
365 |
signals: |
|
366 |
void done(); |
|
367 |
}; |
|
368 |
||
369 |
void tst_QTimer::recurringTimer_data() |
|
370 |
{ |
|
371 |
QTest::addColumn<int>("interval"); |
|
372 |
QTest::newRow("zero timer") << 0; |
|
373 |
QTest::newRow("non-zero timer") << 1; |
|
374 |
} |
|
375 |
||
376 |
void tst_QTimer::recurringTimer() |
|
377 |
{ |
|
378 |
const int target = 5; |
|
379 |
QFETCH(int, interval); |
|
380 |
||
381 |
{ |
|
382 |
RecurringTimerObject object(target); |
|
383 |
QObject::connect(&object, SIGNAL(done()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
384 |
(void) object.startTimer(interval); |
|
385 |
QTestEventLoop::instance().enterLoop(5); |
|
386 |
||
387 |
QCOMPARE(object.times, target); |
|
388 |
} |
|
389 |
||
390 |
{ |
|
391 |
// make sure that eventloop recursion doesn't effect timer recurrance |
|
392 |
RecurringTimerObject object(target); |
|
393 |
object.recurse = true; |
|
394 |
||
395 |
QObject::connect(&object, SIGNAL(done()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
396 |
(void) object.startTimer(interval); |
|
397 |
QTestEventLoop::instance().enterLoop(5); |
|
398 |
||
399 |
QCOMPARE(object.times, target); |
|
400 |
} |
|
401 |
} |
|
402 |
||
403 |
void tst_QTimer::deleteLaterOnQTimer() |
|
404 |
{ |
|
405 |
QTimer *timer = new QTimer; |
|
406 |
connect(timer, SIGNAL(timeout()), timer, SLOT(deleteLater())); |
|
407 |
connect(timer, SIGNAL(destroyed()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
408 |
timer->setInterval(1); |
|
409 |
timer->setSingleShot(true); |
|
410 |
timer->start(); |
|
411 |
QPointer<QTimer> pointer = timer; |
|
412 |
QTestEventLoop::instance().enterLoop(5); |
|
413 |
QVERIFY(pointer.isNull()); |
|
414 |
} |
|
415 |
||
416 |
#if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86) |
|
417 |
// Increase wait as emulator startup can cause unexpected delays |
|
418 |
#define MOVETOTHREAD_TIMEOUT 200 |
|
419 |
#define MOVETOTHREAD_WAIT 5000 |
|
420 |
#else |
|
421 |
#define MOVETOTHREAD_TIMEOUT 200 |
|
422 |
#define MOVETOTHREAD_WAIT 300 |
|
423 |
#endif |
|
424 |
||
425 |
void tst_QTimer::moveToThread() |
|
426 |
{ |
|
427 |
QTimer ti1; |
|
428 |
QTimer ti2; |
|
429 |
ti1.start(MOVETOTHREAD_TIMEOUT); |
|
430 |
ti2.start(MOVETOTHREAD_TIMEOUT); |
|
431 |
QVERIFY((ti1.timerId() & 0xffffff) != (ti2.timerId() & 0xffffff)); |
|
432 |
QThread tr; |
|
433 |
ti1.moveToThread(&tr); |
|
434 |
connect(&ti1,SIGNAL(timeout()), &tr, SLOT(quit())); |
|
435 |
tr.start(); |
|
436 |
QTimer ti3; |
|
437 |
ti3.start(MOVETOTHREAD_TIMEOUT); |
|
438 |
QVERIFY((ti3.timerId() & 0xffffff) != (ti2.timerId() & 0xffffff)); |
|
439 |
QVERIFY((ti3.timerId() & 0xffffff) != (ti1.timerId() & 0xffffff)); |
|
440 |
QTest::qWait(MOVETOTHREAD_WAIT); |
|
441 |
QVERIFY(tr.wait()); |
|
442 |
ti2.stop(); |
|
443 |
QTimer ti4; |
|
444 |
ti4.start(MOVETOTHREAD_TIMEOUT); |
|
445 |
ti3.stop(); |
|
446 |
ti2.start(MOVETOTHREAD_TIMEOUT); |
|
447 |
ti3.start(MOVETOTHREAD_TIMEOUT); |
|
448 |
QVERIFY((ti4.timerId() & 0xffffff) != (ti2.timerId() & 0xffffff)); |
|
449 |
QVERIFY((ti3.timerId() & 0xffffff) != (ti2.timerId() & 0xffffff)); |
|
450 |
QVERIFY((ti3.timerId() & 0xffffff) != (ti1.timerId() & 0xffffff)); |
|
451 |
} |
|
452 |
||
453 |
class RestartedTimerFiresTooSoonObject : public QObject |
|
454 |
{ |
|
455 |
Q_OBJECT |
|
456 |
||
457 |
public: |
|
458 |
QBasicTimer m_timer; |
|
459 |
||
460 |
int m_interval; |
|
461 |
QTime m_startedTime; |
|
462 |
QEventLoop eventLoop; |
|
463 |
||
464 |
inline RestartedTimerFiresTooSoonObject() |
|
465 |
: QObject(), m_interval(0) |
|
466 |
{ } |
|
467 |
||
468 |
void timerFired() |
|
469 |
{ |
|
470 |
static int interval = 1000; |
|
471 |
||
472 |
m_interval = interval; |
|
473 |
m_startedTime.start(); |
|
474 |
m_timer.start(interval, this); |
|
475 |
||
476 |
// alternate between single-shot and 1 sec |
|
477 |
interval = interval ? 0 : 1000; |
|
478 |
} |
|
479 |
||
480 |
void timerEvent(QTimerEvent* ev) |
|
481 |
{ |
|
482 |
if (ev->timerId() != m_timer.timerId()) |
|
483 |
return; |
|
484 |
||
485 |
m_timer.stop(); |
|
486 |
||
487 |
QTime now = QTime::currentTime(); |
|
488 |
int elapsed = m_startedTime.elapsed(); |
|
489 |
||
490 |
if (elapsed < m_interval / 2) { |
|
491 |
// severely too early! |
|
492 |
m_timer.stop(); |
|
493 |
eventLoop.exit(-1); |
|
494 |
return; |
|
495 |
} |
|
496 |
||
497 |
timerFired(); |
|
498 |
||
499 |
// don't do this forever |
|
500 |
static int count = 0; |
|
501 |
if (count++ > 20) { |
|
502 |
m_timer.stop(); |
|
503 |
eventLoop.quit(); |
|
504 |
return; |
|
505 |
} |
|
506 |
} |
|
507 |
}; |
|
508 |
||
509 |
void tst_QTimer::restartedTimerFiresTooSoon() |
|
510 |
{ |
|
511 |
RestartedTimerFiresTooSoonObject object; |
|
512 |
object.timerFired(); |
|
513 |
QVERIFY(object.eventLoop.exec() == 0); |
|
514 |
} |
|
515 |
||
516 |
class LongLastingSlotClass : public QObject |
|
517 |
{ |
|
518 |
Q_OBJECT |
|
519 |
||
520 |
public: |
|
521 |
LongLastingSlotClass(QTimer *timer) : count(0), timer(timer) {} |
|
522 |
||
523 |
public slots: |
|
524 |
void longLastingSlot() |
|
525 |
{ |
|
526 |
// Don't use timers for this, because we are testing them. |
|
527 |
QTime time; |
|
528 |
time.start(); |
|
529 |
while (time.elapsed() < 200) { |
|
530 |
for (int c = 0; c < 100000; c++) {} // Mindless looping. |
|
531 |
} |
|
532 |
if (++count >= 2) { |
|
533 |
timer->stop(); |
|
534 |
} |
|
535 |
} |
|
536 |
||
537 |
public: |
|
538 |
int count; |
|
539 |
QTimer *timer; |
|
540 |
}; |
|
541 |
||
542 |
void tst_QTimer::timerFiresOnlyOncePerProcessEvents_data() |
|
543 |
{ |
|
544 |
QTest::addColumn<int>("interval"); |
|
545 |
QTest::newRow("zero timer") << 0; |
|
546 |
QTest::newRow("non-zero timer") << 10; |
|
547 |
} |
|
548 |
||
549 |
void tst_QTimer::timerFiresOnlyOncePerProcessEvents() |
|
550 |
{ |
|
551 |
QFETCH(int, interval); |
|
552 |
||
553 |
QTimer t; |
|
554 |
LongLastingSlotClass longSlot(&t); |
|
555 |
t.start(interval); |
|
556 |
connect(&t, SIGNAL(timeout()), &longSlot, SLOT(longLastingSlot())); |
|
557 |
// Loop because there may be other events pending. |
|
558 |
while (longSlot.count == 0) { |
|
559 |
QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents); |
|
560 |
} |
|
561 |
||
562 |
QCOMPARE(longSlot.count, 1); |
|
563 |
} |
|
564 |
||
565 |
class TimerIdPersistsAfterThreadExitThread : public QThread |
|
566 |
{ |
|
567 |
public: |
|
568 |
QTimer *timer; |
|
569 |
int timerId, returnValue; |
|
570 |
||
571 |
TimerIdPersistsAfterThreadExitThread() |
|
572 |
: QThread(), timer(0), timerId(-1), returnValue(-1) |
|
573 |
{ } |
|
574 |
~TimerIdPersistsAfterThreadExitThread() |
|
575 |
{ |
|
576 |
delete timer; |
|
577 |
} |
|
578 |
||
579 |
void run() |
|
580 |
{ |
|
581 |
QEventLoop eventLoop; |
|
582 |
timer = new QTimer; |
|
583 |
connect(timer, SIGNAL(timeout()), &eventLoop, SLOT(quit())); |
|
584 |
timer->start(100); |
|
585 |
timerId = timer->timerId(); |
|
586 |
returnValue = eventLoop.exec(); |
|
587 |
} |
|
588 |
}; |
|
589 |
||
590 |
void tst_QTimer::timerIdPersistsAfterThreadExit() |
|
591 |
{ |
|
592 |
TimerIdPersistsAfterThreadExitThread thread; |
|
593 |
thread.start(); |
|
594 |
QVERIFY(thread.wait(30000)); |
|
595 |
QCOMPARE(thread.returnValue, 0); |
|
596 |
||
597 |
// even though the thread has exited, and the event dispatcher destroyed, the timer is still |
|
598 |
// "active", meaning the timer id should NOT be reused (i.e. the event dispatcher should not |
|
599 |
// have unregistered it) |
|
600 |
int timerId = thread.startTimer(100); |
|
601 |
QVERIFY((timerId & 0xffffff) != (thread.timerId & 0xffffff)); |
|
602 |
} |
|
603 |
||
604 |
void tst_QTimer::cancelLongTimer() |
|
605 |
{ |
|
606 |
QTimer timer; |
|
607 |
timer.setSingleShot(true); |
|
608 |
timer.start(1000 * 60 * 60); //set timer for 1 hour (which would overflow Symbian RTimer) |
|
609 |
QCoreApplication::processEvents(); |
|
610 |
QVERIFY(timer.isActive()); //if the timer completes immediately with an error, then this will fail |
|
611 |
timer.stop(); |
|
612 |
QVERIFY(!timer.isActive()); |
|
613 |
} |
|
614 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
615 |
void tst_QTimer::singleShotStaticFunctionZeroTimeout() |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
616 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
617 |
TimerHelper helper; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
618 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
619 |
QTimer::singleShot(0, &helper, SLOT(timeout())); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
620 |
QTest::qWait(500); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
621 |
QCOMPARE(helper.count, 1); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
622 |
QTest::qWait(500); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
623 |
QCOMPARE(helper.count, 1); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
624 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
625 |
|
0 | 626 |
QTEST_MAIN(tst_QTimer) |
627 |
#include "tst_qtimer.moc" |