author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 15 Mar 2010 12:43:09 +0200 | |
branch | RCL_3 |
changeset 6 | dee5afe5301f |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
#include <QtCore> |
|
42 |
||
43 |
#include <qtest.h> |
|
44 |
#include <qtesteventloop.h> |
|
45 |
||
46 |
class PingPong : public QObject |
|
47 |
{ |
|
48 |
public: |
|
49 |
void setPeer(QObject *peer); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
50 |
void resetCounter() {m_counter = 100;} |
0 | 51 |
|
52 |
protected: |
|
53 |
bool event(QEvent *e); |
|
54 |
||
55 |
private: |
|
56 |
QObject *m_peer; |
|
57 |
int m_counter; |
|
58 |
}; |
|
59 |
||
60 |
void PingPong::setPeer(QObject *peer) |
|
61 |
{ |
|
62 |
m_peer = peer; |
|
63 |
m_counter = 100; |
|
64 |
} |
|
65 |
||
66 |
bool PingPong::event(QEvent *) |
|
67 |
{ |
|
68 |
--m_counter; |
|
69 |
if (m_counter > 0) { |
|
70 |
QEvent *e = new QEvent(QEvent::User); |
|
71 |
QCoreApplication::postEvent(m_peer, e); |
|
72 |
} else { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
73 |
QTestEventLoop::instance().exitLoop(); |
0 | 74 |
} |
75 |
return true; |
|
76 |
} |
|
77 |
||
78 |
class EventTester : public QObject |
|
79 |
{ |
|
80 |
public: |
|
81 |
int foo(int bar); |
|
82 |
||
83 |
protected: |
|
84 |
bool event(QEvent *e); |
|
85 |
}; |
|
86 |
||
87 |
bool EventTester::event(QEvent *e) |
|
88 |
{ |
|
89 |
if (e->type() == QEvent::User+1) |
|
90 |
return foo(e->type()) != 0; |
|
91 |
return false; |
|
92 |
} |
|
93 |
||
94 |
int EventTester::foo(int bar) |
|
95 |
{ |
|
96 |
return bar + 1; |
|
97 |
} |
|
98 |
||
99 |
class EventsBench : public QObject |
|
100 |
{ |
|
101 |
Q_OBJECT |
|
102 |
||
103 |
private slots: |
|
104 |
void initTestCase(); |
|
105 |
void cleanupTestCase(); |
|
106 |
||
107 |
void noEvent(); |
|
108 |
void sendEvent_data(); |
|
109 |
void sendEvent(); |
|
110 |
void postEvent_data(); |
|
111 |
void postEvent(); |
|
112 |
}; |
|
113 |
||
114 |
void EventsBench::initTestCase() |
|
115 |
{ |
|
116 |
} |
|
117 |
||
118 |
void EventsBench::cleanupTestCase() |
|
119 |
{ |
|
120 |
} |
|
121 |
||
122 |
void EventsBench::noEvent() |
|
123 |
{ |
|
124 |
EventTester tst; |
|
125 |
int val = 0; |
|
126 |
QBENCHMARK { |
|
127 |
val += tst.foo(1); |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
void EventsBench::sendEvent_data() |
|
132 |
{ |
|
133 |
QTest::addColumn<bool>("filterEvents"); |
|
134 |
QTest::newRow("no eventfilter") << false; |
|
135 |
QTest::newRow("eventfilter") << true; |
|
136 |
} |
|
137 |
||
138 |
void EventsBench::sendEvent() |
|
139 |
{ |
|
140 |
QFETCH(bool, filterEvents); |
|
141 |
EventTester tst; |
|
142 |
if (filterEvents) |
|
143 |
tst.installEventFilter(this); |
|
144 |
QEvent evt(QEvent::Type(QEvent::User+1)); |
|
145 |
QBENCHMARK { |
|
146 |
QCoreApplication::sendEvent(&tst, &evt); |
|
147 |
} |
|
148 |
} |
|
149 |
||
150 |
void EventsBench::postEvent_data() |
|
151 |
{ |
|
152 |
QTest::addColumn<bool>("filterEvents"); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
153 |
// The first time an eventloop is executed, the case runs radically slower at least |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
154 |
// on some platforms, so test the "no eventfilter" case to get a comparable results |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
155 |
// with the "eventfilter" case. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
156 |
QTest::newRow("first time, no eventfilter") << false; |
0 | 157 |
QTest::newRow("no eventfilter") << false; |
158 |
QTest::newRow("eventfilter") << true; |
|
159 |
} |
|
160 |
||
161 |
void EventsBench::postEvent() |
|
162 |
{ |
|
163 |
QFETCH(bool, filterEvents); |
|
164 |
PingPong ping; |
|
165 |
PingPong pong; |
|
166 |
ping.setPeer(&pong); |
|
167 |
pong.setPeer(&ping); |
|
168 |
if (filterEvents) { |
|
169 |
ping.installEventFilter(this); |
|
170 |
pong.installEventFilter(this); |
|
171 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
172 |
|
0 | 173 |
QBENCHMARK { |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
174 |
// In case multiple iterations are done, event needs to be created inside the QBENCHMARK, |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
175 |
// or it gets deleted once first iteration exits and can cause a crash. Similarly, |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
176 |
// ping and pong need their counters reset. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
177 |
QEvent *e = new QEvent(QEvent::User); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
178 |
ping.resetCounter(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
179 |
pong.resetCounter(); |
0 | 180 |
QCoreApplication::postEvent(&ping, e); |
181 |
QTestEventLoop::instance().enterLoop( 61 ); |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
QTEST_MAIN(EventsBench) |
|
186 |
||
187 |
#include "main.moc" |