Merge branch 'master' into refactor
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick1 / 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 ** 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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
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 "../../declarative/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     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(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(), QDeclarative1BorderImage::Stretch);
111     QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::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     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
150     QVERIFY(obj != 0);
151
152     if (remote)
153         QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Loading);
154
155     QCOMPARE(obj->source(), remote ? source : QUrl(source));
156
157     if (error.isEmpty()) {
158         QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::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(), QDeclarative1BorderImage::Stretch);
164         QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
165     } else {
166         QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::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     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
181     QVERIFY(obj != 0);
182     QVERIFY(obj->status() == QDeclarative1BorderImage::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() == QDeclarative1BorderImage::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     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(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(), QDeclarative1BorderImage::Stretch);
205     QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::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     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(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(), QDeclarative1BorderImage::Stretch);
221     QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::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     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(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     QEXPECT_FAIL("", "QTBUG-19538", Continue);
253     QCOMPARE(screenshot, expected);
254
255     delete obj;
256 }
257
258 void tst_qdeclarativeborderimage::tileModes()
259 {
260     {
261         QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; width: 100; height: 300; horizontalTileMode: BorderImage.Repeat; verticalTileMode: BorderImage.Repeat }";
262         QDeclarativeComponent component(&engine);
263         component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
264         QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
265         QVERIFY(obj != 0);
266         QCOMPARE(obj->width(), 100.);
267         QCOMPARE(obj->height(), 300.);
268         QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Repeat);
269         QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Repeat);
270
271         delete obj;
272     }
273     {
274         QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; width: 300; height: 150; horizontalTileMode: BorderImage.Round; verticalTileMode: BorderImage.Round }";
275         QDeclarativeComponent component(&engine);
276         component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
277         QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
278         QVERIFY(obj != 0);
279         QCOMPARE(obj->width(), 300.);
280         QCOMPARE(obj->height(), 150.);
281         QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Round);
282         QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Round);
283
284         delete obj;
285     }
286 }
287
288 void tst_qdeclarativeborderimage::sciSource()
289 {
290     QFETCH(QString, source);
291     QFETCH(bool, valid);
292
293     bool remote = source.startsWith("http");
294     TestHTTPServer *server = 0;
295     if (remote) {
296         server = new TestHTTPServer(SERVER_PORT);
297         QVERIFY(server->isValid());
298         server->serveDirectory(SRCDIR "/data");
299     }
300
301     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\"; width: 300; height: 300 }";
302     QDeclarativeComponent component(&engine);
303     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
304     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
305     QVERIFY(obj != 0);
306
307     if (remote)
308         QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Loading);
309
310     QCOMPARE(obj->source(), remote ? source : QUrl(source));
311     QCOMPARE(obj->width(), 300.);
312     QCOMPARE(obj->height(), 300.);
313
314     if (valid) {
315         QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Ready);
316         QCOMPARE(obj->border()->left(), 10);
317         QCOMPARE(obj->border()->top(), 20);
318         QCOMPARE(obj->border()->right(), 30);
319         QCOMPARE(obj->border()->bottom(), 40);
320         QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Round);
321         QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Repeat);
322     } else {
323         QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Error);
324     }
325
326     delete obj;
327     delete server;
328 }
329
330 void tst_qdeclarativeborderimage::sciSource_data()
331 {
332     QTest::addColumn<QString>("source");
333     QTest::addColumn<bool>("valid");
334
335     QTest::newRow("local") << QUrl::fromLocalFile(SRCDIR "/data/colors-round.sci").toString() << true;
336     QTest::newRow("local quoted filename") << QUrl::fromLocalFile(SRCDIR "/data/colors-round-quotes.sci").toString() << true;
337     QTest::newRow("local not found") << QUrl::fromLocalFile(SRCDIR "/data/no-such-file.sci").toString() << false;
338     QTest::newRow("remote") << SERVER_ADDR "/colors-round.sci" << true;
339     QTest::newRow("remote filename quoted") << SERVER_ADDR "/colors-round-quotes.sci" << true;
340     QTest::newRow("remote image") << SERVER_ADDR "/colors-round-remote.sci" << true;
341     QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.sci" << false;
342 }
343
344 void tst_qdeclarativeborderimage::invalidSciFile()
345 {
346     QTest::ignoreMessage(QtWarningMsg, "QDeclarative1GridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Roun"
347     QTest::ignoreMessage(QtWarningMsg, "QDeclarative1GridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Repea"
348
349     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + QUrl::fromLocalFile(SRCDIR "/data/invalid.sci").toString() +"\"; width: 300; height: 300 }";
350     QDeclarativeComponent component(&engine);
351     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
352     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
353     QVERIFY(obj != 0);
354     QCOMPARE(obj->width(), 300.);
355     QCOMPARE(obj->height(), 300.);
356     QCOMPARE(obj->status(), QDeclarative1ImageBase::Error);
357     QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
358     QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
359
360     delete obj;
361 }
362
363 void tst_qdeclarativeborderimage::pendingRemoteRequest()
364 {
365     QFETCH(QString, source);
366
367     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\" }";
368     QDeclarativeComponent component(&engine);
369     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
370     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
371     QVERIFY(obj != 0);
372     QCOMPARE(obj->status(), QDeclarative1BorderImage::Loading);
373
374     // verify no crash
375     // This will cause a delayed "QThread: Destroyed while thread is still running" warning
376     delete obj;
377     QTest::qWait(50);
378 }
379
380 void tst_qdeclarativeborderimage::pendingRemoteRequest_data()
381 {
382     QTest::addColumn<QString>("source");
383
384     QTest::newRow("png file") << "http://localhost/none.png";
385     QTest::newRow("sci file") << "http://localhost/none.sci";
386 }
387
388 void tst_qdeclarativeborderimage::testQtQuick11Attributes()
389 {
390     QFETCH(QString, code);
391     QFETCH(QString, warning);
392     QFETCH(QString, error);
393
394     QDeclarativeEngine engine;
395     QObject *obj;
396
397     QDeclarativeComponent valid(&engine);
398     valid.setData("import QtQuick 1.1; BorderImage { " + code.toUtf8() + " }", QUrl(""));
399     obj = valid.create();
400     QVERIFY(obj);
401     QVERIFY(valid.errorString().isEmpty());
402     delete obj;
403
404     QDeclarativeComponent invalid(&engine);
405     invalid.setData("import QtQuick 1.0; BorderImage { " + code.toUtf8() + " }", QUrl(""));
406     QTest::ignoreMessage(QtWarningMsg, warning.toUtf8());
407     obj = invalid.create();
408     QCOMPARE(invalid.errorString(), error);
409     delete obj;
410 }
411
412 void tst_qdeclarativeborderimage::testQtQuick11Attributes_data()
413 {
414     QTest::addColumn<QString>("code");
415     QTest::addColumn<QString>("warning");
416     QTest::addColumn<QString>("error");
417
418     QTest::newRow("mirror") << "mirror: true"
419         << "QDeclarativeComponent: Component is not ready"
420         << ":1 \"BorderImage.mirror\" is not available in QtQuick 1.0.\n";
421
422     QTest::newRow("cache") << "cache: true"
423         << "QDeclarativeComponent: Component is not ready"
424         << ":1 \"BorderImage.cache\" is not available in QtQuick 1.0.\n";
425 }
426
427 QTEST_MAIN(tst_qdeclarativeborderimage)
428
429 #include "tst_qdeclarativeborderimage.moc"