Skip to content
Permalink
Browse files

Convert label unit tests to using only QgsMapSettings

- Rebuild all control images
  • Loading branch information
dakcarto committed Mar 4, 2014
1 parent bad1898 commit 53cfa9e203d56501b0f58bb865b910ed1117c3c8
Showing with 176 additions and 93 deletions.
  1. +115 −34 tests/src/python/test_qgspallabeling_base.py
  2. +1 −1 tests/src/python/test_qgspallabeling_canvas.py
  3. +59 −57 tests/src/python/test_qgspallabeling_composer.py
  4. +1 −1 tests/src/python/test_qgspallabeling_server.py
  5. BIN tests/testdata/control_images/expected_pal_canvas/sp_background_rect/sp_background_rect.png
  6. BIN ...ta/control_images/expected_pal_canvas/sp_background_rect_w_offset/sp_background_rect_w_offset.png
  7. BIN tests/testdata/control_images/expected_pal_canvas/sp_background_svg/sp_background_svg.png
  8. BIN ...data/control_images/expected_pal_canvas/sp_background_svg_w_offset/sp_background_svg_w_offset.png
  9. BIN tests/testdata/control_images/expected_pal_canvas/sp_default_label/sp_default_label.png
  10. BIN ...ta/control_images/expected_pal_canvas/sp_partials_labels_disabled/sp_partials_labels_disabled.png
  11. BIN ...data/control_images/expected_pal_canvas/sp_partials_labels_enabled/sp_partials_labels_enabled.png
  12. BIN tests/testdata/control_images/expected_pal_canvas/sp_text_color/sp_text_color.png
  13. BIN tests/testdata/control_images/expected_pal_canvas/sp_text_size_map_unit/sp_text_size_map_unit.png
  14. BIN ...s/testdata/control_images/expected_pal_composer/sp_img_background_rect/sp_img_background_rect.png
  15. BIN ..._images/expected_pal_composer/sp_img_background_rect_w_offset/sp_img_background_rect_w_offset.png
  16. BIN tests/testdata/control_images/expected_pal_composer/sp_img_background_svg/sp_img_background_svg.png
  17. BIN ...ol_images/expected_pal_composer/sp_img_background_svg_w_offset/sp_img_background_svg_w_offset.png
  18. BIN tests/testdata/control_images/expected_pal_composer/sp_img_default_label/sp_img_default_label.png
  19. BIN ..._images/expected_pal_composer/sp_img_partials_labels_disabled/sp_img_partials_labels_disabled.png
  20. BIN ...ol_images/expected_pal_composer/sp_img_partials_labels_enabled/sp_img_partials_labels_enabled.png
  21. BIN tests/testdata/control_images/expected_pal_composer/sp_img_text_color/sp_img_text_color.png
  22. BIN ...data/control_images/expected_pal_composer/sp_img_text_size_map_unit/sp_img_text_size_map_unit.png
@@ -26,20 +26,18 @@
import shutil
import StringIO
import tempfile

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import (
QGis,
QgsCoordinateReferenceSystem,
QgsDataSourceURI,
QgsLabelingEngineInterface,
QgsMapLayerRegistry,
QgsMapRenderer,
QgsMapSettings,
QgsPalLabeling,
QgsPalLayerSettings,
QgsProject,
QgsProviderRegistry,
QgsVectorLayer,
QgsRenderChecker
@@ -55,6 +53,7 @@
openInBrowserTab
)


QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
FONTSLOADED = loadTestFonts()

@@ -78,8 +77,6 @@ class TestQgsPalLabeling(TestCase):
""":type: QgsMapSettings"""
_Canvas = None
""":type: QgsMapCanvas"""
_Map = None
""":type: QgsMapCanvasMap"""
_Pal = None
""":type: QgsPalLabeling"""
_PalEngine = None
@@ -106,29 +103,18 @@ def setUpClass(cls):
cls._TestGroupAbbr = ''
cls._TestGroupCanvasAbbr = ''
cls._TestImage = ''
cls._TestMapSettings = None

