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