From 54589f293297f0c132a7814a14f0d6ab38393b19 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 16 Apr 2015 16:13:37 -0700 Subject: Autotest: Check where global event filters get run Global (application-level) event filters are supposed to be run only in the main thread, so ensure that it is the case. Change-Id: I27eaacb532114dd188c4ffff13d5a17d991b8bd2 Reviewed-by: Olivier Goffart (Woboq GmbH) --- .../qcoreapplication/tst_qcoreapplication.cpp | 55 ++++++++++++++++++++++ .../kernel/qcoreapplication/tst_qcoreapplication.h | 3 ++ 2 files changed, 58 insertions(+) diff --git a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp index 924db17c04..efecf31d66 100644 --- a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp +++ b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2015 The Qt Company Ltd. +** Copyright (C) 2015 Intel Corporation. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. @@ -60,6 +61,23 @@ public: } }; +class ThreadedEventReceiver : public QObject +{ + Q_OBJECT +public: + QList recordedEvents; + bool event(QEvent *event) Q_DECL_OVERRIDE + { + if (event->type() != QEvent::Type(QEvent::User + 1)) + return QObject::event(event); + recordedEvents.append(event->type()); + QThread::currentThread()->quit(); + QCoreApplication::quit(); + moveToThread(0); + return true; + } +}; + void tst_QCoreApplication::sendEventsOnProcessEvents() { int argc = 1; @@ -797,6 +815,43 @@ void tst_QCoreApplication::QTBUG31606_QEventDestructorDeadLock() QVERIFY(spy.recordedEvents.contains(QEvent::User + 2)); } +// this is almost identical to sendEventsOnProcessEvents +void tst_QCoreApplication::applicationEventFilters_mainThread() +{ + int argc = 1; + char *argv[] = { const_cast(QTest::currentAppName()) }; + TestApplication app(argc, argv); + + EventSpy spy; + app.installEventFilter(&spy); + + QCoreApplication::postEvent(&app, new QEvent(QEvent::Type(QEvent::User + 1))); + QTimer::singleShot(10, &app, SLOT(quit())); + app.exec(); + QVERIFY(spy.recordedEvents.contains(QEvent::User + 1)); +} + +void tst_QCoreApplication::applicationEventFilters_auxThread() +{ + int argc = 1; + char *argv[] = { const_cast(QTest::currentAppName()) }; + TestApplication app(argc, argv); + QThread thread; + ThreadedEventReceiver receiver; + receiver.moveToThread(&thread); + + EventSpy spy; + app.installEventFilter(&spy); + + // this is very similar to sendEventsOnProcessEvents + QCoreApplication::postEvent(&receiver, new QEvent(QEvent::Type(QEvent::User + 1))); + QTimer::singleShot(1000, &app, SLOT(quit())); + thread.start(); + app.exec(); + QVERIFY(thread.wait(1000)); + QVERIFY(receiver.recordedEvents.contains(QEvent::User + 1)); + QVERIFY(!spy.recordedEvents.contains(QEvent::User + 1)); +} static void createQObjectOnDestruction() { diff --git a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.h b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.h index 3dd84482d7..09e15723ac 100644 --- a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.h +++ b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.h @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2015 The Qt Company Ltd. +** Copyright (C) 2015 Intel Corporation. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. @@ -58,6 +59,8 @@ private slots: void customEventDispatcher(); void testQuitLock(); void QTBUG31606_QEventDestructorDeadLock(); + void applicationEventFilters_mainThread(); + void applicationEventFilters_auxThread(); }; #endif // TST_QCOREAPPLICATION_H -- cgit v1.2.3