8 changes: 8 additions & 0 deletions python/core/qgsrenderchecker.sip
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class QgsRenderChecker
void setRenderedImage( QString theImageFileName );
void setMapRenderer( QgsMapRenderer * thepMapRenderer ) /Deprecated/;
void setMapSettings( const QgsMapSettings& mapSettings );

/** Set tolerance for color components used by runTest() and compareImages().
* Default value is 0.
* @param theColorTolerance is maximum difference for each color component
* including alpha to be considered correct.
* @note added in 2.1
*/
void setColorTolerance( unsigned int theColorTolerance );
/**
* Test using renderer to generate the image to be compared.
* @param theTestName - to be used as the basis for writing a file to
Expand Down
12 changes: 8 additions & 4 deletions tests/src/python/test_qgspallabeling_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def setUpClass(cls):
cls._TestMapSettings = None
cls._Mismatch = 0
cls._Mismatches = dict()
cls._ColorTol = 0
cls._ColorTols = dict()

# initialize class MapRegistry, Canvas, MapRenderer, Map and PAL
# noinspection PyArgumentList
Expand Down Expand Up @@ -368,12 +370,13 @@ def saveControlImage(self, tmpimg=''):
if not os.path.exists(imgpath):
raise OSError('Control image not created: {0}'.format(imgpath))

def renderCheck(self, mismatch=0, imgpath='', grpprefix=''):
def renderCheck(self, mismatch=0, colortol=0, imgpath='', grpprefix=''):
"""Check rendered map canvas or existing image against control image
mismatch: number of pixels different from control, and still valid check
imgpath: existing image; if present, skips rendering canvas
grpprefix: compare test image/rendering against different test group
:mismatch: number of pixels different from control, and still valid
:colortol: maximum difference for each color component including alpha
:imgpath: existing image; if present, skips rendering canvas
:grpprefix: compare test image/rendering against different test group
"""
if not grpprefix:
grpprefix = self._TestGroupPrefix
Expand All @@ -383,6 +386,7 @@ def renderCheck(self, mismatch=0, imgpath='', grpprefix=''):
chk = QgsRenderChecker()
chk.setControlPathPrefix('expected_' + grpprefix)
chk.setControlName(self._Test)
chk.setColorTolerance(colortol)
chk.setMapSettings(self._MapSettings)
# noinspection PyUnusedLocal
res = False
Expand Down
3 changes: 3 additions & 0 deletions tests/src/python/test_qgspallabeling_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ def tearDownClass(cls):
def setUp(self):
"""Run before each test."""
super(TestCanvasBase, self).setUp()
self._Mismatch = 0
self._ColorTol = 0
self._Mismatches.clear()
self._ColorTols.clear()

def checkTest(self, **kwargs):
self.lyr.writeToLayer(self.layer)
Expand Down
19 changes: 14 additions & 5 deletions tests/src/python/test_qgspallabeling_composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ def setUp(self):
self._TestImage = ''
# ensure per test map settings stay encapsulated
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)
self._Mismatch = 200 # default mismatch for crosscheck
self._Mismatch = 0
self._ColorTol = 0
self._Mismatches.clear()
self._ColorTols.clear()

def _set_up_composition(self, width, height, dpi):
# set up composition and add map
Expand Down Expand Up @@ -259,11 +261,18 @@ def checkTest(self, **kwargs):
res_m, self._TestImage = self.get_composer_output(self._TestKind)
self.assertTrue(res_m, 'Failed to retrieve/save output from composer')
self.saveControlImage(self._TestImage)
mismatch = self._Mismatch
if (self._TestGroup in self._Mismatches
and 'PAL_NO_MISMATCH' not in os.environ):
mismatch = self._Mismatches[self._TestGroup]
mismatch = 0
if 'PAL_NO_MISMATCH' not in os.environ:
# some mismatch expected
mismatch = self._Mismatch if self._Mismatch else 200
if self._TestGroup in self._Mismatches:
mismatch = self._Mismatches[self._TestGroup]
colortol = 0
if 'PAL_NO_COLORTOL' not in os.environ:
if self._TestGroup in self._ColorTols:
colortol = self._ColorTols[self._TestGroup]
self.assertTrue(*self.renderCheck(mismatch=mismatch,
colortol=colortol,
imgpath=self._TestImage))


Expand Down
22 changes: 19 additions & 3 deletions tests/src/python/test_qgspallabeling_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def setUpClass(cls):
# noinspection PyArgumentList
cls._CacheDir = settings.value(
"cache/directory",
os.path.join(QgsApplication.qgisSettingsDirPath(), "cache"),
os.path.join(unicode(QgsApplication.qgisSettingsDirPath()),
"cache"),
type=unicode)

@classmethod
Expand All @@ -109,8 +110,10 @@ def setUp(self):
self._TestImage = ''
# ensure per test map settings stay encapsulated
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)
self._Mismatch = 0
self._ColorTol = 0
self._Mismatches.clear()
self._Mismatch = 50 # default for server tests; some mismatch expected
self._ColorTols.clear()

# noinspection PyPep8Naming
def delete_cache(self):
Expand Down Expand Up @@ -161,7 +164,20 @@ def checkTest(self, **kwargs):
# print self._TestImage.__repr__()
self.saveControlImage(self._TestImage)
self.assertTrue(res_m, 'Failed to retrieve/save image from test server')
self.assertTrue(*self.renderCheck(mismatch=self._Mismatch,
mismatch = 0
if 'PAL_NO_MISMATCH' not in os.environ:
# some mismatch expected
mismatch = self._Mismatch if self._Mismatch else 50
if self._TestGroup in self._Mismatches:
mismatch = self._Mismatches[self._TestGroup]
colortol = 0
if 'PAL_NO_COLORTOL' not in os.environ:
# some mismatch expected
# colortol = self._ColorTol if self._ColorTol else 10
if self._TestGroup in self._ColorTols:
colortol = self._ColorTols[self._TestGroup]
self.assertTrue(*self.renderCheck(mismatch=mismatch,
colortol=colortol,
imgpath=self._TestImage))


Expand Down
8 changes: 7 additions & 1 deletion tests/src/python/test_qgspallabeling_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ def __init__(self):
""":type: QgsPalLabeling"""
self._Canvas = None
""":type: QgsMapCanvas"""
# custom mismatches per group/test (should not mask needed anomaly)
# custom mismatches per group/test (should not mask any needed anomaly)
# e.g. self._Mismatches['TestClassName'] = 300
# check base output class's checkTest() or sublcasses for any defaults
self._Mismatches = dict()
# custom color tolerances per group/test: 1 - 20 (0 default, 20 max)
# (should not mask any needed anomaly)
# e.g. self._ColorTols['TestClassName'] = 10
# check base output class's checkTest() or sublcasses for any defaults
self._ColorTols = dict()

# noinspection PyMethodMayBeStatic
def checkTest(self, **kwargs):
Expand Down