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"
60 #define SERVER_PORT 14446
61 #define SERVER_ADDR "http://127.0.0.1:14446"
63 class tst_qdeclarativeborderimage : public QObject
68 tst_qdeclarativeborderimage();
73 void imageSource_data();
80 void sciSource_data();
81 void invalidSciFile();
82 void pendingRemoteRequest();
83 void pendingRemoteRequest_data();
84 void testQtQuick11Attributes();
85 void testQtQuick11Attributes_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 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
102 QCOMPARE(obj->source(), QUrl());
103 QCOMPARE(obj->width(), 0.);
104 QCOMPARE(obj->height(), 0.);
105 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
106 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::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 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
148 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Loading);
150 QCOMPARE(obj->source(), remote ? source : QUrl(source));
152 if (error.isEmpty()) {
153 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Ready);
154 QCOMPARE(obj->width(), 120.);
155 QCOMPARE(obj->height(), 120.);
156 QCOMPARE(obj->sourceSize().width(), 120);
157 QCOMPARE(obj->sourceSize().height(), 120);
158 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
159 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
161 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Error);
168 void tst_qdeclarativeborderimage::clearSource()
170 QString componentStr = "import QtQuick 1.0\nBorderImage { source: srcImage }";
171 QDeclarativeContext *ctxt = engine.rootContext();
172 ctxt->setContextProperty("srcImage", QUrl::fromLocalFile(SRCDIR "/data/colors.png"));
173 QDeclarativeComponent component(&engine);
174 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
175 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
177 QVERIFY(obj->status() == QDeclarative1BorderImage::Ready);
178 QCOMPARE(obj->width(), 120.);
179 QCOMPARE(obj->height(), 120.);
181 ctxt->setContextProperty("srcImage", "");
182 QVERIFY(obj->source().isEmpty());
183 QVERIFY(obj->status() == QDeclarative1BorderImage::Null);
184 QCOMPARE(obj->width(), 0.);
185 QCOMPARE(obj->height(), 0.);
188 void tst_qdeclarativeborderimage::resized()
190 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + QUrl::fromLocalFile(SRCDIR "/data/colors.png").toString() + "\"; width: 300; height: 300 }";
191 QDeclarativeComponent component(&engine);
192 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
193 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
195 QCOMPARE(obj->width(), 300.);
196 QCOMPARE(obj->height(), 300.);
197 QCOMPARE(obj->sourceSize().width(), 120);
198 QCOMPARE(obj->sourceSize().height(), 120);
199 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
200 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
205 void tst_qdeclarativeborderimage::smooth()
207 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; smooth: true; width: 300; height: 300 }";
208 QDeclarativeComponent component(&engine);
209 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
210 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
212 QCOMPARE(obj->width(), 300.);
213 QCOMPARE(obj->height(), 300.);
214 QCOMPARE(obj->smooth(), true);
215 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
216 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
221 void tst_qdeclarativeborderimage::mirror()
223 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 } }";
224 QDeclarativeComponent component(&engine);
225 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
226 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
229 int width = obj->property("width").toInt();
230 int height = obj->property("height").toInt();
232 QGraphicsScene scene;
233 scene.addItem(qobject_cast<QGraphicsObject *>(obj));
234 QPixmap screenshot(width, height);
236 QPainter p_screenshot(&screenshot);
237 scene.render(&p_screenshot, QRect(0, 0, width, height), QRect(0, 0, width, height));
239 QTransform transform;
240 transform.translate(width, 0).scale(-1, 1.0);
241 QPixmap expected = screenshot.transformed(transform);
243 obj->setProperty("mirror", true);
244 p_screenshot.fillRect(QRect(0, 0, width, height), Qt::white);
245 scene.render(&p_screenshot, QRect(0, 0, width, height), QRect(0, 0, width, height));
247 QEXPECT_FAIL("", "QTBUG-19538", Continue);
248 QCOMPARE(screenshot, expected);
253 void tst_qdeclarativeborderimage::tileModes()
256 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; width: 100; height: 300; horizontalTileMode: BorderImage.Repeat; verticalTileMode: BorderImage.Repeat }";
257 QDeclarativeComponent component(&engine);
258 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
259 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
261 QCOMPARE(obj->width(), 100.);
262 QCOMPARE(obj->height(), 300.);
263 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Repeat);
264 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Repeat);
269 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; width: 300; height: 150; horizontalTileMode: BorderImage.Round; verticalTileMode: BorderImage.Round }";
270 QDeclarativeComponent component(&engine);
271 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
272 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
274 QCOMPARE(obj->width(), 300.);
275 QCOMPARE(obj->height(), 150.);
276 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Round);
277 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Round);
283 void tst_qdeclarativeborderimage::sciSource()
285 QFETCH(QString, source);
288 bool remote = source.startsWith("http");
289 TestHTTPServer *server = 0;
291 server = new TestHTTPServer(SERVER_PORT);
292 QVERIFY(server->isValid());
293 server->serveDirectory(SRCDIR "/data");
296 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\"; width: 300; height: 300 }";
297 QDeclarativeComponent component(&engine);
298 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
299 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
303 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Loading);
305 QCOMPARE(obj->source(), remote ? source : QUrl(source));
306 QCOMPARE(obj->width(), 300.);
307 QCOMPARE(obj->height(), 300.);
310 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Ready);
311 QCOMPARE(obj->border()->left(), 10);
312 QCOMPARE(obj->border()->top(), 20);
313 QCOMPARE(obj->border()->right(), 30);
314 QCOMPARE(obj->border()->bottom(), 40);
315 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Round);
316 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Repeat);
318 QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Error);
325 void tst_qdeclarativeborderimage::sciSource_data()
327 QTest::addColumn<QString>("source");
328 QTest::addColumn<bool>("valid");
330 QTest::newRow("local") << QUrl::fromLocalFile(SRCDIR "/data/colors-round.sci").toString() << true;
331 QTest::newRow("local quoted filename") << QUrl::fromLocalFile(SRCDIR "/data/colors-round-quotes.sci").toString() << true;
332 QTest::newRow("local not found") << QUrl::fromLocalFile(SRCDIR "/data/no-such-file.sci").toString() << false;
333 QTest::newRow("remote") << SERVER_ADDR "/colors-round.sci" << true;
334 QTest::newRow("remote filename quoted") << SERVER_ADDR "/colors-round-quotes.sci" << true;
335 QTest::newRow("remote image") << SERVER_ADDR "/colors-round-remote.sci" << true;
336 QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.sci" << false;
339 void tst_qdeclarativeborderimage::invalidSciFile()
341 QTest::ignoreMessage(QtWarningMsg, "QDeclarative1GridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Roun"
342 QTest::ignoreMessage(QtWarningMsg, "QDeclarative1GridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Repea"
344 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + QUrl::fromLocalFile(SRCDIR "/data/invalid.sci").toString() +"\"; width: 300; height: 300 }";
345 QDeclarativeComponent component(&engine);
346 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
347 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
349 QCOMPARE(obj->width(), 300.);
350 QCOMPARE(obj->height(), 300.);
351 QCOMPARE(obj->status(), QDeclarative1ImageBase::Error);
352 QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
353 QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
358 void tst_qdeclarativeborderimage::pendingRemoteRequest()
360 QFETCH(QString, source);
362 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\" }";
363 QDeclarativeComponent component(&engine);
364 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
365 QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
367 QCOMPARE(obj->status(), QDeclarative1BorderImage::Loading);
370 // This will cause a delayed "QThread: Destroyed while thread is still running" warning
375 void tst_qdeclarativeborderimage::pendingRemoteRequest_data()
377 QTest::addColumn<QString>("source");
379 QTest::newRow("png file") << "http://localhost/none.png";
380 QTest::newRow("sci file") << "http://localhost/none.sci";
383 void tst_qdeclarativeborderimage::testQtQuick11Attributes()
385 QFETCH(QString, code);
386 QFETCH(QString, warning);
387 QFETCH(QString, error);
389 QDeclarativeEngine engine;
392 QDeclarativeComponent valid(&engine);
393 valid.setData("import QtQuick 1.1; BorderImage { " + code.toUtf8() + " }", QUrl(""));
394 obj = valid.create();
396 QVERIFY(valid.errorString().isEmpty());
399 QDeclarativeComponent invalid(&engine);
400 invalid.setData("import QtQuick 1.0; BorderImage { " + code.toUtf8() + " }", QUrl(""));
401 QTest::ignoreMessage(QtWarningMsg, warning.toUtf8());
402 obj = invalid.create();
403 QCOMPARE(invalid.errorString(), error);
407 void tst_qdeclarativeborderimage::testQtQuick11Attributes_data()
409 QTest::addColumn<QString>("code");
410 QTest::addColumn<QString>("warning");
411 QTest::addColumn<QString>("error");
413 QTest::newRow("mirror") << "mirror: true"
414 << "QDeclarativeComponent: Component is not ready"
415 << ":1 \"BorderImage.mirror\" is not available in QtQuick 1.0.\n";
417 QTest::newRow("cache") << "cache: true"
418 << "QDeclarativeComponent: Component is not ready"
419 << ":1 \"BorderImage.cache\" is not available in QtQuick 1.0.\n";
422 QTEST_MAIN(tst_qdeclarativeborderimage)
424 #include "tst_qdeclarativeborderimage.moc"