|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsPalLabeling: base suite of render check tests |
| 3 | +
|
| 4 | +Class is meant to be inherited by classes that test different labeling outputs |
| 5 | +
|
| 6 | +See <qgis-src-dir>/tests/testdata/labeling/README.rst for description. |
| 7 | +
|
| 8 | +.. note:: This program is free software; you can redistribute it and/or modify |
| 9 | +it under the terms of the GNU General Public License as published by |
| 10 | +the Free Software Foundation; either version 2 of the License, or |
| 11 | +(at your option) any later version. |
| 12 | +""" |
| 13 | +__author__ = 'Nyall Dawson' |
| 14 | +__date__ = '2015-08-24' |
| 15 | +__copyright__ = 'Copyright 2015, The QGIS Project' |
| 16 | +# This will get replaced with a git SHA1 when you do a git archive |
| 17 | +__revision__ = '$Format:%H$' |
| 18 | + |
| 19 | +import qgis |
| 20 | +import os |
| 21 | +import sys |
| 22 | + |
| 23 | +from PyQt4.QtCore import Qt, QPointF, QThreadPool |
| 24 | +from PyQt4.QtGui import QFont |
| 25 | + |
| 26 | +from qgis.core import QgsPalLayerSettings |
| 27 | + |
| 28 | +from utilities import ( |
| 29 | + svgSymbolsPath, |
| 30 | + getTempfilePath, |
| 31 | + renderMapToImage, |
| 32 | + mapSettingsString |
| 33 | +) |
| 34 | + |
| 35 | +from test_qgspallabeling_base import TestQgsPalLabeling, runSuite |
| 36 | + |
| 37 | + |
| 38 | +# noinspection PyPep8Naming |
| 39 | +class TestPlacementBase(TestQgsPalLabeling): |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def setUpClass(cls): |
| 43 | + if not cls._BaseSetup: |
| 44 | + TestQgsPalLabeling.setUpClass() |
| 45 | + cls._Pal.setDrawLabelRectOnly(True) |
| 46 | + cls._Pal.saveEngineSettings() |
| 47 | + |
| 48 | + @classmethod |
| 49 | + def tearDownClass(cls): |
| 50 | + TestQgsPalLabeling.tearDownClass() |
| 51 | + #avoid crash on finish, probably related to https://bugreports.qt.io/browse/QTBUG-35760 |
| 52 | + QThreadPool.globalInstance().waitForDone() |
| 53 | + |
| 54 | + def setUp(self): |
| 55 | + """Run before each test.""" |
| 56 | + super(TestPlacementBase, self).setUp() |
| 57 | + self.configTest('pal_placement', 'sp') |
| 58 | + self._TestImage = '' |
| 59 | + # ensure per test map settings stay encapsulated |
| 60 | + self._TestMapSettings = self.cloneMapSettings(self._MapSettings) |
| 61 | + self._Mismatch = 0 |
| 62 | + self._ColorTol = 0 |
| 63 | + self._Mismatches.clear() |
| 64 | + self._ColorTols.clear() |
| 65 | + |
| 66 | + def checkTest(self, **kwargs): |
| 67 | + self.lyr.writeToLayer(self.layer) |
| 68 | + |
| 69 | + ms = self._MapSettings # class settings |
| 70 | + settings_type = 'Class' |
| 71 | + if self._TestMapSettings is not None: |
| 72 | + ms = self._TestMapSettings # per test settings |
| 73 | + settings_type = 'Test' |
| 74 | + if 'PAL_VERBOSE' in os.environ: |
| 75 | + qDebug('MapSettings type: {0}'.format(settings_type)) |
| 76 | + qDebug(mapSettingsString(ms)) |
| 77 | + |
| 78 | + img = renderMapToImage(ms, parallel=False) |
| 79 | + self._TestImage = getTempfilePath('png') |
| 80 | + if not img.save(self._TestImage, 'png'): |
| 81 | + os.unlink(self._TestImage) |
| 82 | + raise OSError('Failed to save output from map render job') |
| 83 | + self.saveControlImage(self._TestImage) |
| 84 | + |
| 85 | + mismatch = 0 |
| 86 | + if 'PAL_NO_MISMATCH' not in os.environ: |
| 87 | + # some mismatch expected |
| 88 | + mismatch = self._Mismatch if self._Mismatch else 0 |
| 89 | + if self._TestGroup in self._Mismatches: |
| 90 | + mismatch = self._Mismatches[self._TestGroup] |
| 91 | + colortol = 0 |
| 92 | + if 'PAL_NO_COLORTOL' not in os.environ: |
| 93 | + colortol = self._ColorTol if self._ColorTol else 0 |
| 94 | + if self._TestGroup in self._ColorTols: |
| 95 | + colortol = self._ColorTols[self._TestGroup] |
| 96 | + self.assertTrue(*self.renderCheck(mismatch=mismatch, |
| 97 | + colortol=colortol, |
| 98 | + imgpath=self._TestImage)) |
| 99 | + |
| 100 | +# noinspection PyPep8Naming |
| 101 | + |
| 102 | + |
| 103 | +class TestPointPlacement(TestPlacementBase): |
| 104 | + |
| 105 | + @classmethod |
| 106 | + def setUpClass(cls): |
| 107 | + TestPlacementBase.setUpClass() |
| 108 | + cls.layer = None |
| 109 | + |
| 110 | + def test_point_placement_around(self): |
| 111 | + # Default point label placement |
| 112 | + self.layer = TestQgsPalLabeling.loadFeatureLayer('point') |
| 113 | + self._TestMapSettings = self.cloneMapSettings(self._MapSettings) |
| 114 | + self.checkTest() |
| 115 | + self.removeMapLayer(self.layer) |
| 116 | + self.layer = None |
| 117 | + |
| 118 | + def test_point_placement_around_obstacle(self): |
| 119 | + # Default point label placement with obstacle |
| 120 | + self.layer = TestQgsPalLabeling.loadFeatureLayer('point2') |
| 121 | + self._TestMapSettings = self.cloneMapSettings(self._MapSettings) |
| 122 | + self.checkTest() |
| 123 | + self.removeMapLayer(self.layer) |
| 124 | + self.layer = None |
| 125 | + |
| 126 | +if __name__ == '__main__': |
| 127 | + # NOTE: unless PAL_SUITE env var is set all test class methods will be run |
| 128 | + # SEE: test_qgspallabeling_tests.suiteTests() to define suite |
| 129 | + suite = ('TestPointPlacement') |
| 130 | + res = runSuite(sys.modules[__name__], suite) |
| 131 | + sys.exit(not res.wasSuccessful()) |
0 commit comments