1
/****************************************************************************
3
** Copyright (C) 2016 The Qt Company Ltd.
4
** Contact: https://www.qt.io/licensing/
6
** This file is part of the test suite of the Qt Toolkit.
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.
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.
27
****************************************************************************/
30
#include <QtTest/QtTest>
34
#include <private/qfont_p.h>
35
#include <private/qfontengine_p.h>
37
class tst_QFontCache : public QObject
43
virtual ~tst_QFontCache();
46
void engineData_data();
52
#ifdef QT_BUILD_INTERNAL
55
Q_AUTOTEST_EXPORT void qt_setQtEnableTestFont(bool value);
57
Q_AUTOTEST_EXPORT void QFontEngine_startCollectingEngines();
58
Q_AUTOTEST_EXPORT QList<QFontEngine *> QFontEngine_stopCollectingEngines();
62
tst_QFontCache::tst_QFontCache()
66
tst_QFontCache::~tst_QFontCache()
70
void tst_QFontCache::engineData_data()
72
QTest::addColumn<QString>("family");
73
QTest::addColumn<QString>("cacheKey");
75
QTest::newRow("unquoted-family-name") << QString("Times New Roman") << QString("Times New Roman");
76
QTest::newRow("quoted-family-name") << QString("'Times New Roman'") << QString("Times New Roman");
77
QTest::newRow("invalid") << QString("invalid") << QString("invalid");
78
QTest::newRow("multiple") << QString("invalid, Times New Roman") << QString("invalid,Times New Roman");
79
QTest::newRow("multiple spaces") << QString("invalid, Times New Roman ") << QString("invalid,Times New Roman");
80
QTest::newRow("multiple spaces quotes") << QString("'invalid', Times New Roman ") << QString("invalid,Times New Roman");
81
QTest::newRow("multiple2") << QString("invalid, Times New Roman , foobar, 'baz'") << QString("invalid,Times New Roman,foobar,baz");
82
QTest::newRow("invalid spaces") << QString("invalid spaces, Times New Roman ") << QString("invalid spaces,Times New Roman");
83
QTest::newRow("invalid spaces quotes") << QString("'invalid spaces', 'Times New Roman' ") << QString("invalid spaces,Times New Roman");
86
void tst_QFontCache::engineData()
88
QFETCH(QString, family);
89
QFETCH(QString, cacheKey);
92
f.exactMatch(); // loads engine
94
QFontPrivate *d = QFontPrivate::get(f);
96
QFontDef req = d->request;
97
// copy-pasted from QFontDatabase::load(), to engineer the cache key
98
if (req.pixelSize == -1) {
99
req.pixelSize = std::floor(((req.pointSize * d->dpi) / 72) * 100 + 0.5) / 100;
100
req.pixelSize = qRound(req.pixelSize);
102
if (req.pointSize < 0)
103
req.pointSize = req.pixelSize*72.0/d->dpi;
104
if (req.stretch == 0)
107
req.family = cacheKey;
109
QFontEngineData *engineData = QFontCache::instance()->findEngineData(req);
111
QCOMPARE(engineData, QFontPrivate::get(f)->engineData);
114
void tst_QFontCache::clear()
116
#ifdef QT_BUILD_INTERNAL
117
QFontEngine_startCollectingEngines();
119
// must not crash, at very least ;)
122
QFontEngine *fontEngine = 0;
124
#ifdef QT_BUILD_INTERNAL
126
// we're never caching the box (and the "test") font engines
127
// let's ensure we're not leaking them as well as the cached ones
128
qt_setQtEnableTestFont(true);
131
f.setFamily("__Qt__Box__Engine__");
132
f.exactMatch(); // loads engine
139
f.setStyleHint(QFont::Serif);
140
const QString familyForHint(f.defaultFamily());
142
// it should at least return a family that is available
143
QVERIFY(db.hasFamily(familyForHint));
144
f.exactMatch(); // loads engine
146
fontEngine = QFontPrivate::get(f)->engineForScript(QChar::Script_Common);
148
QVERIFY(QFontCache::instance()->engineCacheCount.value(fontEngine) > 0); // ensure it is cached
150
// acquire the engine to use it somewhere else:
151
// (e.g. like the we do in QFontSubset() or like QRawFont does in fromFont())
152
fontEngine->ref.ref();
154
// cache the engine once again; there is a special case when the engine is cached more than once
155
QFontCache::instance()->insertEngine(QFontCache::Key(QFontDef(), 0, 1), fontEngine);
159
// e.g. fontEngine->stringToCMap(..);
161
// and whilst it is alive, don't hesitate to add/remove the app-local fonts:
162
// (QFontDatabase::{add,remove}ApplicationFont() clears the cache)
163
QFontCache::instance()->clear();
165
// release the acquired engine:
167
if (!fontEngine->ref.deref())
172
// we may even exit the application now:
173
QFontCache::instance()->cleanup();
175
#ifdef QT_BUILD_INTERNAL
176
QList<QFontEngine *> leakedEngines = QFontEngine_stopCollectingEngines();
177
for (int i = 0; i < leakedEngines.size(); ++i) qWarning() << i << leakedEngines.at(i) << leakedEngines.at(i)->ref.load();
178
// and we are not leaking!
179
QCOMPARE(leakedEngines.size(), 0);
183
QTEST_MAIN(tst_QFontCache)
184
#include "tst_qfontcache.moc"