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/qsgborderimage_p.h>
52 #include <private/qsgimagebase_p.h>
53 #include <private/qsgscalegrid_p_p.h>
54 #include <private/qsgloader_p.h>
55 #include <QtDeclarative/qsgview.h>
56 #include <QtDeclarative/qdeclarativecontext.h>
58 #include "../shared/testhttpserver.h"
59 #include "../shared/util.h"
61 #define SERVER_PORT 14446
62 #define SERVER_ADDR "http://127.0.0.1:14446"
64 class tst_qsgborderimage : public QObject
74 void imageSource_data();
81 void sciSource_data();
82 void invalidSciFile();
83 void pendingRemoteRequest();
84 void pendingRemoteRequest_data();
87 QDeclarativeEngine engine;
90 tst_qsgborderimage::tst_qsgborderimage()
94 void tst_qsgborderimage::noSource()
96 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"\" }";
97 QDeclarativeComponent component(&engine);
98 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
99 QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
101 QCOMPARE(obj->source(), QUrl());
102 QCOMPARE(obj->width(), 0.);
103 QCOMPARE(obj->height(), 0.);
104 QCOMPARE(obj->horizontalTileMode(), QSGBorderImage::Stretch);
105 QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Stretch);
110 void tst_qsgborderimage::imageSource_data()
112 QTest::addColumn<QString>("source");
113 QTest::addColumn<bool>("remote");
114 QTest::addColumn<QString>("error");
116 QTest::newRow("local") << QUrl::fromLocalFile(TESTDATA("colors.png")).toString() << false << "";
117 QTest::newRow("local not found") << QUrl::fromLocalFile(TESTDATA("no-such-file.png")).toString() << false
118 << "file::2:1: QML BorderImage: Cannot open: " + QUrl::fromLocalFile(TESTDATA("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_qsgborderimage::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(TESTDATA(""));
137 if (!error.isEmpty())
138 QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
140 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + source + "\" }";
141 QDeclarativeComponent component(&engine);
142 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
143 QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
147 QTRY_VERIFY(obj->status() == QSGBorderImage::Loading);
149 QCOMPARE(obj->source(), remote ? source : QUrl(source));
151 if (error.isEmpty()) {
152 QTRY_VERIFY(obj->status() == QSGBorderImage::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(), QSGBorderImage::Stretch);
158 QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Stretch);
160 QTRY_VERIFY(obj->status() == QSGBorderImage::Error);
167 void tst_qsgborderimage::clearSource()
169 QString componentStr = "import QtQuick 2.0\nBorderImage { source: srcImage }";
170 QDeclarativeContext *ctxt = engine.rootContext();
171 ctxt->setContextProperty("srcImage", QUrl::fromLocalFile(TESTDATA("colors.png")));
172 QDeclarativeComponent component(&engine);
173 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
174 QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
176 QVERIFY(obj->status() == QSGBorderImage::Ready);
177 QCOMPARE(obj->width(), 120.);
178 QCOMPARE(obj->height(), 120.);
180 ctxt->setContextProperty("srcImage", "");
181 QVERIFY(obj->source().isEmpty());
182 QVERIFY(obj->status() == QSGBorderImage::Null);
183 QCOMPARE(obj->width(), 0.);
184 QCOMPARE(obj->height(), 0.);
187 void tst_qsgborderimage::resized()
189 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + QUrl::fromLocalFile(TESTDATA("colors.png")).toString() + "\"; width: 300; height: 300 }";
190 QDeclarativeComponent component(&engine);
191 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
192 QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(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(), QSGBorderImage::Stretch);
199 QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Stretch);
204 void tst_qsgborderimage::smooth()
206 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + TESTDATA("colors.png") + "\"; smooth: true; width: 300; height: 300 }";
207 QDeclarativeComponent component(&engine);
208 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
209 QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
211 QCOMPARE(obj->width(), 300.);
212 QCOMPARE(obj->height(), 300.);
213 QCOMPARE(obj->smooth(), true);
214 QCOMPARE(obj->horizontalTileMode(), QSGBorderImage::Stretch);
215 QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Stretch);
220 void tst_qsgborderimage::mirror()
222 QSGView *canvas = new QSGView;
225 canvas->setSource(QUrl::fromLocalFile(TESTDATA("mirror.qml")));
226 QSGBorderImage *image = qobject_cast<QSGBorderImage*>(canvas->rootObject());
230 QImage screenshot = canvas->grabFrameBuffer();
232 QImage srcPixmap(screenshot);
233 QTransform transform;
234 transform.translate(image->width(), 0).scale(-1, 1.0);
235 srcPixmap = srcPixmap.transformed(transform);
237 image->setProperty("mirror", true);
238 screenshot = canvas->grabFrameBuffer();
239 QCOMPARE(screenshot, srcPixmap);
244 void tst_qsgborderimage::tileModes()
247 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + TESTDATA("colors.png") + "\"; width: 100; height: 300; horizontalTileMode: BorderImage.Repeat; verticalTileMode: BorderImage.Repeat }";
248 QDeclarativeComponent component(&engine);
249 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
250 QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
252 QCOMPARE(obj->width(), 100.);
253 QCOMPARE(obj->height(), 300.);
254 QCOMPARE(obj->horizontalTileMode(), QSGBorderImage::Repeat);
255 QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Repeat);
260 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + TESTDATA("colors.png") + "\"; width: 300; height: 150; horizontalTileMode: BorderImage.Round; verticalTileMode: BorderImage.Round }";
261 QDeclarativeComponent component(&engine);
262 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
263 QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
265 QCOMPARE(obj->width(), 300.);
266 QCOMPARE(obj->height(), 150.);
267 QCOMPARE(obj->horizontalTileMode(), QSGBorderImage::Round);
268 QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Round);
274 void tst_qsgborderimage::sciSource()
276 QFETCH(QString, source);
279 bool remote = source.startsWith("http");
280 TestHTTPServer *server = 0;
282 server = new TestHTTPServer(SERVER_PORT);
283 QVERIFY(server->isValid());
284 server->serveDirectory(TESTDATA(""));
287 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + source + "\"; width: 300; height: 300 }";
288 QDeclarativeComponent component(&engine);
289 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
290 QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
294 QTRY_VERIFY(obj->status() == QSGBorderImage::Loading);
296 QCOMPARE(obj->source(), remote ? source : QUrl(source));
297 QCOMPARE(obj->width(), 300.);
298 QCOMPARE(obj->height(), 300.);
301 QTRY_VERIFY(obj->status() == QSGBorderImage::Ready);
302 QCOMPARE(obj->border()->left(), 10);
303 QCOMPARE(obj->border()->top(), 20);
304 QCOMPARE(obj->border()->right(), 30);
305 QCOMPARE(obj->border()->bottom(), 40);
306 QCOMPARE(obj->horizontalTileMode(), QSGBorderImage::Round);
307 QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Repeat);
309 QTRY_VERIFY(obj->status() == QSGBorderImage::Error);
316 void tst_qsgborderimage::sciSource_data()
318 QTest::addColumn<QString>("source");
319 QTest::addColumn<bool>("valid");
321 QTest::newRow("local") << QUrl::fromLocalFile(TESTDATA("colors-round.sci")).toString() << true;
322 QTest::newRow("local quoted filename") << QUrl::fromLocalFile(TESTDATA("colors-round-quotes.sci")).toString() << true;
323 QTest::newRow("local not found") << QUrl::fromLocalFile(TESTDATA("no-such-file.sci")).toString() << false;
324 QTest::newRow("remote") << SERVER_ADDR "/colors-round.sci" << true;
325 QTest::newRow("remote filename quoted") << SERVER_ADDR "/colors-round-quotes.sci" << true;
326 QTest::newRow("remote image") << SERVER_ADDR "/colors-round-remote.sci" << true;
327 QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.sci" << false;
330 void tst_qsgborderimage::invalidSciFile()
332 QTest::ignoreMessage(QtWarningMsg, "QSGGridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Roun"
333 QTest::ignoreMessage(QtWarningMsg, "QSGGridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Repea"
335 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + QUrl::fromLocalFile(TESTDATA("invalid.sci")).toString() +"\"; width: 300; height: 300 }";
336 QDeclarativeComponent component(&engine);
337 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
338 QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
340 QCOMPARE(obj->width(), 300.);
341 QCOMPARE(obj->height(), 300.);
342 QCOMPARE(obj->status(), QSGImageBase::Error);
343 QCOMPARE(obj->horizontalTileMode(), QSGBorderImage::Stretch);
344 QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Stretch);
349 void tst_qsgborderimage::pendingRemoteRequest()
351 QFETCH(QString, source);
353 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + source + "\" }";
354 QDeclarativeComponent component(&engine);
355 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
356 QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
358 QCOMPARE(obj->status(), QSGBorderImage::Loading);
361 // This will cause a delayed "QThread: Destroyed while thread is still running" warning
366 void tst_qsgborderimage::pendingRemoteRequest_data()
368 QTest::addColumn<QString>("source");
370 QTest::newRow("png file") << "http://localhost/none.png";
371 QTest::newRow("sci file") << "http://localhost/none.sci";
374 QTEST_MAIN(tst_qsgborderimage)
376 #include "tst_qsgborderimage.moc"