14
14
15
15
import qgis # NOQA
16
16
import os
17
+ import tempfile
18
+ import shutil
17
19
18
20
from qgis .PyQt .QtCore import (
19
21
QSize ,
28
30
QgsVectorLayer ,
29
31
QgsProject ,
30
32
QgsRectangle ,
31
- QgsMultiRenderChecker
33
+ QgsRenderChecker
32
34
)
33
35
from qgis .gui import QgsHighlight
34
36
from qgis .testing import start_app , unittest
@@ -44,55 +46,49 @@ class TestQgsHighlight(unittest.TestCase):
44
46
def setUp (self ):
45
47
self .iface = get_iface ()
46
48
47
- lines_shp = os .path .join (TEST_DATA_DIR , 'lines.shp' )
48
- self .lines_layer = QgsVectorLayer (lines_shp , 'Lines' , 'ogr' )
49
- QgsProject .instance ().addMapLayer (self .lines_layer )
50
- polys_shp = os .path .join (TEST_DATA_DIR , 'polys.shp' )
51
- self .polys_layer = QgsVectorLayer (polys_shp , 'Polygons' , 'ogr' )
52
- QgsProject .instance ().addMapLayer (self .polys_layer )
53
-
54
49
self .iface .mapCanvas ().resize (QSize (400 , 400 ))
55
50
56
- self .iface .mapCanvas ().setExtent (QgsRectangle (- 113 , 28 , - 91 , 40 ))
57
-
58
- self .mapsettings = self .iface .mapCanvas ().mapSettings ()
59
- self .mapsettings .setOutputSize (QSize (400 , 400 ))
60
- self .mapsettings .setOutputDpi (96 )
61
- self .mapsettings .setExtent (QgsRectangle (- 113 , 28 , - 91 , 40 ))
62
- self .mapsettings .setBackgroundColor (QColor ("white" ))
63
-
64
51
def tearDown (self ):
65
52
QgsProject .instance ().removeAllMapLayers ()
66
53
67
- def testLine (self ):
68
- line = next (self .lines_layer .getFeatures ()).geometry ()
69
- highlight = QgsHighlight (self .iface .mapCanvas (), line , self .lines_layer )
70
- color = QColor (Qt .red )
71
- highlight .setColor (color )
72
- color .setAlpha (50 )
73
- highlight .setFillColor (color )
74
- highlight .show ()
75
- image = QImage (QSize (400 , 400 ), QImage .Format_ARGB32 )
76
- painter = QPainter ()
77
- painter .begin (image )
78
- self .iface .mapCanvas ().render (painter )
79
- painter .end ()
54
+ def runTestForLayer (self , layer , testname ):
55
+ tempdir = tempfile .mkdtemp ()
80
56
81
- def testPolygon (self ):
82
- poly = next (self .polys_layer .getFeatures ()).geometry ()
83
- self .iface .mapCanvas ().setExtent (self .polys_layer .extent ())
84
- highlight = QgsHighlight (self .iface .mapCanvas (), poly , self .polys_layer )
57
+ layer = QgsVectorLayer (layer , 'Layer' , 'ogr' )
58
+ QgsProject .instance ().addMapLayer (layer )
59
+ self .iface .mapCanvas ().setExtent (layer .extent ())
60
+
61
+ geom = next (layer .getFeatures ()).geometry ()
62
+
63
+ highlight = QgsHighlight (self .iface .mapCanvas (), geom , layer )
85
64
color = QColor (Qt .red )
86
65
highlight .setColor (color )
87
66
color .setAlpha (50 )
88
67
highlight .setFillColor (color )
89
68
highlight .show ()
69
+
90
70
image = QImage (QSize (400 , 400 ), QImage .Format_ARGB32 )
91
71
painter = QPainter ()
92
72
painter .begin (image )
93
73
self .iface .mapCanvas ().render (painter )
94
74
painter .end ()
75
+ control_image = os .path .join (tempdir , 'highlight_{}.png' .format (testname ))
76
+ image .save (control_image )
77
+ checker = QgsRenderChecker ()
78
+ checker .setControlPathPrefix ("highlight" )
79
+ checker .setControlName ("expected_highlight_{}" .format (testname ))
80
+ checker .setRenderedImage (control_image )
81
+ checker .setSizeTolerance (10 , 10 )
82
+ self .assertTrue (checker .compareImages ("highlight_{}" .format (testname ), 10 ))
83
+ shutil .rmtree (tempdir )
84
+
85
+ def testLine (self ):
86
+ lines_shp = os .path .join (TEST_DATA_DIR , 'lines.shp' )
87
+ self .runTestForLayer (lines_shp , 'lines' )
95
88
89
+ def testPolygon (self ):
90
+ polys_shp = os .path .join (TEST_DATA_DIR , 'polys.shp' )
91
+ self .runTestForLayer (polys_shp , 'polygons' )
96
92
97
93
98
94
if __name__ == '__main__' :
0 commit comments