author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
3 |
** Copyright (C) 2009 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 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
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 |
QTest::newRow("default") << defaultFont; |
|
127 |
defaultFont.setPointSize(defaultFont.pointSize() + 10); |
|
128 |
QTest::newRow("default") << defaultFont; |
|
129 |
QFontDatabase db; |
|
130 |
QStringList list = db.families(); |
|
131 |
for (int i = 0; i < list.count(); ++i) { |
|
132 |
QFont f = QFont(QFontInfo(QFont(list.at(i))).family()); |
|
133 |
QTest::newRow(qPrintable(list.at(i))) << f; |
|
134 |
} |
|
135 |
} |
|
136 |
||
137 |
// public QFont currentFont() const |
|
138 |
void tst_QFontComboBox::currentFont() |
|
139 |
{ |
|
140 |
QFETCH(QFont, currentFont); |
|
141 |
||
142 |
SubQFontComboBox box; |
|
143 |
QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&))); |
|
144 |
QFont oldCurrentFont = box.currentFont(); |
|
145 |
||
146 |
box.setCurrentFont(currentFont); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
147 |
QRegExp foundry(" \\[.*\\]"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
148 |
if (!box.currentFont().family().contains(foundry)) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
149 |
QCOMPARE(box.currentFont(), currentFont); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
150 |
} |
0 | 151 |
QString boxFontFamily = QFontInfo(box.currentFont()).family(); |
152 |
if (!currentFont.family().contains(foundry)) |
|
153 |
boxFontFamily.remove(foundry); |
|
154 |
QCOMPARE(boxFontFamily, currentFont.family()); |
|
155 |
||
156 |
if (oldCurrentFont != box.currentFont()) { |
|
157 |
//the signal may be emit twice if there is a foundry into brackets |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
158 |
QCOMPARE(spy0.count(),1); |
0 | 159 |
} |
160 |
} |
|
161 |
||
162 |
Q_DECLARE_METATYPE(QFontComboBox::FontFilters) |
|
163 |
void tst_QFontComboBox::fontFilters_data() |
|
164 |
{ |
|
165 |
QTest::addColumn<QFontComboBox::FontFilters>("fontFilters"); |
|
166 |
QTest::newRow("AllFonts") |
|
167 |
<< QFontComboBox::FontFilters(QFontComboBox::AllFonts); |
|
168 |
QTest::newRow("ScalableFonts") |
|
169 |
<< QFontComboBox::FontFilters(QFontComboBox::ScalableFonts); |
|
170 |
QTest::newRow("NonScalableFonts") |
|
171 |
<< QFontComboBox::FontFilters(QFontComboBox::NonScalableFonts); |
|
172 |
QTest::newRow("MonospacedFonts") |
|
173 |
<< QFontComboBox::FontFilters(QFontComboBox::MonospacedFonts); |
|
174 |
QTest::newRow("ProportionalFonts") |
|
175 |
<< QFontComboBox::FontFilters(QFontComboBox::ProportionalFonts); |
|
176 |
||
177 |
// combine two |
|
178 |
QTest::newRow("ProportionalFonts | NonScalableFonts") |
|
179 |
<< QFontComboBox::FontFilters(QFontComboBox::ProportionalFonts | QFontComboBox::NonScalableFonts); |
|
180 |
||
181 |
// i.e. all |
|
182 |
QTest::newRow("ScalableFonts | NonScalableFonts") |
|
183 |
<< QFontComboBox::FontFilters(QFontComboBox::ScalableFonts | QFontComboBox::NonScalableFonts); |
|
184 |
||
185 |
} |
|
186 |
||
187 |
// public QFontComboBox::FontFilters fontFilters() const |
|
188 |
void tst_QFontComboBox::fontFilters() |
|
189 |
{ |
|
190 |
QFETCH(QFontComboBox::FontFilters, fontFilters); |
|
191 |
||
192 |
SubQFontComboBox box; |
|
193 |
QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&))); |
|
194 |
QFont currentFont = box.currentFont(); |
|
195 |
||
196 |
box.setFontFilters(fontFilters); |
|
197 |
QCOMPARE(box.fontFilters(), fontFilters); |
|
198 |
||
199 |
QFontDatabase db; |
|
200 |
QStringList list = db.families(); |
|
201 |
int c = 0; |
|
202 |
const int scalableMask = (QFontComboBox::ScalableFonts | QFontComboBox::NonScalableFonts); |
|
203 |
const int spacingMask = (QFontComboBox::ProportionalFonts | QFontComboBox::MonospacedFonts); |
|
204 |
if((fontFilters & scalableMask) == scalableMask) |
|
205 |
fontFilters &= ~scalableMask; |
|
206 |
if((fontFilters & spacingMask) == spacingMask) |
|
207 |
fontFilters &= ~spacingMask; |
|
208 |
||
209 |
for (int i = 0; i < list.count(); ++i) { |
|
210 |
if (fontFilters & QFontComboBox::ScalableFonts) { |
|
211 |
if (!db.isSmoothlyScalable(list[i])) |
|
212 |
continue; |
|
213 |
} else if (fontFilters & QFontComboBox::NonScalableFonts) { |
|
214 |
if (db.isSmoothlyScalable(list[i])) |
|
215 |
continue; |
|
216 |
} |
|
217 |
if (fontFilters & QFontComboBox::MonospacedFonts) { |
|
218 |
if (!db.isFixedPitch(list[i])) |
|
219 |
continue; |
|
220 |
} else if (fontFilters & QFontComboBox::ProportionalFonts) { |
|
221 |
if (db.isFixedPitch(list[i])) |
|
222 |
continue; |
|
223 |
} |
|
224 |
c++; |
|
225 |
} |
|
226 |
||
227 |
QCOMPARE(box.model()->rowCount(), c); |
|
228 |
||
229 |
if (c == 0) |
|
230 |
QCOMPARE(box.currentFont(), QFont()); |
|
231 |
||
232 |
QCOMPARE(spy0.count(), (currentFont != box.currentFont()) ? 1 : 0); |
|
233 |
} |
|
234 |
||
235 |
// public QSize sizeHint() const |
|
236 |
void tst_QFontComboBox::sizeHint() |
|
237 |
{ |
|
238 |
SubQFontComboBox box; |
|
239 |
QSize sizeHint = box.QComboBox::sizeHint(); |
|
240 |
QFontMetrics fm(box.font()); |
|
241 |
sizeHint.setWidth(qMax(sizeHint.width(), fm.width(QLatin1Char('m'))*14)); |
|
242 |
QCOMPARE(box.sizeHint(), sizeHint); |
|
243 |
} |
|
244 |
||
245 |
Q_DECLARE_METATYPE(QFontDatabase::WritingSystem) |
|
246 |
void tst_QFontComboBox::writingSystem_data() |
|
247 |
{ |
|
248 |
QTest::addColumn<QFontDatabase::WritingSystem>("writingSystem"); |
|
249 |
QTest::newRow("Any") << QFontDatabase::Any; |
|
250 |
QTest::newRow("Latin") << QFontDatabase::Latin; |
|
251 |
QTest::newRow("Lao") << QFontDatabase::Lao; |
|
252 |
QTest::newRow("TraditionalChinese") << QFontDatabase::TraditionalChinese; |
|
253 |
QTest::newRow("Ogham") << QFontDatabase::Ogham; |
|
254 |
QTest::newRow("Runic") << QFontDatabase::Runic; |
|
255 |
||
256 |
for (int i = 0; i < 31; ++i) |
|
257 |
QTest::newRow("enum") << (QFontDatabase::WritingSystem)i; |
|
258 |
} |
|
259 |
||
260 |
// public QFontDatabase::WritingSystem writingSystem() const |
|
261 |
void tst_QFontComboBox::writingSystem() |
|
262 |
{ |
|
263 |
QFETCH(QFontDatabase::WritingSystem, writingSystem); |
|
264 |
||
265 |
SubQFontComboBox box; |
|
266 |
QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&))); |
|
267 |
QFont currentFont = box.currentFont(); |
|
268 |
||
269 |
box.setWritingSystem(writingSystem); |
|
270 |
QCOMPARE(box.writingSystem(), writingSystem); |
|
271 |
||
272 |
QFontDatabase db; |
|
273 |
QStringList list = db.families(writingSystem); |
|
274 |
QCOMPARE(box.model()->rowCount(), list.count()); |
|
275 |
||
276 |
if (list.count() == 0) |
|
277 |
QCOMPARE(box.currentFont(), QFont()); |
|
278 |
||
279 |
QCOMPARE(spy0.count(), (currentFont != box.currentFont()) ? 1 : 0); |
|
280 |
} |
|
281 |
||
282 |
// protected void currentFontChanged(QFont const& f) |
|
283 |
void tst_QFontComboBox::currentFontChanged() |
|
284 |
{ |
|
285 |
SubQFontComboBox box; |
|
286 |
QSignalSpy spy0(&box, SIGNAL(currentFontChanged(QFont const&))); |
|
287 |
||
288 |
if (box.model()->rowCount() > 2) { |
|
289 |
QTest::keyPress(&box, Qt::Key_Down); |
|
290 |
QCOMPARE(spy0.count(), 1); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
291 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
292 |
QFont f( "Sans Serif" ); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
293 |
box.setCurrentFont(f); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
294 |
QCOMPARE(spy0.count(), 2); |
0 | 295 |
} else |
296 |
qWarning("Not enough fonts installed on test system. Consider adding some"); |
|
297 |
} |
|
298 |
||
299 |
QTEST_MAIN(tst_QFontComboBox) |
|
300 |
#include "tst_qfontcombobox.moc" |
|
301 |