1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
43 #include <QtTest/QSignalSpy>
44 #include <QtQuick/private/qquickmousearea_p.h>
45 #include <QtQuick/private/qquickrectangle_p.h>
46 #include <private/qquickflickable_p.h>
47 #include <QtQuick/qquickview.h>
48 #include <QtDeclarative/qdeclarativecontext.h>
49 #include <QtDeclarative/qdeclarativeengine.h>
50 #include <QtOpenGL/QGLShaderProgram>
51 #include "../../shared/util.h"
55 class tst_QQuickMouseArea: public QDeclarativeDataTest
59 void dragProperties();
62 void updateMouseAreaPosOnClick();
63 void updateMouseAreaPosOnResize();
64 void noOnClickedWithPressAndHold();
65 void onMousePressRejected();
66 void pressedCanceledOnWindowDeactivate();
69 void pressedOrdering();
70 void preventStealing();
72 void testQtQuick11Attributes();
73 void testQtQuick11Attributes_data();
75 void hoverPropagation();
78 QQuickView *createView();
81 void tst_QQuickMouseArea::dragProperties()
83 QQuickView *canvas = createView();
85 canvas->setSource(testFileUrl("dragproperties.qml"));
87 canvas->requestActivateWindow();
88 QVERIFY(canvas->rootObject() != 0);
90 QQuickMouseArea *mouseRegion = canvas->rootObject()->findChild<QQuickMouseArea*>("mouseregion");
91 QQuickDrag *drag = mouseRegion->drag();
92 QVERIFY(mouseRegion != 0);
96 QQuickItem *blackRect = canvas->rootObject()->findChild<QQuickItem*>("blackrect");
97 QVERIFY(blackRect != 0);
98 QVERIFY(blackRect == drag->target());
99 QQuickItem *rootItem = qobject_cast<QQuickItem*>(canvas->rootObject());
100 QVERIFY(rootItem != 0);
101 QSignalSpy targetSpy(drag, SIGNAL(targetChanged()));
102 drag->setTarget(rootItem);
103 QCOMPARE(targetSpy.count(),1);
104 drag->setTarget(rootItem);
105 QCOMPARE(targetSpy.count(),1);
108 QCOMPARE(drag->axis(), QQuickDrag::XandYAxis);
109 QSignalSpy axisSpy(drag, SIGNAL(axisChanged()));
110 drag->setAxis(QQuickDrag::XAxis);
111 QCOMPARE(drag->axis(), QQuickDrag::XAxis);
112 QCOMPARE(axisSpy.count(),1);
113 drag->setAxis(QQuickDrag::XAxis);
114 QCOMPARE(axisSpy.count(),1);
116 // minimum and maximum properties
117 QSignalSpy xminSpy(drag, SIGNAL(minimumXChanged()));
118 QSignalSpy xmaxSpy(drag, SIGNAL(maximumXChanged()));
119 QSignalSpy yminSpy(drag, SIGNAL(minimumYChanged()));
120 QSignalSpy ymaxSpy(drag, SIGNAL(maximumYChanged()));
122 QCOMPARE(drag->xmin(), 0.0);
123 QCOMPARE(drag->xmax(), rootItem->width()-blackRect->width());
124 QCOMPARE(drag->ymin(), 0.0);
125 QCOMPARE(drag->ymax(), rootItem->height()-blackRect->height());
132 QCOMPARE(drag->xmin(), 10.0);
133 QCOMPARE(drag->xmax(), 10.0);
134 QCOMPARE(drag->ymin(), 10.0);
135 QCOMPARE(drag->ymax(), 10.0);
137 QCOMPARE(xminSpy.count(),1);
138 QCOMPARE(xmaxSpy.count(),1);
139 QCOMPARE(yminSpy.count(),1);
140 QCOMPARE(ymaxSpy.count(),1);
147 QCOMPARE(xminSpy.count(),1);
148 QCOMPARE(xmaxSpy.count(),1);
149 QCOMPARE(yminSpy.count(),1);
150 QCOMPARE(ymaxSpy.count(),1);
153 QSignalSpy filterChildrenSpy(drag, SIGNAL(filterChildrenChanged()));
155 drag->setFilterChildren(true);
157 QVERIFY(drag->filterChildren());
158 QCOMPARE(filterChildrenSpy.count(), 1);
160 drag->setFilterChildren(true);
161 QCOMPARE(filterChildrenSpy.count(), 1);
166 void tst_QQuickMouseArea::resetDrag()
168 QQuickView *canvas = createView();
170 canvas->rootContext()->setContextProperty("haveTarget", QVariant(true));
171 canvas->setSource(testFileUrl("dragreset.qml"));
173 canvas->requestActivateWindow();
174 QVERIFY(canvas->rootObject() != 0);
176 QQuickMouseArea *mouseRegion = canvas->rootObject()->findChild<QQuickMouseArea*>("mouseregion");
177 QQuickDrag *drag = mouseRegion->drag();
178 QVERIFY(mouseRegion != 0);
182 QQuickItem *blackRect = canvas->rootObject()->findChild<QQuickItem*>("blackrect");
183 QVERIFY(blackRect != 0);
184 QVERIFY(blackRect == drag->target());
185 QQuickItem *rootItem = qobject_cast<QQuickItem*>(canvas->rootObject());
186 QVERIFY(rootItem != 0);
187 QSignalSpy targetSpy(drag, SIGNAL(targetChanged()));
188 QVERIFY(drag->target() != 0);
189 canvas->rootContext()->setContextProperty("haveTarget", QVariant(false));
190 QCOMPARE(targetSpy.count(),1);
191 QVERIFY(drag->target() == 0);
197 void tst_QQuickMouseArea::dragging()
199 QQuickView *canvas = createView();
201 canvas->setSource(testFileUrl("dragging.qml"));
203 canvas->requestActivateWindow();
205 QVERIFY(canvas->rootObject() != 0);
207 QQuickMouseArea *mouseRegion = canvas->rootObject()->findChild<QQuickMouseArea*>("mouseregion");
208 QQuickDrag *drag = mouseRegion->drag();
209 QVERIFY(mouseRegion != 0);
213 QQuickItem *blackRect = canvas->rootObject()->findChild<QQuickItem*>("blackrect");
214 QVERIFY(blackRect != 0);
215 QVERIFY(blackRect == drag->target());
217 QVERIFY(!drag->active());
220 QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
221 QGuiApplication::sendEvent(canvas, &pressEvent);
223 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(100,100));
226 QVERIFY(!drag->active());
227 QCOMPARE(blackRect->x(), 50.0);
228 QCOMPARE(blackRect->y(), 50.0);
230 // First move event triggers drag, second is acted upon.
231 // This is due to possibility of higher stacked area taking precedence.
233 QTest::mouseMove(canvas, QPoint(111,111));
235 QTest::mouseMove(canvas, QPoint(122,122));
238 QVERIFY(drag->active());
239 QCOMPARE(blackRect->x(), 72.0);
240 QCOMPARE(blackRect->y(), 72.0);
243 QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
244 QGuiApplication::sendEvent(canvas, &releaseEvent);
246 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(122,122));
250 QVERIFY(!drag->active());
251 QCOMPARE(blackRect->x(), 72.0);
252 QCOMPARE(blackRect->y(), 72.0);
257 QQuickView *tst_QQuickMouseArea::createView()
259 QQuickView *canvas = new QQuickView(0);
260 canvas->setBaseSize(QSize(240,320));
265 void tst_QQuickMouseArea::updateMouseAreaPosOnClick()
267 QQuickView *canvas = createView();
268 canvas->setSource(testFileUrl("updateMousePosOnClick.qml"));
270 canvas->requestActivateWindow();
271 QVERIFY(canvas->rootObject() != 0);
273 QQuickMouseArea *mouseRegion = canvas->rootObject()->findChild<QQuickMouseArea*>("mouseregion");
274 QVERIFY(mouseRegion != 0);
276 QQuickRectangle *rect = canvas->rootObject()->findChild<QQuickRectangle*>("ball");
279 QCOMPARE(mouseRegion->mouseX(), rect->x());
280 QCOMPARE(mouseRegion->mouseY(), rect->y());
282 QMouseEvent event(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
283 QGuiApplication::sendEvent(canvas, &event);
285 QCOMPARE(mouseRegion->mouseX(), 100.0);
286 QCOMPARE(mouseRegion->mouseY(), 100.0);
288 QCOMPARE(mouseRegion->mouseX(), rect->x());
289 QCOMPARE(mouseRegion->mouseY(), rect->y());
294 void tst_QQuickMouseArea::updateMouseAreaPosOnResize()
296 QQuickView *canvas = createView();
297 canvas->setSource(testFileUrl("updateMousePosOnResize.qml"));
299 canvas->requestActivateWindow();
300 QVERIFY(canvas->rootObject() != 0);
302 QQuickMouseArea *mouseRegion = canvas->rootObject()->findChild<QQuickMouseArea*>("mouseregion");
303 QVERIFY(mouseRegion != 0);
305 QQuickRectangle *rect = canvas->rootObject()->findChild<QQuickRectangle*>("brother");
308 QCOMPARE(mouseRegion->mouseX(), 0.0);
309 QCOMPARE(mouseRegion->mouseY(), 0.0);
311 QMouseEvent event(QEvent::MouseButtonPress, rect->pos().toPoint(), Qt::LeftButton, Qt::LeftButton, 0);
312 QGuiApplication::sendEvent(canvas, &event);
314 QVERIFY(!mouseRegion->property("emitPositionChanged").toBool());
315 QVERIFY(mouseRegion->property("mouseMatchesPos").toBool());
317 QCOMPARE(mouseRegion->property("x1").toReal(), 0.0);
318 QCOMPARE(mouseRegion->property("y1").toReal(), 0.0);
320 QCOMPARE(mouseRegion->property("x2").toReal(), rect->x());
321 QCOMPARE(mouseRegion->property("y2").toReal(), rect->y());
323 QCOMPARE(mouseRegion->mouseX(), rect->x());
324 QCOMPARE(mouseRegion->mouseY(), rect->y());
329 void tst_QQuickMouseArea::noOnClickedWithPressAndHold()
332 // We handle onPressAndHold, therefore no onClicked
333 QQuickView *canvas = createView();
334 canvas->setSource(testFileUrl("clickandhold.qml"));
336 canvas->requestActivateWindow();
337 QVERIFY(canvas->rootObject() != 0);
339 QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
340 QGuiApplication::sendEvent(canvas, &pressEvent);
342 QVERIFY(!canvas->rootObject()->property("clicked").toBool());
343 QVERIFY(!canvas->rootObject()->property("held").toBool());
347 QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
348 QGuiApplication::sendEvent(canvas, &releaseEvent);
350 QVERIFY(!canvas->rootObject()->property("clicked").toBool());
351 QVERIFY(canvas->rootObject()->property("held").toBool());
357 // We do not handle onPressAndHold, therefore we get onClicked
358 QQuickView *canvas = createView();
359 canvas->setSource(testFileUrl("noclickandhold.qml"));
361 canvas->requestActivateWindow();
362 QVERIFY(canvas->rootObject() != 0);
364 QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
365 QGuiApplication::sendEvent(canvas, &pressEvent);
367 QVERIFY(!canvas->rootObject()->property("clicked").toBool());
371 QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
372 QGuiApplication::sendEvent(canvas, &releaseEvent);
374 QVERIFY(canvas->rootObject()->property("clicked").toBool());
380 void tst_QQuickMouseArea::onMousePressRejected()
382 QQuickView *canvas = createView();
383 canvas->setSource(testFileUrl("rejectEvent.qml"));
385 canvas->requestActivateWindow();
386 QVERIFY(canvas->rootObject() != 0);
387 QVERIFY(canvas->rootObject()->property("enabled").toBool());
389 QVERIFY(!canvas->rootObject()->property("mr1_pressed").toBool());
390 QVERIFY(!canvas->rootObject()->property("mr1_released").toBool());
391 QVERIFY(!canvas->rootObject()->property("mr1_canceled").toBool());
392 QVERIFY(!canvas->rootObject()->property("mr2_pressed").toBool());
393 QVERIFY(!canvas->rootObject()->property("mr2_released").toBool());
394 QVERIFY(!canvas->rootObject()->property("mr2_canceled").toBool());
396 QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
397 QGuiApplication::sendEvent(canvas, &pressEvent);
399 QVERIFY(canvas->rootObject()->property("mr1_pressed").toBool());
400 QVERIFY(!canvas->rootObject()->property("mr1_released").toBool());
401 QVERIFY(!canvas->rootObject()->property("mr1_canceled").toBool());
402 QVERIFY(canvas->rootObject()->property("mr2_pressed").toBool());
403 QVERIFY(!canvas->rootObject()->property("mr2_released").toBool());
404 QVERIFY(canvas->rootObject()->property("mr2_canceled").toBool());
408 QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
409 QGuiApplication::sendEvent(canvas, &releaseEvent);
411 QVERIFY(canvas->rootObject()->property("mr1_released").toBool());
412 QVERIFY(!canvas->rootObject()->property("mr1_canceled").toBool());
413 QVERIFY(!canvas->rootObject()->property("mr2_released").toBool());
417 void tst_QQuickMouseArea::pressedCanceledOnWindowDeactivate()
419 QQuickView *canvas = createView();
420 canvas->setSource(testFileUrl("pressedCanceled.qml"));
422 canvas->requestActivateWindow();
423 QVERIFY(canvas->rootObject() != 0);
424 QVERIFY(!canvas->rootObject()->property("pressed").toBool());
425 QVERIFY(!canvas->rootObject()->property("canceled").toBool());
426 QVERIFY(!canvas->rootObject()->property("released").toBool());
428 QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
429 QGuiApplication::sendEvent(canvas, &pressEvent);
431 QVERIFY(canvas->rootObject()->property("pressed").toBool());
432 QVERIFY(!canvas->rootObject()->property("canceled").toBool());
433 QVERIFY(!canvas->rootObject()->property("released").toBool());
437 QEvent windowDeactivateEvent(QEvent::WindowDeactivate);
438 QGuiApplication::sendEvent(canvas, &windowDeactivateEvent);
439 QVERIFY(!canvas->rootObject()->property("pressed").toBool());
440 QVERIFY(canvas->rootObject()->property("canceled").toBool());
441 QVERIFY(!canvas->rootObject()->property("released").toBool());
446 QGuiApplication::sendEvent(canvas, &pressEvent);
447 QVERIFY(canvas->rootObject()->property("pressed").toBool());
448 QVERIFY(!canvas->rootObject()->property("canceled").toBool());
449 QVERIFY(!canvas->rootObject()->property("released").toBool());
454 QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
455 QGuiApplication::sendEvent(canvas, &releaseEvent);
456 QVERIFY(!canvas->rootObject()->property("pressed").toBool());
457 QVERIFY(!canvas->rootObject()->property("canceled").toBool());
458 QVERIFY(canvas->rootObject()->property("released").toBool());
462 void tst_QQuickMouseArea::doubleClick()
464 QQuickView *canvas = createView();
465 canvas->setSource(testFileUrl("doubleclick.qml"));
467 canvas->requestActivateWindow();
468 QVERIFY(canvas->rootObject() != 0);
470 QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
471 QGuiApplication::sendEvent(canvas, &pressEvent);
473 QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
474 QGuiApplication::sendEvent(canvas, &releaseEvent);
476 QCOMPARE(canvas->rootObject()->property("released").toInt(), 1);
478 pressEvent = QMouseEvent(QEvent::MouseButtonDblClick, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
479 QGuiApplication::sendEvent(canvas, &pressEvent);
481 QGuiApplication::sendEvent(canvas, &releaseEvent);
483 QCOMPARE(canvas->rootObject()->property("clicked").toInt(), 1);
484 QCOMPARE(canvas->rootObject()->property("doubleClicked").toInt(), 1);
485 QCOMPARE(canvas->rootObject()->property("released").toInt(), 2);
491 void tst_QQuickMouseArea::clickTwice()
493 QQuickView *canvas = createView();
494 canvas->setSource(testFileUrl("clicktwice.qml"));
496 canvas->requestActivateWindow();
497 QVERIFY(canvas->rootObject() != 0);
499 QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
500 QGuiApplication::sendEvent(canvas, &pressEvent);
502 QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
503 QGuiApplication::sendEvent(canvas, &releaseEvent);
505 QCOMPARE(canvas->rootObject()->property("pressed").toInt(), 1);
506 QCOMPARE(canvas->rootObject()->property("released").toInt(), 1);
507 QCOMPARE(canvas->rootObject()->property("clicked").toInt(), 1);
509 pressEvent = QMouseEvent(QEvent::MouseButtonDblClick, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
510 QGuiApplication::sendEvent(canvas, &pressEvent);
512 QGuiApplication::sendEvent(canvas, &pressEvent);
513 QGuiApplication::sendEvent(canvas, &releaseEvent);
515 QCOMPARE(canvas->rootObject()->property("pressed").toInt(), 2);
516 QCOMPARE(canvas->rootObject()->property("released").toInt(), 2);
517 QCOMPARE(canvas->rootObject()->property("clicked").toInt(), 2);
522 void tst_QQuickMouseArea::pressedOrdering()
524 QQuickView *canvas = createView();
525 canvas->setSource(testFileUrl("pressedOrdering.qml"));
527 canvas->requestActivateWindow();
528 QVERIFY(canvas->rootObject() != 0);
530 QCOMPARE(canvas->rootObject()->property("value").toString(), QLatin1String("base"));
532 QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
533 QGuiApplication::sendEvent(canvas, &pressEvent);
535 QCOMPARE(canvas->rootObject()->property("value").toString(), QLatin1String("pressed"));
537 QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0);
538 QGuiApplication::sendEvent(canvas, &releaseEvent);
540 QCOMPARE(canvas->rootObject()->property("value").toString(), QLatin1String("toggled"));
542 QGuiApplication::sendEvent(canvas, &pressEvent);
544 QCOMPARE(canvas->rootObject()->property("value").toString(), QLatin1String("pressed"));
549 void tst_QQuickMouseArea::preventStealing()
551 QQuickView *canvas = createView();
553 canvas->setSource(testFileUrl("preventstealing.qml"));
555 canvas->requestActivateWindow();
556 QVERIFY(canvas->rootObject() != 0);
558 QQuickFlickable *flickable = qobject_cast<QQuickFlickable*>(canvas->rootObject());
559 QVERIFY(flickable != 0);
561 QQuickMouseArea *mouseArea = canvas->rootObject()->findChild<QQuickMouseArea*>("mousearea");
562 QVERIFY(mouseArea != 0);
564 QSignalSpy mousePositionSpy(mouseArea, SIGNAL(positionChanged(QQuickMouseEvent*)));
566 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(80, 80));
568 // Without preventStealing, mouse movement over MouseArea would
569 // cause the Flickable to steal mouse and trigger content movement.
571 QTest::mouseMove(canvas,QPoint(69,69));
572 QTest::mouseMove(canvas,QPoint(58,58));
573 QTest::mouseMove(canvas,QPoint(47,47));
575 // We should have received all three move events
576 QCOMPARE(mousePositionSpy.count(), 3);
577 QVERIFY(mouseArea->pressed());
579 // Flickable content should not have moved.
580 QCOMPARE(flickable->contentX(), 0.);
581 QCOMPARE(flickable->contentY(), 0.);
583 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(47, 47));
585 // Now allow stealing and confirm Flickable does its thing.
586 canvas->rootObject()->setProperty("stealing", false);
588 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(80, 80));
590 // Without preventStealing, mouse movement over MouseArea would
591 // cause the Flickable to steal mouse and trigger content movement.
593 QTest::mouseMove(canvas,QPoint(69,69));
594 QTest::mouseMove(canvas,QPoint(58,58));
595 QTest::mouseMove(canvas,QPoint(47,47));
597 // We should only have received the first move event
598 QCOMPARE(mousePositionSpy.count(), 4);
599 // Our press should be taken away
600 QVERIFY(!mouseArea->pressed());
602 // Flickable content should have moved.
604 QCOMPARE(flickable->contentX(), 11.);
605 QCOMPARE(flickable->contentY(), 11.);
607 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(50, 50));
612 void tst_QQuickMouseArea::clickThrough()
614 //With no handlers defined click, doubleClick and PressAndHold should propagate to those with handlers
615 QQuickView *canvas = createView();
616 canvas->setSource(testFileUrl("clickThrough.qml"));
618 canvas->requestActivateWindow();
619 QVERIFY(canvas->rootObject() != 0);
621 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(100,100));
622 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(100,100));
624 QTRY_COMPARE(canvas->rootObject()->property("presses").toInt(), 0);
625 QTRY_COMPARE(canvas->rootObject()->property("clicks").toInt(), 1);
627 QTest::qWait(800); // to avoid generating a double click.
629 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(100,100));
631 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(100,100));
633 QTRY_COMPARE(canvas->rootObject()->property("presses").toInt(), 0);
634 QTRY_COMPARE(canvas->rootObject()->property("clicks").toInt(), 1);
635 QTRY_COMPARE(canvas->rootObject()->property("pressAndHolds").toInt(), 1);
637 QTest::mouseDClick(canvas, Qt::LeftButton, 0, QPoint(100,100));
640 QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0);
641 QTRY_COMPARE(canvas->rootObject()->property("clicks").toInt(), 2);
642 QTRY_COMPARE(canvas->rootObject()->property("doubleClicks").toInt(), 1);
643 QCOMPARE(canvas->rootObject()->property("pressAndHolds").toInt(), 1);
647 //With handlers defined click, doubleClick and PressAndHold should propagate only when explicitly ignored
648 canvas = createView();
649 canvas->setSource(testFileUrl("clickThrough2.qml"));
651 canvas->requestActivateWindow();
652 QVERIFY(canvas->rootObject() != 0);
654 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(100,100));
655 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(100,100));
657 QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0);
658 QCOMPARE(canvas->rootObject()->property("clicks").toInt(), 0);
660 QTest::qWait(800); // to avoid generating a double click.
662 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(100,100));
664 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(100,100));
667 QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0);
668 QCOMPARE(canvas->rootObject()->property("clicks").toInt(), 0);
669 QCOMPARE(canvas->rootObject()->property("pressAndHolds").toInt(), 0);
671 QTest::mouseDClick(canvas, Qt::LeftButton, 0, QPoint(100,100));
674 QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0);
675 QCOMPARE(canvas->rootObject()->property("clicks").toInt(), 0);
676 QCOMPARE(canvas->rootObject()->property("doubleClicks").toInt(), 0);
677 QCOMPARE(canvas->rootObject()->property("pressAndHolds").toInt(), 0);
679 canvas->rootObject()->setProperty("letThrough", QVariant(true));
681 QTest::qWait(800); // to avoid generating a double click.
682 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(100,100));
683 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(100,100));
685 QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0);
686 QTRY_COMPARE(canvas->rootObject()->property("clicks").toInt(), 1);
688 QTest::qWait(800); // to avoid generating a double click.
689 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(100,100));
691 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(100,100));
694 QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0);
695 QCOMPARE(canvas->rootObject()->property("clicks").toInt(), 1);
696 QCOMPARE(canvas->rootObject()->property("pressAndHolds").toInt(), 1);
698 QTest::mouseDClick(canvas, Qt::LeftButton, 0, QPoint(100,100));
701 QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0);
702 QTRY_COMPARE(canvas->rootObject()->property("clicks").toInt(), 2);
703 QCOMPARE(canvas->rootObject()->property("doubleClicks").toInt(), 1);
704 QCOMPARE(canvas->rootObject()->property("pressAndHolds").toInt(), 1);
706 canvas->rootObject()->setProperty("noPropagation", QVariant(true));
708 QTest::qWait(800); // to avoid generating a double click.
709 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(100,100));
710 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(100,100));
712 QTest::qWait(800); // to avoid generating a double click.
713 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(100,100));
715 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(100,100));
718 QTest::mouseDClick(canvas, Qt::LeftButton, 0, QPoint(100,100));
721 QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0);
722 QTRY_COMPARE(canvas->rootObject()->property("clicks").toInt(), 2);
723 QCOMPARE(canvas->rootObject()->property("doubleClicks").toInt(), 1);
724 QCOMPARE(canvas->rootObject()->property("pressAndHolds").toInt(), 1);
729 void tst_QQuickMouseArea::testQtQuick11Attributes()
731 QFETCH(QString, code);
732 QFETCH(QString, warning);
733 QFETCH(QString, error);
735 QDeclarativeEngine engine;
738 QDeclarativeComponent valid(&engine);
739 valid.setData("import QtQuick 1.1; MouseArea { " + code.toUtf8() + " }", QUrl(""));
740 obj = valid.create();
742 QVERIFY(valid.errorString().isEmpty());
745 QDeclarativeComponent invalid(&engine);
746 invalid.setData("import QtQuick 1.0; MouseArea { " + code.toUtf8() + " }", QUrl(""));
747 QTest::ignoreMessage(QtWarningMsg, warning.toUtf8());
748 obj = invalid.create();
749 QCOMPARE(invalid.errorString(), error);
753 void tst_QQuickMouseArea::testQtQuick11Attributes_data()
755 QTest::addColumn<QString>("code");
756 QTest::addColumn<QString>("warning");
757 QTest::addColumn<QString>("error");
759 QTest::newRow("preventStealing") << "preventStealing: true"
760 << "QDeclarativeComponent: Component is not ready"
761 << ":1 \"MouseArea.preventStealing\" is not available in QtQuick 1.0.\n";
764 void tst_QQuickMouseArea::hoverPosition()
766 QQuickView *canvas = createView();
767 canvas->setSource(testFileUrl("hoverPosition.qml"));
769 QQuickItem *root = canvas->rootObject();
772 QCOMPARE(root->property("mouseX").toReal(), qreal(0));
773 QCOMPARE(root->property("mouseY").toReal(), qreal(0));
775 QTest::mouseMove(canvas,QPoint(10,32));
778 QCOMPARE(root->property("mouseX").toReal(), qreal(10));
779 QCOMPARE(root->property("mouseY").toReal(), qreal(32));
784 void tst_QQuickMouseArea::hoverPropagation()
786 //QTBUG-18175, to behave like GV did.
787 QQuickView *canvas = createView();
788 canvas->setSource(testFileUrl("hoverPropagation.qml"));
790 QQuickItem *root = canvas->rootObject();
793 QCOMPARE(root->property("point1").toBool(), false);
794 QCOMPARE(root->property("point2").toBool(), false);
796 QMouseEvent moveEvent(QEvent::MouseMove, QPoint(32, 32), Qt::NoButton, Qt::NoButton, 0);
797 QGuiApplication::sendEvent(canvas, &moveEvent);
799 QCOMPARE(root->property("point1").toBool(), true);
800 QCOMPARE(root->property("point2").toBool(), false);
802 QMouseEvent moveEvent2(QEvent::MouseMove, QPoint(232, 32), Qt::NoButton, Qt::NoButton, 0);
803 QGuiApplication::sendEvent(canvas, &moveEvent2);
804 QCOMPARE(root->property("point1").toBool(), false);
805 QCOMPARE(root->property("point2").toBool(), true);
810 QTEST_MAIN(tst_QQuickMouseArea)
812 #include "tst_qquickmousearea.moc"