From 7c85ad307b1c9d69b4a9e5424f111d031649c44a Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Mon, 6 Sep 2021 14:32:39 +0200 Subject: [PATCH] Add test-case for QWidget closing exits event loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also when closed by destruction, which as of today doesn't call QWidget::close and therefore also not QWindow::close. Pick-to: 6.2 Change-Id: I426255e2274eae9262243c769df2264fbaa915b0 Reviewed-by: Tor Arne Vestbø --- .../widgets/kernel/qwidget/tst_qwidget.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp index 4fa568228a..0da8d2971b 100644 --- a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp +++ b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include @@ -426,6 +427,7 @@ private slots: void receivesApplicationFontChangeEvent(); void receivesApplicationPaletteChangeEvent(); void deleteWindowInCloseEvent(); + void quitOnClose(); private: bool ensureScreenSize(int width, int height); @@ -12013,5 +12015,34 @@ void tst_QWidget::deleteWindowInCloseEvent() QVERIFY(true); } +/*! + Verify that both closing and deleting the last (only) window-widget + exits the application event loop. +*/ +void tst_QWidget::quitOnClose() +{ + QSignalSpy quitSpy(qApp, &QApplication::lastWindowClosed); + + std::unique_ptrwidget(new QWidget); + widget->show(); + QVERIFY(QTest::qWaitForWindowExposed(widget.get())); + + // QGuiApplication::lastWindowClosed is documented to only be emitted + // when we are in exec() + QTimer::singleShot(0, widget.get(), [&]{ + widget->close(); + }); + QApplication::exec(); + QCOMPARE(quitSpy.count(), 1); + + widget->show(); + QVERIFY(QTest::qWaitForWindowExposed(widget.get())); + QTimer::singleShot(0, widget.get(), [&]{ + widget.reset(); + }); + QApplication::exec(); + QCOMPARE(quitSpy.count(), 2); +} + QTEST_MAIN(tst_QWidget) #include "tst_qwidget.moc"