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
29
30#include <QtTest/QtTest>
31#include <QImageReader>
32#include <QtGui>
33
34class tst_QIcon_Svg : public QObject
35{
36 Q_OBJECT
37public:
38
39public slots:
40 void initTestCase();
41
42private slots:
43 void svgActualSize();
44 void svg();
45 void availableSizes();
46 void isNull();
47 void sizeInPercent();
48
49
50private:
51 QString prefix;
52};
53
54void tst_QIcon_Svg::initTestCase()
55{
56 prefix = QFINDTESTDATA("icons/");
57 if (prefix.isEmpty())
58 QFAIL("Can't find images directory!");
59 if (!QImageReader::supportedImageFormats().contains(t: "svg"))
60 QFAIL("SVG support is not available");
61 QCOMPARE(QImageReader::imageFormat(prefix + "triangle.svg"), QByteArray("svg"));
62}
63
64void tst_QIcon_Svg::svgActualSize()
65{
66 QIcon icon(prefix + "rect.svg");
67 QCOMPARE(icon.actualSize(QSize(16, 2)), QSize(16, 2));
68 QCOMPARE(icon.pixmap(QSize(16, 16)).size(), QSize(16, 2));
69
70 QPixmap p(16, 16);
71 p.fill(fillColor: Qt::cyan);
72 icon.addPixmap(pixmap: p);
73
74 QCOMPARE(icon.actualSize(QSize(16, 16)), QSize(16, 16));
75 QCOMPARE(icon.pixmap(QSize(16, 16)).size(), QSize(16, 16));
76
77 QCOMPARE(icon.actualSize(QSize(16, 14)), QSize(16, 2));
78 QCOMPARE(icon.pixmap(QSize(16, 14)).size(), QSize(16, 2));
79}
80
81void tst_QIcon_Svg::svg()
82{
83 QIcon icon1(prefix + "heart.svg");
84 QVERIFY(!icon1.pixmap(32).isNull());
85 QImage img1 = icon1.pixmap(extent: 32).toImage();
86 QVERIFY(!icon1.pixmap(32, QIcon::Disabled).isNull());
87 QImage img2 = icon1.pixmap(extent: 32, mode: QIcon::Disabled).toImage();
88
89 icon1.addFile(fileName: prefix + "trash.svg", size: QSize(), mode: QIcon::Disabled);
90 QVERIFY(!icon1.pixmap(32, QIcon::Disabled).isNull());
91 QImage img3 = icon1.pixmap(extent: 32, mode: QIcon::Disabled).toImage();
92 QVERIFY(img3 != img2);
93 QVERIFY(img3 != img1);
94
95 QPixmap pm(prefix + "image.png");
96 icon1.addPixmap(pixmap: pm, mode: QIcon::Normal, state: QIcon::On);
97 QVERIFY(!icon1.pixmap(128, QIcon::Normal, QIcon::On).isNull());
98 QImage img4 = icon1.pixmap(extent: 128, mode: QIcon::Normal, state: QIcon::On).toImage();
99 QVERIFY(img4 != img3);
100 QVERIFY(img4 != img2);
101 QVERIFY(img4 != img1);
102
103 QIcon icon2;
104 icon2.addFile(fileName: prefix + "heart.svg");
105 QVERIFY(icon2.pixmap(57).toImage() == icon1.pixmap(57).toImage());
106
107 QIcon icon3(prefix + "trash.svg");
108 icon3.addFile(fileName: prefix + "heart.svg");
109 QVERIFY(icon3.pixmap(57).toImage() == icon1.pixmap(57).toImage());
110
111 QIcon icon4(prefix + "heart.svg");
112 icon4.addFile(fileName: prefix + "image.png", size: QSize(), mode: QIcon::Active);
113 QVERIFY(!icon4.pixmap(32).isNull());
114 QVERIFY(!icon4.pixmap(32, QIcon::Active).isNull());
115 QVERIFY(icon4.pixmap(32).toImage() == img1);
116 QIcon pmIcon(pm);
117 QVERIFY(icon4.pixmap(pm.size(), QIcon::Active).toImage() == pmIcon.pixmap(pm.size(), QIcon::Active).toImage());
118
119#ifndef QT_NO_COMPRESS
120 QIcon icon5(prefix + "heart.svgz");
121 QVERIFY(!icon5.pixmap(32).isNull());
122#endif
123}
124
125void tst_QIcon_Svg::availableSizes()
126{
127 {
128 // checks that there are no availableSizes for scalable images.
129 QIcon icon(prefix + "heart.svg");
130 QList<QSize> availableSizes = icon.availableSizes();
131 qDebug() << availableSizes;
132 QVERIFY(availableSizes.isEmpty());
133 }
134
135 {
136 // even if an a scalable image contain added pixmaps,
137 // availableSizes still should be empty.
138 QIcon icon(prefix + "heart.svg");
139 icon.addFile(fileName: prefix + "image.png", size: QSize(32,32));
140 QList<QSize> availableSizes = icon.availableSizes();
141 QVERIFY(availableSizes.isEmpty());
142 }
143}
144
145void tst_QIcon_Svg::isNull()
146{
147 {
148 //checks that an invalid file results in the icon being null
149 QIcon icon(prefix + "nonExistentFile.svg");
150 QVERIFY(icon.isNull());
151 }
152 {
153 //valid svg, we're not null
154 QIcon icon(prefix + "heart.svg");
155 QVERIFY(!icon.isNull());
156
157 // check for non null of serialized/deserialized valid icon
158 QByteArray buf;
159 QDataStream out(&buf, QIODevice::WriteOnly);
160 out << icon;
161
162 QIcon icon2;
163 QDataStream in(buf);
164 in >> icon2;
165 QVERIFY(!icon2.isNull());
166 }
167 {
168 //invalid svg, but a pixmap added means we're not null
169 QIcon icon(prefix + "nonExistentFile.svg");
170 icon.addFile(fileName: prefix + "image.png", size: QSize(32,32));
171 QVERIFY(!icon.isNull());
172 }
173
174}
175
176void tst_QIcon_Svg::sizeInPercent()
177{
178 QIcon icon(prefix + "rect_size_100percent.svg");
179 QCOMPARE(icon.actualSize(QSize(16, 8)), QSize(16, 8));
180 QCOMPARE(icon.pixmap(QSize(16, 8)).size(), QSize(16, 8));
181
182 QCOMPARE(icon.actualSize(QSize(8, 8)), QSize(8, 4));
183 QCOMPARE(icon.pixmap(QSize(8, 8)).size(), QSize(8, 4));
184}
185
186
187QTEST_MAIN(tst_QIcon_Svg)
188#include "tst_qicon_svg.moc"
189

source code of qtsvg/tests/auto/qicon_svg/tst_qicon_svg.cpp