From 1a1f4a8b3c8850d7abe4dda5b85b850094cf7a6a Mon Sep 17 00:00:00 2001 From: axis Date: Tue, 19 May 2009 12:58:58 +0200 Subject: Added test for checking that timers only fire once per processEvents. --- tests/auto/qtimer/tst_qtimer.cpp | 54 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/auto/qtimer/tst_qtimer.cpp b/tests/auto/qtimer/tst_qtimer.cpp index cde2765..39144af 100644 --- a/tests/auto/qtimer/tst_qtimer.cpp +++ b/tests/auto/qtimer/tst_qtimer.cpp @@ -84,6 +84,8 @@ private slots: void deleteLaterOnQTimer(); // long name, don't want to shadow QObject::deleteLater() void moveToThread(); void restartedTimerFiresTooSoon(); + void timerFiresOnlyOncePerProcessEvents_data(); + void timerFiresOnlyOncePerProcessEvents(); }; class TimerHelper : public QObject @@ -480,6 +482,58 @@ void tst_QTimer::restartedTimerFiresTooSoon() 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("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); + } + +#ifdef Q_OS_SYMBIAN + QEXPECT_FAIL("non-zero timer", "Will be fixed in next commit", Abort); +#endif + QCOMPARE(longSlot.count, 1); +} + QTEST_MAIN(tst_QTimer) #include "tst_qtimer.moc" \ -- cgit v0.12