Skip to content

Commit ecab2bf

Browse files
committedApr 26, 2023
Partially update annotation item test to newer framework
1 parent 4b15675 commit ecab2bf

File tree

2 files changed

+72
-31
lines changed

2 files changed

+72
-31
lines changed
 

‎python/testing/__init__.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import filecmp
2929
import tempfile
3030
from pathlib import Path
31+
from typing import Optional
3132

3233
from qgis.PyQt.QtCore import (
3334
QVariant,
@@ -37,6 +38,7 @@
3738
QUrl
3839
)
3940
from qgis.PyQt.QtGui import (
41+
QImage,
4042
QDesktopServices
4143
)
4244
from qgis.core import (
@@ -75,6 +77,13 @@ def tearDownClass(cls):
7577
if cls.report:
7678
cls.write_local_html_report(cls.report)
7779

80+
@classmethod
81+
def control_path_prefix(cls) -> Optional[str]:
82+
"""
83+
Returns the prefix for test control images used by the class
84+
"""
85+
return None
86+
7887
@classmethod
7988
def write_local_html_report(cls, report: str):
8089
report_dir = QgsRenderChecker.testReportDir()
@@ -90,7 +99,13 @@ def write_local_html_report(cls, report: str):
9099
QDesktopServices.openUrl(QUrl.fromLocalFile(report_file))
91100

92101
@classmethod
93-
def image_check(cls, name, reference_image, image, control_name=None, color_tolerance=2, allowed_mismatch=20):
102+
def image_check(cls,
103+
name: str,
104+
reference_image: str,
105+
image: QImage,
106+
control_name=None,
107+
color_tolerance: int = 2,
108+
allowed_mismatch: int = 20):
94109
temp_dir = QDir.tempPath() + '/'
95110
file_name = temp_dir + name + ".png"
96111
image.save(file_name, "PNG")
@@ -108,7 +123,12 @@ def image_check(cls, name, reference_image, image, control_name=None, color_tole
108123
return result
109124

110125
@classmethod
111-
def render_map_settings_check(cls, name, reference_image, map_settings: QgsMapSettings, color_tolerance=None, allowed_mismatch=None):
126+
def render_map_settings_check(cls,
127+
name: str,
128+
reference_image: str,
129+
map_settings: QgsMapSettings,
130+
color_tolerance: Optional[int] = None,
131+
allowed_mismatch: Optional[int] = None):
112132
checker = QgsMultiRenderChecker()
113133
checker.setMapSettings(map_settings)
114134

‎tests/src/python/test_qgsannotation.py

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545

4646
class TestQgsAnnotation(unittest.TestCase):
4747

48+
@classmethod
49+
def control_path_prefix(cls):
50+
return "annotations"
51+
4852
def setUp(self):
4953
self.report = "<h1>Python QgsAnnotation Tests</h1>\n"
5054

@@ -64,12 +68,16 @@ def testTextAnnotation(self):
6468
doc.setHtml('<p style="font-family: arial; font-weight: bold; font-size: 40px;">test annotation</p>')
6569
a.setDocument(doc)
6670
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+
)
6874

6975
# check clone
7076
clone = a.clone()
7177
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+
)
7381

7482
def testTextAnnotationInLayout(self):
7583
""" test rendering a text annotation"""
@@ -93,12 +101,16 @@ def testSvgAnnotation(self):
93101
svg = TEST_DATA_DIR + "/sample_svg.svg"
94102
a.setFilePath(svg)
95103
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+
)
97107

98108
# check clone
99109
clone = a.clone()
100110
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+
)
102114

103115
def testSvgAnnotationInLayout(self):
104116
""" test rendering a svg annotation"""
@@ -121,12 +133,16 @@ def testHtmlAnnotation(self):
121133
html = TEST_DATA_DIR + "/test_html.html"
122134
a.setSourceFile(html)
123135
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+
)
125139

126140
# check clone
127141
clone = a.clone()
128142
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+
)
130146

131147
def testHtmlAnnotationSetHtmlSource(self):
132148
""" test rendering html annotation where the html is set directly (not from file)"""
@@ -139,7 +155,11 @@ def testHtmlAnnotationSetHtmlSource(self):
139155
htmlText = f.read()
140156
a.setHtmlSource(htmlText)
141157
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+
)
143163

144164
def testHtmlAnnotationInLayout(self):
145165
""" test rendering a svg annotation"""
@@ -166,13 +186,17 @@ def testHtmlAnnotationWithFeature(self):
166186
html = TEST_DATA_DIR + "/test_html_feature.html"
167187
a.setSourceFile(html)
168188
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+
)
170192
f = QgsFeature(layer.fields())
171193
f.setValid(True)
172194
f.setAttributes(['hurstbridge', 'somewhere'])
173195
a.setAssociatedFeature(f)
174196
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+
)
176200

177201
def testFormAnnotation(self):
178202
""" test rendering a form annotation"""
@@ -184,12 +208,16 @@ def testFormAnnotation(self):
184208
ui = TEST_DATA_DIR + "/test_form.ui"
185209
a.setDesignerForm(ui)
186210
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+
)
188214

189215
# check clone
190216
clone = a.clone()
191217
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+
)
193221

194222
def testFormAnnotationInLayout(self):
195223
""" test rendering a form annotation"""
@@ -211,7 +239,9 @@ def testRelativePosition(self):
211239
html = TEST_DATA_DIR + "/test_html.html"
212240
a.setSourceFile(html)
213241
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+
)
215245

216246
def testMargins(self):
217247
""" test rendering an annotation with margins"""
@@ -223,7 +253,9 @@ def testMargins(self):
223253
html = TEST_DATA_DIR + "/test_html.html"
224254
a.setSourceFile(html)
225255
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+
)
227259

228260
def testFillSymbol(self):
229261
""" test rendering an annotation with fill symbol"""
@@ -232,7 +264,11 @@ def testFillSymbol(self):
232264
a.setHasFixedMapPosition(False)
233265
a.setFillSymbol(QgsFillSymbol.createSimple({'color': 'blue', 'width_border': '5', 'outline_color': 'black'}))
234266
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+
)
236272

237273
def renderAnnotation(self, annotation, offset):
238274
image = QImage(600, 400, QImage.Format_RGB32)
@@ -280,21 +316,6 @@ def renderAnnotationInLayout(self, test_name, annotation):
280316
self.report += checker.report()
281317
return result
282318

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-
298319

299320
if __name__ == '__main__':
300321
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.