1818import socketserver
1919import threading
2020import http .server
21- from qgis .PyQt .QtCore import QDir
21+ from qgis .PyQt .QtCore import QDir , QCoreApplication
2222from qgis .PyQt .QtGui import QColor , QImage , QPainter
2323
2424from 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 )
0 commit comments