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 QtQml module 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 | #include <QFont> |
30 | #include <QFontDatabase> |
31 | #include <QString> |
32 | #include <QtTest> |
33 | #include <QCoreApplication> |
34 | |
35 | #include <QtQuick/private/qquickfontmetrics_p.h> |
36 | |
37 | #include <QFontMetricsF> |
38 | |
39 | class tst_QuickFontMetrics : public QObject |
40 | { |
41 | Q_OBJECT |
42 | |
43 | public: |
44 | tst_QuickFontMetrics(); |
45 | |
46 | private Q_SLOTS: |
47 | void properties(); |
48 | void functions_data(); |
49 | void functions(); |
50 | }; |
51 | |
52 | tst_QuickFontMetrics::tst_QuickFontMetrics() |
53 | { |
54 | } |
55 | |
56 | void tst_QuickFontMetrics::properties() |
57 | { |
58 | QStringList families = QFontDatabase().families().mid(pos: 0, alength: 10); |
59 | QQuickFontMetrics metrics; |
60 | |
61 | foreach (const QString &family, families) { |
62 | QFont font(family); |
63 | QFontMetricsF expected(font); |
64 | |
65 | QSignalSpy spy(&metrics, SIGNAL(fontChanged(QFont))); |
66 | metrics.setFont(font); |
67 | QCOMPARE(spy.count(), 1); |
68 | |
69 | QCOMPARE(metrics.ascent(), expected.ascent()); |
70 | QCOMPARE(metrics.descent(), expected.descent()); |
71 | QCOMPARE(metrics.height(), expected.height()); |
72 | QCOMPARE(metrics.leading(), expected.leading()); |
73 | QCOMPARE(metrics.lineSpacing(), expected.lineSpacing()); |
74 | QCOMPARE(metrics.minimumLeftBearing(), expected.minLeftBearing()); |
75 | QCOMPARE(metrics.minimumRightBearing(), expected.minRightBearing()); |
76 | QCOMPARE(metrics.maximumCharacterWidth(), expected.maxWidth()); |
77 | QCOMPARE(metrics.xHeight(), expected.xHeight()); |
78 | QCOMPARE(metrics.averageCharacterWidth(), expected.averageCharWidth()); |
79 | QCOMPARE(metrics.underlinePosition(), expected.underlinePos()); |
80 | QCOMPARE(metrics.overlinePosition(), expected.overlinePos()); |
81 | QCOMPARE(metrics.strikeOutPosition(), expected.strikeOutPos()); |
82 | QCOMPARE(metrics.lineWidth(), expected.lineWidth()); |
83 | } |
84 | } |
85 | |
86 | Q_DECLARE_METATYPE(Qt::TextElideMode) |
87 | |
88 | void tst_QuickFontMetrics::functions_data() |
89 | { |
90 | QTest::addColumn<QString>(name: "text" ); |
91 | QTest::addColumn<Qt::TextElideMode>(name: "mode" ); |
92 | QTest::addColumn<qreal>(name: "width" ); |
93 | QTest::addColumn<int>(name: "flags" ); |
94 | |
95 | QStringList strings; |
96 | strings << QString() |
97 | << QString::fromLatin1(str: "" ) |
98 | << QString::fromLatin1(str: "0" ) |
99 | << QString::fromLatin1(str: "@@@@@@@" ) |
100 | << QString::fromLatin1(str: "Hello" ); |
101 | |
102 | QVector<Qt::TextElideMode> elideModes; |
103 | elideModes << Qt::ElideLeft << Qt::ElideMiddle << Qt::ElideRight << Qt::ElideNone; |
104 | |
105 | for (int stringIndex = 0; stringIndex < strings.size(); ++stringIndex) { |
106 | const QString string = strings.at(i: stringIndex); |
107 | |
108 | for (int elideModeIndex = 0; elideModeIndex < elideModes.size(); ++elideModeIndex) { |
109 | Qt::TextElideMode elideMode = static_cast<Qt::TextElideMode>(elideModes.at(i: elideModeIndex)); |
110 | |
111 | for (qreal width = 0; width < 100; width += 20) { |
112 | const QString tag = QString::fromLatin1(str: "string=%1, mode=%2, width=%3" ).arg(a: string).arg(a: elideMode).arg(a: width); |
113 | QTest::newRow(qPrintable(tag)) << QString() << elideMode << width << 0; |
114 | } |
115 | } |
116 | } |
117 | } |
118 | |
119 | void tst_QuickFontMetrics::functions() |
120 | { |
121 | QFETCH(QString, text); |
122 | QFETCH(Qt::TextElideMode, mode); |
123 | QFETCH(qreal, width); |
124 | QFETCH(int, flags); |
125 | |
126 | QQuickFontMetrics metrics; |
127 | QFontMetricsF expected = QFontMetricsF(QFont()); |
128 | |
129 | QCOMPARE(metrics.elidedText(text, mode, width, flags), expected.elidedText(text, mode, width, flags)); |
130 | QCOMPARE(metrics.advanceWidth(text), expected.horizontalAdvance(text)); |
131 | QCOMPARE(metrics.boundingRect(text), expected.boundingRect(text)); |
132 | QCOMPARE(metrics.tightBoundingRect(text), expected.tightBoundingRect(text)); |
133 | } |
134 | |
135 | QTEST_MAIN(tst_QuickFontMetrics) |
136 | |
137 | #include "tst_quickfontmetrics.moc" |
138 | |