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