|
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 #include <QtTest/QtTest> |
|
43 #include <QtTest/QSignalSpy> |
|
44 #include <private/qdeclarativemousearea_p.h> |
|
45 #include <private/qdeclarativerectangle_p.h> |
|
46 #include <QtDeclarative/qdeclarativeview.h> |
|
47 #include <QtDeclarative/qdeclarativecontext.h> |
|
48 |
|
49 class tst_QDeclarativeMouseArea: public QObject |
|
50 { |
|
51 Q_OBJECT |
|
52 private slots: |
|
53 void dragProperties(); |
|
54 void resetDrag(); |
|
55 void dragging(); |
|
56 void updateMouseAreaPosOnClick(); |
|
57 void updateMouseAreaPosOnResize(); |
|
58 void noOnClickedWithPressAndHold(); |
|
59 void onMousePressRejected(); |
|
60 |
|
61 private: |
|
62 QDeclarativeView *createView(); |
|
63 }; |
|
64 |
|
65 void tst_QDeclarativeMouseArea::dragProperties() |
|
66 { |
|
67 QDeclarativeView *canvas = createView(); |
|
68 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/dragproperties.qml")); |
|
69 canvas->show(); |
|
70 canvas->setFocus(); |
|
71 QVERIFY(canvas->rootObject() != 0); |
|
72 |
|
73 QDeclarativeMouseArea *mouseRegion = canvas->rootObject()->findChild<QDeclarativeMouseArea*>("mouseregion"); |
|
74 QDeclarativeDrag *drag = mouseRegion->drag(); |
|
75 QVERIFY(mouseRegion != 0); |
|
76 QVERIFY(drag != 0); |
|
77 |
|
78 // target |
|
79 QDeclarativeItem *blackRect = canvas->rootObject()->findChild<QDeclarativeItem*>("blackrect"); |
|
80 QVERIFY(blackRect != 0); |
|
81 QVERIFY(blackRect == drag->target()); |
|
82 QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(canvas->rootObject()); |
|
83 QVERIFY(rootItem != 0); |
|
84 QSignalSpy targetSpy(drag, SIGNAL(targetChanged())); |
|
85 drag->setTarget(rootItem); |
|
86 QCOMPARE(targetSpy.count(),1); |
|
87 drag->setTarget(rootItem); |
|
88 QCOMPARE(targetSpy.count(),1); |
|
89 |
|
90 // axis |
|
91 QCOMPARE(drag->axis(), QDeclarativeDrag::XandYAxis); |
|
92 QSignalSpy axisSpy(drag, SIGNAL(axisChanged())); |
|
93 drag->setAxis(QDeclarativeDrag::XAxis); |
|
94 QCOMPARE(drag->axis(), QDeclarativeDrag::XAxis); |
|
95 QCOMPARE(axisSpy.count(),1); |
|
96 drag->setAxis(QDeclarativeDrag::XAxis); |
|
97 QCOMPARE(axisSpy.count(),1); |
|
98 |
|
99 // minimum and maximum properties |
|
100 QSignalSpy xminSpy(drag, SIGNAL(minimumXChanged())); |
|
101 QSignalSpy xmaxSpy(drag, SIGNAL(maximumXChanged())); |
|
102 QSignalSpy yminSpy(drag, SIGNAL(minimumYChanged())); |
|
103 QSignalSpy ymaxSpy(drag, SIGNAL(maximumYChanged())); |
|
104 |
|
105 QCOMPARE(drag->xmin(), 0.0); |
|
106 QCOMPARE(drag->xmax(), rootItem->width()-blackRect->width()); |
|
107 QCOMPARE(drag->ymin(), 0.0); |
|
108 QCOMPARE(drag->ymax(), rootItem->height()-blackRect->height()); |
|
109 |
|
110 drag->setXmin(10); |
|
111 drag->setXmax(10); |
|
112 drag->setYmin(10); |
|
113 drag->setYmax(10); |
|
114 |
|
115 QCOMPARE(drag->xmin(), 10.0); |
|
116 QCOMPARE(drag->xmax(), 10.0); |
|
117 QCOMPARE(drag->ymin(), 10.0); |
|
118 QCOMPARE(drag->ymax(), 10.0); |
|
119 |
|
120 QCOMPARE(xminSpy.count(),1); |
|
121 QCOMPARE(xmaxSpy.count(),1); |
|
122 QCOMPARE(yminSpy.count(),1); |
|
123 QCOMPARE(ymaxSpy.count(),1); |
|
124 |
|
125 drag->setXmin(10); |
|
126 drag->setXmax(10); |
|
127 drag->setYmin(10); |
|
128 drag->setYmax(10); |
|
129 |
|
130 QCOMPARE(xminSpy.count(),1); |
|
131 QCOMPARE(xmaxSpy.count(),1); |
|
132 QCOMPARE(yminSpy.count(),1); |
|
133 QCOMPARE(ymaxSpy.count(),1); |
|
134 |
|
135 delete canvas; |
|
136 } |
|
137 |
|
138 void tst_QDeclarativeMouseArea::resetDrag() |
|
139 { |
|
140 QDeclarativeView *canvas = createView(); |
|
141 |
|
142 canvas->rootContext()->setContextProperty("haveTarget", QVariant(true)); |
|
143 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/dragreset.qml")); |
|
144 canvas->show(); |
|
145 canvas->setFocus(); |
|
146 QVERIFY(canvas->rootObject() != 0); |
|
147 |
|
148 QDeclarativeMouseArea *mouseRegion = canvas->rootObject()->findChild<QDeclarativeMouseArea*>("mouseregion"); |
|
149 QDeclarativeDrag *drag = mouseRegion->drag(); |
|
150 QVERIFY(mouseRegion != 0); |
|
151 QVERIFY(drag != 0); |
|
152 |
|
153 // target |
|
154 QDeclarativeItem *blackRect = canvas->rootObject()->findChild<QDeclarativeItem*>("blackrect"); |
|
155 QVERIFY(blackRect != 0); |
|
156 QVERIFY(blackRect == drag->target()); |
|
157 QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(canvas->rootObject()); |
|
158 QVERIFY(rootItem != 0); |
|
159 QSignalSpy targetSpy(drag, SIGNAL(targetChanged())); |
|
160 QVERIFY(drag->target() != 0); |
|
161 canvas->rootContext()->setContextProperty("haveTarget", QVariant(false)); |
|
162 QCOMPARE(targetSpy.count(),1); |
|
163 QVERIFY(drag->target() == 0); |
|
164 |
|
165 delete canvas; |
|
166 } |
|
167 |
|
168 |
|
169 void tst_QDeclarativeMouseArea::dragging() |
|
170 { |
|
171 QDeclarativeView *canvas = createView(); |
|
172 |
|
173 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/dragging.qml")); |
|
174 canvas->show(); |
|
175 canvas->setFocus(); |
|
176 QVERIFY(canvas->rootObject() != 0); |
|
177 |
|
178 QDeclarativeMouseArea *mouseRegion = canvas->rootObject()->findChild<QDeclarativeMouseArea*>("mouseregion"); |
|
179 QDeclarativeDrag *drag = mouseRegion->drag(); |
|
180 QVERIFY(mouseRegion != 0); |
|
181 QVERIFY(drag != 0); |
|
182 |
|
183 // target |
|
184 QDeclarativeItem *blackRect = canvas->rootObject()->findChild<QDeclarativeItem*>("blackrect"); |
|
185 QVERIFY(blackRect != 0); |
|
186 QVERIFY(blackRect == drag->target()); |
|
187 |
|
188 QVERIFY(!drag->active()); |
|
189 |
|
190 QGraphicsScene *scene = canvas->scene(); |
|
191 QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); |
|
192 pressEvent.setScenePos(QPointF(100, 100)); |
|
193 pressEvent.setButton(Qt::LeftButton); |
|
194 pressEvent.setButtons(Qt::LeftButton); |
|
195 QApplication::sendEvent(scene, &pressEvent); |
|
196 |
|
197 QVERIFY(!drag->active()); |
|
198 QCOMPARE(blackRect->x(), 50.0); |
|
199 QCOMPARE(blackRect->y(), 50.0); |
|
200 |
|
201 QGraphicsSceneMouseEvent moveEvent(QEvent::GraphicsSceneMouseMove); |
|
202 moveEvent.setScenePos(QPointF(110, 110)); |
|
203 moveEvent.setButton(Qt::LeftButton); |
|
204 moveEvent.setButtons(Qt::LeftButton); |
|
205 QApplication::sendEvent(scene, &moveEvent); |
|
206 |
|
207 QVERIFY(drag->active()); |
|
208 QCOMPARE(blackRect->x(), 60.0); |
|
209 QCOMPARE(blackRect->y(), 60.0); |
|
210 |
|
211 QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); |
|
212 releaseEvent.setScenePos(QPointF(110, 110)); |
|
213 releaseEvent.setButton(Qt::LeftButton); |
|
214 releaseEvent.setButtons(Qt::LeftButton); |
|
215 QApplication::sendEvent(scene, &releaseEvent); |
|
216 |
|
217 QVERIFY(!drag->active()); |
|
218 QCOMPARE(blackRect->x(), 60.0); |
|
219 QCOMPARE(blackRect->y(), 60.0); |
|
220 |
|
221 delete canvas; |
|
222 } |
|
223 |
|
224 QDeclarativeView *tst_QDeclarativeMouseArea::createView() |
|
225 { |
|
226 QDeclarativeView *canvas = new QDeclarativeView(0); |
|
227 canvas->setFixedSize(240,320); |
|
228 |
|
229 return canvas; |
|
230 } |
|
231 |
|
232 void tst_QDeclarativeMouseArea::updateMouseAreaPosOnClick() |
|
233 { |
|
234 QDeclarativeView *canvas = createView(); |
|
235 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/updateMousePosOnClick.qml")); |
|
236 canvas->show(); |
|
237 canvas->setFocus(); |
|
238 QVERIFY(canvas->rootObject() != 0); |
|
239 |
|
240 QDeclarativeMouseArea *mouseRegion = canvas->rootObject()->findChild<QDeclarativeMouseArea*>("mouseregion"); |
|
241 QVERIFY(mouseRegion != 0); |
|
242 |
|
243 QDeclarativeRectangle *rect = canvas->rootObject()->findChild<QDeclarativeRectangle*>("ball"); |
|
244 QVERIFY(rect != 0); |
|
245 |
|
246 QCOMPARE(mouseRegion->mouseX(), rect->x()); |
|
247 QCOMPARE(mouseRegion->mouseY(), rect->y()); |
|
248 |
|
249 QGraphicsScene *scene = canvas->scene(); |
|
250 QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress); |
|
251 event.setScenePos(QPointF(100, 100)); |
|
252 event.setButton(Qt::LeftButton); |
|
253 event.setButtons(Qt::LeftButton); |
|
254 QApplication::sendEvent(scene, &event); |
|
255 |
|
256 QCOMPARE(mouseRegion->mouseX(), 100.0); |
|
257 QCOMPARE(mouseRegion->mouseY(), 100.0); |
|
258 |
|
259 QCOMPARE(mouseRegion->mouseX(), rect->x()); |
|
260 QCOMPARE(mouseRegion->mouseY(), rect->y()); |
|
261 |
|
262 delete canvas; |
|
263 } |
|
264 |
|
265 void tst_QDeclarativeMouseArea::updateMouseAreaPosOnResize() |
|
266 { |
|
267 QDeclarativeView *canvas = createView(); |
|
268 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/updateMousePosOnResize.qml")); |
|
269 canvas->show(); |
|
270 canvas->setFocus(); |
|
271 QVERIFY(canvas->rootObject() != 0); |
|
272 |
|
273 QDeclarativeMouseArea *mouseRegion = canvas->rootObject()->findChild<QDeclarativeMouseArea*>("mouseregion"); |
|
274 QVERIFY(mouseRegion != 0); |
|
275 |
|
276 QDeclarativeRectangle *rect = canvas->rootObject()->findChild<QDeclarativeRectangle*>("brother"); |
|
277 QVERIFY(rect != 0); |
|
278 |
|
279 QCOMPARE(mouseRegion->mouseX(), 0.0); |
|
280 QCOMPARE(mouseRegion->mouseY(), 0.0); |
|
281 |
|
282 QGraphicsScene *scene = canvas->scene(); |
|
283 QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress); |
|
284 event.setScenePos(rect->pos()); |
|
285 event.setButton(Qt::LeftButton); |
|
286 event.setButtons(Qt::LeftButton); |
|
287 QApplication::sendEvent(scene, &event); |
|
288 |
|
289 QVERIFY(!mouseRegion->property("emitPositionChanged").toBool()); |
|
290 QVERIFY(mouseRegion->property("mouseMatchesPos").toBool()); |
|
291 |
|
292 QCOMPARE(mouseRegion->property("x1").toInt(), 0); |
|
293 QCOMPARE(mouseRegion->property("y1").toInt(), 0); |
|
294 |
|
295 // XXX: is it on purpose that mouseX is real and mouse.x is int? |
|
296 QCOMPARE(mouseRegion->property("x2").toInt(), (int) rect->x()); |
|
297 QCOMPARE(mouseRegion->property("y2").toInt(), (int) rect->y()); |
|
298 |
|
299 QCOMPARE(mouseRegion->mouseX(), rect->x()); |
|
300 QCOMPARE(mouseRegion->mouseY(), rect->y()); |
|
301 |
|
302 delete canvas; |
|
303 } |
|
304 |
|
305 void tst_QDeclarativeMouseArea::noOnClickedWithPressAndHold() |
|
306 { |
|
307 QDeclarativeView *canvas = createView(); |
|
308 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/clickandhold.qml")); |
|
309 canvas->show(); |
|
310 canvas->setFocus(); |
|
311 QVERIFY(canvas->rootObject() != 0); |
|
312 |
|
313 QGraphicsScene *scene = canvas->scene(); |
|
314 QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); |
|
315 pressEvent.setScenePos(QPointF(100, 100)); |
|
316 pressEvent.setButton(Qt::LeftButton); |
|
317 pressEvent.setButtons(Qt::LeftButton); |
|
318 QApplication::sendEvent(scene, &pressEvent); |
|
319 |
|
320 QVERIFY(!canvas->rootObject()->property("clicked").toBool()); |
|
321 QVERIFY(!canvas->rootObject()->property("held").toBool()); |
|
322 |
|
323 QTest::qWait(1000); |
|
324 |
|
325 QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); |
|
326 releaseEvent.setScenePos(QPointF(100, 100)); |
|
327 releaseEvent.setButton(Qt::LeftButton); |
|
328 releaseEvent.setButtons(Qt::LeftButton); |
|
329 QApplication::sendEvent(scene, &releaseEvent); |
|
330 |
|
331 QVERIFY(!canvas->rootObject()->property("clicked").toBool()); |
|
332 QVERIFY(canvas->rootObject()->property("held").toBool()); |
|
333 } |
|
334 |
|
335 void tst_QDeclarativeMouseArea::onMousePressRejected() |
|
336 { |
|
337 QDeclarativeView *canvas = createView(); |
|
338 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/rejectEvent.qml")); |
|
339 canvas->show(); |
|
340 canvas->setFocus(); |
|
341 QVERIFY(canvas->rootObject() != 0); |
|
342 |
|
343 QVERIFY(!canvas->rootObject()->property("mr1_pressed").toBool()); |
|
344 QVERIFY(!canvas->rootObject()->property("mr1_released").toBool()); |
|
345 QVERIFY(!canvas->rootObject()->property("mr1_canceled").toBool()); |
|
346 QVERIFY(!canvas->rootObject()->property("mr2_pressed").toBool()); |
|
347 QVERIFY(!canvas->rootObject()->property("mr2_released").toBool()); |
|
348 QVERIFY(!canvas->rootObject()->property("mr2_canceled").toBool()); |
|
349 |
|
350 QGraphicsScene *scene = canvas->scene(); |
|
351 QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); |
|
352 pressEvent.setScenePos(QPointF(100, 100)); |
|
353 pressEvent.setButton(Qt::LeftButton); |
|
354 pressEvent.setButtons(Qt::LeftButton); |
|
355 QApplication::sendEvent(scene, &pressEvent); |
|
356 |
|
357 QVERIFY(canvas->rootObject()->property("mr1_pressed").toBool()); |
|
358 QVERIFY(!canvas->rootObject()->property("mr1_released").toBool()); |
|
359 QVERIFY(!canvas->rootObject()->property("mr1_canceled").toBool()); |
|
360 QVERIFY(canvas->rootObject()->property("mr2_pressed").toBool()); |
|
361 QVERIFY(!canvas->rootObject()->property("mr2_released").toBool()); |
|
362 QVERIFY(canvas->rootObject()->property("mr2_canceled").toBool()); |
|
363 |
|
364 QTest::qWait(200); |
|
365 |
|
366 QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); |
|
367 releaseEvent.setScenePos(QPointF(100, 100)); |
|
368 releaseEvent.setButton(Qt::LeftButton); |
|
369 releaseEvent.setButtons(Qt::LeftButton); |
|
370 QApplication::sendEvent(scene, &releaseEvent); |
|
371 |
|
372 QVERIFY(canvas->rootObject()->property("mr1_released").toBool()); |
|
373 QVERIFY(!canvas->rootObject()->property("mr1_canceled").toBool()); |
|
374 QVERIFY(!canvas->rootObject()->property("mr2_released").toBool()); |
|
375 } |
|
376 |
|
377 QTEST_MAIN(tst_QDeclarativeMouseArea) |
|
378 |
|
379 #include "tst_qdeclarativemousearea.moc" |