/**************************************************************************** ** ** 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 #include #include #include #include #include class tst_QDeclarativeMouseArea: public QObject { Q_OBJECT private slots: void dragProperties(); void resetDrag(); void updateMouseAreaPosOnClick(); void noOnClickedWithPressAndHold(); private: QDeclarativeView *createView(); }; void tst_QDeclarativeMouseArea::dragProperties() { QDeclarativeView *canvas = createView(); canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/dragproperties.qml")); canvas->show(); canvas->setFocus(); QVERIFY(canvas->rootObject() != 0); QDeclarativeMouseArea *mouseRegion = canvas->rootObject()->findChild("mouseregion"); QDeclarativeDrag *drag = mouseRegion->drag(); QVERIFY(mouseRegion != 0); QVERIFY(drag != 0); // target QDeclarativeItem *blackRect = canvas->rootObject()->findChild("blackrect"); QVERIFY(blackRect != 0); QVERIFY(blackRect == drag->target()); QDeclarativeItem *rootItem = qobject_cast(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(), QDeclarativeDrag::XandYAxis); QSignalSpy axisSpy(drag, SIGNAL(axisChanged())); drag->setAxis(QDeclarativeDrag::XAxis); QCOMPARE(drag->axis(), QDeclarativeDrag::XAxis); QCOMPARE(axisSpy.count(),1); drag->setAxis(QDeclarativeDrag::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); delete canvas; } void tst_QDeclarativeMouseArea::resetDrag() { QDeclarativeView *canvas = createView(); canvas->rootContext()->setContextProperty("haveTarget", QVariant(true)); canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/dragreset.qml")); canvas->show(); canvas->setFocus(); QVERIFY(canvas->rootObject() != 0); QDeclarativeMouseArea *mouseRegion = canvas->rootObject()->findChild("mouseregion"); QDeclarativeDrag *drag = mouseRegion->drag(); QVERIFY(mouseRegion != 0); QVERIFY(drag != 0); // target QDeclarativeItem *blackRect = canvas->rootObject()->findChild("blackrect"); QVERIFY(blackRect != 0); QVERIFY(blackRect == drag->target()); QDeclarativeItem *rootItem = qobject_cast(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; } QDeclarativeView *tst_QDeclarativeMouseArea::createView() { QDeclarativeView *canvas = new QDeclarativeView(0); canvas->setFixedSize(240,320); return canvas; } void tst_QDeclarativeMouseArea::updateMouseAreaPosOnClick() { QDeclarativeView *canvas = createView(); canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/updateMousePosOnClick.qml")); canvas->show(); canvas->setFocus(); QVERIFY(canvas->rootObject() != 0); QDeclarativeMouseArea *mouseRegion = canvas->rootObject()->findChild("mouseregion"); QVERIFY(mouseRegion != 0); QDeclarativeRectangle *rect = canvas->rootObject()->findChild("ball"); QVERIFY(rect != 0); QCOMPARE(mouseRegion->mouseX(), rect->x()); QCOMPARE(mouseRegion->mouseY(), rect->y()); QGraphicsScene *scene = canvas->scene(); QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress); event.setScenePos(QPointF(100, 100)); event.setButton(Qt::LeftButton); event.setButtons(Qt::LeftButton); QApplication::sendEvent(scene, &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_QDeclarativeMouseArea::noOnClickedWithPressAndHold() { QDeclarativeView *canvas = createView(); canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/clickandhold.qml")); canvas->show(); canvas->setFocus(); QVERIFY(canvas->rootObject() != 0); QGraphicsScene *scene = canvas->scene(); QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); pressEvent.setScenePos(QPointF(100, 100)); pressEvent.setButton(Qt::LeftButton); pressEvent.setButtons(Qt::LeftButton); QApplication::sendEvent(scene, &pressEvent); QVERIFY(!canvas->rootObject()->property("clicked").toBool()); QVERIFY(!canvas->rootObject()->property("held").toBool()); QTest::qWait(1000); QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMousePress); releaseEvent.setScenePos(QPointF(100, 100)); releaseEvent.setButton(Qt::LeftButton); releaseEvent.setButtons(Qt::LeftButton); QApplication::sendEvent(scene, &releaseEvent); QVERIFY(!canvas->rootObject()->property("clicked").toBool()); QVERIFY(canvas->rootObject()->property("held").toBool()); } QTEST_MAIN(tst_QDeclarativeMouseArea) #include "tst_qdeclarativemousearea.moc"