diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2021-10-12 16:33:00 +0200 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2021-10-18 15:29:09 +0200 |
commit | df359bcb703db5a8adbf14e88ba4ae0d54f0cfcd (patch) | |
tree | 54c71f280fa4b5056f6ae3ad7f0aa61cabfcd82e /tests/auto | |
parent | 55f8f916ce18047cb38e3512c7c5c87f7eb991d4 (diff) |
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 <volker.hilsheimer@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp | 116 |
1 files changed, 116 insertions, 0 deletions
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<QEventLoopLocker> 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<QEventLoopLocker> 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<QEventLoopLocker> 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 |