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 <private/qquickpincharea_p.h>
45 #include <QtQuick/private/qquickrectangle_p.h>
46 #include <QtQuick/qquickview.h>
47 #include <QtDeclarative/qdeclarativecontext.h>
48 #include "../../shared/util.h"
50 class tst_QQuickPinchArea: public QDeclarativeDataTest
54 tst_QQuickPinchArea() : device(0) { }
57 void cleanupTestCase();
58 void pinchProperties();
64 QQuickView *createView();
67 void tst_QQuickPinchArea::initTestCase()
69 QDeclarativeDataTest::initTestCase();
71 device = new QTouchDevice;
72 device->setType(QTouchDevice::TouchScreen);
73 QWindowSystemInterface::registerTouchDevice(device);
77 void tst_QQuickPinchArea::cleanupTestCase()
81 void tst_QQuickPinchArea::pinchProperties()
83 QQuickView *canvas = createView();
84 canvas->setSource(testFileUrl("pinchproperties.qml"));
86 canvas->requestActivateWindow();
87 QVERIFY(canvas->rootObject() != 0);
89 QQuickPinchArea *pinchArea = canvas->rootObject()->findChild<QQuickPinchArea*>("pincharea");
90 QQuickPinch *pinch = pinchArea->pinch();
91 QVERIFY(pinchArea != 0);
95 QQuickItem *blackRect = canvas->rootObject()->findChild<QQuickItem*>("blackrect");
96 QVERIFY(blackRect != 0);
97 QVERIFY(blackRect == pinch->target());
98 QQuickItem *rootItem = qobject_cast<QQuickItem*>(canvas->rootObject());
99 QVERIFY(rootItem != 0);
100 QSignalSpy targetSpy(pinch, SIGNAL(targetChanged()));
101 pinch->setTarget(rootItem);
102 QCOMPARE(targetSpy.count(),1);
103 pinch->setTarget(rootItem);
104 QCOMPARE(targetSpy.count(),1);
107 QCOMPARE(pinch->axis(), QQuickPinch::XandYAxis);
108 QSignalSpy axisSpy(pinch, SIGNAL(dragAxisChanged()));
109 pinch->setAxis(QQuickPinch::XAxis);
110 QCOMPARE(pinch->axis(), QQuickPinch::XAxis);
111 QCOMPARE(axisSpy.count(),1);
112 pinch->setAxis(QQuickPinch::XAxis);
113 QCOMPARE(axisSpy.count(),1);
115 // minimum and maximum drag properties
116 QSignalSpy xminSpy(pinch, SIGNAL(minimumXChanged()));
117 QSignalSpy xmaxSpy(pinch, SIGNAL(maximumXChanged()));
118 QSignalSpy yminSpy(pinch, SIGNAL(minimumYChanged()));
119 QSignalSpy ymaxSpy(pinch, SIGNAL(maximumYChanged()));
121 QCOMPARE(pinch->xmin(), 0.0);
122 QCOMPARE(pinch->xmax(), rootItem->width()-blackRect->width());
123 QCOMPARE(pinch->ymin(), 0.0);
124 QCOMPARE(pinch->ymax(), rootItem->height()-blackRect->height());
131 QCOMPARE(pinch->xmin(), 10.0);
132 QCOMPARE(pinch->xmax(), 10.0);
133 QCOMPARE(pinch->ymin(), 10.0);
134 QCOMPARE(pinch->ymax(), 10.0);
136 QCOMPARE(xminSpy.count(),1);
137 QCOMPARE(xmaxSpy.count(),1);
138 QCOMPARE(yminSpy.count(),1);
139 QCOMPARE(ymaxSpy.count(),1);
146 QCOMPARE(xminSpy.count(),1);
147 QCOMPARE(xmaxSpy.count(),1);
148 QCOMPARE(yminSpy.count(),1);
149 QCOMPARE(ymaxSpy.count(),1);
151 // minimum and maximum scale properties
152 QSignalSpy scaleMinSpy(pinch, SIGNAL(minimumScaleChanged()));
153 QSignalSpy scaleMaxSpy(pinch, SIGNAL(maximumScaleChanged()));
155 QCOMPARE(pinch->minimumScale(), 1.0);
156 QCOMPARE(pinch->maximumScale(), 2.0);
158 pinch->setMinimumScale(0.5);
159 pinch->setMaximumScale(1.5);
161 QCOMPARE(pinch->minimumScale(), 0.5);
162 QCOMPARE(pinch->maximumScale(), 1.5);
164 QCOMPARE(scaleMinSpy.count(),1);
165 QCOMPARE(scaleMaxSpy.count(),1);
167 pinch->setMinimumScale(0.5);
168 pinch->setMaximumScale(1.5);
170 QCOMPARE(scaleMinSpy.count(),1);
171 QCOMPARE(scaleMaxSpy.count(),1);
173 // minimum and maximum rotation properties
174 QSignalSpy rotMinSpy(pinch, SIGNAL(minimumRotationChanged()));
175 QSignalSpy rotMaxSpy(pinch, SIGNAL(maximumRotationChanged()));
177 QCOMPARE(pinch->minimumRotation(), 0.0);
178 QCOMPARE(pinch->maximumRotation(), 90.0);
180 pinch->setMinimumRotation(-90.0);
181 pinch->setMaximumRotation(45.0);
183 QCOMPARE(pinch->minimumRotation(), -90.0);
184 QCOMPARE(pinch->maximumRotation(), 45.0);
186 QCOMPARE(rotMinSpy.count(),1);
187 QCOMPARE(rotMaxSpy.count(),1);
189 pinch->setMinimumRotation(-90.0);
190 pinch->setMaximumRotation(45.0);
192 QCOMPARE(rotMinSpy.count(),1);
193 QCOMPARE(rotMaxSpy.count(),1);
198 QTouchEvent::TouchPoint makeTouchPoint(int id, QPoint p, QQuickView *v, QQuickItem *i)
200 QTouchEvent::TouchPoint touchPoint(id);
201 touchPoint.setPos(i->mapFromScene(p));
202 touchPoint.setScreenPos(v->mapToGlobal(p));
203 touchPoint.setScenePos(p);
207 void tst_QQuickPinchArea::scale()
209 QQuickView *canvas = createView();
210 canvas->setSource(testFileUrl("pinchproperties.qml"));
212 canvas->requestActivateWindow();
213 QTest::qWaitForWindowShown(canvas);
214 QVERIFY(canvas->rootObject() != 0);
215 qApp->processEvents();
217 QQuickPinchArea *pinchArea = canvas->rootObject()->findChild<QQuickPinchArea*>("pincharea");
218 QQuickPinch *pinch = pinchArea->pinch();
219 QVERIFY(pinchArea != 0);
222 QQuickItem *root = qobject_cast<QQuickItem*>(canvas->rootObject());
226 QQuickItem *blackRect = canvas->rootObject()->findChild<QQuickItem*>("blackrect");
227 QVERIFY(blackRect != 0);
232 QTest::touchEvent(canvas, device).press(0, p1, canvas);
233 QTest::touchEvent(canvas, device).stationary(0).press(1, p2, canvas);
236 QTest::touchEvent(canvas, device).move(0, p1,canvas).move(1, p2,canvas);
238 QCOMPARE(root->property("scale").toReal(), 1.0);
242 QTest::touchEvent(canvas, device).move(0, p1,canvas).move(1, p2,canvas);
244 QCOMPARE(root->property("scale").toReal(), 1.5);
245 QCOMPARE(root->property("center").toPointF(), QPointF(40, 40)); // blackrect is at 50,50
246 QCOMPARE(blackRect->scale(), 1.5);
248 // scale beyond bound
251 QTest::touchEvent(canvas, device).move(0, p1, canvas).move(1, p2, canvas);
253 QCOMPARE(blackRect->scale(), 2.0);
255 QTest::touchEvent(canvas, device).release(0, p1, canvas).release(1, p2, canvas);
260 void tst_QQuickPinchArea::pan()
262 QQuickView *canvas = createView();
263 canvas->setSource(testFileUrl("pinchproperties.qml"));
265 canvas->requestActivateWindow();
266 QTest::qWaitForWindowShown(canvas);
267 QVERIFY(canvas->rootObject() != 0);
268 qApp->processEvents();
270 QQuickPinchArea *pinchArea = canvas->rootObject()->findChild<QQuickPinchArea*>("pincharea");
271 QQuickPinch *pinch = pinchArea->pinch();
272 QVERIFY(pinchArea != 0);
275 QQuickItem *root = qobject_cast<QQuickItem*>(canvas->rootObject());
279 QQuickItem *blackRect = canvas->rootObject()->findChild<QQuickItem*>("blackrect");
280 QVERIFY(blackRect != 0);
285 QTest::touchEvent(canvas, device).press(0, p1, canvas);
286 QTest::touchEvent(canvas, device).stationary(0).press(1, p2, canvas);
289 QTest::touchEvent(canvas, device).move(0, p1, canvas).move(1, p2, canvas);
291 QCOMPARE(root->property("scale").toReal(), 1.0);
295 QTest::touchEvent(canvas, device).move(0, p1, canvas).move(1, p2, canvas);
297 QCOMPARE(root->property("center").toPointF(), QPointF(60, 60)); // blackrect is at 50,50
299 QCOMPARE(blackRect->x(), 60.0);
300 QCOMPARE(blackRect->y(), 60.0);
302 // pan x beyond bound
303 p1 += QPoint(100,100);
304 p2 += QPoint(100,100);
305 QTest::touchEvent(canvas, device).move(0, p1, canvas).move(1, p2, canvas);
307 QCOMPARE(blackRect->x(), 140.0);
308 QCOMPARE(blackRect->y(), 160.0);
310 QTest::touchEvent(canvas, device).release(0, p1, canvas).release(1, p2, canvas);
315 // test pinch, release one point, touch again to continue pinch
316 void tst_QQuickPinchArea::retouch()
318 QQuickView *canvas = createView();
319 canvas->setSource(testFileUrl("pinchproperties.qml"));
321 canvas->requestActivateWindow();
322 QTest::qWaitForWindowShown(canvas);
323 QVERIFY(canvas->rootObject() != 0);
324 qApp->processEvents();
326 QQuickPinchArea *pinchArea = canvas->rootObject()->findChild<QQuickPinchArea*>("pincharea");
327 QQuickPinch *pinch = pinchArea->pinch();
328 QVERIFY(pinchArea != 0);
331 QQuickItem *root = qobject_cast<QQuickItem*>(canvas->rootObject());
334 QSignalSpy startedSpy(pinchArea, SIGNAL(pinchStarted(QQuickPinchEvent *)));
335 QSignalSpy finishedSpy(pinchArea, SIGNAL(pinchFinished(QQuickPinchEvent *)));
338 QQuickItem *blackRect = canvas->rootObject()->findChild<QQuickItem*>("blackrect");
339 QVERIFY(blackRect != 0);
344 QTest::touchEvent(canvas, device).press(0, p1, canvas);
345 QTest::touchEvent(canvas, device).stationary(0).press(1, p2, canvas);
348 QTest::touchEvent(canvas, device).move(0, p1, canvas).move(1, p2, canvas);
350 QCOMPARE(root->property("scale").toReal(), 1.0);
354 QTest::touchEvent(canvas, device).move(0, p1, canvas).move(1, p2, canvas);
356 QCOMPARE(startedSpy.count(), 1);
358 QCOMPARE(root->property("scale").toReal(), 1.5);
359 QCOMPARE(root->property("center").toPointF(), QPointF(40, 40)); // blackrect is at 50,50
360 QCOMPARE(blackRect->scale(), 1.5);
362 QCOMPARE(canvas->rootObject()->property("pointCount").toInt(), 2);
364 QCOMPARE(startedSpy.count(), 1);
365 QCOMPARE(finishedSpy.count(), 0);
367 QTest::touchEvent(canvas, device).stationary(0).release(1, p2, canvas);
369 QCOMPARE(startedSpy.count(), 1);
370 QCOMPARE(finishedSpy.count(), 0);
372 QCOMPARE(canvas->rootObject()->property("pointCount").toInt(), 1);
374 QTest::touchEvent(canvas, device).stationary(0).press(1, p2, canvas);
377 QTest::touchEvent(canvas, device).move(0, p1, canvas).move(1, p2, canvas);
379 // Lifting and retouching results in onPinchStarted being called again
380 QCOMPARE(startedSpy.count(), 2);
381 QCOMPARE(finishedSpy.count(), 0);
383 QCOMPARE(canvas->rootObject()->property("pointCount").toInt(), 2);
385 QTest::touchEvent(canvas, device).release(0, p1, canvas).release(1, p2, canvas);
387 QCOMPARE(startedSpy.count(), 2);
388 QCOMPARE(finishedSpy.count(), 1);
394 QQuickView *tst_QQuickPinchArea::createView()
396 QQuickView *canvas = new QQuickView(0);
397 canvas->setGeometry(0,0,240,320);
402 QTEST_MAIN(tst_QQuickPinchArea)
404 #include "tst_qquickpincharea.moc"