Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativeborderimage / tst_qdeclarativeborderimage.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
42 #include <QTextDocument>
43 #include <QTcpServer>
44 #include <QTcpSocket>
45 #include <QDir>
46 #include <QGraphicsScene>
47 #include <QPainter>
48
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>
56
57 #include "../shared/testhttpserver.h"
58 #include "../../../shared/util.h"
59
60 #ifdef Q_OS_SYMBIAN
61 // In Symbian OS test data is located in applications private dir
62 #define SRCDIR "."
63 #endif
64
65 #define SERVER_PORT 14446
66 #define SERVER_ADDR "http://127.0.0.1:14446"
67
68 class tst_qdeclarativeborderimage : public QObject
69
70 {
71     Q_OBJECT
72 public:
73     tst_qdeclarativeborderimage();
74
75 private slots:
76     void noSource();
77     void imageSource();
78     void imageSource_data();
79     void clearSource();
80     void resized();
81     void smooth();
82     void mirror();
83     void tileModes();
84     void sciSource();
85     void sciSource_data();
86     void invalidSciFile();
87     void pendingRemoteRequest();
88     void pendingRemoteRequest_data();
89     void testQtQuick11Attributes();
90     void testQtQuick11Attributes_data();
91
92 private:
93     QDeclarativeEngine engine;
94 };
95
96 tst_qdeclarativeborderimage::tst_qdeclarativeborderimage()
97 {
98 }
99
100 void tst_qdeclarativeborderimage::noSource()
101 {
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());
106     QVERIFY(obj != 0);
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);
112
113     delete obj;
114 }
115
116 void tst_qdeclarativeborderimage::imageSource_data()
117 {
118     QTest::addColumn<QString>("source");
119     QTest::addColumn<bool>("remote");
120     QTest::addColumn<QString>("error");
121
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";
128 }
129
130 void tst_qdeclarativeborderimage::imageSource()
131 {
132     QFETCH(QString, source);
133     QFETCH(bool, remote);
134     QFETCH(QString, error);
135
136     TestHTTPServer *server = 0;
137     if (remote) {
138         server = new TestHTTPServer(SERVER_PORT);
139         QVERIFY(server->isValid());
140         server->serveDirectory(SRCDIR "/data");
141     }
142
143     if (!error.isEmpty())
144         QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
145
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());
150     QVERIFY(obj != 0);
151
152     if (remote)
153         QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Loading);
154
155     QCOMPARE(obj->source(), remote ? source : QUrl(source));
156
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);
165     } else {
166         QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Error);
167     }
168
169     delete obj;
170     delete server;
171 }
172
173 void tst_qdeclarativeborderimage::clearSource()
174 {
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());
181     QVERIFY(obj != 0);
182     QVERIFY(obj->status() == QDeclarativeBorderImage::Ready);
183     QCOMPARE(obj->width(), 120.);
184     QCOMPARE(obj->height(), 120.);
185
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.);
191 }
192
193 void tst_qdeclarativeborderimage::resized()
194 {
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());
199     QVERIFY(obj != 0);
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);
206
207     delete obj;
208 }
209
210 void tst_qdeclarativeborderimage::smooth()
211 {
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());
216     QVERIFY(obj != 0);
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);
222
223     delete obj;
224 }
225
226 void tst_qdeclarativeborderimage::mirror()
227 {
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());
232     QVERIFY(obj != 0);
233
234     int width = obj->property("width").toInt();
235     int height = obj->property("height").toInt();
236
237     QGraphicsScene scene;
238     scene.addItem(qobject_cast<QGraphicsObject *>(obj));
239     QPixmap screenshot(width, height);
240     screenshot.fill();
241     QPainter p_screenshot(&screenshot);
242     scene.render(&p_screenshot, QRect(0, 0, width, height), QRect(0, 0, width, height));
243
244     QTransform transform;
245     transform.translate(width, 0).scale(-1, 1.0);
246     QPixmap expected = screenshot.transformed(transform);
247
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));
251
252     QCOMPARE(screenshot, expected);
253
254     delete obj;
255 }
256
257 void tst_qdeclarativeborderimage::tileModes()
258 {
259     {
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());
264         QVERIFY(obj != 0);
265         QCOMPARE(obj->width(), 100.);
266         QCOMPARE(obj->height(), 300.);
267         QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Repeat);
268         QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Repeat);
269
270         delete obj;
271     }
272     {
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());
277         QVERIFY(obj != 0);
278         QCOMPARE(obj->width(), 300.);
279         QCOMPARE(obj->height(), 150.);
280         QCOMPARE(obj->horizontalTileMode(), QDeclarativeBorderImage::Round);
281         QCOMPARE(obj->verticalTileMode(), QDeclarativeBorderImage::Round);
282
283         delete obj;
284     }
285 }
286
287 void tst_qdeclarativeborderimage::sciSource()
288 {
289     QFETCH(QString, source);
290     QFETCH(bool, valid);
291
292     bool remote = source.startsWith("http");
293     TestHTTPServer *server = 0;
294     if (remote) {
295         server = new TestHTTPServer(SERVER_PORT);
296         QVERIFY(server->isValid());
297         server->serveDirectory(SRCDIR "/data");
298     }
299
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());
304     QVERIFY(obj != 0);
305
306     if (remote)
307         QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Loading);
308
309     QCOMPARE(obj->source(), remote ? source : QUrl(source));
310     QCOMPARE(obj->width(), 300.);
311     QCOMPARE(obj->height(), 300.);
312
313     if (valid) {
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);
321     } else {
322         QTRY_VERIFY(obj->status() == QDeclarativeBorderImage::Error);
323     }
324
325     delete obj;
326     delete server;
327 }
328
329 void tst_qdeclarativeborderimage::sciSource_data()
330 {
331     QTest::addColumn<QString>("source");
332     QTest::addColumn<bool>("valid");
333
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;
339 }
340
341 void tst_qdeclarativeborderimage::invalidSciFile()
342 {
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"
345
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());
350     QVERIFY(obj != 0);
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);
356
357     delete obj;
358 }
359
360 void tst_qdeclarativeborderimage::pendingRemoteRequest()
361 {
362     QFETCH(QString, source);
363
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());
368     QVERIFY(obj != 0);
369     QCOMPARE(obj->status(), QDeclarativeBorderImage::Loading);
370
371     // verify no crash
372     // This will cause a delayed "QThread: Destroyed while thread is still running" warning
373     delete obj;
374     QTest::qWait(50);
375 }
376
377 void tst_qdeclarativeborderimage::pendingRemoteRequest_data()
378 {
379     QTest::addColumn<QString>("source");
380
381     QTest::newRow("png file") << "http://localhost/none.png";
382     QTest::newRow("sci file") << "http://localhost/none.sci";
383 }
384
385 void tst_qdeclarativeborderimage::testQtQuick11Attributes()
386 {
387     QFETCH(QString, code);
388     QFETCH(QString, warning);
389     QFETCH(QString, error);
390
391     QDeclarativeEngine engine;
392     QObject *obj;
393
394     QDeclarativeComponent valid(&engine);
395     valid.setData("import QtQuick 1.1; BorderImage { " + code.toUtf8() + " }", QUrl(""));
396     obj = valid.create();
397     QVERIFY(obj);
398     QVERIFY(valid.errorString().isEmpty());
399     delete obj;
400
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);
406     delete obj;
407 }
408
409 void tst_qdeclarativeborderimage::testQtQuick11Attributes_data()
410 {
411     QTest::addColumn<QString>("code");
412     QTest::addColumn<QString>("warning");
413     QTest::addColumn<QString>("error");
414
415     QTest::newRow("mirror") << "mirror: true"
416         << "QDeclarativeComponent: Component is not ready"
417         << ":1 \"BorderImage.mirror\" is not available in QtQuick 1.0.\n";
418
419     QTest::newRow("cache") << "cache: true"
420         << "QDeclarativeComponent: Component is not ready"
421         << ":1 \"BorderImage.cache\" is not available in QtQuick 1.0.\n";
422 }
423
424 QTEST_MAIN(tst_qdeclarativeborderimage)
425
426 #include "tst_qdeclarativeborderimage.moc"