Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtestsupport_core.cpp
Go to the documentation of this file.
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
8using namespace std::chrono_literals;
9
11
22void QTest::qSleep(int ms)
23{
24 QTest::qSleep(std::chrono::milliseconds{ms});
25}
26
49void QTest::qSleep(std::chrono::milliseconds msecs)
50{
51 Q_ASSERT(msecs > 0ms);
52 std::this_thread::sleep_for(msecs);
53}
54
92Q_CORE_EXPORT void QTest::qWait(int msecs)
93{
94 qWait(std::chrono::milliseconds{msecs});
95}
96
112Q_CORE_EXPORT void QTest::qWait(std::chrono::milliseconds msecs)
113{
114 // Ideally this method would be implemented in terms of qWaitFor(), with a
115 // predicate that always returns false, but qWaitFor() uses the 1-arg overload
116 // of processEvents(), which doesn't handle events posted in this round of event
117 // processing, which, together with the 10ms qSleep() after every processEvents(),
118 // lead to a 10x slow-down in some webengine tests.
119
121
122 using namespace std::chrono;
123
125
126 do {
129
130 // If dealine is Forever, processEvents() has already looped forever
131 if (deadline.isForever())
132 break;
133
134 msecs = ceil<milliseconds>(deadline.remainingTimeAsDuration());
135 if (msecs == 0ms)
136 break;
137
138 QTest::qSleep(std::min(10ms, msecs));
139 } while (!deadline.hasExpired());
140}
141
static void processEvents(QEventLoop::ProcessEventsFlags flags=QEventLoop::AllEvents)
Processes some pending events for the calling thread according to the specified flags.
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
static void sendPostedEvents(QObject *receiver=nullptr, int event_type=0)
Immediately dispatches all events which have been previously queued with QCoreApplication::postEvent(...
\inmodule QtCore
bool hasExpired() const noexcept
Returns true if this QDeadlineTimer object has expired, false if there remains time left.
constexpr bool isForever() const noexcept
Returns true if this QDeadlineTimer object never expires, false otherwise.
std::chrono::nanoseconds remainingTimeAsDuration() const noexcept
Returns the time remaining before the deadline.
@ DeferredDelete
Definition qcoreevent.h:100
Combined button and popup list for selecting options.
Q_CORE_EXPORT void qSleep(int ms)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Q_CORE_EXPORT void qWait(int ms)
This is an overloaded member function, provided for convenience. It differs from the above function o...
@ PreciseTimer
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QDeadlineTimer deadline(30s)