summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/kernel/qcoreapplication
diff options
context:
space:
mode:
authorSven Anderson <Sven.Anderson@snom.com>2011-09-08 17:40:55 +0200
committerQt by Nokia <qt-info@nokia.com>2011-11-15 10:16:12 +0100
commit2b7d98ef8fbd6cf49326fa0bbf154e9bacbb7b49 (patch)
tree250f2752acb679a237504ea70888b0ad5efa1251 /tests/auto/corelib/kernel/qcoreapplication
parent51b7d3c8b621c2de6f98f465f478ec574bb14195 (diff)
Allow to create a custom event dispatcher for specific QThreads.
QAbstractEventDispatcher() does no longer install itself into the current thread. Instead the new methods QThread::setEventDispatcher() and QCoreApplication::setEventDispatcher() allow to install a custom event dispatcher into any QThread as long as there is no default event dispatcher created yet. That is, before the thread has been started with QThread::start() or, in case of the main thread, before QCoreApplication has been instantiated. Change-Id: I7367e13d8d8aebed5a5651260bb69b8818eb1b90 Reviewed-by: Olivier Goffart <ogoffart@woboq.com> 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/tst_qcoreapplication.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp
index 62275f841a..09f31414a6 100644
--- a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp
+++ b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp
@@ -62,6 +62,7 @@ private slots:
void reexec();
void execAfterExit();
void eventLoopExecAfterExit();
+ void customEventDispatcher();
};
class EventSpy : public QObject
@@ -586,5 +587,59 @@ void tst_QCoreApplication::eventLoopExecAfterExit()
QCOMPARE(loop.exec(), 0);
}
+class DummyEventDispatcher : public QAbstractEventDispatcher {
+public:
+ DummyEventDispatcher() : QAbstractEventDispatcher(), visited(false) {}
+ bool processEvents(QEventLoop::ProcessEventsFlags) {
+ visited = true;
+ emit awake();
+ QCoreApplication::sendPostedEvents();
+ return false;
+ }
+ bool hasPendingEvents() {
+ extern uint qGlobalPostedEventsCount(); // from qapplication.cpp
+ return qGlobalPostedEventsCount();
+ }
+ void registerSocketNotifier(QSocketNotifier *) {}
+ void unregisterSocketNotifier(QSocketNotifier *) {}
+ void registerTimer(int , int , QObject *) {}
+ bool unregisterTimer(int ) { return false; }
+ bool unregisterTimers(QObject *) { return false; }
+ QList<TimerInfo> registeredTimers(QObject *) const { return QList<TimerInfo>(); }
+ void wakeUp() {}
+ void interrupt() {}
+ void flush() {}
+
+ bool visited;
+};
+
+void tst_QCoreApplication::customEventDispatcher()
+{
+ // there should be no ED yet
+ QVERIFY(!QCoreApplication::eventDispatcher());
+ DummyEventDispatcher *ed = new DummyEventDispatcher;
+ QCoreApplication::setEventDispatcher(ed);
+ // the new ED should be set
+ QCOMPARE(QCoreApplication::eventDispatcher(), ed);
+ // test the alternative API of QAbstractEventDispatcher
+ QCOMPARE(QAbstractEventDispatcher::instance(), ed);
+ QWeakPointer<DummyEventDispatcher> weak_ed(ed);
+ QVERIFY(!weak_ed.isNull());
+ {
+ int argc = 1;
+ char *arg0 = "tst_qcoreapplication";
+ char *argv[] = { arg0 };
+ QCoreApplication app(argc, argv);
+ // instantiating app should not overwrite the ED
+ QCOMPARE(QCoreApplication::eventDispatcher(), ed);
+ QMetaObject::invokeMethod(&app, "quit", Qt::QueuedConnection);
+ app.exec();
+ // the custom ED has really been used?
+ QVERIFY(ed->visited);
+ }
+ // ED has been deleted?
+ QVERIFY(weak_ed.isNull());
+}
+
QTEST_APPLESS_MAIN(tst_QCoreApplication)
#include "tst_qcoreapplication.moc"