| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the test suite of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ | 
| 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 The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 | #include <qtest.h> | 
| 29 | #include <QtTest/QSignalSpy> | 
| 30 | #include <QtQml/qqmlengine.h> | 
| 31 | #include <QtQml/qqmlcomponent.h> | 
| 32 | #include <QtQml/qqmlcontext.h> | 
| 33 | #include <QtQuick/private/qquickfontloader_p.h> | 
| 34 | #include "../../shared/util.h" | 
| 35 | #include "../../shared/testhttpserver.h" | 
| 36 | #include <QtQuick/QQuickView> | 
| 37 | #include <QtQuick/QQuickItem> | 
| 38 |  | 
| 39 | class tst_qquickfontloader : public QQmlDataTest | 
| 40 | { | 
| 41 |     Q_OBJECT | 
| 42 | public: | 
| 43 |     tst_qquickfontloader(); | 
| 44 |  | 
| 45 | private slots: | 
| 46 |     void initTestCase(); | 
| 47 |     void noFont(); | 
| 48 |     void namedFont(); | 
| 49 |     void localFont(); | 
| 50 |     void failLocalFont(); | 
| 51 |     void webFont(); | 
| 52 |     void redirWebFont(); | 
| 53 |     void failWebFont(); | 
| 54 |     void changeFont(); | 
| 55 |     void changeFontSourceViaState(); | 
| 56 |  | 
| 57 | private: | 
| 58 |     QQmlEngine engine; | 
| 59 |     TestHTTPServer server; | 
| 60 | }; | 
| 61 |  | 
| 62 | tst_qquickfontloader::tst_qquickfontloader() | 
| 63 | { | 
| 64 | } | 
| 65 |  | 
| 66 | void tst_qquickfontloader::initTestCase() | 
| 67 | { | 
| 68 |     QQmlDataTest::initTestCase(); | 
| 69 |     server.serveDirectory(dataDirectory()); | 
| 70 |     QVERIFY2(server.listen(), qPrintable(server.errorString())); | 
| 71 | } | 
| 72 |  | 
| 73 | void tst_qquickfontloader::noFont() | 
| 74 | { | 
| 75 |     QString componentStr = "import QtQuick 2.0\nFontLoader { }" ; | 
| 76 |     QQmlComponent component(&engine); | 
| 77 |     component.setData(componentStr.toLatin1(), baseUrl: QUrl::fromLocalFile(localfile: "" )); | 
| 78 |     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(object: component.create()); | 
| 79 |  | 
| 80 |     QVERIFY(fontObject != nullptr); | 
| 81 |     QCOMPARE(fontObject->name(), QString("" )); | 
| 82 |     QCOMPARE(fontObject->source(), QUrl("" )); | 
| 83 |     QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Null); | 
| 84 |  | 
| 85 |     delete fontObject; | 
| 86 | } | 
| 87 |  | 
| 88 | void tst_qquickfontloader::namedFont() | 
| 89 | { | 
| 90 |     QString componentStr = "import QtQuick 2.0\nFontLoader { name: \"Helvetica\" }" ; | 
| 91 |     QQmlComponent component(&engine); | 
| 92 |     component.setData(componentStr.toLatin1(), baseUrl: QUrl::fromLocalFile(localfile: "" )); | 
| 93 |     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(object: component.create()); | 
| 94 |  | 
| 95 |     QVERIFY(fontObject != nullptr); | 
| 96 |     QCOMPARE(fontObject->source(), QUrl("" )); | 
| 97 |     QCOMPARE(fontObject->name(), QString("Helvetica" )); | 
| 98 |     QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Ready); | 
| 99 | } | 
| 100 |  | 
| 101 | void tst_qquickfontloader::localFont() | 
| 102 | { | 
| 103 |     QString componentStr = "import QtQuick 2.0\nFontLoader { source: \""  + testFileUrl(fileName: "tarzeau_ocr_a.ttf" ).toString() + "\" }" ; | 
| 104 |     QQmlComponent component(&engine); | 
| 105 |     component.setData(componentStr.toLatin1(), baseUrl: QUrl::fromLocalFile(localfile: "" )); | 
| 106 |     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(object: component.create()); | 
| 107 |  | 
| 108 |     QVERIFY(fontObject != nullptr); | 
| 109 |     QVERIFY(fontObject->source() != QUrl("" )); | 
| 110 |     QTRY_COMPARE(fontObject->name(), QString("OCRA" )); | 
| 111 |     QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Ready); | 
| 112 | } | 
| 113 |  | 
| 114 | void tst_qquickfontloader::failLocalFont() | 
| 115 | { | 
| 116 |     QString componentStr = "import QtQuick 2.0\nFontLoader { source: \""  + testFileUrl(fileName: "dummy.ttf" ).toString() + "\" }" ; | 
| 117 |     QTest::ignoreMessage(type: QtWarningMsg, message: QString("<Unknown File>:2:1: QML FontLoader: Cannot load font: \""  + testFileUrl(fileName: "dummy.ttf" ).toString() + QLatin1Char('"')).toUtf8().constData()); | 
| 118 |     QQmlComponent component(&engine); | 
| 119 |     component.setData(componentStr.toLatin1(), baseUrl: QUrl::fromLocalFile(localfile: "" )); | 
| 120 |     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(object: component.create()); | 
| 121 |  | 
| 122 |     QVERIFY(fontObject != nullptr); | 
| 123 |     QVERIFY(fontObject->source() != QUrl("" )); | 
| 124 |     QTRY_COMPARE(fontObject->name(), QString("" )); | 
| 125 |     QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Error); | 
| 126 | } | 
| 127 |  | 
| 128 | void tst_qquickfontloader::webFont() | 
| 129 | { | 
| 130 |     QString componentStr = "import QtQuick 2.0\nFontLoader { source: \""  + server.baseUrl().toString() + "/tarzeau_ocr_a.ttf\" }" ; | 
| 131 |     QQmlComponent component(&engine); | 
| 132 |  | 
| 133 |     component.setData(componentStr.toLatin1(), baseUrl: QUrl::fromLocalFile(localfile: "" )); | 
| 134 |     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(object: component.create()); | 
| 135 |  | 
| 136 |     QVERIFY(fontObject != nullptr); | 
| 137 |     QVERIFY(fontObject->source() != QUrl("" )); | 
| 138 |     QTRY_COMPARE(fontObject->name(), QString("OCRA" )); | 
| 139 |     QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Ready); | 
| 140 | } | 
| 141 |  | 
| 142 | void tst_qquickfontloader::redirWebFont() | 
| 143 | { | 
| 144 |     server.addRedirect(filename: "olddir/oldname.ttf" ,redirectName: "../tarzeau_ocr_a.ttf" ); | 
| 145 |  | 
| 146 |     QString componentStr = "import QtQuick 2.0\nFontLoader { source: \""  + server.baseUrl().toString() + "/olddir/oldname.ttf\" }" ; | 
| 147 |     QQmlComponent component(&engine); | 
| 148 |  | 
| 149 |     component.setData(componentStr.toLatin1(), baseUrl: QUrl::fromLocalFile(localfile: "" )); | 
| 150 |     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(object: component.create()); | 
| 151 |  | 
| 152 |     QVERIFY(fontObject != nullptr); | 
| 153 |     QVERIFY(fontObject->source() != QUrl("" )); | 
| 154 |     QTRY_COMPARE(fontObject->name(), QString("OCRA" )); | 
| 155 |     QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Ready); | 
| 156 | } | 
| 157 |  | 
| 158 | void tst_qquickfontloader::failWebFont() | 
| 159 | { | 
| 160 |     QString componentStr = "import QtQuick 2.0\nFontLoader { source: \""  + server.baseUrl().toString() + "/nonexist.ttf\" }" ; | 
| 161 |     const QString expectedError = "<Unknown File>:2:1: QML FontLoader: Cannot load font: \""  + server.baseUrl().toString() + "/nonexist.ttf\"" ; | 
| 162 |     QTest::ignoreMessage(type: QtWarningMsg, message: expectedError.toUtf8()); | 
| 163 |     QQmlComponent component(&engine); | 
| 164 |     component.setData(componentStr.toLatin1(), baseUrl: QUrl::fromLocalFile(localfile: "" )); | 
| 165 |     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(object: component.create()); | 
| 166 |  | 
| 167 |     QVERIFY(fontObject != nullptr); | 
| 168 |     QVERIFY(fontObject->source() != QUrl("" )); | 
| 169 |     QTRY_COMPARE(fontObject->name(), QString("" )); | 
| 170 |     QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Error); | 
| 171 | } | 
| 172 |  | 
| 173 | void tst_qquickfontloader::changeFont() | 
| 174 | { | 
| 175 |     QString componentStr = "import QtQuick 2.0\nFontLoader { source: font }" ; | 
| 176 |     QQmlContext *ctxt = engine.rootContext(); | 
| 177 |     ctxt->setContextProperty("font" , testFileUrl(fileName: "tarzeau_ocr_a.ttf" )); | 
| 178 |     QQmlComponent component(&engine); | 
| 179 |     component.setData(componentStr.toLatin1(), baseUrl: QUrl::fromLocalFile(localfile: "" )); | 
| 180 |     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(object: component.create()); | 
| 181 |  | 
| 182 |     QVERIFY(fontObject != nullptr); | 
| 183 |  | 
| 184 |     QSignalSpy nameSpy(fontObject, SIGNAL(nameChanged())); | 
| 185 |     QSignalSpy statusSpy(fontObject, SIGNAL(statusChanged())); | 
| 186 |  | 
| 187 |     QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Ready); | 
| 188 |     QCOMPARE(nameSpy.count(), 0); | 
| 189 |     QCOMPARE(statusSpy.count(), 0); | 
| 190 |     QTRY_COMPARE(fontObject->name(), QString("OCRA" )); | 
| 191 |  | 
| 192 |     ctxt->setContextProperty("font" , server.urlString(documentPath: "/daniel.ttf" )); | 
| 193 |     QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Loading); | 
| 194 |     QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Ready); | 
| 195 |     QCOMPARE(nameSpy.count(), 1); | 
| 196 |     QCOMPARE(statusSpy.count(), 2); | 
| 197 |     QTRY_COMPARE(fontObject->name(), QString("Daniel" )); | 
| 198 |  | 
| 199 |     ctxt->setContextProperty("font" , testFileUrl(fileName: "tarzeau_ocr_a.ttf" )); | 
| 200 |     QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Ready); | 
| 201 |     QCOMPARE(nameSpy.count(), 2); | 
| 202 |     QCOMPARE(statusSpy.count(), 2); | 
| 203 |     QTRY_COMPARE(fontObject->name(), QString("OCRA" )); | 
| 204 |  | 
| 205 |     ctxt->setContextProperty("font" , server.urlString(documentPath: "/daniel.ttf" )); | 
| 206 |     QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Ready); | 
| 207 |     QCOMPARE(nameSpy.count(), 3); | 
| 208 |     QCOMPARE(statusSpy.count(), 2); | 
| 209 |     QTRY_COMPARE(fontObject->name(), QString("Daniel" )); | 
| 210 | } | 
| 211 |  | 
| 212 | void tst_qquickfontloader::changeFontSourceViaState() | 
| 213 | { | 
| 214 |     QQuickView window(testFileUrl(fileName: "qtbug-20268.qml" )); | 
| 215 |     window.show(); | 
| 216 |     window.requestActivate(); | 
| 217 |     QVERIFY(QTest::qWaitForWindowActive(&window)); | 
| 218 |     QCOMPARE(&window, qGuiApp->focusWindow()); | 
| 219 |  | 
| 220 |     QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(object: qvariant_cast<QObject *>(v: window.rootObject()->property(name: "fontloader" ))); | 
| 221 |     QVERIFY(fontObject != nullptr); | 
| 222 |     QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Ready); | 
| 223 |     QVERIFY(fontObject->source() != QUrl("" )); | 
| 224 |     QTRY_COMPARE(fontObject->name(), QString("OCRA" )); | 
| 225 |  | 
| 226 |     window.rootObject()->setProperty(name: "usename" , value: true); | 
| 227 |  | 
| 228 |     // This warning should probably not be printed once QTBUG-20268 is fixed | 
| 229 |     QString warning = QString(testFileUrl(fileName: "qtbug-20268.qml" ).toString()) + | 
| 230 |                               QLatin1String(":13:5: QML FontLoader: Cannot load font: \"\"" ); | 
| 231 |     QTest::ignoreMessage(type: QtWarningMsg, qPrintable(warning)); | 
| 232 |  | 
| 233 |     QEXPECT_FAIL("" , "QTBUG-20268" , Abort); | 
| 234 |     QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Ready); | 
| 235 |     QCOMPARE(window.rootObject()->property("name" ).toString(), QString("Tahoma" )); | 
| 236 | } | 
| 237 |  | 
| 238 | QTEST_MAIN(tst_qquickfontloader) | 
| 239 |  | 
| 240 | #include "tst_qquickfontloader.moc" | 
| 241 |  |