blob: b5e8efce82abc4aa2ee8c84e50ad9e55ae157c6a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QTest>
#include <QTestEventLoop>
#include <QtCore/QTimer>
// Tests for QTestEventLoop (and some QTRY_* details)
class tst_EventLoop: public QObject
{
Q_OBJECT
bool m_inTestFunction = false;
private slots:
void cleanup();
void fail();
void skip();
void pass();
};
class DeferredFlag
{
bool m_flag;
public:
explicit DeferredFlag(bool initial = false) : m_flag(initial)
{
if (!initial)
QTimer::singleShot(50, [this] { m_flag = true; });
}
explicit operator bool() const { return m_flag; }
bool operator!() const { return !m_flag; }
friend bool operator==(DeferredFlag a, DeferredFlag b) { return bool(a) == bool(b); }
};
char *toString(DeferredFlag val)
{
return qstrdup(bool(val) ? "DeferredFlag(true)" : "DeferredFlag(false)");
}
void tst_EventLoop::cleanup()
{
// QTBUG-104441: looping didn't happen in cleanup() if test failed or skipped.
{
DeferredFlag flag;
auto &loop = QTestEventLoop::instance();
loop.enterLoopMSecs(100);
QVERIFY2(loop.timeout(), "QTestEventLoop exited prematurely in cleanup()");
QVERIFY(flag);
}
{
DeferredFlag flag;
QTRY_VERIFY2(flag, "QTRY_* loop exited prematurely in cleanup()");
}
m_inTestFunction = false;
}
void tst_EventLoop::fail()
{
QVERIFY2(!std::exchange(m_inTestFunction, true), "Earlier test failed to clean up");
QFAIL("Failing test should still clean up");
}
void tst_EventLoop::skip()
{
QVERIFY2(!std::exchange(m_inTestFunction, true), "Earlier test failed to clean up");
QSKIP("Skipping test should still clean up");
}
void tst_EventLoop::pass()
{
QVERIFY2(!std::exchange(m_inTestFunction, true), "Earlier test failed to clean up");
{
DeferredFlag flag;
auto &loop = QTestEventLoop::instance();
loop.enterLoopMSecs(100);
QVERIFY(loop.timeout());
QVERIFY(flag);
}
{
DeferredFlag flag;
QTRY_VERIFY(flag);
}
DeferredFlag flag;
QTestEventLoop loop(this);
QVERIFY(!flag);
loop.enterLoopMSecs(1);
QVERIFY(loop.timeout());
QVERIFY(!flag);
loop.enterLoopMSecs(100);
QVERIFY(loop.timeout());
QVERIFY(flag);
}
QTEST_MAIN(tst_EventLoop)
#include "tst_eventloop.moc"
|