1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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>
52 #define SERVER_PORT 14457
53 #define SERVER_ADDR "http://localhost:14457"
55 class tst_qquickfontloader : public QQmlDataTest
59 tst_qquickfontloader();
71 void changeFontSourceViaState();
75 TestHTTPServer server;
78 tst_qquickfontloader::tst_qquickfontloader() :
83 void tst_qquickfontloader::initTestCase()
85 QQmlDataTest::initTestCase();
86 server.serveDirectory(dataDirectory());
87 QVERIFY(server.isValid());
90 void tst_qquickfontloader::noFont()
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());
97 QVERIFY(fontObject != 0);
98 QCOMPARE(fontObject->name(), QString(""));
99 QCOMPARE(fontObject->source(), QUrl(""));
100 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Null);
105 void tst_qquickfontloader::namedFont()
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());
112 QVERIFY(fontObject != 0);
113 QCOMPARE(fontObject->source(), QUrl(""));
114 QCOMPARE(fontObject->name(), QString("Helvetica"));
115 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
118 void tst_qquickfontloader::localFont()
120 #if defined(Q_OS_WIN)
121 QSKIP("Windows doesn't support font loading.");
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());
128 QVERIFY(fontObject != 0);
129 QVERIFY(fontObject->source() != QUrl(""));
130 QTRY_COMPARE(fontObject->name(), QString("OCRA"));
131 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
134 void tst_qquickfontloader::failLocalFont()
136 #if defined(Q_OS_WIN)
137 QSKIP("Windows doesn't support font loading.");
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());
145 QVERIFY(fontObject != 0);
146 QVERIFY(fontObject->source() != QUrl(""));
147 QTRY_COMPARE(fontObject->name(), QString(""));
148 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Error);
151 void tst_qquickfontloader::webFont()
153 #if defined(Q_OS_WIN)
154 QSKIP("Windows doesn't support font loading.");
156 QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" SERVER_ADDR "/tarzeau_ocr_a.ttf\" }";
157 QQmlComponent component(&engine);
159 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
160 QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
162 QVERIFY(fontObject != 0);
163 QVERIFY(fontObject->source() != QUrl(""));
164 QTRY_COMPARE(fontObject->name(), QString("OCRA"));
165 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
168 void tst_qquickfontloader::redirWebFont()
170 #if defined(Q_OS_WIN)
171 QSKIP("Windows doesn't support font loading.");
173 server.addRedirect("olddir/oldname.ttf","../tarzeau_ocr_a.ttf");
175 QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" SERVER_ADDR "/olddir/oldname.ttf\" }";
176 QQmlComponent component(&engine);
178 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
179 QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
181 QVERIFY(fontObject != 0);
182 QVERIFY(fontObject->source() != QUrl(""));
183 QTRY_COMPARE(fontObject->name(), QString("OCRA"));
184 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
187 void tst_qquickfontloader::failWebFont()
189 #if defined(Q_OS_WIN)
190 QSKIP("Windows doesn't support font loading.");
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());
198 QVERIFY(fontObject != 0);
199 QVERIFY(fontObject->source() != QUrl(""));
200 QTRY_COMPARE(fontObject->name(), QString(""));
201 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Error);
204 void tst_qquickfontloader::changeFont()
206 #if defined(Q_OS_WIN)
207 QSKIP("Windows doesn't support font loading.");
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());
216 QVERIFY(fontObject != 0);
218 QSignalSpy nameSpy(fontObject, SIGNAL(nameChanged()));
219 QSignalSpy statusSpy(fontObject, SIGNAL(statusChanged()));
221 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
222 QCOMPARE(nameSpy.count(), 0);
223 QCOMPARE(statusSpy.count(), 0);
224 QTRY_COMPARE(fontObject->name(), QString("OCRA"));
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"));
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"));
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"));
246 void tst_qquickfontloader::changeFontSourceViaState()
248 #if defined(Q_OS_WIN)
249 QSKIP("Windows doesn't support font loading.");
251 QQuickView window(testFileUrl("qtbug-20268.qml"));
253 window.requestActivateWindow();
254 QTest::qWaitForWindowShown(&window);
255 QTRY_COMPARE(&window, qGuiApp->focusWindow());
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"));
263 window.rootObject()->setProperty("usename", true);
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));
270 QEXPECT_FAIL("", "QTBUG-20268", Abort);
271 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
272 QCOMPARE(window.rootObject()->property("name").toString(), QString("Tahoma"));
275 QTEST_MAIN(tst_qquickfontloader)
277 #include "tst_qquickfontloader.moc"