Skip to content
Permalink
Browse files

Update unit tests

(cherry-picked from 3bfcbaa)
  • Loading branch information
nyalldawson committed Apr 9, 2018
1 parent fba6a24 commit 15d7385b62a906cd4307e775172ffe5888daf4df
@@ -446,13 +446,15 @@ QByteArray QgsSvgCache::getImageData( const QString &path ) const
return;
}

bool ok = true;

QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
if ( !status.isNull() && status.toInt() >= 400 )
{
QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
QgsMessageLog::logMessage( tr( "SVG request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), path ), tr( "SVG" ) );
mRemoteContentCache.insert( path, new QByteArray( mMissingSvg ) );
return;
ok = false;
}

// 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
{
QgsMessageLog::logMessage( tr( "Unexpected MIME type %1 received for %2" ).arg( contentType, path ), tr( "SVG" ) );
mRemoteContentCache.insert( path, new QByteArray( mMissingSvg ) );
return;
ok = false;
}

// read the image data
mRemoteContentCache.insert( path, new QByteArray( reply->readAll() ) );
if ( ok )
{
// read the image data
mRemoteContentCache.insert( path, new QByteArray( reply->readAll() ) );
}
QMetaObject::invokeMethod( const_cast< QgsSvgCache * >( this ), "onRemoteSvgFetched", Qt::QueuedConnection, Q_ARG( QString, path ), Q_ARG( bool, true ) );
} );

@@ -18,7 +18,7 @@
import socketserver
import threading
import http.server
from qgis.PyQt.QtCore import QDir
from qgis.PyQt.QtCore import QDir, QCoreApplication
from qgis.PyQt.QtGui import QColor, QImage, QPainter

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

self.fetched = True
QgsApplication.svgCache().remoteSvgFetched.connect(self.svgFetched)

def tearDown(self):
report_file_path = "%s/qgistest.html" % QDir.tempPath()
with open(report_file_path, 'a') as report_file:
report_file.write(self.report)

def svgFetched(self):
self.fetched = True

def waitForFetch(self):
self.fetched = False
while not self.fetched:
QCoreApplication.processEvents()

def testRemoteSVG(self):
"""Test fetching remote svg."""
url = 'http://localhost:{}/qgis_local_server/sample_svg.svg'.format(str(TestQgsSvgCache.port))
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0),
strokeWidth=0.1, widthScaleFactor=1)
# first should be waiting image
self.assertTrue(self.imageCheck('Remote SVG', 'waiting_svg', image))
self.waitForFetch()

# second should be correct image
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0),
strokeWidth=0.1, widthScaleFactor=1)
self.assertTrue(self.imageCheck('Remote SVG', 'remote_svg', image))
@@ -64,11 +82,26 @@ def testRemoteSvgAsText(self):
url = 'http://localhost:{}/qgis_local_server/svg_as_text.txt'.format(str(TestQgsSvgCache.port))
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0),
strokeWidth=0.1, widthScaleFactor=1)
# first should be waiting image
self.assertTrue(self.imageCheck('Remote SVG as Text', 'waiting_svg', image))

self.waitForFetch()
# second should be correct image
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0),
strokeWidth=0.1, widthScaleFactor=1)
# first should be waiting image
self.assertTrue(self.imageCheck('Remote SVG as Text', 'remote_svg', image))

def testRemoteSvgBadMime(self):
"""Test fetching remote svg with bad mime type"""
url = 'http://localhost:{}/qgis_local_server/logo.png'.format(str(TestQgsSvgCache.port))
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0),
strokeWidth=0.1, widthScaleFactor=1)
# first should be waiting image
self.assertTrue(self.imageCheck('Remote SVG bad MIME type', 'waiting_svg', image))

# second should be correct image
self.waitForFetch()
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0),
strokeWidth=0.1, widthScaleFactor=1)
self.assertTrue(self.imageCheck('Remote SVG bad MIME type', 'bad_svg', image))
@@ -78,7 +111,8 @@ def testRemoteSvgMissing(self):
url = 'http://localhost:{}/qgis_local_server/xxx.svg'.format(str(TestQgsSvgCache.port)) # oooo naughty
image, in_cache = QgsApplication.svgCache().svgAsImage(url, 100, fill=QColor(0, 0, 0), stroke=QColor(0, 0, 0),
strokeWidth=0.1, widthScaleFactor=1)
self.assertTrue(self.imageCheck('Remote SVG missing', 'bad_svg', image))

self.assertTrue(self.imageCheck('Remote SVG missing', 'waiting_svg', image))

def imageCheck(self, name, reference_image, image):
self.report += "<h2>Render {}</h2>\n".format(name)
Binary file not shown.

0 comments on commit 15d7385

Please sign in to comment.
You can’t perform that action at this time.