Skip to content

Commit 7794144

Browse files
committed
When a remote SVG is requested but fails, use the missing SVG icon
as an indicator for users that something went wrong This was previously only used for replies with incorrect mime types or authentication errors, but it meant that an incorrect SVG url would silently result in no symbols rendered. Also add unit tests for fetching remote svg images
1 parent 5d4e1bb commit 7794144

File tree

7 files changed

+670
-3
lines changed

7 files changed

+670
-3
lines changed

src/core/symbology/qgssvgcache.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,9 @@ QByteArray QgsSvgCache::getImageData( const QString &path ) const
433433

434434
if ( reply->error() != QNetworkReply::NoError )
435435
{
436-
QgsMessageLog::logMessage( tr( "SVG request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ), tr( "SVG" ) );
437-
436+
QgsMessageLog::logMessage( tr( "SVG request failed [error: %1 - url: %2]" ).arg( reply->errorString(), path ), tr( "SVG" ) );
438437
reply->deleteLater();
439-
return QByteArray();
438+
return mMissingSvg;
440439
}
441440

442441
QVariant redirect = reply->attribute( QNetworkRequest::RedirectionTargetAttribute );

tests/src/python/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ ADD_PYTHON_TEST(PyQgsReport test_qgsreport.py)
152152
ADD_PYTHON_TEST(PyQgsRulebasedRenderer test_qgsrulebasedrenderer.py)
153153
ADD_PYTHON_TEST(PyQgsSingleSymbolRenderer test_qgssinglesymbolrenderer.py)
154154
ADD_PYTHON_TEST(PyQgsShapefileProvider test_provider_shapefile.py)
155+
ADD_PYTHON_TEST(PyQgsSvgCache test_qgssvgcache.py)
155156
ADD_PYTHON_TEST(PyQgsSymbolButton test_qgssymbolbutton.py)
156157
ADD_PYTHON_TEST(PyQgsTabfileProvider test_provider_tabfile.py)
157158
ADD_PYTHON_TEST(PyQgsTabWidget test_qgstabwidget.py)

tests/src/python/test_qgssvgcache.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsSvgCache.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = '(C) 2018 by Nyall Dawson'
10+
__date__ = '29/03/2018'
11+
__copyright__ = 'Copyright 2018, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis # NOQA
16+
17+
import os
18+
import socketserver
19+
import threading
20+
import http.server
21+
from qgis.PyQt.QtCore import QDir
22+
from qgis.PyQt.QtGui import QColor
23+
24+
from qgis.core import (QgsSvgCache, QgsRenderChecker, QgsApplication)
25+
from qgis.testing import start_app, unittest
26+
from utilities import unitTestDataPath
27+
28+
start_app()
29+
TEST_DATA_DIR = unitTestDataPath()
30+
31+
32+
class TestQgsSvgCache(unittest.TestCase):
33+
34+
@classmethod
35+
def setUpClass(cls):
36+
# Bring up a simple HTTP server, for remote SVG tests
37+
os.chdir(unitTestDataPath() + '')
38+
handler = http.server.SimpleHTTPRequestHandler
39+
40+
cls.httpd = socketserver.TCPServer(('localhost', 0), handler)
41+
cls.port = cls.httpd.server_address[1]
42+
43+
cls.httpd_thread = threading.Thread(target=cls.httpd.serve_forever)
44+
cls.httpd_thread.setDaemon(True)
45+
cls.httpd_thread.start()
46+
47+
def setUp(self):
48+
self.report = "<h1>Python QgsSvgCache Tests</h1>\n"
49+
50+
def tearDown(self):
51+
report_file_path = "%s/qgistest.html" % QDir.tempPath()
52+
with open(report_file_path, 'a') as report_file:
53+
report_file.write(self.report)
54+
55+
def testRemoteSVG(self):
56+
"""Test fetching remote svg."""
57+
url = 'http://localhost:{}/qgis_local_server/sample_svg.svg'.format(str(TestQgsSvgCache.port))
58+
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0), strokeWidth=0.1, widthScaleFactor=1)
59+
self.assertTrue(self.imageCheck('Remote SVG', 'remote_svg', image))
60+
61+
def testRemoteSvgAsText(self):
62+
"""Test fetching remote svg with text mime format - e.g. github raw svgs"""
63+
url = 'http://localhost:{}/qgis_local_server/svg_as_text.txt'.format(str(TestQgsSvgCache.port))
64+
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0), strokeWidth=0.1, widthScaleFactor=1)
65+
self.assertTrue(self.imageCheck('Remote SVG as Text', 'remote_svg', image))
66+
67+
def testRemoteSvgBadMime(self):
68+
"""Test fetching remote svg with bad mime type"""
69+
url = 'http://localhost:{}/qgis_local_server/logo.png'.format(str(TestQgsSvgCache.port))
70+
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0), strokeWidth=0.1, widthScaleFactor=1)
71+
self.assertTrue(self.imageCheck('Remote SVG bad MIME type', 'bad_svg', image))
72+
73+
def testRemoteSvgMissing(self):
74+
"""Test fetching remote svg with bad url"""
75+
url = 'http://localhost:{}/qgis_local_server/xxx.svg'.format(str(TestQgsSvgCache.port)) # oooo naughty
76+
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0), strokeWidth=0.1, widthScaleFactor=1)
77+
self.assertTrue(self.imageCheck('Remote SVG missing', 'bad_svg', image))
78+
79+
def imageCheck(self, name, reference_image, image):
80+
self.report += "<h2>Render {}</h2>\n".format(name)
81+
temp_dir = QDir.tempPath() + '/'
82+
file_name = temp_dir + 'svg_' + name + ".png"
83+
image.save(file_name, "PNG")
84+
checker = QgsRenderChecker()
85+
checker.setControlPathPrefix("svg_cache")
86+
checker.setControlName("expected_" + reference_image)
87+
checker.setRenderedImage(file_name)
88+
checker.setColorTolerance(2)
89+
result = checker.compareImages(name, 20)
90+
self.report += checker.report()
91+
print((self.report))
92+
return result
93+
94+
if __name__ == '__main__':
95+
unittest.main()
Loading
Loading

0 commit comments

Comments
 (0)