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