Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
13
QT_BEGIN_NAMESPACE
14
15
namespace
QTest
{
16
17
Q_CORE_EXPORT
void
qSleep
(
int
ms);
18
Q_CORE_EXPORT
void
qSleep
(
std
::chrono::milliseconds msecs);
19
20
extern
Q_CORE_EXPORT
std
::atomic<
std
::
chrono
::
milliseconds
>
defaultTryTimeout
;
21
22
namespace
Internal
{
23
enum
class
WaitForResult
{
24
Failed
= -1,
25
NotYet
= 0,
26
Done
= 1,
27
};
28
29
inline
bool
waitForMore
(
bool
) {
return
true
; }
30
inline
bool
waitForMore
(
WaitForResult
value) {
return
value ==
WaitForResult
::
NotYet
; }
31
32
inline
bool
waitForSucceeded
(
bool
value) {
return
value; }
33
inline
bool
waitForSucceeded
(
WaitForResult
value) {
return
value >=
WaitForResult
::
Done
; }
34
}
35
36
template
<
typename
Functor>
37
[[nodiscard]]
bool
38
qWaitFor
(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
77
template
<
typename
Functor>
78
[[nodiscard]]
bool
qWaitFor
(Functor predicate,
int
timeout)
79
{
80
return
qWaitFor(predicate, QDeadlineTimer{timeout, Qt::PreciseTimer});
81
}
82
83
Q_CORE_EXPORT
void
qWait
(
int
ms);
84
85
Q_CORE_EXPORT
void
qWait
(
std
::chrono::milliseconds msecs);
86
87
}
// namespace QTest
88
89
QT_END_NAMESPACE
90
91
#
endif
QTest::Internal
Definition
qtestsupport_core.h:22
QTest::Internal::waitForSucceeded
bool waitForSucceeded(bool value)
Definition
qtestsupport_core.h:32
QTest::Internal::WaitForResult
WaitForResult
Definition
qtestsupport_core.h:23
QTest::Internal::WaitForResult::NotYet
@ NotYet
Definition
qtestsupport_core.h:25
QTest::Internal::WaitForResult::Failed
@ Failed
Definition
qtestsupport_core.h:24
QTest::Internal::WaitForResult::Done
@ Done
Definition
qtestsupport_core.h:26
QTest::Internal::waitForMore
bool waitForMore(WaitForResult value)
Definition
qtestsupport_core.h:30
QTest::Internal::waitForMore
bool waitForMore(bool)
Definition
qtestsupport_core.h:29
QTest::Internal::waitForSucceeded
bool waitForSucceeded(WaitForResult value)
Definition
qtestsupport_core.h:33
QTest
[15]
Definition
qtestsupport_core.h:15
QTest::qWait
Q_CORE_EXPORT void qWait(std::chrono::milliseconds msecs)
Definition
qtestsupport_core.cpp:156
QTest::qSleep
Q_CORE_EXPORT void qSleep(int ms)
\variable QTest::defaultTryTimeout
Definition
qtestsupport_core.cpp:51
QTest::qWaitFor
bool qWaitFor(Functor predicate, QDeadlineTimer deadline=QDeadlineTimer(defaultTryTimeout.load(std::memory_order_relaxed)))
Definition
qtestsupport_core.h:38
QTest::qSleep
Q_CORE_EXPORT void qSleep(std::chrono::milliseconds msecs)
Definition
qtestsupport_core.cpp:78
QTest::qWait
Q_CORE_EXPORT void qWait(int ms)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition
qtestsupport_core.cpp:121
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qtbase
src
corelib
kernel
qtestsupport_core.h
Generated on
for Qt by
1.18.0