45
45
46
46
class TestQgsAnnotation (unittest .TestCase ):
47
47
48
+ @classmethod
49
+ def control_path_prefix (cls ):
50
+ return "annotations"
51
+
48
52
def setUp (self ):
49
53
self .report = "<h1>Python QgsAnnotation Tests</h1>\n "
50
54
@@ -64,12 +68,16 @@ def testTextAnnotation(self):
64
68
doc .setHtml ('<p style="font-family: arial; font-weight: bold; font-size: 40px;">test annotation</p>' )
65
69
a .setDocument (doc )
66
70
im = self .renderAnnotation (a , QPointF (20 , 30 ))
67
- self .assertTrue (self .imageCheck ('text_annotation' , 'text_annotation' , im ))
71
+ self .assertTrue (
72
+ self .image_check ('text_annotation' , 'text_annotation' , im )
73
+ )
68
74
69
75
# check clone
70
76
clone = a .clone ()
71
77
im = self .renderAnnotation (clone , QPointF (20 , 30 ))
72
- self .assertTrue (self .imageCheck ('text_annotation' , 'text_annotation' , im ))
78
+ self .assertTrue (
79
+ self .image_check ('text_annotation' , 'text_annotation' , im )
80
+ )
73
81
74
82
def testTextAnnotationInLayout (self ):
75
83
""" test rendering a text annotation"""
@@ -93,12 +101,16 @@ def testSvgAnnotation(self):
93
101
svg = TEST_DATA_DIR + "/sample_svg.svg"
94
102
a .setFilePath (svg )
95
103
im = self .renderAnnotation (a , QPointF (20 , 30 ))
96
- self .assertTrue (self .imageCheck ('svg_annotation' , 'svg_annotation' , im ))
104
+ self .assertTrue (
105
+ self .image_check ('svg_annotation' , 'svg_annotation' , im )
106
+ )
97
107
98
108
# check clone
99
109
clone = a .clone ()
100
110
im = self .renderAnnotation (clone , QPointF (20 , 30 ))
101
- self .assertTrue (self .imageCheck ('svg_annotation' , 'svg_annotation' , im ))
111
+ self .assertTrue (
112
+ self .image_check ('svg_annotation' , 'svg_annotation' , im )
113
+ )
102
114
103
115
def testSvgAnnotationInLayout (self ):
104
116
""" test rendering a svg annotation"""
@@ -121,12 +133,16 @@ def testHtmlAnnotation(self):
121
133
html = TEST_DATA_DIR + "/test_html.html"
122
134
a .setSourceFile (html )
123
135
im = self .renderAnnotation (a , QPointF (20 , 30 ))
124
- self .assertTrue (self .imageCheck ('html_annotation' , 'html_annotation' , im ))
136
+ self .assertTrue (
137
+ self .image_check ('html_annotation' , 'html_annotation' , im )
138
+ )
125
139
126
140
# check clone
127
141
clone = a .clone ()
128
142
im = self .renderAnnotation (clone , QPointF (20 , 30 ))
129
- self .assertTrue (self .imageCheck ('html_annotation' , 'html_annotation' , im ))
143
+ self .assertTrue (
144
+ self .image_check ('html_annotation' , 'html_annotation' , im )
145
+ )
130
146
131
147
def testHtmlAnnotationSetHtmlSource (self ):
132
148
""" test rendering html annotation where the html is set directly (not from file)"""
@@ -139,7 +155,11 @@ def testHtmlAnnotationSetHtmlSource(self):
139
155
htmlText = f .read ()
140
156
a .setHtmlSource (htmlText )
141
157
im = self .renderAnnotation (a , QPointF (20 , 30 ))
142
- self .assertTrue (self .imageCheck ('html_annotation_html_source' , 'html_annotation' , im ))
158
+ self .assertTrue (
159
+ self .image_check (
160
+ 'html_annotation_html_source' , 'html_annotation' , im
161
+ )
162
+ )
143
163
144
164
def testHtmlAnnotationInLayout (self ):
145
165
""" test rendering a svg annotation"""
@@ -166,13 +186,17 @@ def testHtmlAnnotationWithFeature(self):
166
186
html = TEST_DATA_DIR + "/test_html_feature.html"
167
187
a .setSourceFile (html )
168
188
im = self .renderAnnotation (a , QPointF (20 , 30 ))
169
- self .assertTrue (self .imageCheck ('html_nofeature' , 'html_nofeature' , im ))
189
+ self .assertTrue (
190
+ self .image_check ('html_nofeature' , 'html_nofeature' , im )
191
+ )
170
192
f = QgsFeature (layer .fields ())
171
193
f .setValid (True )
172
194
f .setAttributes (['hurstbridge' , 'somewhere' ])
173
195
a .setAssociatedFeature (f )
174
196
im = self .renderAnnotation (a , QPointF (20 , 30 ))
175
- self .assertTrue (self .imageCheck ('html_feature' , 'html_feature' , im ))
197
+ self .assertTrue (
198
+ self .image_check ('html_feature' , 'html_feature' , im )
199
+ )
176
200
177
201
def testFormAnnotation (self ):
178
202
""" test rendering a form annotation"""
@@ -184,12 +208,16 @@ def testFormAnnotation(self):
184
208
ui = TEST_DATA_DIR + "/test_form.ui"
185
209
a .setDesignerForm (ui )
186
210
im = self .renderAnnotation (a , QPointF (20 , 30 ))
187
- self .assertTrue (self .imageCheck ('form_annotation' , 'form_annotation' , im ))
211
+ self .assertTrue (
212
+ self .image_check ('form_annotation' , 'form_annotation' , im )
213
+ )
188
214
189
215
# check clone
190
216
clone = a .clone ()
191
217
im = self .renderAnnotation (clone , QPointF (20 , 30 ))
192
- self .assertTrue (self .imageCheck ('form_annotation' , 'form_annotation' , im ))
218
+ self .assertTrue (
219
+ self .image_check ('form_annotation' , 'form_annotation' , im )
220
+ )
193
221
194
222
def testFormAnnotationInLayout (self ):
195
223
""" test rendering a form annotation"""
@@ -211,7 +239,9 @@ def testRelativePosition(self):
211
239
html = TEST_DATA_DIR + "/test_html.html"
212
240
a .setSourceFile (html )
213
241
im = self .renderAnnotation (a , QPointF (20 , 30 ))
214
- self .assertTrue (self .imageCheck ('relative_style' , 'relative_style' , im ))
242
+ self .assertTrue (
243
+ self .image_check ('relative_style' , 'relative_style' , im )
244
+ )
215
245
216
246
def testMargins (self ):
217
247
""" test rendering an annotation with margins"""
@@ -223,7 +253,9 @@ def testMargins(self):
223
253
html = TEST_DATA_DIR + "/test_html.html"
224
254
a .setSourceFile (html )
225
255
im = self .renderAnnotation (a , QPointF (20 , 30 ))
226
- self .assertTrue (self .imageCheck ('annotation_margins' , 'annotation_margins' , im ))
256
+ self .assertTrue (
257
+ self .image_check ('annotation_margins' , 'annotation_margins' , im )
258
+ )
227
259
228
260
def testFillSymbol (self ):
229
261
""" test rendering an annotation with fill symbol"""
@@ -232,7 +264,11 @@ def testFillSymbol(self):
232
264
a .setHasFixedMapPosition (False )
233
265
a .setFillSymbol (QgsFillSymbol .createSimple ({'color' : 'blue' , 'width_border' : '5' , 'outline_color' : 'black' }))
234
266
im = self .renderAnnotation (a , QPointF (20 , 30 ))
235
- self .assertTrue (self .imageCheck ('annotation_fillstyle' , 'annotation_fillstyle' , im ))
267
+ self .assertTrue (
268
+ self .image_check (
269
+ 'annotation_fillstyle' , 'annotation_fillstyle' , im
270
+ )
271
+ )
236
272
237
273
def renderAnnotation (self , annotation , offset ):
238
274
image = QImage (600 , 400 , QImage .Format_RGB32 )
@@ -280,21 +316,6 @@ def renderAnnotationInLayout(self, test_name, annotation):
280
316
self .report += checker .report ()
281
317
return result
282
318
283
- def imageCheck (self , name , reference_image , image ):
284
- self .report += f"<h2>Render { name } </h2>\n "
285
- temp_dir = QDir .tempPath () + '/'
286
- file_name = temp_dir + 'annotation_' + name + ".png"
287
- image .save (file_name , "PNG" )
288
- checker = QgsMultiRenderChecker ()
289
- checker .setControlPathPrefix ("annotations" )
290
- checker .setControlName ("expected_" + reference_image )
291
- checker .setRenderedImage (file_name )
292
- checker .setColorTolerance (2 )
293
- result = checker .runTest (name , 20 )
294
- self .report += checker .report ()
295
- print (self .report )
296
- return result
297
-
298
319
299
320
if __name__ == '__main__' :
300
321
unittest .main ()
0 commit comments