From df359bcb703db5a8adbf14e88ba4ae0d54f0cfcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Tue, 12 Oct 2021 16:33:00 +0200 Subject: Decouple quitOnLastWindowClosed from quitLockEnabled In a512e210ac5b032c5fc2edf1ddf72e5a414485fda512e21 quitOnLastWindowClosed was changed to be implemented in terms of quitLockEnabled, but without any documentation to that end. Although the two features are similar (automatic quit under certain conditions), and interact, it doesn't make sense to overlap them until we actually expose them as a single property (automaticQuit e.g.) The logic for determining whether we can can quit automatically has been refactored to take both properties into account, on both a Core and Gui level. The call sites still need to check the individual properties to determine whether to activate automatic quit for that particular code path. Change-Id: I38c3e8cb30db373ea73dd45f150e5048c0db2f4d Reviewed-by: Volker Hilsheimer --- .../kernel/qguiapplication/tst_qguiapplication.cpp | 116 +++++++++++++++++++++ 1 file changed, 116 insertions(+) (limited to 'tests/auto') diff --git a/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp b/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp index ec7f7dc0dc..836419659b 100644 --- a/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp +++ b/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp @@ -77,6 +77,7 @@ private slots: void quitOnLastWindowClosed(); void quitOnLastWindowClosedMulti(); void dontQuitOnLastWindowClosed(); + void quitOnLastWindowClosedWithEventLoopLocker(); void genericPluginsAndWindowSystemEvents(); void layoutDirection(); void globalShareContext(); @@ -969,6 +970,121 @@ void tst_QGuiApplication::dontQuitOnLastWindowClosed() QCOMPARE(spyLastWindowClosed.count(), 1); // lastWindowClosed emitted } +class QuitSpy : public QObject +{ + Q_OBJECT +public: + QuitSpy() + { + qGuiApp->installEventFilter(this); + } + bool eventFilter(QObject *o, QEvent *e) override + { + Q_UNUSED(o); + if (e->type() == QEvent::Quit) + ++quits; + + return false; + } + + int quits = 0; +}; + +void tst_QGuiApplication::quitOnLastWindowClosedWithEventLoopLocker() +{ + int argc = 0; + QGuiApplication app(argc, nullptr); + + QVERIFY(app.quitOnLastWindowClosed()); + QVERIFY(app.isQuitLockEnabled()); + + auto defaultRestorer = qScopeGuard([&]{ + app.setQuitLockEnabled(true); + app.setQuitOnLastWindowClosed(true); + }); + + { + // Disabling QEventLoopLocker support should not affect + // quitting when last window is closed. + app.setQuitLockEnabled(false); + + QuitSpy quitSpy; + QWindow window; + window.show(); + QVERIFY(QTest::qWaitForWindowExposed(&window)); + QTimer::singleShot(0, &window, &QWindow::close); + QTimer::singleShot(200, &app, []{ QCoreApplication::exit(0); }); + app.exec(); + QCOMPARE(quitSpy.quits, 1); + } + + { + // Disabling quitOnLastWindowClosed support should not affect + // quitting when last QEventLoopLocker goes out of scope. + app.setQuitLockEnabled(true); + app.setQuitOnLastWindowClosed(false); + + QuitSpy quitSpy; + QScopedPointer locker(new QEventLoopLocker); + QWindow window; + window.show(); + QVERIFY(QTest::qWaitForWindowExposed(&window)); + QTimer::singleShot(0, [&]{ locker.reset(nullptr); }); + QTimer::singleShot(200, &app, []{ QCoreApplication::exit(0); }); + app.exec(); + QCOMPARE(quitSpy.quits, 1); + } + + { + // With both properties enabled we need to get rid of both + // the window and locker to trigger a quit. + app.setQuitLockEnabled(true); + app.setQuitOnLastWindowClosed(true); + + QuitSpy quitSpy; + QScopedPointer locker(new QEventLoopLocker); + QWindow window; + window.show(); + QVERIFY(QTest::qWaitForWindowExposed(&window)); + QTimer::singleShot(0, &window, &QWindow::close); + QTimer::singleShot(200, &app, []{ QCoreApplication::exit(0); }); + app.exec(); + QCOMPARE(quitSpy.quits, 0); + + window.show(); + QVERIFY(QTest::qWaitForWindowExposed(&window)); + QTimer::singleShot(0, [&]{ locker.reset(nullptr); }); + QTimer::singleShot(200, &app, []{ QCoreApplication::exit(0); }); + app.exec(); + QCOMPARE(quitSpy.quits, 0); + + window.show(); + QVERIFY(QTest::qWaitForWindowExposed(&window)); + QTimer::singleShot(0, [&]{ locker.reset(nullptr); }); + QTimer::singleShot(0, &window, &QWindow::close); + QTimer::singleShot(200, &app, []{ QCoreApplication::exit(0); }); + app.exec(); + QCOMPARE(quitSpy.quits, 1); + } + + { + // With neither properties enabled we don't get automatic quit. + app.setQuitLockEnabled(false); + app.setQuitOnLastWindowClosed(false); + + QuitSpy quitSpy; + QScopedPointer locker(new QEventLoopLocker); + QWindow window; + window.show(); + QVERIFY(QTest::qWaitForWindowExposed(&window)); + QTimer::singleShot(0, [&]{ locker.reset(nullptr); }); + QTimer::singleShot(0, &window, &QWindow::close); + QTimer::singleShot(200, &app, []{ QCoreApplication::exit(0); }); + app.exec(); + QCOMPARE(quitSpy.quits, 0); + } +} + static Qt::ScreenOrientation testOrientationToSend = Qt::PrimaryOrientation; class TestPlugin : public QObject -- cgit v1.2.3