# initialize class MapRegistry, Canvas, MapRenderer, Map and PAL
# noinspection PyArgumentList
cls._MapRegistry = QgsMapLayerRegistry.instance()
# set color to match render test comparisons background
cls._Canvas.setCanvasColor(QColor(152, 219, 249))
cls._Map = cls._Canvas.map()
cls._Map.resize(QSize(600, 400)) # is this necessary now?
cls._MapRenderer = cls._Canvas.mapRenderer()

cls._MapSettings = QgsMapSettings()
cls._CRS = QgsCoordinateReferenceSystem()
""":type: QgsCoordinateReferenceSystem"""
# default for labeling test data sources: WGS 84 / UTM zone 13N
cls._CRS.createFromSrid(32613)
cls._MapSettings.setBackgroundColor(QColor(152, 219, 249))
cls._MapSettings.setOutputSize(QSize(600, 400))
cls._MapSettings.setOutputDpi(72)
cls._MapSettings.setFlag(QgsMapSettings.Antialiasing)
cls._MapSettings.setDestinationCrs(cls._CRS)
cls._MapSettings.setCrsTransformEnabled(False)
cls._MapSettings.setMapUnits(cls._CRS.mapUnits()) # meters
cls._MapSettings.setExtent(cls.aoiExtent())
cls._MapSettings = cls.getBaseMapSettings()
osize = cls._MapSettings.outputSize()
cls._Canvas.resize(QSize(osize.width(), osize.height())) # necessary?
# set color to match render test comparisons background
cls._Canvas.setCanvasColor(cls._MapSettings.backgroundColor())

cls.setDefaultEngineSettings()
msg = ('\nCould not initialize PAL labeling engine, '
@@ -137,19 +123,19 @@ def setUpClass(cls):

cls._BaseSetup = True

@classmethod
def tearDownClass(cls):
"""Run after all tests"""
pass
# cls.removeAllLayers()

@classmethod
def setDefaultEngineSettings(cls):
"""Restore default settings for pal labelling"""
cls._Pal = QgsPalLabeling()
cls._MapRenderer.setLabelingEngine(cls._Pal)
cls._PalEngine = cls._MapRenderer.labelingEngine()

@classmethod
def tearDownClass(cls):
"""Run after all tests"""
pass
# cls.removeAllLayers()

@classmethod
def removeAllLayers(cls):
cls._MapRegistry.removeAllMapLayers()
@@ -192,6 +178,44 @@ def aoiExtent(cls):
aoilayer = QgsVectorLayer(uri.uri(), 'aoi', 'spatialite')
return aoilayer.extent()

@classmethod
def getBaseMapSettings(cls):
"""
:rtype: QgsMapSettings
"""
ms = QgsMapSettings()
crs = QgsCoordinateReferenceSystem()
""":type: QgsCoordinateReferenceSystem"""
# default for labeling test data: WGS 84 / UTM zone 13N
crs.createFromSrid(32613)
ms.setBackgroundColor(QColor(152, 219, 249))
ms.setOutputSize(QSize(600, 400))
ms.setOutputDpi(72)
ms.setFlag(QgsMapSettings.Antialiasing)
ms.setDestinationCrs(crs)
ms.setCrsTransformEnabled(False)
ms.setMapUnits(crs.mapUnits()) # meters
ms.setExtent(cls.aoiExtent())
return ms

def cloneMapSettings(self, oms):
"""
:param oms: QgsMapSettings
:rtype: QgsMapSettings
"""
ms = QgsMapSettings()
ms.setBackgroundColor(oms.backgroundColor())
ms.setOutputSize(oms.outputSize())
ms.setOutputDpi(oms.outputDpi())
ms.setFlags(oms.flags())
ms.setDestinationCrs(oms.destinationCrs())
ms.setCrsTransformEnabled(oms.hasCrsTransformEnabled())
ms.setMapUnits(oms.mapUnits())
ms.setExtent(oms.extent())

ms.setLayers(oms.layers())
return ms

def configTest(self, prefix, abbr):
"""Call in setUp() function of test subclass"""
self._TestGroupPrefix = prefix
@@ -244,13 +268,14 @@ def controlImagePath(self, grpprefix=''):
'expected_' + grpprefix,
self._Test, self._Test + '.png')

