|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 #include <QApplication> |
|
43 #include <QWidget> |
|
44 #include <QDialog> |
|
45 #include <QPushButton> |
|
46 #include <QtTest/QtTest> |
|
47 |
|
48 #include "qnativeevents.h" |
|
49 #include "nativeeventlist.h" |
|
50 #include "expectedeventlist.h" |
|
51 |
|
52 #ifdef Q_OS_MAC |
|
53 |
|
54 QT_USE_NAMESPACE |
|
55 |
|
56 class tst_MacNativeEvents : public QObject |
|
57 { |
|
58 Q_OBJECT |
|
59 private slots: |
|
60 void testMouseMoveLocation(); |
|
61 void testPushButtonPressRelease(); |
|
62 void testMouseLeftDoubleClick(); |
|
63 void stressTestMouseLeftDoubleClick(); |
|
64 void testMouseDragInside(); |
|
65 void testMouseDragOutside(); |
|
66 void testMouseDragToNonClientArea(); |
|
67 void testDragWindow(); |
|
68 void testMouseEnter(); |
|
69 void testChildDialogInFrontOfModalParent(); |
|
70 void testKeyPressOnToplevel(); |
|
71 }; |
|
72 |
|
73 void tst_MacNativeEvents::testMouseMoveLocation() |
|
74 { |
|
75 QWidget w; |
|
76 w.setMouseTracking(true); |
|
77 w.show(); |
|
78 QPoint p = w.geometry().center(); |
|
79 |
|
80 NativeEventList native; |
|
81 native.append(new QNativeMouseMoveEvent(p, Qt::NoModifier)); |
|
82 |
|
83 ExpectedEventList expected(&w); |
|
84 expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(p), p, Qt::NoButton, Qt::NoButton, Qt::NoModifier)); |
|
85 |
|
86 native.play(); |
|
87 QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!"); |
|
88 } |
|
89 |
|
90 void tst_MacNativeEvents::testPushButtonPressRelease() |
|
91 { |
|
92 // Check that a native mouse press and release generates the |
|
93 // same qevents on a pushbutton: |
|
94 QPushButton w("click me"); |
|
95 w.show(); |
|
96 QPoint p = w.geometry().center(); |
|
97 |
|
98 NativeEventList native; |
|
99 native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 1, Qt::NoModifier)); |
|
100 native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier)); |
|
101 |
|
102 ExpectedEventList expected(&w); |
|
103 expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); |
|
104 expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::NoButton, Qt::NoModifier)); |
|
105 |
|
106 native.play(); |
|
107 QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!"); |
|
108 } |
|
109 |
|
110 void tst_MacNativeEvents::testMouseLeftDoubleClick() |
|
111 { |
|
112 // Check that a native double click makes |
|
113 // the test widget receive a press-release-click-release: |
|
114 QWidget w; |
|
115 w.show(); |
|
116 QPoint p = w.geometry().center(); |
|
117 |
|
118 NativeEventList native; |
|
119 native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 1, Qt::NoModifier)); |
|
120 native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier)); |
|
121 native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 2, Qt::NoModifier)); |
|
122 native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier)); |
|
123 |
|
124 ExpectedEventList expected(&w); |
|
125 expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); |
|
126 expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::NoButton, Qt::NoModifier)); |
|
127 expected.append(new QMouseEvent(QEvent::MouseButtonDblClick, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); |
|
128 expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::NoButton, Qt::NoModifier)); |
|
129 |
|
130 native.play(); |
|
131 QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!"); |
|
132 } |
|
133 |
|
134 void tst_MacNativeEvents::stressTestMouseLeftDoubleClick() |
|
135 { |
|
136 // Check that multiple, fast, double clicks makes |
|
137 // the test widget receive correct click events |
|
138 QWidget w; |
|
139 w.show(); |
|
140 QPoint p = w.geometry().center(); |
|
141 |
|
142 NativeEventList native; |
|
143 ExpectedEventList expected(&w); |
|
144 |
|
145 for (int i=0; i<10; ++i){ |
|
146 native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 1, Qt::NoModifier)); |
|
147 native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier)); |
|
148 native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 2, Qt::NoModifier)); |
|
149 native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier)); |
|
150 |
|
151 expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); |
|
152 expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::NoButton, Qt::NoModifier)); |
|
153 expected.append(new QMouseEvent(QEvent::MouseButtonDblClick, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); |
|
154 expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::NoButton, Qt::NoModifier)); |
|
155 } |
|
156 |
|
157 native.play(); |
|
158 QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!"); |
|
159 } |
|
160 |
|
161 void tst_MacNativeEvents::testMouseDragInside() |
|
162 { |
|
163 // Check that a mouse drag inside a widget |
|
164 // will cause press-move-release events to be delivered |
|
165 QWidget w; |
|
166 w.show(); |
|
167 QPoint p1 = w.geometry().center(); |
|
168 QPoint p2 = p1 - QPoint(10, 0); |
|
169 QPoint p3 = p1 - QPoint(20, 0); |
|
170 QPoint p4 = p1 - QPoint(30, 0); |
|
171 |
|
172 NativeEventList native; |
|
173 native.append(new QNativeMouseButtonEvent(p1, Qt::LeftButton, 1, Qt::NoModifier)); |
|
174 native.append(new QNativeMouseDragEvent(p2, Qt::LeftButton, Qt::NoModifier)); |
|
175 native.append(new QNativeMouseDragEvent(p3, Qt::LeftButton, Qt::NoModifier)); |
|
176 native.append(new QNativeMouseButtonEvent(p4, Qt::LeftButton, 0, Qt::NoModifier)); |
|
177 |
|
178 ExpectedEventList expected(&w); |
|
179 expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(p1), p1, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); |
|
180 expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(p2), p2, Qt::NoButton, Qt::LeftButton, Qt::NoModifier)); |
|
181 expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(p3), p3, Qt::NoButton, Qt::LeftButton, Qt::NoModifier)); |
|
182 expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p4), p4, Qt::LeftButton, Qt::NoButton, Qt::NoModifier)); |
|
183 |
|
184 native.play(); |
|
185 QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!"); |
|
186 } |
|
187 |
|
188 void tst_MacNativeEvents::testMouseDragOutside() |
|
189 { |
|
190 // Check that if we drag the mouse from inside the |
|
191 // widget, and release it outside, we still get mouse move |
|
192 // and release events when the mouse is outside the widget. |
|
193 QWidget w; |
|
194 w.show(); |
|
195 QPoint inside1 = w.geometry().center(); |
|
196 QPoint inside2 = inside1 - QPoint(10, 0); |
|
197 QPoint outside1 = w.geometry().topLeft() - QPoint(50, 0); |
|
198 QPoint outside2 = outside1 - QPoint(10, 0); |
|
199 |
|
200 NativeEventList native; |
|
201 native.append(new QNativeMouseButtonEvent(inside1, Qt::LeftButton, 1, Qt::NoModifier)); |
|
202 native.append(new QNativeMouseDragEvent(inside2, Qt::LeftButton, Qt::NoModifier)); |
|
203 native.append(new QNativeMouseDragEvent(outside1, Qt::LeftButton, Qt::NoModifier)); |
|
204 native.append(new QNativeMouseButtonEvent(outside2, Qt::LeftButton, 0, Qt::NoModifier)); |
|
205 |
|
206 ExpectedEventList expected(&w); |
|
207 expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(inside1), inside1, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); |
|
208 expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(inside2), inside2, Qt::NoButton, Qt::LeftButton, Qt::NoModifier)); |
|
209 expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(outside1), outside1, Qt::NoButton, Qt::LeftButton, Qt::NoModifier)); |
|
210 expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(outside2), outside2, Qt::LeftButton, Qt::NoButton, Qt::NoModifier)); |
|
211 |
|
212 native.play(); |
|
213 QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!"); |
|
214 } |
|
215 |
|
216 void tst_MacNativeEvents::testMouseDragToNonClientArea() |
|
217 { |
|
218 // Check that if we drag the mouse from inside the |
|
219 // widget, and release it on the title bar, we still get mouse move |
|
220 // and release events when the mouse is on the title bar |
|
221 QWidget w; |
|
222 w.show(); |
|
223 QPoint inside1 = w.geometry().center(); |
|
224 QPoint inside2 = inside1 - QPoint(10, 0); |
|
225 QPoint titlebar1 = w.geometry().topLeft() - QPoint(-100, 10); |
|
226 QPoint titlebar2 = titlebar1 - QPoint(10, 0); |
|
227 |
|
228 NativeEventList native; |
|
229 native.append(new QNativeMouseButtonEvent(inside1, Qt::LeftButton, 1, Qt::NoModifier)); |
|
230 native.append(new QNativeMouseDragEvent(inside2, Qt::LeftButton, Qt::NoModifier)); |
|
231 native.append(new QNativeMouseDragEvent(titlebar1, Qt::LeftButton, Qt::NoModifier)); |
|
232 native.append(new QNativeMouseButtonEvent(titlebar2, Qt::LeftButton, 0, Qt::NoModifier)); |
|
233 |
|
234 ExpectedEventList expected(&w); |
|
235 expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(inside1), inside1, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); |
|
236 expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(inside2), inside2, Qt::NoButton, Qt::LeftButton, Qt::NoModifier)); |
|
237 expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(titlebar1), titlebar1, Qt::NoButton, Qt::LeftButton, Qt::NoModifier)); |
|
238 expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(titlebar2), titlebar2, Qt::LeftButton, Qt::NoButton, Qt::NoModifier)); |
|
239 |
|
240 native.play(); |
|
241 QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!"); |
|
242 } |
|
243 |
|
244 void tst_MacNativeEvents::testDragWindow() |
|
245 { |
|
246 // Check that if we drag the mouse from inside the |
|
247 // widgets title bar, we get a move event on the window |
|
248 QWidget w; |
|
249 w.show(); |
|
250 QPoint titlebar = w.geometry().topLeft() - QPoint(-100, 10); |
|
251 QPoint moveTo = titlebar + QPoint(100, 0); |
|
252 |
|
253 NativeEventList native; |
|
254 native.append(new QNativeMouseButtonEvent(titlebar, Qt::LeftButton, 1, Qt::NoModifier)); |
|
255 native.append(new QNativeMouseDragEvent(moveTo, Qt::LeftButton, Qt::NoModifier)); |
|
256 native.append(500, new QNativeMouseButtonEvent(moveTo, Qt::LeftButton, 0, Qt::NoModifier)); |
|
257 |
|
258 ExpectedEventList expected(&w); |
|
259 expected.append(new QMouseEvent(QEvent::NonClientAreaMouseButtonPress, w.mapFromGlobal(titlebar), titlebar, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); |
|
260 expected.append(new QMouseEvent(QEvent::NonClientAreaMouseButtonRelease, w.mapFromGlobal(titlebar), moveTo, Qt::LeftButton, Qt::NoButton, Qt::NoModifier)); |
|
261 |
|
262 native.play(); |
|
263 QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!"); |
|
264 } |
|
265 |
|
266 void tst_MacNativeEvents::testMouseEnter() |
|
267 { |
|
268 // When a mouse enters a widget, both a mouse enter events and a |
|
269 // mouse move event should be sendt. Lets test this: |
|
270 QWidget w; |
|
271 w.setMouseTracking(true); |
|
272 w.show(); |
|
273 QPoint outside = w.geometry().topLeft() - QPoint(50, 0); |
|
274 QPoint inside = w.geometry().center(); |
|
275 |
|
276 NativeEventList native; |
|
277 native.append(new QNativeMouseMoveEvent(outside, Qt::NoModifier)); |
|
278 native.append(new QNativeMouseMoveEvent(inside, Qt::NoModifier)); |
|
279 |
|
280 ExpectedEventList expected(&w); |
|
281 expected.append(new QEvent(QEvent::Enter)); |
|
282 expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(inside), inside, Qt::NoButton, Qt::NoButton, Qt::NoModifier)); |
|
283 |
|
284 native.play(); |
|
285 QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!"); |
|
286 } |
|
287 |
|
288 void tst_MacNativeEvents::testChildDialogInFrontOfModalParent() |
|
289 { |
|
290 // Test that a child dialog of a modal parent dialog is |
|
291 // in front of the parent, and active: |
|
292 QDialog parent; |
|
293 parent.setWindowModality(Qt::ApplicationModal); |
|
294 QDialog child(&parent); |
|
295 QPushButton button("close", &child); |
|
296 connect(&button, SIGNAL(clicked()), &child, SLOT(close())); |
|
297 parent.show(); |
|
298 child.show(); |
|
299 QPoint inside = button.mapToGlobal(button.geometry().center()); |
|
300 |
|
301 // Post a click on the button to close the child dialog: |
|
302 NativeEventList native; |
|
303 native.append(new QNativeMouseButtonEvent(inside, Qt::LeftButton, 1, Qt::NoModifier)); |
|
304 native.append(new QNativeMouseButtonEvent(inside, Qt::LeftButton, 0, Qt::NoModifier)); |
|
305 |
|
306 native.play(); |
|
307 QTest::qWait(100); |
|
308 QVERIFY(!child.isVisible()); |
|
309 } |
|
310 |
|
311 void tst_MacNativeEvents::testKeyPressOnToplevel() |
|
312 { |
|
313 // Check that we receive keyevents for |
|
314 // toplevel widgets. For leagacy reasons, and according to Qt on |
|
315 // other platforms (carbon port + linux), we should get these events |
|
316 // even when the focus policy is set to Qt::NoFocus when there is no |
|
317 // other focus widget on screen: |
|
318 QWidget w; |
|
319 w.show(); |
|
320 |
|
321 NativeEventList native; |
|
322 native.append(new QNativeKeyEvent(QNativeKeyEvent::Key_A, true, Qt::NoModifier)); |
|
323 native.append(new QNativeKeyEvent(QNativeKeyEvent::Key_A, false, Qt::NoModifier)); |
|
324 |
|
325 ExpectedEventList expected(&w); |
|
326 expected.append(new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier)); |
|
327 expected.append(new QKeyEvent(QEvent::KeyRelease, Qt::Key_A, Qt::NoModifier)); |
|
328 |
|
329 native.play(); |
|
330 QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!"); |
|
331 } |
|
332 |
|
333 #include "tst_macnativeevents.moc" |
|
334 |
|
335 QTEST_MAIN(tst_MacNativeEvents) |
|
336 |
|
337 #else |
|
338 |
|
339 QTEST_NOOP_MAIN |
|
340 |
|
341 #endif |