Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick1 / qdeclarativepincharea / tst_qdeclarativepincharea.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
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>
49
50 class tst_QDeclarative1PinchArea: public QObject
51 {
52     Q_OBJECT
53 public:
54     tst_QDeclarative1PinchArea() : device(0) { }
55 private slots:
56     void initTestCase() {
57         if (!device) {
58             device = new QTouchDevice;
59             device->setType(QTouchDevice::TouchScreen);
60             QWindowSystemInterface::registerTouchDevice(device);
61         }
62     }
63     void pinchProperties();
64     void scale();
65     void pan();
66     void flickable();
67
68 private:
69     QDeclarativeView *createView();
70     QTouchDevice *device;
71 };
72
73 void tst_QDeclarative1PinchArea::pinchProperties()
74 {
75     QDeclarativeView *canvas = createView();
76     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
77     canvas->show();
78     canvas->setFocus();
79     QVERIFY(canvas->rootObject() != 0);
80
81     QDeclarative1PinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarative1PinchArea*>("pincharea");
82     QDeclarative1Pinch *pinch = pinchArea->pinch();
83     QVERIFY(pinchArea != 0);
84     QVERIFY(pinch != 0);
85
86     // target
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);
97
98     // axis
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);
106
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()));
112
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());
117
118     pinch->setXmin(10);
119     pinch->setXmax(10);
120     pinch->setYmin(10);
121     pinch->setYmax(10);
122
123     QCOMPARE(pinch->xmin(), 10.0);
124     QCOMPARE(pinch->xmax(), 10.0);
125     QCOMPARE(pinch->ymin(), 10.0);
126     QCOMPARE(pinch->ymax(), 10.0);
127
128     QCOMPARE(xminSpy.count(),1);
129     QCOMPARE(xmaxSpy.count(),1);
130     QCOMPARE(yminSpy.count(),1);
131     QCOMPARE(ymaxSpy.count(),1);
132
133     pinch->setXmin(10);
134     pinch->setXmax(10);
135     pinch->setYmin(10);
136     pinch->setYmax(10);
137
138     QCOMPARE(xminSpy.count(),1);
139     QCOMPARE(xmaxSpy.count(),1);
140     QCOMPARE(yminSpy.count(),1);
141     QCOMPARE(ymaxSpy.count(),1);
142
143     // minimum and maximum scale properties
144     QSignalSpy scaleMinSpy(pinch, SIGNAL(minimumScaleChanged()));
145     QSignalSpy scaleMaxSpy(pinch, SIGNAL(maximumScaleChanged()));
146
147     QCOMPARE(pinch->minimumScale(), 1.0);
148     QCOMPARE(pinch->maximumScale(), 2.0);
149
150     pinch->setMinimumScale(0.5);
151     pinch->setMaximumScale(1.5);
152
153     QCOMPARE(pinch->minimumScale(), 0.5);
154     QCOMPARE(pinch->maximumScale(), 1.5);
155
156     QCOMPARE(scaleMinSpy.count(),1);
157     QCOMPARE(scaleMaxSpy.count(),1);
158
159     pinch->setMinimumScale(0.5);
160     pinch->setMaximumScale(1.5);
161
162     QCOMPARE(scaleMinSpy.count(),1);
163     QCOMPARE(scaleMaxSpy.count(),1);
164
165     // minimum and maximum rotation properties
166     QSignalSpy rotMinSpy(pinch, SIGNAL(minimumRotationChanged()));
167     QSignalSpy rotMaxSpy(pinch, SIGNAL(maximumRotationChanged()));
168
169     QCOMPARE(pinch->minimumRotation(), 0.0);
170     QCOMPARE(pinch->maximumRotation(), 90.0);
171
172     pinch->setMinimumRotation(-90.0);
173     pinch->setMaximumRotation(45.0);
174
175     QCOMPARE(pinch->minimumRotation(), -90.0);
176     QCOMPARE(pinch->maximumRotation(), 45.0);
177
178     QCOMPARE(rotMinSpy.count(),1);
179     QCOMPARE(rotMaxSpy.count(),1);
180
181     pinch->setMinimumRotation(-90.0);
182     pinch->setMaximumRotation(45.0);
183
184     QCOMPARE(rotMinSpy.count(),1);
185     QCOMPARE(rotMaxSpy.count(),1);
186
187     delete canvas;
188 }
189
190 QTouchEvent::TouchPoint makeTouchPoint(int id, QPoint p, QGraphicsView *v, QGraphicsItem *i)
191 {
192     QTouchEvent::TouchPoint touchPoint(id);
193     touchPoint.setPos(i->mapFromScene(p));
194     touchPoint.setScreenPos(v->mapToGlobal(p));
195     touchPoint.setScenePos(p);
196     return touchPoint;
197 }
198
199 void tst_QDeclarative1PinchArea::scale()
200 {
201     QDeclarativeView *canvas = createView();
202     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
203     canvas->show();
204     canvas->setFocus();
205     QTest::qWaitForWindowShown(canvas);
206     QVERIFY(canvas->rootObject() != 0);
207     qApp->processEvents();
208
209     QDeclarative1PinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarative1PinchArea*>("pincharea");
210     QDeclarative1Pinch *pinch = pinchArea->pinch();
211     QVERIFY(pinchArea != 0);
212     QVERIFY(pinch != 0);
213
214     QDeclarativeItem *root = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
215     QVERIFY(root != 0);
216
217     // target
218     QDeclarativeItem *blackRect = canvas->rootObject()->findChild<QDeclarativeItem*>("blackrect");
219     QVERIFY(blackRect != 0);
220
221     QWidget *vp = canvas->viewport();
222
223     QPoint p1(80, 80);
224     QPoint p2(100, 100);
225
226     QTest::touchEvent(vp, device).press(0, p1, canvas);
227     QTest::touchEvent(vp, device).stationary(0).press(1, p2, canvas);
228     p1 -= QPoint(10,10);
229     p2 += QPoint(10,10);
230     QTest::touchEvent(vp, device).move(0, p1, canvas).move(1, p2, canvas);
231
232     QCOMPARE(root->property("scale").toReal(), 1.0);
233
234     p1 -= QPoint(10,10);
235     p2 += QPoint(10,10);
236     QTest::touchEvent(vp, device).move(0, p1, canvas).move(1, p2, canvas);
237
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);
241
242     // scale beyond bound
243     p1 -= QPoint(50,50);
244     p2 += QPoint(50,50);
245     QTest::touchEvent(vp, device).move(0, p1, canvas).move(1, p2, canvas);
246
247     QCOMPARE(blackRect->scale(), 2.0);
248
249     QTest::touchEvent(vp, device).release(0, p1, canvas).release(1, p2, canvas);
250
251     delete canvas;
252 }
253
254 void tst_QDeclarative1PinchArea::pan()
255 {
256     QDeclarativeView *canvas = createView();
257     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
258     canvas->show();
259     canvas->setFocus();
260     QTest::qWaitForWindowShown(canvas);
261     QVERIFY(canvas->rootObject() != 0);
262     qApp->processEvents();
263
264     QDeclarative1PinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarative1PinchArea*>("pincharea");
265     QDeclarative1Pinch *pinch = pinchArea->pinch();
266     QVERIFY(pinchArea != 0);
267     QVERIFY(pinch != 0);
268
269     QDeclarativeItem *root = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
270     QVERIFY(root != 0);
271
272     // target
273     QDeclarativeItem *blackRect = canvas->rootObject()->findChild<QDeclarativeItem*>("blackrect");
274     QVERIFY(blackRect != 0);
275
276     QWidget *vp = canvas->viewport();
277
278     QPoint p1(80, 80);
279     QPoint p2(100, 100);
280
281     QTest::touchEvent(vp, device).press(0, p1, canvas);
282     QTest::touchEvent(vp, device).stationary(0).press(1, p2, canvas);
283     p1 += QPoint(10,10);
284     p2 += QPoint(10,10);
285     QTest::touchEvent(vp, device).move(0, p1, canvas).move(1, p2, canvas);
286
287     QCOMPARE(root->property("scale").toReal(), 1.0);
288
289     p1 += QPoint(10,10);
290     p2 += QPoint(10,10);
291     QTest::touchEvent(vp, device).move(0, p1, canvas).move(1, p2, canvas);
292
293     QCOMPARE(root->property("center").toPointF(), QPointF(60, 60)); // blackrect is at 50,50
294
295     QCOMPARE(blackRect->x(), 60.0);
296     QCOMPARE(blackRect->y(), 60.0);
297
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);
302
303     QCOMPARE(blackRect->x(), 140.0);
304     QCOMPARE(blackRect->y(), 160.0);
305
306     QTest::touchEvent(vp, device).release(0, p1, canvas).release(1, p2, canvas);
307
308     delete canvas;
309 }
310
311 void tst_QDeclarative1PinchArea::flickable()
312 {
313     QDeclarativeView *canvas = createView();
314     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/flickresize.qml"));
315     canvas->show();
316     canvas->setFocus();
317     QTest::qWaitForWindowShown(canvas);
318     QVERIFY(canvas->rootObject() != 0);
319     qApp->processEvents();
320
321     QDeclarative1PinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarative1PinchArea*>("pincharea");
322     QDeclarative1Pinch *pinch = pinchArea->pinch();
323     QVERIFY(pinchArea != 0);
324     QVERIFY(pinch != 0);
325
326     QDeclarative1Flickable *root = qobject_cast<QDeclarative1Flickable*>(canvas->rootObject());
327     QVERIFY(root != 0);
328
329     QWidget *vp = canvas->viewport();
330
331     QPoint p1(110, 80);
332     QPoint p2(100, 100);
333
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);
337     {
338         p1 -= QPoint(10,10);
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);
342     }
343     {
344         p1 -= QPoint(10,10);
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);
348     }
349     {
350         p1 -= QPoint(10,10);
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);
354     }
355
356     // Flickable has reacted to the gesture
357     QVERIFY(root->isMoving());
358     QVERIFY(root->property("scale").toReal() == 1.0);
359
360     // add another touch point and continue moving
361     QTest::touchEvent(vp, device).stationary(0).press(1, p2, canvas);
362     p1 -= QPoint(10,10);
363     p2 += QPoint(10,10);
364     QTest::touchEvent(vp, device).move(0, p1, canvas).move(1, p2, canvas);
365
366     QCOMPARE(root->property("scale").toReal(), 1.0);
367
368     p1 -= QPoint(10,10);
369     p2 += QPoint(10,10);
370     QTest::touchEvent(vp, device).move(0, p1, canvas).move(1, p2, canvas);
371
372     // PinchArea has stolen the gesture.
373     QVERIFY(!root->isMoving());
374     QVERIFY(root->property("scale").toReal() > 1.0);
375
376     QTest::mouseRelease(vp, Qt::LeftButton, 0, canvas->mapFromScene(p1));
377     QTest::touchEvent(vp, device).release(0, p1, canvas).release(1, p2, canvas);
378
379     delete canvas;
380 }
381
382 QDeclarativeView *tst_QDeclarative1PinchArea::createView()
383 {
384     QDeclarativeView *canvas = new QDeclarativeView(0);
385     canvas->viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
386     canvas->setFixedSize(240,320);
387
388     return canvas;
389 }
390
391 QTEST_MAIN(tst_QDeclarative1PinchArea)
392
393 #include "tst_qdeclarativepincharea.moc"