Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtestsupport_core.h
Go to the documentation of this file.
1// Copyright (C) 2018 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// Qt-Security score:significant reason:default
4
5#ifndef QTESTSUPPORT_CORE_H
6#define QTESTSUPPORT_CORE_H
7
8#include <QtCore/qcoreapplication.h>
9#include <QtCore/qdeadlinetimer.h>
10
11#include <chrono>
12
13QT_BEGIN_NAMESPACE
14
15namespace QTest {
16
17Q_CORE_EXPORT void qSleep(int ms);
18Q_CORE_EXPORT void qSleep(std::chrono::milliseconds msecs);
19
20extern Q_CORE_EXPORT std::atomic<std::chrono::milliseconds> defaultTryTimeout;
21
22namespace Internal {
23enum class WaitForResult {
24 Failed = -1,
25 NotYet = 0,
26 Done = 1,
27};
28
29inline bool waitForMore(bool) { return true; }
30inline bool waitForMore(WaitForResult value) { return value == WaitForResult::NotYet; }
31
32inline bool waitForSucceeded(bool value) { return value; }
33inline bool waitForSucceeded(WaitForResult value) { return value >= WaitForResult::Done; }
34}
35
36template <typename Functor>
37[[nodiscard]] bool
38qWaitFor(Functor predicate, QDeadlineTimer deadline = QDeadlineTimer(
39 defaultTryTimeout.load(std::memory_order_relaxed)))
40{
41 using Internal::waitForMore; // customization point
42 using Internal::waitForSucceeded; // customization point
43
44 // We should not spin the event loop in case the predicate is already true,
45 // otherwise we might send new events that invalidate the predicate.
46 if (waitForSucceeded(predicate()))
47 return true;
48
49 // qWait() is expected to spin the event loop at least once, even when
50 // called with a small timeout like 1ns.
51
52 do {
53 // We explicitly do not pass the remaining time to processEvents, as
54 // that would keep spinning processEvents for the whole duration if
55 // new events were posted as part of processing events, and we need
56 // to return back to this function to check the predicate between
57 // each pass of processEvents. Our own timer will take care of the
58 // timeout.
59 QCoreApplication::processEvents(QEventLoop::AllEvents);
60 QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
61
62 if (auto predresult = predicate(); waitForSucceeded(predresult))
63 return true;
64 else if (!waitForMore(predresult))
65 return false;
66
67 using namespace std::chrono;
68
69 if (const auto remaining = deadline.remainingTimeAsDuration(); remaining > 0ns)
70 qSleep((std::min)(10ms, ceil<milliseconds>(remaining)));
71
72 } while (!deadline.hasExpired());
73
74 return waitForSucceeded(predicate()); // Last chance
75}
76
77template <typename Functor>
78[[nodiscard]] bool qWaitFor(Functor predicate, int timeout)
79{
80 return qWaitFor(predicate, QDeadlineTimer{timeout, Qt::PreciseTimer});
81}
82
83Q_CORE_EXPORT void qWait(int ms);
84
85Q_CORE_EXPORT void qWait(std::chrono::milliseconds msecs);
86
87} // namespace QTest
88
89QT_END_NAMESPACE
90
91#endif
bool waitForSucceeded(bool value)
bool waitForMore(WaitForResult value)
bool waitForSucceeded(WaitForResult value)
Q_CORE_EXPORT void qWait(std::chrono::milliseconds msecs)
Q_CORE_EXPORT void qSleep(int ms)
\variable QTest::defaultTryTimeout
bool qWaitFor(Functor predicate, QDeadlineTimer deadline=QDeadlineTimer(defaultTryTimeout.load(std::memory_order_relaxed)))
Q_CORE_EXPORT void qSleep(std::chrono::milliseconds msecs)
Q_CORE_EXPORT void qWait(int ms)
This is an overloaded member function, provided for convenience. It differs from the above function o...