author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 11:15:19 +0300 | |
branch | RCL_3 |
changeset 11 | 25a739ee40f4 |
parent 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
||
45 |
#include <qicon.h> |
|
46 |
||
47 |
#if defined(Q_OS_SYMBIAN) |
|
48 |
#define SRCDIR "." |
|
49 |
#endif |
|
50 |
#include <qiconengine.h> |
|
51 |
||
52 |
Q_DECLARE_METATYPE(QSize) |
|
53 |
||
54 |
//TESTED_CLASS= |
|
55 |
//TESTED_FILES= |
|
56 |
||
57 |
class tst_QIcon : public QObject |
|
58 |
{ |
|
59 |
Q_OBJECT |
|
60 |
public: |
|
61 |
tst_QIcon(); |
|
62 |
||
63 |
private slots: |
|
64 |
void init(); |
|
65 |
void cleanup(); |
|
66 |
||
67 |
void actualSize_data(); // test with 1 pixmap |
|
68 |
void actualSize(); |
|
69 |
void actualSize2_data(); // test with 2 pixmaps with different aspect ratio |
|
70 |
void actualSize2(); |
|
71 |
void svgActualSize(); |
|
72 |
void isNull(); |
|
73 |
void bestMatch(); |
|
74 |
void cacheKey(); |
|
75 |
void detach(); |
|
76 |
void svg(); |
|
77 |
void addFile(); |
|
78 |
void availableSizes(); |
|
79 |
void streamAvailableSizes_data(); |
|
80 |
void streamAvailableSizes(); |
|
81 |
void fromTheme(); |
|
82 |
||
83 |
void task184901_badCache(); |
|
84 |
void task223279_inconsistentAddFile(); |
|
85 |
void task239461_custom_iconengine_crash(); |
|
86 |
||
87 |
private: |
|
88 |
QString oldCurrentDir; |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
89 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
90 |
const static QIcon staticIcon; |
0 | 91 |
}; |
92 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
93 |
// Creating an icon statically should not cause a crash. |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
94 |
// But we do not officially support this. See QTBUG-8666 |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
95 |
const QIcon tst_QIcon::staticIcon = QIcon::fromTheme("edit-find"); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
96 |
|
0 | 97 |
void tst_QIcon::init() |
98 |
{ |
|
99 |
QString srcdir(QLatin1String(SRCDIR)); |
|
100 |
if (!srcdir.isEmpty()) { |
|
101 |
oldCurrentDir = QDir::current().absolutePath(); |
|
102 |
QDir::setCurrent(srcdir); |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
void tst_QIcon::cleanup() |
|
107 |
{ |
|
108 |
if (!oldCurrentDir.isEmpty()) { |
|
109 |
QDir::setCurrent(oldCurrentDir); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
tst_QIcon::tst_QIcon() |
|
114 |
{ |
|
115 |
} |
|
116 |
||
117 |
void tst_QIcon::actualSize_data() |
|
118 |
{ |
|
119 |
QTest::addColumn<QString>("source"); |
|
120 |
QTest::addColumn<QSize>("argument"); |
|
121 |
QTest::addColumn<QSize>("result"); |
|
122 |
||
123 |
// square image |
|
124 |
QTest::newRow("resource0") << ":/image.png" << QSize(128, 128) << QSize(128, 128); |
|
125 |
QTest::newRow("resource1") << ":/image.png" << QSize( 64, 64) << QSize( 64, 64); |
|
126 |
QTest::newRow("resource2") << ":/image.png" << QSize( 32, 64) << QSize( 32, 32); |
|
127 |
QTest::newRow("resource3") << ":/image.png" << QSize( 16, 64) << QSize( 16, 16); |
|
128 |
QTest::newRow("resource4") << ":/image.png" << QSize( 16, 128) << QSize( 16, 16); |
|
129 |
QTest::newRow("resource5") << ":/image.png" << QSize( 128, 16) << QSize( 16, 16); |
|
130 |
QTest::newRow("resource6") << ":/image.png" << QSize( 150, 150) << QSize( 128, 128); |
|
131 |
// rect image |
|
132 |
QTest::newRow("resource7") << ":/rect.png" << QSize( 20, 40) << QSize( 20, 40); |
|
133 |
QTest::newRow("resource8") << ":/rect.png" << QSize( 10, 20) << QSize( 10, 20); |
|
134 |
QTest::newRow("resource9") << ":/rect.png" << QSize( 15, 50) << QSize( 15, 30); |
|
135 |
QTest::newRow("resource10") << ":/rect.png" << QSize( 25, 50) << QSize( 20, 40); |
|
136 |
||
137 |
const QString prefix = QLatin1String(SRCDIR) + QLatin1String("/"); |
|
138 |
QTest::newRow("external0") << prefix + "image.png" << QSize(128, 128) << QSize(128, 128); |
|
139 |
QTest::newRow("external1") << prefix + "image.png" << QSize( 64, 64) << QSize( 64, 64); |
|
140 |
QTest::newRow("external2") << prefix + "image.png" << QSize( 32, 64) << QSize( 32, 32); |
|
141 |
QTest::newRow("external3") << prefix + "image.png" << QSize( 16, 64) << QSize( 16, 16); |
|
142 |
QTest::newRow("external4") << prefix + "image.png" << QSize( 16, 128) << QSize( 16, 16); |
|
143 |
QTest::newRow("external5") << prefix + "image.png" << QSize( 128, 16) << QSize( 16, 16); |
|
144 |
QTest::newRow("external6") << prefix + "image.png" << QSize( 150, 150) << QSize( 128, 128); |
|
145 |
// rect image |
|
146 |
QTest::newRow("external7") << ":/rect.png" << QSize( 20, 40) << QSize( 20, 40); |
|
147 |
QTest::newRow("external8") << ":/rect.png" << QSize( 10, 20) << QSize( 10, 20); |
|
148 |
QTest::newRow("external9") << ":/rect.png" << QSize( 15, 50) << QSize( 15, 30); |
|
149 |
QTest::newRow("external10") << ":/rect.png" << QSize( 25, 50) << QSize( 20, 40); |
|
150 |
} |
|
151 |
||
152 |
void tst_QIcon::actualSize() |
|
153 |
{ |
|
154 |
QFETCH(QString, source); |
|
155 |
QFETCH(QSize, argument); |
|
156 |
QFETCH(QSize, result); |
|
157 |
||
158 |
{ |
|
159 |
QPixmap pixmap(source); |
|
160 |
QIcon icon(pixmap); |
|
161 |
QCOMPARE(icon.actualSize(argument), result); |
|
162 |
QCOMPARE(icon.pixmap(argument).size(), result); |
|
163 |
} |
|
164 |
||
165 |
{ |
|
166 |
QIcon icon(source); |
|
167 |
QCOMPARE(icon.actualSize(argument), result); |
|
168 |
QCOMPARE(icon.pixmap(argument).size(), result); |
|
169 |
} |
|
170 |
} |
|
171 |
||
172 |
void tst_QIcon::actualSize2_data() |
|
173 |
{ |
|
174 |
QTest::addColumn<QSize>("argument"); |
|
175 |
QTest::addColumn<QSize>("result"); |
|
176 |
||
177 |
// two images - 128x128 and 20x40. Let the games begin |
|
178 |
QTest::newRow("trivial1") << QSize( 128, 128) << QSize( 128, 128); |
|
179 |
QTest::newRow("trivial2") << QSize( 20, 40) << QSize( 20, 40); |
|
180 |
||
181 |
// QIcon chooses the one with the smallest area to choose the pixmap |
|
182 |
QTest::newRow("best1") << QSize( 100, 100) << QSize( 100, 100); |
|
183 |
QTest::newRow("best2") << QSize( 20, 20) << QSize( 10, 20); |
|
184 |
QTest::newRow("best3") << QSize( 15, 30) << QSize( 15, 30); |
|
185 |
QTest::newRow("best4") << QSize( 5, 5) << QSize( 2, 5); |
|
186 |
QTest::newRow("best5") << QSize( 10, 15) << QSize( 7, 15); |
|
187 |
} |
|
188 |
||
189 |
void tst_QIcon::actualSize2() |
|
190 |
{ |
|
191 |
QIcon icon; |
|
192 |
const QString prefix = QLatin1String(SRCDIR) + QLatin1String("/"); |
|
193 |
||
194 |
icon.addPixmap(QPixmap(prefix + "image.png")); |
|
195 |
icon.addPixmap(QPixmap(prefix + "rect.png")); |
|
196 |
||
197 |
QFETCH(QSize, argument); |
|
198 |
QFETCH(QSize, result); |
|
199 |
||
200 |
QCOMPARE(icon.actualSize(argument), result); |
|
201 |
QCOMPARE(icon.pixmap(argument).size(), result); |
|
202 |
} |
|
203 |
||
204 |
void tst_QIcon::svgActualSize() |
|
205 |
{ |
|
206 |
const QString prefix = QLatin1String(SRCDIR) + QLatin1String("/"); |
|
207 |
QIcon icon(prefix + "rect.svg"); |
|
208 |
QCOMPARE(icon.actualSize(QSize(16, 16)), QSize(16, 2)); |
|
209 |
QCOMPARE(icon.pixmap(QSize(16, 16)).size(), QSize(16, 2)); |
|
210 |
||
211 |
QPixmap p(16, 16); |
|
212 |
p.fill(Qt::cyan); |
|
213 |
icon.addPixmap(p); |
|
214 |
||
215 |
QCOMPARE(icon.actualSize(QSize(16, 16)), QSize(16, 16)); |
|
216 |
QCOMPARE(icon.pixmap(QSize(16, 16)).size(), QSize(16, 16)); |
|
217 |
||
218 |
QCOMPARE(icon.actualSize(QSize(16, 14)), QSize(16, 2)); |
|
219 |
QCOMPARE(icon.pixmap(QSize(16, 14)).size(), QSize(16, 2)); |
|
220 |
} |
|
221 |
||
222 |
void tst_QIcon::isNull() { |
|
223 |
// test default constructor |
|
224 |
QIcon defaultConstructor; |
|
225 |
QVERIFY(defaultConstructor.isNull()); |
|
226 |
||
227 |
// test copy constructor |
|
228 |
QVERIFY(QIcon(defaultConstructor).isNull()); |
|
229 |
||
230 |
// test pixmap constructor |
|
231 |
QPixmap nullPixmap; |
|
232 |
QVERIFY(QIcon(nullPixmap).isNull()); |
|
233 |
||
234 |
// test string constructor with empty string |
|
235 |
QIcon iconEmptyString = QIcon(QString()); |
|
236 |
QVERIFY(iconEmptyString.isNull()); |
|
237 |
QVERIFY(!iconEmptyString.actualSize(QSize(32, 32)).isValid());; |
|
238 |
||
239 |
// test string constructor with non-existing file |
|
240 |
QIcon iconNoFile = QIcon("imagedoesnotexist"); |
|
241 |
QVERIFY(!iconNoFile.isNull()); |
|
242 |
QVERIFY(!iconNoFile.actualSize(QSize(32, 32)).isValid()); |
|
243 |
||
244 |
// test string constructor with non-existing file with suffix |
|
245 |
QIcon iconNoFileSuffix = QIcon("imagedoesnotexist.png"); |
|
246 |
QVERIFY(!iconNoFileSuffix.isNull()); |
|
247 |
QVERIFY(!iconNoFileSuffix.actualSize(QSize(32, 32)).isValid()); |
|
248 |
||
249 |
const QString prefix = QLatin1String(SRCDIR) + QLatin1String("/"); |
|
250 |
// test string constructor with existing file but unsupported format |
|
251 |
QIcon iconUnsupportedFormat = QIcon(prefix + "tst_qicon.cpp"); |
|
252 |
QVERIFY(!iconUnsupportedFormat.isNull()); |
|
253 |
QVERIFY(!iconUnsupportedFormat.actualSize(QSize(32, 32)).isValid()); |
|
254 |
||
255 |
// test string constructor with existing file and supported format |
|
256 |
QIcon iconSupportedFormat = QIcon(prefix + "image.png"); |
|
257 |
QVERIFY(!iconSupportedFormat.isNull()); |
|
258 |
QVERIFY(iconSupportedFormat.actualSize(QSize(32, 32)).isValid()); |
|
259 |
} |
|
260 |
||
261 |
void tst_QIcon::bestMatch() |
|
262 |
{ |
|
263 |
QPixmap p1(1, 1); |
|
264 |
QPixmap p2(2, 2); |
|
265 |
QPixmap p3(3, 3); |
|
266 |
QPixmap p4(4, 4); |
|
267 |
QPixmap p5(5, 5); |
|
268 |
QPixmap p6(6, 6); |
|
269 |
QPixmap p7(7, 7); |
|
270 |
QPixmap p8(8, 8); |
|
271 |
||
272 |
p1.fill(Qt::black); |
|
273 |
p2.fill(Qt::black); |
|
274 |
p3.fill(Qt::black); |
|
275 |
p4.fill(Qt::black); |
|
276 |
p5.fill(Qt::black); |
|
277 |
p6.fill(Qt::black); |
|
278 |
p7.fill(Qt::black); |
|
279 |
p8.fill(Qt::black); |
|
280 |
||
281 |
for (int i = 0; i < 4; ++i) { |
|
282 |
for (int j = 0; j < 2; ++j) { |
|
283 |
QIcon::State state = (j == 0) ? QIcon::On : QIcon::Off; |
|
284 |
QIcon::State oppositeState = (state == QIcon::On) ? QIcon::Off |
|
285 |
: QIcon::On; |
|
286 |
QIcon::Mode mode; |
|
287 |
QIcon::Mode oppositeMode; |
|
288 |
||
289 |
QIcon icon; |
|
290 |
||
291 |
switch (i) { |
|
292 |
case 0: |
|
293 |
default: |
|
294 |
mode = QIcon::Normal; |
|
295 |
oppositeMode = QIcon::Active; |
|
296 |
break; |
|
297 |
case 1: |
|
298 |
mode = QIcon::Active; |
|
299 |
oppositeMode = QIcon::Normal; |
|
300 |
break; |
|
301 |
case 2: |
|
302 |
mode = QIcon::Disabled; |
|
303 |
oppositeMode = QIcon::Selected; |
|
304 |
break; |
|
305 |
case 3: |
|
306 |
mode = QIcon::Selected; |
|
307 |
oppositeMode = QIcon::Disabled; |
|
308 |
} |
|
309 |
||
310 |
/* |
|
311 |
The test mirrors the code in |
|
312 |
QPixmapIconEngine::bestMatch(), to make sure that |
|
313 |
nobody breaks QPixmapIconEngine by mistake. Before |
|
314 |
you change this test or the code that it tests, |
|
315 |
please talk to the maintainer if possible. |
|
316 |
*/ |
|
317 |
if (mode == QIcon::Disabled || mode == QIcon::Selected) { |
|
318 |
icon.addPixmap(p1, oppositeMode, oppositeState); |
|
319 |
QVERIFY(icon.pixmap(100, mode, state).size() == p1.size()); |
|
320 |
||
321 |
icon.addPixmap(p2, oppositeMode, state); |
|
322 |
QVERIFY(icon.pixmap(100, mode, state).size() == p2.size()); |
|
323 |
||
324 |
icon.addPixmap(p3, QIcon::Active, oppositeState); |
|
325 |
QVERIFY(icon.pixmap(100, mode, state).size() == p3.size()); |
|
326 |
||
327 |
icon.addPixmap(p4, QIcon::Normal, oppositeState); |
|
328 |
QVERIFY(icon.pixmap(100, mode, state).size() == p4.size()); |
|
329 |
||
330 |
icon.addPixmap(p5, mode, oppositeState); |
|
331 |
QVERIFY(icon.pixmap(100, mode, state).size() == p5.size()); |
|
332 |
||
333 |
icon.addPixmap(p6, QIcon::Active, state); |
|
334 |
QVERIFY(icon.pixmap(100, mode, state).size() == p6.size()); |
|
335 |
||
336 |
icon.addPixmap(p7, QIcon::Normal, state); |
|
337 |
QVERIFY(icon.pixmap(100, mode, state).size() == p7.size()); |
|
338 |
||
339 |
icon.addPixmap(p8, mode, state); |
|
340 |
QVERIFY(icon.pixmap(100, mode, state).size() == p8.size()); |
|
341 |
} else { |
|
342 |
icon.addPixmap(p1, QIcon::Selected, oppositeState); |
|
343 |
QVERIFY(icon.pixmap(100, mode, state).size() == p1.size()); |
|
344 |
||
345 |
icon.addPixmap(p2, QIcon::Disabled, oppositeState); |
|
346 |
QVERIFY(icon.pixmap(100, mode, state).size() == p2.size()); |
|
347 |
||
348 |
icon.addPixmap(p3, QIcon::Selected, state); |
|
349 |
QVERIFY(icon.pixmap(100, mode, state).size() == p3.size()); |
|
350 |
||
351 |
icon.addPixmap(p4, QIcon::Disabled, state); |
|
352 |
QVERIFY(icon.pixmap(100, mode, state).size() == p4.size()); |
|
353 |
||
354 |
icon.addPixmap(p5, oppositeMode, oppositeState); |
|
355 |
QVERIFY(icon.pixmap(100, mode, state).size() == p5.size()); |
|
356 |
||
357 |
icon.addPixmap(p6, mode, oppositeState); |
|
358 |
QVERIFY(icon.pixmap(100, mode, state).size() == p6.size()); |
|
359 |
||
360 |
icon.addPixmap(p7, oppositeMode, state); |
|
361 |
QVERIFY(icon.pixmap(100, mode, state).size() == p7.size()); |
|
362 |
||
363 |
icon.addPixmap(p8, mode, state); |
|
364 |
QVERIFY(icon.pixmap(100, mode, state).size() == p8.size()); |
|
365 |
} |
|
366 |
} |
|
367 |
} |
|
368 |
} |
|
369 |
||
370 |
void tst_QIcon::cacheKey() |
|
371 |
{ |
|
372 |
QIcon icon1("image.png"); |
|
373 |
qint64 icon1_key = icon1.cacheKey(); |
|
374 |
QIcon icon2 = icon1; |
|
375 |
||
376 |
QVERIFY(icon2.cacheKey() == icon1.cacheKey()); |
|
377 |
icon2.detach(); |
|
378 |
QVERIFY(icon2.cacheKey() != icon1.cacheKey()); |
|
379 |
QVERIFY(icon1.cacheKey() == icon1_key); |
|
380 |
} |
|
381 |
||
382 |
void tst_QIcon::detach() |
|
383 |
{ |
|
384 |
QImage img(32, 32, QImage::Format_ARGB32_Premultiplied); |
|
385 |
img.fill(0xffff0000); |
|
386 |
QIcon icon1(QPixmap::fromImage(img)); |
|
387 |
QIcon icon2 = icon1; |
|
388 |
icon2.addFile("image.png", QSize(64, 64)); |
|
389 |
||
390 |
QImage img1 = icon1.pixmap(64, 64).toImage(); |
|
391 |
QImage img2 = icon2.pixmap(64, 64).toImage(); |
|
392 |
QVERIFY(img1 != img2); |
|
393 |
||
394 |
img1 = icon1.pixmap(32, 32).toImage(); |
|
395 |
img2 = icon2.pixmap(32, 32).toImage(); |
|
396 |
QVERIFY(img1 == img2); |
|
397 |
} |
|
398 |
||
399 |
void tst_QIcon::svg() |
|
400 |
{ |
|
401 |
QIcon icon1("heart.svg"); |
|
402 |
||
403 |
QVERIFY(!icon1.pixmap(32).isNull()); |
|
404 |
QImage img1 = icon1.pixmap(32).toImage(); |
|
405 |
QVERIFY(!icon1.pixmap(32, QIcon::Disabled).isNull()); |
|
406 |
QImage img2 = icon1.pixmap(32, QIcon::Disabled).toImage(); |
|
407 |
||
408 |
icon1.addFile("trash.svg", QSize(), QIcon::Disabled); |
|
409 |
QVERIFY(!icon1.pixmap(32, QIcon::Disabled).isNull()); |
|
410 |
QImage img3 = icon1.pixmap(32, QIcon::Disabled).toImage(); |
|
411 |
QVERIFY(img3 != img2); |
|
412 |
QVERIFY(img3 != img1); |
|
413 |
||
414 |
QPixmap pm("image.png"); |
|
415 |
icon1.addPixmap(pm, QIcon::Normal, QIcon::On); |
|
416 |
QVERIFY(!icon1.pixmap(128, QIcon::Normal, QIcon::On).isNull()); |
|
417 |
QImage img4 = icon1.pixmap(128, QIcon::Normal, QIcon::On).toImage(); |
|
418 |
QVERIFY(img4 != img3); |
|
419 |
QVERIFY(img4 != img2); |
|
420 |
QVERIFY(img4 != img1); |
|
421 |
||
422 |
QIcon icon2; |
|
423 |
icon2.addFile("heart.svg"); |
|
424 |
QVERIFY(icon2.pixmap(57).toImage() == icon1.pixmap(57).toImage()); |
|
425 |
||
426 |
QIcon icon3("trash.svg"); |
|
427 |
icon3.addFile("heart.svg"); |
|
428 |
QVERIFY(icon3.pixmap(57).toImage() == icon1.pixmap(57).toImage()); |
|
429 |
||
430 |
QIcon icon4("heart.svg"); |
|
431 |
icon4.addFile("image.png", QSize(), QIcon::Active); |
|
432 |
QVERIFY(!icon4.pixmap(32).isNull()); |
|
433 |
QVERIFY(!icon4.pixmap(32, QIcon::Active).isNull()); |
|
434 |
QVERIFY(icon4.pixmap(32).toImage() == img1); |
|
435 |
QIcon pmIcon(pm); |
|
436 |
QVERIFY(icon4.pixmap(pm.size(), QIcon::Active).toImage() == pmIcon.pixmap(pm.size(), QIcon::Active).toImage()); |
|
437 |
||
438 |
#ifndef QT_NO_COMPRESS |
|
439 |
QIcon icon5("heart.svgz"); |
|
440 |
QVERIFY(!icon5.pixmap(32).isNull()); |
|
441 |
#endif |
|
442 |
} |
|
443 |
||
444 |
void tst_QIcon::addFile() |
|
445 |
{ |
|
446 |
QIcon icon; |
|
447 |
icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")); |
|
448 |
icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-32.png")); |
|
449 |
icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-128.png")); |
|
450 |
icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-16.png"), QSize(), QIcon::Selected); |
|
451 |
icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-32.png"), QSize(), QIcon::Selected); |
|
452 |
icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-128.png"), QSize(), QIcon::Selected); |
|
453 |
||
454 |
#ifndef Q_OS_WINCE |
|
455 |
QVERIFY(icon.pixmap(16, QIcon::Normal).toImage() == |
|
456 |
QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")).toImage()); |
|
457 |
QVERIFY(icon.pixmap(32, QIcon::Normal).toImage() == |
|
458 |
QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-32.png")).toImage()); |
|
459 |
QVERIFY(icon.pixmap(128, QIcon::Normal).toImage() == |
|
460 |
QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-128.png")).toImage()); |
|
461 |
QVERIFY(icon.pixmap(16, QIcon::Selected).toImage() == |
|
462 |
QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-16.png")).toImage()); |
|
463 |
QVERIFY(icon.pixmap(32, QIcon::Selected).toImage() == |
|
464 |
QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-32.png")).toImage()); |
|
465 |
QVERIFY(icon.pixmap(128, QIcon::Selected).toImage() == |
|
466 |
QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-128.png")).toImage()); |
|
467 |
#else |
|
468 |
// WinCE only includes the 16x16 images for size reasons |
|
469 |
QVERIFY(icon.pixmap(16, QIcon::Normal).toImage() == |
|
470 |
QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")).toImage()); |
|
471 |
QVERIFY(icon.pixmap(16, QIcon::Selected).toImage() == |
|
472 |
QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-16.png")).toImage()); |
|
473 |
#endif |
|
474 |
} |
|
475 |
||
476 |
static bool sizeLess(const QSize &a, const QSize &b) |
|
477 |
{ |
|
478 |
return a.width() < b.width(); |
|
479 |
} |
|
480 |
||
481 |
void tst_QIcon::availableSizes() |
|
482 |
{ |
|
483 |
{ |
|
484 |
QIcon icon; |
|
485 |
icon.addFile("image.png", QSize(32,32)); |
|
486 |
icon.addFile("image.png", QSize(64,64)); |
|
487 |
icon.addFile("image.png", QSize(128,128)); |
|
488 |
icon.addFile("image.png", QSize(256,256), QIcon::Disabled); |
|
489 |
icon.addFile("image.png", QSize(16,16), QIcon::Normal, QIcon::On); |
|
490 |
||
491 |
QList<QSize> availableSizes = icon.availableSizes(); |
|
492 |
QCOMPARE(availableSizes.size(), 3); |
|
493 |
qSort(availableSizes.begin(), availableSizes.end(), sizeLess); |
|
494 |
QCOMPARE(availableSizes.at(0), QSize(32,32)); |
|
495 |
QCOMPARE(availableSizes.at(1), QSize(64,64)); |
|
496 |
QCOMPARE(availableSizes.at(2), QSize(128,128)); |
|
497 |
||
498 |
availableSizes = icon.availableSizes(QIcon::Disabled); |
|
499 |
QCOMPARE(availableSizes.size(), 1); |
|
500 |
QCOMPARE(availableSizes.at(0), QSize(256,256)); |
|
501 |
||
502 |
availableSizes = icon.availableSizes(QIcon::Normal, QIcon::On); |
|
503 |
QCOMPARE(availableSizes.size(), 1); |
|
504 |
QCOMPARE(availableSizes.at(0), QSize(16,16)); |
|
505 |
} |
|
506 |
||
507 |
{ |
|
508 |
// checks that there are no availableSizes for scalable images. |
|
509 |
QIcon icon("heart.svg"); |
|
510 |
QList<QSize> availableSizes = icon.availableSizes(); |
|
511 |
QVERIFY(availableSizes.isEmpty()); |
|
512 |
} |
|
513 |
||
514 |
{ |
|
515 |
// even if an a scalable image contain added pixmaps, |
|
516 |
// availableSizes still should be empty. |
|
517 |
QIcon icon("heart.svg"); |
|
518 |
icon.addFile("image.png", QSize(32,32)); |
|
519 |
QList<QSize> availableSizes = icon.availableSizes(); |
|
520 |
QVERIFY(availableSizes.isEmpty()); |
|
521 |
} |
|
522 |
||
523 |
{ |
|
524 |
// we try to load an icon from resources |
|
525 |
QIcon icon(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")); |
|
526 |
QList<QSize> availableSizes = icon.availableSizes(); |
|
527 |
QCOMPARE(availableSizes.size(), 1); |
|
528 |
QCOMPARE(availableSizes.at(0), QSize(16, 16)); |
|
529 |
} |
|
530 |
||
531 |
{ |
|
532 |
// load an icon from binary data. |
|
533 |
QPixmap pix; |
|
534 |
QFile file(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")); |
|
535 |
QVERIFY(file.open(QIODevice::ReadOnly)); |
|
536 |
uchar *data = file.map(0, file.size()); |
|
537 |
QVERIFY(data != 0); |
|
538 |
pix.loadFromData(data, file.size()); |
|
539 |
QIcon icon(pix); |
|
540 |
||
541 |
QList<QSize> availableSizes = icon.availableSizes(); |
|
542 |
QCOMPARE(availableSizes.size(), 1); |
|
543 |
QCOMPARE(availableSizes.at(0), QSize(16,16)); |
|
544 |
} |
|
545 |
||
546 |
{ |
|
547 |
// there shouldn't be available sizes for invalid images! |
|
548 |
QVERIFY(QIcon(QLatin1String("")).availableSizes().isEmpty()); |
|
549 |
QVERIFY(QIcon(QLatin1String("non-existing.png")).availableSizes().isEmpty()); |
|
550 |
} |
|
551 |
} |
|
552 |
||
553 |
void tst_QIcon::streamAvailableSizes_data() |
|
554 |
{ |
|
555 |
QTest::addColumn<QIcon>("icon"); |
|
556 |
||
557 |
QIcon icon; |
|
558 |
icon.addFile(":/image.png", QSize(32,32)); |
|
559 |
QTest::newRow( "32x32" ) << icon; |
|
560 |
icon.addFile(":/image.png", QSize(64,64)); |
|
561 |
QTest::newRow( "64x64" ) << icon; |
|
562 |
icon.addFile(":/image.png", QSize(128,128)); |
|
563 |
QTest::newRow( "128x128" ) << icon; |
|
564 |
icon.addFile(":/image.png", QSize(256,256)); |
|
565 |
QTest::newRow( "256x256" ) << icon; |
|
566 |
} |
|
567 |
||
568 |
void tst_QIcon::streamAvailableSizes() |
|
569 |
{ |
|
570 |
QFETCH(QIcon, icon); |
|
571 |
||
572 |
QByteArray ba; |
|
573 |
// write to QByteArray |
|
574 |
{ |
|
575 |
QBuffer buffer(&ba); |
|
576 |
buffer.open(QIODevice::WriteOnly); |
|
577 |
QDataStream stream(&buffer); |
|
578 |
stream << icon; |
|
579 |
} |
|
580 |
||
581 |
// read from QByteArray |
|
582 |
{ |
|
583 |
QBuffer buffer(&ba); |
|
584 |
buffer.open(QIODevice::ReadOnly); |
|
585 |
QDataStream stream(&buffer); |
|
586 |
QIcon i; |
|
587 |
stream >> i; |
|
588 |
QCOMPARE(i.isNull(), icon.isNull()); |
|
589 |
QCOMPARE(i.availableSizes(), icon.availableSizes()); |
|
590 |
} |
|
591 |
} |
|
592 |
||
593 |
||
594 |
static inline bool operator<(const QSize &lhs, const QSize &rhs) |
|
595 |
{ |
|
596 |
if (lhs.width() < rhs.width()) |
|
597 |
return true; |
|
598 |
else if (lhs.width() == lhs.width()) |
|
599 |
return lhs.height() < lhs.height(); |
|
600 |
return false; |
|
601 |
} |
|
602 |
||
603 |
void tst_QIcon::task184901_badCache() |
|
604 |
{ |
|
605 |
QPixmap pm("image.png"); |
|
606 |
QIcon icon(pm); |
|
607 |
||
608 |
//the disabled icon must have an effect (grayed) |
|
609 |
QVERIFY(icon.pixmap(32, QIcon::Normal).toImage() != icon.pixmap(32, QIcon::Disabled).toImage()); |
|
610 |
||
611 |
icon.addPixmap(pm, QIcon::Disabled); |
|
612 |
//the disabled icon must now be the same as the normal one. |
|
613 |
QVERIFY( icon.pixmap(32, QIcon::Normal).toImage() == icon.pixmap(32, QIcon::Disabled).toImage() ); |
|
614 |
} |
|
615 |
||
616 |
void tst_QIcon::fromTheme() |
|
617 |
{ |
|
618 |
QString searchPath = QLatin1String(":/icons"); |
|
619 |
QIcon::setThemeSearchPaths(QStringList() << searchPath); |
|
620 |
QVERIFY(QIcon::themeSearchPaths().size() == 1); |
|
621 |
QCOMPARE(searchPath, QIcon::themeSearchPaths()[0]); |
|
622 |
||
623 |
QString themeName("testtheme"); |
|
624 |
QIcon::setThemeName(themeName); |
|
625 |
QCOMPARE(QIcon::themeName(), themeName); |
|
626 |
||
627 |
// Test normal icon |
|
628 |
QIcon appointmentIcon = QIcon::fromTheme("appointment-new"); |
|
629 |
QVERIFY(!appointmentIcon.isNull()); |
|
630 |
QVERIFY(!appointmentIcon.availableSizes(QIcon::Normal, QIcon::Off).isEmpty()); |
|
631 |
QVERIFY(appointmentIcon.availableSizes().contains(QSize(16, 16))); |
|
632 |
QVERIFY(appointmentIcon.availableSizes().contains(QSize(32, 32))); |
|
633 |
QVERIFY(appointmentIcon.availableSizes().contains(QSize(22, 22))); |
|
634 |
||
635 |
// Test icon from parent theme |
|
636 |
QIcon abIcon = QIcon::fromTheme("address-book-new"); |
|
637 |
QVERIFY(!abIcon.isNull()); |
|
638 |
QVERIFY(QIcon::hasThemeIcon("address-book-new")); |
|
639 |
QVERIFY(!abIcon.availableSizes().isEmpty()); |
|
640 |
||
641 |
// Test non existing icon |
|
642 |
QIcon noIcon = QIcon::fromTheme("broken-icon"); |
|
643 |
QVERIFY(noIcon.isNull()); |
|
644 |
QVERIFY(!QIcon::hasThemeIcon("broken-icon")); |
|
645 |
||
646 |
// Test non existing icon with fallback |
|
647 |
noIcon = QIcon::fromTheme("broken-icon", abIcon); |
|
648 |
QVERIFY(noIcon.cacheKey() == abIcon.cacheKey()); |
|
649 |
||
650 |
// Test svg-only icon |
|
651 |
noIcon = QIcon::fromTheme("svg-icon", abIcon); |
|
652 |
QVERIFY(!noIcon.availableSizes().isEmpty()); |
|
653 |
||
654 |
QByteArray ba; |
|
655 |
// write to QByteArray |
|
656 |
{ |
|
657 |
QBuffer buffer(&ba); |
|
658 |
buffer.open(QIODevice::WriteOnly); |
|
659 |
QDataStream stream(&buffer); |
|
660 |
stream << abIcon; |
|
661 |
} |
|
662 |
||
663 |
// read from QByteArray |
|
664 |
{ |
|
665 |
QBuffer buffer(&ba); |
|
666 |
buffer.open(QIODevice::ReadOnly); |
|
667 |
QDataStream stream(&buffer); |
|
668 |
QIcon i; |
|
669 |
stream >> i; |
|
670 |
QCOMPARE(i.isNull(), abIcon.isNull()); |
|
671 |
QCOMPARE(i.availableSizes(), abIcon.availableSizes()); |
|
672 |
} |
|
673 |
||
674 |
// Make sure setting the theme name clears the state |
|
675 |
QIcon::setThemeName(""); |
|
676 |
abIcon = QIcon::fromTheme("address-book-new"); |
|
677 |
QVERIFY(abIcon.isNull()); |
|
678 |
} |
|
679 |
||
680 |
||
681 |
void tst_QIcon::task223279_inconsistentAddFile() |
|
682 |
{ |
|
683 |
QIcon icon1; |
|
684 |
icon1.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")); |
|
685 |
icon1.addFile(QLatin1String("IconThatDoesntExist"), QSize(32, 32)); |
|
686 |
QPixmap pm1 = icon1.pixmap(32, 32); |
|
687 |
||
688 |
QIcon icon2; |
|
689 |
icon2.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")); |
|
690 |
icon2.addFile(QLatin1String("IconThatDoesntExist")); |
|
691 |
QPixmap pm2 = icon1.pixmap(32, 32); |
|
692 |
||
693 |
QCOMPARE(pm1.isNull(), false); |
|
694 |
QCOMPARE(pm1.size(), QSize(16,16)); |
|
695 |
QCOMPARE(pm1.isNull(), pm2.isNull()); |
|
696 |
QCOMPARE(pm1.size(), pm2.size()); |
|
697 |
} |
|
698 |
||
699 |
||
700 |
// During detach, v2 engines are cloned, while v1 engines are only |
|
701 |
// passed on, so v1 engines need to be referenced counted. This test |
|
702 |
// verifies that the engine is destroyed once and only once. |
|
703 |
||
704 |
class IconEngine : public QIconEngine |
|
705 |
{ |
|
706 |
public: |
|
707 |
~IconEngine() { destructorCalled++; } |
|
708 |
virtual void paint(QPainter *, const QRect &, QIcon::Mode, QIcon::State) { } |
|
709 |
static int destructorCalled; |
|
710 |
}; |
|
711 |
int IconEngine::destructorCalled = 0; |
|
712 |
||
713 |
void tst_QIcon::task239461_custom_iconengine_crash() |
|
714 |
{ |
|
715 |
QIconEngine *engine = new IconEngine(); |
|
716 |
{ |
|
717 |
QIcon icon(engine); |
|
718 |
QIcon icon2 = icon; |
|
719 |
||
720 |
QPixmap pixmap(32, 32); |
|
721 |
icon.addPixmap(pixmap); |
|
722 |
} |
|
723 |
QCOMPARE(IconEngine::destructorCalled, 1); |
|
724 |
} |
|
725 |
||
726 |
||
727 |
QTEST_MAIN(tst_QIcon) |
|
728 |
#include "tst_qicon.moc" |