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 | #include <qfontcombobox.h> |
32 | |
33 | class tst_QFontComboBox : public QObject |
34 | { |
35 | Q_OBJECT |
36 | |
37 | private slots: |
38 | void initTestCase(); |
39 | |
40 | void qfontcombobox_data(); |
41 | void qfontcombobox(); |
42 | void currentFont_data(); |
43 | void currentFont(); |
44 | void fontFilters_data(); |
45 | void fontFilters(); |
46 | void sizeHint(); |
47 | void writingSystem_data(); |
48 | void writingSystem(); |
49 | void currentFontChanged(); |
50 | }; |
51 | |
52 | // Subclass that exposes the protected functions. |
53 | class SubQFontComboBox : public QFontComboBox |
54 | { |
55 | public: |
56 | void call_currentFontChanged(QFont const& f) |
57 | { return SubQFontComboBox::currentFontChanged(f); } |
58 | |
59 | bool call_event(QEvent* e) |
60 | { return SubQFontComboBox::event(e); } |
61 | }; |
62 | |
63 | void tst_QFontComboBox::initTestCase() |
64 | { |
65 | if (QGuiApplication::platformName().startsWith(s: QLatin1String("wayland" ), cs: Qt::CaseInsensitive)) |
66 | QSKIP("Wayland: This freezes. Figure out why." ); |
67 | } |
68 | |
69 | void tst_QFontComboBox::qfontcombobox_data() |
70 | { |
71 | } |
72 | |
73 | void tst_QFontComboBox::qfontcombobox() |
74 | { |
75 | SubQFontComboBox box; |
76 | QCOMPARE(box.currentFont(), QFont()); |
77 | QCOMPARE(box.fontFilters(), QFontComboBox::AllFonts); |
78 | box.setCurrentFont(QFont()); |
79 | box.setFontFilters(QFontComboBox::AllFonts); |
80 | box.setWritingSystem(QFontDatabase::Any); |
81 | QVERIFY(box.sizeHint() != QSize()); |
82 | QCOMPARE(box.writingSystem(), QFontDatabase::Any); |
83 | box.call_currentFontChanged(f: QFont()); |
84 | QEvent event(QEvent::None); |
85 | QCOMPARE(box.call_event(&event), false); |
86 | } |
87 | |
88 | void tst_QFontComboBox::currentFont_data() |
89 | { |
90 | QTest::addColumn<QFont>(name: "currentFont" ); |
91 | QFontDatabase db; |
92 | // Normalize the names |
93 | QFont defaultFont; |
94 | QFontInfo fi(defaultFont); |
95 | defaultFont = QFont(fi.family()); // make sure we have a real font name and not something like 'Sans Serif'. |
96 | if (!db.isPrivateFamily(family: defaultFont.family())) |
97 | QTest::newRow(dataTag: "default" ) << defaultFont; |
98 | defaultFont.setPointSize(defaultFont.pointSize() + 10); |
99 | if (!db.isPrivateFamily(family: defaultFont.family())) |
100 | QTest::newRow(dataTag: "default2" ) << defaultFont; |
101 | QStringList list = db.families(); |
102 | for (int i = 0; i < list.count(); ++i) { |
103 | QFont f = QFont(QFontInfo(QFont(list.at(i))).family()); |
104 | if (!db.isPrivateFamily(family: f.family())) |
105 | QTest::newRow(qPrintable(list.at(i))) << f; |
106 | } |
107 | } |
108 | |
109 | // public QFont currentFont() const |
110 | void tst_QFontComboBox::currentFont() |
111 | { |
112 | QFETCH(QFont, currentFont); |
113 | |
114 | SubQFontComboBox box; |
115 | QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont))); |
116 | QFont oldCurrentFont = box.currentFont(); |
117 | |
118 | box.setCurrentFont(currentFont); |
119 | QRegularExpression foundry(" \\[.*\\]" ); |
120 | if (!box.currentFont().family().contains(re: foundry)) { |
121 | QCOMPARE(box.currentFont(), currentFont); |
122 | } |
123 | QString boxFontFamily = QFontInfo(box.currentFont()).family(); |
124 | if (!currentFont.family().contains(re: foundry)) |
125 | boxFontFamily.remove(re: foundry); |
126 | QCOMPARE(boxFontFamily, currentFont.family()); |
127 | |
128 | if (oldCurrentFont != box.currentFont()) { |
129 | //the signal may be emit twice if there is a foundry into brackets |
130 | QCOMPARE(spy0.count(),1); |
131 | } |
132 | } |
133 | |
134 | Q_DECLARE_METATYPE(QFontComboBox::FontFilters) |
135 | void tst_QFontComboBox::fontFilters_data() |
136 | { |
137 | QTest::addColumn<QFontComboBox::FontFilters>(name: "fontFilters" ); |
138 | QTest::newRow(dataTag: "AllFonts" ) |
139 | << QFontComboBox::FontFilters(QFontComboBox::AllFonts); |
140 | QTest::newRow(dataTag: "ScalableFonts" ) |
141 | << QFontComboBox::FontFilters(QFontComboBox::ScalableFonts); |
142 | QTest::newRow(dataTag: "NonScalableFonts" ) |
143 | << QFontComboBox::FontFilters(QFontComboBox::NonScalableFonts); |
144 | QTest::newRow(dataTag: "MonospacedFonts" ) |
145 | << QFontComboBox::FontFilters(QFontComboBox::MonospacedFonts); |
146 | QTest::newRow(dataTag: "ProportionalFonts" ) |
147 | << QFontComboBox::FontFilters(QFontComboBox::ProportionalFonts); |
148 | |
149 | // combine two |
150 | QTest::newRow(dataTag: "ProportionalFonts | NonScalableFonts" ) |
151 | << QFontComboBox::FontFilters(QFontComboBox::ProportionalFonts | QFontComboBox::NonScalableFonts); |
152 | |
153 | // i.e. all |
154 | QTest::newRow(dataTag: "ScalableFonts | NonScalableFonts" ) |
155 | << QFontComboBox::FontFilters(QFontComboBox::ScalableFonts | QFontComboBox::NonScalableFonts); |
156 | |
157 | } |
158 | |
159 | // public QFontComboBox::FontFilters fontFilters() const |
160 | void tst_QFontComboBox::fontFilters() |
161 | { |
162 | QFETCH(QFontComboBox::FontFilters, fontFilters); |
163 | |
164 | SubQFontComboBox box; |
165 | QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont))); |
166 | QFont currentFont = box.currentFont(); |
167 | |
168 | box.setFontFilters(fontFilters); |
169 | QCOMPARE(box.fontFilters(), fontFilters); |
170 | |
171 | QFontDatabase db; |
172 | QStringList list = db.families(); |
173 | int c = 0; |
174 | const int scalableMask = (QFontComboBox::ScalableFonts | QFontComboBox::NonScalableFonts); |
175 | const int spacingMask = (QFontComboBox::ProportionalFonts | QFontComboBox::MonospacedFonts); |
176 | if((fontFilters & scalableMask) == scalableMask) |
177 | fontFilters &= ~scalableMask; |
178 | if((fontFilters & spacingMask) == spacingMask) |
179 | fontFilters &= ~spacingMask; |
180 | |
181 | for (int i = 0; i < list.count(); ++i) { |
182 | if (db.isPrivateFamily(family: list[i])) |
183 | continue; |
184 | if (fontFilters & QFontComboBox::ScalableFonts) { |
185 | if (!db.isSmoothlyScalable(family: list[i])) |
186 | continue; |
187 | } else if (fontFilters & QFontComboBox::NonScalableFonts) { |
188 | if (db.isSmoothlyScalable(family: list[i])) |
189 | continue; |
190 | } |
191 | if (fontFilters & QFontComboBox::MonospacedFonts) { |
192 | if (!db.isFixedPitch(family: list[i])) |
193 | continue; |
194 | } else if (fontFilters & QFontComboBox::ProportionalFonts) { |
195 | if (db.isFixedPitch(family: list[i])) |
196 | continue; |
197 | } |
198 | c++; |
199 | } |
200 | |
201 | QCOMPARE(box.model()->rowCount(), c); |
202 | |
203 | if (c == 0) |
204 | QCOMPARE(box.currentFont(), QFont()); |
205 | |
206 | QCOMPARE(spy0.count(), (currentFont != box.currentFont()) ? 1 : 0); |
207 | } |
208 | |
209 | // public QSize sizeHint() const |
210 | void tst_QFontComboBox::sizeHint() |
211 | { |
212 | SubQFontComboBox box; |
213 | QSize sizeHint = box.QComboBox::sizeHint(); |
214 | QFontMetrics fm(box.font()); |
215 | sizeHint.setWidth(qMax(a: sizeHint.width(), b: fm.horizontalAdvance(QLatin1Char('m'))*14)); |
216 | QCOMPARE(box.sizeHint(), sizeHint); |
217 | } |
218 | |
219 | Q_DECLARE_METATYPE(QFontDatabase::WritingSystem) |
220 | void tst_QFontComboBox::writingSystem_data() |
221 | { |
222 | QTest::addColumn<QFontDatabase::WritingSystem>(name: "writingSystem" ); |
223 | QTest::newRow(dataTag: "Any" ) << QFontDatabase::Any; |
224 | QTest::newRow(dataTag: "Latin" ) << QFontDatabase::Latin; |
225 | QTest::newRow(dataTag: "Lao" ) << QFontDatabase::Lao; |
226 | QTest::newRow(dataTag: "TraditionalChinese" ) << QFontDatabase::TraditionalChinese; |
227 | QTest::newRow(dataTag: "Ogham" ) << QFontDatabase::Ogham; |
228 | QTest::newRow(dataTag: "Runic" ) << QFontDatabase::Runic; |
229 | |
230 | for (int i = 0; i < 31; ++i) |
231 | QTest::newRow(dataTag: ("enum " + QByteArray::number(i)).constData()) << (QFontDatabase::WritingSystem)i; |
232 | } |
233 | |
234 | // public QFontDatabase::WritingSystem writingSystem() const |
235 | void tst_QFontComboBox::writingSystem() |
236 | { |
237 | QFETCH(QFontDatabase::WritingSystem, writingSystem); |
238 | |
239 | SubQFontComboBox box; |
240 | QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont))); |
241 | QFont currentFont = box.currentFont(); |
242 | |
243 | box.setWritingSystem(writingSystem); |
244 | QCOMPARE(box.writingSystem(), writingSystem); |
245 | |
246 | QFontDatabase db; |
247 | QStringList list = db.families(writingSystem); |
248 | int c = list.count(); |
249 | for (int i = 0; i < list.count(); ++i) { |
250 | if (db.isPrivateFamily(family: list[i])) |
251 | c--; |
252 | } |
253 | QCOMPARE(box.model()->rowCount(), c); |
254 | |
255 | if (list.count() == 0) |
256 | QCOMPARE(box.currentFont(), QFont()); |
257 | |
258 | QCOMPARE(spy0.count(), (currentFont != box.currentFont()) ? 1 : 0); |
259 | } |
260 | |
261 | // protected void currentFontChanged(QFont const& f) |
262 | void tst_QFontComboBox::currentFontChanged() |
263 | { |
264 | // The absence of this file does not affect the test results |
265 | QFontDatabase::addApplicationFont(fileName: "ArianaVioleta-dz2K.ttf" ); |
266 | |
267 | SubQFontComboBox *box = new SubQFontComboBox; |
268 | QSignalSpy spy0(box, SIGNAL(currentFontChanged(QFont))); |
269 | |
270 | if (box->model()->rowCount() > 2) { |
271 | QTest::keyPress(widget: box, key: Qt::Key_Down); |
272 | QCOMPARE(spy0.count(), 1); |
273 | |
274 | QFont f( "Sans Serif" ); |
275 | box->setCurrentFont(f); |
276 | QCOMPARE(spy0.count(), 2); |
277 | } else |
278 | qWarning(msg: "Not enough fonts installed on test system. Consider adding some" ); |
279 | } |
280 | |
281 | QTEST_MAIN(tst_QFontComboBox) |
282 | #include "tst_qfontcombobox.moc" |
283 | |
284 | |