Test posted events in tst_QEventDispatcher with various flags
[profile/ivi/qtbase.git] / tests / auto / corelib / kernel / qeventdispatcher / tst_qeventdispatcher.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifdef QT_GUI_LIB
43 #  include <QtGui/QGuiApplication>
44 #else
45 #  include <QtCore/QCoreApplication>
46 #endif
47 #include <QtTest/QtTest>
48
49 enum {
50     PreciseTimerInterval    =   10,
51     CoarseTimerInterval     =  200,
52     VeryCoarseTimerInterval = 1000
53 };
54
55 class tst_QEventDispatcher : public QObject
56 {
57     Q_OBJECT
58
59     QAbstractEventDispatcher *eventDispatcher;
60     int receivedEventType;
61     int timerIdFromEvent;
62
63 protected:
64     bool event(QEvent *e);
65
66 public:
67     inline tst_QEventDispatcher()
68         : QObject(),
69           eventDispatcher(QAbstractEventDispatcher::instance(thread())),
70           receivedEventType(-1),
71           timerIdFromEvent(-1)
72     { }
73
74 private slots:
75     void initTestCase();
76     void registerTimer();
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();
81 };
82
83 bool tst_QEventDispatcher::event(QEvent *e)
84 {
85     switch (receivedEventType = e->type()) {
86     case QEvent::Timer:
87     {
88         timerIdFromEvent = static_cast<QTimerEvent *>(e)->timerId();
89         return true;
90     }
91     default:
92         break;
93     }
94     return QObject::event(e);
95 }
96
97 // drain the system event queue after the test starts to avoid destabilizing the test functions
98 void tst_QEventDispatcher::initTestCase()
99 {
100     QElapsedTimer elapsedTimer;
101     elapsedTimer.start();
102     while (!elapsedTimer.hasExpired(CoarseTimerInterval) && eventDispatcher->processEvents(QEventLoop::AllEvents)) {
103             ;
104     }
105 }
106
107 // test that the eventDispatcher's timer implementation is complete and working
108 void tst_QEventDispatcher::registerTimer()
109 {
110 #define FIND_TIMERS() \
111         do { \
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; \
129                 } \
130             } \
131         } while (0)
132
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);
140
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;
145     FIND_TIMERS();
146     QVERIFY(foundPrecise && foundCoarse && foundVeryCoarse);
147
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);
158     FIND_TIMERS();
159     QVERIFY(!foundPrecise && foundCoarse && foundVeryCoarse);
160
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);
171     FIND_TIMERS();
172     QVERIFY(!foundPrecise && !foundCoarse && foundVeryCoarse);
173
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());
178
179 #undef FIND_TIMERS
180 }
181
182 void tst_QEventDispatcher::sendPostedEvents_data()
183 {
184     QTest::addColumn<int>("processEventsFlagsInt");
185
186     QTest::newRow("WaitForMoreEvents") << int(QEventLoop::WaitForMoreEvents);
187     QTest::newRow("AllEvents") << int(QEventLoop::AllEvents);
188 }
189
190 // test that the eventDispatcher sends posted events correctly
191 void tst_QEventDispatcher::sendPostedEvents()
192 {
193     QFETCH(int, processEventsFlagsInt);
194     QEventLoop::ProcessEventsFlags processEventsFlags = QEventLoop::ProcessEventsFlags(processEventsFlagsInt);
195
196     QElapsedTimer elapsedTimer;
197     elapsedTimer.start();
198     while (!elapsedTimer.hasExpired(200)) {
199         receivedEventType = -1;
200         QCoreApplication::postEvent(this, new QEvent(QEvent::User));
201
202         // event shouldn't be delivered as a result of posting
203         QCOMPARE(receivedEventType, -1);
204
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));
209     }
210 }
211
212 QTEST_MAIN(tst_QEventDispatcher)
213 #include "tst_qeventdispatcher.moc"