d71c999ba9782858631cb5b5716cb177a575c362
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qsgpincharea / tst_qsgpincharea.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/qsgpincharea_p.h>
45 #include <private/qsgrectangle_p.h>
46 #include <QtDeclarative/qsgview.h>
47 #include <QtDeclarative/qdeclarativecontext.h>
48 #include <QtOpenGL/QGLShaderProgram>
49
50 #ifdef Q_OS_SYMBIAN
51 // In Symbian OS test data is located in applications private dir
52 #define SRCDIR "."
53 #endif
54
55 class tst_QSGPinchArea: public QObject
56 {
57     Q_OBJECT
58 private slots:
59     void initTestCase();
60     void cleanupTestCase();
61     void pinchProperties();
62     void scale();
63     void pan();
64     void retouch();
65
66 private:
67     QSGView *createView();
68 };
69 void tst_QSGPinchArea::initTestCase()
70 {
71 }
72
73 void tst_QSGPinchArea::cleanupTestCase()
74 {
75
76 }
77 void tst_QSGPinchArea::pinchProperties()
78 {
79     QSGView *canvas = createView();
80     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
81     canvas->show();
82     canvas->requestActivateWindow();
83     QVERIFY(canvas->rootObject() != 0);
84
85     QSGPinchArea *pinchArea = canvas->rootObject()->findChild<QSGPinchArea*>("pincharea");
86     QSGPinch *pinch = pinchArea->pinch();
87     QVERIFY(pinchArea != 0);
88     QVERIFY(pinch != 0);
89
90     // target
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);
101
102     // axis
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);
110
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()));
116
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());
121
122     pinch->setXmin(10);
123     pinch->setXmax(10);
124     pinch->setYmin(10);
125     pinch->setYmax(10);
126
127     QCOMPARE(pinch->xmin(), 10.0);
128     QCOMPARE(pinch->xmax(), 10.0);
129     QCOMPARE(pinch->ymin(), 10.0);
130     QCOMPARE(pinch->ymax(), 10.0);
131
132     QCOMPARE(xminSpy.count(),1);
133     QCOMPARE(xmaxSpy.count(),1);
134     QCOMPARE(yminSpy.count(),1);
135     QCOMPARE(ymaxSpy.count(),1);
136
137     pinch->setXmin(10);
138     pinch->setXmax(10);
139     pinch->setYmin(10);
140     pinch->setYmax(10);
141
142     QCOMPARE(xminSpy.count(),1);
143     QCOMPARE(xmaxSpy.count(),1);
144     QCOMPARE(yminSpy.count(),1);
145     QCOMPARE(ymaxSpy.count(),1);
146
147     // minimum and maximum scale properties
148     QSignalSpy scaleMinSpy(pinch, SIGNAL(minimumScaleChanged()));
149     QSignalSpy scaleMaxSpy(pinch, SIGNAL(maximumScaleChanged()));
150
151     QCOMPARE(pinch->minimumScale(), 1.0);
152     QCOMPARE(pinch->maximumScale(), 2.0);
153
154     pinch->setMinimumScale(0.5);
155     pinch->setMaximumScale(1.5);
156
157     QCOMPARE(pinch->minimumScale(), 0.5);
158     QCOMPARE(pinch->maximumScale(), 1.5);
159
160     QCOMPARE(scaleMinSpy.count(),1);
161     QCOMPARE(scaleMaxSpy.count(),1);
162
163     pinch->setMinimumScale(0.5);
164     pinch->setMaximumScale(1.5);
165
166     QCOMPARE(scaleMinSpy.count(),1);
167     QCOMPARE(scaleMaxSpy.count(),1);
168
169     // minimum and maximum rotation properties
170     QSignalSpy rotMinSpy(pinch, SIGNAL(minimumRotationChanged()));
171     QSignalSpy rotMaxSpy(pinch, SIGNAL(maximumRotationChanged()));
172
173     QCOMPARE(pinch->minimumRotation(), 0.0);
174     QCOMPARE(pinch->maximumRotation(), 90.0);
175
176     pinch->setMinimumRotation(-90.0);
177     pinch->setMaximumRotation(45.0);
178
179     QCOMPARE(pinch->minimumRotation(), -90.0);
180     QCOMPARE(pinch->maximumRotation(), 45.0);
181
182     QCOMPARE(rotMinSpy.count(),1);
183     QCOMPARE(rotMaxSpy.count(),1);
184
185     pinch->setMinimumRotation(-90.0);
186     pinch->setMaximumRotation(45.0);
187
188     QCOMPARE(rotMinSpy.count(),1);
189     QCOMPARE(rotMaxSpy.count(),1);
190
191     delete canvas;
192 }
193
194 QTouchEvent::TouchPoint makeTouchPoint(int id, QPoint p, QSGView *v, QSGItem *i)
195 {
196     QTouchEvent::TouchPoint touchPoint(id);
197     touchPoint.setPos(i->mapFromScene(p));
198     touchPoint.setScreenPos(v->mapToGlobal(p));
199     touchPoint.setScenePos(p);
200     return touchPoint;
201 }
202
203 void tst_QSGPinchArea::scale()
204 {
205     QSGView *canvas = createView();
206     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
207     canvas->show();
208     canvas->requestActivateWindow();
209     QTest::qWaitForWindowShown(canvas);
210     QVERIFY(canvas->rootObject() != 0);
211     qApp->processEvents();
212
213     QSGPinchArea *pinchArea = canvas->rootObject()->findChild<QSGPinchArea*>("pincharea");
214     QSGPinch *pinch = pinchArea->pinch();
215     QVERIFY(pinchArea != 0);
216     QVERIFY(pinch != 0);
217
218     QSGItem *root = qobject_cast<QSGItem*>(canvas->rootObject());
219     QVERIFY(root != 0);
220
221     // target
222     QSGItem *blackRect = canvas->rootObject()->findChild<QSGItem*>("blackrect");
223     QVERIFY(blackRect != 0);
224
225     QPoint p1(80, 80);
226     QPoint p2(100, 100);
227
228     QTest::touchEvent(canvas).press(0, p1, canvas);
229     QTest::touchEvent(canvas).stationary(0).press(1, p2, canvas);
230     p1 -= QPoint(10,10);
231     p2 += QPoint(10,10);
232     QTest::touchEvent(canvas).move(0, p1,canvas).move(1, p2,canvas);
233
234     QCOMPARE(root->property("scale").toReal(), 1.0);
235
236     p1 -= QPoint(10,10);
237     p2 += QPoint(10,10);
238     QTest::touchEvent(canvas).move(0, p1,canvas).move(1, p2,canvas);
239
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);
243
244     // scale beyond bound
245     p1 -= QPoint(50,50);
246     p2 += QPoint(50,50);
247     QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
248
249     QCOMPARE(blackRect->scale(), 2.0);
250
251     QTest::touchEvent(canvas).release(0, p1, canvas).release(1, p2, canvas);
252
253     delete canvas;
254 }
255
256 void tst_QSGPinchArea::pan()
257 {
258     QSGView *canvas = createView();
259     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
260     canvas->show();
261     canvas->requestActivateWindow();
262     QTest::qWaitForWindowShown(canvas);
263     QVERIFY(canvas->rootObject() != 0);
264     qApp->processEvents();
265
266     QSGPinchArea *pinchArea = canvas->rootObject()->findChild<QSGPinchArea*>("pincharea");
267     QSGPinch *pinch = pinchArea->pinch();
268     QVERIFY(pinchArea != 0);
269     QVERIFY(pinch != 0);
270
271     QSGItem *root = qobject_cast<QSGItem*>(canvas->rootObject());
272     QVERIFY(root != 0);
273
274     // target
275     QSGItem *blackRect = canvas->rootObject()->findChild<QSGItem*>("blackrect");
276     QVERIFY(blackRect != 0);
277
278     QPoint p1(80, 80);
279     QPoint p2(100, 100);
280
281     QTest::touchEvent(canvas).press(0, p1, canvas);
282     QTest::touchEvent(canvas).stationary(0).press(1, p2, canvas);
283     p1 += QPoint(10,10);
284     p2 += QPoint(10,10);
285     QTest::touchEvent(canvas).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(canvas).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(canvas).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(canvas).release(0, p1, canvas).release(1, p2, canvas);
307
308     delete canvas;
309 }
310
311 // test pinch, release one point, touch again to continue pinch
312 void tst_QSGPinchArea::retouch()
313 {
314     QSGView *canvas = createView();
315     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
316     canvas->show();
317     canvas->requestActivateWindow();
318     QTest::qWaitForWindowShown(canvas);
319     QVERIFY(canvas->rootObject() != 0);
320     qApp->processEvents();
321
322     QSGPinchArea *pinchArea = canvas->rootObject()->findChild<QSGPinchArea*>("pincharea");
323     QSGPinch *pinch = pinchArea->pinch();
324     QVERIFY(pinchArea != 0);
325     QVERIFY(pinch != 0);
326
327     QSGItem *root = qobject_cast<QSGItem*>(canvas->rootObject());
328     QVERIFY(root != 0);
329
330     QSignalSpy startedSpy(pinchArea, SIGNAL(pinchStarted(QSGPinchEvent *)));
331     QSignalSpy finishedSpy(pinchArea, SIGNAL(pinchFinished(QSGPinchEvent *)));
332
333     // target
334     QSGItem *blackRect = canvas->rootObject()->findChild<QSGItem*>("blackrect");
335     QVERIFY(blackRect != 0);
336
337     QPoint p1(80, 80);
338     QPoint p2(100, 100);
339
340     QTest::touchEvent(canvas).press(0, p1, canvas);
341     QTest::touchEvent(canvas).stationary(0).press(1, p2, canvas);
342     p1 -= QPoint(10,10);
343     p2 += QPoint(10,10);
344     QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
345
346     QCOMPARE(root->property("scale").toReal(), 1.0);
347
348     p1 -= QPoint(10,10);
349     p2 += QPoint(10,10);
350     QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
351
352     QCOMPARE(startedSpy.count(), 1);
353
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);
357
358     QCOMPARE(canvas->rootObject()->property("pointCount").toInt(), 2);
359
360     QCOMPARE(startedSpy.count(), 1);
361     QCOMPARE(finishedSpy.count(), 0);
362
363     QTest::touchEvent(canvas).stationary(0).release(1, p2, canvas);
364
365     QCOMPARE(startedSpy.count(), 1);
366     QCOMPARE(finishedSpy.count(), 0);
367
368     QCOMPARE(canvas->rootObject()->property("pointCount").toInt(), 1);
369
370     QTest::touchEvent(canvas).stationary(0).press(1, p2, canvas);
371     p1 -= QPoint(10,10);
372     p2 += QPoint(10,10);
373     QTest::touchEvent(canvas).move(0, p1, canvas).move(1, p2, canvas);
374
375     // Lifting and retouching results in onPinchStarted being called again
376     QCOMPARE(startedSpy.count(), 2);
377     QCOMPARE(finishedSpy.count(), 0);
378
379     QCOMPARE(canvas->rootObject()->property("pointCount").toInt(), 2);
380
381     QTest::touchEvent(canvas).release(0, p1, canvas).release(1, p2, canvas);
382
383     QCOMPARE(startedSpy.count(), 2);
384     QCOMPARE(finishedSpy.count(), 1);
385
386     delete canvas;
387 }
388
389
390 QSGView *tst_QSGPinchArea::createView()
391 {
392     QSGView *canvas = new QSGView(0);
393     canvas->setGeometry(0,0,240,320);
394
395     return canvas;
396 }
397
398 QTEST_MAIN(tst_QSGPinchArea)
399
400 #include "tst_qsgpincharea.moc"