1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qtestsupport_core.h"
5
6#include <thread>
7
8QT_BEGIN_NAMESPACE
9
10/*!
11 Sleeps for \a ms milliseconds, blocking execution of the
12 test. qSleep() will not do any event processing and leave your test
13 unresponsive. Network communication might time out while
14 sleeping. Use \l {QTest::qWait()} to do non-blocking sleeping.
15
16 \a ms must be greater than 0.
17
18 \note Starting from Qt 6.7, this function is implemented using
19 \c {std::this_thread::sleep_for}, so the accuracy of time spent depends
20 on the Standard Library implementation. Before Qt 6.7 this function called
21 either \c nanosleep() on Unix or \c Sleep() on Windows, so the accuracy of
22 time spent in this function depended on the operating system.
23
24 Example:
25 \snippet code/src_qtestlib_qtestcase.cpp 23
26
27 \sa {QTest::qWait()}
28*/
29Q_CORE_EXPORT void QTest::qSleep(int ms)
30{
31 Q_ASSERT(ms > 0);
32 std::this_thread::sleep_for(rtime: std::chrono::milliseconds{ms});
33}
34
35/*! \fn template <typename Functor> bool QTest::qWaitFor(Functor predicate, int timeout)
36
37 Waits for \a timeout milliseconds or until the \a predicate returns true.
38
39 Returns \c true if the \a predicate returned true at any point, otherwise returns \c false.
40
41 Example:
42
43 \snippet code/src_corelib_kernel_qtestsupport_core_snippet.cpp 0
44
45 The code above will wait for the object to become ready, for a
46 maximum of three seconds.
47
48 \since 5.10
49*/
50
51
52/*! \fn void QTest::qWait(int ms)
53
54 Waits for \a ms milliseconds. While waiting, events will be processed and
55 your test will stay responsive to user interface events or network communication.
56
57 Example:
58
59 \snippet code/src_corelib_kernel_qtestsupport_core.cpp 1
60
61 The code above will wait until the network server is responding for a
62 maximum of about 12.5 seconds.
63
64 \sa QTest::qSleep(), QSignalSpy::wait()
65*/
66Q_CORE_EXPORT void QTest::qWait(int ms)
67{
68 // Ideally this method would be implemented in terms of qWaitFor(), with a
69 // predicate that always returns false, but qWaitFor() uses the 1-arg overload
70 // of processEvents(), which doesn't handle events posted in this round of event
71 // processing, which, together with the 10ms qSleep() after every processEvents(),
72 // lead to a 10x slow-down in some webengine tests.
73
74 Q_ASSERT(QCoreApplication::instance());
75
76 QDeadlineTimer timer(ms, Qt::PreciseTimer);
77 int remaining = ms;
78 do {
79 QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: remaining);
80 QCoreApplication::sendPostedEvents(receiver: nullptr, event_type: QEvent::DeferredDelete);
81 remaining = timer.remainingTime();
82 if (remaining <= 0)
83 break;
84 QTest::qSleep(ms: qMin(a: 10, b: remaining));
85 remaining = timer.remainingTime();
86 } while (remaining > 0);
87}
88
89QT_END_NAMESPACE
90

source code of qtbase/src/corelib/kernel/qtestsupport_core.cpp