diff options
author | Stephen Kelly <stephen.kelly@kdab.com> | 2011-10-26 13:29:51 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-01-31 23:22:15 +0100 |
commit | a512e210ac5b032c5fc2edf1ddf72e5a414485fd (patch) | |
tree | 0e9615f317fe5c24bd002155b205a01d5716a1d3 /tests/auto/corelib/kernel/qcoreapplication | |
parent | 36a590c90ddab6fa438b7b54b05d89e8f9698a52 (diff) |
Add the event loop quitlock feature to QtCore.
A feature of a ref-counted quit (managed by a quit-lock class)
is added to both QEventLoop and QCoreApplication.
This allows, for example, an event loop to quit() when there is
no more work for it to do.
quitOnLastWindowClosed is implemented in terms of the refcount in
QCoreApplication so that jobs can be completed before the
application quits.
Change-Id: I14c8f4e7ee12bbf81a6e5849290d4c8ff37fa110
Reviewed-by: David Faure <faure@kde.org>
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Diffstat (limited to 'tests/auto/corelib/kernel/qcoreapplication')
-rw-r--r-- | tests/auto/corelib/kernel/qcoreapplication/qcoreapplication.pro | 2 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp | 114 |
2 files changed, 114 insertions, 2 deletions
diff --git a/tests/auto/corelib/kernel/qcoreapplication/qcoreapplication.pro b/tests/auto/corelib/kernel/qcoreapplication/qcoreapplication.pro index f9a38c2f14..14df20c986 100644 --- a/tests/auto/corelib/kernel/qcoreapplication/qcoreapplication.pro +++ b/tests/auto/corelib/kernel/qcoreapplication/qcoreapplication.pro @@ -1,4 +1,4 @@ CONFIG += testcase parallel_test TARGET = tst_qcoreapplication -QT = core testlib +QT = core testlib core-private SOURCES = tst_qcoreapplication.cpp diff --git a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp index fc3d8e0fbb..97c9757107 100644 --- a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp +++ b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp @@ -39,10 +39,13 @@ ** ****************************************************************************/ - #include <QtCore/QtCore> #include <QtTest/QtTest> +#include <private/qcoreapplication_p.h> +#include <private/qeventloop_p.h> +#include <private/qthread_p.h> + class tst_QCoreApplication: public QObject { Q_OBJECT @@ -63,6 +66,7 @@ private slots: void execAfterExit(); void eventLoopExecAfterExit(); void customEventDispatcher(); + void testQuitLock(); }; class EventSpy : public QObject @@ -640,5 +644,113 @@ void tst_QCoreApplication::customEventDispatcher() QVERIFY(weak_ed.isNull()); } +class JobObject : public QObject +{ + Q_OBJECT +public: + + explicit JobObject(QEventLoop *loop, QObject *parent = 0) + : QObject(parent), locker(loop) + { + QTimer::singleShot(1000, this, SLOT(timeout())); + } + + explicit JobObject(QObject *parent = 0) + : QObject(parent) + { + QTimer::singleShot(1000, this, SLOT(timeout())); + } + +public slots: + void startSecondaryJob() + { + new JobObject(); + } + +private slots: + void timeout() + { + emit done(); + deleteLater(); + } + +signals: + void done(); + +private: + QEventLoopLocker locker; +}; + +class QuitTester : public QObject +{ + Q_OBJECT +public: + QuitTester(QObject *parent = 0) + : QObject(parent) + { + QTimer::singleShot(0, this, SLOT(doTest())); + } + +private slots: + void doTest() + { + QCoreApplicationPrivate *privateClass = static_cast<QCoreApplicationPrivate*>(QObjectPrivate::get(qApp)); + + { + QCOMPARE(privateClass->quitLockRef.load(), 0); + // Test with a lock active so that the refcount doesn't drop to zero during these tests, causing a quit. + // (until we exit the scope) + QEventLoopLocker locker; + + QCOMPARE(privateClass->quitLockRef.load(), 1); + + JobObject *job1 = new JobObject(this); + + QCOMPARE(privateClass->quitLockRef.load(), 2); + + delete job1; + + QCOMPARE(privateClass->quitLockRef.load(), 1); + + job1 = new JobObject(this); + + QCOMPARE(privateClass->quitLockRef.load(), 2); + + JobObject *job2 = new JobObject(this); + + QCOMPARE(privateClass->quitLockRef.load(), 3); + + delete job1; + + QCOMPARE(privateClass->quitLockRef.load(), 2); + + JobObject *job3 = new JobObject(job2); + + QCOMPARE(privateClass->quitLockRef.load(), 3); + + JobObject *job4 = new JobObject(job2); + + QCOMPARE(privateClass->quitLockRef.load(), 4); + + delete job2; + + QCOMPARE(privateClass->quitLockRef.load(), 1); + + } + QCOMPARE(privateClass->quitLockRef.load(), 0); + } +}; + +void tst_QCoreApplication::testQuitLock() +{ + int argc = 1; + char *argv[] = { "tst_qcoreapplication" }; + QCoreApplication app(argc, argv); + + QuitTester tester; + app.exec(); +} + + QTEST_APPLESS_MAIN(tst_QCoreApplication) #include "tst_qcoreapplication.moc" |