1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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>
49 #include "../../../shared/util.h"
50 #include <QtOpenGL/QGLShaderProgram>
53 // In Symbian OS test data is located in applications private dir
57 class tst_qsgflickable : public QObject
65 void cleanupTestCase();
68 void horizontalViewportSize();
69 void verticalViewportSize();
71 void boundsBehavior();
72 void maximumFlickVelocity();
73 void flickDeceleration();
75 void disabledContent();
76 void nestedPressDelay();
77 void flickableDirection();
79 void returnToBounds();
83 QDeclarativeEngine engine;
86 T *findItem(QSGItem *parent, const QString &objectName);
89 tst_qsgflickable::tst_qsgflickable()
93 void tst_qsgflickable::initTestCase()
96 if (!QGLShaderProgram::hasOpenGLShaderPrograms(canvas.context()))
97 QSKIP("Flickable item needs OpenGL 2.0", SkipAll);
100 void tst_qsgflickable::cleanupTestCase()
105 void tst_qsgflickable::create()
107 QDeclarativeEngine engine;
108 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/flickable01.qml"));
109 QSGFlickable *obj = qobject_cast<QSGFlickable*>(c.create());
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.);
119 QCOMPARE(obj->horizontalVelocity(), 0.);
120 QCOMPARE(obj->verticalVelocity(), 0.);
122 QCOMPARE(obj->isInteractive(), true);
123 QCOMPARE(obj->boundsBehavior(), QSGFlickable::DragAndOvershootBounds);
124 QCOMPARE(obj->pressDelay(), 0);
125 QCOMPARE(obj->maximumFlickVelocity(), 2500.);
130 void tst_qsgflickable::horizontalViewportSize()
132 QDeclarativeEngine engine;
133 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/flickable02.qml"));
134 QSGFlickable *obj = qobject_cast<QSGFlickable*>(c.create());
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);
147 void tst_qsgflickable::verticalViewportSize()
149 QDeclarativeEngine engine;
150 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/flickable03.qml"));
151 QSGFlickable *obj = qobject_cast<QSGFlickable*>(c.create());
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);
164 void tst_qsgflickable::properties()
166 QDeclarativeEngine engine;
167 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/flickable04.qml"));
168 QSGFlickable *obj = qobject_cast<QSGFlickable*>(c.create());
171 QCOMPARE(obj->isInteractive(), false);
172 QCOMPARE(obj->boundsBehavior(), QSGFlickable::StopAtBounds);
173 QCOMPARE(obj->pressDelay(), 200);
174 QCOMPARE(obj->maximumFlickVelocity(), 2000.);
176 QVERIFY(obj->property("ok").toBool() == false);
177 QMetaObject::invokeMethod(obj, "check");
178 QVERIFY(obj->property("ok").toBool() == true);
183 void tst_qsgflickable::boundsBehavior()
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()));
191 QVERIFY(flickable->boundsBehavior() == QSGFlickable::StopAtBounds);
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);
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);
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);
212 void tst_qsgflickable::maximumFlickVelocity()
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()));
220 QCOMPARE(flickable->maximumFlickVelocity(), 1.0);
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);
229 void tst_qsgflickable::flickDeceleration()
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()));
237 QCOMPARE(flickable->flickDeceleration(), 1.0);
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);
246 void tst_qsgflickable::pressDelay()
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()));
254 QCOMPARE(flickable->pressDelay(), 100);
256 flickable->setPressDelay(200);
257 QCOMPARE(flickable->pressDelay(), 200);
258 QCOMPARE(spy.count(),1);
259 flickable->setPressDelay(200);
260 QCOMPARE(spy.count(),1);
264 void tst_qsgflickable::disabledContent()
266 QSGView *canvas = new QSGView;
267 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/disabledcontent.qml"));
270 QVERIFY(canvas->rootObject() != 0);
272 QSGFlickable *flickable = qobject_cast<QSGFlickable*>(canvas->rootObject());
273 QVERIFY(flickable != 0);
275 QVERIFY(flickable->contentX() == 0);
276 QVERIFY(flickable->contentY() == 0);
278 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(50, 50));
280 QMouseEvent mv(QEvent::MouseMove, QPoint(70,70), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
281 QApplication::sendEvent(canvas, &mv);
284 QMouseEvent mv(QEvent::MouseMove, QPoint(90,90), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
285 QApplication::sendEvent(canvas, &mv);
288 QMouseEvent mv(QEvent::MouseMove, QPoint(100,100), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
289 QApplication::sendEvent(canvas, &mv);
292 QVERIFY(flickable->contentX() < 0);
293 QVERIFY(flickable->contentY() < 0);
295 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(90, 90));
302 void tst_qsgflickable::nestedPressDelay()
304 QSGView *canvas = new QSGView;
305 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/nestedPressDelay.qml"));
308 QVERIFY(canvas->rootObject() != 0);
310 QSGFlickable *outer = qobject_cast<QSGFlickable*>(canvas->rootObject());
313 QSGFlickable *inner = canvas->rootObject()->findChild<QSGFlickable*>("innerFlickable");
316 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(150, 150));
317 // the MouseArea is not pressed immediately
318 QVERIFY(outer->property("pressed").toBool() == false);
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);
324 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(150, 150));
329 void tst_qsgflickable::flickableDirection()
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()));
337 QCOMPARE(flickable->flickableDirection(), QSGFlickable::VerticalFlick);
339 flickable->setFlickableDirection(QSGFlickable::HorizontalAndVerticalFlick);
340 QCOMPARE(flickable->flickableDirection(), QSGFlickable::HorizontalAndVerticalFlick);
341 QCOMPARE(spy.count(),1);
343 flickable->setFlickableDirection(QSGFlickable::AutoFlickDirection);
344 QCOMPARE(flickable->flickableDirection(), QSGFlickable::AutoFlickDirection);
345 QCOMPARE(spy.count(),2);
347 flickable->setFlickableDirection(QSGFlickable::HorizontalFlick);
348 QCOMPARE(flickable->flickableDirection(), QSGFlickable::HorizontalFlick);
349 QCOMPARE(spy.count(),3);
351 flickable->setFlickableDirection(QSGFlickable::HorizontalFlick);
352 QCOMPARE(flickable->flickableDirection(), QSGFlickable::HorizontalFlick);
353 QCOMPARE(spy.count(),3);
357 void tst_qsgflickable::resizeContent()
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");
365 QCOMPARE(obj->contentX(), 0.);
366 QCOMPARE(obj->contentY(), 0.);
367 QCOMPARE(obj->contentWidth(), 300.);
368 QCOMPARE(obj->contentHeight(), 300.);
370 QMetaObject::invokeMethod(root, "resizeContent");
372 QCOMPARE(obj->contentX(), 100.);
373 QCOMPARE(obj->contentY(), 100.);
374 QCOMPARE(obj->contentWidth(), 600.);
375 QCOMPARE(obj->contentHeight(), 600.);
381 void tst_qsgflickable::returnToBounds()
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");
389 QCOMPARE(obj->contentX(), 0.);
390 QCOMPARE(obj->contentY(), 0.);
391 QCOMPARE(obj->contentWidth(), 300.);
392 QCOMPARE(obj->contentHeight(), 300.);
394 obj->setContentX(100);
395 obj->setContentY(400);
396 QTRY_COMPARE(obj->contentX(), 100.);
397 QTRY_COMPARE(obj->contentY(), 400.);
399 QMetaObject::invokeMethod(root, "returnToBounds");
401 QTRY_COMPARE(obj->contentX(), 0.);
402 QTRY_COMPARE(obj->contentY(), 0.);
407 void tst_qsgflickable::wheel()
409 QSGView *canvas = new QSGView;
410 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/wheel.qml"));
413 QVERIFY(canvas->rootObject() != 0);
415 QSGFlickable *flick = canvas->rootObject()->findChild<QSGFlickable*>("flick");
419 QWheelEvent event(QPoint(200, 200), -120, Qt::NoButton, Qt::NoModifier, Qt::Vertical);
420 event.setAccepted(false);
421 QApplication::sendEvent(canvas, &event);
424 QTRY_VERIFY(flick->contentY() > 0);
425 QVERIFY(flick->contentX() == 0);
427 flick->setContentY(0);
428 QVERIFY(flick->contentY() == 0);
431 QWheelEvent event(QPoint(200, 200), -120, Qt::NoButton, Qt::NoModifier, Qt::Horizontal);
432 event.setAccepted(false);
433 QApplication::sendEvent(canvas, &event);
436 QTRY_VERIFY(flick->contentX() > 0);
437 QVERIFY(flick->contentY() == 0);
444 T *tst_qsgflickable::findItem(QSGItem *parent, const QString &objectName)
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));
452 //qDebug() << "try" << item;
453 if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) {
454 return static_cast<T*>(item);
456 item = findItem<T>(item, objectName);
458 return static_cast<T*>(item);
464 QTEST_MAIN(tst_qsgflickable)
466 #include "tst_qsgflickable.moc"