1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
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 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());
125 QVERIFY(fontObject != 0);
126 QVERIFY(fontObject->source() != QUrl(""));
127 QTRY_COMPARE(fontObject->name(), QString("OCRA"));
128 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
131 void tst_qquickfontloader::failLocalFont()
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());
139 QVERIFY(fontObject != 0);
140 QVERIFY(fontObject->source() != QUrl(""));
141 QTRY_COMPARE(fontObject->name(), QString(""));
142 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Error);
145 void tst_qquickfontloader::webFont()
147 QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" SERVER_ADDR "/tarzeau_ocr_a.ttf\" }";
148 QQmlComponent component(&engine);
150 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
151 QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
153 QVERIFY(fontObject != 0);
154 QVERIFY(fontObject->source() != QUrl(""));
155 QTRY_COMPARE(fontObject->name(), QString("OCRA"));
156 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
159 void tst_qquickfontloader::redirWebFont()
161 server.addRedirect("olddir/oldname.ttf","../tarzeau_ocr_a.ttf");
163 QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" SERVER_ADDR "/olddir/oldname.ttf\" }";
164 QQmlComponent component(&engine);
166 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
167 QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
169 QVERIFY(fontObject != 0);
170 QVERIFY(fontObject->source() != QUrl(""));
171 QTRY_COMPARE(fontObject->name(), QString("OCRA"));
172 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
175 void tst_qquickfontloader::failWebFont()
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());
183 QVERIFY(fontObject != 0);
184 QVERIFY(fontObject->source() != QUrl(""));
185 QTRY_COMPARE(fontObject->name(), QString(""));
186 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Error);
189 void tst_qquickfontloader::changeFont()
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());
198 QVERIFY(fontObject != 0);
200 QSignalSpy nameSpy(fontObject, SIGNAL(nameChanged()));
201 QSignalSpy statusSpy(fontObject, SIGNAL(statusChanged()));
203 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
204 QCOMPARE(nameSpy.count(), 0);
205 QCOMPARE(statusSpy.count(), 0);
206 QTRY_COMPARE(fontObject->name(), QString("OCRA"));
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"));
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"));
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"));
228 void tst_qquickfontloader::changeFontSourceViaState()
230 QQuickView window(testFileUrl("qtbug-20268.qml"));
232 window.requestActivateWindow();
233 QVERIFY(QTest::qWaitForWindowActive(&window));
234 QCOMPARE(&window, qGuiApp->focusWindow());
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"));
242 window.rootObject()->setProperty("usename", true);
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));
249 QEXPECT_FAIL("", "QTBUG-20268", Abort);
250 QTRY_VERIFY(fontObject->status() == QQuickFontLoader::Ready);
251 QCOMPARE(window.rootObject()->property("name").toString(), QString("Tahoma"));
254 QTEST_MAIN(tst_qquickfontloader)
256 #include "tst_qquickfontloader.moc"