diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2022-07-11 17:46:38 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2022-08-08 20:49:03 +0200 |
commit | 35ad157d88c7bfcb9b90b01111b0f43dd2e012d9 (patch) | |
tree | 52f365a93af8035ff66858fb5659a01adfbb20b5 /tests/auto/testlib/selftests/eventloop | |
parent | dfaefa6c5e3ed360e97101a7bc750ee0a46114f4 (diff) |
Add some testing of QTestEventLoop
Fairly minimal for now, just enough to verify a bug and serve as the
sign of success when it's fixed. Tests fail in ways they shouldn't,
for now; see expected_eventloop.* for details, notably "Earlier test
failed to clean up" messages.
Pick-to: 6.4 6.3
Task-number: QTBUG-104441
Change-Id: I59be4aa5f21fed23b19a0593a8c2f6c9956507df
Reviewed-by: Jason McDonald <macadder1@gmail.com>
Diffstat (limited to 'tests/auto/testlib/selftests/eventloop')
-rw-r--r-- | tests/auto/testlib/selftests/eventloop/CMakeLists.txt | 16 | ||||
-rw-r--r-- | tests/auto/testlib/selftests/eventloop/tst_eventloop.cpp | 96 |
2 files changed, 112 insertions, 0 deletions
diff --git a/tests/auto/testlib/selftests/eventloop/CMakeLists.txt b/tests/auto/testlib/selftests/eventloop/CMakeLists.txt new file mode 100644 index 0000000000..f1fa9a788c --- /dev/null +++ b/tests/auto/testlib/selftests/eventloop/CMakeLists.txt @@ -0,0 +1,16 @@ +##################################################################### +## eventloop Binary: +##################################################################### + +qt_internal_add_executable(eventloop + NO_INSTALL + OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + SOURCES + tst_eventloop.cpp + PUBLIC_LIBRARIES + Qt::Test +) + +## Scopes: +##################################################################### +qt_internal_apply_testlib_coverage_options(eventloop) diff --git a/tests/auto/testlib/selftests/eventloop/tst_eventloop.cpp b/tests/auto/testlib/selftests/eventloop/tst_eventloop.cpp new file mode 100644 index 0000000000..b5e8efce82 --- /dev/null +++ b/tests/auto/testlib/selftests/eventloop/tst_eventloop.cpp @@ -0,0 +1,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" |