BorderImage fails for .sci source containing a URL
[qt:qt.git] / tests / auto / declarative / qdeclarativeborderimage / tst_qdeclarativeborderimage.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 ** 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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
42 #include <QTextDocument>
43 #include <QTcpServer>
44 #include <QTcpSocket>
45 #include <QDir>
46
47 #include <QtDeclarative/qdeclarativeengine.h>
48 #include <QtDeclarative/qdeclarativecomponent.h>
49 #include <private/qdeclarativeborderimage_p.h>
50 #include <private/qdeclarativeimagebase_p.h>
51 #include <private/qdeclarativescalegrid_p_p.h>
52 #include <private/qdeclarativeloader_p.h>
53 #include <QtDeclarative/qdeclarativecontext.h>
54
55 #include "../shared/testhttpserver.h"
56 #include "../../../shared/util.h"
57
58 #ifdef Q_OS_SYMBIAN
59 // In Symbian OS test data is located in applications private dir
60 #define SRCDIR "."
61 #endif
62
63 #define SERVER_PORT 14446
64 #define SERVER_ADDR "http://127.0.0.1:14446"
65
66 class tst_qdeclarativeborderimage : public QObject
67
68 {
69     Q_OBJECT
70 public:
71     tst_qdeclarativeborderimage();
72
73 private slots:
74     void noSource();
75     void imageSource();
76     void imageSource_data();
77     void clearSource();
78     void resized();
79     void smooth();
80     void tileModes();
81     void sciSource();
82     void sciSource_data();
83     void invalidSciFile();
84     void pendingRemoteRequest();
85     void pendingRemoteRequest_data();
86
87 private:
88     QDeclarativeEngine engine;
89 };
90
91 tst_qdeclarativeborderimage::tst_qdeclarativeborderimage()
92 {
93 }
94
95 void tst_qdeclarativeborderimage::noSource()
96 {
97     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"\" }";
98     QDeclarativeComponent component(&engine);
99     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
100     QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
101     QVERIFY(obj != 0);
102     QCOMPARE(obj->source(), QUrl());
103     QCOMPARE(obj->width(), 0.);
104     QCOMPARE(obj->height(), 0.);
105     QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Stretch);
106     QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Stretch);
107
108     delete obj;
109 }
110
111 void tst_qdeclarativeborderimage::imageSource_data()
112 {
113     QTest::addColumn<QString>("source");
114     QTest::addColumn<bool>("remote");
115     QTest::addColumn<QString>("error");
116
117     QTest::newRow("local") << QUrl::fromLocalFile(SRCDIR "/data/colors.png").toString() << false << "";
118     QTest::newRow("local not found") << QUrl::fromLocalFile(SRCDIR "/data/no-such-file.png").toString() << false
119         << "file::2:1: QML BorderImage: Cannot open: " + QUrl::fromLocalFile(SRCDIR "/data/no-such-file.png").toString();
120     QTest::newRow("remote") << SERVER_ADDR "/colors.png" << true << "";
121     QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.png" << true
122         << "file::2:1: QML BorderImage: Error downloading " SERVER_ADDR "/no-such-file.png - server replied: Not found";
123 }
124
125 void tst_qdeclarativeborderimage::imageSource()
126 {
127     QFETCH(QString, source);
128     QFETCH(bool, remote);
129     QFETCH(QString, error);
130
131     TestHTTPServer *server = 0;
132     if (remote) {
133         server = new TestHTTPServer(SERVER_PORT);
134         QVERIFY(server->isValid());
135         server->serveDirectory(SRCDIR "/data");
136     }
137
138     if (!error.isEmpty())
139         QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
140
141     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\" }";
142     QDeclarativeComponent component(&engine);
143     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
144     QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
145     QVERIFY(obj != 0);
146
147     if (remote)
148         QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Loading);
149
150     QCOMPARE(obj->source(), remote ? source : QUrl(source));
151
152     if (error.isEmpty()) {
153         QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Ready);
154         QCOMPARE(obj->width(), 120.);
155         QCOMPARE(obj->height(), 120.);
156         QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Stretch);
157         QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Stretch);
158     } else {
159         QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Error);
160     }
161
162     delete obj;
163     delete server;
164 }
165
166 void tst_qdeclarativeborderimage::clearSource()
167 {
168     QString componentStr = "import QtQuick 1.0\nBorderImage { source: srcImage }";
169     QDeclarativeContext *ctxt = engine.rootContext();
170     ctxt->setContextProperty("srcImage", QUrl::fromLocalFile(SRCDIR "/data/colors.png"));
171     QDeclarativeComponent component(&engine);
172     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
173     QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
174     QVERIFY(obj != 0);
175     QVERIFY(obj->status() == QDeclarativeBorderImage::Ready);
176     QCOMPARE(obj->width(), 120.);
177     QCOMPARE(obj->height(), 120.);
178
179     ctxt->setContextProperty("srcImage", "");
180     QVERIFY(obj->source().isEmpty());
181     QVERIFY(obj->status() == QDeclarativeBorderImage::Null);
182     QCOMPARE(obj->width(), 0.);
183     QCOMPARE(obj->height(), 0.);
184 }
185
186 void tst_qdeclarativeborderimage::resized()
187 {
188     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + QUrl::fromLocalFile(SRCDIR "/data/colors.png").toString() + "\"; width: 300; height: 300 }";
189     QDeclarativeComponent component(&engine);
190     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
191     QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
192     QVERIFY(obj != 0);
193     QCOMPARE(obj->width(), 300.);
194     QCOMPARE(obj->height(), 300.);
195     QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Stretch);
196     QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Stretch);
197
198     delete obj;
199 }
200
201 void tst_qdeclarativeborderimage::smooth()
202 {
203     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; smooth: true; width: 300; height: 300 }";
204     QDeclarativeComponent component(&engine);
205     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
206     QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
207     QVERIFY(obj != 0);
208     QCOMPARE(obj->width(), 300.);
209     QCOMPARE(obj->height(), 300.);
210     QCOMPARE(obj->smooth(), true);
211     QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Stretch);
212     QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Stretch);
213
214     delete obj;
215 }
216
217 void tst_qdeclarativeborderimage::tileModes()
218 {
219     {
220         QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; width: 100; height: 300; horizontalTileMode: BorderImage.Repeat; verticalTileMode: BorderImage.Repeat }";
221         QDeclarativeComponent component(&engine);
222         component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
223         QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
224         QVERIFY(obj != 0);
225         QCOMPARE(obj->width(), 100.);
226         QCOMPARE(obj->height(), 300.);
227         QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Repeat);
228         QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Repeat);
229
230         delete obj;
231     }
232     {
233         QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; width: 300; height: 150; horizontalTileMode: BorderImage.Round; verticalTileMode: BorderImage.Round }";
234         QDeclarativeComponent component(&engine);
235         component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
236         QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
237         QVERIFY(obj != 0);
238         QCOMPARE(obj->width(), 300.);
239         QCOMPARE(obj->height(), 150.);
240         QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Round);
241         QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Round);
242
243         delete obj;
244     }
245 }
246
247 void tst_qdeclarativeborderimage::sciSource()
248 {
249     QFETCH(QString, source);
250     QFETCH(bool, valid);
251
252     bool remote = source.startsWith("http");
253     TestHTTPServer *server = 0;
254     if (remote) {
255         server = new TestHTTPServer(SERVER_PORT);
256         QVERIFY(server->isValid());
257         server->serveDirectory(SRCDIR "/data");
258     }
259
260     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\"; width: 300; height: 300 }";
261     QDeclarativeComponent component(&engine);
262     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
263     QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
264     QVERIFY(obj != 0);
265
266     if (remote)
267         QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Loading);
268
269     QCOMPARE(obj->source(), remote ? source : QUrl(source));
270     QCOMPARE(obj->width(), 300.);
271     QCOMPARE(obj->height(), 300.);
272
273     if (valid) {
274         QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Ready);
275         QCOMPARE(obj->border()->left(), 10);
276         QCOMPARE(obj->border()->top(), 20);
277         QCOMPARE(obj->border()->right(), 30);
278         QCOMPARE(obj->border()->bottom(), 40);
279         QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Round);
280         QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Repeat);
281     } else {
282         QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Error);
283     }
284
285     delete obj;
286     delete server;
287 }
288
289 void tst_qdeclarativeborderimage::sciSource_data()
290 {
291     QTest::addColumn<QString>("source");
292     QTest::addColumn<bool>("valid");
293
294     QTest::newRow("local") << QUrl::fromLocalFile(SRCDIR "/data/colors-round.sci").toString() << true;
295     QTest::newRow("local not found") << QUrl::fromLocalFile(SRCDIR "/data/no-such-file.sci").toString() << false;
296     QTest::newRow("remote") << SERVER_ADDR "/colors-round.sci" << true;
297     QTest::newRow("remote image") << SERVER_ADDR "/colors-round-remote.sci" << true;
298     QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.sci" << false;
299 }
300
301 void tst_qdeclarativeborderimage::invalidSciFile()
302 {
303     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeGridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Roun"
304     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeGridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Repea"
305
306     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + QUrl::fromLocalFile(SRCDIR "/data/invalid.sci").toString() +"\"; width: 300; height: 300 }";
307     QDeclarativeComponent component(&engine);
308     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
309     QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
310     QVERIFY(obj != 0);
311     QCOMPARE(obj->width(), 300.);
312     QCOMPARE(obj->height(), 300.);
313     QCOMPARE(obj->status(), QDeclarativeImageBase::Error);
314     QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Stretch);
315     QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Stretch);
316
317     delete obj;
318 }
319
320 void tst_qdeclarativeborderimage::pendingRemoteRequest()
321 {
322     QFETCH(QString, source);
323
324     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\" }";
325     QDeclarativeComponent component(&engine);
326     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
327     QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
328     QVERIFY(obj != 0);
329     QCOMPARE(obj->status(), QDeclarativeBorderImage::Loading);
330
331     // verify no crash
332     // This will cause a delayed "QThread: Destroyed while thread is still running" warning
333     delete obj;
334     QTest::qWait(50);
335 }
336
337 void tst_qdeclarativeborderimage::pendingRemoteRequest_data()
338 {
339     QTest::addColumn<QString>("source");
340
341     QTest::newRow("png file") << "http://localhost/none.png";
342     QTest::newRow("sci file") << "http://localhost/none.sci";
343 }
344
345 QTEST_MAIN(tst_qdeclarativeborderimage)
346
347 #include "tst_qdeclarativeborderimage.moc"