2323import sys
2424import datetime
2525import glob
26+ import shutil
2627import StringIO
2728import tempfile
2829from PyQt4 .QtCore import *
3637 QgsMapRenderer ,
3738 QgsPalLabeling ,
3839 QgsPalLayerSettings ,
40+ QgsProject ,
3941 QgsProviderRegistry ,
4042 QgsVectorLayer ,
4143 QgsRenderChecker
@@ -64,6 +66,7 @@ class TestQgsPalLabeling(TestCase):
6466 _PalDataDir = os .path .join (_TestDataDir , 'labeling' )
6567 _PalFeaturesDb = os .path .join (_PalDataDir , 'pal_features_v3.sqlite' )
6668 _TestFont = TESTFONT
69+ _TestProj = None
6770 _MapRegistry = None
6871 _MapRenderer = None
6972 _Canvas = None
@@ -77,8 +80,7 @@ def setUpClass(cls):
7780 QGISAPP , CANVAS , IFACE , PARENT
7881
7982 # verify that spatialite provider is available
80- msg = ('\n Spatialite provider not found, '
81- 'SKIPPING TEST SUITE' )
83+ msg = '\n Spatialite provider not found, SKIPPING TEST SUITE'
8284 res = 'spatialite' in QgsProviderRegistry .instance ().providerList ()
8385 assert res , msg
8486
@@ -90,6 +92,7 @@ def setUpClass(cls):
9092 cls ._TestGroup = ''
9193 cls ._TestGroupPrefix = ''
9294 cls ._TestGroupAbbr = ''
95+ cls ._TestImage = ''
9396
9497 # initialize class MapRegistry, Canvas, MapRenderer, Map and PAL
9598 cls ._MapRegistry = QgsMapLayerRegistry .instance ()
@@ -98,10 +101,10 @@ def setUpClass(cls):
98101 cls ._Map = cls ._Canvas .map ()
99102 cls ._Map .resize (QSize (600 , 400 ))
100103 cls ._MapRenderer = cls ._Canvas .mapRenderer ()
101- crs = QgsCoordinateReferenceSystem ()
104+ cls . _CRS = QgsCoordinateReferenceSystem ()
102105 # default for labeling test data sources: WGS 84 / UTM zone 13N
103- crs .createFromSrid (32613 )
104- cls ._MapRenderer .setDestinationCrs (crs )
106+ cls . _CRS .createFromSrid (32613 )
107+ cls ._MapRenderer .setDestinationCrs (cls . _CRS )
105108 # use platform's native logical output dpi for QgsMapRenderer on launch
106109
107110 cls ._Pal = QgsPalLabeling ()
@@ -195,7 +198,7 @@ def settingsDict(lyr):
195198 res [attr ] = value
196199 return res
197200
198- def saveContolImage (self ):
201+ def saveContolImage (self , tmpimg = '' ):
199202 if 'PAL_CONTROL_IMAGE' not in os .environ :
200203 return
201204 testgrpdir = 'expected_' + self ._TestGroupPrefix
@@ -208,21 +211,69 @@ def saveContolImage(self):
208211 for f in glob .glob (imgbasepath + '.*' ):
209212 if os .path .exists (f ):
210213 os .remove (f )
211- self ._Map .render ()
212- self ._Canvas .saveAsImage (imgpath )
213-
214- def renderCheck (self , mismatch = 0 ):
214+ if tmpimg :
215+ if os .path .exists (tmpimg ):
216+ shutil .copyfile (tmpimg , imgpath )
217+ else :
218+ self ._Map .render ()
219+ self ._Canvas .saveAsImage (imgpath )
220+
221+ def renderCheck (self , mismatch = 0 , imgpath = '' , grpprefix = '' ):
222+ """Check rendered map canvas or existing image against control image
223+
224+ mismatch: number of pixels different from control, and still valid check
225+ imgpath: existing image; if present, skips rendering canvas
226+ grpprefix: compare test image/rendering against different test group
227+ """
228+ if not grpprefix :
229+ grpprefix = self ._TestGroupPrefix
215230 chk = QgsRenderChecker ()
216- chk .setControlPathPrefix ('expected_' + self . _TestGroupPrefix )
231+ chk .setControlPathPrefix ('expected_' + grpprefix )
217232 chk .setControlName (self ._Test )
218233 chk .setMapRenderer (self ._MapRenderer )
219- res = chk .runTest (self ._Test , mismatch )
234+ res = False
235+ if imgpath :
236+ res = chk .compareImages (self ._Test , mismatch , str (imgpath ))
237+ else :
238+ res = chk .runTest (self ._Test , mismatch )
220239 if PALREPORT and not res : # don't report ok checks
221240 testname = self ._TestGroup + ' . ' + self ._Test
222241 PALREPORTS [testname ] = str (chk .report ().toLocal8Bit ())
223242 msg = '\n Render check failed for "{0}"' .format (self ._Test )
224243 return res , msg
225244
245+ def defaultWmsParams (self , projpath , layername ):
246+ return {
247+ 'SERVICE' : 'WMS' ,
248+ 'VERSION' : '1.3.0' ,
249+ 'REQUEST' : 'GetMap' ,
250+ 'MAP' : str (projpath ).strip (),
251+ # layer stacking order for rendering: bottom,to,top
252+ 'LAYERS' : ['background' , str (layername ).strip ()], # or 'name,name'
253+ 'STYLES' : ',' ,
254+ 'CRS' : 'EPSG:32613' , # self._CRS, # authid str or QgsCoordinateReferenceSystem obj
255+ 'BBOX' : '606510,4823130,612510,4827130' , # self.aoiExtent(),
256+ 'FORMAT' : 'image/png' , # or: 'image/png; mode=8bit'
257+ 'WIDTH' : '600' ,
258+ 'HEIGHT' : '400' ,
259+ 'DPI' : '72' ,
260+ 'MAP_RESOLUTION' : '72' ,
261+ 'FORMAT_OPTIONS' : 'dpi:72' ,
262+ 'TRANSPARENT' : 'FALSE' ,
263+ 'IgnoreGetMapUrl' : '1'
264+ }
265+
266+ @classmethod
267+ def setUpServerProjectAndDir (cls , testprojpath , testdir ):
268+ cls ._TestProj = QgsProject .instance ()
269+ cls ._TestProj .setFileName (testprojpath )
270+ try :
271+ shutil .copy (cls ._PalFeaturesDb , testdir )
272+ for qml in glob .glob (cls ._PalDataDir + os .sep + '*.qml' ):
273+ shutil .copy (qml , testdir )
274+ except IOError , e :
275+ raise IOError (str (e ) + '\n Could not set up test server directory' )
276+
226277
227278class TestPALConfig (TestQgsPalLabeling ):
228279
0 commit comments