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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
43 #include <QtTest/QSignalSpy>
44 #include <private/qdeclarativepincharea_p.h>
45 #include <private/qdeclarativerectangle_p.h>
46 #include <private/qdeclarativeflickable_p.h>
47 #include <QtDeclarative/qdeclarativeview.h>
48 #include <QtDeclarative/qdeclarativecontext.h>
51 // In Symbian OS test data is located in applications private dir
55 class tst_QDeclarativePinchArea: public QObject
59 void pinchProperties();
65 QDeclarativeView *createView();
68 void tst_QDeclarativePinchArea::pinchProperties()
70 QDeclarativeView *canvas = createView();
71 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
74 QVERIFY(canvas->rootObject() != 0);
76 QDeclarativePinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarativePinchArea*>("pincharea");
77 QDeclarativePinch *pinch = pinchArea->pinch();
78 QVERIFY(pinchArea != 0);
82 QDeclarativeItem *blackRect = canvas->rootObject()->findChild<QDeclarativeItem*>("blackrect");
83 QVERIFY(blackRect != 0);
84 QVERIFY(blackRect == pinch->target());
85 QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
86 QVERIFY(rootItem != 0);
87 QSignalSpy targetSpy(pinch, SIGNAL(targetChanged()));
88 pinch->setTarget(rootItem);
89 QCOMPARE(targetSpy.count(),1);
90 pinch->setTarget(rootItem);
91 QCOMPARE(targetSpy.count(),1);
94 QCOMPARE(pinch->axis(), QDeclarativePinch::XandYAxis);
95 QSignalSpy axisSpy(pinch, SIGNAL(dragAxisChanged()));
96 pinch->setAxis(QDeclarativePinch::XAxis);
97 QCOMPARE(pinch->axis(), QDeclarativePinch::XAxis);
98 QCOMPARE(axisSpy.count(),1);
99 pinch->setAxis(QDeclarativePinch::XAxis);
100 QCOMPARE(axisSpy.count(),1);
102 // minimum and maximum drag properties
103 QSignalSpy xminSpy(pinch, SIGNAL(minimumXChanged()));
104 QSignalSpy xmaxSpy(pinch, SIGNAL(maximumXChanged()));
105 QSignalSpy yminSpy(pinch, SIGNAL(minimumYChanged()));
106 QSignalSpy ymaxSpy(pinch, SIGNAL(maximumYChanged()));
108 QCOMPARE(pinch->xmin(), 0.0);
109 QCOMPARE(pinch->xmax(), rootItem->width()-blackRect->width());
110 QCOMPARE(pinch->ymin(), 0.0);
111 QCOMPARE(pinch->ymax(), rootItem->height()-blackRect->height());
118 QCOMPARE(pinch->xmin(), 10.0);
119 QCOMPARE(pinch->xmax(), 10.0);
120 QCOMPARE(pinch->ymin(), 10.0);
121 QCOMPARE(pinch->ymax(), 10.0);
123 QCOMPARE(xminSpy.count(),1);
124 QCOMPARE(xmaxSpy.count(),1);
125 QCOMPARE(yminSpy.count(),1);
126 QCOMPARE(ymaxSpy.count(),1);
133 QCOMPARE(xminSpy.count(),1);
134 QCOMPARE(xmaxSpy.count(),1);
135 QCOMPARE(yminSpy.count(),1);
136 QCOMPARE(ymaxSpy.count(),1);
138 // minimum and maximum scale properties
139 QSignalSpy scaleMinSpy(pinch, SIGNAL(minimumScaleChanged()));
140 QSignalSpy scaleMaxSpy(pinch, SIGNAL(maximumScaleChanged()));
142 QCOMPARE(pinch->minimumScale(), 1.0);
143 QCOMPARE(pinch->maximumScale(), 2.0);
145 pinch->setMinimumScale(0.5);
146 pinch->setMaximumScale(1.5);
148 QCOMPARE(pinch->minimumScale(), 0.5);
149 QCOMPARE(pinch->maximumScale(), 1.5);
151 QCOMPARE(scaleMinSpy.count(),1);
152 QCOMPARE(scaleMaxSpy.count(),1);
154 pinch->setMinimumScale(0.5);
155 pinch->setMaximumScale(1.5);
157 QCOMPARE(scaleMinSpy.count(),1);
158 QCOMPARE(scaleMaxSpy.count(),1);
160 // minimum and maximum rotation properties
161 QSignalSpy rotMinSpy(pinch, SIGNAL(minimumRotationChanged()));
162 QSignalSpy rotMaxSpy(pinch, SIGNAL(maximumRotationChanged()));
164 QCOMPARE(pinch->minimumRotation(), 0.0);
165 QCOMPARE(pinch->maximumRotation(), 90.0);
167 pinch->setMinimumRotation(-90.0);
168 pinch->setMaximumRotation(45.0);
170 QCOMPARE(pinch->minimumRotation(), -90.0);
171 QCOMPARE(pinch->maximumRotation(), 45.0);
173 QCOMPARE(rotMinSpy.count(),1);
174 QCOMPARE(rotMaxSpy.count(),1);
176 pinch->setMinimumRotation(-90.0);
177 pinch->setMaximumRotation(45.0);
179 QCOMPARE(rotMinSpy.count(),1);
180 QCOMPARE(rotMaxSpy.count(),1);
185 QTouchEvent::TouchPoint makeTouchPoint(int id, QPoint p, QGraphicsView *v, QGraphicsItem *i)
187 QTouchEvent::TouchPoint touchPoint(id);
188 touchPoint.setPos(i->mapFromScene(p));
189 touchPoint.setScreenPos(v->mapToGlobal(p));
190 touchPoint.setScenePos(p);
194 void tst_QDeclarativePinchArea::scale()
196 QDeclarativeView *canvas = createView();
197 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
200 QTest::qWaitForWindowShown(canvas);
201 QVERIFY(canvas->rootObject() != 0);
202 qApp->processEvents();
204 QDeclarativePinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarativePinchArea*>("pincharea");
205 QDeclarativePinch *pinch = pinchArea->pinch();
206 QVERIFY(pinchArea != 0);
209 QDeclarativeItem *root = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
213 QDeclarativeItem *blackRect = canvas->rootObject()->findChild<QDeclarativeItem*>("blackrect");
214 QVERIFY(blackRect != 0);
216 QWidget *vp = canvas->viewport();
221 QTest::touchEvent(vp).press(0, p1);
222 QTest::touchEvent(vp).stationary(0).press(1, p2);
225 QTest::touchEvent(vp).move(0, p1).move(1, p2);
227 QCOMPARE(root->property("scale").toReal(), 1.0);
231 QTest::touchEvent(vp).move(0, p1).move(1, p2);
233 QCOMPARE(root->property("scale").toReal(), 1.5);
234 QCOMPARE(root->property("center").toPointF(), QPointF(40, 40)); // blackrect is at 50,50
235 QCOMPARE(blackRect->scale(), 1.5);
237 // scale beyond bound
240 QTest::touchEvent(vp).move(0, p1).move(1, p2);
242 QCOMPARE(blackRect->scale(), 2.0);
244 QTest::touchEvent(vp).release(0, p1).release(1, p2);
249 void tst_QDeclarativePinchArea::pan()
251 QDeclarativeView *canvas = createView();
252 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
255 QTest::qWaitForWindowShown(canvas);
256 QVERIFY(canvas->rootObject() != 0);
257 qApp->processEvents();
259 QDeclarativePinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarativePinchArea*>("pincharea");
260 QDeclarativePinch *pinch = pinchArea->pinch();
261 QVERIFY(pinchArea != 0);
264 QDeclarativeItem *root = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
268 QDeclarativeItem *blackRect = canvas->rootObject()->findChild<QDeclarativeItem*>("blackrect");
269 QVERIFY(blackRect != 0);
271 QWidget *vp = canvas->viewport();
276 QTest::touchEvent(vp).press(0, p1);
277 QTest::touchEvent(vp).stationary(0).press(1, p2);
280 QTest::touchEvent(vp).move(0, p1).move(1, p2);
282 QCOMPARE(root->property("scale").toReal(), 1.0);
286 QTest::touchEvent(vp).move(0, p1).move(1, p2);
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(vp).move(0, p1).move(1, p2);
298 QCOMPARE(blackRect->x(), 140.0);
299 QCOMPARE(blackRect->y(), 160.0);
301 QTest::touchEvent(vp).release(0, p1).release(1, p2);
306 void tst_QDeclarativePinchArea::flickable()
308 QDeclarativeView *canvas = createView();
309 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/flickresize.qml"));
312 QTest::qWaitForWindowShown(canvas);
313 QVERIFY(canvas->rootObject() != 0);
314 qApp->processEvents();
316 QDeclarativePinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarativePinchArea*>("pincharea");
317 QDeclarativePinch *pinch = pinchArea->pinch();
318 QVERIFY(pinchArea != 0);
321 QDeclarativeFlickable *root = qobject_cast<QDeclarativeFlickable*>(canvas->rootObject());
324 QWidget *vp = canvas->viewport();
329 // begin by moving one touch point (mouse)
330 QTest::mousePress(vp, Qt::LeftButton, 0, canvas->mapFromScene(p1));
331 QTest::touchEvent(vp).press(0, p1);
334 QMouseEvent mv(QEvent::MouseMove, canvas->mapFromScene(p1), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
335 QApplication::sendEvent(canvas->viewport(), &mv);
336 QTest::touchEvent(vp).move(0, p1);
340 QMouseEvent mv(QEvent::MouseMove, canvas->mapFromScene(p1), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
341 QApplication::sendEvent(vp, &mv);
342 QTest::touchEvent(vp).move(0, p1);
346 QMouseEvent mv(QEvent::MouseMove, canvas->mapFromScene(p1), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
347 QApplication::sendEvent(vp, &mv);
348 QTest::touchEvent(vp).move(0, p1);
351 // Flickable has reacted to the gesture
352 QVERIFY(root->isMoving());
353 QVERIFY(root->property("scale").toReal() == 1.0);
355 // add another touch point and continue moving
356 QTest::touchEvent(vp).stationary(0).press(1, p2);
359 QTest::touchEvent(vp).move(0, p1).move(1, p2);
361 QCOMPARE(root->property("scale").toReal(), 1.0);
365 QTest::touchEvent(vp).move(0, p1).move(1, p2);
367 // PinchArea has stolen the gesture.
368 QVERIFY(!root->isMoving());
369 QVERIFY(root->property("scale").toReal() > 1.0);
371 QTest::mouseRelease(vp, Qt::LeftButton, 0, canvas->mapFromScene(p1));
372 QTest::touchEvent(vp).release(0, p1).release(1, p2);
377 QDeclarativeView *tst_QDeclarativePinchArea::createView()
379 QDeclarativeView *canvas = new QDeclarativeView(0);
380 canvas->viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
381 canvas->setFixedSize(240,320);
386 QTEST_MAIN(tst_QDeclarativePinchArea)
388 #include "tst_qdeclarativepincharea.moc"