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 ** 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/QtTest>
43 #include <QtDeclarative/qdeclarativeengine.h>
44 #include <QtDeclarative/qdeclarativeimageprovider.h>
45 #include <QtQuick1/private/qdeclarativeimage_p.h>
46 #include <QImageReader>
47 #include <QWaitCondition>
48 #include "../../../shared/util.h"
50 Q_DECLARE_METATYPE(QDeclarativeImageProvider*);
52 class tst_qdeclarativeimageprovider : public QObject
56 tst_qdeclarativeimageprovider()
61 void requestImage_sync_data();
62 void requestImage_sync();
63 void requestImage_async_data();
64 void requestImage_async();
66 void requestPixmap_sync_data();
67 void requestPixmap_sync();
68 void requestPixmap_async();
70 void removeProvider_data();
71 void removeProvider();
76 QString newImageFileName() const;
77 void fillRequestTestsData(const QString &id);
78 void runTest(bool async, QDeclarativeImageProvider *provider);
82 class TestQImageProvider : public QDeclarativeImageProvider
85 TestQImageProvider(bool *deleteWatch = 0)
86 : QDeclarativeImageProvider(Image), deleteWatch(deleteWatch)
96 QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize)
100 if (id == QLatin1String("no-such-file.png"))
105 QImage image(width, height, QImage::Format_RGB32);
107 *size = QSize(width, height);
108 if (requestedSize.isValid())
109 image = image.scaled(requestedSize);
116 Q_DECLARE_METATYPE(TestQImageProvider*);
119 class TestQPixmapProvider : public QDeclarativeImageProvider
122 TestQPixmapProvider(bool *deleteWatch = 0)
123 : QDeclarativeImageProvider(Pixmap), deleteWatch(deleteWatch)
127 ~TestQPixmapProvider()
133 QPixmap requestPixmap(const QString &id, QSize *size, const QSize& requestedSize)
137 if (id == QLatin1String("no-such-file.png"))
142 QPixmap image(width, height);
144 *size = QSize(width, height);
145 if (requestedSize.isValid())
146 image = image.scaled(requestedSize);
153 Q_DECLARE_METATYPE(TestQPixmapProvider*);
156 QString tst_qdeclarativeimageprovider::newImageFileName() const
158 // need to generate new filenames each time or else images are loaded
159 // from cache and we won't get loading status changes when testing
161 static int count = 0;
162 return QString("image://test/image-%1.png").arg(count++);
165 void tst_qdeclarativeimageprovider::fillRequestTestsData(const QString &id)
167 QTest::addColumn<QString>("source");
168 QTest::addColumn<QString>("imageId");
169 QTest::addColumn<QString>("properties");
170 QTest::addColumn<QSize>("size");
171 QTest::addColumn<QString>("error");
173 QString fileName = newImageFileName();
174 QTest::newRow(QTest::toString(id + " simple test"))
175 << "image://test/" + fileName << fileName << "" << QSize(100,100) << "";
177 fileName = newImageFileName();
178 QTest::newRow(QTest::toString(id + " simple test with capitalization"))//As it's a URL, should make no difference
179 << "image://Test/" + fileName << fileName << "" << QSize(100,100) << "";
181 fileName = newImageFileName();
182 QTest::newRow(QTest::toString(id + " url with no id"))
183 << "image://test/" + fileName << "" + fileName << "" << QSize(100,100) << "";
185 fileName = newImageFileName();
186 QTest::newRow(QTest::toString(id + " url with path"))
187 << "image://test/test/path" + fileName << "test/path" + fileName << "" << QSize(100,100) << "";
189 fileName = newImageFileName();
190 QTest::newRow(QTest::toString(id + " url with fragment"))
191 << "image://test/faq.html?#question13" + fileName << "faq.html?#question13" + fileName << "" << QSize(100,100) << "";
193 fileName = newImageFileName();
194 QTest::newRow(QTest::toString(id + " url with query"))
195 << "image://test/cgi-bin/drawgraph.cgi?type=pie&color=green" + fileName << "cgi-bin/drawgraph.cgi?type=pie&color=green" + fileName
196 << "" << QSize(100,100) << "";
198 fileName = newImageFileName();
199 QTest::newRow(QTest::toString(id + " scaled image"))
200 << "image://test/" + fileName << fileName << "sourceSize: \"80x30\"" << QSize(80,30) << "";
202 QTest::newRow(QTest::toString(id + " missing"))
203 << "image://test/no-such-file.png" << "no-such-file.png" << "" << QSize(100,100)
204 << "file::2:1: QML Image: Failed to get image from provider: image://test/no-such-file.png";
206 QTest::newRow(QTest::toString(id + " unknown provider"))
207 << "image://bogus/exists.png" << "" << "" << QSize()
208 << "file::2:1: QML Image: Failed to get image from provider: image://bogus/exists.png";
211 void tst_qdeclarativeimageprovider::runTest(bool async, QDeclarativeImageProvider *provider)
213 QFETCH(QString, source);
214 QFETCH(QString, imageId);
215 QFETCH(QString, properties);
217 QFETCH(QString, error);
219 if (!error.isEmpty())
220 QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
222 QDeclarativeEngine engine;
224 engine.addImageProvider("test", provider);
225 QVERIFY(engine.imageProvider("test") != 0);
227 QString componentStr = "import QtQuick 1.0\nImage { source: \"" + source + "\"; "
228 + (async ? "asynchronous: true; " : "")
230 QDeclarativeComponent component(&engine);
231 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
232 QDeclarative1Image *obj = qobject_cast<QDeclarative1Image*>(component.create());
236 QTRY_VERIFY(obj->status() == QDeclarative1Image::Loading);
238 QCOMPARE(obj->source(), QUrl(source));
240 if (error.isEmpty()) {
242 QTRY_VERIFY(obj->status() == QDeclarative1Image::Ready);
244 QVERIFY(obj->status() == QDeclarative1Image::Ready);
245 if (QByteArray(QTest::currentDataTag()).startsWith("qimage"))
246 QCOMPARE(static_cast<TestQImageProvider*>(provider)->lastImageId, imageId);
248 QCOMPARE(static_cast<TestQPixmapProvider*>(provider)->lastImageId, imageId);
250 QCOMPARE(obj->width(), qreal(size.width()));
251 QCOMPARE(obj->height(), qreal(size.height()));
252 QCOMPARE(obj->pixmap().width(), size.width());
253 QCOMPARE(obj->pixmap().height(), size.height());
254 QCOMPARE(obj->fillMode(), QDeclarative1Image::Stretch);
255 QCOMPARE(obj->progress(), 1.0);
258 QTRY_VERIFY(obj->status() == QDeclarative1Image::Error);
260 QVERIFY(obj->status() == QDeclarative1Image::Error);
266 void tst_qdeclarativeimageprovider::requestImage_sync_data()
268 fillRequestTestsData("qimage|sync");
271 void tst_qdeclarativeimageprovider::requestImage_sync()
273 bool deleteWatch = false;
274 runTest(false, new TestQImageProvider(&deleteWatch));
275 QVERIFY(deleteWatch);
278 void tst_qdeclarativeimageprovider::requestImage_async_data()
280 fillRequestTestsData("qimage|async");
283 void tst_qdeclarativeimageprovider::requestImage_async()
285 bool deleteWatch = false;
286 runTest(true, new TestQImageProvider(&deleteWatch));
287 QVERIFY(deleteWatch);
290 void tst_qdeclarativeimageprovider::requestPixmap_sync_data()
292 fillRequestTestsData("qpixmap");
295 void tst_qdeclarativeimageprovider::requestPixmap_sync()
297 bool deleteWatch = false;
298 runTest(false, new TestQPixmapProvider(&deleteWatch));
299 QVERIFY(deleteWatch);
302 void tst_qdeclarativeimageprovider::requestPixmap_async()
304 QDeclarativeEngine engine;
305 QDeclarativeImageProvider *provider = new TestQPixmapProvider();
307 engine.addImageProvider("test", provider);
308 QVERIFY(engine.imageProvider("test") != 0);
310 // pixmaps are loaded synchronously regardless of 'asynchronous' value
311 QString componentStr = "import QtQuick 1.0\nImage { asynchronous: true; source: \"image://test/pixmap-async-test.png\" }";
312 QDeclarativeComponent component(&engine);
313 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
314 QDeclarative1Image *obj = qobject_cast<QDeclarative1Image*>(component.create());
320 void tst_qdeclarativeimageprovider::removeProvider_data()
322 QTest::addColumn<QDeclarativeImageProvider*>("provider");
324 QTest::newRow("qimage") << static_cast<QDeclarativeImageProvider*>(new TestQImageProvider);
325 QTest::newRow("qpixmap") << static_cast<QDeclarativeImageProvider*>(new TestQPixmapProvider);
328 void tst_qdeclarativeimageprovider::removeProvider()
330 QFETCH(QDeclarativeImageProvider*, provider);
332 QDeclarativeEngine engine;
334 engine.addImageProvider("test", provider);
335 QVERIFY(engine.imageProvider("test") != 0);
337 // add provider, confirm it works
338 QString componentStr = "import QtQuick 1.0\nImage { source: \"" + newImageFileName() + "\" }";
339 QDeclarativeComponent component(&engine);
340 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
341 QDeclarative1Image *obj = qobject_cast<QDeclarative1Image*>(component.create());
344 QCOMPARE(obj->status(), QDeclarative1Image::Ready);
346 // remove the provider and confirm
347 QString fileName = newImageFileName();
348 QString error("file::2:1: QML Image: Failed to get image from provider: " + fileName);
349 QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
351 engine.removeImageProvider("test");
353 obj->setSource(QUrl(fileName));
354 QCOMPARE(obj->status(), QDeclarative1Image::Error);
359 class TestThreadProvider : public QDeclarativeImageProvider
362 TestThreadProvider() : QDeclarativeImageProvider(Image), ok(false) {}
364 ~TestThreadProvider() {}
366 QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize)
373 for (int i = 0; i < 10000; i++)
374 v.prepend(i); //do some computation
375 QImage image(50,50, QImage::Format_RGB32);
376 image.fill(QColor(id).rgb());
378 *size = image.size();
379 if (requestedSize.isValid())
380 image = image.scaled(requestedSize);
390 void tst_qdeclarativeimageprovider::threadTest()
392 QDeclarativeEngine engine;
394 TestThreadProvider *provider = new TestThreadProvider;
396 engine.addImageProvider("test_thread", provider);
397 QVERIFY(engine.imageProvider("test_thread") != 0);
399 QString componentStr = "import QtQuick 1.0\nItem { \n"
400 "Image { source: \"image://test_thread/blue\"; asynchronous: true; }\n"
401 "Image { source: \"image://test_thread/red\"; asynchronous: true; }\n"
402 "Image { source: \"image://test_thread/green\"; asynchronous: true; }\n"
403 "Image { source: \"image://test_thread/yellow\"; asynchronous: true; }\n"
405 QDeclarativeComponent component(&engine);
406 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
407 QObject *obj = component.create();
410 QList<QDeclarative1Image *> images = obj->findChildren<QDeclarative1Image *>();
411 QCOMPARE(images.count(), 4);
413 foreach(QDeclarative1Image *img, images) {
414 QCOMPARE(img->status(), QDeclarative1Image::Loading);
417 provider->cond.wakeAll();
419 foreach(QDeclarative1Image *img, images) {
420 QTRY_VERIFY(img->status() == QDeclarative1Image::Ready);
425 QTEST_MAIN(tst_qdeclarativeimageprovider)
427 #include "tst_qdeclarativeimageprovider.moc"