diff options
Diffstat (limited to 'tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp')
-rw-r--r-- | tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp | 65 |
1 files changed, 47 insertions, 18 deletions
diff --git a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp index 1bd27cd0ce..3c0adda5de 100644 --- a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp +++ b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp @@ -135,24 +135,53 @@ void tst_QTimer::timeout() void tst_QTimer::remainingTime() { - QTimer timer; - QSignalSpy timeoutSpy(&timer, &QTimer::timeout); - timer.setTimerType(Qt::PreciseTimer); - timer.start(200); - - QCOMPARE(timeoutSpy.count(), 0); - QTest::qWait(50); - QCOMPARE(timeoutSpy.count(), 0); - - int remainingTime = timer.remainingTime(); - QVERIFY2(remainingTime >= 50 && remainingTime <= 200, qPrintable(QString::number(remainingTime))); - - QVERIFY(timeoutSpy.wait()); - QCOMPARE(timeoutSpy.count(), 1); - - // the timer is still active, so it should have a non-zero remaining time - remainingTime = timer.remainingTime(); - QVERIFY2(remainingTime >= 50, qPrintable(QString::number(remainingTime))); + QTimer tested; + tested.setTimerType(Qt::PreciseTimer); + + QTimer tester; + tester.setTimerType(Qt::PreciseTimer); + tester.setSingleShot(true); + + const int testedInterval = 200; + const int testerInterval = 50; + const int expectedRemainingTime = testedInterval - testerInterval; + + int testIteration = 0; + const int desiredTestCount = 2; + + auto connection = QObject::connect(&tested, &QTimer::timeout, [&tester]() { + // We let tested (which isn't a single-shot) run repeatedly, to verify + // it *does* repeat, and check that the single-shot tester, starting + // at the same time, does finish first each time, by about the right duration. + tester.start(); // Start tester again. + }); + + QObject::connect(&tester, &QTimer::timeout, [&]() { + const int remainingTime = tested.remainingTime(); + // We expect that remainingTime is at most 150 and not overdue. + const bool remainingTimeInRange = remainingTime > 0 + && remainingTime <= expectedRemainingTime; + if (remainingTimeInRange) + ++testIteration; + else + testIteration = desiredTestCount; // We are going to fail on QVERIFY2() + // below, so we don't want to iterate + // anymore and quickly exit the QTRY_...() + // with this failure. + if (testIteration == desiredTestCount) + QObject::disconnect(connection); // Last iteration, don't start tester again. + QVERIFY2(remainingTimeInRange, qPrintable("Remaining time " + + QByteArray::number(remainingTime) + "ms outside expected range (0ms, " + + QByteArray::number(expectedRemainingTime) + "ms]")); + }); + + tested.start(testedInterval); + tester.start(testerInterval); // Start tester for the 1st time. + + // Test it desiredTestCount times, give it reasonable amount of time + // (twice as much as needed). + QTRY_COMPARE_WITH_TIMEOUT(testIteration, desiredTestCount, + testedInterval * desiredTestCount * 2); } void tst_QTimer::remainingTimeInitial_data() |