From 374a173d1417a9f8c313088420cb1b792fe45977 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Wed, 18 Jan 2017 15:14:45 +0100 Subject: Improve QTest::qWait() precision and switch to QDeadlineTimer Do not wait up to the timeout ms after already having waited several times. At the same time upgrade to using the QDeadlineTimer which is designed for this purpose. Change-Id: Iaf5e4f4655605d5143ce91040c6eb6706752e504 Reviewed-by: Thiago Macieira --- src/testlib/qtestsystem.h | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'src/testlib/qtestsystem.h') diff --git a/src/testlib/qtestsystem.h b/src/testlib/qtestsystem.h index 4973412f9b..b38cd2bdb4 100644 --- a/src/testlib/qtestsystem.h +++ b/src/testlib/qtestsystem.h @@ -42,7 +42,7 @@ #include #include -#include +#include #ifdef QT_GUI_LIB # include #endif @@ -58,27 +58,29 @@ namespace QTest { Q_ASSERT(QCoreApplication::instance()); - QElapsedTimer timer; - timer.start(); + QDeadlineTimer timer(ms); + int remaining = ms; do { - QCoreApplication::processEvents(QEventLoop::AllEvents, ms); + QCoreApplication::processEvents(QEventLoop::AllEvents, remaining); QCoreApplication::sendPostedEvents(Q_NULLPTR, QEvent::DeferredDelete); - QTest::qSleep(10); - } while (timer.elapsed() < ms); + remaining = timer.remainingTime(); + if (remaining <= 0) + break; + QTest::qSleep(qMin(10, remaining)); + remaining = timer.remainingTime(); + } while (remaining > 0); } #ifdef QT_GUI_LIB inline static bool qWaitForWindowActive(QWindow *window, int timeout = 5000) { - QElapsedTimer timer; - timer.start(); - while (!window->isActive()) { - int remaining = timeout - int(timer.elapsed()); - if (remaining <= 0) - break; + QDeadlineTimer timer(timeout); + int remaining = timeout; + while (!window->isActive() && remaining > 0) { QCoreApplication::processEvents(QEventLoop::AllEvents, remaining); QCoreApplication::sendPostedEvents(Q_NULLPTR, QEvent::DeferredDelete); QTest::qSleep(10); + remaining = timer.remainingTime(); } // Try ensuring the platform window receives the real position. // (i.e. that window->pos() reflects reality) @@ -99,15 +101,13 @@ namespace QTest inline static bool qWaitForWindowExposed(QWindow *window, int timeout = 5000) { - QElapsedTimer timer; - timer.start(); - while (!window->isExposed()) { - int remaining = timeout - int(timer.elapsed()); - if (remaining <= 0) - break; + QDeadlineTimer timer(timeout); + int remaining = timeout; + while (!window->isExposed() && remaining > 0) { QCoreApplication::processEvents(QEventLoop::AllEvents, remaining); QCoreApplication::sendPostedEvents(Q_NULLPTR, QEvent::DeferredDelete); QTest::qSleep(10); + remaining = timer.remainingTime(); } return window->isExposed(); } -- cgit v1.2.3