diff options
Diffstat (limited to 'tests/auto/declarative/qsgmousearea')
16 files changed, 1077 insertions, 0 deletions
diff --git a/tests/auto/declarative/qsgmousearea/data/clickThrough.qml b/tests/auto/declarative/qsgmousearea/data/clickThrough.qml new file mode 100644 index 0000000000..0d954f8511 --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/data/clickThrough.qml @@ -0,0 +1,23 @@ +import QtQuick 2.0 + +Item{ + width: 200 + height: 200 + property int doubleClicks: 0 + property int clicks: 0 + property int pressAndHolds: 0 + property int presses: 0 + MouseArea{ + z: 0 + anchors.fill: parent + onPressed: presses++ + onClicked: clicks++ + onPressAndHold: pressAndHolds++ + onDoubleClicked: doubleClicks++ + } + MouseArea{ + z: 1 + enabled: true + anchors.fill: parent + } +} diff --git a/tests/auto/declarative/qsgmousearea/data/clickThrough2.qml b/tests/auto/declarative/qsgmousearea/data/clickThrough2.qml new file mode 100644 index 0000000000..bc73a1bf8a --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/data/clickThrough2.qml @@ -0,0 +1,32 @@ +import QtQuick 2.0 + +Item{ + width: 300 + height: 300 + property int doubleClicks: 0 + property int clicks: 0 + property int pressAndHolds: 0 + property int presses: 0 + property bool letThrough: false + Rectangle{ + z: 0 + color: "lightsteelblue" + width: 150 + height: 150 + MouseArea{ + anchors.fill: parent + onPressed: presses++ + onClicked: clicks++ + onPressAndHold: pressAndHolds++ + onDoubleClicked: doubleClicks++ + } + } + MouseArea{ + z: 1 + enabled: true + anchors.fill: parent + onClicked: mouse.accepted = !letThrough; + onDoubleClicked: mouse.accepted = !letThrough; + onPressAndHold: mouse.accepted = !letThrough; + } +} diff --git a/tests/auto/declarative/qsgmousearea/data/clickandhold.qml b/tests/auto/declarative/qsgmousearea/data/clickandhold.qml new file mode 100644 index 0000000000..5e4e48f6db --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/data/clickandhold.qml @@ -0,0 +1,13 @@ +import QtQuick 2.0 + +Item { + id: root + property bool clicked: false + property bool held: false + + MouseArea { + width: 200; height: 200 + onClicked: { root.clicked = true } + onPressAndHold: { root.held = true } + } +} diff --git a/tests/auto/declarative/qsgmousearea/data/clicktwice.qml b/tests/auto/declarative/qsgmousearea/data/clicktwice.qml new file mode 100644 index 0000000000..002d1b9047 --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/data/clicktwice.qml @@ -0,0 +1,16 @@ +import QtQuick 2.0 + +Item { + id: root + property int clicked: 0 + property int pressed: 0 + property int released: 0 + + MouseArea { + width: 200; height: 200 + onPressed: { root.pressed++ } + onClicked: { root.clicked++ } + onReleased: { root.released++ } + } +} + diff --git a/tests/auto/declarative/qsgmousearea/data/doubleclick.qml b/tests/auto/declarative/qsgmousearea/data/doubleclick.qml new file mode 100644 index 0000000000..1030d0c33e --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/data/doubleclick.qml @@ -0,0 +1,16 @@ +import QtQuick 2.0 + +Item { + id: root + property int clicked: 0 + property int doubleClicked: 0 + property int released: 0 + + MouseArea { + width: 200; height: 200 + onClicked: { root.clicked++ } + onDoubleClicked: { root.doubleClicked++ } + onReleased: { root.released++ } + } +} + diff --git a/tests/auto/declarative/qsgmousearea/data/dragging.qml b/tests/auto/declarative/qsgmousearea/data/dragging.qml new file mode 100644 index 0000000000..d9b6ac4083 --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/data/dragging.qml @@ -0,0 +1,28 @@ +import QtQuick 2.0 +Rectangle { + id: whiteRect + width: 200 + height: 200 + color: "white" + Rectangle { + id: blackRect + objectName: "blackrect" + color: "black" + y: 50 + x: 50 + width: 100 + height: 100 + opacity: (whiteRect.width-blackRect.x+whiteRect.height-blackRect.y-199)/200 + Text { text: blackRect.opacity} + MouseArea { + objectName: "mouseregion" + anchors.fill: parent + drag.target: blackRect + drag.axis: Drag.XandYAxis + drag.minimumX: 0 + drag.maximumX: whiteRect.width-blackRect.width + drag.minimumY: 0 + drag.maximumY: whiteRect.height-blackRect.height + } + } + } diff --git a/tests/auto/declarative/qsgmousearea/data/dragproperties.qml b/tests/auto/declarative/qsgmousearea/data/dragproperties.qml new file mode 100644 index 0000000000..421dfe26b7 --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/data/dragproperties.qml @@ -0,0 +1,28 @@ +import QtQuick 2.0 +Rectangle { + id: whiteRect + width: 200 + height: 200 + color: "white" + Rectangle { + id: blackRect + objectName: "blackrect" + color: "black" + y: 50 + x: 50 + width: 100 + height: 100 + opacity: (whiteRect.width-blackRect.x+whiteRect.height-blackRect.y-199)/200 + Text { text: blackRect.opacity} + MouseArea { + objectName: "mouseregion" + anchors.fill: parent + drag.target: blackRect + drag.axis: Drag.XandYAxis + drag.minimumX: 0 + drag.maximumX: whiteRect.width-blackRect.width + drag.minimumY: 0 + drag.maximumY: whiteRect.height-blackRect.height + } + } + } diff --git a/tests/auto/declarative/qsgmousearea/data/dragreset.qml b/tests/auto/declarative/qsgmousearea/data/dragreset.qml new file mode 100644 index 0000000000..d7949f9139 --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/data/dragreset.qml @@ -0,0 +1,28 @@ +import QtQuick 2.0 +Rectangle { + id: whiteRect + width: 200 + height: 200 + color: "white" + Rectangle { + id: blackRect + objectName: "blackrect" + color: "black" + y: 50 + x: 50 + width: 100 + height: 100 + opacity: (whiteRect.width-blackRect.x+whiteRect.height-blackRect.y-199)/200 + Text { text: blackRect.opacity} + MouseArea { + objectName: "mouseregion" + anchors.fill: parent + drag.target: haveTarget ? blackRect : undefined + drag.axis: Drag.XandYAxis + drag.minimumX: 0 + drag.maximumX: whiteRect.width-blackRect.width + drag.minimumY: 0 + drag.maximumY: whiteRect.height-blackRect.height + } + } + } diff --git a/tests/auto/declarative/qsgmousearea/data/hoverPosition.qml b/tests/auto/declarative/qsgmousearea/data/hoverPosition.qml new file mode 100644 index 0000000000..834f91ff29 --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/data/hoverPosition.qml @@ -0,0 +1,17 @@ +import QtQuick 2.0 + +Rectangle { + width: 400; height: 400; + + property real mouseX: mousetracker.mouseX + property real mouseY: mousetracker.mouseY + + Rectangle { + width: 100; height: 100; + MouseArea { + id: mousetracker; + anchors.fill: parent; + hoverEnabled: true + } + } +} diff --git a/tests/auto/declarative/qsgmousearea/data/pressedOrdering.qml b/tests/auto/declarative/qsgmousearea/data/pressedOrdering.qml new file mode 100644 index 0000000000..7aa3098100 --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/data/pressedOrdering.qml @@ -0,0 +1,28 @@ +import QtQuick 2.0 + +Item { + id: root + property string value: "base" + + MouseArea { + id: mouseArea + width: 200; height: 200 + onClicked: toggleState.state = "toggled" + } + + StateGroup { + states: State { + name: "pressed" + when: mouseArea.pressed + PropertyChanges { target: root; value: "pressed" } + } + } + + StateGroup { + id: toggleState + states: State { + name: "toggled" + PropertyChanges { target: root; value: "toggled" } + } + } +} diff --git a/tests/auto/declarative/qsgmousearea/data/preventstealing.qml b/tests/auto/declarative/qsgmousearea/data/preventstealing.qml new file mode 100644 index 0000000000..fb0d6955c1 --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/data/preventstealing.qml @@ -0,0 +1,24 @@ +import QtQuick 2.0 + +Flickable { + property bool stealing: true + width: 200 + height: 200 + contentWidth: 400 + contentHeight: 400 + Rectangle { + color: "black" + width: 400 + height: 400 + Rectangle { + x: 50; y: 50 + width: 100; height: 100 + color: "steelblue" + MouseArea { + objectName: "mousearea" + anchors.fill: parent + preventStealing: stealing + } + } + } +} diff --git a/tests/auto/declarative/qsgmousearea/data/rejectEvent.qml b/tests/auto/declarative/qsgmousearea/data/rejectEvent.qml new file mode 100644 index 0000000000..816fc76fac --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/data/rejectEvent.qml @@ -0,0 +1,28 @@ +import QtQuick 2.0 + +Rectangle { + id: root + color: "#ffffff" + width: 320; height: 240 + property bool mr1_pressed: false + property bool mr1_released: false + property bool mr1_canceled: false + property bool mr2_pressed: false + property bool mr2_released: false + property bool mr2_canceled: false + + MouseArea { + id: mouseRegion1 + anchors.fill: parent + onPressed: { root.mr1_pressed = true } + onReleased: { root.mr1_released = true } + onCanceled: { root.mr1_canceled = true } + } + MouseArea { + id: mouseRegion2 + width: 120; height: 120 + onPressed: { root.mr2_pressed = true; mouse.accepted = false } + onReleased: { root.mr2_released = true } + onCanceled: { root.mr2_canceled = true } + } +} diff --git a/tests/auto/declarative/qsgmousearea/data/updateMousePosOnClick.qml b/tests/auto/declarative/qsgmousearea/data/updateMousePosOnClick.qml new file mode 100644 index 0000000000..7377a2e86c --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/data/updateMousePosOnClick.qml @@ -0,0 +1,20 @@ +import QtQuick 2.0 + +Rectangle { + color: "#ffffff" + width: 320; height: 240 + MouseArea { + id: mouseRegion + objectName: "mouseregion" + anchors.fill: parent + Rectangle { + id: ball + objectName: "ball" + width: 20; height: 20 + radius: 10 + color: "#0000ff" + x: { mouseRegion.mouseX } + y: mouseRegion.mouseY + } + } +} diff --git a/tests/auto/declarative/qsgmousearea/data/updateMousePosOnResize.qml b/tests/auto/declarative/qsgmousearea/data/updateMousePosOnResize.qml new file mode 100644 index 0000000000..ad52ef3820 --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/data/updateMousePosOnResize.qml @@ -0,0 +1,38 @@ +import QtQuick 2.0 + +Rectangle { + color: "#ffffff" + width: 320; height: 240 + Rectangle { + id: brother + objectName: "brother" + color: "lightgreen" + x: 200; y: 100 + width: 120; height: 120 + } + MouseArea { + id: mouseRegion + objectName: "mouseregion" + + property int x1 + property int y1 + property int x2 + property int y2 + property bool emitPositionChanged: false + property bool mouseMatchesPos: true + + anchors.fill: brother + onPressed: { + if (mouse.x != mouseX || mouse.y != mouseY) + mouseMatchesPos = false + x1 = mouseX; y1 = mouseY + anchors.fill = parent + } + onPositionChanged: { emitPositionChanged = true } + onMousePositionChanged: { + if (mouse.x != mouseX || mouse.y != mouseY) + mouseMatchesPos = false + x2 = mouseX; y2 = mouseY + } + } +} diff --git a/tests/auto/declarative/qsgmousearea/qsgmousearea.pro b/tests/auto/declarative/qsgmousearea/qsgmousearea.pro new file mode 100644 index 0000000000..f0179e8eb2 --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/qsgmousearea.pro @@ -0,0 +1,18 @@ +load(qttest_p4) +contains(QT_CONFIG,declarative): QT += declarative gui network +macx:CONFIG -= app_bundle + +HEADERS += ../shared/testhttpserver.h +SOURCES += tst_qsgmousearea.cpp ../shared/testhttpserver.cpp + +symbian: { + importFiles.files = data + importFiles.path = . + DEPLOYMENT += importFiles +} else { + DEFINES += SRCDIR=\\\"$$PWD\\\" +} + +CONFIG += parallel_test + +QT += core-private gui-private declarative-private diff --git a/tests/auto/declarative/qsgmousearea/tst_qsgmousearea.cpp b/tests/auto/declarative/qsgmousearea/tst_qsgmousearea.cpp new file mode 100644 index 0000000000..4e1d253c3d --- /dev/null +++ b/tests/auto/declarative/qsgmousearea/tst_qsgmousearea.cpp @@ -0,0 +1,720 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> +#include <QtTest/QSignalSpy> +#include <private/qsgmousearea_p.h> +#include <private/qsgrectangle_p.h> +#include <private/qsgflickable_p.h> +#include <QtDeclarative/qsgview.h> +#include <QtDeclarative/qdeclarativecontext.h> +#include <QtDeclarative/qdeclarativeengine.h> +#include <QtOpenGL/QGLShaderProgram> + +#ifdef Q_OS_SYMBIAN +// In Symbian OS test data is located in applications private dir +#define SRCDIR "." +#endif + +class tst_QSGMouseArea: public QObject +{ + Q_OBJECT +private slots: + void initTestCase(); + void cleanupTestCase(); + void dragProperties(); + void resetDrag(); + void dragging(); + void updateMouseAreaPosOnClick(); + void updateMouseAreaPosOnResize(); + void noOnClickedWithPressAndHold(); + void onMousePressRejected(); + void doubleClick(); + void clickTwice(); + void pressedOrdering(); + void preventStealing(); + void clickThrough(); + void testQtQuick11Attributes(); + void testQtQuick11Attributes_data(); + void hoverPosition(); + +private: + QSGView *createView(); +}; + +void tst_QSGMouseArea::initTestCase() +{ + QSGView canvas; + if (!QGLShaderProgram::hasOpenGLShaderPrograms(canvas.context())) + QSKIP("MouseArea needs OpenGL 2.0", SkipAll); +} + +void tst_QSGMouseArea::cleanupTestCase() +{ + +} + +void tst_QSGMouseArea::dragProperties() +{ + QSGView *canvas = createView(); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/dragproperties.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QSGMouseArea *mouseRegion = canvas->rootObject()->findChild<QSGMouseArea*>("mouseregion"); + QSGDrag *drag = mouseRegion->drag(); + QVERIFY(mouseRegion != 0); + QVERIFY(drag != 0); + + // target + QSGItem *blackRect = canvas->rootObject()->findChild<QSGItem*>("blackrect"); + QVERIFY(blackRect != 0); + QVERIFY(blackRect == drag->target()); + QSGItem *rootItem = qobject_cast<QSGItem*>(canvas->rootObject()); + QVERIFY(rootItem != 0); + QSignalSpy targetSpy(drag, SIGNAL(targetChanged())); + drag->setTarget(rootItem); + QCOMPARE(targetSpy.count(),1); + drag->setTarget(rootItem); + QCOMPARE(targetSpy.count(),1); + + // axis + QCOMPARE(drag->axis(), QSGDrag::XandYAxis); + QSignalSpy axisSpy(drag, SIGNAL(axisChanged())); + drag->setAxis(QSGDrag::XAxis); + QCOMPARE(drag->axis(), QSGDrag::XAxis); + QCOMPARE(axisSpy.count(),1); + drag->setAxis(QSGDrag::XAxis); + QCOMPARE(axisSpy.count(),1); + + // minimum and maximum properties + QSignalSpy xminSpy(drag, SIGNAL(minimumXChanged())); + QSignalSpy xmaxSpy(drag, SIGNAL(maximumXChanged())); + QSignalSpy yminSpy(drag, SIGNAL(minimumYChanged())); + QSignalSpy ymaxSpy(drag, SIGNAL(maximumYChanged())); + + QCOMPARE(drag->xmin(), 0.0); + QCOMPARE(drag->xmax(), rootItem->width()-blackRect->width()); + QCOMPARE(drag->ymin(), 0.0); + QCOMPARE(drag->ymax(), rootItem->height()-blackRect->height()); + + drag->setXmin(10); + drag->setXmax(10); + drag->setYmin(10); + drag->setYmax(10); + + QCOMPARE(drag->xmin(), 10.0); + QCOMPARE(drag->xmax(), 10.0); + QCOMPARE(drag->ymin(), 10.0); + QCOMPARE(drag->ymax(), 10.0); + + QCOMPARE(xminSpy.count(),1); + QCOMPARE(xmaxSpy.count(),1); + QCOMPARE(yminSpy.count(),1); + QCOMPARE(ymaxSpy.count(),1); + + drag->setXmin(10); + drag->setXmax(10); + drag->setYmin(10); + drag->setYmax(10); + + QCOMPARE(xminSpy.count(),1); + QCOMPARE(xmaxSpy.count(),1); + QCOMPARE(yminSpy.count(),1); + QCOMPARE(ymaxSpy.count(),1); + + // filterChildren + QSignalSpy filterChildrenSpy(drag, SIGNAL(filterChildrenChanged())); + + drag->setFilterChildren(true); + + QVERIFY(drag->filterChildren()); + QCOMPARE(filterChildrenSpy.count(), 1); + + drag->setFilterChildren(true); + QCOMPARE(filterChildrenSpy.count(), 1); + + delete canvas; +} + +void tst_QSGMouseArea::resetDrag() +{ + QSGView *canvas = createView(); + + canvas->rootContext()->setContextProperty("haveTarget", QVariant(true)); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/dragreset.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QSGMouseArea *mouseRegion = canvas->rootObject()->findChild<QSGMouseArea*>("mouseregion"); + QSGDrag *drag = mouseRegion->drag(); + QVERIFY(mouseRegion != 0); + QVERIFY(drag != 0); + + // target + QSGItem *blackRect = canvas->rootObject()->findChild<QSGItem*>("blackrect"); + QVERIFY(blackRect != 0); + QVERIFY(blackRect == drag->target()); + QSGItem *rootItem = qobject_cast<QSGItem*>(canvas->rootObject()); + QVERIFY(rootItem != 0); + QSignalSpy targetSpy(drag, SIGNAL(targetChanged())); + QVERIFY(drag->target() != 0); + canvas->rootContext()->setContextProperty("haveTarget", QVariant(false)); + QCOMPARE(targetSpy.count(),1); + QVERIFY(drag->target() == 0); + + delete canvas; +} + + +void tst_QSGMouseArea::dragging() +{ + QSGView *canvas = createView(); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/dragging.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QSGMouseArea *mouseRegion = canvas->rootObject()->findChild<QSGMouseArea*>("mouseregion"); + QSGDrag *drag = mouseRegion->drag(); + QVERIFY(mouseRegion != 0); + QVERIFY(drag != 0); + + // target + QSGItem *blackRect = canvas->rootObject()->findChild<QSGItem*>("blackrect"); + QVERIFY(blackRect != 0); + QVERIFY(blackRect == drag->target()); + + QVERIFY(!drag->active()); + + QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &pressEvent); + + QVERIFY(!drag->active()); + QCOMPARE(blackRect->x(), 50.0); + QCOMPARE(blackRect->y(), 50.0); + + // First move event triggers drag, second is acted upon. + // This is due to possibility of higher stacked area taking precedence. + QMouseEvent moveEvent(QEvent::MouseMove, QPoint(106, 106), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &moveEvent); + moveEvent = QMouseEvent(QEvent::MouseMove, QPoint(110, 110), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &moveEvent); + + QVERIFY(drag->active()); + QCOMPARE(blackRect->x(), 60.0); + QCOMPARE(blackRect->y(), 60.0); + + QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(110, 110), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &releaseEvent); + + QVERIFY(!drag->active()); + QCOMPARE(blackRect->x(), 60.0); + QCOMPARE(blackRect->y(), 60.0); + + delete canvas; +} + +QSGView *tst_QSGMouseArea::createView() +{ + QSGView *canvas = new QSGView(0); + canvas->setFixedSize(240,320); + + return canvas; +} + +void tst_QSGMouseArea::updateMouseAreaPosOnClick() +{ + QSGView *canvas = createView(); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/updateMousePosOnClick.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QSGMouseArea *mouseRegion = canvas->rootObject()->findChild<QSGMouseArea*>("mouseregion"); + QVERIFY(mouseRegion != 0); + + QSGRectangle *rect = canvas->rootObject()->findChild<QSGRectangle*>("ball"); + QVERIFY(rect != 0); + + QCOMPARE(mouseRegion->mouseX(), rect->x()); + QCOMPARE(mouseRegion->mouseY(), rect->y()); + + QMouseEvent event(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &event); + + QCOMPARE(mouseRegion->mouseX(), 100.0); + QCOMPARE(mouseRegion->mouseY(), 100.0); + + QCOMPARE(mouseRegion->mouseX(), rect->x()); + QCOMPARE(mouseRegion->mouseY(), rect->y()); + + delete canvas; +} + +void tst_QSGMouseArea::updateMouseAreaPosOnResize() +{ + QSGView *canvas = createView(); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/updateMousePosOnResize.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QSGMouseArea *mouseRegion = canvas->rootObject()->findChild<QSGMouseArea*>("mouseregion"); + QVERIFY(mouseRegion != 0); + + QSGRectangle *rect = canvas->rootObject()->findChild<QSGRectangle*>("brother"); + QVERIFY(rect != 0); + + QCOMPARE(mouseRegion->mouseX(), 0.0); + QCOMPARE(mouseRegion->mouseY(), 0.0); + + QMouseEvent event(QEvent::MouseButtonPress, rect->pos().toPoint(), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &event); + + QVERIFY(!mouseRegion->property("emitPositionChanged").toBool()); + QVERIFY(mouseRegion->property("mouseMatchesPos").toBool()); + + QCOMPARE(mouseRegion->property("x1").toInt(), 0); + QCOMPARE(mouseRegion->property("y1").toInt(), 0); + + // XXX: is it on purpose that mouseX is real and mouse.x is int? + QCOMPARE(mouseRegion->property("x2").toInt(), (int) rect->x()); + QCOMPARE(mouseRegion->property("y2").toInt(), (int) rect->y()); + + QCOMPARE(mouseRegion->mouseX(), rect->x()); + QCOMPARE(mouseRegion->mouseY(), rect->y()); + + delete canvas; +} + +void tst_QSGMouseArea::noOnClickedWithPressAndHold() +{ + QSGView *canvas = createView(); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/clickandhold.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &pressEvent); + + QVERIFY(!canvas->rootObject()->property("clicked").toBool()); + QVERIFY(!canvas->rootObject()->property("held").toBool()); + + QTest::qWait(1000); + + QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &releaseEvent); + + QVERIFY(!canvas->rootObject()->property("clicked").toBool()); + QVERIFY(canvas->rootObject()->property("held").toBool()); + + delete canvas; +} + +void tst_QSGMouseArea::onMousePressRejected() +{ + QSGView *canvas = createView(); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/rejectEvent.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + QVERIFY(canvas->rootObject()->property("enabled").toBool()); + + QVERIFY(!canvas->rootObject()->property("mr1_pressed").toBool()); + QVERIFY(!canvas->rootObject()->property("mr1_released").toBool()); + QVERIFY(!canvas->rootObject()->property("mr1_canceled").toBool()); + QVERIFY(!canvas->rootObject()->property("mr2_pressed").toBool()); + QVERIFY(!canvas->rootObject()->property("mr2_released").toBool()); + QVERIFY(!canvas->rootObject()->property("mr2_canceled").toBool()); + + QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &pressEvent); + + QVERIFY(canvas->rootObject()->property("mr1_pressed").toBool()); + QVERIFY(!canvas->rootObject()->property("mr1_released").toBool()); + QVERIFY(!canvas->rootObject()->property("mr1_canceled").toBool()); + QVERIFY(canvas->rootObject()->property("mr2_pressed").toBool()); + QVERIFY(!canvas->rootObject()->property("mr2_released").toBool()); + QVERIFY(canvas->rootObject()->property("mr2_canceled").toBool()); + + QTest::qWait(200); + + QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &releaseEvent); + + QVERIFY(canvas->rootObject()->property("mr1_released").toBool()); + QVERIFY(!canvas->rootObject()->property("mr1_canceled").toBool()); + QVERIFY(!canvas->rootObject()->property("mr2_released").toBool()); + + delete canvas; +} + +void tst_QSGMouseArea::doubleClick() +{ + QSGView *canvas = createView(); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/doubleclick.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &pressEvent); + + QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &releaseEvent); + + QCOMPARE(canvas->rootObject()->property("released").toInt(), 1); + + pressEvent = QMouseEvent(QEvent::MouseButtonDblClick, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &pressEvent); + + QApplication::sendEvent(canvas, &releaseEvent); + + QCOMPARE(canvas->rootObject()->property("clicked").toInt(), 1); + QCOMPARE(canvas->rootObject()->property("doubleClicked").toInt(), 1); + QCOMPARE(canvas->rootObject()->property("released").toInt(), 2); + + delete canvas; +} + +// QTBUG-14832 +void tst_QSGMouseArea::clickTwice() +{ + QSGView *canvas = createView(); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/clicktwice.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &pressEvent); + + QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &releaseEvent); + + QCOMPARE(canvas->rootObject()->property("pressed").toInt(), 1); + QCOMPARE(canvas->rootObject()->property("released").toInt(), 1); + QCOMPARE(canvas->rootObject()->property("clicked").toInt(), 1); + + pressEvent = QMouseEvent(QEvent::MouseButtonDblClick, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &pressEvent); + + QApplication::sendEvent(canvas, &pressEvent); + QApplication::sendEvent(canvas, &releaseEvent); + + QCOMPARE(canvas->rootObject()->property("pressed").toInt(), 2); + QCOMPARE(canvas->rootObject()->property("released").toInt(), 2); + QCOMPARE(canvas->rootObject()->property("clicked").toInt(), 2); + + delete canvas; +} + +void tst_QSGMouseArea::pressedOrdering() +{ + QSGView *canvas = createView(); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pressedOrdering.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QCOMPARE(canvas->rootObject()->property("value").toString(), QLatin1String("base")); + + QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &pressEvent); + + QCOMPARE(canvas->rootObject()->property("value").toString(), QLatin1String("pressed")); + + QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &releaseEvent); + + QCOMPARE(canvas->rootObject()->property("value").toString(), QLatin1String("toggled")); + + QApplication::sendEvent(canvas, &pressEvent); + + QCOMPARE(canvas->rootObject()->property("value").toString(), QLatin1String("pressed")); + + delete canvas; +} + +void tst_QSGMouseArea::preventStealing() +{ + QSGView *canvas = createView(); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/preventstealing.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QSGFlickable *flickable = qobject_cast<QSGFlickable*>(canvas->rootObject()); + QVERIFY(flickable != 0); + + QSGMouseArea *mouseArea = canvas->rootObject()->findChild<QSGMouseArea*>("mousearea"); + QVERIFY(mouseArea != 0); + + QSignalSpy mousePositionSpy(mouseArea, SIGNAL(positionChanged(QSGMouseEvent*))); + + QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(80, 80)); + + // Without preventStealing, mouse movement over MouseArea would + // cause the Flickable to steal mouse and trigger content movement. + + QMouseEvent moveEvent(QEvent::MouseMove, QPoint(70, 70), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &moveEvent); + + moveEvent = QMouseEvent(QEvent::MouseMove, QPoint(60, 60), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &moveEvent); + + moveEvent = QMouseEvent(QEvent::MouseMove, QPoint(50, 50), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &moveEvent); + + // We should have received all three move events + QCOMPARE(mousePositionSpy.count(), 3); + QVERIFY(mouseArea->pressed()); + + // Flickable content should not have moved. + QCOMPARE(flickable->contentX(), 0.); + QCOMPARE(flickable->contentY(), 0.); + + QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(50, 50)); + + // Now allow stealing and confirm Flickable does its thing. + canvas->rootObject()->setProperty("stealing", false); + + QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(80, 80)); + + // Without preventStealing, mouse movement over MouseArea would + // cause the Flickable to steal mouse and trigger content movement. + moveEvent = QMouseEvent(QEvent::MouseMove, QPoint(70, 70), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &moveEvent); + + moveEvent = QMouseEvent(QEvent::MouseMove, QPoint(60, 60), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &moveEvent); + + moveEvent = QMouseEvent(QEvent::MouseMove, QPoint(50, 50), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &moveEvent); + + // We should only have received the first move event + QCOMPARE(mousePositionSpy.count(), 4); + // Our press should be taken away + QVERIFY(!mouseArea->pressed()); + + // Flickable content should have moved. + QCOMPARE(flickable->contentX(), 10.); + QCOMPARE(flickable->contentY(), 10.); + + QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(50, 50)); + + delete canvas; +} + +void tst_QSGMouseArea::clickThrough() +{ + //With no handlers defined click, doubleClick and PressAndHold should propagate to those with handlers + QSGView *canvas = createView(); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/clickThrough.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &pressEvent); + + QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &releaseEvent); + + QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0); + QCOMPARE(canvas->rootObject()->property("clicks").toInt(), 1); + + QApplication::sendEvent(canvas, &pressEvent); + QTest::qWait(1000); + QApplication::sendEvent(canvas, &releaseEvent); + + QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0); + QCOMPARE(canvas->rootObject()->property("clicks").toInt(), 1); + QCOMPARE(canvas->rootObject()->property("pressAndHolds").toInt(), 1); + + QApplication::sendEvent(canvas, &pressEvent); + QApplication::sendEvent(canvas, &releaseEvent); + pressEvent = QMouseEvent(QEvent::MouseButtonDblClick, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + + QApplication::sendEvent(canvas, &pressEvent); + QApplication::sendEvent(canvas, &releaseEvent); + + QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0); + QCOMPARE(canvas->rootObject()->property("clicks").toInt(), 2); + QCOMPARE(canvas->rootObject()->property("doubleClicks").toInt(), 1); + QCOMPARE(canvas->rootObject()->property("pressAndHolds").toInt(), 1); + + delete canvas; + + //With handlers defined click, doubleClick and PressAndHold should propagate only when explicitly ignored + canvas = createView(); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/clickThrough2.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + pressEvent = QMouseEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &pressEvent); + + releaseEvent = QMouseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &releaseEvent); + + QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0); + QCOMPARE(canvas->rootObject()->property("clicks").toInt(), 0); + + QApplication::sendEvent(canvas, &pressEvent); + QTest::qWait(1000); + QApplication::sendEvent(canvas, &releaseEvent); + + QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0); + QCOMPARE(canvas->rootObject()->property("clicks").toInt(), 0); + QCOMPARE(canvas->rootObject()->property("pressAndHolds").toInt(), 0); + + QApplication::sendEvent(canvas, &pressEvent); + QApplication::sendEvent(canvas, &releaseEvent); + + pressEvent = QMouseEvent(QEvent::MouseButtonDblClick, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &pressEvent); + QApplication::sendEvent(canvas, &releaseEvent); + + QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0); + QCOMPARE(canvas->rootObject()->property("clicks").toInt(), 0); + QCOMPARE(canvas->rootObject()->property("doubleClicks").toInt(), 0); + QCOMPARE(canvas->rootObject()->property("pressAndHolds").toInt(), 0); + + canvas->rootObject()->setProperty("letThrough", QVariant(true)); + + pressEvent = QMouseEvent(QEvent::MouseButtonPress, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &pressEvent); + + releaseEvent = QMouseEvent(QEvent::MouseButtonRelease, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + QApplication::sendEvent(canvas, &releaseEvent); + + QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0); + QCOMPARE(canvas->rootObject()->property("clicks").toInt(), 1); + + QApplication::sendEvent(canvas, &pressEvent); + QTest::qWait(1000); + QApplication::sendEvent(canvas, &releaseEvent); + + QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0); + QCOMPARE(canvas->rootObject()->property("clicks").toInt(), 1); + QCOMPARE(canvas->rootObject()->property("pressAndHolds").toInt(), 1); + + QApplication::sendEvent(canvas, &pressEvent); + QApplication::sendEvent(canvas, &releaseEvent); + pressEvent = QMouseEvent(QEvent::MouseButtonDblClick, QPoint(100, 100), Qt::LeftButton, Qt::LeftButton, 0); + + QApplication::sendEvent(canvas, &pressEvent); + QApplication::sendEvent(canvas, &releaseEvent); + + QCOMPARE(canvas->rootObject()->property("presses").toInt(), 0); + QCOMPARE(canvas->rootObject()->property("clicks").toInt(), 2); + QCOMPARE(canvas->rootObject()->property("doubleClicks").toInt(), 1); + QCOMPARE(canvas->rootObject()->property("pressAndHolds").toInt(), 1); + + delete canvas; +} + +void tst_QSGMouseArea::testQtQuick11Attributes() +{ + QFETCH(QString, code); + QFETCH(QString, warning); + QFETCH(QString, error); + + QDeclarativeEngine engine; + QObject *obj; + + QDeclarativeComponent valid(&engine); + valid.setData("import QtQuick 1.1; MouseArea { " + code.toUtf8() + " }", QUrl("")); + obj = valid.create(); + QVERIFY(obj); + QVERIFY(valid.errorString().isEmpty()); + delete obj; + + QDeclarativeComponent invalid(&engine); + invalid.setData("import QtQuick 1.0; MouseArea { " + code.toUtf8() + " }", QUrl("")); + QTest::ignoreMessage(QtWarningMsg, warning.toUtf8()); + obj = invalid.create(); + QCOMPARE(invalid.errorString(), error); + delete obj; +} + +void tst_QSGMouseArea::testQtQuick11Attributes_data() +{ + QTest::addColumn<QString>("code"); + QTest::addColumn<QString>("warning"); + QTest::addColumn<QString>("error"); + + QTest::newRow("preventStealing") << "preventStealing: true" + << "QDeclarativeComponent: Component is not ready" + << ":1 \"MouseArea.preventStealing\" is not available in QtQuick 1.0.\n"; +} + +void tst_QSGMouseArea::hoverPosition() +{ + QSGView *canvas = createView(); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/hoverPosition.qml")); + + QSGItem *root = canvas->rootObject(); + QVERIFY(root != 0); + + QCOMPARE(root->property("mouseX").toReal(), qreal(0)); + QCOMPARE(root->property("mouseY").toReal(), qreal(0)); + + QMouseEvent moveEvent(QEvent::MouseMove, QPoint(10, 32), Qt::NoButton, Qt::NoButton, 0); + QApplication::sendEvent(canvas, &moveEvent); + + QCOMPARE(root->property("mouseX").toReal(), qreal(10)); + QCOMPARE(root->property("mouseY").toReal(), qreal(32)); + + delete canvas; +} + +QTEST_MAIN(tst_QSGMouseArea) + +#include "tst_qsgmousearea.moc" |