1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include <QtTest/QSignalSpy>
43 #include <QtDeclarative/qdeclarativeengine.h>
44 #include <QtDeclarative/qdeclarativecomponent.h>
45 #include <QtDeclarative/qdeclarativecontext.h>
46 #include <QtDeclarative/private/qdeclarativefontloader_p.h>
47 #include "../shared/util.h"
48 #include "../../../shared/util.h"
49 #include "../../declarative/shared/testhttpserver.h"
51 #define SERVER_PORT 14448
53 class tst_qdeclarativefontloader : public QObject
57 tst_qdeclarativefontloader();
71 QDeclarativeEngine engine;
72 TestHTTPServer server;
75 tst_qdeclarativefontloader::tst_qdeclarativefontloader() :
78 server.serveDirectory(TESTDATA(""));
81 void tst_qdeclarativefontloader::init()
83 QVERIFY(server.isValid());
86 void tst_qdeclarativefontloader::noFont()
88 QString componentStr = "import QtQuick 2.0\nFontLoader { }";
89 QDeclarativeComponent component(&engine);
90 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
91 QDeclarativeFontLoader *fontObject = qobject_cast<QDeclarativeFontLoader*>(component.create());
93 QVERIFY(fontObject != 0);
94 QCOMPARE(fontObject->name(), QString(""));
95 QCOMPARE(fontObject->source(), QUrl(""));
96 QTRY_VERIFY(fontObject->status() == QDeclarativeFontLoader::Null);
101 void tst_qdeclarativefontloader::namedFont()
103 QString componentStr = "import QtQuick 2.0\nFontLoader { name: \"Helvetica\" }";
104 QDeclarativeComponent component(&engine);
105 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
106 QDeclarativeFontLoader *fontObject = qobject_cast<QDeclarativeFontLoader*>(component.create());
108 QVERIFY(fontObject != 0);
109 QCOMPARE(fontObject->source(), QUrl(""));
110 QCOMPARE(fontObject->name(), QString("Helvetica"));
111 QTRY_VERIFY(fontObject->status() == QDeclarativeFontLoader::Ready);
114 void tst_qdeclarativefontloader::localFont()
116 QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" + TESTDATA("tarzeau_ocr_a.ttf") + "\" }";
117 QDeclarativeComponent component(&engine);
118 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
119 QDeclarativeFontLoader *fontObject = qobject_cast<QDeclarativeFontLoader*>(component.create());
121 QVERIFY(fontObject != 0);
122 QVERIFY(fontObject->source() != QUrl(""));
123 QTRY_COMPARE(fontObject->name(), QString("OCRA"));
124 QTRY_VERIFY(fontObject->status() == QDeclarativeFontLoader::Ready);
127 void tst_qdeclarativefontloader::failLocalFont()
129 QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"" + QUrl::fromLocalFile(TESTDATA("dummy.ttf")).toString() + "\" }";
130 QTest::ignoreMessage(QtWarningMsg, QString("file::2:1: QML FontLoader: Cannot load font: \"" + QUrl::fromLocalFile(TESTDATA("dummy.ttf")).toString() + "\"").toUtf8().constData());
131 QDeclarativeComponent component(&engine);
132 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
133 QDeclarativeFontLoader *fontObject = qobject_cast<QDeclarativeFontLoader*>(component.create());
135 QVERIFY(fontObject != 0);
136 QVERIFY(fontObject->source() != QUrl(""));
137 QTRY_COMPARE(fontObject->name(), QString(""));
138 QTRY_VERIFY(fontObject->status() == QDeclarativeFontLoader::Error);
141 void tst_qdeclarativefontloader::webFont()
143 QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"http://localhost:14448/tarzeau_ocr_a.ttf\" }";
144 QDeclarativeComponent component(&engine);
146 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
147 QDeclarativeFontLoader *fontObject = qobject_cast<QDeclarativeFontLoader*>(component.create());
149 QVERIFY(fontObject != 0);
150 QVERIFY(fontObject->source() != QUrl(""));
151 QTRY_COMPARE(fontObject->name(), QString("OCRA"));
152 QTRY_VERIFY(fontObject->status() == QDeclarativeFontLoader::Ready);
155 void tst_qdeclarativefontloader::redirWebFont()
157 server.addRedirect("olddir/oldname.ttf","../tarzeau_ocr_a.ttf");
159 QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"http://localhost:14448/olddir/oldname.ttf\" }";
160 QDeclarativeComponent component(&engine);
162 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
163 QDeclarativeFontLoader *fontObject = qobject_cast<QDeclarativeFontLoader*>(component.create());
165 QVERIFY(fontObject != 0);
166 QVERIFY(fontObject->source() != QUrl(""));
167 QTRY_COMPARE(fontObject->name(), QString("OCRA"));
168 QTRY_VERIFY(fontObject->status() == QDeclarativeFontLoader::Ready);
171 void tst_qdeclarativefontloader::failWebFont()
173 QString componentStr = "import QtQuick 2.0\nFontLoader { source: \"http://localhost:14448/nonexist.ttf\" }";
174 QTest::ignoreMessage(QtWarningMsg, "file::2:1: QML FontLoader: Cannot load font: \"http://localhost:14448/nonexist.ttf\"");
175 QDeclarativeComponent component(&engine);
176 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
177 QDeclarativeFontLoader *fontObject = qobject_cast<QDeclarativeFontLoader*>(component.create());
179 QVERIFY(fontObject != 0);
180 QVERIFY(fontObject->source() != QUrl(""));
181 QTRY_COMPARE(fontObject->name(), QString(""));
182 QTRY_VERIFY(fontObject->status() == QDeclarativeFontLoader::Error);
185 void tst_qdeclarativefontloader::changeFont()
187 QString componentStr = "import QtQuick 2.0\nFontLoader { source: font }";
188 QDeclarativeContext *ctxt = engine.rootContext();
189 ctxt->setContextProperty("font", QUrl::fromLocalFile(TESTDATA("tarzeau_ocr_a.ttf")));
190 QDeclarativeComponent component(&engine);
191 component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
192 QDeclarativeFontLoader *fontObject = qobject_cast<QDeclarativeFontLoader*>(component.create());
194 QVERIFY(fontObject != 0);
196 QSignalSpy nameSpy(fontObject, SIGNAL(nameChanged()));
197 QSignalSpy statusSpy(fontObject, SIGNAL(statusChanged()));
199 QTRY_VERIFY(fontObject->status() == QDeclarativeFontLoader::Ready);
200 QCOMPARE(nameSpy.count(), 0);
201 QCOMPARE(statusSpy.count(), 0);
202 QTRY_COMPARE(fontObject->name(), QString("OCRA"));
204 ctxt->setContextProperty("font", "http://localhost:14448/daniel.ttf");
205 QTRY_VERIFY(fontObject->status() == QDeclarativeFontLoader::Loading);
206 QTRY_VERIFY(fontObject->status() == QDeclarativeFontLoader::Ready);
207 QCOMPARE(nameSpy.count(), 1);
208 QCOMPARE(statusSpy.count(), 2);
209 QTRY_COMPARE(fontObject->name(), QString("Daniel"));
211 ctxt->setContextProperty("font", QUrl::fromLocalFile(TESTDATA("tarzeau_ocr_a.ttf")));
212 QTRY_VERIFY(fontObject->status() == QDeclarativeFontLoader::Ready);
213 QCOMPARE(nameSpy.count(), 2);
214 QCOMPARE(statusSpy.count(), 2);
215 QTRY_COMPARE(fontObject->name(), QString("OCRA"));
217 ctxt->setContextProperty("font", "http://localhost:14448/daniel.ttf");
218 QTRY_VERIFY(fontObject->status() == QDeclarativeFontLoader::Ready);
219 QCOMPARE(nameSpy.count(), 3);
220 QCOMPARE(statusSpy.count(), 2);
221 QTRY_COMPARE(fontObject->name(), QString("Daniel"));
224 QTEST_MAIN(tst_qdeclarativefontloader)
226 #include "tst_qdeclarativefontloader.moc"