1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Copyright (C) 2016 Alex Char. |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the ICNS autotests in the Qt ImageFormats module. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <QtTest/QtTest> |
31 | #include <QtGui/QtGui> |
32 | |
33 | class tst_qicns: public QObject |
34 | { |
35 | Q_OBJECT |
36 | |
37 | private slots: |
38 | void initTestCase(); |
39 | void readIcons_data(); |
40 | void readIcons(); |
41 | void writeIcons_data(); |
42 | void writeIcons(); |
43 | }; |
44 | |
45 | void tst_qicns::initTestCase() |
46 | { |
47 | if (!QImageReader::supportedImageFormats().contains(t: "icns" )) |
48 | QSKIP("The image format handler is not installed." ); |
49 | } |
50 | |
51 | void tst_qicns::readIcons_data() |
52 | { |
53 | QTest::addColumn<QString>(name: "fileName" ); |
54 | QTest::addColumn<QSize>(name: "size" ); |
55 | QTest::addColumn<int>(name: "imageCount" ); |
56 | QTest::addColumn<QByteArray>(name: "format" ); |
57 | |
58 | QTest::newRow(dataTag: "1" ) << QStringLiteral("test-png" ) << QSize(128, 128) << 7 << QByteArrayLiteral("png" ); |
59 | QTest::newRow(dataTag: "2" ) << QStringLiteral("test-jp2" ) << QSize(128, 128) << 7 << QByteArrayLiteral("jp2" ); |
60 | QTest::newRow(dataTag: "3" ) << QStringLiteral("test-32bit" ) << QSize(128, 128) << 4 << QByteArray(); |
61 | QTest::newRow(dataTag: "4" ) << QStringLiteral("test-legacy" ) << QSize(48, 48) << 12 << QByteArray(); |
62 | QTest::newRow(dataTag: "5" ) << QStringLiteral("test-variants" ) << QSize(128, 128) << 5 << QByteArrayLiteral("png" ); |
63 | } |
64 | |
65 | void tst_qicns::readIcons() |
66 | { |
67 | QFETCH(QString, fileName); |
68 | QFETCH(QSize, size); |
69 | QFETCH(int, imageCount); |
70 | QFETCH(QByteArray, format); |
71 | |
72 | if (!format.isEmpty() && !QImageReader::supportedImageFormats().contains(t: format)) |
73 | QSKIP("This test requires another image format plugin" ); |
74 | const QString path = QStringLiteral(":/icns/" ) + fileName + QStringLiteral(".icns" ); |
75 | QImageReader reader(path); |
76 | QVERIFY(reader.canRead()); |
77 | QCOMPARE(reader.imageCount(), imageCount); |
78 | |
79 | for (int i = 0; i < reader.imageCount(); ++i) { |
80 | QVERIFY2(reader.jumpToImage(i), qPrintable(reader.errorString())); |
81 | QImage image = reader.read(); |
82 | if (i == 0) |
83 | QCOMPARE(image.size(), size); |
84 | QVERIFY2(!image.isNull(), qPrintable(reader.errorString())); |
85 | } |
86 | } |
87 | |
88 | void tst_qicns::writeIcons_data() |
89 | { |
90 | QTest::addColumn<QString>(name: "fileName" ); |
91 | QTest::addColumn<QSize>(name: "size" ); |
92 | |
93 | QTest::newRow(dataTag: "1" ) << QStringLiteral("test-write-16" ) << QSize(16, 16); |
94 | QTest::newRow(dataTag: "2" ) << QStringLiteral("test-write-32" ) << QSize(32, 32); |
95 | QTest::newRow(dataTag: "3" ) << QStringLiteral("test-write-64" ) << QSize(64, 64); |
96 | QTest::newRow(dataTag: "4" ) << QStringLiteral("test-write-128" ) << QSize(128, 128); |
97 | QTest::newRow(dataTag: "5" ) << QStringLiteral("test-write-512" ) << QSize(512, 512); |
98 | QTest::newRow(dataTag: "6" ) << QStringLiteral("test-write-1024" ) << QSize(1024, 1024); |
99 | } |
100 | |
101 | void tst_qicns::writeIcons() |
102 | { |
103 | QTemporaryDir temp(QDir::tempPath() + QStringLiteral("/tst_qincs" )); |
104 | QVERIFY2(temp.isValid(), "Unable to create temp dir." ); |
105 | |
106 | QFETCH(QString, fileName); |
107 | QFETCH(QSize, size); |
108 | |
109 | const QString distPath = QStringLiteral("%1/%2.icns" ).arg(a: temp.path()).arg(a: fileName); |
110 | const QString sourcePath = QStringLiteral(":/icns/%1.png" ).arg(a: fileName); |
111 | |
112 | QImage image(sourcePath); |
113 | QVERIFY(!image.isNull()); |
114 | QVERIFY(image.size() == size); |
115 | |
116 | QImageWriter writer(distPath, QByteArrayLiteral("icns" )); |
117 | QVERIFY2(writer.canWrite(), qPrintable(writer.errorString())); |
118 | QVERIFY2(writer.write(image), qPrintable(writer.errorString())); |
119 | |
120 | QVERIFY(image == QImage(distPath)); |
121 | } |
122 | |
123 | QTEST_MAIN(tst_qicns) |
124 | #include "tst_qicns.moc" |
125 | |