Fix test fails related to QTBUG-22237
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qsgborderimage / tst_qsgborderimage.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/qsgborderimage_p.h>
52 #include <private/qsgimagebase_p.h>
53 #include <private/qsgscalegrid_p_p.h>
54 #include <private/qsgloader_p.h>
55 #include <QtDeclarative/qsgview.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_qsgborderimage : public QObject
65
66 {
67     Q_OBJECT
68 public:
69     tst_qsgborderimage();
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_qsgborderimage::tst_qsgborderimage()
91 {
92 }
93
94 void tst_qsgborderimage::noSource()
95 {
96     QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"\" }";
97     QDeclarativeComponent component(&engine);
98     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
99     QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(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(), QSGBorderImage::Stretch);
105     QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Stretch);
106
107     delete obj;
108 }
109
110 void tst_qsgborderimage::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(TESTDATA("colors.png")).toString() << false << "";
117     QTest::newRow("local not found") << QUrl::fromLocalFile(TESTDATA("no-such-file.png")).toString() << false
118         << "file::2:1: QML BorderImage: Cannot open: " + QUrl::fromLocalFile(TESTDATA("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_qsgborderimage::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(TESTDATA(""));
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     QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
144     QVERIFY(obj != 0);
145
146     if (remote)
147         QTRY_VERIFY(obj->status() == QSGBorderImage::Loading);
148
149     QCOMPARE(obj->source(), remote ? source : QUrl(source));
150
151     if (error.isEmpty()) {
152         QTRY_VERIFY(obj->status() == QSGBorderImage::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(), QSGBorderImage::Stretch);
158         QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Stretch);
159     } else {
160         QTRY_VERIFY(obj->status() == QSGBorderImage::Error);
161     }
162
163     delete obj;
164     delete server;
165 }
166
167 void tst_qsgborderimage::clearSource()
168 {
169     QString componentStr = "import QtQuick 2.0\nBorderImage { source: srcImage }";
170     QDeclarativeContext *ctxt = engine.rootContext();
171     ctxt->setContextProperty("srcImage", QUrl::fromLocalFile(TESTDATA("colors.png")));
172     QDeclarativeComponent component(&engine);
173     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
174     QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
175     QVERIFY(obj != 0);
176     QVERIFY(obj->status() == QSGBorderImage::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() == QSGBorderImage::Null);
183     QCOMPARE(obj->width(), 0.);
184     QCOMPARE(obj->height(), 0.);
185 }
186
187 void tst_qsgborderimage::resized()
188 {
189     QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + QUrl::fromLocalFile(TESTDATA("colors.png")).toString() + "\"; width: 300; height: 300 }";
190     QDeclarativeComponent component(&engine);
191     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
192     QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(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(), QSGBorderImage::Stretch);
199     QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Stretch);
200
201     delete obj;
202 }
203
204 void tst_qsgborderimage::smooth()
205 {
206     QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + TESTDATA("colors.png") + "\"; smooth: true; width: 300; height: 300 }";
207     QDeclarativeComponent component(&engine);
208     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
209     QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(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(), QSGBorderImage::Stretch);
215     QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Stretch);
216
217     delete obj;
218 }
219
220 void tst_qsgborderimage::mirror()
221 {
222     QSGView *canvas = new QSGView;
223     canvas->show();
224
225     canvas->setSource(QUrl::fromLocalFile(TESTDATA("mirror.qml")));
226     QSGBorderImage *image = qobject_cast<QSGBorderImage*>(canvas->rootObject());
227     QVERIFY(image != 0);
228     canvas->show();
229
230     QImage screenshot = canvas->grabFrameBuffer();
231
232     QImage srcPixmap(screenshot);
233     QTransform transform;
234     transform.translate(image->width(), 0).scale(-1, 1.0);
235     srcPixmap = srcPixmap.transformed(transform);
236
237     image->setProperty("mirror", true);
238     screenshot = canvas->grabFrameBuffer();
239     QCOMPARE(screenshot, srcPixmap);
240
241     delete canvas;
242 }
243
244 void tst_qsgborderimage::tileModes()
245 {
246     {
247         QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + TESTDATA("colors.png") + "\"; width: 100; height: 300; horizontalTileMode: BorderImage.Repeat; verticalTileMode: BorderImage.Repeat }";
248         QDeclarativeComponent component(&engine);
249         component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
250         QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
251         QVERIFY(obj != 0);
252         QCOMPARE(obj->width(), 100.);
253         QCOMPARE(obj->height(), 300.);
254         QCOMPARE(obj->horizontalTileMode(), QSGBorderImage::Repeat);
255         QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Repeat);
256
257         delete obj;
258     }
259     {
260         QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + TESTDATA("colors.png") + "\"; width: 300; height: 150; horizontalTileMode: BorderImage.Round; verticalTileMode: BorderImage.Round }";
261         QDeclarativeComponent component(&engine);
262         component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
263         QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
264         QVERIFY(obj != 0);
265         QCOMPARE(obj->width(), 300.);
266         QCOMPARE(obj->height(), 150.);
267         QCOMPARE(obj->horizontalTileMode(), QSGBorderImage::Round);
268         QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Round);
269
270         delete obj;
271     }
272 }
273
274 void tst_qsgborderimage::sciSource()
275 {
276     QFETCH(QString, source);
277     QFETCH(bool, valid);
278
279     bool remote = source.startsWith("http");
280     TestHTTPServer *server = 0;
281     if (remote) {
282         server = new TestHTTPServer(SERVER_PORT);
283         QVERIFY(server->isValid());
284         server->serveDirectory(TESTDATA(""));
285     }
286
287     QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + source + "\"; width: 300; height: 300 }";
288     QDeclarativeComponent component(&engine);
289     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
290     QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
291     QVERIFY(obj != 0);
292
293     if (remote)
294         QTRY_VERIFY(obj->status() == QSGBorderImage::Loading);
295
296     QCOMPARE(obj->source(), remote ? source : QUrl(source));
297     QCOMPARE(obj->width(), 300.);
298     QCOMPARE(obj->height(), 300.);
299
300     if (valid) {
301         QTRY_VERIFY(obj->status() == QSGBorderImage::Ready);
302         QCOMPARE(obj->border()->left(), 10);
303         QCOMPARE(obj->border()->top(), 20);
304         QCOMPARE(obj->border()->right(), 30);
305         QCOMPARE(obj->border()->bottom(), 40);
306         QCOMPARE(obj->horizontalTileMode(), QSGBorderImage::Round);
307         QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Repeat);
308     } else {
309         QTRY_VERIFY(obj->status() == QSGBorderImage::Error);
310     }
311
312     delete obj;
313     delete server;
314 }
315
316 void tst_qsgborderimage::sciSource_data()
317 {
318     QTest::addColumn<QString>("source");
319     QTest::addColumn<bool>("valid");
320
321     QTest::newRow("local") << QUrl::fromLocalFile(TESTDATA("colors-round.sci")).toString() << true;
322     QTest::newRow("local quoted filename") << QUrl::fromLocalFile(TESTDATA("colors-round-quotes.sci")).toString() << true;
323     QTest::newRow("local not found") << QUrl::fromLocalFile(TESTDATA("no-such-file.sci")).toString() << false;
324     QTest::newRow("remote") << SERVER_ADDR "/colors-round.sci" << true;
325     QTest::newRow("remote filename quoted") << SERVER_ADDR "/colors-round-quotes.sci" << true;
326     QTest::newRow("remote image") << SERVER_ADDR "/colors-round-remote.sci" << true;
327     QTest::newRow("remote not found") << SERVER_ADDR "/no-such-file.sci" << false;
328 }
329
330 void tst_qsgborderimage::invalidSciFile()
331 {
332     QTest::ignoreMessage(QtWarningMsg, "QSGGridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Roun"
333     QTest::ignoreMessage(QtWarningMsg, "QSGGridScaledImage: Invalid tile rule specified. Using Stretch."); // for "Repea"
334
335     QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + QUrl::fromLocalFile(TESTDATA("invalid.sci")).toString() +"\"; width: 300; height: 300 }";
336     QDeclarativeComponent component(&engine);
337     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
338     QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
339     QVERIFY(obj != 0);
340     QCOMPARE(obj->width(), 300.);
341     QCOMPARE(obj->height(), 300.);
342     QCOMPARE(obj->status(), QSGImageBase::Error);
343     QCOMPARE(obj->horizontalTileMode(), QSGBorderImage::Stretch);
344     QCOMPARE(obj->verticalTileMode(), QSGBorderImage::Stretch);
345
346     delete obj;
347 }
348
349 void tst_qsgborderimage::pendingRemoteRequest()
350 {
351     QFETCH(QString, source);
352
353     QString componentStr = "import QtQuick 2.0\nBorderImage { source: \"" + source + "\" }";
354     QDeclarativeComponent component(&engine);
355     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
356     QSGBorderImage *obj = qobject_cast<QSGBorderImage*>(component.create());
357     QVERIFY(obj != 0);
358     QCOMPARE(obj->status(), QSGBorderImage::Loading);
359
360     // verify no crash
361     // This will cause a delayed "QThread: Destroyed while thread is still running" warning
362     delete obj;
363     QTest::qWait(50);
364 }
365
366 void tst_qsgborderimage::pendingRemoteRequest_data()
367 {
368     QTest::addColumn<QString>("source");
369
370     QTest::newRow("png file") << "http://localhost/none.png";
371     QTest::newRow("sci file") << "http://localhost/none.sci";
372 }
373
374 QTEST_MAIN(tst_qsgborderimage)
375
376 #include "tst_qsgborderimage.moc"