1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QtTest/QtTest>
44 #include <qfontcombobox.h>
46 class tst_QFontComboBox : public QObject
52 void cleanupTestCase();
57 void qfontcombobox_data();
59 void currentFont_data();
61 void fontFilters_data();
64 void writingSystem_data();
66 void currentFontChanged();
69 // Subclass that exposes the protected functions.
70 class SubQFontComboBox : public QFontComboBox
73 void call_currentFontChanged(QFont const& f)
74 { return SubQFontComboBox::currentFontChanged(f); }
76 bool call_event(QEvent* e)
77 { return SubQFontComboBox::event(e); }
80 // This will be called before the first test function is executed.
81 // It is only called once.
82 void tst_QFontComboBox::initTestCase()
86 // This will be called after the last test function is executed.
87 // It is only called once.
88 void tst_QFontComboBox::cleanupTestCase()
92 // This will be called before each test function is executed.
93 void tst_QFontComboBox::init()
97 // This will be called after every test function.
98 void tst_QFontComboBox::cleanup()
102 void tst_QFontComboBox::qfontcombobox_data()
106 void tst_QFontComboBox::qfontcombobox()
108 SubQFontComboBox box;
109 QCOMPARE(box.currentFont(), QFont());
110 QCOMPARE(box.fontFilters(), QFontComboBox::AllFonts);
111 box.setCurrentFont(QFont());
112 box.setFontFilters(QFontComboBox::AllFonts);
113 box.setWritingSystem(QFontDatabase::Any);
114 QVERIFY(box.sizeHint() != QSize());
115 QCOMPARE(box.writingSystem(), QFontDatabase::Any);
116 box.call_currentFontChanged(QFont());
117 QEvent event(QEvent::None);
118 QCOMPARE(box.call_event(&event), false);
121 void tst_QFontComboBox::currentFont_data()
123 QTest::addColumn<QFont>("currentFont");
124 // Normalize the names
126 QFontInfo fi(defaultFont);
127 defaultFont = QFont(fi.family()); // make sure we have a real font name and not something like 'Sans Serif'.
128 QTest::newRow("default") << defaultFont;
129 defaultFont.setPointSize(defaultFont.pointSize() + 10);
130 QTest::newRow("default2") << defaultFont;
132 QStringList list = db.families();
133 for (int i = 0; i < list.count(); ++i) {
134 QFont f = QFont(QFontInfo(QFont(list.at(i))).family());
135 QTest::newRow(qPrintable(list.at(i))) << f;
139 // public QFont currentFont() const
140 void tst_QFontComboBox::currentFont()
142 QFETCH(QFont, currentFont);
144 SubQFontComboBox box;
145 QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&)));
146 QFont oldCurrentFont = box.currentFont();
148 box.setCurrentFont(currentFont);
149 QRegExp foundry(" \\[.*\\]");
150 if (!box.currentFont().family().contains(foundry)) {
151 QCOMPARE(box.currentFont(), currentFont);
153 QString boxFontFamily = QFontInfo(box.currentFont()).family();
154 if (!currentFont.family().contains(foundry))
155 boxFontFamily.remove(foundry);
156 QCOMPARE(boxFontFamily, currentFont.family());
158 if (oldCurrentFont != box.currentFont()) {
159 //the signal may be emit twice if there is a foundry into brackets
160 QCOMPARE(spy0.count(),1);
164 Q_DECLARE_METATYPE(QFontComboBox::FontFilters)
165 void tst_QFontComboBox::fontFilters_data()
167 QTest::addColumn<QFontComboBox::FontFilters>("fontFilters");
168 QTest::newRow("AllFonts")
169 << QFontComboBox::FontFilters(QFontComboBox::AllFonts);
170 QTest::newRow("ScalableFonts")
171 << QFontComboBox::FontFilters(QFontComboBox::ScalableFonts);
172 QTest::newRow("NonScalableFonts")
173 << QFontComboBox::FontFilters(QFontComboBox::NonScalableFonts);
174 QTest::newRow("MonospacedFonts")
175 << QFontComboBox::FontFilters(QFontComboBox::MonospacedFonts);
176 QTest::newRow("ProportionalFonts")
177 << QFontComboBox::FontFilters(QFontComboBox::ProportionalFonts);
180 QTest::newRow("ProportionalFonts | NonScalableFonts")
181 << QFontComboBox::FontFilters(QFontComboBox::ProportionalFonts | QFontComboBox::NonScalableFonts);
184 QTest::newRow("ScalableFonts | NonScalableFonts")
185 << QFontComboBox::FontFilters(QFontComboBox::ScalableFonts | QFontComboBox::NonScalableFonts);
189 // public QFontComboBox::FontFilters fontFilters() const
190 void tst_QFontComboBox::fontFilters()
192 QFETCH(QFontComboBox::FontFilters, fontFilters);
194 SubQFontComboBox box;
195 QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&)));
196 QFont currentFont = box.currentFont();
198 box.setFontFilters(fontFilters);
199 QCOMPARE(box.fontFilters(), fontFilters);
202 QStringList list = db.families();
204 const int scalableMask = (QFontComboBox::ScalableFonts | QFontComboBox::NonScalableFonts);
205 const int spacingMask = (QFontComboBox::ProportionalFonts | QFontComboBox::MonospacedFonts);
206 if((fontFilters & scalableMask) == scalableMask)
207 fontFilters &= ~scalableMask;
208 if((fontFilters & spacingMask) == spacingMask)
209 fontFilters &= ~spacingMask;
211 for (int i = 0; i < list.count(); ++i) {
212 if (fontFilters & QFontComboBox::ScalableFonts) {
213 if (!db.isSmoothlyScalable(list[i]))
215 } else if (fontFilters & QFontComboBox::NonScalableFonts) {
216 if (db.isSmoothlyScalable(list[i]))
219 if (fontFilters & QFontComboBox::MonospacedFonts) {
220 if (!db.isFixedPitch(list[i]))
222 } else if (fontFilters & QFontComboBox::ProportionalFonts) {
223 if (db.isFixedPitch(list[i]))
229 QCOMPARE(box.model()->rowCount(), c);
232 QCOMPARE(box.currentFont(), QFont());
234 QCOMPARE(spy0.count(), (currentFont != box.currentFont()) ? 1 : 0);
237 // public QSize sizeHint() const
238 void tst_QFontComboBox::sizeHint()
240 SubQFontComboBox box;
241 QSize sizeHint = box.QComboBox::sizeHint();
242 QFontMetrics fm(box.font());
243 sizeHint.setWidth(qMax(sizeHint.width(), fm.width(QLatin1Char('m'))*14));
244 QCOMPARE(box.sizeHint(), sizeHint);
247 Q_DECLARE_METATYPE(QFontDatabase::WritingSystem)
248 void tst_QFontComboBox::writingSystem_data()
250 QTest::addColumn<QFontDatabase::WritingSystem>("writingSystem");
251 QTest::newRow("Any") << QFontDatabase::Any;
252 QTest::newRow("Latin") << QFontDatabase::Latin;
253 QTest::newRow("Lao") << QFontDatabase::Lao;
254 QTest::newRow("TraditionalChinese") << QFontDatabase::TraditionalChinese;
255 QTest::newRow("Ogham") << QFontDatabase::Ogham;
256 QTest::newRow("Runic") << QFontDatabase::Runic;
258 for (int i = 0; i < 31; ++i)
259 QTest::newRow("enum") << (QFontDatabase::WritingSystem)i;
262 // public QFontDatabase::WritingSystem writingSystem() const
263 void tst_QFontComboBox::writingSystem()
265 QFETCH(QFontDatabase::WritingSystem, writingSystem);
267 SubQFontComboBox box;
268 QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&)));
269 QFont currentFont = box.currentFont();
271 box.setWritingSystem(writingSystem);
272 QCOMPARE(box.writingSystem(), writingSystem);
275 QStringList list = db.families(writingSystem);
276 QCOMPARE(box.model()->rowCount(), list.count());
278 if (list.count() == 0)
279 QCOMPARE(box.currentFont(), QFont());
281 QCOMPARE(spy0.count(), (currentFont != box.currentFont()) ? 1 : 0);
284 // protected void currentFontChanged(QFont const& f)
285 void tst_QFontComboBox::currentFontChanged()
287 SubQFontComboBox box;
288 QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&)));
290 if (box.model()->rowCount() > 2) {
291 QTest::keyPress(&box, Qt::Key_Down);
292 QCOMPARE(spy0.count(), 1);
294 QFont f( "Sans Serif" );
295 box.setCurrentFont(f);
296 QCOMPARE(spy0.count(), 2);
298 qWarning("Not enough fonts installed on test system. Consider adding some");
301 QTEST_MAIN(tst_QFontComboBox)
302 #include "tst_qfontcombobox.moc"