Skip to content

Commit 15d7385

Browse files
committed
Update unit tests
(cherry-picked from 3bfcbaa)
1 parent fba6a24 commit 15d7385

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

src/core/symbology/qgssvgcache.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,13 +446,15 @@ QByteArray QgsSvgCache::getImageData( const QString &path ) const
446446
return;
447447
}
448448

449+
bool ok = true;
450+
449451
QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
450452
if ( !status.isNull() && status.toInt() >= 400 )
451453
{
452454
QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
453455
QgsMessageLog::logMessage( tr( "SVG request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), path ), tr( "SVG" ) );
454456
mRemoteContentCache.insert( path, new QByteArray( mMissingSvg ) );
455-
return;
457+
ok = false;
456458
}
457459

458460
// we accept both real SVG mime types AND plain text types - because some sites
@@ -463,11 +465,14 @@ QByteArray QgsSvgCache::getImageData( const QString &path ) const
463465
{
464466
QgsMessageLog::logMessage( tr( "Unexpected MIME type %1 received for %2" ).arg( contentType, path ), tr( "SVG" ) );
465467
mRemoteContentCache.insert( path, new QByteArray( mMissingSvg ) );
466-
return;
468+
ok = false;
467469
}
468470

469-
// read the image data
470-
mRemoteContentCache.insert( path, new QByteArray( reply->readAll() ) );
471+
if ( ok )
472+
{
473+
// read the image data
474+
mRemoteContentCache.insert( path, new QByteArray( reply->readAll() ) );
475+
}
471476
QMetaObject::invokeMethod( const_cast< QgsSvgCache * >( this ), "onRemoteSvgFetched", Qt::QueuedConnection, Q_ARG( QString, path ), Q_ARG( bool, true ) );
472477
} );
473478

tests/src/python/test_qgssvgcache.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import socketserver
1919
import threading
2020
import http.server
21-
from qgis.PyQt.QtCore import QDir
21+
from qgis.PyQt.QtCore import QDir, QCoreApplication
2222
from qgis.PyQt.QtGui import QColor, QImage, QPainter
2323

2424
from qgis.core import (QgsSvgCache, QgsRenderChecker, QgsApplication, QgsMultiRenderChecker)
@@ -47,14 +47,32 @@ def setUpClass(cls):
4747
def setUp(self):
4848
self.report = "<h1>Python QgsSvgCache Tests</h1>\n"
4949

50+
self.fetched = True
51+
QgsApplication.svgCache().remoteSvgFetched.connect(self.svgFetched)
52+
5053
def tearDown(self):
5154
report_file_path = "%s/qgistest.html" % QDir.tempPath()
5255
with open(report_file_path, 'a') as report_file:
5356
report_file.write(self.report)
5457

58+
def svgFetched(self):
59+
self.fetched = True
60+
61+
def waitForFetch(self):
62+
self.fetched = False
63+
while not self.fetched:
64+
QCoreApplication.processEvents()
65+
5566
def testRemoteSVG(self):
5667
"""Test fetching remote svg."""
5768
url = 'http://localhost:{}/qgis_local_server/sample_svg.svg'.format(str(TestQgsSvgCache.port))
69+
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0),
70+
strokeWidth=0.1, widthScaleFactor=1)
71+
# first should be waiting image
72+
self.assertTrue(self.imageCheck('Remote SVG', 'waiting_svg', image))
73+
self.waitForFetch()
74+
75+
# second should be correct image
5876
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0),
5977
strokeWidth=0.1, widthScaleFactor=1)
6078
self.assertTrue(self.imageCheck('Remote SVG', 'remote_svg', image))
@@ -64,11 +82,26 @@ def testRemoteSvgAsText(self):
6482
url = 'http://localhost:{}/qgis_local_server/svg_as_text.txt'.format(str(TestQgsSvgCache.port))
6583
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0),
6684
strokeWidth=0.1, widthScaleFactor=1)
85+
# first should be waiting image
86+
self.assertTrue(self.imageCheck('Remote SVG as Text', 'waiting_svg', image))
87+
88+
self.waitForFetch()
89+
# second should be correct image
90+
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0),
91+
strokeWidth=0.1, widthScaleFactor=1)
92+
# first should be waiting image
6793
self.assertTrue(self.imageCheck('Remote SVG as Text', 'remote_svg', image))
6894

6995
def testRemoteSvgBadMime(self):
7096
"""Test fetching remote svg with bad mime type"""
7197
url = 'http://localhost:{}/qgis_local_server/logo.png'.format(str(TestQgsSvgCache.port))
98+
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0),
99+
strokeWidth=0.1, widthScaleFactor=1)
100+
# first should be waiting image
101+
self.assertTrue(self.imageCheck('Remote SVG bad MIME type', 'waiting_svg', image))
102+
103+
# second should be correct image
104+
self.waitForFetch()
72105
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0),
73106
strokeWidth=0.1, widthScaleFactor=1)
74107
self.assertTrue(self.imageCheck('Remote SVG bad MIME type', 'bad_svg', image))
@@ -78,7 +111,8 @@ def testRemoteSvgMissing(self):
78111
url = 'http://localhost:{}/qgis_local_server/xxx.svg'.format(str(TestQgsSvgCache.port)) # oooo naughty
79112
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0),
80113
strokeWidth=0.1, widthScaleFactor=1)
81-
self.assertTrue(self.imageCheck('Remote SVG missing', 'bad_svg', image))
114+
115+
self.assertTrue(self.imageCheck('Remote SVG missing', 'waiting_svg', image))
82116

83117
def imageCheck(self, name, reference_image, image):
84118
self.report += "<h2>Render {}</h2>\n".format(name)
2.71 KB
Loading

0 commit comments

Comments
 (0)