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#include <QtTest/QtTest>
29#include <QtGui>
30#include <QtCore>
31
32class tst_QIcoImageFormat : public QObject
33{
34 Q_OBJECT
35
36private slots:
37 void initTestCase();
38 void format();
39 void canRead_data();
40 void canRead();
41 void SequentialFile_data();
42 void SequentialFile();
43 void imageCount_data();
44 void imageCount();
45 void jumpToNextImage_data();
46 void jumpToNextImage();
47 void loopCount_data();
48 void loopCount();
49 void nextImageDelay_data();
50 void nextImageDelay();
51 void pngCompression_data();
52 void pngCompression();
53 void write_data();
54 void write();
55
56private:
57 QString m_IconPath;
58};
59
60void tst_QIcoImageFormat::initTestCase()
61{
62 m_IconPath = QFINDTESTDATA("icons");
63 if (m_IconPath.isEmpty())
64 QFAIL("Cannot find icons directory containing testdata!");
65}
66
67void tst_QIcoImageFormat::format()
68{
69 {
70 QImageReader reader(m_IconPath + "/valid/35FLOPPY.ICO", "ico");
71 QByteArray fmt = reader.format();
72 QCOMPARE(const_cast<const char*>(fmt.data()), "ico" );
73 }
74
75 {
76 QImageReader reader(m_IconPath + "/valid/yellow.cur", "ico");
77 QByteArray fmt = reader.format();
78 QCOMPARE(const_cast<const char*>(fmt.data()), "ico" );
79 }
80}
81
82void tst_QIcoImageFormat::canRead_data()
83{
84 QTest::addColumn<QString>(name: "fileName");
85 QTest::addColumn<int>(name: "isValid");
86
87 QTest::newRow(dataTag: "floppy (16px,32px - 16 colors)") << "valid/35FLOPPY.ICO" << 1;
88 QTest::newRow(dataTag: "16px,32px,48px - 256,16M colors") << "valid/abcardWindow.ico" << 1;
89 QTest::newRow(dataTag: "16px - 16 colors") << "valid/App.ico" << 1;
90 QTest::newRow(dataTag: "16px,32px,48px - 16,256,16M colors") << "valid/Obj_N2_Internal_Mem.ico" << 1;
91 QTest::newRow(dataTag: "16px - 16,256,16M colors") << "valid/Status_Play.ico" << 1;
92 QTest::newRow(dataTag: "16px,32px - 16 colors") << "valid/TIMER01.ICO" << 1;
93 QTest::newRow(dataTag: "16px16c, 32px32c, 32px256c 1") << "valid/WORLD.ico" << 1;
94 QTest::newRow(dataTag: "16px16c, 32px32c, 32px256c 2") << "valid/WORLDH.ico" << 1;
95 QTest::newRow(dataTag: "invalid floppy (first 8 bytes = 0xff)") << "invalid/35floppy.ico" << 0;
96 QTest::newRow(dataTag: "103x16px, 24BPP") << "valid/trolltechlogo_tiny.ico" << 1;
97 QTest::newRow(dataTag: "includes 32BPP w/alpha") << "valid/semitransparent.ico" << 1;
98 QTest::newRow(dataTag: "PNG compression") << "valid/Qt.ico" << 1;
99 QTest::newRow(dataTag: "CUR file") << "valid/yellow.cur" << 1;
100}
101
102void tst_QIcoImageFormat::canRead()
103{
104 QFETCH(QString, fileName);
105 QFETCH(int, isValid);
106
107 QImageReader reader(m_IconPath + QLatin1Char('/') + fileName);
108 QCOMPARE(reader.canRead(), (isValid == 0 ? false : true));
109}
110
111class QSequentialFile : public QFile
112{
113public:
114 QSequentialFile(const QString &name) : QFile(name) {}
115
116 virtual ~QSequentialFile() {}
117
118 virtual bool isSequential() const {
119 return true;
120 }
121
122};
123
124void tst_QIcoImageFormat::SequentialFile_data()
125{
126 QTest::addColumn<QString>(name: "fileName");
127 QTest::addColumn<int>(name: "isValid");
128
129 QTest::newRow(dataTag: "floppy (16,32 pixels - 16 colors)") << "valid/35FLOPPY.ICO" << 1;
130
131 QTest::newRow(dataTag: "invalid floppy (first 8 bytes = 0xff)") << "invalid/35floppy.ico" << 0;
132
133
134}
135
136void tst_QIcoImageFormat::SequentialFile()
137{
138 QFETCH(QString, fileName);
139 QFETCH(int, isValid);
140
141 QSequentialFile *file = new QSequentialFile(m_IconPath + QLatin1Char('/') + fileName);
142 QVERIFY(file);
143 QVERIFY(file->open(QFile::ReadOnly));
144 QImageReader reader(file);
145
146 // Perform the check twice. If canRead() does not restore the sequential device back to its original state,
147 // it will fail on the second try.
148 QCOMPARE(reader.canRead(), (isValid == 0 ? false : true));
149 QCOMPARE(reader.canRead(), (isValid == 0 ? false : true));
150 file->close();
151}
152
153
154void tst_QIcoImageFormat::imageCount_data()
155{
156 QTest::addColumn<QString>(name: "fileName");
157 QTest::addColumn<int>(name: "count");
158
159 QTest::newRow(dataTag: "floppy (16px,32px - 16 colors)") << "valid/35FLOPPY.ICO" << 2;
160 QTest::newRow(dataTag: "16px,32px,48px - 256,16M colors") << "valid/abcardWindow.ico" << 6;
161 QTest::newRow(dataTag: "16px - 16 colors") << "valid/App.ico" << 1;
162 QTest::newRow(dataTag: "16px,32px,48px - 16,256,16M colors") << "valid/Obj_N2_Internal_Mem.ico" << 9;
163 QTest::newRow(dataTag: "16px - 16,256,16M colors") << "valid/Status_Play.ico" << 3;
164 QTest::newRow(dataTag: "16px,32px - 16 colors") << "valid/TIMER01.ICO" << 2;
165 QTest::newRow(dataTag: "16px16c, 32px32c, 32px256c 1") << "valid/WORLD.ico" << 3;
166 QTest::newRow(dataTag: "16px16c, 32px32c, 32px256c 2") << "valid/WORLDH.ico" << 3;
167 QTest::newRow(dataTag: "invalid floppy (first 8 bytes = 0xff)") << "invalid/35floppy.ico" << -1;
168 QTest::newRow(dataTag: "includes 32BPP w/alpha") << "valid/semitransparent.ico" << 9;
169 QTest::newRow(dataTag: "PNG compression") << "valid/Qt.ico" << 4;
170 QTest::newRow(dataTag: "CUR file") << "valid/yellow.cur" << 1;
171}
172
173void tst_QIcoImageFormat::imageCount()
174{
175 QFETCH(QString, fileName);
176 QFETCH(int, count);
177
178 QImageReader reader(m_IconPath + QLatin1Char('/') + fileName);
179 QCOMPARE(reader.imageCount(), count);
180}
181
182void tst_QIcoImageFormat::jumpToNextImage_data()
183{
184 QTest::addColumn<QString>(name: "fileName");
185 QTest::addColumn<int>(name: "count");
186
187 QTest::newRow(dataTag: "floppy (16px,32px - 16 colors)") << "valid/35FLOPPY.ICO" << 2;
188 QTest::newRow(dataTag: "16px,32px,48px - 256,16M colors") << "valid/abcardWindow.ico" << 6;
189 QTest::newRow(dataTag: "16px - 16 colors") << "valid/App.ico" << 1;
190 QTest::newRow(dataTag: "16px,32px,48px - 16,256,16M colors") << "valid/Obj_N2_Internal_Mem.ico" << 9;
191 QTest::newRow(dataTag: "16px - 16,256,16M colors") << "valid/Status_Play.ico" << 3;
192 QTest::newRow(dataTag: "16px,32px - 16 colors") << "valid/TIMER01.ICO" << 2;
193 QTest::newRow(dataTag: "16px16c, 32px32c, 32px256c 1") << "valid/WORLD.ico" << 3;
194 QTest::newRow(dataTag: "16px16c, 32px32c, 32px256c 2") << "valid/WORLDH.ico" << 3;
195 QTest::newRow(dataTag: "includes 32BPP w/alpha") << "valid/semitransparent.ico" << 9;
196 QTest::newRow(dataTag: "PNG compression") << "valid/Qt.ico" << 4;
197 QTest::newRow(dataTag: "CUR file") << "valid/yellow.cur" << 1;
198}
199
200void tst_QIcoImageFormat::jumpToNextImage()
201{
202 QFETCH(QString, fileName);
203 QFETCH(int, count);
204
205 QImageReader reader(m_IconPath + QLatin1Char('/') + fileName);
206 bool bJumped = reader.jumpToImage(imageNumber: 0);
207 while (bJumped) {
208 count--;
209 bJumped = reader.jumpToNextImage();
210 }
211 QCOMPARE(count, 0);
212}
213
214void tst_QIcoImageFormat::loopCount_data()
215{
216 QTest::addColumn<QString>(name: "fileName");
217 QTest::addColumn<int>(name: "count");
218
219 QTest::newRow(dataTag: "floppy (16px,32px - 16 colors)") << "valid/35FLOPPY.ICO" << 0;
220 QTest::newRow(dataTag: "invalid floppy (first 8 bytes = 0xff)") << "invalid/35floppy.ico" << -1;
221}
222
223void tst_QIcoImageFormat::loopCount()
224{
225 QFETCH(QString, fileName);
226 QFETCH(int, count);
227
228 QImageReader reader(m_IconPath + QLatin1Char('/') + fileName);
229 QCOMPARE(reader.loopCount(), count);
230 QCOMPARE(reader.canRead(), count < 0 ? false : true);
231}
232
233void tst_QIcoImageFormat::nextImageDelay_data()
234{
235 QTest::addColumn<QString>(name: "fileName");
236 QTest::addColumn<int>(name: "count");
237
238 QTest::newRow(dataTag: "floppy (16px,32px - 16 colors)") << "valid/35FLOPPY.ICO" << 2;
239 QTest::newRow(dataTag: "16px,32px,48px - 256,16M colors") << "valid/abcardWindow.ico" << 6;
240 QTest::newRow(dataTag: "16px - 16 colors") << "valid/App.ico" << 1;
241 QTest::newRow(dataTag: "16px,32px,48px - 16,256,16M colors") << "valid/Obj_N2_Internal_Mem.ico" << 9;
242 QTest::newRow(dataTag: "16px - 16,256,16M colors") << "valid/Status_Play.ico" << 3;
243 QTest::newRow(dataTag: "16px,32px - 16 colors") << "valid/TIMER01.ICO" << 2;
244 QTest::newRow(dataTag: "16px16c, 32px32c, 32px256c 1") << "valid/WORLD.ico" << 3;
245 QTest::newRow(dataTag: "16px16c, 32px32c, 32px256c 2") << "valid/WORLDH.ico" << 3;
246 QTest::newRow(dataTag: "invalid floppy (first 8 bytes = 0xff)") << "invalid/35floppy.ico" << -1;
247 QTest::newRow(dataTag: "includes 32BPP w/alpha") << "valid/semitransparent.ico" << 9;
248 QTest::newRow(dataTag: "PNG compression") << "valid/Qt.ico" << 4;
249 QTest::newRow(dataTag: "CUR file") << "valid/yellow.cur" << 1;
250}
251
252void tst_QIcoImageFormat::nextImageDelay()
253{
254 QFETCH(QString, fileName);
255 QFETCH(int, count);
256
257 QImageReader reader(m_IconPath + QLatin1Char('/') + fileName);
258 if (count == -1) {
259 QCOMPARE(reader.nextImageDelay(), -1);
260 } else {
261 int i;
262 for (i = 0; i < count; i++) {
263 QVERIFY(reader.jumpToImage(i));
264 QCOMPARE(reader.nextImageDelay(), 0);
265 }
266 }
267}
268
269void tst_QIcoImageFormat::pngCompression_data()
270{
271 QTest::addColumn<QString>(name: "fileName");
272 QTest::addColumn<int>(name: "index");
273 QTest::addColumn<int>(name: "width");
274 QTest::addColumn<int>(name: "height");
275
276 QTest::newRow(dataTag: "PNG compression") << "valid/Qt.ico" << 4 << 256 << 256;
277}
278
279void tst_QIcoImageFormat::pngCompression()
280{
281 QFETCH(QString, fileName);
282 QFETCH(int, index);
283 QFETCH(int, width);
284 QFETCH(int, height);
285
286 QImageReader reader(m_IconPath + QLatin1Char('/') + fileName);
287
288 QImage image;
289 reader.jumpToImage(imageNumber: index);
290
291 QSize size = reader.size();
292 QCOMPARE(size.width(), width);
293 QCOMPARE(size.height(), height);
294
295 reader.read(image: &image);
296
297 QCOMPARE(image.width(), width);
298 QCOMPARE(image.height(), height);
299}
300
301void tst_QIcoImageFormat::write_data()
302{
303 QTest::addColumn<QSize>(name: "inSize");
304 QTest::addColumn<QSize>(name: "outSize");
305
306 QTest::newRow(dataTag: "64x64") << QSize(64, 64) << QSize(64, 64);
307 QTest::newRow(dataTag: "128x200") << QSize(128, 200) << QSize(128, 200);
308 QTest::newRow(dataTag: "256x256") << QSize(256, 256) << QSize(256, 256);
309 QTest::newRow(dataTag: "400x400") << QSize(400, 400) << QSize(256, 256);
310}
311
312void tst_QIcoImageFormat::write()
313{
314 QFETCH(QSize, inSize);
315 QFETCH(QSize, outSize);
316
317 QImage inImg;
318 {
319 QImageReader reader(m_IconPath + "/valid/Qt.ico");
320 reader.jumpToImage(imageNumber: 4);
321 reader.setScaledSize(inSize);
322 inImg = reader.read();
323 QVERIFY(!inImg.isNull());
324 QCOMPARE(inImg.size(), inSize);
325 }
326
327 QBuffer buf;
328 {
329 buf.open(openMode: QIODevice::WriteOnly);
330 QImageWriter writer(&buf, "ico");
331 QVERIFY(writer.write(inImg));
332 buf.close();
333 }
334 {
335 buf.open(openMode: QIODevice::ReadOnly);
336 QImageReader reader(&buf);
337 QVERIFY(reader.canRead());
338 QCOMPARE(reader.format(), QByteArray("ico"));
339 QImage outImg = reader.read();
340 QVERIFY(!outImg.isNull());
341 QCOMPARE(outImg.size(), outSize);
342 buf.close();
343 }
344}
345
346QTEST_MAIN(tst_QIcoImageFormat)
347#include "tst_qicoimageformat.moc"
348
349

source code of qtbase/tests/auto/gui/image/qicoimageformat/tst_qicoimageformat.cpp