1 /****************************************************************************
3 ** Copyright (C) 2011 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 <QTextDocument>
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>
55 #include "../shared/testhttpserver.h"
56 #include "../../../shared/util.h"
59 // In Symbian OS test data is located in applications private dir
63 #define SERVER_PORT 14446
64 #define SERVER_ADDR "http://127.0.0.1:14446"
66 class tst_qdeclarativeborderimage : public QObject
71 tst_qdeclarativeborderimage();
76 void imageSource_data();
82 void sciSource_data();
83 void invalidSciFile();
84 void pendingRemoteRequest();
85 void pendingRemoteRequest_data();
88 QDeclarativeEngine engine;
91 tst_qdeclarativeborderimage::tst_qdeclarativeborderimage()
95 void tst_qdeclarativeborderimage::noSource()
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());
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);
111 void tst_qdeclarativeborderimage::imageSource_data()
113 QTest::addColumn<QString>("source");
114 QTest::addColumn<bool>("remote");
115 QTest::addColumn<QString>("error");
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";
125 void tst_qdeclarativeborderimage::imageSource()
127 QFETCH(QString, source);
128 QFETCH(bool, remote);
129 QFETCH(QString, error);
131 TestHTTPServer *server = 0;
133 server = new TestHTTPServer(SERVER_PORT);
134 QVERIFY(server->isValid());
135 server->serveDirectory(SRCDIR "/data");
138 if (!error.isEmpty())
139 QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
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());
148 QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Loading);
150 QCOMPARE(obj->source(), remote ? source : QUrl(source));
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);
159 QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Error);
166 void tst_qdeclarativeborderimage::clearSource()
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());
175 QVERIFY(obj->status() == QDeclarativeBorderImage::Ready);
176 QCOMPARE(obj->width(), 120.);
177 QCOMPARE(obj->height(), 120.);
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.);
186 void tst_qdeclarativeborderimage::resized()
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());
193 QCOMPARE(obj->width(), 300.);
194 QCOMPARE(obj->height(), 300.);
195 QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Stretch);
196 QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Stretch);
201 void tst_qdeclarativeborderimage::smooth()
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());
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);
217 void tst_qdeclarativeborderimage::tileModes()
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());
225 QCOMPARE(obj->width(), 100.);
226 QCOMPARE(obj->height(), 300.);
227 QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Repeat);
228 QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Repeat);
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());
238 QCOMPARE(obj->width(), 300.);
239 QCOMPARE(obj->height(), 150.);
240 QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Round);
241 QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Round);
247 void tst_qdeclarativeborderimage::sciSource()
249 QFETCH(QString, source);
252 bool remote = source.startsWith("http");
253 TestHTTPServer *server = 0;
255 server = new TestHTTPServer(SERVER_PORT);
256 QVERIFY(server->isValid());
257 server->serveDirectory(SRCDIR "/data");
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());
267 QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Loading);
269 QCOMPARE(obj->source(), remote ? source : QUrl(source));
270 QCOMPARE(obj->width(), 300.);
271 QCOMPARE(obj->height(), 300.);
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);
282 QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Error);
289 void tst_qdeclarativeborderimage::sciSource_data()
291 QTest::addColumn<QString>("source");
292 QTest::addColumn<bool>("valid");
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;
301 void tst_qdeclarativeborderimage::invalidSciFile()
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"
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());
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);
320 void tst_qdeclarativeborderimage::pendingRemoteRequest()
322 QFETCH(QString, source);
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());
329 QCOMPARE(obj->status(), QDeclarativeBorderImage::Loading);
332 // This will cause a delayed "QThread: Destroyed while thread is still running" warning
337 void tst_qdeclarativeborderimage::pendingRemoteRequest_data()
339 QTest::addColumn<QString>("source");
341 QTest::newRow("png file") << "http://localhost/none.png";
342 QTest::newRow("sci file") << "http://localhost/none.sci";
345 QTEST_MAIN(tst_qdeclarativeborderimage)
347 #include "tst_qdeclarativeborderimage.moc"