Remove Symbian-specific code from tests.
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick1 / qdeclarativeimageprovider / tst_qdeclarativeimageprovider.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
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 <QtQuick1/private/qdeclarativeimage_p.h>
46 #include <QImageReader>
47 #include <QWaitCondition>
48 #include "../../../shared/util.h"
49
50 Q_DECLARE_METATYPE(QDeclarativeImageProvider*);
51
52 class tst_qdeclarativeimageprovider : public QObject
53 {
54     Q_OBJECT
55 public:
56     tst_qdeclarativeimageprovider()
57     {
58     }
59
60 private slots:
61     void requestImage_sync_data();
62     void requestImage_sync();
63     void requestImage_async_data();
64     void requestImage_async();
65
66     void requestPixmap_sync_data();
67     void requestPixmap_sync();
68     void requestPixmap_async();
69
70     void removeProvider_data();
71     void removeProvider();
72
73     void threadTest();
74
75 private:
76     QString newImageFileName() const;
77     void fillRequestTestsData(const QString &id);
78     void runTest(bool async, QDeclarativeImageProvider *provider);
79 };
80
81
82 class TestQImageProvider : public QDeclarativeImageProvider
83 {
84 public:
85     TestQImageProvider(bool *deleteWatch = 0)
86         : QDeclarativeImageProvider(Image), deleteWatch(deleteWatch)
87     {
88     }
89
90     ~TestQImageProvider()
91     {
92         if (deleteWatch)
93             *deleteWatch = true;
94     }
95
96     QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize)
97     {
98         lastImageId = id;
99
100         if (id == QLatin1String("no-such-file.png"))
101             return QImage();
102
103         int width = 100; 
104         int height = 100;
105         QImage image(width, height, QImage::Format_RGB32);
106         if (size) 
107             *size = QSize(width, height);
108         if (requestedSize.isValid())
109             image = image.scaled(requestedSize);
110         return image;
111     }
112
113     bool *deleteWatch;
114     QString lastImageId;
115 };
116 Q_DECLARE_METATYPE(TestQImageProvider*);
117
118
119 class TestQPixmapProvider : public QDeclarativeImageProvider
120 {
121 public:
122     TestQPixmapProvider(bool *deleteWatch = 0)
123         : QDeclarativeImageProvider(Pixmap), deleteWatch(deleteWatch)
124     {
125     }
126
127     ~TestQPixmapProvider()
128     {
129         if (deleteWatch)
130             *deleteWatch = true;
131     }
132
133     QPixmap requestPixmap(const QString &id, QSize *size, const QSize& requestedSize)
134     {
135         lastImageId = id;
136
137         if (id == QLatin1String("no-such-file.png"))
138             return QPixmap();
139
140         int width = 100; 
141         int height = 100;
142         QPixmap image(width, height);
143         if (size) 
144             *size = QSize(width, height);
145         if (requestedSize.isValid())
146             image = image.scaled(requestedSize);
147         return image;
148     }
149
150     bool *deleteWatch;
151     QString lastImageId;
152 };
153 Q_DECLARE_METATYPE(TestQPixmapProvider*);
154
155
156 QString tst_qdeclarativeimageprovider::newImageFileName() const
157 {
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 
160     // async loading
161     static int count = 0;
162     return QString("image://test/image-%1.png").arg(count++);
163 }
164
165 void tst_qdeclarativeimageprovider::fillRequestTestsData(const QString &id)
166 {
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");
172
173     QString fileName = newImageFileName();
174     QTest::newRow(QTest::toString(id + " simple test"))
175             << "image://test/" + fileName << fileName << "" << QSize(100,100) << "";
176
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) << "";
180
181     fileName = newImageFileName();
182     QTest::newRow(QTest::toString(id + " url with no id"))
183         << "image://test/" + fileName << "" + fileName << "" << QSize(100,100) << "";
184
185     fileName = newImageFileName();
186     QTest::newRow(QTest::toString(id + " url with path"))
187         << "image://test/test/path" + fileName << "test/path" + fileName << "" << QSize(100,100) << "";
188
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) << "";
192
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) << "";
197
198     fileName = newImageFileName();
199     QTest::newRow(QTest::toString(id + " scaled image"))
200             << "image://test/" + fileName << fileName << "sourceSize: \"80x30\"" << QSize(80,30) << "";
201
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";
205
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";
209 }
210
211 void tst_qdeclarativeimageprovider::runTest(bool async, QDeclarativeImageProvider *provider)
212 {
213     QFETCH(QString, source);
214     QFETCH(QString, imageId);
215     QFETCH(QString, properties);
216     QFETCH(QSize, size);
217     QFETCH(QString, error);
218
219     if (!error.isEmpty())
220         QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
221
222     QDeclarativeEngine engine;
223
224     engine.addImageProvider("test", provider);
225     QVERIFY(engine.imageProvider("test") != 0);
226
227     QString componentStr = "import QtQuick 1.0\nImage { source: \"" + source + "\"; " 
228             + (async ? "asynchronous: true; " : "")
229             + properties + " }";
230     QDeclarativeComponent component(&engine);
231     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
232     QDeclarative1Image *obj = qobject_cast<QDeclarative1Image*>(component.create());
233     QVERIFY(obj != 0);
234
235     if (async) 
236         QTRY_VERIFY(obj->status() == QDeclarative1Image::Loading);
237
238     QCOMPARE(obj->source(), QUrl(source));
239
240     if (error.isEmpty()) {
241         if (async)
242             QTRY_VERIFY(obj->status() == QDeclarative1Image::Ready);
243         else
244             QVERIFY(obj->status() == QDeclarative1Image::Ready);
245         if (QByteArray(QTest::currentDataTag()).startsWith("qimage"))
246             QCOMPARE(static_cast<TestQImageProvider*>(provider)->lastImageId, imageId);
247         else
248             QCOMPARE(static_cast<TestQPixmapProvider*>(provider)->lastImageId, imageId);
249
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);
256     } else {
257         if (async)
258             QTRY_VERIFY(obj->status() == QDeclarative1Image::Error);
259         else
260             QVERIFY(obj->status() == QDeclarative1Image::Error);
261     }
262
263     delete obj;
264 }
265
266 void tst_qdeclarativeimageprovider::requestImage_sync_data()
267 {
268     fillRequestTestsData("qimage|sync");
269 }
270
271 void tst_qdeclarativeimageprovider::requestImage_sync()
272 {
273     bool deleteWatch = false;
274     runTest(false, new TestQImageProvider(&deleteWatch));
275     QVERIFY(deleteWatch);
276 }
277
278 void tst_qdeclarativeimageprovider::requestImage_async_data()
279 {
280     fillRequestTestsData("qimage|async");
281 }
282
283 void tst_qdeclarativeimageprovider::requestImage_async()
284 {
285     bool deleteWatch = false;
286     runTest(true, new TestQImageProvider(&deleteWatch));
287     QVERIFY(deleteWatch);
288 }
289
290 void tst_qdeclarativeimageprovider::requestPixmap_sync_data()
291 {
292     fillRequestTestsData("qpixmap");
293 }
294
295 void tst_qdeclarativeimageprovider::requestPixmap_sync()
296 {
297     bool deleteWatch = false;
298     runTest(false, new TestQPixmapProvider(&deleteWatch));
299     QVERIFY(deleteWatch);
300 }
301
302 void tst_qdeclarativeimageprovider::requestPixmap_async()
303 {
304     QDeclarativeEngine engine;
305     QDeclarativeImageProvider *provider = new TestQPixmapProvider();
306
307     engine.addImageProvider("test", provider);
308     QVERIFY(engine.imageProvider("test") != 0);
309
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());
315     QVERIFY(obj != 0);
316
317     delete obj;
318 }
319
320 void tst_qdeclarativeimageprovider::removeProvider_data()
321 {
322     QTest::addColumn<QDeclarativeImageProvider*>("provider");
323
324     QTest::newRow("qimage") << static_cast<QDeclarativeImageProvider*>(new TestQImageProvider);
325     QTest::newRow("qpixmap") << static_cast<QDeclarativeImageProvider*>(new TestQPixmapProvider);
326 }
327
328 void tst_qdeclarativeimageprovider::removeProvider()
329 {
330     QFETCH(QDeclarativeImageProvider*, provider);
331
332     QDeclarativeEngine engine;
333
334     engine.addImageProvider("test", provider);
335     QVERIFY(engine.imageProvider("test") != 0);
336
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());
342     QVERIFY(obj != 0);
343
344     QCOMPARE(obj->status(), QDeclarative1Image::Ready);
345
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());
350
351     engine.removeImageProvider("test");
352
353     obj->setSource(QUrl(fileName));
354     QCOMPARE(obj->status(), QDeclarative1Image::Error);
355
356     delete obj;
357 }
358
359 class TestThreadProvider : public QDeclarativeImageProvider
360 {
361     public:
362         TestThreadProvider() : QDeclarativeImageProvider(Image), ok(false) {}
363
364         ~TestThreadProvider() {}
365
366         QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize)
367         {
368             mutex.lock();
369             if (!ok)
370                 cond.wait(&mutex);
371             mutex.unlock();
372             QVector<int> v;
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());
377             if (size)
378                 *size = image.size();
379             if (requestedSize.isValid())
380                 image = image.scaled(requestedSize);
381             return image;
382         }
383
384         QWaitCondition cond;
385         QMutex mutex;
386         bool ok;
387 };
388
389
390 void tst_qdeclarativeimageprovider::threadTest()
391 {
392     QDeclarativeEngine engine;
393
394     TestThreadProvider *provider = new TestThreadProvider;
395
396     engine.addImageProvider("test_thread", provider);
397     QVERIFY(engine.imageProvider("test_thread") != 0);
398
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"
404             " }";
405     QDeclarativeComponent component(&engine);
406     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
407     QObject *obj = component.create();
408     //MUST not deadlock
409     QVERIFY(obj != 0);
410     QList<QDeclarative1Image *> images = obj->findChildren<QDeclarative1Image *>();
411     QCOMPARE(images.count(), 4);
412     QTest::qWait(100);
413     foreach(QDeclarative1Image *img, images) {
414         QCOMPARE(img->status(), QDeclarative1Image::Loading);
415     }
416     provider->ok = true;
417     provider->cond.wakeAll();
418     QTest::qWait(250);
419     foreach(QDeclarative1Image *img, images) {
420         QTRY_VERIFY(img->status() == QDeclarative1Image::Ready);
421     }
422 }
423
424
425 QTEST_MAIN(tst_qdeclarativeimageprovider)
426
427 #include "tst_qdeclarativeimageprovider.moc"