|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
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 |
|
42 |
|
43 #include <QtTest/QtTest> |
|
44 #include <qapplication.h> |
|
45 #include <qlineedit.h> |
|
46 #include <qlabel.h> |
|
47 #include <qdialog.h> |
|
48 |
|
49 |
|
50 #include <qevent.h> |
|
51 #include <qwidget.h> |
|
52 |
|
53 //TESTED_CLASS= |
|
54 //TESTED_FILES=gui/widgets/qmenubar.h gui/widgets/qmenubar.cpp |
|
55 |
|
56 class MouseEventWidget : public QWidget |
|
57 { |
|
58 public: |
|
59 MouseEventWidget(QWidget *parent = 0) : QWidget(parent) |
|
60 { |
|
61 setFocusPolicy(Qt::StrongFocus); |
|
62 } |
|
63 bool mousePressEventRecieved; |
|
64 bool mouseReleaseEventRecieved; |
|
65 #ifdef QT3_SUPPORT |
|
66 int mousePressStateBefore; |
|
67 int mousePressStateAfter; |
|
68 #endif |
|
69 int mousePressButton; |
|
70 int mousePressButtons; |
|
71 int mousePressModifiers; |
|
72 #ifdef QT3_SUPPORT |
|
73 int mouseReleaseStateBefore; |
|
74 int mouseReleaseStateAfter; |
|
75 #endif |
|
76 int mouseReleaseButton; |
|
77 int mouseReleaseButtons; |
|
78 int mouseReleaseModifiers; |
|
79 protected: |
|
80 void mousePressEvent(QMouseEvent *e) |
|
81 { |
|
82 QWidget::mousePressEvent(e); |
|
83 #ifdef QT3_SUPPORT |
|
84 mousePressStateBefore = e->state(); |
|
85 mousePressStateAfter = e->stateAfter(); |
|
86 #endif |
|
87 mousePressButton = e->button(); |
|
88 mousePressButtons = e->buttons(); |
|
89 mousePressModifiers = e->modifiers(); |
|
90 mousePressEventRecieved = TRUE; |
|
91 e->accept(); |
|
92 } |
|
93 void mouseReleaseEvent(QMouseEvent *e) |
|
94 { |
|
95 QWidget::mouseReleaseEvent(e); |
|
96 #ifdef QT3_SUPPORT |
|
97 mouseReleaseStateBefore = e->state(); |
|
98 mouseReleaseStateAfter = e->stateAfter(); |
|
99 #endif |
|
100 mouseReleaseButton = e->button(); |
|
101 mouseReleaseButtons = e->buttons(); |
|
102 mouseReleaseModifiers = e->modifiers(); |
|
103 mouseReleaseEventRecieved = TRUE; |
|
104 e->accept(); |
|
105 } |
|
106 }; |
|
107 |
|
108 class tst_QMouseEvent : public QObject |
|
109 { |
|
110 Q_OBJECT |
|
111 |
|
112 public: |
|
113 tst_QMouseEvent(); |
|
114 virtual ~tst_QMouseEvent(); |
|
115 |
|
116 |
|
117 public slots: |
|
118 void initTestCase(); |
|
119 void cleanupTestCase(); |
|
120 void init(); |
|
121 void cleanup(); |
|
122 private slots: |
|
123 void checkMousePressEvent_data(); |
|
124 void checkMousePressEvent(); |
|
125 void checkMouseReleaseEvent_data(); |
|
126 void checkMouseReleaseEvent(); |
|
127 |
|
128 void qt3supportConstructors(); |
|
129 |
|
130 private: |
|
131 MouseEventWidget* testMouseWidget; |
|
132 }; |
|
133 |
|
134 |
|
135 |
|
136 tst_QMouseEvent::tst_QMouseEvent() |
|
137 { |
|
138 } |
|
139 |
|
140 tst_QMouseEvent::~tst_QMouseEvent() |
|
141 { |
|
142 |
|
143 } |
|
144 |
|
145 void tst_QMouseEvent::initTestCase() |
|
146 { |
|
147 testMouseWidget = new MouseEventWidget(0); |
|
148 testMouseWidget->show(); |
|
149 } |
|
150 |
|
151 void tst_QMouseEvent::cleanupTestCase() |
|
152 { |
|
153 delete testMouseWidget; |
|
154 } |
|
155 |
|
156 void tst_QMouseEvent::init() |
|
157 { |
|
158 testMouseWidget->mousePressEventRecieved = FALSE; |
|
159 testMouseWidget->mouseReleaseEventRecieved = FALSE; |
|
160 #ifdef QT3_SUPPORT |
|
161 testMouseWidget->mousePressStateBefore = 0; |
|
162 testMouseWidget->mousePressStateAfter = 0; |
|
163 testMouseWidget->mouseReleaseStateBefore = 0; |
|
164 testMouseWidget->mouseReleaseStateAfter = 0; |
|
165 #endif |
|
166 testMouseWidget->mousePressButton = 0; |
|
167 testMouseWidget->mousePressButtons = 0; |
|
168 testMouseWidget->mousePressModifiers = 0; |
|
169 testMouseWidget->mouseReleaseButton = 0; |
|
170 testMouseWidget->mouseReleaseButtons = 0; |
|
171 testMouseWidget->mouseReleaseModifiers = 0; |
|
172 } |
|
173 |
|
174 void tst_QMouseEvent::cleanup() |
|
175 { |
|
176 } |
|
177 |
|
178 void tst_QMouseEvent::checkMousePressEvent_data() |
|
179 { |
|
180 QTest::addColumn<int>("buttonPressed"); |
|
181 QTest::addColumn<int>("keyPressed"); |
|
182 |
|
183 QTest::newRow("leftButton-nokey") << int(Qt::LeftButton) << int(Qt::NoButton); |
|
184 QTest::newRow("leftButton-shiftkey") << int(Qt::LeftButton) << int(Qt::ShiftModifier); |
|
185 QTest::newRow("leftButton-controlkey") << int(Qt::LeftButton) << int(Qt::ControlModifier); |
|
186 QTest::newRow("leftButton-altkey") << int(Qt::LeftButton) << int(Qt::AltModifier); |
|
187 QTest::newRow("leftButton-metakey") << int(Qt::LeftButton) << int(Qt::MetaModifier); |
|
188 QTest::newRow("rightButton-nokey") << int(Qt::RightButton) << int(Qt::NoButton); |
|
189 QTest::newRow("rightButton-shiftkey") << int(Qt::RightButton) << int(Qt::ShiftModifier); |
|
190 QTest::newRow("rightButton-controlkey") << int(Qt::RightButton) << int(Qt::ControlModifier); |
|
191 QTest::newRow("rightButton-altkey") << int(Qt::RightButton) << int(Qt::AltModifier); |
|
192 QTest::newRow("rightButton-metakey") << int(Qt::RightButton) << int(Qt::MetaModifier); |
|
193 QTest::newRow("midButton-nokey") << int(Qt::MidButton) << int(Qt::NoButton); |
|
194 QTest::newRow("midButton-shiftkey") << int(Qt::MidButton) << int(Qt::ShiftModifier); |
|
195 QTest::newRow("midButton-controlkey") << int(Qt::MidButton) << int(Qt::ControlModifier); |
|
196 QTest::newRow("midButton-altkey") << int(Qt::MidButton) << int(Qt::AltModifier); |
|
197 QTest::newRow("midButton-metakey") << int(Qt::MidButton) << int(Qt::MetaModifier); |
|
198 } |
|
199 |
|
200 void tst_QMouseEvent::checkMousePressEvent() |
|
201 { |
|
202 QFETCH(int,buttonPressed); |
|
203 QFETCH(int,keyPressed); |
|
204 int button = buttonPressed; |
|
205 int buttons = button; |
|
206 int modifiers = keyPressed; |
|
207 |
|
208 QTest::mousePress(testMouseWidget, Qt::MouseButton(buttonPressed), Qt::KeyboardModifiers(keyPressed)); |
|
209 QVERIFY(testMouseWidget->mousePressEventRecieved); |
|
210 QCOMPARE(testMouseWidget->mousePressButton, button); |
|
211 QCOMPARE(testMouseWidget->mousePressButtons, buttons); |
|
212 QCOMPARE(testMouseWidget->mousePressModifiers, modifiers); |
|
213 #ifdef QT3_SUPPORT |
|
214 int stateAfter = buttons|modifiers; |
|
215 int stateBefore = stateAfter & ~button; |
|
216 |
|
217 QCOMPARE(testMouseWidget->mousePressStateBefore, stateBefore); |
|
218 QCOMPARE(testMouseWidget->mousePressStateAfter, stateAfter); |
|
219 #endif |
|
220 |
|
221 QTest::mouseRelease(testMouseWidget, Qt::MouseButton(buttonPressed), Qt::KeyboardModifiers(keyPressed)); |
|
222 } |
|
223 |
|
224 void tst_QMouseEvent::checkMouseReleaseEvent_data() |
|
225 { |
|
226 QTest::addColumn<int>("buttonReleased"); |
|
227 QTest::addColumn<int>("keyPressed"); |
|
228 |
|
229 QTest::newRow("leftButton-nokey") << int(Qt::LeftButton) << int(Qt::NoButton); |
|
230 QTest::newRow("leftButton-shiftkey") << int(Qt::LeftButton) << int(Qt::ShiftModifier); |
|
231 QTest::newRow("leftButton-controlkey") << int(Qt::LeftButton) << int(Qt::ControlModifier); |
|
232 QTest::newRow("leftButton-altkey") << int(Qt::LeftButton) << int(Qt::AltModifier); |
|
233 QTest::newRow("leftButton-metakey") << int(Qt::LeftButton) << int(Qt::MetaModifier); |
|
234 QTest::newRow("rightButton-nokey") << int(Qt::RightButton) << int(Qt::NoButton); |
|
235 QTest::newRow("rightButton-shiftkey") << int(Qt::RightButton) << int(Qt::ShiftModifier); |
|
236 QTest::newRow("rightButton-controlkey") << int(Qt::RightButton) << int(Qt::ControlModifier); |
|
237 QTest::newRow("rightButton-altkey") << int(Qt::RightButton) << int(Qt::AltModifier); |
|
238 QTest::newRow("rightButton-metakey") << int(Qt::RightButton) << int(Qt::MetaModifier); |
|
239 QTest::newRow("midButton-nokey") << int(Qt::MidButton) << int(Qt::NoButton); |
|
240 QTest::newRow("midButton-shiftkey") << int(Qt::MidButton) << int(Qt::ShiftModifier); |
|
241 QTest::newRow("midButton-controlkey") << int(Qt::MidButton) << int(Qt::ControlModifier); |
|
242 QTest::newRow("midButton-altkey") << int(Qt::MidButton) << int(Qt::AltModifier); |
|
243 QTest::newRow("midButton-metakey") << int(Qt::MidButton) << int(Qt::MetaModifier); |
|
244 } |
|
245 |
|
246 void tst_QMouseEvent::checkMouseReleaseEvent() |
|
247 { |
|
248 QFETCH(int,buttonReleased); |
|
249 QFETCH(int,keyPressed); |
|
250 int button = buttonReleased; |
|
251 int buttons = 0; |
|
252 int modifiers = keyPressed; |
|
253 |
|
254 QTest::mouseClick(testMouseWidget, Qt::MouseButton(buttonReleased), Qt::KeyboardModifiers(keyPressed)); |
|
255 QVERIFY(testMouseWidget->mouseReleaseEventRecieved); |
|
256 QCOMPARE(testMouseWidget->mouseReleaseButton, button); |
|
257 QCOMPARE(testMouseWidget->mouseReleaseButtons, buttons); |
|
258 QCOMPARE(testMouseWidget->mouseReleaseModifiers, modifiers); |
|
259 #ifdef QT3_SUPPORT |
|
260 int stateAfter = buttons|modifiers; |
|
261 int stateBefore = stateAfter|button; |
|
262 |
|
263 QCOMPARE(testMouseWidget->mouseReleaseStateBefore, stateBefore); |
|
264 QCOMPARE(testMouseWidget->mouseReleaseStateAfter, stateAfter); |
|
265 #endif |
|
266 } |
|
267 |
|
268 void tst_QMouseEvent::qt3supportConstructors() |
|
269 { |
|
270 #if !defined(QT3_SUPPORT) |
|
271 QSKIP( "No Qt3Support present", SkipAll); |
|
272 #else |
|
273 // make sure the state() and stateAfter() functions return the |
|
274 // same thing they did in Qt 3 when using these constructors |
|
275 |
|
276 { |
|
277 QMouseEvent e(QEvent::MouseButtonPress, QPoint(0, 0), Qt::LeftButton, 0); |
|
278 QCOMPARE(e.state(), Qt::ButtonState(Qt::NoButton)); |
|
279 QCOMPARE(e.stateAfter(), Qt::ButtonState(Qt::LeftButton)); |
|
280 } |
|
281 |
|
282 { |
|
283 QMouseEvent e(QEvent::MouseButtonPress, QPoint(0, 0), QPoint(0, 0), Qt::LeftButton, 0); |
|
284 QCOMPARE(e.state(), Qt::ButtonState(Qt::NoButton)); |
|
285 QCOMPARE(e.stateAfter(), Qt::ButtonState(Qt::LeftButton)); |
|
286 } |
|
287 #endif |
|
288 } |
|
289 |
|
290 QTEST_MAIN(tst_QMouseEvent) |
|
291 #include "tst_qmouseevent.moc" |