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/qsgpincharea_p.h>
45 #include <private/qsgrectangle_p.h>
46 #include <QtDeclarative/qsgview.h>
47 #include <QtDeclarative/qdeclarativecontext.h>
48 #include <QtOpenGL/QGLShaderProgram>
51 // In Symbian OS test data is located in applications private dir
55 class tst_QSGPinchArea: public QObject
60 void cleanupTestCase();
61 void pinchProperties();
67 QSGView *createView();
69 void tst_QSGPinchArea::initTestCase()
73 void tst_QSGPinchArea::cleanupTestCase()
77 void tst_QSGPinchArea::pinchProperties()
79 QSGView *canvas = createView();
80 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
82 canvas->requestActivateWindow();
83 QVERIFY(canvas->rootObject() != 0);
85 QSGPinchArea *pinchArea = canvas->rootObject()->findChild<QSGPinchArea*>("pincharea");
86 QSGPinch *pinch = pinchArea->pinch();
87 QVERIFY(pinchArea != 0);
91 QSGItem *blackRect = canvas->rootObject()->findChild<QSGItem*>("blackrect");
92 QVERIFY(blackRect != 0);
93 QVERIFY(blackRect == pinch->target());
94 QSGItem *rootItem = qobject_cast<QSGItem*>(canvas->rootObject());
95 QVERIFY(rootItem != 0);
96 QSignalSpy targetSpy(pinch, SIGNAL(targetChanged()));
97 pinch->setTarget(rootItem);
98 QCOMPARE(targetSpy.count(),1);
99 pinch->setTarget(rootItem);
100 QCOMPARE(targetSpy.count(),1);
103 QCOMPARE(pinch->axis(), QSGPinch::XandYAxis);
104 QSignalSpy axisSpy(pinch, SIGNAL(dragAxisChanged()));
105 pinch->setAxis(QSGPinch::XAxis);
106 QCOMPARE(pinch->axis(), QSGPinch::XAxis);
107 QCOMPARE(axisSpy.count(),1);
108 pinch->setAxis(QSGPinch::XAxis);
109 QCOMPARE(axisSpy.count(),1);
111 // minimum and maximum drag properties
112 QSignalSpy xminSpy(pinch, SIGNAL(minimumXChanged()));
113 QSignalSpy xmaxSpy(pinch, SIGNAL(maximumXChanged()));
114 QSignalSpy yminSpy(pinch, SIGNAL(minimumYChanged()));
115 QSignalSpy ymaxSpy(pinch, SIGNAL(maximumYChanged()));
117 QCOMPARE(pinch->xmin(), 0.0);
118 QCOMPARE(pinch->xmax(), rootItem->width()-blackRect->width());
119 QCOMPARE(pinch->ymin(), 0.0);
120 QCOMPARE(pinch->ymax(), rootItem->height()-blackRect->height());
127 QCOMPARE(pinch->xmin(), 10.0);
128 QCOMPARE(pinch->xmax(), 10.0);
129 QCOMPARE(pinch->ymin(), 10.0);
130 QCOMPARE(pinch->ymax(), 10.0);
132 QCOMPARE(xminSpy.count(),1);
133 QCOMPARE(xmaxSpy.count(),1);
134 QCOMPARE(yminSpy.count(),1);
135 QCOMPARE(ymaxSpy.count(),1);
142 QCOMPARE(xminSpy.count(),1);
143 QCOMPARE(xmaxSpy.count(),1);
144 QCOMPARE(yminSpy.count(),1);
145 QCOMPARE(ymaxSpy.count(),1);
147 // minimum and maximum scale properties
148 QSignalSpy scaleMinSpy(pinch, SIGNAL(minimumScaleChanged()));
149 QSignalSpy scaleMaxSpy(pinch, SIGNAL(maximumScaleChanged()));
151 QCOMPARE(pinch->minimumScale(), 1.0);
152 QCOMPARE(pinch->maximumScale(), 2.0);
154 pinch->setMinimumScale(0.5);
155 pinch->setMaximumScale(1.5);
157 QCOMPARE(pinch->minimumScale(), 0.5);
158 QCOMPARE(pinch->maximumScale(), 1.5);
160 QCOMPARE(scaleMinSpy.count(),1);
161 QCOMPARE(scaleMaxSpy.count(),1);
163 pinch->setMinimumScale(0.5);
164 pinch->setMaximumScale(1.5);
166 QCOMPARE(scaleMinSpy.count(),1);
167 QCOMPARE(scaleMaxSpy.count(),1);
169 // minimum and maximum rotation properties
170 QSignalSpy rotMinSpy(pinch, SIGNAL(minimumRotationChanged()));
171 QSignalSpy rotMaxSpy(pinch, SIGNAL(maximumRotationChanged()));
173 QCOMPARE(pinch->minimumRotation(), 0.0);
174 QCOMPARE(pinch->maximumRotation(), 90.0);
176 pinch->setMinimumRotation(-90.0);
177 pinch->setMaximumRotation(45.0);
179 QCOMPARE(pinch->minimumRotation(), -90.0);
180 QCOMPARE(pinch->maximumRotation(), 45.0);
182 QCOMPARE(rotMinSpy.count(),1);
183 QCOMPARE(rotMaxSpy.count(),1);
185 pinch->setMinimumRotation(-90.0);
186 pinch->setMaximumRotation(45.0);
188 QCOMPARE(rotMinSpy.count(),1);
189 QCOMPARE(rotMaxSpy.count(),1);
194 QTouchEvent::TouchPoint makeTouchPoint(int id, QPoint p, QSGView *v, QSGItem *i)
196 QTouchEvent::TouchPoint touchPoint(id);
197 touchPoint.setPos(i->mapFromScene(p));
198 touchPoint.setScreenPos(v->mapToGlobal(p));
199 touchPoint.setScenePos(p);
203 void tst_QSGPinchArea::scale()
205 QSGView *canvas = createView();
206 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
208 canvas->requestActivateWindow();
209 QTest::qWaitForWindowShown(canvas);
210 QVERIFY(canvas->rootObject() != 0);
211 qApp->processEvents();
213 QSGPinchArea *pinchArea = canvas->rootObject()->findChild<QSGPinchArea*>("pincharea");
214 QSGPinch *pinch = pinchArea->pinch();
215 QVERIFY(pinchArea != 0);
218 QSGItem *root = qobject_cast<QSGItem*>(canvas->rootObject());
222 QSGItem *blackRect = canvas->rootObject()->findChild<QSGItem*>("blackrect");
223 QVERIFY(blackRect != 0);
228 QTest::touchEvent(canvas).press(0, p1, canvas);
229 QTest::touchEvent(canvas).stationary(0).press(1, p2, canvas);
232 QTest::touchEvent(canvas).move(0, p1,canvas).move(1, p2,canvas);
234 QCOMPARE(root->property("scale").toReal(), 1.0);
238 QTest::touchEvent(canvas).move(0, p1,canvas).move(1, p2,canvas);
240 QCOMPARE(root->property("scale").toReal(), 1.5);
241 QCOMPARE(root->property("center").toPointF(), QPointF(40, 40)); // blackrect is at 50,50
242 QCOMPARE(blackRect->scale(), 1.5);
244 // scale beyond bound
247 QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
249 QCOMPARE(blackRect->scale(), 2.0);
251 QTest::touchEvent(canvas).release(0, p1, canvas).release(1, p2, canvas);
256 void tst_QSGPinchArea::pan()
258 QSGView *canvas = createView();
259 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
261 canvas->requestActivateWindow();
262 QTest::qWaitForWindowShown(canvas);
263 QVERIFY(canvas->rootObject() != 0);
264 qApp->processEvents();
266 QSGPinchArea *pinchArea = canvas->rootObject()->findChild<QSGPinchArea*>("pincharea");
267 QSGPinch *pinch = pinchArea->pinch();
268 QVERIFY(pinchArea != 0);
271 QSGItem *root = qobject_cast<QSGItem*>(canvas->rootObject());
275 QSGItem *blackRect = canvas->rootObject()->findChild<QSGItem*>("blackrect");
276 QVERIFY(blackRect != 0);
281 QTest::touchEvent(canvas).press(0, p1, canvas);
282 QTest::touchEvent(canvas).stationary(0).press(1, p2, canvas);
285 QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
287 QCOMPARE(root->property("scale").toReal(), 1.0);
291 QTest::touchEvent(canvas).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(canvas).move(0, p1, canvas).move(1, p2, canvas);
303 QCOMPARE(blackRect->x(), 140.0);
304 QCOMPARE(blackRect->y(), 160.0);
306 QTest::touchEvent(canvas).release(0, p1, canvas).release(1, p2, canvas);
311 // test pinch, release one point, touch again to continue pinch
312 void tst_QSGPinchArea::retouch()
314 QSGView *canvas = createView();
315 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
317 canvas->requestActivateWindow();
318 QTest::qWaitForWindowShown(canvas);
319 QVERIFY(canvas->rootObject() != 0);
320 qApp->processEvents();
322 QSGPinchArea *pinchArea = canvas->rootObject()->findChild<QSGPinchArea*>("pincharea");
323 QSGPinch *pinch = pinchArea->pinch();
324 QVERIFY(pinchArea != 0);
327 QSGItem *root = qobject_cast<QSGItem*>(canvas->rootObject());
330 QSignalSpy startedSpy(pinchArea, SIGNAL(pinchStarted(QSGPinchEvent *)));
331 QSignalSpy finishedSpy(pinchArea, SIGNAL(pinchFinished(QSGPinchEvent *)));
334 QSGItem *blackRect = canvas->rootObject()->findChild<QSGItem*>("blackrect");
335 QVERIFY(blackRect != 0);
340 QTest::touchEvent(canvas).press(0, p1, canvas);
341 QTest::touchEvent(canvas).stationary(0).press(1, p2, canvas);
344 QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
346 QCOMPARE(root->property("scale").toReal(), 1.0);
350 QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
352 QCOMPARE(startedSpy.count(), 1);
354 QCOMPARE(root->property("scale").toReal(), 1.5);
355 QCOMPARE(root->property("center").toPointF(), QPointF(40, 40)); // blackrect is at 50,50
356 QCOMPARE(blackRect->scale(), 1.5);
358 QCOMPARE(canvas->rootObject()->property("pointCount").toInt(), 2);
360 QCOMPARE(startedSpy.count(), 1);
361 QCOMPARE(finishedSpy.count(), 0);
363 QTest::touchEvent(canvas).stationary(0).release(1, p2, canvas);
365 QCOMPARE(startedSpy.count(), 1);
366 QCOMPARE(finishedSpy.count(), 0);
368 QCOMPARE(canvas->rootObject()->property("pointCount").toInt(), 1);
370 QTest::touchEvent(canvas).stationary(0).press(1, p2, canvas);
373 QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
375 // Lifting and retouching results in onPinchStarted being called again
376 QCOMPARE(startedSpy.count(), 2);
377 QCOMPARE(finishedSpy.count(), 0);
379 QCOMPARE(canvas->rootObject()->property("pointCount").toInt(), 2);
381 QTest::touchEvent(canvas).release(0, p1, canvas).release(1, p2, canvas);
383 QCOMPARE(startedSpy.count(), 2);
384 QCOMPARE(finishedSpy.count(), 1);
390 QSGView *tst_QSGPinchArea::createView()
392 QSGView *canvas = new QSGView(0);
393 canvas->setGeometry(0,0,240,320);
398 QTEST_MAIN(tst_QSGPinchArea)
400 #include "tst_qsgpincharea.moc"