1 /****************************************************************************
3 ** Copyright (C) 2009 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 <QtTest/QtTest>
43 #include <QtDeclarative/qdeclarativeengine.h>
44 #include <QtDeclarative/qdeclarativeimageprovider.h>
45 #include <private/qdeclarativeimage_p.h>
46 #include <QImageReader>
47 #include <QWaitCondition>
48 #include "../../../shared/util.h"
51 // In Symbian OS test data is located in applications private dir
55 Q_DECLARE_METATYPE(QDeclarativeImageProvider*);
57 class tst_qdeclarativeimageprovider : public QObject
61 tst_qdeclarativeimageprovider()
66 void requestImage_sync_data();
67 void requestImage_sync();
68 void requestImage_async_data();
69 void requestImage_async();
71 void requestPixmap_sync_data();
72 void requestPixmap_sync();
73 void requestPixmap_async();
75 void removeProvider_data();
76 void removeProvider();
81 QString newImageFileName() const;
82 void fillRequestTestsData(const QString &id);
83 void runTest(bool async, QDeclarativeImageProvider *provider);
87 class TestQImageProvider : public QDeclarativeImageProvider
90 TestQImageProvider(bool *deleteWatch = 0)
91 : QDeclarativeImageProvider(Image), deleteWatch(deleteWatch)
101 QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize)
105 if (id == QLatin1String("no-such-file.png"))
110 QImage image(width, height, QImage::Format_RGB32);
112 *size = QSize(width, height);
113 if (requestedSize.isValid())
114 image = image.scaled(requestedSize);
121 Q_DECLARE_METATYPE(TestQImageProvider*);
124 class TestQPixmapProvider : public QDeclarativeImageProvider
127 TestQPixmapProvider(bool *deleteWatch = 0)
128 : QDeclarativeImageProvider(Pixmap), deleteWatch(deleteWatch)
132 ~TestQPixmapProvider()
138 QPixmap requestPixmap(const QString &id, QSize *size, const QSize& requestedSize)
142 if (id == QLatin1String("no-such-file.png"))
147 QPixmap image(width, height);
149 *size = QSize(width, height);
150 if (requestedSize.isValid())
151 image = image.scaled(requestedSize);
158 Q_DECLARE_METATYPE(TestQPixmapProvider*);
161 QString tst_qdeclarativeimageprovider::newImageFileName() const
163 // need to generate new filenames each time or else images are loaded
164 // from cache and we won't get loading status changes when testing
166 static int count = 0;
167 return QString("image://test/image-%1.png").arg(count++);
170 void tst_qdeclarativeimageprovider::fillRequestTestsData(const QString &id)
172 QTest::addColumn<QString>("source");
173 QTest::addColumn<QString>("imageId");
174 QTest::addColumn<QString>("properties");
175 QTest::addColumn<QSize>("size");
176 QTest::addColumn<QString>("error");
178 QString fileName = newImageFileName();
179 QTest::newRow(QTest::toString(id + " simple test"))
180 << "image://test/" + fileName << fileName << "" << QSize(100,100) << "";
182 fileName = newImageFileName();
183 QTest::newRow(QTest::toString(id + " simple test with capitalization"))//As it's a URL, should make no difference
184 << "image://Test/" + fileName << fileName << "" << QSize(100,100) << "";
186 fileName = newImageFileName();
187 QTest::newRow(QTest::toString(id + " url with no id"))
188 << "image://test/" + fileName << "" + fileName << "" << QSize(100,100) << "";
190 fileName = newImageFileName();
191 QTest::newRow(QTest::toString(id + " url with path"))
192 << "image://test/test/path" + fileName << "test/path" + fileName << "" << QSize(100,100) << "";
194 fileName = newImageFileName();
195 QTest::newRow(QTest::toString(id + " url with fragment"))
196 << "image://test/faq.html?#question13" + fileName << "faq.html?#question13" + fileName << "" << QSize(100,100) << "";
198 fileName = newImageFileName();
199 QTest::newRow(QTest::toString(id + " url with query"))
200 << "image://test/cgi-bin/drawgraph.cgi?type=pie&color=green" + fileName << "cgi-bin/drawgraph.cgi?type=pie&color=green" + fileName
201 << "" << QSize(100,100) << "";
203 fileName = newImageFileName();
204 QTest::newRow(QTest::toString(id + " scaled image"))
205 << "image://test/" + fileName << fileName << "sourceSize: \"80x30\"" << QSize(80,30) << "";
207 QTest::newRow(QTest::toString(id + " missing"))
208 << "image://test/no-such-file.png" << "no-such-file.png" << "" << QSize(100,100)
209 << "file::2:1: QML Image: Failed to get image from provider: image://test/no-such-file.png";
211 QTest::newRow(QTest::toString(id + " unknown provider"))
212 << "image://bogus/exists.png" << "" << "" << QSize()
213 << "file::2:1: QML Image: Failed to get image from provider: image://bogus/exists.png";
216 void tst_qdeclarativeimageprovider::runTest(bool async, QDeclarativeImageProvider *provider)
218 QFETCH(QString, source);
219 QFETCH(QString, imageId);
220 QFETCH(QString, properties);
222 QFETCH(QString, error);
224 if (!error.isEmpty())
225 QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
227 QDeclarativeEngine engine;
229 engine.addImageProvider("test", provider);
230 QVERIFY(engine.imageProvider("test") != 0);
232 QString componentStr = "import QtQuick 1.0\nImage { source: \"" + source + "\"; "
233 + (async ? "asynchronous: true; " : "")
235 QDeclarativeComponent component(&engine);
236 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
237 QDeclarativeImage *obj = qobject_cast<QDeclarativeImage*>(component.create());
241 QTRY_VERIFY(obj->status() == QDeclarativeImage::Loading);
243 QCOMPARE(obj->source(), QUrl(source));
245 if (error.isEmpty()) {
247 QTRY_VERIFY(obj->status() == QDeclarativeImage::Ready);
249 QVERIFY(obj->status() == QDeclarativeImage::Ready);
250 if (QByteArray(QTest::currentDataTag()).startsWith("qimage"))
251 QCOMPARE(static_cast<TestQImageProvider*>(provider)->lastImageId, imageId);
253 QCOMPARE(static_cast<TestQPixmapProvider*>(provider)->lastImageId, imageId);
255 QCOMPARE(obj->width(), qreal(size.width()));
256 QCOMPARE(obj->height(), qreal(size.height()));
257 QCOMPARE(obj->pixmap().width(), size.width());
258 QCOMPARE(obj->pixmap().height(), size.height());
259 QCOMPARE(obj->fillMode(), QDeclarativeImage::Stretch);
260 QCOMPARE(obj->progress(), 1.0);
263 QTRY_VERIFY(obj->status() == QDeclarativeImage::Error);
265 QVERIFY(obj->status() == QDeclarativeImage::Error);
271 void tst_qdeclarativeimageprovider::requestImage_sync_data()
273 fillRequestTestsData("qimage|sync");
276 void tst_qdeclarativeimageprovider::requestImage_sync()
278 bool deleteWatch = false;
279 runTest(false, new TestQImageProvider(&deleteWatch));
280 QVERIFY(deleteWatch);
283 void tst_qdeclarativeimageprovider::requestImage_async_data()
285 fillRequestTestsData("qimage|async");
288 void tst_qdeclarativeimageprovider::requestImage_async()
290 bool deleteWatch = false;
291 runTest(true, new TestQImageProvider(&deleteWatch));
292 QVERIFY(deleteWatch);
295 void tst_qdeclarativeimageprovider::requestPixmap_sync_data()
297 fillRequestTestsData("qpixmap");
300 void tst_qdeclarativeimageprovider::requestPixmap_sync()
302 bool deleteWatch = false;
303 runTest(false, new TestQPixmapProvider(&deleteWatch));
304 QVERIFY(deleteWatch);
307 void tst_qdeclarativeimageprovider::requestPixmap_async()
309 QDeclarativeEngine engine;
310 QDeclarativeImageProvider *provider = new TestQPixmapProvider();
312 engine.addImageProvider("test", provider);
313 QVERIFY(engine.imageProvider("test") != 0);
315 // pixmaps are loaded synchronously regardless of 'asynchronous' value
316 QString componentStr = "import QtQuick 1.0\nImage { asynchronous: true; source: \"image://test/pixmap-async-test.png\" }";
317 QDeclarativeComponent component(&engine);
318 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
319 QDeclarativeImage *obj = qobject_cast<QDeclarativeImage*>(component.create());
325 void tst_qdeclarativeimageprovider::removeProvider_data()
327 QTest::addColumn<QDeclarativeImageProvider*>("provider");
329 QTest::newRow("qimage") << static_cast<QDeclarativeImageProvider*>(new TestQImageProvider);
330 QTest::newRow("qpixmap") << static_cast<QDeclarativeImageProvider*>(new TestQPixmapProvider);
333 void tst_qdeclarativeimageprovider::removeProvider()
335 QFETCH(QDeclarativeImageProvider*, provider);
337 QDeclarativeEngine engine;
339 engine.addImageProvider("test", provider);
340 QVERIFY(engine.imageProvider("test") != 0);
342 // add provider, confirm it works
343 QString componentStr = "import QtQuick 1.0\nImage { source: \"" + newImageFileName() + "\" }";
344 QDeclarativeComponent component(&engine);
345 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
346 QDeclarativeImage *obj = qobject_cast<QDeclarativeImage*>(component.create());
349 QCOMPARE(obj->status(), QDeclarativeImage::Ready);
351 // remove the provider and confirm
352 QString fileName = newImageFileName();
353 QString error("file::2:1: QML Image: Failed to get image from provider: " + fileName);
354 QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
356 engine.removeImageProvider("test");
358 obj->setSource(QUrl(fileName));
359 QCOMPARE(obj->status(), QDeclarativeImage::Error);
364 class TestThreadProvider : public QDeclarativeImageProvider
367 TestThreadProvider() : QDeclarativeImageProvider(Image), ok(false) {}
369 ~TestThreadProvider() {}
371 QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize)
378 for (int i = 0; i < 10000; i++)
379 v.prepend(i); //do some computation
380 QImage image(50,50, QImage::Format_RGB32);
381 image.fill(QColor(id).rgb());
383 *size = image.size();
384 if (requestedSize.isValid())
385 image = image.scaled(requestedSize);
395 void tst_qdeclarativeimageprovider::threadTest()
397 QDeclarativeEngine engine;
399 TestThreadProvider *provider = new TestThreadProvider;
401 engine.addImageProvider("test_thread", provider);
402 QVERIFY(engine.imageProvider("test_thread") != 0);
404 QString componentStr = "import QtQuick 1.0\nItem { \n"
405 "Image { source: \"image://test_thread/blue\"; asynchronous: true; }\n"
406 "Image { source: \"image://test_thread/red\"; asynchronous: true; }\n"
407 "Image { source: \"image://test_thread/green\"; asynchronous: true; }\n"
408 "Image { source: \"image://test_thread/yellow\"; asynchronous: true; }\n"
410 QDeclarativeComponent component(&engine);
411 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
412 QObject *obj = component.create();
415 QList<QDeclarativeImage *> images = obj->findChildren<QDeclarativeImage *>();
416 QCOMPARE(images.count(), 4);
418 foreach(QDeclarativeImage *img, images) {
419 QCOMPARE(img->status(), QDeclarativeImage::Loading);
422 provider->cond.wakeAll();
424 foreach(QDeclarativeImage *img, images) {
425 QTRY_VERIFY(img->status() == QDeclarativeImage::Ready);
430 QTEST_MAIN(tst_qdeclarativeimageprovider)
432 #include "tst_qdeclarativeimageprovider.moc"