bbddbfe74921d87eab197b1bc0e97f2698d2b177
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qsgflickable / tst_qsgflickable.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 #include <qtest.h>
42 #include <QtTest/QSignalSpy>
43 #include <QtDeclarative/qdeclarativeengine.h>
44 #include <QtDeclarative/qdeclarativecomponent.h>
45 #include <QtDeclarative/qsgview.h>
46 #include <private/qsgflickable_p.h>
47 #include <private/qdeclarativevaluetype_p.h>
48 #include <math.h>
49 #include "../../../shared/util.h"
50 #include <QtOpenGL/QGLShaderProgram>
51
52 #ifdef Q_OS_SYMBIAN
53 // In Symbian OS test data is located in applications private dir
54 #define SRCDIR "."
55 #endif
56
57 class tst_qsgflickable : public QObject
58 {
59     Q_OBJECT
60 public:
61     tst_qsgflickable();
62
63 private slots:
64     void initTestCase();
65     void cleanupTestCase();
66
67     void create();
68     void horizontalViewportSize();
69     void verticalViewportSize();
70     void properties();
71     void boundsBehavior();
72     void maximumFlickVelocity();
73     void flickDeceleration();
74     void pressDelay();
75     void disabledContent();
76     void nestedPressDelay();
77     void flickableDirection();
78     void resizeContent();
79     void returnToBounds();
80     void wheel();
81
82 private:
83     QDeclarativeEngine engine;
84
85     template<typename T>
86     T *findItem(QSGItem *parent, const QString &objectName);
87 };
88
89 tst_qsgflickable::tst_qsgflickable()
90 {
91 }
92
93 void tst_qsgflickable::initTestCase()
94 {
95     QSGView canvas;
96     if (!QGLShaderProgram::hasOpenGLShaderPrograms(canvas.context()))
97         QSKIP("Flickable item needs OpenGL 2.0", SkipAll);
98 }
99
100 void tst_qsgflickable::cleanupTestCase()
101 {
102
103 }
104
105 void tst_qsgflickable::create()
106 {
107     QDeclarativeEngine engine;
108     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/flickable01.qml"));
109     QSGFlickable *obj = qobject_cast<QSGFlickable*>(c.create());
110
111     QVERIFY(obj != 0);
112     QCOMPARE(obj->isAtXBeginning(), true);
113     QCOMPARE(obj->isAtXEnd(), false);
114     QCOMPARE(obj->isAtYBeginning(), true);
115     QCOMPARE(obj->isAtYEnd(), false);
116     QCOMPARE(obj->contentX(), 0.);
117     QCOMPARE(obj->contentY(), 0.);
118
119     QCOMPARE(obj->horizontalVelocity(), 0.);
120     QCOMPARE(obj->verticalVelocity(), 0.);
121
122     QCOMPARE(obj->isInteractive(), true);
123     QCOMPARE(obj->boundsBehavior(), QSGFlickable::DragAndOvershootBounds);
124     QCOMPARE(obj->pressDelay(), 0);
125     QCOMPARE(obj->maximumFlickVelocity(), 2500.);
126
127     delete obj;
128 }
129
130 void tst_qsgflickable::horizontalViewportSize()
131 {
132     QDeclarativeEngine engine;
133     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/flickable02.qml"));
134     QSGFlickable *obj = qobject_cast<QSGFlickable*>(c.create());
135
136     QVERIFY(obj != 0);
137     QCOMPARE(obj->contentWidth(), 800.);
138     QCOMPARE(obj->contentHeight(), 300.);
139     QCOMPARE(obj->isAtXBeginning(), true);
140     QCOMPARE(obj->isAtXEnd(), false);
141     QCOMPARE(obj->isAtYBeginning(), true);
142     QCOMPARE(obj->isAtYEnd(), false);
143
144     delete obj;
145 }
146
147 void tst_qsgflickable::verticalViewportSize()
148 {
149     QDeclarativeEngine engine;
150     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/flickable03.qml"));
151     QSGFlickable *obj = qobject_cast<QSGFlickable*>(c.create());
152
153     QVERIFY(obj != 0);
154     QCOMPARE(obj->contentWidth(), 200.);
155     QCOMPARE(obj->contentHeight(), 1200.);
156     QCOMPARE(obj->isAtXBeginning(), true);
157     QCOMPARE(obj->isAtXEnd(), false);
158     QCOMPARE(obj->isAtYBeginning(), true);
159     QCOMPARE(obj->isAtYEnd(), false);
160
161     delete obj;
162 }
163
164 void tst_qsgflickable::properties()
165 {
166     QDeclarativeEngine engine;
167     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/flickable04.qml"));
168     QSGFlickable *obj = qobject_cast<QSGFlickable*>(c.create());
169
170     QVERIFY(obj != 0);
171     QCOMPARE(obj->isInteractive(), false);
172     QCOMPARE(obj->boundsBehavior(), QSGFlickable::StopAtBounds);
173     QCOMPARE(obj->pressDelay(), 200);
174     QCOMPARE(obj->maximumFlickVelocity(), 2000.);
175
176     QVERIFY(obj->property("ok").toBool() == false);
177     QMetaObject::invokeMethod(obj, "check");
178     QVERIFY(obj->property("ok").toBool() == true);
179
180     delete obj;
181 }
182
183 void tst_qsgflickable::boundsBehavior()
184 {
185     QDeclarativeComponent component(&engine);
186     component.setData("import QtQuick 2.0; Flickable { boundsBehavior: Flickable.StopAtBounds }", QUrl::fromLocalFile(""));
187     QSGFlickable *flickable = qobject_cast<QSGFlickable*>(component.create());
188     QSignalSpy spy(flickable, SIGNAL(boundsBehaviorChanged()));
189
190     QVERIFY(flickable);
191     QVERIFY(flickable->boundsBehavior() == QSGFlickable::StopAtBounds);
192
193     flickable->setBoundsBehavior(QSGFlickable::DragAndOvershootBounds);
194     QVERIFY(flickable->boundsBehavior() == QSGFlickable::DragAndOvershootBounds);
195     QCOMPARE(spy.count(),1);
196     flickable->setBoundsBehavior(QSGFlickable::DragAndOvershootBounds);
197     QCOMPARE(spy.count(),1);
198
199     flickable->setBoundsBehavior(QSGFlickable::DragOverBounds);
200     QVERIFY(flickable->boundsBehavior() == QSGFlickable::DragOverBounds);
201     QCOMPARE(spy.count(),2);
202     flickable->setBoundsBehavior(QSGFlickable::DragOverBounds);
203     QCOMPARE(spy.count(),2);
204
205     flickable->setBoundsBehavior(QSGFlickable::StopAtBounds);
206     QVERIFY(flickable->boundsBehavior() == QSGFlickable::StopAtBounds);
207     QCOMPARE(spy.count(),3);
208     flickable->setBoundsBehavior(QSGFlickable::StopAtBounds);
209     QCOMPARE(spy.count(),3);
210 }
211
212 void tst_qsgflickable::maximumFlickVelocity()
213 {
214     QDeclarativeComponent component(&engine);
215     component.setData("import QtQuick 2.0; Flickable { maximumFlickVelocity: 1.0; }", QUrl::fromLocalFile(""));
216     QSGFlickable *flickable = qobject_cast<QSGFlickable*>(component.create());
217     QSignalSpy spy(flickable, SIGNAL(maximumFlickVelocityChanged()));
218
219     QVERIFY(flickable);
220     QCOMPARE(flickable->maximumFlickVelocity(), 1.0);
221
222     flickable->setMaximumFlickVelocity(2.0);
223     QCOMPARE(flickable->maximumFlickVelocity(), 2.0);
224     QCOMPARE(spy.count(),1);
225     flickable->setMaximumFlickVelocity(2.0);
226     QCOMPARE(spy.count(),1);
227 }
228
229 void tst_qsgflickable::flickDeceleration()
230 {
231     QDeclarativeComponent component(&engine);
232     component.setData("import QtQuick 2.0; Flickable { flickDeceleration: 1.0; }", QUrl::fromLocalFile(""));
233     QSGFlickable *flickable = qobject_cast<QSGFlickable*>(component.create());
234     QSignalSpy spy(flickable, SIGNAL(flickDecelerationChanged()));
235
236     QVERIFY(flickable);
237     QCOMPARE(flickable->flickDeceleration(), 1.0);
238
239     flickable->setFlickDeceleration(2.0);
240     QCOMPARE(flickable->flickDeceleration(), 2.0);
241     QCOMPARE(spy.count(),1);
242     flickable->setFlickDeceleration(2.0);
243     QCOMPARE(spy.count(),1);
244 }
245
246 void tst_qsgflickable::pressDelay()
247 {
248     QDeclarativeComponent component(&engine);
249     component.setData("import QtQuick 2.0; Flickable { pressDelay: 100; }", QUrl::fromLocalFile(""));
250     QSGFlickable *flickable = qobject_cast<QSGFlickable*>(component.create());
251     QSignalSpy spy(flickable, SIGNAL(pressDelayChanged()));
252
253     QVERIFY(flickable);
254     QCOMPARE(flickable->pressDelay(), 100);
255
256     flickable->setPressDelay(200);
257     QCOMPARE(flickable->pressDelay(), 200);
258     QCOMPARE(spy.count(),1);
259     flickable->setPressDelay(200);
260     QCOMPARE(spy.count(),1);
261 }
262
263 // QT-4677
264 void tst_qsgflickable::disabledContent()
265 {
266     QSGView *canvas = new QSGView;
267     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/disabledcontent.qml"));
268     canvas->show();
269     canvas->setFocus();
270     QVERIFY(canvas->rootObject() != 0);
271
272     QSGFlickable *flickable = qobject_cast<QSGFlickable*>(canvas->rootObject());
273     QVERIFY(flickable != 0);
274
275     QVERIFY(flickable->contentX() == 0);
276     QVERIFY(flickable->contentY() == 0);
277
278     QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(50, 50));
279     {
280         QMouseEvent mv(QEvent::MouseMove, QPoint(70,70), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
281         QApplication::sendEvent(canvas, &mv);
282     }
283     {
284         QMouseEvent mv(QEvent::MouseMove, QPoint(90,90), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
285         QApplication::sendEvent(canvas, &mv);
286     }
287     {
288         QMouseEvent mv(QEvent::MouseMove, QPoint(100,100), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
289         QApplication::sendEvent(canvas, &mv);
290     }
291
292     QVERIFY(flickable->contentX() < 0);
293     QVERIFY(flickable->contentY() < 0);
294
295     QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(90, 90));
296
297     delete canvas;
298 }
299
300
301 // QTBUG-17361
302 void tst_qsgflickable::nestedPressDelay()
303 {
304     QSGView *canvas = new QSGView;
305     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/nestedPressDelay.qml"));
306     canvas->show();
307     canvas->setFocus();
308     QVERIFY(canvas->rootObject() != 0);
309
310     QSGFlickable *outer = qobject_cast<QSGFlickable*>(canvas->rootObject());
311     QVERIFY(outer != 0);
312
313     QSGFlickable *inner = canvas->rootObject()->findChild<QSGFlickable*>("innerFlickable");
314     QVERIFY(inner != 0);
315
316     QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(150, 150));
317     // the MouseArea is not pressed immediately
318     QVERIFY(outer->property("pressed").toBool() == false);
319
320     // The outer pressDelay will prevail (50ms, vs. 10sec)
321     // QTRY_VERIFY() has 5sec timeout, so will timeout well within 10sec.
322     QTRY_VERIFY(outer->property("pressed").toBool() == true);
323
324     QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(150, 150));
325
326     delete canvas;
327 }
328
329 void tst_qsgflickable::flickableDirection()
330 {
331     QDeclarativeComponent component(&engine);
332     component.setData("import QtQuick 2.0; Flickable { flickableDirection: Flickable.VerticalFlick; }", QUrl::fromLocalFile(""));
333     QSGFlickable *flickable = qobject_cast<QSGFlickable*>(component.create());
334     QSignalSpy spy(flickable, SIGNAL(flickableDirectionChanged()));
335
336     QVERIFY(flickable);
337     QCOMPARE(flickable->flickableDirection(), QSGFlickable::VerticalFlick);
338
339     flickable->setFlickableDirection(QSGFlickable::HorizontalAndVerticalFlick);
340     QCOMPARE(flickable->flickableDirection(), QSGFlickable::HorizontalAndVerticalFlick);
341     QCOMPARE(spy.count(),1);
342
343     flickable->setFlickableDirection(QSGFlickable::AutoFlickDirection);
344     QCOMPARE(flickable->flickableDirection(), QSGFlickable::AutoFlickDirection);
345     QCOMPARE(spy.count(),2);
346
347     flickable->setFlickableDirection(QSGFlickable::HorizontalFlick);
348     QCOMPARE(flickable->flickableDirection(), QSGFlickable::HorizontalFlick);
349     QCOMPARE(spy.count(),3);
350
351     flickable->setFlickableDirection(QSGFlickable::HorizontalFlick);
352     QCOMPARE(flickable->flickableDirection(), QSGFlickable::HorizontalFlick);
353     QCOMPARE(spy.count(),3);
354 }
355
356 // QtQuick 1.1
357 void tst_qsgflickable::resizeContent()
358 {
359     QDeclarativeEngine engine;
360     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/resize.qml"));
361     QSGItem *root = qobject_cast<QSGItem*>(c.create());
362     QSGFlickable *obj = findItem<QSGFlickable>(root, "flick");
363
364     QVERIFY(obj != 0);
365     QCOMPARE(obj->contentX(), 0.);
366     QCOMPARE(obj->contentY(), 0.);
367     QCOMPARE(obj->contentWidth(), 300.);
368     QCOMPARE(obj->contentHeight(), 300.);
369
370     QMetaObject::invokeMethod(root, "resizeContent");
371
372     QCOMPARE(obj->contentX(), 100.);
373     QCOMPARE(obj->contentY(), 100.);
374     QCOMPARE(obj->contentWidth(), 600.);
375     QCOMPARE(obj->contentHeight(), 600.);
376
377     delete root;
378 }
379
380 // QtQuick 1.1
381 void tst_qsgflickable::returnToBounds()
382 {
383     QDeclarativeEngine engine;
384     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/resize.qml"));
385     QSGItem *root = qobject_cast<QSGItem*>(c.create());
386     QSGFlickable *obj = findItem<QSGFlickable>(root, "flick");
387
388     QVERIFY(obj != 0);
389     QCOMPARE(obj->contentX(), 0.);
390     QCOMPARE(obj->contentY(), 0.);
391     QCOMPARE(obj->contentWidth(), 300.);
392     QCOMPARE(obj->contentHeight(), 300.);
393
394     obj->setContentX(100);
395     obj->setContentY(400);
396     QTRY_COMPARE(obj->contentX(), 100.);
397     QTRY_COMPARE(obj->contentY(), 400.);
398
399     QMetaObject::invokeMethod(root, "returnToBounds");
400
401     QTRY_COMPARE(obj->contentX(), 0.);
402     QTRY_COMPARE(obj->contentY(), 0.);
403
404     delete root;
405 }
406
407 void tst_qsgflickable::wheel()
408 {
409     QSGView *canvas = new QSGView;
410     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/wheel.qml"));
411     canvas->show();
412     canvas->setFocus();
413     QVERIFY(canvas->rootObject() != 0);
414
415     QSGFlickable *flick = canvas->rootObject()->findChild<QSGFlickable*>("flick");
416     QVERIFY(flick != 0);
417
418     {
419         QWheelEvent event(QPoint(200, 200), -120, Qt::NoButton, Qt::NoModifier, Qt::Vertical);
420         event.setAccepted(false);
421         QApplication::sendEvent(canvas, &event);
422     }
423
424     QTRY_VERIFY(flick->contentY() > 0);
425     QVERIFY(flick->contentX() == 0);
426
427     flick->setContentY(0);
428     QVERIFY(flick->contentY() == 0);
429
430     {
431         QWheelEvent event(QPoint(200, 200), -120, Qt::NoButton, Qt::NoModifier, Qt::Horizontal);
432         event.setAccepted(false);
433         QApplication::sendEvent(canvas, &event);
434     }
435
436     QTRY_VERIFY(flick->contentX() > 0);
437     QVERIFY(flick->contentY() == 0);
438
439     delete canvas;
440 }
441
442
443 template<typename T>
444 T *tst_qsgflickable::findItem(QSGItem *parent, const QString &objectName)
445 {
446     const QMetaObject &mo = T::staticMetaObject;
447     //qDebug() << parent->childItems().count() << "children";
448     for (int i = 0; i < parent->childItems().count(); ++i) {
449         QSGItem *item = qobject_cast<QSGItem*>(parent->childItems().at(i));
450         if(!item)
451             continue;
452         //qDebug() << "try" << item;
453         if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) {
454             return static_cast<T*>(item);
455         }
456         item = findItem<T>(item, objectName);
457         if (item)
458             return static_cast<T*>(item);
459     }
460
461     return 0;
462 }
463
464 QTEST_MAIN(tst_qsgflickable)
465
466 #include "tst_qsgflickable.moc"