Skip to content

Commit

Permalink
Partially update annotation item test to newer framework
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 26, 2023
1 parent 4b15675 commit ecab2bf
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 31 deletions.
24 changes: 22 additions & 2 deletions python/testing/__init__.py
Expand Up @@ -28,6 +28,7 @@
import filecmp
import tempfile
from pathlib import Path
from typing import Optional

from qgis.PyQt.QtCore import (
QVariant,
Expand All @@ -37,6 +38,7 @@
QUrl
)
from qgis.PyQt.QtGui import (
QImage,
QDesktopServices
)
from qgis.core import (
Expand Down Expand Up @@ -75,6 +77,13 @@ def tearDownClass(cls):
if cls.report:
cls.write_local_html_report(cls.report)

@classmethod
def control_path_prefix(cls) -> Optional[str]:
"""
Returns the prefix for test control images used by the class
"""
return None

@classmethod
def write_local_html_report(cls, report: str):
report_dir = QgsRenderChecker.testReportDir()
Expand All @@ -90,7 +99,13 @@ def write_local_html_report(cls, report: str):
QDesktopServices.openUrl(QUrl.fromLocalFile(report_file))

@classmethod
def image_check(cls, name, reference_image, image, control_name=None, color_tolerance=2, allowed_mismatch=20):
def image_check(cls,
name: str,
reference_image: str,
image: QImage,
control_name=None,
color_tolerance: int = 2,
allowed_mismatch: int = 20):
temp_dir = QDir.tempPath() + '/'
file_name = temp_dir + name + ".png"
image.save(file_name, "PNG")
Expand All @@ -108,7 +123,12 @@ def image_check(cls, name, reference_image, image, control_name=None, color_tole
return result

@classmethod
def render_map_settings_check(cls, name, reference_image, map_settings: QgsMapSettings, color_tolerance=None, allowed_mismatch=None):
def render_map_settings_check(cls,
name: str,
reference_image: str,
map_settings: QgsMapSettings,
color_tolerance: Optional[int] = None,
allowed_mismatch: Optional[int] = None):
checker = QgsMultiRenderChecker()
checker.setMapSettings(map_settings)

Expand Down
79 changes: 50 additions & 29 deletions tests/src/python/test_qgsannotation.py
Expand Up @@ -45,6 +45,10 @@

class TestQgsAnnotation(unittest.TestCase):

@classmethod
def control_path_prefix(cls):
return "annotations"

def setUp(self):
self.report = "<h1>Python QgsAnnotation Tests</h1>\n"

Expand All @@ -64,12 +68,16 @@ def testTextAnnotation(self):
doc.setHtml('<p style="font-family: arial; font-weight: bold; font-size: 40px;">test annotation</p>')
a.setDocument(doc)
im = self.renderAnnotation(a, QPointF(20, 30))
self.assertTrue(self.imageCheck('text_annotation', 'text_annotation', im))
self.assertTrue(
self.image_check('text_annotation', 'text_annotation', im)
)

# check clone
clone = a.clone()
im = self.renderAnnotation(clone, QPointF(20, 30))
self.assertTrue(self.imageCheck('text_annotation', 'text_annotation', im))
self.assertTrue(
self.image_check('text_annotation', 'text_annotation', im)
)

def testTextAnnotationInLayout(self):
""" test rendering a text annotation"""
Expand All @@ -93,12 +101,16 @@ def testSvgAnnotation(self):
svg = TEST_DATA_DIR + "/sample_svg.svg"
a.setFilePath(svg)
im = self.renderAnnotation(a, QPointF(20, 30))
self.assertTrue(self.imageCheck('svg_annotation', 'svg_annotation', im))
self.assertTrue(
self.image_check('svg_annotation', 'svg_annotation', im)
)

# check clone
clone = a.clone()
im = self.renderAnnotation(clone, QPointF(20, 30))
self.assertTrue(self.imageCheck('svg_annotation', 'svg_annotation', im))
self.assertTrue(
self.image_check('svg_annotation', 'svg_annotation', im)
)

def testSvgAnnotationInLayout(self):
""" test rendering a svg annotation"""
Expand All @@ -121,12 +133,16 @@ def testHtmlAnnotation(self):
html = TEST_DATA_DIR + "/test_html.html"
a.setSourceFile(html)
im = self.renderAnnotation(a, QPointF(20, 30))
self.assertTrue(self.imageCheck('html_annotation', 'html_annotation', im))
self.assertTrue(
self.image_check('html_annotation', 'html_annotation', im)
)

# check clone
clone = a.clone()
im = self.renderAnnotation(clone, QPointF(20, 30))
self.assertTrue(self.imageCheck('html_annotation', 'html_annotation', im))
self.assertTrue(
self.image_check('html_annotation', 'html_annotation', im)
)

def testHtmlAnnotationSetHtmlSource(self):
""" test rendering html annotation where the html is set directly (not from file)"""
Expand All @@ -139,7 +155,11 @@ def testHtmlAnnotationSetHtmlSource(self):
htmlText = f.read()
a.setHtmlSource(htmlText)
im = self.renderAnnotation(a, QPointF(20, 30))
self.assertTrue(self.imageCheck('html_annotation_html_source', 'html_annotation', im))
self.assertTrue(
self.image_check(
'html_annotation_html_source', 'html_annotation', im
)
)

def testHtmlAnnotationInLayout(self):
""" test rendering a svg annotation"""
Expand All @@ -166,13 +186,17 @@ def testHtmlAnnotationWithFeature(self):
html = TEST_DATA_DIR + "/test_html_feature.html"
a.setSourceFile(html)
im = self.renderAnnotation(a, QPointF(20, 30))
self.assertTrue(self.imageCheck('html_nofeature', 'html_nofeature', im))
self.assertTrue(
self.image_check('html_nofeature', 'html_nofeature', im)
)
f = QgsFeature(layer.fields())
f.setValid(True)
f.setAttributes(['hurstbridge', 'somewhere'])
a.setAssociatedFeature(f)
im = self.renderAnnotation(a, QPointF(20, 30))
self.assertTrue(self.imageCheck('html_feature', 'html_feature', im))
self.assertTrue(
self.image_check('html_feature', 'html_feature', im)
)

def testFormAnnotation(self):
""" test rendering a form annotation"""
Expand All @@ -184,12 +208,16 @@ def testFormAnnotation(self):
ui = TEST_DATA_DIR + "/test_form.ui"
a.setDesignerForm(ui)
im = self.renderAnnotation(a, QPointF(20, 30))
self.assertTrue(self.imageCheck('form_annotation', 'form_annotation', im))
self.assertTrue(
self.image_check('form_annotation', 'form_annotation', im)
)

# check clone
clone = a.clone()
im = self.renderAnnotation(clone, QPointF(20, 30))
self.assertTrue(self.imageCheck('form_annotation', 'form_annotation', im))
self.assertTrue(
self.image_check('form_annotation', 'form_annotation', im)
)

def testFormAnnotationInLayout(self):
""" test rendering a form annotation"""
Expand All @@ -211,7 +239,9 @@ def testRelativePosition(self):
html = TEST_DATA_DIR + "/test_html.html"
a.setSourceFile(html)
im = self.renderAnnotation(a, QPointF(20, 30))
self.assertTrue(self.imageCheck('relative_style', 'relative_style', im))
self.assertTrue(
self.image_check('relative_style', 'relative_style', im)
)

def testMargins(self):
""" test rendering an annotation with margins"""
Expand All @@ -223,7 +253,9 @@ def testMargins(self):
html = TEST_DATA_DIR + "/test_html.html"
a.setSourceFile(html)
im = self.renderAnnotation(a, QPointF(20, 30))
self.assertTrue(self.imageCheck('annotation_margins', 'annotation_margins', im))
self.assertTrue(
self.image_check('annotation_margins', 'annotation_margins', im)
)

def testFillSymbol(self):
""" test rendering an annotation with fill symbol"""
Expand All @@ -232,7 +264,11 @@ def testFillSymbol(self):
a.setHasFixedMapPosition(False)
a.setFillSymbol(QgsFillSymbol.createSimple({'color': 'blue', 'width_border': '5', 'outline_color': 'black'}))
im = self.renderAnnotation(a, QPointF(20, 30))
self.assertTrue(self.imageCheck('annotation_fillstyle', 'annotation_fillstyle', im))
self.assertTrue(
self.image_check(
'annotation_fillstyle', 'annotation_fillstyle', im
)
)

def renderAnnotation(self, annotation, offset):
image = QImage(600, 400, QImage.Format_RGB32)
Expand Down Expand Up @@ -280,21 +316,6 @@ def renderAnnotationInLayout(self, test_name, annotation):
self.report += checker.report()
return result

def imageCheck(self, name, reference_image, image):
self.report += f"<h2>Render {name}</h2>\n"
temp_dir = QDir.tempPath() + '/'
file_name = temp_dir + 'annotation_' + name + ".png"
image.save(file_name, "PNG")
checker = QgsMultiRenderChecker()
checker.setControlPathPrefix("annotations")
checker.setControlName("expected_" + reference_image)
checker.setRenderedImage(file_name)
checker.setColorTolerance(2)
result = checker.runTest(name, 20)
self.report += checker.report()
print(self.report)
return result


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

0 comments on commit ecab2bf

Please sign in to comment.