QQuickCanvas renames
[profile/ivi/qtdeclarative.git] / tests / auto / quick / qquickfontloader / tst_qquickfontloader.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 <QtTest/QSignalSpy>
43 #include <QtQml/qqmlengine.h>
44 #include <QtQml/qqmlcomponent.h>
45 #include <QtQml/qqmlcontext.h>
46 #include <QtQuick/private/qquickfontloader_p.h>
47 #include "../../shared/util.h"
48 #include "../../shared/testhttpserver.h"
49 #include <QtQuick/QQuickView>
50 #include <QtQuick/QQuickItem>
51
52 #define SERVER_PORT 14457
53 #define SERVER_ADDR "http://localhost:14457"
54
55 class tst_qquickfontloader : public QQmlDataTest
56 {
57     Q_OBJECT
58 public:
59     tst_qquickfontloader();
60
61 private slots:
62     void initTestCase();
63     void noFont();
64     void namedFont();
65     void localFont();
66     void failLocalFont();
67     void webFont();
68     void redirWebFont();
69     void failWebFont();
70     void changeFont();
71     void changeFontSourceViaState();
72
73 private:
74     QQmlEngine engine;
75     TestHTTPServer server;
76 };
77
78 tst_qquickfontloader::tst_qquickfontloader() :
79     server(SERVER_PORT)
80 {
81 }
82
83 void tst_qquickfontloader::initTestCase()
84 {
85     QQmlDataTest::initTestCase();
86     server.serveDirectory(dataDirectory());
87     QVERIFY(server.isValid());
88 }
89
90 void tst_qquickfontloader::noFont()
91 {
92     QString componentStr = "import QtQuick 2.0\nFontLoader { }";
93     QQmlComponent component(&engine);
94     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
95     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
96
97     QVERIFY(fontObject != 0);
98     QCOMPARE(fontObject->name(), QString(""));
99     QCOMPARE(fontObject->source(), QUrl(""));
100     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Null);
101
102     delete fontObject;
103 }
104
105 void tst_qquickfontloader::namedFont()
106 {
107     QString componentStr = "import QtQuick 2.0\nFontLoader { name: \"Helvetica\" }";
108     QQmlComponent component(&engine);
109     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
110     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
111
112     QVERIFY(fontObject != 0);
113     QCOMPARE(fontObject->source(), QUrl(""));
114     QCOMPARE(fontObject->name(), QString("Helvetica"));
115     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
116 }
117
118 void tst_qquickfontloader::localFont()
119 {
120 #if defined(Q_OS_WIN)
121     QSKIP("Windows doesn't support font loading.");
122 #endif
123     QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" + testFileUrl("tarzeau_ocr_a.ttf").toString() + "\" }";
124     QQmlComponent component(&engine);
125     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
126     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
127
128     QVERIFY(fontObject != 0);
129     QVERIFY(fontObject->source() != QUrl(""));
130     QTRY_COMPARE(fontObject->name(), QString("OCRA"));
131     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
132 }
133
134 void tst_qquickfontloader::failLocalFont()
135 {
136 #if defined(Q_OS_WIN)
137     QSKIP("Windows doesn't support font loading.");
138 #endif
139     QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" + testFileUrl("dummy.ttf").toString() + "\" }";
140     QTest::ignoreMessage(QtWarningMsg, QString("file::2:1: QML FontLoader: Cannot load font: \"" + testFileUrl("dummy.ttf").toString() + "\"").toUtf8().constData());
141     QQmlComponent component(&engine);
142     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
143     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
144
145     QVERIFY(fontObject != 0);
146     QVERIFY(fontObject->source() != QUrl(""));
147     QTRY_COMPARE(fontObject->name(), QString(""));
148     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Error);
149 }
150
151 void tst_qquickfontloader::webFont()
152 {
153 #if defined(Q_OS_WIN)
154     QSKIP("Windows doesn't support font loading.");
155 #endif
156     QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" SERVER_ADDR "/tarzeau_ocr_a.ttf\" }";
157     QQmlComponent component(&engine);
158
159     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
160     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
161
162     QVERIFY(fontObject != 0);
163     QVERIFY(fontObject->source() != QUrl(""));
164     QTRY_COMPARE(fontObject->name(), QString("OCRA"));
165     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
166 }
167
168 void tst_qquickfontloader::redirWebFont()
169 {
170 #if defined(Q_OS_WIN)
171     QSKIP("Windows doesn't support font loading.");
172 #endif
173     server.addRedirect("olddir/oldname.ttf","../tarzeau_ocr_a.ttf");
174
175     QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" SERVER_ADDR "/olddir/oldname.ttf\" }";
176     QQmlComponent component(&engine);
177
178     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
179     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
180
181     QVERIFY(fontObject != 0);
182     QVERIFY(fontObject->source() != QUrl(""));
183     QTRY_COMPARE(fontObject->name(), QString("OCRA"));
184     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
185 }
186
187 void tst_qquickfontloader::failWebFont()
188 {
189 #if defined(Q_OS_WIN)
190     QSKIP("Windows doesn't support font loading.");
191 #endif
192     QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" SERVER_ADDR "/nonexist.ttf\" }";
193     QTest::ignoreMessage(QtWarningMsg, "file::2:1: QML FontLoader: Cannot load font: \"" SERVER_ADDR "/nonexist.ttf\"");
194     QQmlComponent component(&engine);
195     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
196     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
197
198     QVERIFY(fontObject != 0);
199     QVERIFY(fontObject->source() != QUrl(""));
200     QTRY_COMPARE(fontObject->name(), QString(""));
201     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Error);
202 }
203
204 void tst_qquickfontloader::changeFont()
205 {
206 #if defined(Q_OS_WIN)
207     QSKIP("Windows doesn't support font loading.");
208 #endif
209     QString componentStr = "import QtQuick 2.0\nFontLoader { source: font }";
210     QQmlContext *ctxt = engine.rootContext();
211     ctxt->setContextProperty("font", testFileUrl("tarzeau_ocr_a.ttf"));
212     QQmlComponent component(&engine);
213     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
214     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
215
216     QVERIFY(fontObject != 0);
217
218     QSignalSpy nameSpy(fontObject, SIGNAL(nameChanged()));
219     QSignalSpy statusSpy(fontObject, SIGNAL(statusChanged()));
220
221     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
222     QCOMPARE(nameSpy.count(), 0);
223     QCOMPARE(statusSpy.count(), 0);
224     QTRY_COMPARE(fontObject->name(), QString("OCRA"));
225
226     ctxt->setContextProperty("font", SERVER_ADDR "/daniel.ttf");
227     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Loading);
228     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
229     QCOMPARE(nameSpy.count(), 1);
230     QCOMPARE(statusSpy.count(), 2);
231     QTRY_COMPARE(fontObject->name(), QString("Daniel"));
232
233     ctxt->setContextProperty("font", testFileUrl("tarzeau_ocr_a.ttf"));
234     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
235     QCOMPARE(nameSpy.count(), 2);
236     QCOMPARE(statusSpy.count(), 2);
237     QTRY_COMPARE(fontObject->name(), QString("OCRA"));
238
239     ctxt->setContextProperty("font", SERVER_ADDR "/daniel.ttf");
240     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
241     QCOMPARE(nameSpy.count(), 3);
242     QCOMPARE(statusSpy.count(), 2);
243     QTRY_COMPARE(fontObject->name(), QString("Daniel"));
244 }
245
246 void tst_qquickfontloader::changeFontSourceViaState()
247 {
248 #if defined(Q_OS_WIN)
249     QSKIP("Windows doesn't support font loading.");
250 #endif
251     QQuickView window(testFileUrl("qtbug-20268.qml"));
252     window.show();
253     window.requestActivateWindow();
254     QTest::qWaitForWindowShown(&window);
255     QTRY_COMPARE(&window, qGuiApp->focusWindow());
256
257     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(qvariant_cast<QObject *>(window.rootObject()->property("fontloader")));
258     QVERIFY(fontObject != 0);
259     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
260     QVERIFY(fontObject->source() != QUrl(""));
261     QTRY_COMPARE(fontObject->name(), QString("OCRA"));
262
263     window.rootObject()->setProperty("usename", true);
264
265     // This warning should probably not be printed once QTBUG-20268 is fixed
266     QString warning = QString(testFileUrl("qtbug-20268.qml").toString()) +
267                               QLatin1String(":13:5: QML FontLoader: Cannot load font: \"\"");
268     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
269
270     QEXPECT_FAIL("", "QTBUG-20268", Abort);
271     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
272     QCOMPARE(window.rootObject()->property("name").toString(), QString("Tahoma"));
273 }
274
275 QTEST_MAIN(tst_qquickfontloader)
276
277 #include "tst_qquickfontloader.moc"