8e59b5b9e068f5227f2fd67db665f68226f8ecb3
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick1 / qdeclarativeborderimage / tst_qdeclarativeborderimage.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 "../../shared/testhttpserver.h"
58
59 #define SERVER_PORT 14446
60 #define SERVER_ADDR "http://127.0.0.1:14446"
61
62 class tst_qdeclarativeborderimage : public QObject
63
64 {
65     Q_OBJECT
66 public:
67     tst_qdeclarativeborderimage();
68
69 private slots:
70     void noSource();
71     void imageSource();
72     void imageSource_data();
73     void clearSource();
74     void resized();
75     void smooth();
76     void mirror();
77     void tileModes();
78     void sciSource();
79     void sciSource_data();
80     void invalidSciFile();
81     void pendingRemoteRequest();
82     void pendingRemoteRequest_data();
83     void testQtQuick11Attributes();
84     void testQtQuick11Attributes_data();
85
86 private:
87     QDeclarativeEngine engine;
88 };
89
90 tst_qdeclarativeborderimage::tst_qdeclarativeborderimage()
91 {
92 }
93
94 void tst_qdeclarativeborderimage::noSource()
95 {
96     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"\" }";
97     QDeclarativeComponent component(&engine);
98     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
99     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
100     QVERIFY(obj != 0);
101     QCOMPARE(obj->source(), QUrl());
102     QCOMPARE(obj->width(), 0.);
103     QCOMPARE(obj->height(), 0.);
104     QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
105     QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
106
107     delete obj;
108 }
109
110 void tst_qdeclarativeborderimage::imageSource_data()
111 {
112     QTest::addColumn<QString>("source");
113     QTest::addColumn<bool>("remote");
114     QTest::addColumn<QString>("error");
115
116     QTest::newRow("local") << QUrl::fromLocalFile(SRCDIR "/data/colors.png").toString() << false << "";
117     QTest::newRow("local not found") << QUrl::fromLocalFile(SRCDIR "/data/no-such-file.png").toString() << false
118         << "file::2:1: QML BorderImage: Cannot open: " + QUrl::fromLocalFile(SRCDIR "/data/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";
122 }
123
124 void tst_qdeclarativeborderimage::imageSource()
125 {
126     QFETCH(QString, source);
127     QFETCH(bool, remote);
128     QFETCH(QString, error);
129
130     TestHTTPServer *server = 0;
131     if (remote) {
132         server = new TestHTTPServer(SERVER_PORT);
133         QVERIFY(server->isValid());
134         server->serveDirectory(SRCDIR "/data");
135     }
136
137     if (!error.isEmpty())
138         QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
139
140     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\" }";
141     QDeclarativeComponent component(&engine);
142     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
143     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
144     QVERIFY(obj != 0);
145
146     if (remote)
147         QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Loading);
148
149     QCOMPARE(obj->source(), remote ? source : QUrl(source));
150
151     if (error.isEmpty()) {
152         QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::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(), QDeclarative1BorderImage::Stretch);
158         QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
159     } else {
160         QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Error);
161     }
162
163     delete obj;
164     delete server;
165 }
166
167 void tst_qdeclarativeborderimage::clearSource()
168 {
169     QString componentStr = "import QtQuick 1.0\nBorderImage { source: srcImage }";
170     QDeclarativeContext *ctxt = engine.rootContext();
171     ctxt->setContextProperty("srcImage", QUrl::fromLocalFile(SRCDIR "/data/colors.png"));
172     QDeclarativeComponent component(&engine);
173     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
174     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
175     QVERIFY(obj != 0);
176     QVERIFY(obj->status() == QDeclarative1BorderImage::Ready);
177     QCOMPARE(obj->width(), 120.);
178     QCOMPARE(obj->height(), 120.);
179
180     ctxt->setContextProperty("srcImage", "");
181     QVERIFY(obj->source().isEmpty());
182     QVERIFY(obj->status() == QDeclarative1BorderImage::Null);
183     QCOMPARE(obj->width(), 0.);
184     QCOMPARE(obj->height(), 0.);
185 }
186
187 void tst_qdeclarativeborderimage::resized()
188 {
189     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + QUrl::fromLocalFile(SRCDIR "/data/colors.png").toString() + "\"; width: 300; height: 300 }";
190     QDeclarativeComponent component(&engine);
191     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
192     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
193     QVERIFY(obj != 0);
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(), QDeclarative1BorderImage::Stretch);
199     QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
200
201     delete obj;
202 }
203
204 void tst_qdeclarativeborderimage::smooth()
205 {
206     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; smooth: true; width: 300; height: 300 }";
207     QDeclarativeComponent component(&engine);
208     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
209     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
210     QVERIFY(obj != 0);
211     QCOMPARE(obj->width(), 300.);
212     QCOMPARE(obj->height(), 300.);
213     QCOMPARE(obj->smooth(), true);
214     QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
215     QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
216
217     delete obj;
218 }
219
220 void tst_qdeclarativeborderimage::mirror()
221 {
222     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 } }";
223     QDeclarativeComponent component(&engine);
224     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
225     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
226     QVERIFY(obj != 0);
227
228     int width = obj->property("width").toInt();
229     int height = obj->property("height").toInt();
230
231     QGraphicsScene scene;
232     scene.addItem(qobject_cast<QGraphicsObject *>(obj));
233     QPixmap screenshot(width, height);
234     screenshot.fill();
235     QPainter p_screenshot(&screenshot);
236     scene.render(&p_screenshot, QRect(0, 0, width, height), QRect(0, 0, width, height));
237
238     QTransform transform;
239     transform.translate(width, 0).scale(-1, 1.0);
240     QPixmap expected = screenshot.transformed(transform);
241
242     obj->setProperty("mirror", true);
243     p_screenshot.fillRect(QRect(0, 0, width, height), Qt::white);
244     scene.render(&p_screenshot, QRect(0, 0, width, height), QRect(0, 0, width, height));
245
246     QEXPECT_FAIL("", "QTBUG-19538", Continue);
247     QCOMPARE(screenshot, expected);
248
249     delete obj;
250 }
251
252 void tst_qdeclarativeborderimage::tileModes()
253 {
254     {
255         QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; width: 100; height: 300; horizontalTileMode: BorderImage.Repeat; verticalTileMode: BorderImage.Repeat }";
256         QDeclarativeComponent component(&engine);
257         component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
258         QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
259         QVERIFY(obj != 0);
260         QCOMPARE(obj->width(), 100.);
261         QCOMPARE(obj->height(), 300.);
262         QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Repeat);
263         QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Repeat);
264
265         delete obj;
266     }
267     {
268         QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" SRCDIR "/data/colors.png\"; width: 300; height: 150; horizontalTileMode: BorderImage.Round; verticalTileMode: BorderImage.Round }";
269         QDeclarativeComponent component(&engine);
270         component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
271         QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
272         QVERIFY(obj != 0);
273         QCOMPARE(obj->width(), 300.);
274         QCOMPARE(obj->height(), 150.);
275         QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Round);
276         QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Round);
277
278         delete obj;
279     }
280 }
281
282 void tst_qdeclarativeborderimage::sciSource()
283 {
284     QFETCH(QString, source);
285     QFETCH(bool, valid);
286
287     bool remote = source.startsWith("http");
288     TestHTTPServer *server = 0;
289     if (remote) {
290         server = new TestHTTPServer(SERVER_PORT);
291         QVERIFY(server->isValid());
292         server->serveDirectory(SRCDIR "/data");
293     }
294
295     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\"; width: 300; height: 300 }";
296     QDeclarativeComponent component(&engine);
297     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
298     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
299     QVERIFY(obj != 0);
300
301     if (remote)
302         QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Loading);
303
304     QCOMPARE(obj->source(), remote ? source : QUrl(source));
305     QCOMPARE(obj->width(), 300.);
306     QCOMPARE(obj->height(), 300.);
307
308     if (valid) {
309         QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Ready);
310         QCOMPARE(obj->border()->left(), 10);
311         QCOMPARE(obj->border()->top(), 20);
312         QCOMPARE(obj->border()->right(), 30);
313         QCOMPARE(obj->border()->bottom(), 40);
314         QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Round);
315         QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Repeat);
316     } else {
317         QTRY_VERIFY(obj->status() == QDeclarative1BorderImage::Error);
318     }
319
320     delete obj;
321     delete server;
322 }
323
324 void tst_qdeclarativeborderimage::sciSource_data()
325 {
326     QTest::addColumn<QString>("source");
327     QTest::addColumn<bool>("valid");
328
329     QTest::newRow("local") << QUrl::fromLocalFile(SRCDIR "/data/colors-round.sci").toString() << true;
330     QTest::newRow("local quoted filename") << QUrl::fromLocalFile(SRCDIR "/data/colors-round-quotes.sci").toString() << true;
331     QTest::newRow("local not found") << QUrl::fromLocalFile(SRCDIR "/data/no-such-file.sci").toString() << false;
332     QTest::newRow("remote") << SERVER_ADDR "/colors-round.sci" << true;
333     QTest::newRow("remote filename quoted") << SERVER_ADDR "/colors-round-quotes.sci" << true;
334     QTest::newRow("remote image") << SERVER_ADDR "/colors-round-remote.sci" << true;
335     QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.sci" << false;
336 }
337
338 void tst_qdeclarativeborderimage::invalidSciFile()
339 {
340     QTest::ignoreMessage(QtWarningMsg, "QDeclarative1GridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Roun"
341     QTest::ignoreMessage(QtWarningMsg, "QDeclarative1GridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Repea"
342
343     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + QUrl::fromLocalFile(SRCDIR "/data/invalid.sci").toString() +"\"; width: 300; height: 300 }";
344     QDeclarativeComponent component(&engine);
345     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
346     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
347     QVERIFY(obj != 0);
348     QCOMPARE(obj->width(), 300.);
349     QCOMPARE(obj->height(), 300.);
350     QCOMPARE(obj->status(), QDeclarative1ImageBase::Error);
351     QCOMPARE(obj->horizontalTileMode(), QDeclarative1BorderImage::Stretch);
352     QCOMPARE(obj->verticalTileMode(), QDeclarative1BorderImage::Stretch);
353
354     delete obj;
355 }
356
357 void tst_qdeclarativeborderimage::pendingRemoteRequest()
358 {
359     QFETCH(QString, source);
360
361     QString componentStr = "import QtQuick 1.0\nBorderImage { source: \"" + source + "\" }";
362     QDeclarativeComponent component(&engine);
363     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
364     QDeclarative1BorderImage *obj = qobject_cast<QDeclarative1BorderImage*>(component.create());
365     QVERIFY(obj != 0);
366     QCOMPARE(obj->status(), QDeclarative1BorderImage::Loading);
367
368     // verify no crash
369     // This will cause a delayed "QThread: Destroyed while thread is still running" warning
370     delete obj;
371     QTest::qWait(50);
372 }
373
374 void tst_qdeclarativeborderimage::pendingRemoteRequest_data()
375 {
376     QTest::addColumn<QString>("source");
377
378     QTest::newRow("png file") << "http://localhost/none.png";
379     QTest::newRow("sci file") << "http://localhost/none.sci";
380 }
381
382 void tst_qdeclarativeborderimage::testQtQuick11Attributes()
383 {
384     QFETCH(QString, code);
385     QFETCH(QString, warning);
386     QFETCH(QString, error);
387
388     QDeclarativeEngine engine;
389     QObject *obj;
390
391     QDeclarativeComponent valid(&engine);
392     valid.setData("import QtQuick 1.1; BorderImage { " + code.toUtf8() + " }", QUrl(""));
393     obj = valid.create();
394     QVERIFY(obj);
395     QVERIFY(valid.errorString().isEmpty());
396     delete obj;
397
398     QDeclarativeComponent invalid(&engine);
399     invalid.setData("import QtQuick 1.0; BorderImage { " + code.toUtf8() + " }", QUrl(""));
400     QTest::ignoreMessage(QtWarningMsg, warning.toUtf8());
401     obj = invalid.create();
402     QCOMPARE(invalid.errorString(), error);
403     delete obj;
404 }
405
406 void tst_qdeclarativeborderimage::testQtQuick11Attributes_data()
407 {
408     QTest::addColumn<QString>("code");
409     QTest::addColumn<QString>("warning");
410     QTest::addColumn<QString>("error");
411
412     QTest::newRow("mirror") << "mirror: true"
413         << "QDeclarativeComponent: Component is not ready"
414         << ":1 \"BorderImage.mirror\" is not available in QtQuick 1.0.\n";
415
416     QTest::newRow("cache") << "cache: true"
417         << "QDeclarativeComponent: Component is not ready"
418         << ":1 \"BorderImage.cache\" is not available in QtQuick 1.0.\n";
419 }
420
421 QTEST_MAIN(tst_qdeclarativeborderimage)
422
423 #include "tst_qdeclarativeborderimage.moc"