1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
43 #include <QtTest/QSignalSpy>
44 #include <QtQuick1/private/qdeclarativepincharea_p.h>
45 #include <QtQuick1/private/qdeclarativerectangle_p.h>
46 #include <QtQuick1/private/qdeclarativeflickable_p.h>
47 #include <QtQuick1/qdeclarativeview.h>
48 #include <QtDeclarative/qdeclarativecontext.h>
50 class tst_QDeclarative1PinchArea: public QObject
54 tst_QDeclarative1PinchArea() : device(0) { }
58 device = new QTouchDevice;
59 device->setType(QTouchDevice::TouchScreen);
60 QWindowSystemInterface::registerTouchDevice(device);
63 void pinchProperties();
69 QDeclarativeView *createView();
73 void tst_QDeclarative1PinchArea::pinchProperties()
75 QDeclarativeView *canvas = createView();
76 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
79 QVERIFY(canvas->rootObject() != 0);
81 QDeclarative1PinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarative1PinchArea*>("pincharea");
82 QDeclarative1Pinch *pinch = pinchArea->pinch();
83 QVERIFY(pinchArea != 0);
87 QDeclarativeItem *blackRect = canvas->rootObject()->findChild<QDeclarativeItem*>("blackrect");
88 QVERIFY(blackRect != 0);
89 QVERIFY(blackRect == pinch->target());
90 QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
91 QVERIFY(rootItem != 0);
92 QSignalSpy targetSpy(pinch, SIGNAL(targetChanged()));
93 pinch->setTarget(rootItem);
94 QCOMPARE(targetSpy.count(),1);
95 pinch->setTarget(rootItem);
96 QCOMPARE(targetSpy.count(),1);
99 QCOMPARE(pinch->axis(), QDeclarative1Pinch::XandYAxis);
100 QSignalSpy axisSpy(pinch, SIGNAL(dragAxisChanged()));
101 pinch->setAxis(QDeclarative1Pinch::XAxis);
102 QCOMPARE(pinch->axis(), QDeclarative1Pinch::XAxis);
103 QCOMPARE(axisSpy.count(),1);
104 pinch->setAxis(QDeclarative1Pinch::XAxis);
105 QCOMPARE(axisSpy.count(),1);
107 // minimum and maximum drag properties
108 QSignalSpy xminSpy(pinch, SIGNAL(minimumXChanged()));
109 QSignalSpy xmaxSpy(pinch, SIGNAL(maximumXChanged()));
110 QSignalSpy yminSpy(pinch, SIGNAL(minimumYChanged()));
111 QSignalSpy ymaxSpy(pinch, SIGNAL(maximumYChanged()));
113 QCOMPARE(pinch->xmin(), 0.0);
114 QCOMPARE(pinch->xmax(), rootItem->width()-blackRect->width());
115 QCOMPARE(pinch->ymin(), 0.0);
116 QCOMPARE(pinch->ymax(), rootItem->height()-blackRect->height());
123 QCOMPARE(pinch->xmin(), 10.0);
124 QCOMPARE(pinch->xmax(), 10.0);
125 QCOMPARE(pinch->ymin(), 10.0);
126 QCOMPARE(pinch->ymax(), 10.0);
128 QCOMPARE(xminSpy.count(),1);
129 QCOMPARE(xmaxSpy.count(),1);
130 QCOMPARE(yminSpy.count(),1);
131 QCOMPARE(ymaxSpy.count(),1);
138 QCOMPARE(xminSpy.count(),1);
139 QCOMPARE(xmaxSpy.count(),1);
140 QCOMPARE(yminSpy.count(),1);
141 QCOMPARE(ymaxSpy.count(),1);
143 // minimum and maximum scale properties
144 QSignalSpy scaleMinSpy(pinch, SIGNAL(minimumScaleChanged()));
145 QSignalSpy scaleMaxSpy(pinch, SIGNAL(maximumScaleChanged()));
147 QCOMPARE(pinch->minimumScale(), 1.0);
148 QCOMPARE(pinch->maximumScale(), 2.0);
150 pinch->setMinimumScale(0.5);
151 pinch->setMaximumScale(1.5);
153 QCOMPARE(pinch->minimumScale(), 0.5);
154 QCOMPARE(pinch->maximumScale(), 1.5);
156 QCOMPARE(scaleMinSpy.count(),1);
157 QCOMPARE(scaleMaxSpy.count(),1);
159 pinch->setMinimumScale(0.5);
160 pinch->setMaximumScale(1.5);
162 QCOMPARE(scaleMinSpy.count(),1);
163 QCOMPARE(scaleMaxSpy.count(),1);
165 // minimum and maximum rotation properties
166 QSignalSpy rotMinSpy(pinch, SIGNAL(minimumRotationChanged()));
167 QSignalSpy rotMaxSpy(pinch, SIGNAL(maximumRotationChanged()));
169 QCOMPARE(pinch->minimumRotation(), 0.0);
170 QCOMPARE(pinch->maximumRotation(), 90.0);
172 pinch->setMinimumRotation(-90.0);
173 pinch->setMaximumRotation(45.0);
175 QCOMPARE(pinch->minimumRotation(), -90.0);
176 QCOMPARE(pinch->maximumRotation(), 45.0);
178 QCOMPARE(rotMinSpy.count(),1);
179 QCOMPARE(rotMaxSpy.count(),1);
181 pinch->setMinimumRotation(-90.0);
182 pinch->setMaximumRotation(45.0);
184 QCOMPARE(rotMinSpy.count(),1);
185 QCOMPARE(rotMaxSpy.count(),1);
190 QTouchEvent::TouchPoint makeTouchPoint(int id, QPoint p, QGraphicsView *v, QGraphicsItem *i)
192 QTouchEvent::TouchPoint touchPoint(id);
193 touchPoint.setPos(i->mapFromScene(p));
194 touchPoint.setScreenPos(v->mapToGlobal(p));
195 touchPoint.setScenePos(p);
199 void tst_QDeclarative1PinchArea::scale()
201 QDeclarativeView *canvas = createView();
202 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
205 QTest::qWaitForWindowShown(canvas);
206 QVERIFY(canvas->rootObject() != 0);
207 qApp->processEvents();
209 QDeclarative1PinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarative1PinchArea*>("pincharea");
210 QDeclarative1Pinch *pinch = pinchArea->pinch();
211 QVERIFY(pinchArea != 0);
214 QDeclarativeItem *root = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
218 QDeclarativeItem *blackRect = canvas->rootObject()->findChild<QDeclarativeItem*>("blackrect");
219 QVERIFY(blackRect != 0);
221 QWidget *vp = canvas->viewport();
226 QTest::touchEvent(vp, device).press(0, p1, canvas);
227 QTest::touchEvent(vp, device).stationary(0).press(1, p2, canvas);
230 QTest::touchEvent(vp, device).move(0, p1, canvas).move(1, p2, canvas);
232 QCOMPARE(root->property("scale").toReal(), 1.0);
236 QTest::touchEvent(vp, device).move(0, p1, canvas).move(1, p2, canvas);
238 QCOMPARE(root->property("scale").toReal(), 1.5);
239 QCOMPARE(root->property("center").toPointF(), QPointF(40, 40)); // blackrect is at 50,50
240 QCOMPARE(blackRect->scale(), 1.5);
242 // scale beyond bound
245 QTest::touchEvent(vp, device).move(0, p1, canvas).move(1, p2, canvas);
247 QCOMPARE(blackRect->scale(), 2.0);
249 QTest::touchEvent(vp, device).release(0, p1, canvas).release(1, p2, canvas);
254 void tst_QDeclarative1PinchArea::pan()
256 QDeclarativeView *canvas = createView();
257 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
260 QTest::qWaitForWindowShown(canvas);
261 QVERIFY(canvas->rootObject() != 0);
262 qApp->processEvents();
264 QDeclarative1PinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarative1PinchArea*>("pincharea");
265 QDeclarative1Pinch *pinch = pinchArea->pinch();
266 QVERIFY(pinchArea != 0);
269 QDeclarativeItem *root = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
273 QDeclarativeItem *blackRect = canvas->rootObject()->findChild<QDeclarativeItem*>("blackrect");
274 QVERIFY(blackRect != 0);
276 QWidget *vp = canvas->viewport();
281 QTest::touchEvent(vp, device).press(0, p1, canvas);
282 QTest::touchEvent(vp, device).stationary(0).press(1, p2, canvas);
285 QTest::touchEvent(vp, device).move(0, p1, canvas).move(1, p2, canvas);
287 QCOMPARE(root->property("scale").toReal(), 1.0);
291 QTest::touchEvent(vp, device).move(0, p1, canvas).move(1, p2, canvas);
293 QCOMPARE(root->property("center").toPointF(), QPointF(60, 60)); // blackrect is at 50,50
295 QCOMPARE(blackRect->x(), 60.0);
296 QCOMPARE(blackRect->y(), 60.0);
298 // pan x beyond bound
299 p1 += QPoint(100,100);
300 p2 += QPoint(100,100);
301 QTest::touchEvent(vp, device).move(0, p1, canvas).move(1, p2, canvas);
303 QCOMPARE(blackRect->x(), 140.0);
304 QCOMPARE(blackRect->y(), 160.0);
306 QTest::touchEvent(vp, device).release(0, p1, canvas).release(1, p2, canvas);
311 void tst_QDeclarative1PinchArea::flickable()
313 QDeclarativeView *canvas = createView();
314 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/flickresize.qml"));
317 QTest::qWaitForWindowShown(canvas);
318 QVERIFY(canvas->rootObject() != 0);
319 qApp->processEvents();
321 QDeclarative1PinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarative1PinchArea*>("pincharea");
322 QDeclarative1Pinch *pinch = pinchArea->pinch();
323 QVERIFY(pinchArea != 0);
326 QDeclarative1Flickable *root = qobject_cast<QDeclarative1Flickable*>(canvas->rootObject());
329 QWidget *vp = canvas->viewport();
334 // begin by moving one touch point (mouse)
335 QTest::mousePress(vp, Qt::LeftButton, 0, canvas->mapFromScene(p1));
336 QTest::touchEvent(vp, device).press(0, p1, canvas);
339 QMouseEvent mv(QEvent::MouseMove, canvas->mapFromScene(p1), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
340 QApplication::sendEvent(canvas->viewport(), &mv);
341 QTest::touchEvent(vp, device).move(0, p1, canvas);
345 QMouseEvent mv(QEvent::MouseMove, canvas->mapFromScene(p1), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
346 QApplication::sendEvent(vp, &mv);
347 QTest::touchEvent(vp, device).move(0, p1, canvas);
351 QMouseEvent mv(QEvent::MouseMove, canvas->mapFromScene(p1), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
352 QApplication::sendEvent(vp, &mv);
353 QTest::touchEvent(vp, device).move(0, p1, canvas);
356 // Flickable has reacted to the gesture
357 QVERIFY(root->isMoving());
358 QVERIFY(root->property("scale").toReal() == 1.0);
360 // add another touch point and continue moving
361 QTest::touchEvent(vp, device).stationary(0).press(1, p2, canvas);
364 QTest::touchEvent(vp, device).move(0, p1, canvas).move(1, p2, canvas);
366 QCOMPARE(root->property("scale").toReal(), 1.0);
370 QTest::touchEvent(vp, device).move(0, p1, canvas).move(1, p2, canvas);
372 // PinchArea has stolen the gesture.
373 QVERIFY(!root->isMoving());
374 QVERIFY(root->property("scale").toReal() > 1.0);
376 QTest::mouseRelease(vp, Qt::LeftButton, 0, canvas->mapFromScene(p1));
377 QTest::touchEvent(vp, device).release(0, p1, canvas).release(1, p2, canvas);
382 QDeclarativeView *tst_QDeclarative1PinchArea::createView()
384 QDeclarativeView *canvas = new QDeclarativeView(0);
385 canvas->viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
386 canvas->setFixedSize(240,320);
391 QTEST_MAIN(tst_QDeclarative1PinchArea)
393 #include "tst_qdeclarativepincharea.moc"