Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick2 / qquickborderimage / tst_qquickborderimage.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
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/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 <QtDeclarative/qdeclarativecontext.h>
57
58 #include "../../shared/testhttpserver.h"
59 #include "../../shared/util.h"
60
61 #define SERVER_PORT 14446
62 #define SERVER_ADDR "http://127.0.0.1:14446"
63
64 class tst_qquickborderimage : public QDeclarativeDataTest
65
66 {
67     Q_OBJECT
68 public:
69     tst_qquickborderimage();
70
71 private slots:
72     void noSource();
73     void imageSource();
74     void imageSource_data();
75     void clearSource();
76     void resized();
77     void smooth();
78     void mirror();
79     void tileModes();
80     void sciSource();
81     void sciSource_data();
82     void invalidSciFile();
83     void pendingRemoteRequest();
84     void pendingRemoteRequest_data();
85
86 private:
87     QDeclarativeEngine engine;
88 };
89
90 tst_qquickborderimage::tst_qquickborderimage()
91 {
92 }
93
94 void tst_qquickborderimage::noSource()
95 {
96     QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"\" }";
97     QDeclarativeComponent component(&engine);
98     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
99     QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(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(), QQuickBorderImage::Stretch);
105     QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Stretch);
106
107     delete obj;
108 }
109
110 void tst_qquickborderimage::imageSource_data()
111 {
112     QTest::addColumn<QString>("source");
113     QTest::addColumn<bool>("remote");
114     QTest::addColumn<QString>("error");
115
116     QTest::newRow("local") << testFileUrl("colors.png").toString() << false << "";
117     QTest::newRow("local not found") << testFileUrl("no-such-file.png").toString() << false
118         << "file::2:1: QML BorderImage: Cannot open: " + testFileUrl("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_qquickborderimage::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(dataDirectory());
135     }
136
137     if (!error.isEmpty())
138         QTest::ignoreMessage(QtWarningMsg, error.toUtf8());
139
140     QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + source + "\" }";
141     QDeclarativeComponent component(&engine);
142     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
143     QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
144     QVERIFY(obj != 0);
145
146     if (remote)
147         QTRY_VERIFY(obj->status() == QQuickBorderImage::Loading);
148
149     QCOMPARE(obj->source(), remote ? source : QUrl(source));
150
151     if (error.isEmpty()) {
152         QTRY_VERIFY(obj->status() == QQuickBorderImage::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(), QQuickBorderImage::Stretch);
158         QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Stretch);
159     } else {
160         QTRY_VERIFY(obj->status() == QQuickBorderImage::Error);
161     }
162
163     delete obj;
164     delete server;
165 }
166
167 void tst_qquickborderimage::clearSource()
168 {
169     QString componentStr = "import QtQuick 2.0\nBorderImage { source: srcImage }";
170     QDeclarativeContext *ctxt = engine.rootContext();
171     ctxt->setContextProperty("srcImage", testFileUrl("colors.png"));
172     QDeclarativeComponent component(&engine);
173     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
174     QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
175     QVERIFY(obj != 0);
176     QVERIFY(obj->status() == QQuickBorderImage::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() == QQuickBorderImage::Null);
183     QCOMPARE(obj->width(), 0.);
184     QCOMPARE(obj->height(), 0.);
185 }
186
187 void tst_qquickborderimage::resized()
188 {
189     QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + testFileUrl("colors.png").toString() + "\"; width: 300; height: 300 }";
190     QDeclarativeComponent component(&engine);
191     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
192     QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(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(), QQuickBorderImage::Stretch);
199     QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Stretch);
200
201     delete obj;
202 }
203
204 void tst_qquickborderimage::smooth()
205 {
206     QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + testFile("colors.png") + "\"; smooth: true; width: 300; height: 300 }";
207     QDeclarativeComponent component(&engine);
208     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
209     QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(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(), QQuickBorderImage::Stretch);
215     QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Stretch);
216
217     delete obj;
218 }
219
220 void tst_qquickborderimage::mirror()
221 {
222     QQuickView *canvas = new QQuickView;
223     canvas->setSource(testFileUrl("mirror.qml"));
224     QQuickBorderImage *image = qobject_cast<QQuickBorderImage*>(canvas->rootObject());
225     QVERIFY(image != 0);
226
227     QImage screenshot = canvas->grabFrameBuffer();
228
229     QImage srcPixmap(screenshot);
230     QTransform transform;
231     transform.translate(image->width(), 0).scale(-1, 1.0);
232     srcPixmap = srcPixmap.transformed(transform);
233
234     image->setProperty("mirror", true);
235     screenshot = canvas->grabFrameBuffer();
236     QCOMPARE(screenshot, srcPixmap);
237
238     delete canvas;
239 }
240
241 void tst_qquickborderimage::tileModes()
242 {
243     {
244         QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + testFile("colors.png") + "\"; width: 100; height: 300; horizontalTileMode: BorderImage.Repeat; verticalTileMode: BorderImage.Repeat }";
245         QDeclarativeComponent component(&engine);
246         component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
247         QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
248         QVERIFY(obj != 0);
249         QCOMPARE(obj->width(), 100.);
250         QCOMPARE(obj->height(), 300.);
251         QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Repeat);
252         QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Repeat);
253
254         delete obj;
255     }
256     {
257         QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + testFile("colors.png") + "\"; width: 300; height: 150; horizontalTileMode: BorderImage.Round; verticalTileMode: BorderImage.Round }";
258         QDeclarativeComponent component(&engine);
259         component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
260         QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
261         QVERIFY(obj != 0);
262         QCOMPARE(obj->width(), 300.);
263         QCOMPARE(obj->height(), 150.);
264         QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Round);
265         QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Round);
266
267         delete obj;
268     }
269 }
270
271 void tst_qquickborderimage::sciSource()
272 {
273     QFETCH(QString, source);
274     QFETCH(bool, valid);
275
276     bool remote = source.startsWith("http");
277     TestHTTPServer *server = 0;
278     if (remote) {
279         server = new TestHTTPServer(SERVER_PORT);
280         QVERIFY(server->isValid());
281         server->serveDirectory(dataDirectory());
282     }
283
284     QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + source + "\"; width: 300; height: 300 }";
285     QDeclarativeComponent component(&engine);
286     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
287     QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
288     QVERIFY(obj != 0);
289
290     if (remote)
291         QTRY_VERIFY(obj->status() == QQuickBorderImage::Loading);
292
293     QCOMPARE(obj->source(), remote ? source : QUrl(source));
294     QCOMPARE(obj->width(), 300.);
295     QCOMPARE(obj->height(), 300.);
296
297     if (valid) {
298         QTRY_VERIFY(obj->status() == QQuickBorderImage::Ready);
299         QCOMPARE(obj->border()->left(), 10);
300         QCOMPARE(obj->border()->top(), 20);
301         QCOMPARE(obj->border()->right(), 30);
302         QCOMPARE(obj->border()->bottom(), 40);
303         QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Round);
304         QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Repeat);
305     } else {
306         QTRY_VERIFY(obj->status() == QQuickBorderImage::Error);
307     }
308
309     delete obj;
310     delete server;
311 }
312
313 void tst_qquickborderimage::sciSource_data()
314 {
315     QTest::addColumn<QString>("source");
316     QTest::addColumn<bool>("valid");
317
318     QTest::newRow("local") << testFileUrl("colors-round.sci").toString() << true;
319     QTest::newRow("local quoted filename") << testFileUrl("colors-round-quotes.sci").toString() << true;
320     QTest::newRow("local not found") << testFileUrl("no-such-file.sci").toString() << false;
321     QTest::newRow("remote") << SERVER_ADDR "/colors-round.sci" << true;
322     QTest::newRow("remote filename quoted") << SERVER_ADDR "/colors-round-quotes.sci" << true;
323     QTest::newRow("remote image") << SERVER_ADDR "/colors-round-remote.sci" << true;
324     QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.sci" << false;
325 }
326
327 void tst_qquickborderimage::invalidSciFile()
328 {
329     QTest::ignoreMessage(QtWarningMsg, "QQuickGridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Roun"
330     QTest::ignoreMessage(QtWarningMsg, "QQuickGridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Repea"
331
332     QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + testFileUrl("invalid.sci").toString() +"\"; width: 300; height: 300 }";
333     QDeclarativeComponent component(&engine);
334     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
335     QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
336     QVERIFY(obj != 0);
337     QCOMPARE(obj->width(), 300.);
338     QCOMPARE(obj->height(), 300.);
339     QCOMPARE(obj->status(), QQuickImageBase::Error);
340     QCOMPARE(obj->horizontalTileMode(), QQuickBorderImage::Stretch);
341     QCOMPARE(obj->verticalTileMode(), QQuickBorderImage::Stretch);
342
343     delete obj;
344 }
345
346 void tst_qquickborderimage::pendingRemoteRequest()
347 {
348     QFETCH(QString, source);
349
350     QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + source + "\" }";
351     QDeclarativeComponent component(&engine);
352     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
353     QQuickBorderImage *obj = qobject_cast<QQuickBorderImage*>(component.create());
354     QVERIFY(obj != 0);
355     QCOMPARE(obj->status(), QQuickBorderImage::Loading);
356
357     // verify no crash
358     // This will cause a delayed "QThread: Destroyed while thread is still running" warning
359     delete obj;
360     QTest::qWait(50);
361 }
362
363 void tst_qquickborderimage::pendingRemoteRequest_data()
364 {
365     QTest::addColumn<QString>("source");
366
367     QTest::newRow("png file") << "http://localhost/none.png";
368     QTest::newRow("sci file") << "http://localhost/none.sci";
369 }
370
371 QTEST_MAIN(tst_qquickborderimage)
372
373 #include "tst_qquickborderimage.moc"