23
23
import sys
24
24
import datetime
25
25
import glob
26
+ import shutil
26
27
import StringIO
27
28
import tempfile
28
29
from PyQt4 .QtCore import *
36
37
QgsMapRenderer ,
37
38
QgsPalLabeling ,
38
39
QgsPalLayerSettings ,
40
+ QgsProject ,
39
41
QgsProviderRegistry ,
40
42
QgsVectorLayer ,
41
43
QgsRenderChecker
@@ -64,6 +66,7 @@ class TestQgsPalLabeling(TestCase):
64
66
_PalDataDir = os .path .join (_TestDataDir , 'labeling' )
65
67
_PalFeaturesDb = os .path .join (_PalDataDir , 'pal_features_v3.sqlite' )
66
68
_TestFont = TESTFONT
69
+ _TestProj = None
67
70
_MapRegistry = None
68
71
_MapRenderer = None
69
72
_Canvas = None
@@ -77,8 +80,7 @@ def setUpClass(cls):
77
80
QGISAPP , CANVAS , IFACE , PARENT
78
81
79
82
# 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'
82
84
res = 'spatialite' in QgsProviderRegistry .instance ().providerList ()
83
85
assert res , msg
84
86
@@ -90,6 +92,7 @@ def setUpClass(cls):
90
92
cls ._TestGroup = ''
91
93
cls ._TestGroupPrefix = ''
92
94
cls ._TestGroupAbbr = ''
95
+ cls ._TestImage = ''
93
96
94
97
# initialize class MapRegistry, Canvas, MapRenderer, Map and PAL
95
98
cls ._MapRegistry = QgsMapLayerRegistry .instance ()
@@ -98,10 +101,10 @@ def setUpClass(cls):
98
101
cls ._Map = cls ._Canvas .map ()
99
102
cls ._Map .resize (QSize (600 , 400 ))
100
103
cls ._MapRenderer = cls ._Canvas .mapRenderer ()
101
- crs = QgsCoordinateReferenceSystem ()
104
+ cls . _CRS = QgsCoordinateReferenceSystem ()
102
105
# 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 )
105
108
# use platform's native logical output dpi for QgsMapRenderer on launch
106
109
107
110
cls ._Pal = QgsPalLabeling ()
@@ -195,7 +198,7 @@ def settingsDict(lyr):
195
198
res [attr ] = value
196
199
return res
197
200
198
- def saveContolImage (self ):
201
+ def saveContolImage (self , tmpimg = '' ):
199
202
if 'PAL_CONTROL_IMAGE' not in os .environ :
200
203
return
201
204
testgrpdir = 'expected_' + self ._TestGroupPrefix
@@ -208,21 +211,69 @@ def saveContolImage(self):
208
211
for f in glob .glob (imgbasepath + '.*' ):
209
212
if os .path .exists (f ):
210
213
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
215
230
chk = QgsRenderChecker ()
216
- chk .setControlPathPrefix ('expected_' + self . _TestGroupPrefix )
231
+ chk .setControlPathPrefix ('expected_' + grpprefix )
217
232
chk .setControlName (self ._Test )
218
233
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 )
220
239
if PALREPORT and not res : # don't report ok checks
221
240
testname = self ._TestGroup + ' . ' + self ._Test
222
241
PALREPORTS [testname ] = str (chk .report ().toLocal8Bit ())
223
242
msg = '\n Render check failed for "{0}"' .format (self ._Test )
224
243
return res , msg
225
244
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
+
226
277
227
278
class TestPALConfig (TestQgsPalLabeling ):
228
279
0 commit comments