Change copyrights from Nokia to Digia
[profile/ivi/qtdeclarative.git] / tests / auto / quick / qquickfontloader / tst_qquickfontloader.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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     QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" + testFileUrl("tarzeau_ocr_a.ttf").toString() + "\" }";
121     QQmlComponent component(&engine);
122     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
123     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
124
125     QVERIFY(fontObject != 0);
126     QVERIFY(fontObject->source() != QUrl(""));
127     QTRY_COMPARE(fontObject->name(), QString("OCRA"));
128     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
129 }
130
131 void tst_qquickfontloader::failLocalFont()
132 {
133     QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" + testFileUrl("dummy.ttf").toString() + "\" }";
134     QTest::ignoreMessage(QtWarningMsg, QString("file::2:1: QML FontLoader: Cannot load font: \"" + testFileUrl("dummy.ttf").toString() + "\"").toUtf8().constData());
135     QQmlComponent component(&engine);
136     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
137     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
138
139     QVERIFY(fontObject != 0);
140     QVERIFY(fontObject->source() != QUrl(""));
141     QTRY_COMPARE(fontObject->name(), QString(""));
142     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Error);
143 }
144
145 void tst_qquickfontloader::webFont()
146 {
147     QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" SERVER_ADDR "/tarzeau_ocr_a.ttf\" }";
148     QQmlComponent component(&engine);
149
150     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
151     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
152
153     QVERIFY(fontObject != 0);
154     QVERIFY(fontObject->source() != QUrl(""));
155     QTRY_COMPARE(fontObject->name(), QString("OCRA"));
156     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
157 }
158
159 void tst_qquickfontloader::redirWebFont()
160 {
161     server.addRedirect("olddir/oldname.ttf","../tarzeau_ocr_a.ttf");
162
163     QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" SERVER_ADDR "/olddir/oldname.ttf\" }";
164     QQmlComponent component(&engine);
165
166     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
167     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
168
169     QVERIFY(fontObject != 0);
170     QVERIFY(fontObject->source() != QUrl(""));
171     QTRY_COMPARE(fontObject->name(), QString("OCRA"));
172     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
173 }
174
175 void tst_qquickfontloader::failWebFont()
176 {
177     QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" SERVER_ADDR "/nonexist.ttf\" }";
178     QTest::ignoreMessage(QtWarningMsg, "file::2:1: QML FontLoader: Cannot load font: \"" SERVER_ADDR "/nonexist.ttf\"");
179     QQmlComponent component(&engine);
180     component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
181     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
182
183     QVERIFY(fontObject != 0);
184     QVERIFY(fontObject->source() != QUrl(""));
185     QTRY_COMPARE(fontObject->name(), QString(""));
186     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Error);
187 }
188
189 void tst_qquickfontloader::changeFont()
190 {
191     QString componentStr = "import QtQuick 2.0\nFontLoader { source: font }";
192     QQmlContext *ctxt = engine.rootContext();
193     ctxt->setContextProperty("font", testFileUrl("tarzeau_ocr_a.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
200     QSignalSpy nameSpy(fontObject, SIGNAL(nameChanged()));
201     QSignalSpy statusSpy(fontObject, SIGNAL(statusChanged()));
202
203     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
204     QCOMPARE(nameSpy.count(), 0);
205     QCOMPARE(statusSpy.count(), 0);
206     QTRY_COMPARE(fontObject->name(), QString("OCRA"));
207
208     ctxt->setContextProperty("font", SERVER_ADDR "/daniel.ttf");
209     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Loading);
210     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
211     QCOMPARE(nameSpy.count(), 1);
212     QCOMPARE(statusSpy.count(), 2);
213     QTRY_COMPARE(fontObject->name(), QString("Daniel"));
214
215     ctxt->setContextProperty("font", testFileUrl("tarzeau_ocr_a.ttf"));
216     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
217     QCOMPARE(nameSpy.count(), 2);
218     QCOMPARE(statusSpy.count(), 2);
219     QTRY_COMPARE(fontObject->name(), QString("OCRA"));
220
221     ctxt->setContextProperty("font", SERVER_ADDR "/daniel.ttf");
222     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
223     QCOMPARE(nameSpy.count(), 3);
224     QCOMPARE(statusSpy.count(), 2);
225     QTRY_COMPARE(fontObject->name(), QString("Daniel"));
226 }
227
228 void tst_qquickfontloader::changeFontSourceViaState()
229 {
230     QQuickView window(testFileUrl("qtbug-20268.qml"));
231     window.show();
232     window.requestActivateWindow();
233     QVERIFY(QTest::qWaitForWindowActive(&window));
234     QCOMPARE(&window, qGuiApp->focusWindow());
235
236     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(qvariant_cast<QObject *>(window.rootObject()->property("fontloader")));
237     QVERIFY(fontObject != 0);
238     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
239     QVERIFY(fontObject->source() != QUrl(""));
240     QTRY_COMPARE(fontObject->name(), QString("OCRA"));
241
242     window.rootObject()->setProperty("usename", true);
243
244     // This warning should probably not be printed once QTBUG-20268 is fixed
245     QString warning = QString(testFileUrl("qtbug-20268.qml").toString()) +
246                               QLatin1String(":13:5: QML FontLoader: Cannot load font: \"\"");
247     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
248
249     QEXPECT_FAIL("", "QTBUG-20268", Abort);
250     QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
251     QCOMPARE(window.rootObject()->property("name").toString(), QString("Tahoma"));
252 }
253
254 QTEST_MAIN(tst_qquickfontloader)
255
256 #include "tst_qquickfontloader.moc"