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
32#include "../../../src/plugins/imageformats/svg/qsvgiohandler.cpp"
33#include <QImage>
34#include <QStringList>
35#include <QVector>
36
37#ifndef SRCDIR
38#define SRCDIR
39#endif
40
41
42QStringList logMessages;
43
44static void messageHandler(QtMsgType pType, const QMessageLogContext& pContext, const QString& pMsg)
45{
46 Q_UNUSED(pType);
47 Q_UNUSED(pContext);
48 logMessages.append(t: pMsg);
49}
50
51
52class tst_QSvgPlugin : public QObject
53{
54Q_OBJECT
55
56public:
57 tst_QSvgPlugin();
58 virtual ~tst_QSvgPlugin();
59
60private slots:
61 void checkSize_data();
62 void checkSize();
63 void checkImageInclude();
64 void encodings_data();
65 void encodings();
66};
67
68
69
70tst_QSvgPlugin::tst_QSvgPlugin()
71{
72}
73
74tst_QSvgPlugin::~tst_QSvgPlugin()
75{
76}
77
78void tst_QSvgPlugin::checkSize_data()
79{
80 QTest::addColumn<QString>(name: "filename");
81 QTest::addColumn<int>(name: "imageHeight");
82 QTest::addColumn<int>(name: "imageWidth");
83
84 QTest::newRow(dataTag: "square") << QFINDTESTDATA("square.svg") << 50 << 50;
85 QTest::newRow(dataTag: "square_size") << QFINDTESTDATA("square_size.svg") << 200 << 200;
86 QTest::newRow(dataTag: "square_size_viewbox") << QFINDTESTDATA("square_size_viewbox.svg") << 200 << 200;
87 QTest::newRow(dataTag: "square_viewbox") << QFINDTESTDATA("square_viewbox.svg") << 100 << 100;
88 QTest::newRow(dataTag: "tall") << QFINDTESTDATA("tall.svg") << 50 << 25;
89 QTest::newRow(dataTag: "tall_size") << QFINDTESTDATA("tall_size.svg") << 200 << 100;
90 QTest::newRow(dataTag: "tall_size_viewbox") << QFINDTESTDATA("tall_size_viewbox.svg") << 200 << 100;
91 QTest::newRow(dataTag: "tall_viewbox") << QFINDTESTDATA("tall_viewbox.svg") << 100 << 50;
92 QTest::newRow(dataTag: "wide") << QFINDTESTDATA("wide.svg") << 25 << 50;
93 QTest::newRow(dataTag: "wide_size") << QFINDTESTDATA("wide_size.svg") << 100 << 200;
94 QTest::newRow(dataTag: "wide_size_viewbox") << QFINDTESTDATA("wide_size_viewbox.svg") << 100 << 200;
95 QTest::newRow(dataTag: "wide_viewbox") << QFINDTESTDATA("wide_viewbox.svg") << 50 << 100;
96}
97
98void tst_QSvgPlugin::checkSize()
99{
100 QFETCH(QString, filename);
101 QFETCH(int, imageHeight);
102 QFETCH(int, imageWidth);
103
104 QFile file(filename);
105 file.open(flags: QIODevice::ReadOnly);
106
107 QSvgIOHandler plugin;
108 plugin.setDevice(&file);
109
110 QImage image;
111 plugin.read(image: &image);
112
113 file.close();
114
115 QCOMPARE(imageHeight, image.height());
116 QCOMPARE(imageWidth, image.width());
117}
118
119void tst_QSvgPlugin::checkImageInclude()
120{
121 const QString filename(QFINDTESTDATA("imageInclude.svg"));
122 const QString path = filename.left(n: filename.size() - strlen(s: "imageInclude.svg"));
123
124 QFile file(filename);
125 file.open(flags: QIODevice::ReadOnly);
126
127 QSvgIOHandler plugin;
128 plugin.setDevice(&file);
129
130 QImage image;
131 qInstallMessageHandler(messageHandler);
132 plugin.read(image: &image);
133 qInstallMessageHandler(nullptr);
134
135 file.close();
136
137 QCOMPARE(logMessages.size(), 8);
138 QCOMPARE(logMessages.at(0), QString("Could not create image from \"%1notExisting.svg\"").arg(path));
139 QCOMPARE(logMessages.at(1), QString("Could not create image from \"%1./notExisting.svg\"").arg(path));
140 QCOMPARE(logMessages.at(2), QString("Could not create image from \"%1../notExisting.svg\"").arg(path));
141 QCOMPARE(logMessages.at(3), QString("Could not create image from \"%1notExisting.svg\"").arg(QDir::rootPath()));
142 QCOMPARE(logMessages.at(4), QLatin1String("Could not create image from \":/notExisting.svg\""));
143 QCOMPARE(logMessages.at(5), QLatin1String("Could not create image from \"qrc:///notExisting.svg\""));
144 QCOMPARE(logMessages.at(6), QLatin1String("Could not create image from \"file:///notExisting.svg\""));
145 QCOMPARE(logMessages.at(7), QLatin1String("Could not create image from \"http://qt.io/notExisting.svg\""));
146
147 logMessages.clear();
148}
149
150void tst_QSvgPlugin::encodings_data()
151{
152 QTest::addColumn<QString>(name: "filename");
153
154 QTest::newRow(dataTag: "utf-8") << QFINDTESTDATA("simple_Utf8.svg");
155 QTest::newRow(dataTag: "utf-16LE") << QFINDTESTDATA("simple_Utf16LE.svg");
156 QTest::newRow(dataTag: "utf-16BE") << QFINDTESTDATA("simple_Utf16BE.svg");
157 QTest::newRow(dataTag: "utf-32LE") << QFINDTESTDATA("simple_Utf32LE.svg");
158 QTest::newRow(dataTag: "utf-32BE") << QFINDTESTDATA("simple_Utf32BE.svg");
159}
160
161void tst_QSvgPlugin::encodings()
162{
163 QFETCH(QString, filename);
164
165 {
166 QFile file(filename);
167 file.open(flags: QIODevice::ReadOnly);
168 QVERIFY(QSvgIOHandler::canRead(&file));
169 }
170
171 QFile file(filename);
172 file.open(flags: QIODevice::ReadOnly);
173 QSvgIOHandler plugin;
174 plugin.setDevice(&file);
175 QVERIFY(plugin.canRead());
176 QImage img;
177 QVERIFY(plugin.read(&img));
178 QCOMPARE(img.size(), QSize(50, 50));
179}
180
181QTEST_MAIN(tst_QSvgPlugin)
182#include "tst_qsvgplugin.moc"
183

source code of qtsvg/tests/auto/qsvgplugin/tst_qsvgplugin.cpp