1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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/qquickborderimage_p.h>
52 #include <private/qquickimagebase_p.h>
53 #include <private/qquickscalegrid_p_p.h>
54 #include <private/qquickloader_p.h>
55 #include <QtQuick/qquickview.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_qquickborderimage : public QDeclarativeDataTest
69 tst_qquickborderimage();
74 void imageSource_data();
81 void sciSource_data();
82 void invalidSciFile();
83 void pendingRemoteRequest();
84 void pendingRemoteRequest_data();
87 QDeclarativeEngine engine;
90 tst_qquickborderimage::tst_qquickborderimage()
94 void tst_qquickborderimage::noSource()
96 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"\" }";
97 QDeclarativeComponent component(&engine);
98 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
99 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
101 QCOMPARE(obj->source(), QUrl());
102 QCOMPARE(obj->width(), 0.);
103 QCOMPARE(obj->height(), 0.);
104 QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Stretch);
105 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Stretch);
110 void tst_qquickborderimage::imageSource_data()
112 QTest::addColumn<QString>("source");
113 QTest::addColumn<bool>("remote");
114 QTest::addColumn<QString>("error");
116 QTest::newRow("local") << testFileUrl("colors.png").toString() << false << "";
117 QTest::newRow("local not found") << testFileUrl("no-such-file.png").toString() << false
118 << "file::2:1: QML BorderImage: Cannot open: " + testFileUrl("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_qquickborderimage::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(dataDirectory());
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 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
147 QTRY_VERIFY(obj->status() == QQuickBorderImage::Loading);
149 QCOMPARE(obj->source(), remote ? source : QUrl(source));
151 if (error.isEmpty()) {
152 QTRY_VERIFY(obj->status() == QQuickBorderImage::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(), QQuickBorderImage::Stretch);
158 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Stretch);
160 QTRY_VERIFY(obj->status() == QQuickBorderImage::Error);
167 void tst_qquickborderimage::clearSource()
169 QString componentStr = "import QtQuick 2.0\nBorderImage { source: srcImage }";
170 QDeclarativeContext *ctxt = engine.rootContext();
171 ctxt->setContextProperty("srcImage", testFileUrl("colors.png"));
172 QDeclarativeComponent component(&engine);
173 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
174 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
176 QVERIFY(obj->status() == QQuickBorderImage::Ready);
177 QCOMPARE(obj->width(), 120.);
178 QCOMPARE(obj->height(), 120.);
180 ctxt->setContextProperty("srcImage", "");
181 QVERIFY(obj->source().isEmpty());
182 QVERIFY(obj->status() == QQuickBorderImage::Null);
183 QCOMPARE(obj->width(), 0.);
184 QCOMPARE(obj->height(), 0.);
187 void tst_qquickborderimage::resized()
189 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + testFileUrl("colors.png").toString() + "\"; width: 300; height: 300 }";
190 QDeclarativeComponent component(&engine);
191 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
192 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(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(), QQuickBorderImage::Stretch);
199 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Stretch);
204 void tst_qquickborderimage::smooth()
206 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + testFile("colors.png") + "\"; smooth: true; width: 300; height: 300 }";
207 QDeclarativeComponent component(&engine);
208 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
209 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
211 QCOMPARE(obj->width(), 300.);
212 QCOMPARE(obj->height(), 300.);
213 QCOMPARE(obj->smooth(), true);
214 QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Stretch);
215 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Stretch);
220 void tst_qquickborderimage::mirror()
222 QQuickView *canvas = new QQuickView;
223 canvas->setSource(testFileUrl("mirror.qml"));
224 QQuickBorderImage *image = qobject_cast<QQuickBorderImage*>(canvas->rootObject());
227 QImage screenshot = canvas->grabFrameBuffer();
229 QImage srcPixmap(screenshot);
230 QTransform transform;
231 transform.translate(image->width(), 0).scale(-1, 1.0);
232 srcPixmap = srcPixmap.transformed(transform);
234 image->setProperty("mirror", true);
235 screenshot = canvas->grabFrameBuffer();
236 QCOMPARE(screenshot, srcPixmap);
241 void tst_qquickborderimage::tileModes()
244 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + testFile("colors.png") + "\"; width: 100; height: 300; horizontalTileMode: BorderImage.Repeat; verticalTileMode: BorderImage.Repeat }";
245 QDeclarativeComponent component(&engine);
246 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
247 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
249 QCOMPARE(obj->width(), 100.);
250 QCOMPARE(obj->height(), 300.);
251 QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Repeat);
252 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Repeat);
257 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + testFile("colors.png") + "\"; width: 300; height: 150; horizontalTileMode: BorderImage.Round; verticalTileMode: BorderImage.Round }";
258 QDeclarativeComponent component(&engine);
259 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
260 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
262 QCOMPARE(obj->width(), 300.);
263 QCOMPARE(obj->height(), 150.);
264 QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Round);
265 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Round);
271 void tst_qquickborderimage::sciSource()
273 QFETCH(QString, source);
276 bool remote = source.startsWith("http");
277 TestHTTPServer *server = 0;
279 server = new TestHTTPServer(SERVER_PORT);
280 QVERIFY(server->isValid());
281 server->serveDirectory(dataDirectory());
284 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + source + "\"; width: 300; height: 300 }";
285 QDeclarativeComponent component(&engine);
286 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
287 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
291 QTRY_VERIFY(obj->status() == QQuickBorderImage::Loading);
293 QCOMPARE(obj->source(), remote ? source : QUrl(source));
294 QCOMPARE(obj->width(), 300.);
295 QCOMPARE(obj->height(), 300.);
298 QTRY_VERIFY(obj->status() == QQuickBorderImage::Ready);
299 QCOMPARE(obj->border()->left(), 10);
300 QCOMPARE(obj->border()->top(), 20);
301 QCOMPARE(obj->border()->right(), 30);
302 QCOMPARE(obj->border()->bottom(), 40);
303 QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Round);
304 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Repeat);
306 QTRY_VERIFY(obj->status() == QQuickBorderImage::Error);
313 void tst_qquickborderimage::sciSource_data()
315 QTest::addColumn<QString>("source");
316 QTest::addColumn<bool>("valid");
318 QTest::newRow("local") << testFileUrl("colors-round.sci").toString() << true;
319 QTest::newRow("local quoted filename") << testFileUrl("colors-round-quotes.sci").toString() << true;
320 QTest::newRow("local not found") << testFileUrl("no-such-file.sci").toString() << false;
321 QTest::newRow("remote") << SERVER_ADDR "/colors-round.sci" << true;
322 QTest::newRow("remote filename quoted") << SERVER_ADDR "/colors-round-quotes.sci" << true;
323 QTest::newRow("remote image") << SERVER_ADDR "/colors-round-remote.sci" << true;
324 QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.sci" << false;
327 void tst_qquickborderimage::invalidSciFile()
329 QTest::ignoreMessage(QtWarningMsg, "QQuickGridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Roun"
330 QTest::ignoreMessage(QtWarningMsg, "QQuickGridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Repea"
332 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + testFileUrl("invalid.sci").toString() +"\"; width: 300; height: 300 }";
333 QDeclarativeComponent component(&engine);
334 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
335 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
337 QCOMPARE(obj->width(), 300.);
338 QCOMPARE(obj->height(), 300.);
339 QCOMPARE(obj->status(), QQuickImageBase::Error);
340 QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Stretch);
341 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Stretch);
346 void tst_qquickborderimage::pendingRemoteRequest()
348 QFETCH(QString, source);
350 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + source + "\" }";
351 QDeclarativeComponent component(&engine);
352 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
353 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
355 QCOMPARE(obj->status(), QQuickBorderImage::Loading);
358 // This will cause a delayed "QThread: Destroyed while thread is still running" warning
363 void tst_qquickborderimage::pendingRemoteRequest_data()
365 QTest::addColumn<QString>("source");
367 QTest::newRow("png file") << "http://localhost/none.png";
368 QTest::newRow("sci file") << "http://localhost/none.sci";
371 QTEST_MAIN(tst_qquickborderimage)
373 #include "tst_qquickborderimage.moc"