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 "../../shared/testhttpserver.h"
59 #define SERVER_PORT 14446
60 #define SERVER_ADDR "http://127.0.0.1:14446"
62 class tst_qdeclarativeborderimage : public QObject
67 tst_qdeclarativeborderimage();
72 void imageSource_data();
79 void sciSource_data();
80 void invalidSciFile();
81 void pendingRemoteRequest();
82 void pendingRemoteRequest_data();
83 void testQtQuick11Attributes();
84 void testQtQuick11Attributes_data();
87 QDeclarativeEngine engine;
90 tst_qdeclarativeborderimage::tst_qdeclarativeborderimage()
94 void tst_qdeclarativeborderimage::noSource()
96 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"\" }";
97 QDeclarativeComponent component(&engine);
98 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
99 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
101 QCOMPARE(obj->source(), QUrl());
102 QCOMPARE(obj->width(), 0.);
103 QCOMPARE(obj->height(), 0.);
104 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
105 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
110 void tst_qdeclarativeborderimage::imageSource_data()
112 QTest::addColumn<QString>("source");
113 QTest::addColumn<bool>("remote");
114 QTest::addColumn<QString>("error");
116 QTest::newRow("local") << QUrl::fromLocalFile(SRCDIR "/data/colors.png").toString() << false << "";
117 QTest::newRow("local not found") << QUrl::fromLocalFile(SRCDIR "/data/no-such-file.png").toString() << false
118 << "file::2:1: QML BorderImage: Cannot open: " + QUrl::fromLocalFile(SRCDIR "/data/no-such-file.png").toString();
119 QTest::newRow("remote") << SERVER_ADDR "/colors.png" << true << "";
120 QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.png" << true
121 << "file::2:1: QML BorderImage: Error downloading " SERVER_ADDR "/no-such-file.png - server replied: Not found";
124 void tst_qdeclarativeborderimage::imageSource()
126 QFETCH(QString, source);
127 QFETCH(bool, remote);
128 QFETCH(QString, error);
130 TestHTTPServer *server = 0;
132 server = new TestHTTPServer(SERVER_PORT);
133 QVERIFY(server->isValid());
134 server->serveDirectory(SRCDIR "/data");
137 if (!error.isEmpty())
138 QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
140 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\" }";
141 QDeclarativeComponent component(&engine);
142 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
143 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
147 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Loading);
149 QCOMPARE(obj->source(), remote ? source : QUrl(source));
151 if (error.isEmpty()) {
152 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Ready);
153 QCOMPARE(obj->width(), 120.);
154 QCOMPARE(obj->height(), 120.);
155 QCOMPARE(obj->sourceSize().width(), 120);
156 QCOMPARE(obj->sourceSize().height(), 120);
157 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
158 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
160 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Error);
167 void tst_qdeclarativeborderimage::clearSource()
169 QString componentStr = "import QtQuick 1.0\nBorderImage { source: srcImage }";
170 QDeclarativeContext *ctxt = engine.rootContext();
171 ctxt->setContextProperty("srcImage", QUrl::fromLocalFile(SRCDIR "/data/colors.png"));
172 QDeclarativeComponent component(&engine);
173 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
174 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
176 QVERIFY(obj->status() == QDeclarative1BorderImage::Ready);
177 QCOMPARE(obj->width(), 120.);
178 QCOMPARE(obj->height(), 120.);
180 ctxt->setContextProperty("srcImage", "");
181 QVERIFY(obj->source().isEmpty());
182 QVERIFY(obj->status() == QDeclarative1BorderImage::Null);
183 QCOMPARE(obj->width(), 0.);
184 QCOMPARE(obj->height(), 0.);
187 void tst_qdeclarativeborderimage::resized()
189 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + QUrl::fromLocalFile(SRCDIR "/data/colors.png").toString() + "\"; width: 300; height: 300 }";
190 QDeclarativeComponent component(&engine);
191 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
192 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
194 QCOMPARE(obj->width(), 300.);
195 QCOMPARE(obj->height(), 300.);
196 QCOMPARE(obj->sourceSize().width(), 120);
197 QCOMPARE(obj->sourceSize().height(), 120);
198 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
199 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
204 void tst_qdeclarativeborderimage::smooth()
206 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; smooth: true; width: 300; height: 300 }";
207 QDeclarativeComponent component(&engine);
208 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
209 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
211 QCOMPARE(obj->width(), 300.);
212 QCOMPARE(obj->height(), 300.);
213 QCOMPARE(obj->smooth(), true);
214 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
215 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
220 void tst_qdeclarativeborderimage::mirror()
222 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 } }";
223 QDeclarativeComponent component(&engine);
224 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
225 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
228 int width = obj->property("width").toInt();
229 int height = obj->property("height").toInt();
231 QGraphicsScene scene;
232 scene.addItem(qobject_cast<QGraphicsObject *>(obj));
233 QPixmap screenshot(width, height);
235 QPainter p_screenshot(&screenshot);
236 scene.render(&p_screenshot, QRect(0, 0, width, height), QRect(0, 0, width, height));
238 QTransform transform;
239 transform.translate(width, 0).scale(-1, 1.0);
240 QPixmap expected = screenshot.transformed(transform);
242 obj->setProperty("mirror", true);
243 p_screenshot.fillRect(QRect(0, 0, width, height), Qt::white);
244 scene.render(&p_screenshot, QRect(0, 0, width, height), QRect(0, 0, width, height));
246 QEXPECT_FAIL("", "QTBUG-19538", Continue);
247 QCOMPARE(screenshot, expected);
252 void tst_qdeclarativeborderimage::tileModes()
255 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; width: 100; height: 300; horizontalTileMode: BorderImage.Repeat; verticalTileMode: BorderImage.Repeat }";
256 QDeclarativeComponent component(&engine);
257 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
258 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
260 QCOMPARE(obj->width(), 100.);
261 QCOMPARE(obj->height(), 300.);
262 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Repeat);
263 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Repeat);
268 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; width: 300; height: 150; horizontalTileMode: BorderImage.Round; verticalTileMode: BorderImage.Round }";
269 QDeclarativeComponent component(&engine);
270 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
271 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
273 QCOMPARE(obj->width(), 300.);
274 QCOMPARE(obj->height(), 150.);
275 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Round);
276 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Round);
282 void tst_qdeclarativeborderimage::sciSource()
284 QFETCH(QString, source);
287 bool remote = source.startsWith("http");
288 TestHTTPServer *server = 0;
290 server = new TestHTTPServer(SERVER_PORT);
291 QVERIFY(server->isValid());
292 server->serveDirectory(SRCDIR "/data");
295 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\"; width: 300; height: 300 }";
296 QDeclarativeComponent component(&engine);
297 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
298 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
302 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Loading);
304 QCOMPARE(obj->source(), remote ? source : QUrl(source));
305 QCOMPARE(obj->width(), 300.);
306 QCOMPARE(obj->height(), 300.);
309 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Ready);
310 QCOMPARE(obj->border()->left(), 10);
311 QCOMPARE(obj->border()->top(), 20);
312 QCOMPARE(obj->border()->right(), 30);
313 QCOMPARE(obj->border()->bottom(), 40);
314 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Round);
315 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Repeat);
317 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Error);
324 void tst_qdeclarativeborderimage::sciSource_data()
326 QTest::addColumn<QString>("source");
327 QTest::addColumn<bool>("valid");
329 QTest::newRow("local") << QUrl::fromLocalFile(SRCDIR "/data/colors-round.sci").toString() << true;
330 QTest::newRow("local quoted filename") << QUrl::fromLocalFile(SRCDIR "/data/colors-round-quotes.sci").toString() << true;
331 QTest::newRow("local not found") << QUrl::fromLocalFile(SRCDIR "/data/no-such-file.sci").toString() << false;
332 QTest::newRow("remote") << SERVER_ADDR "/colors-round.sci" << true;
333 QTest::newRow("remote filename quoted") << SERVER_ADDR "/colors-round-quotes.sci" << true;
334 QTest::newRow("remote image") << SERVER_ADDR "/colors-round-remote.sci" << true;
335 QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.sci" << false;
338 void tst_qdeclarativeborderimage::invalidSciFile()
340 QTest::ignoreMessage(QtWarningMsg, "QDeclarative1GridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Roun"
341 QTest::ignoreMessage(QtWarningMsg, "QDeclarative1GridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Repea"
343 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + QUrl::fromLocalFile(SRCDIR "/data/invalid.sci").toString() +"\"; width: 300; height: 300 }";
344 QDeclarativeComponent component(&engine);
345 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
346 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
348 QCOMPARE(obj->width(), 300.);
349 QCOMPARE(obj->height(), 300.);
350 QCOMPARE(obj->status(), QDeclarative1ImageBase::Error);
351 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
352 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
357 void tst_qdeclarativeborderimage::pendingRemoteRequest()
359 QFETCH(QString, source);
361 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\" }";
362 QDeclarativeComponent component(&engine);
363 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
364 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
366 QCOMPARE(obj->status(), QDeclarative1BorderImage::Loading);
369 // This will cause a delayed "QThread: Destroyed while thread is still running" warning
374 void tst_qdeclarativeborderimage::pendingRemoteRequest_data()
376 QTest::addColumn<QString>("source");
378 QTest::newRow("png file") << "http://localhost/none.png";
379 QTest::newRow("sci file") << "http://localhost/none.sci";
382 void tst_qdeclarativeborderimage::testQtQuick11Attributes()
384 QFETCH(QString, code);
385 QFETCH(QString, warning);
386 QFETCH(QString, error);
388 QDeclarativeEngine engine;
391 QDeclarativeComponent valid(&engine);
392 valid.setData("import QtQuick 1.1; BorderImage { " + code.toUtf8() + " }", QUrl(""));
393 obj = valid.create();
395 QVERIFY(valid.errorString().isEmpty());
398 QDeclarativeComponent invalid(&engine);
399 invalid.setData("import QtQuick 1.0; BorderImage { " + code.toUtf8() + " }", QUrl(""));
400 QTest::ignoreMessage(QtWarningMsg, warning.toUtf8());
401 obj = invalid.create();
402 QCOMPARE(invalid.errorString(), error);
406 void tst_qdeclarativeborderimage::testQtQuick11Attributes_data()
408 QTest::addColumn<QString>("code");
409 QTest::addColumn<QString>("warning");
410 QTest::addColumn<QString>("error");
412 QTest::newRow("mirror") << "mirror: true"
413 << "QDeclarativeComponent: Component is not ready"
414 << ":1 \"BorderImage.mirror\" is not available in QtQuick 1.0.\n";
416 QTest::newRow("cache") << "cache: true"
417 << "QDeclarativeComponent: Component is not ready"
418 << ":1 \"BorderImage.cache\" is not available in QtQuick 1.0.\n";
421 QTEST_MAIN(tst_qdeclarativeborderimage)
423 #include "tst_qdeclarativeborderimage.moc"