|
| 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() |
0 commit comments