def saveContolImage(self, tmpimg=''):
def saveControlImage(self, tmpimg=''):
# don't save control images for RenderVsOtherOutput (Vs) tests, since
# those control images belong to a different test result
if ('PAL_CONTROL_IMAGE' not in os.environ
or 'Vs' in self._TestGroup):
return
imgpath = self.controlImagePath()
# print "saveControlImage: {0}".format(imgpath)
testdir = os.path.dirname(imgpath)
if not os.path.exists(testdir):
os.makedirs(testdir)
@@ -263,12 +288,68 @@ def saveContolImage(self, tmpimg=''):
if tmpimg and os.path.exists(tmpimg):
shutil.copyfile(tmpimg, imgpath)
else:
self._Map.render()
self._Canvas.saveAsImage(imgpath)
print '\nsaveControlImage.render(): entered'
print '{0}.{1}'.format(self._TestGroup, self._TestFunction)

ms = self._MapSettings # class settings
""":type: QgsMapSettings"""
if self._TestMapSettings is not None:
ms = self._TestMapSettings # per test settings
print 'self._MapSettings...'
print 'ms.layers(): {0}'.format(
[self._MapRegistry.mapLayer(i).name() for i in ms.layers()]
)
print 'ms.outputSize(): {0} x {1}'.format(
ms.outputSize().width(), ms.outputSize().height())
print 'ms.outputDpi(): {0}'.format(ms.outputDpi())
print 'ms.mapUnits(): {0}'.format(ms.mapUnits())
print 'ms.extent(): {0}'.format(ms.extent().toString())
print 'ms.hasCrsTransformEnabled(): {0}'.format(
ms.hasCrsTransformEnabled())
print 'ms.destinationCrs(): {0}'.format(
ms.destinationCrs().authid())

# pal = QgsPalLabeling()
pal = self._Pal.clone() # or custom settings are lost
pal.init(ms)
r = QgsMapRenderer()
r.setLabelingEngine(pal)

# this seems too redundant
r.setOutputSize(ms.outputSize(), ms.outputDpi())
r.setMapUnits(ms.mapUnits())
r.setExtent(ms.extent())
r.setProjectionsEnabled(ms.hasCrsTransformEnabled())
r.setDestinationCrs(ms.destinationCrs())
r.setLayerSet(ms.layers())

ctx = r.rendererContext()
ctx.setDrawEditingInformation(
ms.testFlag(QgsMapSettings.DrawEditingInfo))
ctx.setForceVectorOutput(
ms.testFlag(QgsMapSettings.ForceVectorOutput))
ctx.setUseAdvancedEffects(
ms.testFlag(QgsMapSettings.UseAdvancedEffects))

image = QImage(ms.outputSize(), QImage.Format_ARGB32)
image.fill(ms.backgroundColor().rgb())
image.setDotsPerMeterX(ms.outputDpi() / 25.4 * 1000)
image.setDotsPerMeterY(ms.outputDpi() / 25.4 * 1000)

p = QPainter(image)
r.render(p)
p.end()

if not image.save(imgpath, 'png'):
os.unlink(imgpath)

# delete extraneous world file (always generated)
wrld_file = imgbasepath + '.PNGw'
if os.path.exists(wrld_file):
os.remove(wrld_file)
# wrld_file = imgbasepath + '.PNGw'
# if os.path.exists(wrld_file):
# os.remove(wrld_file)

if not os.path.exists(imgpath):
raise OSError('Control image not created: {0}'.format(imgpath))

def renderCheck(self, mismatch=0, imgpath='', grpprefix=''):
"""Check rendered map canvas or existing image against control image
@@ -63,7 +63,7 @@ def setUp(self):

def checkTest(self, **kwargs):
self.lyr.writeToLayer(self.layer)
self.saveContolImage()
self.saveControlImage()
self.assertTrue(*self.renderCheck())


0 comments on commit 53cfa9e

Please sign in to comment.
You can’t perform that action at this time.