1974c389e070bd997ae3bb4ed0b31af4d6648c95
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick2 / qquickpincharea / tst_qquickpincharea.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
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"
49
50 class tst_QQuickPinchArea: public QDeclarativeDataTest
51 {
52     Q_OBJECT
53 public:
54     tst_QQuickPinchArea() : device(0) { }
55 private slots:
56     void initTestCase();
57     void cleanupTestCase();
58     void pinchProperties();
59     void scale();
60     void pan();
61     void retouch();
62
63 private:
64     QQuickView *createView();
65     QTouchDevice *device;
66 };
67 void tst_QQuickPinchArea::initTestCase()
68 {
69     QDeclarativeDataTest::initTestCase();
70     if (!device) {
71         device = new QTouchDevice;
72         device->setType(QTouchDevice::TouchScreen);
73         QWindowSystemInterface::registerTouchDevice(device);
74     }
75 }
76
77 void tst_QQuickPinchArea::cleanupTestCase()
78 {
79
80 }
81 void tst_QQuickPinchArea::pinchProperties()
82 {
83     QQuickView *canvas = createView();
84     canvas->setSource(testFileUrl("pinchproperties.qml"));
85     canvas->show();
86     canvas->requestActivateWindow();
87     QVERIFY(canvas->rootObject() != 0);
88
89     QQuickPinchArea *pinchArea = canvas->rootObject()->findChild<QQuickPinchArea*>("pincharea");
90     QQuickPinch *pinch = pinchArea->pinch();
91     QVERIFY(pinchArea != 0);
92     QVERIFY(pinch != 0);
93
94     // target
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);
105
106     // axis
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);
114
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()));
120
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());
125
126     pinch->setXmin(10);
127     pinch->setXmax(10);
128     pinch->setYmin(10);
129     pinch->setYmax(10);
130
131     QCOMPARE(pinch->xmin(), 10.0);
132     QCOMPARE(pinch->xmax(), 10.0);
133     QCOMPARE(pinch->ymin(), 10.0);
134     QCOMPARE(pinch->ymax(), 10.0);
135
136     QCOMPARE(xminSpy.count(),1);
137     QCOMPARE(xmaxSpy.count(),1);
138     QCOMPARE(yminSpy.count(),1);
139     QCOMPARE(ymaxSpy.count(),1);
140
141     pinch->setXmin(10);
142     pinch->setXmax(10);
143     pinch->setYmin(10);
144     pinch->setYmax(10);
145
146     QCOMPARE(xminSpy.count(),1);
147     QCOMPARE(xmaxSpy.count(),1);
148     QCOMPARE(yminSpy.count(),1);
149     QCOMPARE(ymaxSpy.count(),1);
150
151     // minimum and maximum scale properties
152     QSignalSpy scaleMinSpy(pinch, SIGNAL(minimumScaleChanged()));
153     QSignalSpy scaleMaxSpy(pinch, SIGNAL(maximumScaleChanged()));
154
155     QCOMPARE(pinch->minimumScale(), 1.0);
156     QCOMPARE(pinch->maximumScale(), 2.0);
157
158     pinch->setMinimumScale(0.5);
159     pinch->setMaximumScale(1.5);
160
161     QCOMPARE(pinch->minimumScale(), 0.5);
162     QCOMPARE(pinch->maximumScale(), 1.5);
163
164     QCOMPARE(scaleMinSpy.count(),1);
165     QCOMPARE(scaleMaxSpy.count(),1);
166
167     pinch->setMinimumScale(0.5);
168     pinch->setMaximumScale(1.5);
169
170     QCOMPARE(scaleMinSpy.count(),1);
171     QCOMPARE(scaleMaxSpy.count(),1);
172
173     // minimum and maximum rotation properties
174     QSignalSpy rotMinSpy(pinch, SIGNAL(minimumRotationChanged()));
175     QSignalSpy rotMaxSpy(pinch, SIGNAL(maximumRotationChanged()));
176
177     QCOMPARE(pinch->minimumRotation(), 0.0);
178     QCOMPARE(pinch->maximumRotation(), 90.0);
179
180     pinch->setMinimumRotation(-90.0);
181     pinch->setMaximumRotation(45.0);
182
183     QCOMPARE(pinch->minimumRotation(), -90.0);
184     QCOMPARE(pinch->maximumRotation(), 45.0);
185
186     QCOMPARE(rotMinSpy.count(),1);
187     QCOMPARE(rotMaxSpy.count(),1);
188
189     pinch->setMinimumRotation(-90.0);
190     pinch->setMaximumRotation(45.0);
191
192     QCOMPARE(rotMinSpy.count(),1);
193     QCOMPARE(rotMaxSpy.count(),1);
194
195     delete canvas;
196 }
197
198 QTouchEvent::TouchPoint makeTouchPoint(int id, QPoint p, QQuickView *v, QQuickItem *i)
199 {
200     QTouchEvent::TouchPoint touchPoint(id);
201     touchPoint.setPos(i->mapFromScene(p));
202     touchPoint.setScreenPos(v->mapToGlobal(p));
203     touchPoint.setScenePos(p);
204     return touchPoint;
205 }
206
207 void tst_QQuickPinchArea::scale()
208 {
209     QQuickView *canvas = createView();
210     canvas->setSource(testFileUrl("pinchproperties.qml"));
211     canvas->show();
212     canvas->requestActivateWindow();
213     QTest::qWaitForWindowShown(canvas);
214     QVERIFY(canvas->rootObject() != 0);
215     qApp->processEvents();
216
217     QQuickPinchArea *pinchArea = canvas->rootObject()->findChild<QQuickPinchArea*>("pincharea");
218     QQuickPinch *pinch = pinchArea->pinch();
219     QVERIFY(pinchArea != 0);
220     QVERIFY(pinch != 0);
221
222     QQuickItem *root = qobject_cast<QQuickItem*>(canvas->rootObject());
223     QVERIFY(root != 0);
224
225     // target
226     QQuickItem *blackRect = canvas->rootObject()->findChild<QQuickItem*>("blackrect");
227     QVERIFY(blackRect != 0);
228
229     QPoint p1(80, 80);
230     QPoint p2(100, 100);
231
232     QTest::touchEvent(canvas, device).press(0, p1, canvas);
233     QTest::touchEvent(canvas, device).stationary(0).press(1, p2, canvas);
234     p1 -= QPoint(10,10);
235     p2 += QPoint(10,10);
236     QTest::touchEvent(canvas, device).move(0, p1,canvas).move(1, p2,canvas);
237
238     QCOMPARE(root->property("scale").toReal(), 1.0);
239
240     p1 -= QPoint(10,10);
241     p2 += QPoint(10,10);
242     QTest::touchEvent(canvas, device).move(0, p1,canvas).move(1, p2,canvas);
243
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);
247
248     // scale beyond bound
249     p1 -= QPoint(50,50);
250     p2 += QPoint(50,50);
251     QTest::touchEvent(canvas, device).move(0, p1, canvas).move(1, p2, canvas);
252
253     QCOMPARE(blackRect->scale(), 2.0);
254
255     QTest::touchEvent(canvas, device).release(0, p1, canvas).release(1, p2, canvas);
256
257     delete canvas;
258 }
259
260 void tst_QQuickPinchArea::pan()
261 {
262     QQuickView *canvas = createView();
263     canvas->setSource(testFileUrl("pinchproperties.qml"));
264     canvas->show();
265     canvas->requestActivateWindow();
266     QTest::qWaitForWindowShown(canvas);
267     QVERIFY(canvas->rootObject() != 0);
268     qApp->processEvents();
269
270     QQuickPinchArea *pinchArea = canvas->rootObject()->findChild<QQuickPinchArea*>("pincharea");
271     QQuickPinch *pinch = pinchArea->pinch();
272     QVERIFY(pinchArea != 0);
273     QVERIFY(pinch != 0);
274
275     QQuickItem *root = qobject_cast<QQuickItem*>(canvas->rootObject());
276     QVERIFY(root != 0);
277
278     // target
279     QQuickItem *blackRect = canvas->rootObject()->findChild<QQuickItem*>("blackrect");
280     QVERIFY(blackRect != 0);
281
282     QPoint p1(80, 80);
283     QPoint p2(100, 100);
284
285     QTest::touchEvent(canvas, device).press(0, p1, canvas);
286     QTest::touchEvent(canvas, device).stationary(0).press(1, p2, canvas);
287     p1 += QPoint(10,10);
288     p2 += QPoint(10,10);
289     QTest::touchEvent(canvas, device).move(0, p1, canvas).move(1, p2, canvas);
290
291     QCOMPARE(root->property("scale").toReal(), 1.0);
292
293     p1 += QPoint(10,10);
294     p2 += QPoint(10,10);
295     QTest::touchEvent(canvas, device).move(0, p1, canvas).move(1, p2, canvas);
296
297     QCOMPARE(root->property("center").toPointF(), QPointF(60, 60)); // blackrect is at 50,50
298
299     QCOMPARE(blackRect->x(), 60.0);
300     QCOMPARE(blackRect->y(), 60.0);
301
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);
306
307     QCOMPARE(blackRect->x(), 140.0);
308     QCOMPARE(blackRect->y(), 160.0);
309
310     QTest::touchEvent(canvas, device).release(0, p1, canvas).release(1, p2, canvas);
311
312     delete canvas;
313 }
314
315 // test pinch, release one point, touch again to continue pinch
316 void tst_QQuickPinchArea::retouch()
317 {
318     QQuickView *canvas = createView();
319     canvas->setSource(testFileUrl("pinchproperties.qml"));
320     canvas->show();
321     canvas->requestActivateWindow();
322     QTest::qWaitForWindowShown(canvas);
323     QVERIFY(canvas->rootObject() != 0);
324     qApp->processEvents();
325
326     QQuickPinchArea *pinchArea = canvas->rootObject()->findChild<QQuickPinchArea*>("pincharea");
327     QQuickPinch *pinch = pinchArea->pinch();
328     QVERIFY(pinchArea != 0);
329     QVERIFY(pinch != 0);
330
331     QQuickItem *root = qobject_cast<QQuickItem*>(canvas->rootObject());
332     QVERIFY(root != 0);
333
334     QSignalSpy startedSpy(pinchArea, SIGNAL(pinchStarted(QQuickPinchEvent *)));
335     QSignalSpy finishedSpy(pinchArea, SIGNAL(pinchFinished(QQuickPinchEvent *)));
336
337     // target
338     QQuickItem *blackRect = canvas->rootObject()->findChild<QQuickItem*>("blackrect");
339     QVERIFY(blackRect != 0);
340
341     QPoint p1(80, 80);
342     QPoint p2(100, 100);
343
344     QTest::touchEvent(canvas, device).press(0, p1, canvas);
345     QTest::touchEvent(canvas, device).stationary(0).press(1, p2, canvas);
346     p1 -= QPoint(10,10);
347     p2 += QPoint(10,10);
348     QTest::touchEvent(canvas, device).move(0, p1, canvas).move(1, p2, canvas);
349
350     QCOMPARE(root->property("scale").toReal(), 1.0);
351
352     p1 -= QPoint(10,10);
353     p2 += QPoint(10,10);
354     QTest::touchEvent(canvas, device).move(0, p1, canvas).move(1, p2, canvas);
355
356     QCOMPARE(startedSpy.count(), 1);
357
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);
361
362     QCOMPARE(canvas->rootObject()->property("pointCount").toInt(), 2);
363
364     QCOMPARE(startedSpy.count(), 1);
365     QCOMPARE(finishedSpy.count(), 0);
366
367     QTest::touchEvent(canvas, device).stationary(0).release(1, p2, canvas);
368
369     QCOMPARE(startedSpy.count(), 1);
370     QCOMPARE(finishedSpy.count(), 0);
371
372     QCOMPARE(canvas->rootObject()->property("pointCount").toInt(), 1);
373
374     QTest::touchEvent(canvas, device).stationary(0).press(1, p2, canvas);
375     p1 -= QPoint(10,10);
376     p2 += QPoint(10,10);
377     QTest::touchEvent(canvas, device).move(0, p1, canvas).move(1, p2, canvas);
378
379     // Lifting and retouching results in onPinchStarted being called again
380     QCOMPARE(startedSpy.count(), 2);
381     QCOMPARE(finishedSpy.count(), 0);
382
383     QCOMPARE(canvas->rootObject()->property("pointCount").toInt(), 2);
384
385     QTest::touchEvent(canvas, device).release(0, p1, canvas).release(1, p2, canvas);
386
387     QCOMPARE(startedSpy.count(), 2);
388     QCOMPARE(finishedSpy.count(), 1);
389
390     delete canvas;
391 }
392
393
394 QQuickView *tst_QQuickPinchArea::createView()
395 {
396     QQuickView *canvas = new QQuickView(0);
397     canvas->setGeometry(0,0,240,320);
398
399     return canvas;
400 }
401
402 QTEST_MAIN(tst_QQuickPinchArea)
403
404 #include "tst_qquickpincharea.moc"