Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativepincharea / tst_qdeclarativepincharea.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 ** 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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
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>
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_QDeclarativePinchArea: public QObject
56 {
57     Q_OBJECT
58 private slots:
59     void pinchProperties();
60     void scale();
61     void pan();
62     void flickable();
63
64 private:
65     QDeclarativeView *createView();
66 };
67
68 void tst_QDeclarativePinchArea::pinchProperties()
69 {
70     QDeclarativeView *canvas = createView();
71     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
72     canvas->show();
73     canvas->setFocus();
74     QVERIFY(canvas->rootObject() != 0);
75
76     QDeclarativePinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarativePinchArea*>("pincharea");
77     QDeclarativePinch *pinch = pinchArea->pinch();
78     QVERIFY(pinchArea != 0);
79     QVERIFY(pinch != 0);
80
81     // target
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);
92
93     // axis
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);
101
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()));
107
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());
112
113     pinch->setXmin(10);
114     pinch->setXmax(10);
115     pinch->setYmin(10);
116     pinch->setYmax(10);
117
118     QCOMPARE(pinch->xmin(), 10.0);
119     QCOMPARE(pinch->xmax(), 10.0);
120     QCOMPARE(pinch->ymin(), 10.0);
121     QCOMPARE(pinch->ymax(), 10.0);
122
123     QCOMPARE(xminSpy.count(),1);
124     QCOMPARE(xmaxSpy.count(),1);
125     QCOMPARE(yminSpy.count(),1);
126     QCOMPARE(ymaxSpy.count(),1);
127
128     pinch->setXmin(10);
129     pinch->setXmax(10);
130     pinch->setYmin(10);
131     pinch->setYmax(10);
132
133     QCOMPARE(xminSpy.count(),1);
134     QCOMPARE(xmaxSpy.count(),1);
135     QCOMPARE(yminSpy.count(),1);
136     QCOMPARE(ymaxSpy.count(),1);
137
138     // minimum and maximum scale properties
139     QSignalSpy scaleMinSpy(pinch, SIGNAL(minimumScaleChanged()));
140     QSignalSpy scaleMaxSpy(pinch, SIGNAL(maximumScaleChanged()));
141
142     QCOMPARE(pinch->minimumScale(), 1.0);
143     QCOMPARE(pinch->maximumScale(), 2.0);
144
145     pinch->setMinimumScale(0.5);
146     pinch->setMaximumScale(1.5);
147
148     QCOMPARE(pinch->minimumScale(), 0.5);
149     QCOMPARE(pinch->maximumScale(), 1.5);
150
151     QCOMPARE(scaleMinSpy.count(),1);
152     QCOMPARE(scaleMaxSpy.count(),1);
153
154     pinch->setMinimumScale(0.5);
155     pinch->setMaximumScale(1.5);
156
157     QCOMPARE(scaleMinSpy.count(),1);
158     QCOMPARE(scaleMaxSpy.count(),1);
159
160     // minimum and maximum rotation properties
161     QSignalSpy rotMinSpy(pinch, SIGNAL(minimumRotationChanged()));
162     QSignalSpy rotMaxSpy(pinch, SIGNAL(maximumRotationChanged()));
163
164     QCOMPARE(pinch->minimumRotation(), 0.0);
165     QCOMPARE(pinch->maximumRotation(), 90.0);
166
167     pinch->setMinimumRotation(-90.0);
168     pinch->setMaximumRotation(45.0);
169
170     QCOMPARE(pinch->minimumRotation(), -90.0);
171     QCOMPARE(pinch->maximumRotation(), 45.0);
172
173     QCOMPARE(rotMinSpy.count(),1);
174     QCOMPARE(rotMaxSpy.count(),1);
175
176     pinch->setMinimumRotation(-90.0);
177     pinch->setMaximumRotation(45.0);
178
179     QCOMPARE(rotMinSpy.count(),1);
180     QCOMPARE(rotMaxSpy.count(),1);
181
182     delete canvas;
183 }
184
185 QTouchEvent::TouchPoint makeTouchPoint(int id, QPoint p, QGraphicsView *v, QGraphicsItem *i)
186 {
187     QTouchEvent::TouchPoint touchPoint(id);
188     touchPoint.setPos(i->mapFromScene(p));
189     touchPoint.setScreenPos(v->mapToGlobal(p));
190     touchPoint.setScenePos(p);
191     return touchPoint;
192 }
193
194 void tst_QDeclarativePinchArea::scale()
195 {
196     QDeclarativeView *canvas = createView();
197     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
198     canvas->show();
199     canvas->setFocus();
200     QTest::qWaitForWindowShown(canvas);
201     QVERIFY(canvas->rootObject() != 0);
202     qApp->processEvents();
203
204     QDeclarativePinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarativePinchArea*>("pincharea");
205     QDeclarativePinch *pinch = pinchArea->pinch();
206     QVERIFY(pinchArea != 0);
207     QVERIFY(pinch != 0);
208
209     QDeclarativeItem *root = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
210     QVERIFY(root != 0);
211
212     // target
213     QDeclarativeItem *blackRect = canvas->rootObject()->findChild<QDeclarativeItem*>("blackrect");
214     QVERIFY(blackRect != 0);
215
216     QWidget *vp = canvas->viewport();
217
218     QPoint p1(80, 80);
219     QPoint p2(100, 100);
220
221     QTest::touchEvent(vp).press(0, p1);
222     QTest::touchEvent(vp).stationary(0).press(1, p2);
223     p1 -= QPoint(10,10);
224     p2 += QPoint(10,10);
225     QTest::touchEvent(vp).move(0, p1).move(1, p2);
226
227     QCOMPARE(root->property("scale").toReal(), 1.0);
228
229     p1 -= QPoint(10,10);
230     p2 += QPoint(10,10);
231     QTest::touchEvent(vp).move(0, p1).move(1, p2);
232
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);
236
237     // scale beyond bound
238     p1 -= QPoint(50,50);
239     p2 += QPoint(50,50);
240     QTest::touchEvent(vp).move(0, p1).move(1, p2);
241
242     QCOMPARE(blackRect->scale(), 2.0);
243
244     QTest::touchEvent(vp).release(0, p1).release(1, p2);
245
246     delete canvas;
247 }
248
249 void tst_QDeclarativePinchArea::pan()
250 {
251     QDeclarativeView *canvas = createView();
252     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/pinchproperties.qml"));
253     canvas->show();
254     canvas->setFocus();
255     QTest::qWaitForWindowShown(canvas);
256     QVERIFY(canvas->rootObject() != 0);
257     qApp->processEvents();
258
259     QDeclarativePinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarativePinchArea*>("pincharea");
260     QDeclarativePinch *pinch = pinchArea->pinch();
261     QVERIFY(pinchArea != 0);
262     QVERIFY(pinch != 0);
263
264     QDeclarativeItem *root = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
265     QVERIFY(root != 0);
266
267     // target
268     QDeclarativeItem *blackRect = canvas->rootObject()->findChild<QDeclarativeItem*>("blackrect");
269     QVERIFY(blackRect != 0);
270
271     QWidget *vp = canvas->viewport();
272
273     QPoint p1(80, 80);
274     QPoint p2(100, 100);
275
276     QTest::touchEvent(vp).press(0, p1);
277     QTest::touchEvent(vp).stationary(0).press(1, p2);
278     p1 += QPoint(10,10);
279     p2 += QPoint(10,10);
280     QTest::touchEvent(vp).move(0, p1).move(1, p2);
281
282     QCOMPARE(root->property("scale").toReal(), 1.0);
283
284     p1 += QPoint(10,10);
285     p2 += QPoint(10,10);
286     QTest::touchEvent(vp).move(0, p1).move(1, p2);
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(vp).move(0, p1).move(1, p2);
297
298     QCOMPARE(blackRect->x(), 140.0);
299     QCOMPARE(blackRect->y(), 160.0);
300
301     QTest::touchEvent(vp).release(0, p1).release(1, p2);
302
303     delete canvas;
304 }
305
306 void tst_QDeclarativePinchArea::flickable()
307 {
308     QDeclarativeView *canvas = createView();
309     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/flickresize.qml"));
310     canvas->show();
311     canvas->setFocus();
312     QTest::qWaitForWindowShown(canvas);
313     QVERIFY(canvas->rootObject() != 0);
314     qApp->processEvents();
315
316     QDeclarativePinchArea *pinchArea = canvas->rootObject()->findChild<QDeclarativePinchArea*>("pincharea");
317     QDeclarativePinch *pinch = pinchArea->pinch();
318     QVERIFY(pinchArea != 0);
319     QVERIFY(pinch != 0);
320
321     QDeclarativeFlickable *root = qobject_cast<QDeclarativeFlickable*>(canvas->rootObject());
322     QVERIFY(root != 0);
323
324     QWidget *vp = canvas->viewport();
325
326     QPoint p1(110, 80);
327     QPoint p2(100, 100);
328
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);
332     {
333         p1 -= QPoint(10,10);
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);
337     }
338     {
339         p1 -= QPoint(10,10);
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);
343     }
344     {
345         p1 -= QPoint(10,10);
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);
349     }
350
351     // Flickable has reacted to the gesture
352     QVERIFY(root->isMoving());
353     QVERIFY(root->property("scale").toReal() == 1.0);
354
355     // add another touch point and continue moving
356     QTest::touchEvent(vp).stationary(0).press(1, p2);
357     p1 -= QPoint(10,10);
358     p2 += QPoint(10,10);
359     QTest::touchEvent(vp).move(0, p1).move(1, p2);
360
361     QCOMPARE(root->property("scale").toReal(), 1.0);
362
363     p1 -= QPoint(10,10);
364     p2 += QPoint(10,10);
365     QTest::touchEvent(vp).move(0, p1).move(1, p2);
366
367     // PinchArea has stolen the gesture.
368     QVERIFY(!root->isMoving());
369     QVERIFY(root->property("scale").toReal() > 1.0);
370
371     QTest::mouseRelease(vp, Qt::LeftButton, 0, canvas->mapFromScene(p1));
372     QTest::touchEvent(vp).release(0, p1).release(1, p2);
373
374     delete canvas;
375 }
376
377 QDeclarativeView *tst_QDeclarativePinchArea::createView()
378 {
379     QDeclarativeView *canvas = new QDeclarativeView(0);
380     canvas->viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
381     canvas->setFixedSize(240,320);
382
383     return canvas;
384 }
385
386 QTEST_MAIN(tst_QDeclarativePinchArea)
387
388 #include "tst_qdeclarativepincharea.moc"