~gabriel1984sibiu/minitube/qt5.6

« back to all changes in this revision

Viewing changes to tests/auto/gui/text/qfontcache/tst_qfontcache.cpp

  • Committer: Grevutiu Gabriel
  • Date: 2017-06-13 08:43:17 UTC
  • Revision ID: gabriel1984sibiu@gmail.com-20170613084317-ek0zqe0u9g3ocvi8
OriginalĀ upstreamĀ code

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
29
 
 
30
#include <QtTest/QtTest>
 
31
 
 
32
 
 
33
#include <qfont.h>
 
34
#include <private/qfont_p.h>
 
35
#include <private/qfontengine_p.h>
 
36
 
 
37
class tst_QFontCache : public QObject
 
38
{
 
39
Q_OBJECT
 
40
 
 
41
public:
 
42
    tst_QFontCache();
 
43
    virtual ~tst_QFontCache();
 
44
 
 
45
private slots:
 
46
    void engineData_data();
 
47
    void engineData();
 
48
 
 
49
    void clear();
 
50
};
 
51
 
 
52
#ifdef QT_BUILD_INTERNAL
 
53
QT_BEGIN_NAMESPACE
 
54
// qfontdatabase.cpp
 
55
Q_AUTOTEST_EXPORT void qt_setQtEnableTestFont(bool value);
 
56
// qfontengine.cpp
 
57
Q_AUTOTEST_EXPORT void QFontEngine_startCollectingEngines();
 
58
Q_AUTOTEST_EXPORT QList<QFontEngine *> QFontEngine_stopCollectingEngines();
 
59
QT_END_NAMESPACE
 
60
#endif
 
61
 
 
62
tst_QFontCache::tst_QFontCache()
 
63
{
 
64
}
 
65
 
 
66
tst_QFontCache::~tst_QFontCache()
 
67
{
 
68
}
 
69
 
 
70
void tst_QFontCache::engineData_data()
 
71
{
 
72
    QTest::addColumn<QString>("family");
 
73
    QTest::addColumn<QString>("cacheKey");
 
74
 
 
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");
 
84
}
 
85
 
 
86
void tst_QFontCache::engineData()
 
87
{
 
88
    QFETCH(QString, family);
 
89
    QFETCH(QString, cacheKey);
 
90
 
 
91
    QFont f(family);
 
92
    f.exactMatch(); // loads engine
 
93
 
 
94
    QFontPrivate *d = QFontPrivate::get(f);
 
95
 
 
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);
 
101
    }
 
102
    if (req.pointSize < 0)
 
103
        req.pointSize = req.pixelSize*72.0/d->dpi;
 
104
    if (req.stretch == 0)
 
105
        req.stretch = 100;
 
106
 
 
107
    req.family = cacheKey;
 
108
 
 
109
    QFontEngineData *engineData = QFontCache::instance()->findEngineData(req);
 
110
 
 
111
    QCOMPARE(engineData, QFontPrivate::get(f)->engineData);
 
112
}
 
113
 
 
114
void tst_QFontCache::clear()
 
115
{
 
116
#ifdef QT_BUILD_INTERNAL
 
117
    QFontEngine_startCollectingEngines();
 
118
#else
 
119
    // must not crash, at very least ;)
 
120
#endif
 
121
 
 
122
    QFontEngine *fontEngine = 0;
 
123
 
 
124
#ifdef QT_BUILD_INTERNAL
 
125
    {
 
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);
 
129
 
 
130
        QFont f;
 
131
        f.setFamily("__Qt__Box__Engine__");
 
132
        f.exactMatch(); // loads engine
 
133
    }
 
134
#endif
 
135
    {
 
136
        QFontDatabase db;
 
137
 
 
138
        QFont f;
 
139
        f.setStyleHint(QFont::Serif);
 
140
        const QString familyForHint(f.defaultFamily());
 
141
 
 
142
        // it should at least return a family that is available
 
143
        QVERIFY(db.hasFamily(familyForHint));
 
144
        f.exactMatch(); // loads engine
 
145
 
 
146
        fontEngine = QFontPrivate::get(f)->engineForScript(QChar::Script_Common);
 
147
        QVERIFY(fontEngine);
 
148
        QVERIFY(QFontCache::instance()->engineCacheCount.value(fontEngine) > 0); // ensure it is cached
 
149
 
 
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();
 
153
 
 
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);
 
156
    }
 
157
 
 
158
    // use it:
 
159
    // e.g. fontEngine->stringToCMap(..);
 
160
 
 
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();
 
164
 
 
165
    // release the acquired engine:
 
166
    if (fontEngine) {
 
167
        if (!fontEngine->ref.deref())
 
168
            delete fontEngine;
 
169
        fontEngine = 0;
 
170
    }
 
171
 
 
172
    // we may even exit the application now:
 
173
    QFontCache::instance()->cleanup();
 
174
 
 
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);
 
180
#endif
 
181
}
 
182
 
 
183
QTEST_MAIN(tst_QFontCache)
 
184
#include "tst_qfontcache.moc"