Skip to content
Permalink
Browse files
Add control images to highlight check
  • Loading branch information
m-kuhn committed Nov 8, 2017
1 parent 05868c2 commit 593bff2cf7426cfa0a9d3a0b9fcd17ce3c8c31fe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
@@ -14,6 +14,8 @@

import qgis # NOQA
import os
import tempfile
import shutil

from qgis.PyQt.QtCore import (
QSize,
@@ -28,7 +30,7 @@
QgsVectorLayer,
QgsProject,
QgsRectangle,
QgsMultiRenderChecker
QgsRenderChecker
)
from qgis.gui import QgsHighlight
from qgis.testing import start_app, unittest
@@ -44,55 +46,49 @@ class TestQgsHighlight(unittest.TestCase):
def setUp(self):
self.iface = get_iface()

lines_shp = os.path.join(TEST_DATA_DIR, 'lines.shp')
self.lines_layer = QgsVectorLayer(lines_shp, 'Lines', 'ogr')
QgsProject.instance().addMapLayer(self.lines_layer)
polys_shp = os.path.join(TEST_DATA_DIR, 'polys.shp')
self.polys_layer = QgsVectorLayer(polys_shp, 'Polygons', 'ogr')
QgsProject.instance().addMapLayer(self.polys_layer)

self.iface.mapCanvas().resize(QSize(400, 400))

self.iface.mapCanvas().setExtent(QgsRectangle(-113, 28, -91, 40))

self.mapsettings = self.iface.mapCanvas().mapSettings()
self.mapsettings.setOutputSize(QSize(400, 400))
self.mapsettings.setOutputDpi(96)
self.mapsettings.setExtent(QgsRectangle(-113, 28, -91, 40))
self.mapsettings.setBackgroundColor(QColor("white"))

def tearDown(self):
QgsProject.instance().removeAllMapLayers()

def testLine(self):
line = next(self.lines_layer.getFeatures()).geometry()
highlight = QgsHighlight(self.iface.mapCanvas(), line, self.lines_layer)
color = QColor(Qt.red)
highlight.setColor(color)
color.setAlpha(50)
highlight.setFillColor(color)
highlight.show()
image = QImage(QSize(400, 400), QImage.Format_ARGB32)
painter = QPainter()
painter.begin(image)
self.iface.mapCanvas().render(painter)
painter.end()
def runTestForLayer(self, layer, testname):
tempdir = tempfile.mkdtemp()

def testPolygon(self):
poly = next(self.polys_layer.getFeatures()).geometry()
self.iface.mapCanvas().setExtent(self.polys_layer.extent())
highlight = QgsHighlight(self.iface.mapCanvas(), poly, self.polys_layer)
layer = QgsVectorLayer(layer, 'Layer', 'ogr')
QgsProject.instance().addMapLayer(layer)
self.iface.mapCanvas().setExtent(layer.extent())

geom = next(layer.getFeatures()).geometry()

highlight = QgsHighlight(self.iface.mapCanvas(), geom, layer)
color = QColor(Qt.red)
highlight.setColor(color)
color.setAlpha(50)
highlight.setFillColor(color)
highlight.show()

image = QImage(QSize(400, 400), QImage.Format_ARGB32)
painter = QPainter()
painter.begin(image)
self.iface.mapCanvas().render(painter)
painter.end()
control_image = os.path.join(tempdir, 'highlight_{}.png'.format(testname))
image.save(control_image)
checker = QgsRenderChecker()
checker.setControlPathPrefix("highlight")
checker.setControlName("expected_highlight_{}".format(testname))
checker.setRenderedImage(control_image)
checker.setSizeTolerance(10, 10)
self.assertTrue(checker.compareImages("highlight_{}".format(testname), 10))
shutil.rmtree(tempdir)

def testLine(self):
lines_shp = os.path.join(TEST_DATA_DIR, 'lines.shp')
self.runTestForLayer(lines_shp, 'lines')

def testPolygon(self):
polys_shp = os.path.join(TEST_DATA_DIR, 'polys.shp')
self.runTestForLayer(polys_shp, 'polygons')


if __name__ == '__main__':
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 593bff2

Please sign in to comment.