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