Qt 6.x
The Qt SDK
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#include <QtCore/qthread.h>
10
12
13namespace QTest {
14
15Q_CORE_EXPORT void qSleep(int ms);
16Q_CORE_EXPORT void qSleep(std::chrono::milliseconds msecs);
17
18template <typename Functor>
19[[nodiscard]] static bool
21{
22 // We should not spin the event loop in case the predicate is already true,
23 // otherwise we might send new events that invalidate the predicate.
24 if (predicate())
25 return true;
26
27 // qWait() is expected to spin the event loop, even when called with a small
28 // timeout like 1ms, so we we can't use a simple while-loop here based on
29 // the deadline timer not having timed out. Use do-while instead.
30
31 using namespace std::chrono;
32
33 auto remaining = 0ms;
34 do {
35 // We explicitly do not pass the remaining time to processEvents, as
36 // that would keep spinning processEvents for the whole duration if
37 // new events were posted as part of processing events, and we need
38 // to return back to this function to check the predicate between
39 // each pass of processEvents. Our own timer will take care of the
40 // timeout.
43
44 if (predicate())
45 return true;
46
47 if (deadline.isForever()) { // No point checking remaining time
48 qSleep(10ms);
49 continue;
50 }
51
52 remaining = ceil<milliseconds>(deadline.remainingTimeAsDuration());
53 if (remaining == 0ms)
54 break;
55
56 qSleep(std::min(10ms, remaining));
57 } while (!deadline.hasExpired());
58
59 return predicate(); // Last chance
60}
61
62template <typename Functor>
63[[nodiscard]] static bool qWaitFor(Functor predicate, int timeout)
64{
66}
67
68Q_CORE_EXPORT void qWait(int ms);
69
70Q_CORE_EXPORT void qWait(std::chrono::milliseconds msecs);
71
72} // namespace QTest
73
75
76#endif
static void processEvents(QEventLoop::ProcessEventsFlags flags=QEventLoop::AllEvents)
Processes some pending events for the calling thread according to the specified flags.
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
const auto predicate
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...
static bool qWaitFor(Functor predicate, QDeadlineTimer deadline=QDeadlineTimer(std::chrono::seconds{5}))
Q_CORE_EXPORT void qWait(int ms)
This is an overloaded member function, provided for convenience. It differs from the above function o...
GLbitfield GLuint64 timeout
[4]
QDeadlineTimer deadline(30s)