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 ** 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 <QTextDocument>
46 #include <QGraphicsScene>
49 #include <QtDeclarative/qdeclarativeengine.h>
50 #include <QtDeclarative/qdeclarativecomponent.h>
51 #include <private/qdeclarativeborderimage_p.h>
52 #include <private/qdeclarativeimagebase_p.h>
53 #include <private/qdeclarativescalegrid_p_p.h>
54 #include <private/qdeclarativeloader_p.h>
55 #include <QtDeclarative/qdeclarativecontext.h>
57 #include "../../declarative/shared/testhttpserver.h"
58 #include "../../../shared/util.h"
61 // In Symbian OS test data is located in applications private dir
65 #define SERVER_PORT 14446
66 #define SERVER_ADDR "http://127.0.0.1:14446"
68 class tst_qdeclarativeborderimage : public QObject
73 tst_qdeclarativeborderimage();
78 void imageSource_data();
85 void sciSource_data();
86 void invalidSciFile();
87 void pendingRemoteRequest();
88 void pendingRemoteRequest_data();
89 void testQtQuick11Attributes();
90 void testQtQuick11Attributes_data();
93 QDeclarativeEngine engine;
96 tst_qdeclarativeborderimage::tst_qdeclarativeborderimage()
100 void tst_qdeclarativeborderimage::noSource()
102 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"\" }";
103 QDeclarativeComponent component(&engine);
104 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
105 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
107 QCOMPARE(obj->source(), QUrl());
108 QCOMPARE(obj->width(), 0.);
109 QCOMPARE(obj->height(), 0.);
110 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
111 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
116 void tst_qdeclarativeborderimage::imageSource_data()
118 QTest::addColumn<QString>("source");
119 QTest::addColumn<bool>("remote");
120 QTest::addColumn<QString>("error");
122 QTest::newRow("local") << QUrl::fromLocalFile(SRCDIR "/data/colors.png").toString() << false << "";
123 QTest::newRow("local not found") << QUrl::fromLocalFile(SRCDIR "/data/no-such-file.png").toString() << false
124 << "file::2:1: QML BorderImage: Cannot open: " + QUrl::fromLocalFile(SRCDIR "/data/no-such-file.png").toString();
125 QTest::newRow("remote") << SERVER_ADDR "/colors.png" << true << "";
126 QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.png" << true
127 << "file::2:1: QML BorderImage: Error downloading " SERVER_ADDR "/no-such-file.png - server replied: Not found";
130 void tst_qdeclarativeborderimage::imageSource()
132 QFETCH(QString, source);
133 QFETCH(bool, remote);
134 QFETCH(QString, error);
136 TestHTTPServer *server = 0;
138 server = new TestHTTPServer(SERVER_PORT);
139 QVERIFY(server->isValid());
140 server->serveDirectory(SRCDIR "/data");
143 if (!error.isEmpty())
144 QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
146 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\" }";
147 QDeclarativeComponent component(&engine);
148 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
149 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
153 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Loading);
155 QCOMPARE(obj->source(), remote ? source : QUrl(source));
157 if (error.isEmpty()) {
158 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Ready);
159 QCOMPARE(obj->width(), 120.);
160 QCOMPARE(obj->height(), 120.);
161 QCOMPARE(obj->sourceSize().width(), 120);
162 QCOMPARE(obj->sourceSize().height(), 120);
163 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
164 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
166 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Error);
173 void tst_qdeclarativeborderimage::clearSource()
175 QString componentStr = "import QtQuick 1.0\nBorderImage { source: srcImage }";
176 QDeclarativeContext *ctxt = engine.rootContext();
177 ctxt->setContextProperty("srcImage", QUrl::fromLocalFile(SRCDIR "/data/colors.png"));
178 QDeclarativeComponent component(&engine);
179 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
180 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
182 QVERIFY(obj->status() == QDeclarative1BorderImage::Ready);
183 QCOMPARE(obj->width(), 120.);
184 QCOMPARE(obj->height(), 120.);
186 ctxt->setContextProperty("srcImage", "");
187 QVERIFY(obj->source().isEmpty());
188 QVERIFY(obj->status() == QDeclarative1BorderImage::Null);
189 QCOMPARE(obj->width(), 0.);
190 QCOMPARE(obj->height(), 0.);
193 void tst_qdeclarativeborderimage::resized()
195 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + QUrl::fromLocalFile(SRCDIR "/data/colors.png").toString() + "\"; width: 300; height: 300 }";
196 QDeclarativeComponent component(&engine);
197 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
198 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
200 QCOMPARE(obj->width(), 300.);
201 QCOMPARE(obj->height(), 300.);
202 QCOMPARE(obj->sourceSize().width(), 120);
203 QCOMPARE(obj->sourceSize().height(), 120);
204 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
205 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
210 void tst_qdeclarativeborderimage::smooth()
212 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; smooth: true; width: 300; height: 300 }";
213 QDeclarativeComponent component(&engine);
214 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
215 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
217 QCOMPARE(obj->width(), 300.);
218 QCOMPARE(obj->height(), 300.);
219 QCOMPARE(obj->smooth(), true);
220 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
221 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
226 void tst_qdeclarativeborderimage::mirror()
228 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/heart200.png\"; smooth: true; width: 300; height: 300; border { top: 50; right: 50; bottom: 50; left: 50 } }";
229 QDeclarativeComponent component(&engine);
230 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
231 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
234 int width = obj->property("width").toInt();
235 int height = obj->property("height").toInt();
237 QGraphicsScene scene;
238 scene.addItem(qobject_cast<QGraphicsObject *>(obj));
239 QPixmap screenshot(width, height);
241 QPainter p_screenshot(&screenshot);
242 scene.render(&p_screenshot, QRect(0, 0, width, height), QRect(0, 0, width, height));
244 QTransform transform;
245 transform.translate(width, 0).scale(-1, 1.0);
246 QPixmap expected = screenshot.transformed(transform);
248 obj->setProperty("mirror", true);
249 p_screenshot.fillRect(QRect(0, 0, width, height), Qt::white);
250 scene.render(&p_screenshot, QRect(0, 0, width, height), QRect(0, 0, width, height));
252 QEXPECT_FAIL("", "QTBUG-19538", Continue);
253 QCOMPARE(screenshot, expected);
258 void tst_qdeclarativeborderimage::tileModes()
261 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; width: 100; height: 300; horizontalTileMode: BorderImage.Repeat; verticalTileMode: BorderImage.Repeat }";
262 QDeclarativeComponent component(&engine);
263 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
264 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
266 QCOMPARE(obj->width(), 100.);
267 QCOMPARE(obj->height(), 300.);
268 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Repeat);
269 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Repeat);
274 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; width: 300; height: 150; horizontalTileMode: BorderImage.Round; verticalTileMode: BorderImage.Round }";
275 QDeclarativeComponent component(&engine);
276 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
277 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
279 QCOMPARE(obj->width(), 300.);
280 QCOMPARE(obj->height(), 150.);
281 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Round);
282 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Round);
288 void tst_qdeclarativeborderimage::sciSource()
290 QFETCH(QString, source);
293 bool remote = source.startsWith("http");
294 TestHTTPServer *server = 0;
296 server = new TestHTTPServer(SERVER_PORT);
297 QVERIFY(server->isValid());
298 server->serveDirectory(SRCDIR "/data");
301 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\"; width: 300; height: 300 }";
302 QDeclarativeComponent component(&engine);
303 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
304 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
308 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Loading);
310 QCOMPARE(obj->source(), remote ? source : QUrl(source));
311 QCOMPARE(obj->width(), 300.);
312 QCOMPARE(obj->height(), 300.);
315 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Ready);
316 QCOMPARE(obj->border()->left(), 10);
317 QCOMPARE(obj->border()->top(), 20);
318 QCOMPARE(obj->border()->right(), 30);
319 QCOMPARE(obj->border()->bottom(), 40);
320 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Round);
321 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Repeat);
323 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Error);
330 void tst_qdeclarativeborderimage::sciSource_data()
332 QTest::addColumn<QString>("source");
333 QTest::addColumn<bool>("valid");
335 QTest::newRow("local") << QUrl::fromLocalFile(SRCDIR "/data/colors-round.sci").toString() << true;
336 QTest::newRow("local quoted filename") << QUrl::fromLocalFile(SRCDIR "/data/colors-round-quotes.sci").toString() << true;
337 QTest::newRow("local not found") << QUrl::fromLocalFile(SRCDIR "/data/no-such-file.sci").toString() << false;
338 QTest::newRow("remote") << SERVER_ADDR "/colors-round.sci" << true;
339 QTest::newRow("remote filename quoted") << SERVER_ADDR "/colors-round-quotes.sci" << true;
340 QTest::newRow("remote image") << SERVER_ADDR "/colors-round-remote.sci" << true;
341 QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.sci" << false;
344 void tst_qdeclarativeborderimage::invalidSciFile()
346 QTest::ignoreMessage(QtWarningMsg, "QDeclarative1GridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Roun"
347 QTest::ignoreMessage(QtWarningMsg, "QDeclarative1GridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Repea"
349 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + QUrl::fromLocalFile(SRCDIR "/data/invalid.sci").toString() +"\"; width: 300; height: 300 }";
350 QDeclarativeComponent component(&engine);
351 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
352 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
354 QCOMPARE(obj->width(), 300.);
355 QCOMPARE(obj->height(), 300.);
356 QCOMPARE(obj->status(), QDeclarative1ImageBase::Error);
357 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
358 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
363 void tst_qdeclarativeborderimage::pendingRemoteRequest()
365 QFETCH(QString, source);
367 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\" }";
368 QDeclarativeComponent component(&engine);
369 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
370 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
372 QCOMPARE(obj->status(), QDeclarative1BorderImage::Loading);
375 // This will cause a delayed "QThread: Destroyed while thread is still running" warning
380 void tst_qdeclarativeborderimage::pendingRemoteRequest_data()
382 QTest::addColumn<QString>("source");
384 QTest::newRow("png file") << "http://localhost/none.png";
385 QTest::newRow("sci file") << "http://localhost/none.sci";
388 void tst_qdeclarativeborderimage::testQtQuick11Attributes()
390 QFETCH(QString, code);
391 QFETCH(QString, warning);
392 QFETCH(QString, error);
394 QDeclarativeEngine engine;
397 QDeclarativeComponent valid(&engine);
398 valid.setData("import QtQuick 1.1; BorderImage { " + code.toUtf8() + " }", QUrl(""));
399 obj = valid.create();
401 QVERIFY(valid.errorString().isEmpty());
404 QDeclarativeComponent invalid(&engine);
405 invalid.setData("import QtQuick 1.0; BorderImage { " + code.toUtf8() + " }", QUrl(""));
406 QTest::ignoreMessage(QtWarningMsg, warning.toUtf8());
407 obj = invalid.create();
408 QCOMPARE(invalid.errorString(), error);
412 void tst_qdeclarativeborderimage::testQtQuick11Attributes_data()
414 QTest::addColumn<QString>("code");
415 QTest::addColumn<QString>("warning");
416 QTest::addColumn<QString>("error");
418 QTest::newRow("mirror") << "mirror: true"
419 << "QDeclarativeComponent: Component is not ready"
420 << ":1 \"BorderImage.mirror\" is not available in QtQuick 1.0.\n";
422 QTest::newRow("cache") << "cache: true"
423 << "QDeclarativeComponent: Component is not ready"
424 << ":1 \"BorderImage.cache\" is not available in QtQuick 1.0.\n";
427 QTEST_MAIN(tst_qdeclarativeborderimage)
429 #include "tst_qdeclarativeborderimage.moc"