1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 # include <QtGui/QGuiApplication>
45 # include <QtCore/QCoreApplication>
47 #include <QtTest/QtTest>
50 PreciseTimerInterval = 10,
51 CoarseTimerInterval = 200,
52 VeryCoarseTimerInterval = 1000
55 class tst_QEventDispatcher : public QObject
59 QAbstractEventDispatcher *eventDispatcher;
60 int receivedEventType;
64 bool event(QEvent *e);
67 inline tst_QEventDispatcher()
69 eventDispatcher(QAbstractEventDispatcher::instance(thread())),
70 receivedEventType(-1),
77 /* void registerSocketNotifier(); */ // Not implemented here, see tst_QSocketNotifier instead
78 /* void registerEventNotifiier(); */ // Not implemented here, see tst_QWinEventNotifier instead
79 void sendPostedEvents_data();
80 void sendPostedEvents();
83 bool tst_QEventDispatcher::event(QEvent *e)
85 switch (receivedEventType = e->type()) {
88 timerIdFromEvent = static_cast<QTimerEvent *>(e)->timerId();
94 return QObject::event(e);
97 // drain the system event queue after the test starts to avoid destabilizing the test functions
98 void tst_QEventDispatcher::initTestCase()
100 QElapsedTimer elapsedTimer;
101 elapsedTimer.start();
102 while (!elapsedTimer.hasExpired(CoarseTimerInterval) && eventDispatcher->processEvents(QEventLoop::AllEvents)) {
107 // test that the eventDispatcher's timer implementation is complete and working
108 void tst_QEventDispatcher::registerTimer()
110 #define FIND_TIMERS() \
112 foundPrecise = false; \
113 foundCoarse = false; \
114 foundVeryCoarse = false; \
115 for (int i = 0; i < registeredTimers.count(); ++i) { \
116 const QAbstractEventDispatcher::TimerInfo &timerInfo = registeredTimers.at(i); \
117 if (timerInfo.timerId == preciseTimerId) { \
118 QCOMPARE(timerInfo.interval, int(PreciseTimerInterval)); \
119 QCOMPARE(timerInfo.timerType, Qt::PreciseTimer); \
120 foundPrecise = true; \
121 } else if (timerInfo.timerId == coarseTimerId) { \
122 QCOMPARE(timerInfo.interval, int(CoarseTimerInterval)); \
123 QCOMPARE(timerInfo.timerType, Qt::CoarseTimer); \
124 foundCoarse = true; \
125 } else if (timerInfo.timerId == veryCoarseTimerId) { \
126 QCOMPARE(timerInfo.interval, int(VeryCoarseTimerInterval)); \
127 QCOMPARE(timerInfo.timerType, Qt::VeryCoarseTimer); \
128 foundVeryCoarse = true; \
133 // start 3 timers, each with the different timer types and different intervals
134 int preciseTimerId = eventDispatcher->registerTimer(PreciseTimerInterval, Qt::PreciseTimer, this);
135 int coarseTimerId = eventDispatcher->registerTimer(CoarseTimerInterval, Qt::CoarseTimer, this);
136 int veryCoarseTimerId = eventDispatcher->registerTimer(VeryCoarseTimerInterval, Qt::VeryCoarseTimer, this);
137 QVERIFY(preciseTimerId > 0);
138 QVERIFY(coarseTimerId > 0);
139 QVERIFY(veryCoarseTimerId > 0);
141 // check that all 3 are present in the eventDispatcher's registeredTimer() list
142 QList<QAbstractEventDispatcher::TimerInfo> registeredTimers = eventDispatcher->registeredTimers(this);
143 QCOMPARE(registeredTimers.count(), 3);
144 bool foundPrecise, foundCoarse, foundVeryCoarse;
146 QVERIFY(foundPrecise && foundCoarse && foundVeryCoarse);
148 // process events, waiting for the next event... this should only fire the precise timer
149 receivedEventType = -1;
150 timerIdFromEvent = -1;
151 QVERIFY(eventDispatcher->processEvents(QEventLoop::WaitForMoreEvents));
152 QCOMPARE(receivedEventType, int(QEvent::Timer));
153 QCOMPARE(timerIdFromEvent, preciseTimerId);
154 // now unregister it and make sure it's gone
155 eventDispatcher->unregisterTimer(preciseTimerId);
156 registeredTimers = eventDispatcher->registeredTimers(this);
157 QCOMPARE(registeredTimers.count(), 2);
159 QVERIFY(!foundPrecise && foundCoarse && foundVeryCoarse);
161 // do the same again for the coarse timer
162 receivedEventType = -1;
163 timerIdFromEvent = -1;
164 QVERIFY(eventDispatcher->processEvents(QEventLoop::WaitForMoreEvents));
165 QCOMPARE(receivedEventType, int(QEvent::Timer));
166 QCOMPARE(timerIdFromEvent, coarseTimerId);
167 // now unregister it and make sure it's gone
168 eventDispatcher->unregisterTimer(coarseTimerId);
169 registeredTimers = eventDispatcher->registeredTimers(this);
170 QCOMPARE(registeredTimers.count(), 1);
172 QVERIFY(!foundPrecise && !foundCoarse && foundVeryCoarse);
174 // not going to wait for the VeryCoarseTimer, would take too long, just unregister it
175 eventDispatcher->unregisterTimers(this);
176 registeredTimers = eventDispatcher->registeredTimers(this);
177 QVERIFY(registeredTimers.isEmpty());
182 void tst_QEventDispatcher::sendPostedEvents_data()
184 QTest::addColumn<int>("processEventsFlagsInt");
186 QTest::newRow("WaitForMoreEvents") << int(QEventLoop::WaitForMoreEvents);
187 QTest::newRow("AllEvents") << int(QEventLoop::AllEvents);
190 // test that the eventDispatcher sends posted events correctly
191 void tst_QEventDispatcher::sendPostedEvents()
193 QFETCH(int, processEventsFlagsInt);
194 QEventLoop::ProcessEventsFlags processEventsFlags = QEventLoop::ProcessEventsFlags(processEventsFlagsInt);
196 QElapsedTimer elapsedTimer;
197 elapsedTimer.start();
198 while (!elapsedTimer.hasExpired(200)) {
199 receivedEventType = -1;
200 QCoreApplication::postEvent(this, new QEvent(QEvent::User));
202 // event shouldn't be delivered as a result of posting
203 QCOMPARE(receivedEventType, -1);
205 // since there is a pending posted event, this should not actually block, it should send the posted event and return
206 QVERIFY(eventDispatcher->processEvents(processEventsFlags));
207 // event shouldn't be delivered as a result of posting
208 QCOMPARE(receivedEventType, int(QEvent::User));
212 QTEST_MAIN(tst_QEventDispatcher)
213 #include "tst_qeventdispatcher.moc"