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
qtesteventloop.h
Go to the documentation of this file.
1// Copyright (C) 2021 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 QTESTEVENTLOOP_H
5#define QTESTEVENTLOOP_H
6
7#include <QtTest/qttestglobal.h>
8#include <QtTest/qtestcase.h>
9
10#include <QtCore/qcoreapplication.h>
11#include <QtCore/qeventloop.h>
12#include <QtCore/qobject.h>
13#include <QtCore/qpointer.h>
14#include <QtCore/qthread.h>
15
17
18
19class Q_TESTLIB_EXPORT QTestEventLoop : public QObject
20{
22
23public:
24 QTestEventLoop(QObject *parent = nullptr)
25 : QObject(parent), _timeout(false)
26 {}
27
28 void enterLoopMSecs(int ms) { enterLoop(std::chrono::milliseconds{ms}); };
29 void enterLoop(int secs) { enterLoop(std::chrono::seconds{secs}); }
30 inline void enterLoop(std::chrono::milliseconds msecs);
31
32 inline void changeInterval(int secs)
33 { killTimer(timerId); timerId = startTimer(secs * 1000); }
34
35 inline bool timeout() const
36 { return _timeout; }
37
38 inline static QTestEventLoop &instance()
39 {
40 Q_CONSTINIT static QPointer<QTestEventLoop> testLoop;
41 if (testLoop.isNull())
43 return *static_cast<QTestEventLoop *>(testLoop);
44 }
45
46public Q_SLOTS:
47 inline void exitLoop();
48
49protected:
50 inline void timerEvent(QTimerEvent *e) override;
51
52private:
53 QEventLoop *loop = nullptr;
54 int timerId = -1;
55 uint _timeout :1;
56 Q_DECL_UNUSED_MEMBER uint reserved :31;
57};
58
59inline void QTestEventLoop::enterLoop(std::chrono::milliseconds msecs)
60{
61 Q_ASSERT(!loop);
62 _timeout = false;
63
65 return;
66
67 using namespace std::chrono_literals;
68 QEventLoop l;
69 // if tests want to measure sub-second precision, use a precise timer
70 timerId = startTimer(msecs, msecs < 1s ? Qt::PreciseTimer : Qt::CoarseTimer);
71
72 loop = &l;
73 l.exec();
74 loop = nullptr;
75}
76
78{
80 {
82 return;
83 }
84
85 if (timerId != -1)
86 killTimer(timerId);
87 timerId = -1;
88
89 if (loop)
90 loop->exit();
91}
92
94{
95 if (e->timerId() != timerId)
96 return;
97 _timeout = true;
98 exitLoop();
99}
100
102
103#endif
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
\inmodule QtCore
Definition qeventloop.h:16
int exec(ProcessEventsFlags flags=AllEvents)
Enters the main event loop and waits until exit() is called.
void exit(int returnCode=0)
Tells the event loop to exit with a return code.
\inmodule QtCore
Definition qobject.h:103
int startTimer(int interval, Qt::TimerType timerType=Qt::CoarseTimer)
This is an overloaded function that will start a timer of type timerType and a timeout of interval mi...
Definition qobject.cpp:1817
QThread * thread() const
Returns the thread in which the object lives.
Definition qobject.cpp:1598
void killTimer(int id)
Kills the timer with timer identifier, id.
Definition qobject.cpp:1912
void enterLoop(int secs)
QTestEventLoop(QObject *parent=nullptr)
static QTestEventLoop & instance()
bool timeout() const
void enterLoopMSecs(int ms)
void changeInterval(int secs)
void timerEvent(QTimerEvent *e) override
This event handler can be reimplemented in a subclass to receive timer events for the object.
static QThread * currentThread()
Definition qthread.cpp:1039
\inmodule QtCore
Definition qcoreevent.h:366
int timerId() const
Returns the unique timer identifier, which is the same identifier as returned from QObject::startTime...
Definition qcoreevent.h:370
Combined button and popup list for selecting options.
Q_TESTLIB_EXPORT bool currentTestResolved()
Q_TESTLIB_EXPORT bool runningTest()
@ CoarseTimer
@ PreciseTimer
@ QueuedConnection
#define Q_DECL_UNUSED_MEMBER
GLdouble s
[6]
Definition qopenglext.h:235
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_OBJECT
#define Q_SLOTS
unsigned int uint
Definition qtypes.h:34
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(nullptr), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
\threadsafe This is an overloaded member function, provided for convenience. It differs from the abov...