1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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 <private/qquickrectangle_p.h>
46 #include <QtDeclarative/qquickview.h>
47 #include <QtDeclarative/qdeclarativecontext.h>
48 #include "../shared/util.h"
50 class tst_QQuickPinchArea: public QObject
55 void cleanupTestCase();
56 void pinchProperties();
62 QQuickView *createView();
64 void tst_QQuickPinchArea::initTestCase()
68 void tst_QQuickPinchArea::cleanupTestCase()
72 void tst_QQuickPinchArea::pinchProperties()
74 QQuickView *canvas = createView();
75 canvas->setSource(QUrl::fromLocalFile(TESTDATA("pinchproperties.qml")));
77 canvas->requestActivateWindow();
78 QVERIFY(canvas->rootObject() != 0);
80 QQuickPinchArea *pinchArea = canvas->rootObject()->findChild<QQuickPinchArea*>("pincharea");
81 QQuickPinch *pinch = pinchArea->pinch();
82 QVERIFY(pinchArea != 0);
86 QQuickItem *blackRect = canvas->rootObject()->findChild<QQuickItem*>("blackrect");
87 QVERIFY(blackRect != 0);
88 QVERIFY(blackRect == pinch->target());
89 QQuickItem *rootItem = qobject_cast<QQuickItem*>(canvas->rootObject());
90 QVERIFY(rootItem != 0);
91 QSignalSpy targetSpy(pinch, SIGNAL(targetChanged()));
92 pinch->setTarget(rootItem);
93 QCOMPARE(targetSpy.count(),1);
94 pinch->setTarget(rootItem);
95 QCOMPARE(targetSpy.count(),1);
98 QCOMPARE(pinch->axis(), QQuickPinch::XandYAxis);
99 QSignalSpy axisSpy(pinch, SIGNAL(dragAxisChanged()));
100 pinch->setAxis(QQuickPinch::XAxis);
101 QCOMPARE(pinch->axis(), QQuickPinch::XAxis);
102 QCOMPARE(axisSpy.count(),1);
103 pinch->setAxis(QQuickPinch::XAxis);
104 QCOMPARE(axisSpy.count(),1);
106 // minimum and maximum drag properties
107 QSignalSpy xminSpy(pinch, SIGNAL(minimumXChanged()));
108 QSignalSpy xmaxSpy(pinch, SIGNAL(maximumXChanged()));
109 QSignalSpy yminSpy(pinch, SIGNAL(minimumYChanged()));
110 QSignalSpy ymaxSpy(pinch, SIGNAL(maximumYChanged()));
112 QCOMPARE(pinch->xmin(), 0.0);
113 QCOMPARE(pinch->xmax(), rootItem->width()-blackRect->width());
114 QCOMPARE(pinch->ymin(), 0.0);
115 QCOMPARE(pinch->ymax(), rootItem->height()-blackRect->height());
122 QCOMPARE(pinch->xmin(), 10.0);
123 QCOMPARE(pinch->xmax(), 10.0);
124 QCOMPARE(pinch->ymin(), 10.0);
125 QCOMPARE(pinch->ymax(), 10.0);
127 QCOMPARE(xminSpy.count(),1);
128 QCOMPARE(xmaxSpy.count(),1);
129 QCOMPARE(yminSpy.count(),1);
130 QCOMPARE(ymaxSpy.count(),1);
137 QCOMPARE(xminSpy.count(),1);
138 QCOMPARE(xmaxSpy.count(),1);
139 QCOMPARE(yminSpy.count(),1);
140 QCOMPARE(ymaxSpy.count(),1);
142 // minimum and maximum scale properties
143 QSignalSpy scaleMinSpy(pinch, SIGNAL(minimumScaleChanged()));
144 QSignalSpy scaleMaxSpy(pinch, SIGNAL(maximumScaleChanged()));
146 QCOMPARE(pinch->minimumScale(), 1.0);
147 QCOMPARE(pinch->maximumScale(), 2.0);
149 pinch->setMinimumScale(0.5);
150 pinch->setMaximumScale(1.5);
152 QCOMPARE(pinch->minimumScale(), 0.5);
153 QCOMPARE(pinch->maximumScale(), 1.5);
155 QCOMPARE(scaleMinSpy.count(),1);
156 QCOMPARE(scaleMaxSpy.count(),1);
158 pinch->setMinimumScale(0.5);
159 pinch->setMaximumScale(1.5);
161 QCOMPARE(scaleMinSpy.count(),1);
162 QCOMPARE(scaleMaxSpy.count(),1);
164 // minimum and maximum rotation properties
165 QSignalSpy rotMinSpy(pinch, SIGNAL(minimumRotationChanged()));
166 QSignalSpy rotMaxSpy(pinch, SIGNAL(maximumRotationChanged()));
168 QCOMPARE(pinch->minimumRotation(), 0.0);
169 QCOMPARE(pinch->maximumRotation(), 90.0);
171 pinch->setMinimumRotation(-90.0);
172 pinch->setMaximumRotation(45.0);
174 QCOMPARE(pinch->minimumRotation(), -90.0);
175 QCOMPARE(pinch->maximumRotation(), 45.0);
177 QCOMPARE(rotMinSpy.count(),1);
178 QCOMPARE(rotMaxSpy.count(),1);
180 pinch->setMinimumRotation(-90.0);
181 pinch->setMaximumRotation(45.0);
183 QCOMPARE(rotMinSpy.count(),1);
184 QCOMPARE(rotMaxSpy.count(),1);
189 QTouchEvent::TouchPoint makeTouchPoint(int id, QPoint p, QQuickView *v, QQuickItem *i)
191 QTouchEvent::TouchPoint touchPoint(id);
192 touchPoint.setPos(i->mapFromScene(p));
193 touchPoint.setScreenPos(v->mapToGlobal(p));
194 touchPoint.setScenePos(p);
198 void tst_QQuickPinchArea::scale()
200 QQuickView *canvas = createView();
201 canvas->setSource(QUrl::fromLocalFile(TESTDATA("pinchproperties.qml")));
203 canvas->requestActivateWindow();
204 QTest::qWaitForWindowShown(canvas);
205 QVERIFY(canvas->rootObject() != 0);
206 qApp->processEvents();
208 QQuickPinchArea *pinchArea = canvas->rootObject()->findChild<QQuickPinchArea*>("pincharea");
209 QQuickPinch *pinch = pinchArea->pinch();
210 QVERIFY(pinchArea != 0);
213 QQuickItem *root = qobject_cast<QQuickItem*>(canvas->rootObject());
217 QQuickItem *blackRect = canvas->rootObject()->findChild<QQuickItem*>("blackrect");
218 QVERIFY(blackRect != 0);
223 QTest::touchEvent(canvas).press(0, p1, canvas);
224 QTest::touchEvent(canvas).stationary(0).press(1, p2, canvas);
227 QTest::touchEvent(canvas).move(0, p1,canvas).move(1, p2,canvas);
229 QCOMPARE(root->property("scale").toReal(), 1.0);
233 QTest::touchEvent(canvas).move(0, p1,canvas).move(1, p2,canvas);
235 QCOMPARE(root->property("scale").toReal(), 1.5);
236 QCOMPARE(root->property("center").toPointF(), QPointF(40, 40)); // blackrect is at 50,50
237 QCOMPARE(blackRect->scale(), 1.5);
239 // scale beyond bound
242 QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
244 QCOMPARE(blackRect->scale(), 2.0);
246 QTest::touchEvent(canvas).release(0, p1, canvas).release(1, p2, canvas);
251 void tst_QQuickPinchArea::pan()
253 QQuickView *canvas = createView();
254 canvas->setSource(QUrl::fromLocalFile(TESTDATA("pinchproperties.qml")));
256 canvas->requestActivateWindow();
257 QTest::qWaitForWindowShown(canvas);
258 QVERIFY(canvas->rootObject() != 0);
259 qApp->processEvents();
261 QQuickPinchArea *pinchArea = canvas->rootObject()->findChild<QQuickPinchArea*>("pincharea");
262 QQuickPinch *pinch = pinchArea->pinch();
263 QVERIFY(pinchArea != 0);
266 QQuickItem *root = qobject_cast<QQuickItem*>(canvas->rootObject());
270 QQuickItem *blackRect = canvas->rootObject()->findChild<QQuickItem*>("blackrect");
271 QVERIFY(blackRect != 0);
276 QTest::touchEvent(canvas).press(0, p1, canvas);
277 QTest::touchEvent(canvas).stationary(0).press(1, p2, canvas);
280 QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
282 QCOMPARE(root->property("scale").toReal(), 1.0);
286 QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
288 QCOMPARE(root->property("center").toPointF(), QPointF(60, 60)); // blackrect is at 50,50
290 QCOMPARE(blackRect->x(), 60.0);
291 QCOMPARE(blackRect->y(), 60.0);
293 // pan x beyond bound
294 p1 += QPoint(100,100);
295 p2 += QPoint(100,100);
296 QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
298 QCOMPARE(blackRect->x(), 140.0);
299 QCOMPARE(blackRect->y(), 160.0);
301 QTest::touchEvent(canvas).release(0, p1, canvas).release(1, p2, canvas);
306 // test pinch, release one point, touch again to continue pinch
307 void tst_QQuickPinchArea::retouch()
309 QQuickView *canvas = createView();
310 canvas->setSource(QUrl::fromLocalFile(TESTDATA("pinchproperties.qml")));
312 canvas->requestActivateWindow();
313 QTest::qWaitForWindowShown(canvas);
314 QVERIFY(canvas->rootObject() != 0);
315 qApp->processEvents();
317 QQuickPinchArea *pinchArea = canvas->rootObject()->findChild<QQuickPinchArea*>("pincharea");
318 QQuickPinch *pinch = pinchArea->pinch();
319 QVERIFY(pinchArea != 0);
322 QQuickItem *root = qobject_cast<QQuickItem*>(canvas->rootObject());
325 QSignalSpy startedSpy(pinchArea, SIGNAL(pinchStarted(QQuickPinchEvent *)));
326 QSignalSpy finishedSpy(pinchArea, SIGNAL(pinchFinished(QQuickPinchEvent *)));
329 QQuickItem *blackRect = canvas->rootObject()->findChild<QQuickItem*>("blackrect");
330 QVERIFY(blackRect != 0);
335 QTest::touchEvent(canvas).press(0, p1, canvas);
336 QTest::touchEvent(canvas).stationary(0).press(1, p2, canvas);
339 QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
341 QCOMPARE(root->property("scale").toReal(), 1.0);
345 QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
347 QCOMPARE(startedSpy.count(), 1);
349 QCOMPARE(root->property("scale").toReal(), 1.5);
350 QCOMPARE(root->property("center").toPointF(), QPointF(40, 40)); // blackrect is at 50,50
351 QCOMPARE(blackRect->scale(), 1.5);
353 QCOMPARE(canvas->rootObject()->property("pointCount").toInt(), 2);
355 QCOMPARE(startedSpy.count(), 1);
356 QCOMPARE(finishedSpy.count(), 0);
358 QTest::touchEvent(canvas).stationary(0).release(1, p2, canvas);
360 QCOMPARE(startedSpy.count(), 1);
361 QCOMPARE(finishedSpy.count(), 0);
363 QCOMPARE(canvas->rootObject()->property("pointCount").toInt(), 1);
365 QTest::touchEvent(canvas).stationary(0).press(1, p2, canvas);
368 QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
370 // Lifting and retouching results in onPinchStarted being called again
371 QCOMPARE(startedSpy.count(), 2);
372 QCOMPARE(finishedSpy.count(), 0);
374 QCOMPARE(canvas->rootObject()->property("pointCount").toInt(), 2);
376 QTest::touchEvent(canvas).release(0, p1, canvas).release(1, p2, canvas);
378 QCOMPARE(startedSpy.count(), 2);
379 QCOMPARE(finishedSpy.count(), 1);
385 QQuickView *tst_QQuickPinchArea::createView()
387 QQuickView *canvas = new QQuickView(0);
388 canvas->setGeometry(0,0,240,320);
393 QTEST_MAIN(tst_QQuickPinchArea)
395 #include "tst_qquickpincharea.moc"