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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
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"
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 QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
107 QCOMPARE(obj->source(), QUrl());
108 QCOMPARE(obj->width(), 0.);
109 QCOMPARE(obj->height(), 0.);
110 QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Stretch);
111 QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::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 QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
153 QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Loading);
155 QCOMPARE(obj->source(), remote ? source : QUrl(source));
157 if (error.isEmpty()) {
158 QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::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(), QDeclarativeBorderImage::Stretch);
164 QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Stretch);
166 QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::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 QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
182 QVERIFY(obj->status() == QDeclarativeBorderImage::Ready);
183 QCOMPARE(obj->width(), 120.);
184 QCOMPARE(obj->height(), 120.);
186 ctxt->setContextProperty("srcImage", "");
187 QVERIFY(obj->source().isEmpty());
188 QVERIFY(obj->status() == QDeclarativeBorderImage::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 QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(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(), QDeclarativeBorderImage::Stretch);
205 QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::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 QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
217 QCOMPARE(obj->width(), 300.);
218 QCOMPARE(obj->height(), 300.);
219 QCOMPARE(obj->smooth(), true);
220 QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Stretch);
221 QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::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 QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(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 QCOMPARE(screenshot, expected);
257 void tst_qdeclarativeborderimage::tileModes()
260 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; width: 100; height: 300; horizontalTileMode: BorderImage.Repeat; verticalTileMode: BorderImage.Repeat }";
261 QDeclarativeComponent component(&engine);
262 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
263 QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
265 QCOMPARE(obj->width(), 100.);
266 QCOMPARE(obj->height(), 300.);
267 QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Repeat);
268 QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Repeat);
273 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; width: 300; height: 150; horizontalTileMode: BorderImage.Round; verticalTileMode: BorderImage.Round }";
274 QDeclarativeComponent component(&engine);
275 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
276 QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
278 QCOMPARE(obj->width(), 300.);
279 QCOMPARE(obj->height(), 150.);
280 QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Round);
281 QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Round);
287 void tst_qdeclarativeborderimage::sciSource()
289 QFETCH(QString, source);
292 bool remote = source.startsWith("http");
293 TestHTTPServer *server = 0;
295 server = new TestHTTPServer(SERVER_PORT);
296 QVERIFY(server->isValid());
297 server->serveDirectory(SRCDIR "/data");
300 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\"; width: 300; height: 300 }";
301 QDeclarativeComponent component(&engine);
302 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
303 QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
307 QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Loading);
309 QCOMPARE(obj->source(), remote ? source : QUrl(source));
310 QCOMPARE(obj->width(), 300.);
311 QCOMPARE(obj->height(), 300.);
314 QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Ready);
315 QCOMPARE(obj->border()->left(), 10);
316 QCOMPARE(obj->border()->top(), 20);
317 QCOMPARE(obj->border()->right(), 30);
318 QCOMPARE(obj->border()->bottom(), 40);
319 QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Round);
320 QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Repeat);
322 QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Error);
329 void tst_qdeclarativeborderimage::sciSource_data()
331 QTest::addColumn<QString>("source");
332 QTest::addColumn<bool>("valid");
334 QTest::newRow("local") << QUrl::fromLocalFile(SRCDIR "/data/colors-round.sci").toString() << true;
335 QTest::newRow("local not found") << QUrl::fromLocalFile(SRCDIR "/data/no-such-file.sci").toString() << false;
336 QTest::newRow("remote") << SERVER_ADDR "/colors-round.sci" << true;
337 QTest::newRow("remote image") << SERVER_ADDR "/colors-round-remote.sci" << true;
338 QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.sci" << false;
341 void tst_qdeclarativeborderimage::invalidSciFile()
343 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeGridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Roun"
344 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeGridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Repea"
346 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + QUrl::fromLocalFile(SRCDIR "/data/invalid.sci").toString() +"\"; width: 300; height: 300 }";
347 QDeclarativeComponent component(&engine);
348 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
349 QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
351 QCOMPARE(obj->width(), 300.);
352 QCOMPARE(obj->height(), 300.);
353 QCOMPARE(obj->status(), QDeclarativeImageBase::Error);
354 QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Stretch);
355 QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Stretch);
360 void tst_qdeclarativeborderimage::pendingRemoteRequest()
362 QFETCH(QString, source);
364 QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\" }";
365 QDeclarativeComponent component(&engine);
366 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
367 QDeclarativeBorderImage *obj = qobject_cast<QDeclarativeBorderImage*>(component.create());
369 QCOMPARE(obj->status(), QDeclarativeBorderImage::Loading);
372 // This will cause a delayed "QThread: Destroyed while thread is still running" warning
377 void tst_qdeclarativeborderimage::pendingRemoteRequest_data()
379 QTest::addColumn<QString>("source");
381 QTest::newRow("png file") << "http://localhost/none.png";
382 QTest::newRow("sci file") << "http://localhost/none.sci";
385 void tst_qdeclarativeborderimage::testQtQuick11Attributes()
387 QFETCH(QString, code);
388 QFETCH(QString, warning);
389 QFETCH(QString, error);
391 QDeclarativeEngine engine;
394 QDeclarativeComponent valid(&engine);
395 valid.setData("import QtQuick 1.1; BorderImage { " + code.toUtf8() + " }", QUrl(""));
396 obj = valid.create();
398 QVERIFY(valid.errorString().isEmpty());
401 QDeclarativeComponent invalid(&engine);
402 invalid.setData("import QtQuick 1.0; BorderImage { " + code.toUtf8() + " }", QUrl(""));
403 QTest::ignoreMessage(QtWarningMsg, warning.toUtf8());
404 obj = invalid.create();
405 QCOMPARE(invalid.errorString(), error);
409 void tst_qdeclarativeborderimage::testQtQuick11Attributes_data()
411 QTest::addColumn<QString>("code");
412 QTest::addColumn<QString>("warning");
413 QTest::addColumn<QString>("error");
415 QTest::newRow("mirror") << "mirror: true"
416 << "QDeclarativeComponent: Component is not ready"
417 << ":1 \"BorderImage.mirror\" is not available in QtQuick 1.0.\n";
419 QTest::newRow("cache") << "cache: true"
420 << "QDeclarativeComponent: Component is not ready"
421 << ":1 \"BorderImage.cache\" is not available in QtQuick 1.0.\n";
424 QTEST_MAIN(tst_qdeclarativeborderimage)
426 #include "tst_qdeclarativeborderimage.moc"