1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QTextDocument>
49 #include <QtQml/qqmlengine.h>
50 #include <QtQml/qqmlcomponent.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 <QtQml/qqmlcontext.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 Q_DECLARE_METATYPE(QQuickImageBase::Status)
66 class tst_qquickborderimage : public QQmlDataTest
71 tst_qquickborderimage();
77 void imageSource_data();
84 void sciSource_data();
85 void invalidSciFile();
86 void validSciFiles_data();
88 void pendingRemoteRequest();
89 void pendingRemoteRequest_data();
91 void statusChanges_data();
92 void sourceSizeChanges();
93 void progressAndStatusChanges();
99 void tst_qquickborderimage::cleanup()
102 window.releaseResources();
103 engine.clearComponentCache();
106 tst_qquickborderimage::tst_qquickborderimage()
110 void tst_qquickborderimage::noSource()
112 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"\" }";
113 QQmlComponent component(&engine);
114 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
115 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
117 QCOMPARE(obj->source(), QUrl());
118 QCOMPARE(obj->width(), 0.);
119 QCOMPARE(obj->height(), 0.);
120 QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Stretch);
121 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Stretch);
126 void tst_qquickborderimage::imageSource_data()
128 QTest::addColumn<QString>("source");
129 QTest::addColumn<bool>("remote");
130 QTest::addColumn<QString>("error");
132 QTest::newRow("local") << testFileUrl("colors.png").toString() << false << "";
133 QTest::newRow("local not found") << testFileUrl("no-such-file.png").toString() << false
134 << "file::2:1: QML BorderImage: Cannot open: " + testFileUrl("no-such-file.png").toString();
135 QTest::newRow("remote") << SERVER_ADDR "/colors.png" << true << "";
136 QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.png" << true
137 << "file::2:1: QML BorderImage: Error downloading " SERVER_ADDR "/no-such-file.png - server replied: Not found";
140 void tst_qquickborderimage::imageSource()
142 QFETCH(QString, source);
143 QFETCH(bool, remote);
144 QFETCH(QString, error);
146 TestHTTPServer *server = 0;
148 server = new TestHTTPServer(SERVER_PORT);
149 QVERIFY(server->isValid());
150 server->serveDirectory(dataDirectory());
153 if (!error.isEmpty())
154 QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
156 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + source + "\" }";
157 QQmlComponent component(&engine);
158 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
159 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
163 QTRY_VERIFY(obj->status() == QQuickBorderImage::Loading);
165 QCOMPARE(obj->source(), remote ? source : QUrl(source));
167 if (error.isEmpty()) {
168 QTRY_VERIFY(obj->status() == QQuickBorderImage::Ready);
169 QCOMPARE(obj->width(), 120.);
170 QCOMPARE(obj->height(), 120.);
171 QCOMPARE(obj->sourceSize().width(), 120);
172 QCOMPARE(obj->sourceSize().height(), 120);
173 QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Stretch);
174 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Stretch);
176 QTRY_VERIFY(obj->status() == QQuickBorderImage::Error);
183 void tst_qquickborderimage::clearSource()
185 QString componentStr = "import QtQuick 2.0\nBorderImage { source: srcImage }";
186 QQmlContext *ctxt = engine.rootContext();
187 ctxt->setContextProperty("srcImage", testFileUrl("colors.png"));
188 QQmlComponent component(&engine);
189 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
190 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
192 QVERIFY(obj->status() == QQuickBorderImage::Ready);
193 QCOMPARE(obj->width(), 120.);
194 QCOMPARE(obj->height(), 120.);
196 ctxt->setContextProperty("srcImage", "");
197 QVERIFY(obj->source().isEmpty());
198 QVERIFY(obj->status() == QQuickBorderImage::Null);
199 QCOMPARE(obj->width(), 0.);
200 QCOMPARE(obj->height(), 0.);
205 void tst_qquickborderimage::resized()
207 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + testFileUrl("colors.png").toString() + "\"; width: 300; height: 300 }";
208 QQmlComponent component(&engine);
209 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
210 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
212 QCOMPARE(obj->width(), 300.);
213 QCOMPARE(obj->height(), 300.);
214 QCOMPARE(obj->sourceSize().width(), 120);
215 QCOMPARE(obj->sourceSize().height(), 120);
216 QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Stretch);
217 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Stretch);
222 void tst_qquickborderimage::smooth()
224 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + testFile("colors.png") + "\"; smooth: true; width: 300; height: 300 }";
225 QQmlComponent component(&engine);
226 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
227 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
229 QCOMPARE(obj->width(), 300.);
230 QCOMPARE(obj->height(), 300.);
231 QCOMPARE(obj->smooth(), true);
232 QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Stretch);
233 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Stretch);
238 void tst_qquickborderimage::mirror()
240 QQuickView *window = new QQuickView;
241 window->setSource(testFileUrl("mirror.qml"));
242 QQuickBorderImage *image = qobject_cast<QQuickBorderImage*>(window->rootObject());
245 QImage screenshot = window->grabWindow();
247 QImage srcPixmap(screenshot);
248 QTransform transform;
249 transform.translate(image->width(), 0).scale(-1, 1.0);
250 srcPixmap = srcPixmap.transformed(transform);
252 image->setProperty("mirror", true);
253 screenshot = window->grabWindow();
254 QCOMPARE(screenshot, srcPixmap);
259 void tst_qquickborderimage::tileModes()
262 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + testFile("colors.png") + "\"; width: 100; height: 300; horizontalTileMode: BorderImage.Repeat; verticalTileMode: BorderImage.Repeat }";
263 QQmlComponent component(&engine);
264 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
265 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
267 QCOMPARE(obj->width(), 100.);
268 QCOMPARE(obj->height(), 300.);
269 QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Repeat);
270 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Repeat);
275 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + testFile("colors.png") + "\"; width: 300; height: 150; horizontalTileMode: BorderImage.Round; verticalTileMode: BorderImage.Round }";
276 QQmlComponent component(&engine);
277 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
278 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
280 QCOMPARE(obj->width(), 300.);
281 QCOMPARE(obj->height(), 150.);
282 QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Round);
283 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Round);
289 void tst_qquickborderimage::sciSource()
291 QFETCH(QString, source);
294 bool remote = source.startsWith("http");
295 TestHTTPServer *server = 0;
297 server = new TestHTTPServer(SERVER_PORT);
298 QVERIFY(server->isValid());
299 server->serveDirectory(dataDirectory());
302 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + source + "\"; width: 300; height: 300 }";
303 QQmlComponent component(&engine);
304 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
305 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
309 QTRY_VERIFY(obj->status() == QQuickBorderImage::Loading);
311 QCOMPARE(obj->source(), remote ? source : QUrl(source));
312 QCOMPARE(obj->width(), 300.);
313 QCOMPARE(obj->height(), 300.);
316 QTRY_VERIFY(obj->status() == QQuickBorderImage::Ready);
317 QCOMPARE(obj->border()->left(), 10);
318 QCOMPARE(obj->border()->top(), 20);
319 QCOMPARE(obj->border()->right(), 30);
320 QCOMPARE(obj->border()->bottom(), 40);
321 QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Round);
322 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Repeat);
324 QTRY_VERIFY(obj->status() == QQuickBorderImage::Error);
331 void tst_qquickborderimage::sciSource_data()
333 QTest::addColumn<QString>("source");
334 QTest::addColumn<bool>("valid");
336 QTest::newRow("local") << testFileUrl("colors-round.sci").toString() << true;
337 QTest::newRow("local quoted filename") << testFileUrl("colors-round-quotes.sci").toString() << true;
338 QTest::newRow("local not found") << testFileUrl("no-such-file.sci").toString() << false;
339 QTest::newRow("remote") << SERVER_ADDR "/colors-round.sci" << true;
340 QTest::newRow("remote filename quoted") << SERVER_ADDR "/colors-round-quotes.sci" << true;
341 QTest::newRow("remote image") << SERVER_ADDR "/colors-round-remote.sci" << true;
342 QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.sci" << false;
345 void tst_qquickborderimage::invalidSciFile()
347 QTest::ignoreMessage(QtWarningMsg, "QQuickGridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Roun"
348 QTest::ignoreMessage(QtWarningMsg, "QQuickGridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Repea"
350 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + testFileUrl("invalid.sci").toString() +"\"; width: 300; height: 300 }";
351 QQmlComponent component(&engine);
352 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
353 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
355 QCOMPARE(obj->width(), 300.);
356 QCOMPARE(obj->height(), 300.);
357 QCOMPARE(obj->status(), QQuickImageBase::Error);
358 QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Stretch);
359 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Stretch);
364 void tst_qquickborderimage::validSciFiles_data()
366 QTest::addColumn<QString>("source");
368 QTest::newRow("valid1") << testFileUrl("valid1.sci").toString();
369 QTest::newRow("valid2") << testFileUrl("valid2.sci").toString();
370 QTest::newRow("valid3") << testFileUrl("valid3.sci").toString();
371 QTest::newRow("valid4") << testFileUrl("valid4.sci").toString();
374 void tst_qquickborderimage::validSciFiles()
376 QFETCH(QString, source);
378 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + source +"\"; width: 300; height: 300 }";
379 QQmlComponent component(&engine);
380 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
381 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
383 QCOMPARE(obj->width(), 300.);
384 QCOMPARE(obj->height(), 300.);
385 QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Round);
386 QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Repeat);
391 void tst_qquickborderimage::pendingRemoteRequest()
393 QFETCH(QString, source);
395 QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + source + "\" }";
396 QQmlComponent component(&engine);
397 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
398 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
400 QCOMPARE(obj->status(), QQuickBorderImage::Loading);
403 // This will cause a delayed "QThread: Destroyed while thread is still running" warning
408 void tst_qquickborderimage::pendingRemoteRequest_data()
410 QTest::addColumn<QString>("source");
412 QTest::newRow("png file") << "http://localhost/none.png";
413 QTest::newRow("sci file") << "http://localhost/none.sci";
417 void tst_qquickborderimage::statusChanges_data()
419 QTest::addColumn<QString>("source");
420 QTest::addColumn<int>("emissions");
421 QTest::addColumn<bool>("remote");
422 QTest::addColumn<QQuickImageBase::Status>("finalStatus");
424 QTest::newRow("localfile") << testFileUrl("colors.png").toString() << 1 << false << QQuickImageBase::Ready;
425 QTest::newRow("nofile") << "" << 0 << false << QQuickImageBase::Null;
426 QTest::newRow("nonexistent") << testFileUrl("thisfiledoesnotexist.png").toString() << 1 << false << QQuickImageBase::Error;
427 QTest::newRow("noprotocol") << QString("thisfiledoesnotexisteither.png") << 2 << false << QQuickImageBase::Error;
428 QTest::newRow("remote") << "http://localhost:14446/colors.png" << 2 << true << QQuickImageBase::Ready;
431 void tst_qquickborderimage::statusChanges()
433 QFETCH(QString, source);
434 QFETCH(int, emissions);
435 QFETCH(bool, remote);
436 QFETCH(QQuickImageBase::Status, finalStatus);
438 TestHTTPServer *server = 0;
440 server = new TestHTTPServer(SERVER_PORT);
441 QVERIFY(server->isValid());
442 server->serveDirectory(dataDirectory(), TestHTTPServer::Delay);
445 QString componentStr = "import QtQuick 2.0\nBorderImage { width: 300; height: 300 }";
446 QQmlComponent component(&engine);
447 component.setData(componentStr.toLatin1(), QUrl(""));
449 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
450 qRegisterMetaType<QQuickImageBase::Status>();
451 QSignalSpy spy(obj, SIGNAL(statusChanged(QQuickImageBase::Status)));
453 obj->setSource(source);
454 QTRY_VERIFY(obj->status() == finalStatus);
455 QCOMPARE(spy.count(), emissions);
461 void tst_qquickborderimage::sourceSizeChanges()
463 TestHTTPServer server(14449);
464 QVERIFY(server.isValid());
465 server.serveDirectory(dataDirectory());
468 QQmlComponent component(&engine);
469 component.setData("import QtQuick 2.0\nBorderImage { source: srcImage }", QUrl::fromLocalFile(""));
470 QTRY_VERIFY(component.isReady());
471 QQmlContext *ctxt = engine.rootContext();
472 ctxt->setContextProperty("srcImage", "");
473 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
476 QSignalSpy sourceSizeSpy(obj, SIGNAL(sourceSizeChanged()));
479 ctxt->setContextProperty("srcImage", QUrl(""));
480 QTRY_COMPARE(obj->status(), QQuickBorderImage::Null);
481 QTRY_COMPARE(sourceSizeSpy.count(), 0);
483 ctxt->setContextProperty("srcImage", testFileUrl("heart200.png"));
484 QTRY_COMPARE(obj->status(), QQuickBorderImage::Ready);
485 QTRY_COMPARE(sourceSizeSpy.count(), 1);
487 ctxt->setContextProperty("srcImage", testFileUrl("heart200.png"));
488 QTRY_COMPARE(obj->status(), QQuickBorderImage::Ready);
489 QTRY_COMPARE(sourceSizeSpy.count(), 1);
491 ctxt->setContextProperty("srcImage", testFileUrl("heart200_copy.png"));
492 QTRY_COMPARE(obj->status(), QQuickBorderImage::Ready);
493 QTRY_COMPARE(sourceSizeSpy.count(), 1);
495 ctxt->setContextProperty("srcImage", testFileUrl("colors.png"));
496 QTRY_COMPARE(obj->status(), QQuickBorderImage::Ready);
497 QTRY_COMPARE(sourceSizeSpy.count(), 2);
499 ctxt->setContextProperty("srcImage", QUrl(""));
500 QTRY_COMPARE(obj->status(), QQuickBorderImage::Null);
501 QTRY_COMPARE(sourceSizeSpy.count(), 3);
504 ctxt->setContextProperty("srcImage", QUrl("http://127.0.0.1:14449/heart200.png"));
505 QTRY_COMPARE(obj->status(), QQuickBorderImage::Ready);
506 QTRY_COMPARE(sourceSizeSpy.count(), 4);
508 ctxt->setContextProperty("srcImage", QUrl("http://127.0.0.1:14449/heart200.png"));
509 QTRY_COMPARE(obj->status(), QQuickBorderImage::Ready);
510 QTRY_COMPARE(sourceSizeSpy.count(), 4);
512 ctxt->setContextProperty("srcImage", QUrl("http://127.0.0.1:14449/heart200_copy.png"));
513 QTRY_COMPARE(obj->status(), QQuickBorderImage::Ready);
514 QTRY_COMPARE(sourceSizeSpy.count(), 4);
516 ctxt->setContextProperty("srcImage", QUrl("http://127.0.0.1:14449/colors.png"));
517 QTRY_COMPARE(obj->status(), QQuickBorderImage::Ready);
518 QTRY_COMPARE(sourceSizeSpy.count(), 5);
520 ctxt->setContextProperty("srcImage", QUrl(""));
521 QTRY_COMPARE(obj->status(), QQuickBorderImage::Null);
522 QTRY_COMPARE(sourceSizeSpy.count(), 6);
527 void tst_qquickborderimage::progressAndStatusChanges()
529 TestHTTPServer server(14449);
530 QVERIFY(server.isValid());
531 server.serveDirectory(dataDirectory());
534 QString componentStr = "import QtQuick 2.0\nBorderImage { source: srcImage }";
535 QQmlContext *ctxt = engine.rootContext();
536 ctxt->setContextProperty("srcImage", testFileUrl("heart200.png"));
537 QQmlComponent component(&engine);
538 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
539 QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
541 QVERIFY(obj->status() == QQuickBorderImage::Ready);
542 QTRY_VERIFY(obj->progress() == 1.0);
544 qRegisterMetaType<QQuickBorderImage::Status>();
545 QSignalSpy sourceSpy(obj, SIGNAL(sourceChanged(const QUrl &)));
546 QSignalSpy progressSpy(obj, SIGNAL(progressChanged(qreal)));
547 QSignalSpy statusSpy(obj, SIGNAL(statusChanged(QQuickImageBase::Status)));
550 ctxt->setContextProperty("srcImage", testFileUrl("heart200.png"));
551 QTRY_VERIFY(obj->status() == QQuickBorderImage::Ready);
552 QTRY_VERIFY(obj->progress() == 1.0);
553 QTRY_COMPARE(sourceSpy.count(), 0);
554 QTRY_COMPARE(progressSpy.count(), 0);
555 QTRY_COMPARE(statusSpy.count(), 0);
557 // Loading local file
558 ctxt->setContextProperty("srcImage", testFileUrl("colors.png"));
559 QTRY_VERIFY(obj->status() == QQuickBorderImage::Ready);
560 QTRY_VERIFY(obj->progress() == 1.0);
561 QTRY_COMPARE(sourceSpy.count(), 1);
562 QTRY_COMPARE(progressSpy.count(), 0);
563 QTRY_COMPARE(statusSpy.count(), 1);
565 // Loading remote file
566 ctxt->setContextProperty("srcImage", "http://127.0.0.1:14449/heart200.png");
567 QTRY_VERIFY(obj->status() == QQuickBorderImage::Loading);
568 QTRY_VERIFY(obj->progress() == 0.0);
569 QTRY_VERIFY(obj->status() == QQuickBorderImage::Ready);
570 QTRY_VERIFY(obj->progress() == 1.0);
571 QTRY_COMPARE(sourceSpy.count(), 2);
572 QTRY_VERIFY(progressSpy.count() > 1);
573 QTRY_COMPARE(statusSpy.count(), 3);
575 ctxt->setContextProperty("srcImage", "");
576 QTRY_VERIFY(obj->status() == QQuickBorderImage::Null);
577 QTRY_VERIFY(obj->progress() == 0.0);
578 QTRY_COMPARE(sourceSpy.count(), 3);
579 QTRY_VERIFY(progressSpy.count() > 2);
580 QTRY_COMPARE(statusSpy.count(), 4);
585 QTEST_MAIN(tst_qquickborderimage)
587 #include "tst_qquickborderimage.moc"