1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
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>
48 #include <QSignalSpy>
49 #include <QtDeclarative/qdeclarativecontext.h>
50 
51 #include "../shared/testhttpserver.h"
52 #include "../../../shared/util.h"
53 
54 #ifdef Q_OS_SYMBIAN
55 // In Symbian OS test data is located in applications private dir
56 #define SRCDIR "."
57 #endif
58 
59 class tst_qdeclarativeanimatedimage : public QObject
60 {
61     Q_OBJECT
62 public:
tst_qdeclarativeanimatedimage()63     tst_qdeclarativeanimatedimage() {}
64 
65 private slots:
66     void play();
67     void pause();
68     void stopped();
69     void setFrame();
70     void frameCount();
71     void mirror_running();
72     void mirror_notRunning();
73     void mirror_notRunning_data();
74     void remote();
75     void remote_data();
76     void sourceSize();
77     void sourceSizeReadOnly();
78     void invalidSource();
79     void qtbug_16520();
80     void progressAndStatusChanges();
81 
82 private:
83     QPixmap grabScene(QGraphicsScene *scene, int width, int height);
84 };
85 
grabScene(QGraphicsScene * scene,int width,int height)86 QPixmap tst_qdeclarativeanimatedimage::grabScene(QGraphicsScene *scene, int width, int height)
87 {
88     QPixmap screenshot(width, height);
89     screenshot.fill();
90     QPainter p_screenshot(&screenshot);
91     scene->render(&p_screenshot, QRect(0, 0, width, height), QRect(0, 0, width, height));
92     return screenshot;
93 }
94 
play()95 void tst_qdeclarativeanimatedimage::play()
96 {
97     QDeclarativeEngine engine;
98     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickman.qml"));
99     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
100     QVERIFY(anim);
101     QVERIFY(anim->isPlaying());
102 
103     delete anim;
104 }
105 
pause()106 void tst_qdeclarativeanimatedimage::pause()
107 {
108     QDeclarativeEngine engine;
109     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickmanpause.qml"));
110     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
111     QVERIFY(anim);
112     QVERIFY(anim->isPlaying());
113     QVERIFY(anim->isPaused());
114 
115     delete anim;
116 }
117 
stopped()118 void tst_qdeclarativeanimatedimage::stopped()
119 {
120     QDeclarativeEngine engine;
121     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickmanstopped.qml"));
122     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
123     QVERIFY(anim);
124     QVERIFY(!anim->isPlaying());
125     QCOMPARE(anim->currentFrame(), 0);
126 
127     delete anim;
128 }
129 
setFrame()130 void tst_qdeclarativeanimatedimage::setFrame()
131 {
132     QDeclarativeEngine engine;
133     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickmanpause.qml"));
134     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
135     QVERIFY(anim);
136     QVERIFY(anim->isPlaying());
137     QCOMPARE(anim->currentFrame(), 2);
138 
139     delete anim;
140 }
141 
frameCount()142 void tst_qdeclarativeanimatedimage::frameCount()
143 {
144     QDeclarativeEngine engine;
145     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/colors.qml"));
146     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
147     QVERIFY(anim);
148     QVERIFY(anim->isPlaying());
149     QCOMPARE(anim->frameCount(), 3);
150 
151     delete anim;
152 }
153 
mirror_running()154 void tst_qdeclarativeanimatedimage::mirror_running()
155 {
156     // test where mirror is set to true after animation has started
157 
158     QDeclarativeEngine engine;
159     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/hearts.qml"));
160     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
161     QVERIFY(anim);
162 
163     QGraphicsScene scene;
164     int width = anim->property("width").toInt();
165     int height = anim->property("height").toInt();
166     scene.addItem(qobject_cast<QGraphicsObject *>(anim));
167 
168     QCOMPARE(anim->currentFrame(), 0);
169     QPixmap frame0 = grabScene(&scene, width, height);
170     anim->setCurrentFrame(1);
171     QPixmap frame1 = grabScene(&scene, width, height);
172 
173     anim->setCurrentFrame(0);
174 
175     QSignalSpy spy(anim, SIGNAL(frameChanged()));
176     anim->setPlaying(true);
177 
178     QTRY_VERIFY(spy.count() == 1); spy.clear();
179     anim->setProperty("mirror", true);
180 
181     QCOMPARE(anim->currentFrame(), 1);
182     QPixmap frame1_flipped = grabScene(&scene, width, height);
183 
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);
187 
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);
192 
193     QCOMPARE(frame0_flipped, frame0_expected);
194     QCOMPARE(frame1_flipped, frame1_expected);
195 }
196 
mirror_notRunning()197 void tst_qdeclarativeanimatedimage::mirror_notRunning()
198 {
199     QFETCH(QUrl, fileUrl);
200 
201     QDeclarativeEngine engine;
202     QDeclarativeComponent component(&engine, fileUrl);
203     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
204     QVERIFY(anim);
205 
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);
211 
212     QTransform transform;
213     transform.translate(width, 0).scale(-1, 1.0);
214     QPixmap expected = screenshot.transformed(transform);
215 
216     int frame = anim->currentFrame();
217     bool playing = anim->isPlaying();
218     bool paused = anim->isPlaying();
219 
220     anim->setProperty("mirror", true);
221     screenshot = grabScene(&scene, width, height);
222 
223     QCOMPARE(screenshot, expected);
224 
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);
229 
230     delete anim;
231 }
232 
mirror_notRunning_data()233 void tst_qdeclarativeanimatedimage::mirror_notRunning_data()
234 {
235     QTest::addColumn<QUrl>("fileUrl");
236 
237     QTest::newRow("paused") << QUrl::fromLocalFile(SRCDIR "/data/stickmanpause.qml");
238     QTest::newRow("stopped") << QUrl::fromLocalFile(SRCDIR "/data/stickmanstopped.qml");
239 }
240 
remote()241 void tst_qdeclarativeanimatedimage::remote()
242 {
243     QFETCH(QString, fileName);
244     QFETCH(bool, paused);
245 
246     TestHTTPServer server(14449);
247     QVERIFY(server.isValid());
248     server.serveDirectory(SRCDIR "/data");
249 
250     QDeclarativeEngine engine;
251     QDeclarativeComponent component(&engine, QUrl("http://127.0.0.1:14449/" + fileName));
252     QTRY_VERIFY(component.isReady());
253 
254     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
255     QVERIFY(anim);
256 
257     QTRY_VERIFY(anim->isPlaying());
258     if (paused) {
259         QTRY_VERIFY(anim->isPaused());
260         QCOMPARE(anim->currentFrame(), 2);
261     }
262     QVERIFY(anim->status() != QDeclarativeAnimatedImage::Error);
263 
264     delete anim;
265 }
266 
sourceSize()267 void tst_qdeclarativeanimatedimage::sourceSize()
268 {
269     QDeclarativeEngine engine;
270     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickmanscaled.qml"));
271     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
272     QVERIFY(anim);
273     QCOMPARE(anim->width(),240.0);
274     QCOMPARE(anim->height(),180.0);
275     QCOMPARE(anim->sourceSize(),QSize(160,120));
276 
277     delete anim;
278 }
279 
sourceSizeReadOnly()280 void tst_qdeclarativeanimatedimage::sourceSizeReadOnly()
281 {
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"));
286 }
287 
remote_data()288 void tst_qdeclarativeanimatedimage::remote_data()
289 {
290     QTest::addColumn<QString>("fileName");
291     QTest::addColumn<bool>("paused");
292 
293     QTest::newRow("playing") << "stickman.qml" << false;
294     QTest::newRow("paused") << "stickmanpause.qml" << true;
295 }
296 
invalidSource()297 void tst_qdeclarativeanimatedimage::invalidSource()
298 {
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());
303 
304     QTest::ignoreMessage(QtWarningMsg, "file::2:2: QML AnimatedImage: Error Reading Animated Image File file:no-such-file.gif");
305 
306     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
307     QVERIFY(anim);
308 
309     QVERIFY(!anim->isPlaying());
310     QVERIFY(!anim->isPaused());
311     QCOMPARE(anim->currentFrame(), 0);
312     QCOMPARE(anim->frameCount(), 0);
313     QTRY_VERIFY(anim->status() == 3);
314 }
315 
qtbug_16520()316 void tst_qdeclarativeanimatedimage::qtbug_16520()
317 {
318     TestHTTPServer server(14449);
319     QVERIFY(server.isValid());
320     server.serveDirectory(SRCDIR "/data");
321 
322     QDeclarativeEngine engine;
323     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/qtbug-16520.qml"));
324     QTRY_VERIFY(component.isReady());
325 
326     QDeclarativeRectangle *root = qobject_cast<QDeclarativeRectangle *>(component.create());
327     QVERIFY(root);
328     QDeclarativeAnimatedImage *anim = root->findChild<QDeclarativeAnimatedImage*>("anim");
329 
330     anim->setProperty("source", "http://127.0.0.1:14449/stickman.gif");
331 
332     QTRY_VERIFY(anim->opacity() == 0);
333     QTRY_VERIFY(anim->opacity() == 1);
334 
335     delete anim;
336 }
337 
progressAndStatusChanges()338 void tst_qdeclarativeanimatedimage::progressAndStatusChanges()
339 {
340     TestHTTPServer server(14449);
341     QVERIFY(server.isValid());
342     server.serveDirectory(SRCDIR "/data");
343 
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());
351     QVERIFY(obj != 0);
352     QVERIFY(obj->status() == QDeclarativeImage::Ready);
353     QTRY_VERIFY(obj->progress() == 1.0);
354 
355     QSignalSpy sourceSpy(obj, SIGNAL(sourceChanged(const QUrl &)));
356     QSignalSpy progressSpy(obj, SIGNAL(progressChanged(qreal)));
357     QSignalSpy statusSpy(obj, SIGNAL(statusChanged(QDeclarativeImageBase::Status)));
358 
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);
366 
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);
376 
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);
383 }
384 
385 QTEST_MAIN(tst_qdeclarativeanimatedimage)
386 
387 #include "tst_qdeclarativeanimatedimage.moc"
388