diff options
author | Ahmad Samir <[email protected]> | 2023-03-05 00:13:32 +0200 |
---|---|---|
committer | Ahmad Samir <[email protected]> | 2023-06-30 02:24:53 +0300 |
commit | 57712e1160dd2bd37e8a0838cf0298b740a96983 (patch) | |
tree | 0d668885e8fbb4c8b9971d499e490bc48c90a2be | |
parent | c580a7b0fa09473fb6afaf3416c858b71171ad48 (diff) |
QTest: add qWait(chrono::milliseconds) overload
The code bailed out of the while loop when "remainingTime <= 0",
for a QDeadlineTimer::isForever() timer remainingTime() returns -1,
whereas remainingTimeAsDuration() returns nanoseconds::max().
I.e. the original code stopped the while loop when the timer isForever()
or "expired" (i.e. remaining time is 0). I am not sure the isForever()
part was intended that way or not, but it makes sense, if
QCoreApplication::processEvents() has already run "forever", there is no
point looping again.
[ChangeLog][QtCore][QTest] Added qWait(chrono::milliseconds) overload.
Change-Id: I871c28b053c6ba4f81a7e2d434aa4fbe03c8c5e7
Reviewed-by: Thiago Macieira <[email protected]>
-rw-r--r-- | src/corelib/kernel/qtestsupport_core.cpp | 42 | ||||
-rw-r--r-- | src/corelib/kernel/qtestsupport_core.h | 2 | ||||
-rw-r--r-- | src/testlib/doc/snippets/code/src_corelib_kernel_qtestsupport_core.cpp | 3 |
3 files changed, 35 insertions, 12 deletions
diff --git a/src/corelib/kernel/qtestsupport_core.cpp b/src/corelib/kernel/qtestsupport_core.cpp index a6221444180..44bde894d24 100644 --- a/src/corelib/kernel/qtestsupport_core.cpp +++ b/src/corelib/kernel/qtestsupport_core.cpp @@ -68,10 +68,23 @@ void QTest::qSleep(std::chrono::milliseconds msecs) \since 5.10 */ +/*! + \overload + + Waits for \a msecs. Equivalent to calling: + \code + QTest::qWait(std::chrono::milliseconds{msecs}); + \endcode +*/ +Q_CORE_EXPORT void QTest::qWait(int msecs) +{ + qWait(std::chrono::milliseconds{msecs}); +} -/*! \fn void QTest::qWait(int ms) +/*! + \since 6.7 - Waits for \a ms milliseconds. While waiting, events will be processed and + Waits for \a msecs. While waiting, events will be processed and your test will stay responsive to user interface events or network communication. Example: @@ -83,7 +96,7 @@ void QTest::qSleep(std::chrono::milliseconds msecs) \sa QTest::qSleep(), QSignalSpy::wait() */ -Q_CORE_EXPORT void QTest::qWait(int ms) +Q_CORE_EXPORT void QTest::qWait(std::chrono::milliseconds msecs) { // Ideally this method would be implemented in terms of qWaitFor(), with a // predicate that always returns false, but qWaitFor() uses the 1-arg overload @@ -93,17 +106,24 @@ Q_CORE_EXPORT void QTest::qWait(int ms) Q_ASSERT(QCoreApplication::instance()); - QDeadlineTimer timer(ms, Qt::PreciseTimer); - int remaining = ms; + using namespace std::chrono; + + QDeadlineTimer deadline(msecs, Qt::PreciseTimer); + do { - QCoreApplication::processEvents(QEventLoop::AllEvents, remaining); + QCoreApplication::processEvents(QEventLoop::AllEvents, deadline); QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete); - remaining = timer.remainingTime(); - if (remaining <= 0) + + // If dealine is Forever, processEvents() has already looped forever + if (deadline.isForever()) + break; + + msecs = ceil<milliseconds>(deadline.remainingTimeAsDuration()); + if (msecs == 0ms) break; - QTest::qSleep(qMin(10, remaining)); - remaining = timer.remainingTime(); - } while (remaining > 0); + + QTest::qSleep(std::min(10ms, msecs)); + } while (!deadline.hasExpired()); } QT_END_NAMESPACE diff --git a/src/corelib/kernel/qtestsupport_core.h b/src/corelib/kernel/qtestsupport_core.h index f3aa2299104..fdacd116e9e 100644 --- a/src/corelib/kernel/qtestsupport_core.h +++ b/src/corelib/kernel/qtestsupport_core.h @@ -54,6 +54,8 @@ template <typename Functor> Q_CORE_EXPORT void qWait(int ms); +Q_CORE_EXPORT void qWait(std::chrono::milliseconds msecs); + } // namespace QTest QT_END_NAMESPACE diff --git a/src/testlib/doc/snippets/code/src_corelib_kernel_qtestsupport_core.cpp b/src/testlib/doc/snippets/code/src_corelib_kernel_qtestsupport_core.cpp index 906a88b20f1..1bd6d3c0682 100644 --- a/src/testlib/doc/snippets/code/src_corelib_kernel_qtestsupport_core.cpp +++ b/src/testlib/doc/snippets/code/src_corelib_kernel_qtestsupport_core.cpp @@ -18,9 +18,10 @@ int myNetworkServerNotResponding() int MyObject::isReady() { //! [1] + using namespace std::chrono_literals; int i = 0; while (myNetworkServerNotResponding() && i++ < 50) - QTest::qWait(250); + QTest::qWait(250ms); //! [1] return 1; } |