72b3dfe8ca8162c62ce4c14b931ed574616727d5
[profile/ivi/qtbase.git] / tests / auto / widgets / widgets / qfontcombobox / tst_qfontcombobox.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include <QtTest/QtTest>
44 #include <qfontcombobox.h>
45
46 class tst_QFontComboBox : public QObject
47 {
48     Q_OBJECT
49
50 public slots:
51     void initTestCase();
52     void cleanupTestCase();
53     void init();
54     void cleanup();
55
56 private slots:
57     void qfontcombobox_data();
58     void qfontcombobox();
59     void currentFont_data();
60     void currentFont();
61     void fontFilters_data();
62     void fontFilters();
63     void sizeHint();
64     void writingSystem_data();
65     void writingSystem();
66     void currentFontChanged();
67 };
68
69 // Subclass that exposes the protected functions.
70 class SubQFontComboBox : public QFontComboBox
71 {
72 public:
73     void call_currentFontChanged(QFont const& f)
74         { return SubQFontComboBox::currentFontChanged(f); }
75
76     bool call_event(QEvent* e)
77         { return SubQFontComboBox::event(e); }
78 };
79
80 // This will be called before the first test function is executed.
81 // It is only called once.
82 void tst_QFontComboBox::initTestCase()
83 {
84 }
85
86 // This will be called after the last test function is executed.
87 // It is only called once.
88 void tst_QFontComboBox::cleanupTestCase()
89 {
90 }
91
92 // This will be called before each test function is executed.
93 void tst_QFontComboBox::init()
94 {
95 }
96
97 // This will be called after every test function.
98 void tst_QFontComboBox::cleanup()
99 {
100 }
101
102 void tst_QFontComboBox::qfontcombobox_data()
103 {
104 }
105
106 void tst_QFontComboBox::qfontcombobox()
107 {
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);
119 }
120
121 void tst_QFontComboBox::currentFont_data()
122 {
123     QTest::addColumn<QFont>("currentFont");
124     // Normalize the names
125     QFont defaultFont;
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;
131     QFontDatabase db;
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;
136     }
137 }
138
139 // public QFont currentFont() const
140 void tst_QFontComboBox::currentFont()
141 {
142     QFETCH(QFont, currentFont);
143
144     SubQFontComboBox box;
145     QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&)));
146     QFont oldCurrentFont = box.currentFont();
147
148     box.setCurrentFont(currentFont);
149     QRegExp foundry(" \\[.*\\]");
150     if (!box.currentFont().family().contains(foundry)) {
151         QCOMPARE(box.currentFont(), currentFont);
152     }
153     QString boxFontFamily = QFontInfo(box.currentFont()).family();
154     if (!currentFont.family().contains(foundry))
155         boxFontFamily.remove(foundry);
156     QCOMPARE(boxFontFamily, currentFont.family());
157
158     if (oldCurrentFont != box.currentFont()) {
159         //the signal may be emit twice if there is a foundry into brackets
160         QCOMPARE(spy0.count(),1);
161     }
162 }
163
164 Q_DECLARE_METATYPE(QFontComboBox::FontFilters)
165 void tst_QFontComboBox::fontFilters_data()
166 {
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);
178
179     // combine two
180     QTest::newRow("ProportionalFonts | NonScalableFonts")
181         << QFontComboBox::FontFilters(QFontComboBox::ProportionalFonts | QFontComboBox::NonScalableFonts);
182
183     // i.e. all
184     QTest::newRow("ScalableFonts | NonScalableFonts")
185         << QFontComboBox::FontFilters(QFontComboBox::ScalableFonts | QFontComboBox::NonScalableFonts);
186
187 }
188
189 // public QFontComboBox::FontFilters fontFilters() const
190 void tst_QFontComboBox::fontFilters()
191 {
192     QFETCH(QFontComboBox::FontFilters, fontFilters);
193
194     SubQFontComboBox box;
195     QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&)));
196     QFont currentFont = box.currentFont();
197
198     box.setFontFilters(fontFilters);
199     QCOMPARE(box.fontFilters(), fontFilters);
200
201     QFontDatabase db;
202     QStringList list = db.families();
203     int c = 0;
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;
210
211     for (int i = 0; i < list.count(); ++i) {
212         if (fontFilters & QFontComboBox::ScalableFonts) {
213             if (!db.isSmoothlyScalable(list[i]))
214                 continue;
215         } else if (fontFilters & QFontComboBox::NonScalableFonts) {
216             if (db.isSmoothlyScalable(list[i]))
217                 continue;
218         }
219         if (fontFilters & QFontComboBox::MonospacedFonts) {
220             if (!db.isFixedPitch(list[i]))
221                 continue;
222         } else if (fontFilters & QFontComboBox::ProportionalFonts) {
223             if (db.isFixedPitch(list[i]))
224                 continue;
225         }
226         c++;
227     }
228
229     QCOMPARE(box.model()->rowCount(), c);
230
231     if (c == 0)
232         QCOMPARE(box.currentFont(), QFont());
233
234     QCOMPARE(spy0.count(), (currentFont != box.currentFont()) ? 1 : 0);
235 }
236
237 // public QSize sizeHint() const
238 void tst_QFontComboBox::sizeHint()
239 {
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);
245 }
246
247 Q_DECLARE_METATYPE(QFontDatabase::WritingSystem)
248 void tst_QFontComboBox::writingSystem_data()
249 {
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;
257
258     for (int i = 0; i < 31; ++i)
259         QTest::newRow("enum") << (QFontDatabase::WritingSystem)i;
260 }
261
262 // public QFontDatabase::WritingSystem writingSystem() const
263 void tst_QFontComboBox::writingSystem()
264 {
265     QFETCH(QFontDatabase::WritingSystem, writingSystem);
266
267     SubQFontComboBox box;
268     QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&)));
269     QFont currentFont = box.currentFont();
270
271     box.setWritingSystem(writingSystem);
272     QCOMPARE(box.writingSystem(), writingSystem);
273
274     QFontDatabase db;
275     QStringList list = db.families(writingSystem);
276     QCOMPARE(box.model()->rowCount(), list.count());
277
278     if (list.count() == 0)
279         QCOMPARE(box.currentFont(), QFont());
280
281     QCOMPARE(spy0.count(), (currentFont != box.currentFont()) ? 1 : 0);
282 }
283
284 // protected void currentFontChanged(QFont const& f)
285 void tst_QFontComboBox::currentFontChanged()
286 {
287     SubQFontComboBox box;
288     QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&)));
289
290     if (box.model()->rowCount() > 2) {
291         QTest::keyPress(&box, Qt::Key_Down);
292         QCOMPARE(spy0.count(), 1);
293
294         QFont f( "Sans Serif" );
295         box.setCurrentFont(f);
296         QCOMPARE(spy0.count(), 2);
297     } else
298         qWarning("Not enough fonts installed on test system. Consider adding some");
299 }
300
301 QTEST_MAIN(tst_QFontComboBox)
302 #include "tst_qfontcombobox.moc"
303