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 <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"
49
50 #ifdef Q_OS_SYMBIAN
51 // In Symbian OS test data is located in applications private dir
52 #define SRCDIR "."
53 #endif
54
55 Q_DECLARE_METATYPE(QDeclarativeImageProvider*);
56
57 class tst_qdeclarativeimageprovider : public QObject
58 {
59 Q_OBJECT
60 public:
tst_qdeclarativeimageprovider()61 tst_qdeclarativeimageprovider()
62 {
63 }
64
65 private slots:
66 void requestImage_sync_data();
67 void requestImage_sync();
68 void requestImage_async_data();
69 void requestImage_async();
70
71 void requestPixmap_sync_data();
72 void requestPixmap_sync();
73 void requestPixmap_async();
74
75 void removeProvider_data();
76 void removeProvider();
77
78 void threadTest();
79
80 private:
81 QString newImageFileName() const;
82 void fillRequestTestsData(const QString &id);
83 void runTest(bool async, QDeclarativeImageProvider *provider);
84 };
85
86
87 class TestQImageProvider : public QDeclarativeImageProvider
88 {
89 public:
TestQImageProvider(bool * deleteWatch=0)90 TestQImageProvider(bool *deleteWatch = 0)
91 : QDeclarativeImageProvider(Image), deleteWatch(deleteWatch)
92 {
93 }
94
~TestQImageProvider()95 ~TestQImageProvider()
96 {
97 if (deleteWatch)
98 *deleteWatch = true;
99 }
100
requestImage(const QString & id,QSize * size,const QSize & requestedSize)101 QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize)
102 {
103 lastImageId = id;
104
105 if (id == QLatin1String("no-such-file.png"))
106 return QImage();
107
108 int width = 100;
109 int height = 100;
110 QImage image(width, height, QImage::Format_RGB32);
111 if (size)
112 *size = QSize(width, height);
113 if (requestedSize.isValid())
114 image = image.scaled(requestedSize);
115 return image;
116 }
117
118 bool *deleteWatch;
119 QString lastImageId;
120 };
121 Q_DECLARE_METATYPE(TestQImageProvider*);
122
123
124 class TestQPixmapProvider : public QDeclarativeImageProvider
125 {
126 public:
TestQPixmapProvider(bool * deleteWatch=0)127 TestQPixmapProvider(bool *deleteWatch = 0)
128 : QDeclarativeImageProvider(Pixmap), deleteWatch(deleteWatch)
129 {
130 }
131
~TestQPixmapProvider()132 ~TestQPixmapProvider()
133 {
134 if (deleteWatch)
135 *deleteWatch = true;
136 }
137
requestPixmap(const QString & id,QSize * size,const QSize & requestedSize)138 QPixmap requestPixmap(const QString &id, QSize *size, const QSize& requestedSize)
139 {
140 lastImageId = id;
141
142 if (id == QLatin1String("no-such-file.png"))
143 return QPixmap();
144
145 int width = 100;
146 int height = 100;
147 QPixmap image(width, height);
148 if (size)
149 *size = QSize(width, height);
150 if (requestedSize.isValid())
151 image = image.scaled(requestedSize);
152 return image;
153 }
154
155 bool *deleteWatch;
156 QString lastImageId;
157 };
158 Q_DECLARE_METATYPE(TestQPixmapProvider*);
159
160
newImageFileName() const161 QString tst_qdeclarativeimageprovider::newImageFileName() const
162 {
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
165 // async loading
166 static int count = 0;
167 return QString("image://test/image-%1.png").arg(count++);
168 }
169
fillRequestTestsData(const QString & id)170 void tst_qdeclarativeimageprovider::fillRequestTestsData(const QString &id)
171 {
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");
177
178 QString fileName = newImageFileName();
179 QTest::newRow(QTest::toString(id + " simple test"))
180 << "image://test/" + fileName << fileName << "" << QSize(100,100) << "";
181
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) << "";
185
186 fileName = newImageFileName();
187 QTest::newRow(QTest::toString(id + " url with no id"))
188 << "image://test/" + fileName << "" + fileName << "" << QSize(100,100) << "";
189
190 fileName = newImageFileName();
191 QTest::newRow(QTest::toString(id + " url with path"))
192 << "image://test/test/path" + fileName << "test/path" + fileName << "" << QSize(100,100) << "";
193
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) << "";
197
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) << "";
202
203 fileName = newImageFileName();
204 QTest::newRow(QTest::toString(id + " scaled image"))
205 << "image://test/" + fileName << fileName << "sourceSize: \"80x30\"" << QSize(80,30) << "";
206
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";
210
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";
214 }
215
runTest(bool async,QDeclarativeImageProvider * provider)216 void tst_qdeclarativeimageprovider::runTest(bool async, QDeclarativeImageProvider *provider)
217 {
218 QFETCH(QString, source);
219 QFETCH(QString, imageId);
220 QFETCH(QString, properties);
221 QFETCH(QSize, size);
222 QFETCH(QString, error);
223
224 if (!error.isEmpty())
225 QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
226
227 QDeclarativeEngine engine;
228
229 engine.addImageProvider("test", provider);
230 QVERIFY(engine.imageProvider("test") != 0);
231
232 QString componentStr = "import QtQuick 1.0\nImage { source: \"" + source + "\"; "
233 + (async ? "asynchronous: true; " : "")
234 + properties + " }";
235 QDeclarativeComponent component(&engine);
236 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
237 QDeclarativeImage *obj = qobject_cast<QDeclarativeImage*>(component.create());
238 QVERIFY(obj != 0);
239
240 if (async)
241 QTRY_VERIFY(obj->status() == QDeclarativeImage::Loading);
242
243 QCOMPARE(obj->source(), QUrl(source));
244
245 if (error.isEmpty()) {
246 if (async)
247 QTRY_VERIFY(obj->status() == QDeclarativeImage::Ready);
248 else
249 QVERIFY(obj->status() == QDeclarativeImage::Ready);
250 if (QByteArray(QTest::currentDataTag()).startsWith("qimage"))
251 QCOMPARE(static_cast<TestQImageProvider*>(provider)->lastImageId, imageId);
252 else
253 QCOMPARE(static_cast<TestQPixmapProvider*>(provider)->lastImageId, imageId);
254
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);
261 } else {
262 if (async)
263 QTRY_VERIFY(obj->status() == QDeclarativeImage::Error);
264 else
265 QVERIFY(obj->status() == QDeclarativeImage::Error);
266 }
267
268 delete obj;
269 }
270
requestImage_sync_data()271 void tst_qdeclarativeimageprovider::requestImage_sync_data()
272 {
273 fillRequestTestsData("qimage|sync");
274 }
275
requestImage_sync()276 void tst_qdeclarativeimageprovider::requestImage_sync()
277 {
278 bool deleteWatch = false;
279 runTest(false, new TestQImageProvider(&deleteWatch));
280 QVERIFY(deleteWatch);
281 }
282
requestImage_async_data()283 void tst_qdeclarativeimageprovider::requestImage_async_data()
284 {
285 fillRequestTestsData("qimage|async");
286 }
287
requestImage_async()288 void tst_qdeclarativeimageprovider::requestImage_async()
289 {
290 bool deleteWatch = false;
291 runTest(true, new TestQImageProvider(&deleteWatch));
292 QVERIFY(deleteWatch);
293 }
294
requestPixmap_sync_data()295 void tst_qdeclarativeimageprovider::requestPixmap_sync_data()
296 {
297 fillRequestTestsData("qpixmap");
298 }
299
requestPixmap_sync()300 void tst_qdeclarativeimageprovider::requestPixmap_sync()
301 {
302 bool deleteWatch = false;
303 runTest(false, new TestQPixmapProvider(&deleteWatch));
304 QVERIFY(deleteWatch);
305 }
306
requestPixmap_async()307 void tst_qdeclarativeimageprovider::requestPixmap_async()
308 {
309 QDeclarativeEngine engine;
310 QDeclarativeImageProvider *provider = new TestQPixmapProvider();
311
312 engine.addImageProvider("test", provider);
313 QVERIFY(engine.imageProvider("test") != 0);
314
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());
320 QVERIFY(obj != 0);
321
322 delete obj;
323 }
324
removeProvider_data()325 void tst_qdeclarativeimageprovider::removeProvider_data()
326 {
327 QTest::addColumn<QDeclarativeImageProvider*>("provider");
328
329 QTest::newRow("qimage") << static_cast<QDeclarativeImageProvider*>(new TestQImageProvider);
330 QTest::newRow("qpixmap") << static_cast<QDeclarativeImageProvider*>(new TestQPixmapProvider);
331 }
332
removeProvider()333 void tst_qdeclarativeimageprovider::removeProvider()
334 {
335 QFETCH(QDeclarativeImageProvider*, provider);
336
337 QDeclarativeEngine engine;
338
339 engine.addImageProvider("test", provider);
340 QVERIFY(engine.imageProvider("test") != 0);
341
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());
347 QVERIFY(obj != 0);
348
349 QCOMPARE(obj->status(), QDeclarativeImage::Ready);
350
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());
355
356 engine.removeImageProvider("test");
357
358 obj->setSource(QUrl(fileName));
359 QCOMPARE(obj->status(), QDeclarativeImage::Error);
360
361 delete obj;
362 }
363
364 class TestThreadProvider : public QDeclarativeImageProvider
365 {
366 public:
TestThreadProvider()367 TestThreadProvider() : QDeclarativeImageProvider(Image), ok(false) {}
368
~TestThreadProvider()369 ~TestThreadProvider() {}
370
requestImage(const QString & id,QSize * size,const QSize & requestedSize)371 QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize)
372 {
373 mutex.lock();
374 if (!ok)
375 cond.wait(&mutex);
376 mutex.unlock();
377 QVector<int> v;
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());
382 if (size)
383 *size = image.size();
384 if (requestedSize.isValid())
385 image = image.scaled(requestedSize);
386 return image;
387 }
388
389 QWaitCondition cond;
390 QMutex mutex;
391 bool ok;
392 };
393
394
threadTest()395 void tst_qdeclarativeimageprovider::threadTest()
396 {
397 QDeclarativeEngine engine;
398
399 TestThreadProvider *provider = new TestThreadProvider;
400
401 engine.addImageProvider("test_thread", provider);
402 QVERIFY(engine.imageProvider("test_thread") != 0);
403
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"
409 " }";
410 QDeclarativeComponent component(&engine);
411 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
412 QObject *obj = component.create();
413 //MUST not deadlock
414 QVERIFY(obj != 0);
415 QList<QDeclarativeImage *> images = obj->findChildren<QDeclarativeImage *>();
416 QCOMPARE(images.count(), 4);
417 QTest::qWait(100);
418 foreach(QDeclarativeImage *img, images) {
419 QCOMPARE(img->status(), QDeclarativeImage::Loading);
420 }
421 provider->ok = true;
422 provider->cond.wakeAll();
423 QTest::qWait(250);
424 foreach(QDeclarativeImage *img, images) {
425 QTRY_VERIFY(img->status() == QDeclarativeImage::Ready);
426 }
427 }
428
429
430 QTEST_MAIN(tst_qdeclarativeimageprovider)
431
432 #include "tst_qdeclarativeimageprovider.moc"
433