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 ** 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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include <QtDeclarative/qdeclarativeengine.h>
43 #include <QtDeclarative/qdeclarativecomponent.h>
44 #include <QtDeclarative/qdeclarativeview.h>
45 #include <private/qdeclarativerectangle_p.h>
46 #include <private/qdeclarativeimage_p.h>
47 #include <private/qdeclarativeanimatedimage_p.h>
49 #include <QtDeclarative/qdeclarativecontext.h>
51 #include "../shared/testhttpserver.h"
52 #include "../../../shared/util.h"
55 // In Symbian OS test data is located in applications private dir
59 class tst_qdeclarativeanimatedimage : public QObject
63 tst_qdeclarativeanimatedimage() {}
71 void mirror_running();
72 void mirror_notRunning();
73 void mirror_notRunning_data();
77 void sourceSizeReadOnly();
80 void progressAndStatusChanges();
83 QPixmap grabScene(QGraphicsScene *scene, int width, int height);
86 QPixmap tst_qdeclarativeanimatedimage::grabScene(QGraphicsScene *scene, int width, int height)
88 QPixmap screenshot(width, height);
90 QPainter p_screenshot(&screenshot);
91 scene->render(&p_screenshot, QRect(0, 0, width, height), QRect(0, 0, width, height));
95 void tst_qdeclarativeanimatedimage::play()
97 QDeclarativeEngine engine;
98 QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickman.qml"));
99 QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
101 QVERIFY(anim->isPlaying());
106 void tst_qdeclarativeanimatedimage::pause()
108 QDeclarativeEngine engine;
109 QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickmanpause.qml"));
110 QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
112 QVERIFY(anim->isPlaying());
113 QVERIFY(anim->isPaused());
118 void tst_qdeclarativeanimatedimage::stopped()
120 QDeclarativeEngine engine;
121 QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickmanstopped.qml"));
122 QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
124 QVERIFY(!anim->isPlaying());
125 QCOMPARE(anim->currentFrame(), 0);
130 void tst_qdeclarativeanimatedimage::setFrame()
132 QDeclarativeEngine engine;
133 QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickmanpause.qml"));
134 QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
136 QVERIFY(anim->isPlaying());
137 QCOMPARE(anim->currentFrame(), 2);
142 void tst_qdeclarativeanimatedimage::frameCount()
144 QDeclarativeEngine engine;
145 QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/colors.qml"));
146 QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
148 QVERIFY(anim->isPlaying());
149 QCOMPARE(anim->frameCount(), 3);
154 void tst_qdeclarativeanimatedimage::mirror_running()
156 // test where mirror is set to true after animation has started
158 QDeclarativeEngine engine;
159 QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/hearts.qml"));
160 QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
163 QGraphicsScene scene;
164 int width = anim->property("width").toInt();
165 int height = anim->property("height").toInt();
166 scene.addItem(qobject_cast<QGraphicsObject *>(anim));
168 QCOMPARE(anim->currentFrame(), 0);
169 QPixmap frame0 = grabScene(&scene, width, height);
170 anim->setCurrentFrame(1);
171 QPixmap frame1 = grabScene(&scene, width, height);
173 anim->setCurrentFrame(0);
175 QSignalSpy spy(anim, SIGNAL(frameChanged()));
176 anim->setPlaying(true);
178 QTRY_VERIFY(spy.count() == 1); spy.clear();
179 anim->setProperty("mirror", true);
181 QCOMPARE(anim->currentFrame(), 1);
182 QPixmap frame1_flipped = grabScene(&scene, width, height);
184 QTRY_VERIFY(spy.count() == 1); spy.clear();
185 QCOMPARE(anim->currentFrame(), 0); // animation only has 2 frames, should cycle back to first
186 QPixmap frame0_flipped = grabScene(&scene, width, height);
188 QTransform transform;
189 transform.translate(width, 0).scale(-1, 1.0);
190 QPixmap frame0_expected = frame0.transformed(transform);
191 QPixmap frame1_expected = frame1.transformed(transform);
193 QCOMPARE(frame0_flipped, frame0_expected);
194 QCOMPARE(frame1_flipped, frame1_expected);
197 void tst_qdeclarativeanimatedimage::mirror_notRunning()
199 QFETCH(QUrl, fileUrl);
201 QDeclarativeEngine engine;
202 QDeclarativeComponent component(&engine, fileUrl);
203 QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
206 QGraphicsScene scene;
207 int width = anim->property("width").toInt();
208 int height = anim->property("height").toInt();
209 scene.addItem(qobject_cast<QGraphicsObject *>(anim));
210 QPixmap screenshot = grabScene(&scene, width, height);
212 QTransform transform;
213 transform.translate(width, 0).scale(-1, 1.0);
214 QPixmap expected = screenshot.transformed(transform);
216 int frame = anim->currentFrame();
217 bool playing = anim->isPlaying();
218 bool paused = anim->isPlaying();
220 anim->setProperty("mirror", true);
221 screenshot = grabScene(&scene, width, height);
223 QCOMPARE(screenshot, expected);
225 // mirroring should not change the current frame or playing status
226 QCOMPARE(anim->currentFrame(), frame);
227 QCOMPARE(anim->isPlaying(), playing);
228 QCOMPARE(anim->isPaused(), paused);
233 void tst_qdeclarativeanimatedimage::mirror_notRunning_data()
235 QTest::addColumn<QUrl>("fileUrl");
237 QTest::newRow("paused") << QUrl::fromLocalFile(SRCDIR "/data/stickmanpause.qml");
238 QTest::newRow("stopped") << QUrl::fromLocalFile(SRCDIR "/data/stickmanstopped.qml");
241 void tst_qdeclarativeanimatedimage::remote()
243 QFETCH(QString, fileName);
244 QFETCH(bool, paused);
246 TestHTTPServer server(14449);
247 QVERIFY(server.isValid());
248 server.serveDirectory(SRCDIR "/data");
250 QDeclarativeEngine engine;
251 QDeclarativeComponent component(&engine, QUrl("http://127.0.0.1:14449/" + fileName));
252 QTRY_VERIFY(component.isReady());
254 QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
257 QTRY_VERIFY(anim->isPlaying());
259 QTRY_VERIFY(anim->isPaused());
260 QCOMPARE(anim->currentFrame(), 2);
262 QVERIFY(anim->status() != QDeclarativeAnimatedImage::Error);
267 void tst_qdeclarativeanimatedimage::sourceSize()
269 QDeclarativeEngine engine;
270 QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickmanscaled.qml"));
271 QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
273 QCOMPARE(anim->width(),240.0);
274 QCOMPARE(anim->height(),180.0);
275 QCOMPARE(anim->sourceSize(),QSize(160,120));
280 void tst_qdeclarativeanimatedimage::sourceSizeReadOnly()
282 QDeclarativeEngine engine;
283 QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickmanerror1.qml"));
284 QVERIFY(component.isError());
285 QCOMPARE(component.errors().at(0).description(), QString("Invalid property assignment: \"sourceSize\" is a read-only property"));
288 void tst_qdeclarativeanimatedimage::remote_data()
290 QTest::addColumn<QString>("fileName");
291 QTest::addColumn<bool>("paused");
293 QTest::newRow("playing") << "stickman.qml" << false;
294 QTest::newRow("paused") << "stickmanpause.qml" << true;
297 void tst_qdeclarativeanimatedimage::invalidSource()
299 QDeclarativeEngine engine;
300 QDeclarativeComponent component(&engine);
301 component.setData("import QtQuick 1.0\n AnimatedImage { source: \"no-such-file.gif\" }", QUrl::fromLocalFile(""));
302 QVERIFY(component.isReady());
304 QTest::ignoreMessage(QtWarningMsg, "file::2:2: QML AnimatedImage: Error Reading Animated Image File file:no-such-file.gif");
306 QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
309 QVERIFY(!anim->isPlaying());
310 QVERIFY(!anim->isPaused());
311 QCOMPARE(anim->currentFrame(), 0);
312 QCOMPARE(anim->frameCount(), 0);
313 QTRY_VERIFY(anim->status() == 3);
316 void tst_qdeclarativeanimatedimage::qtbug_16520()
318 TestHTTPServer server(14449);
319 QVERIFY(server.isValid());
320 server.serveDirectory(SRCDIR "/data");
322 QDeclarativeEngine engine;
323 QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/qtbug-16520.qml"));
324 QTRY_VERIFY(component.isReady());
326 QDeclarativeRectangle *root = qobject_cast<QDeclarativeRectangle *>(component.create());
328 QDeclarativeAnimatedImage *anim = root->findChild<QDeclarativeAnimatedImage*>("anim");
330 anim->setProperty("source", "http://127.0.0.1:14449/stickman.gif");
332 QTRY_VERIFY(anim->opacity() == 0);
333 QTRY_VERIFY(anim->opacity() == 1);
338 void tst_qdeclarativeanimatedimage::progressAndStatusChanges()
340 TestHTTPServer server(14449);
341 QVERIFY(server.isValid());
342 server.serveDirectory(SRCDIR "/data");
344 QDeclarativeEngine engine;
345 QString componentStr = "import QtQuick 1.0\nAnimatedImage { source: srcImage }";
346 QDeclarativeContext *ctxt = engine.rootContext();
347 ctxt->setContextProperty("srcImage", QUrl::fromLocalFile(SRCDIR "/data/stickman.gif"));
348 QDeclarativeComponent component(&engine);
349 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
350 QDeclarativeImage *obj = qobject_cast<QDeclarativeImage*>(component.create());
352 QVERIFY(obj->status() == QDeclarativeImage::Ready);
353 QTRY_VERIFY(obj->progress() == 1.0);
355 QSignalSpy sourceSpy(obj, SIGNAL(sourceChanged(const QUrl &)));
356 QSignalSpy progressSpy(obj, SIGNAL(progressChanged(qreal)));
357 QSignalSpy statusSpy(obj, SIGNAL(statusChanged(QDeclarativeImageBase::Status)));
359 // Loading local file
360 ctxt->setContextProperty("srcImage", QUrl::fromLocalFile(SRCDIR "/data/colors.gif"));
361 QTRY_VERIFY(obj->status() == QDeclarativeImage::Ready);
362 QTRY_VERIFY(obj->progress() == 1.0);
363 QTRY_COMPARE(sourceSpy.count(), 1);
364 QTRY_COMPARE(progressSpy.count(), 0);
365 QTRY_COMPARE(statusSpy.count(), 0);
367 // Loading remote file
368 ctxt->setContextProperty("srcImage", "http://127.0.0.1:14449/stickman.gif");
369 QTRY_VERIFY(obj->status() == QDeclarativeImage::Loading);
370 QTRY_VERIFY(obj->progress() == 0.0);
371 QTRY_VERIFY(obj->status() == QDeclarativeImage::Ready);
372 QTRY_VERIFY(obj->progress() == 1.0);
373 QTRY_COMPARE(sourceSpy.count(), 2);
374 QTRY_VERIFY(progressSpy.count() > 1);
375 QTRY_COMPARE(statusSpy.count(), 2);
377 ctxt->setContextProperty("srcImage", "");
378 QTRY_VERIFY(obj->status() == QDeclarativeImage::Null);
379 QTRY_VERIFY(obj->progress() == 0.0);
380 QTRY_COMPARE(sourceSpy.count(), 3);
381 QTRY_VERIFY(progressSpy.count() > 2);
382 QTRY_COMPARE(statusSpy.count(), 3);
385 QTEST_MAIN(tst_qdeclarativeanimatedimage)
387 #include "tst_qdeclarativeanimatedimage.moc"