|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsHighlight. |
| 3 | +
|
| 4 | +.. note:: This program is free software; you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU General Public License as published by |
| 6 | +the Free Software Foundation; either version 2 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +""" |
| 9 | +__author__ = 'Matthias Kuhn' |
| 10 | +__date__ = '8.11.2017' |
| 11 | +__copyright__ = 'Copyright 2017, The QGIS Project' |
| 12 | +# This will get replaced with a git SHA1 when you do a git archive |
| 13 | +__revision__ = '$Format:%H$' |
| 14 | + |
| 15 | +import qgis # NOQA |
| 16 | +import os |
| 17 | +import tempfile |
| 18 | +import shutil |
| 19 | + |
| 20 | +from qgis.PyQt.QtCore import ( |
| 21 | + QSize, |
| 22 | + Qt |
| 23 | +) |
| 24 | +from qgis.PyQt.QtGui import ( |
| 25 | + QColor, |
| 26 | + QImage, |
| 27 | + QPainter, |
| 28 | + QResizeEvent |
| 29 | +) |
| 30 | +from qgis.core import ( |
| 31 | + QgsVectorLayer, |
| 32 | + QgsProject, |
| 33 | + QgsRectangle, |
| 34 | + QgsRenderChecker |
| 35 | +) |
| 36 | +from qgis.gui import QgsHighlight |
| 37 | +from qgis.testing import start_app, unittest |
| 38 | +from qgis.testing.mocked import get_iface |
| 39 | +from utilities import unitTestDataPath |
| 40 | + |
| 41 | +start_app() |
| 42 | +TEST_DATA_DIR = unitTestDataPath() |
| 43 | + |
| 44 | + |
| 45 | +class TestQgsHighlight(unittest.TestCase): |
| 46 | + |
| 47 | + def setUp(self): |
| 48 | + self.iface = get_iface() |
| 49 | + |
| 50 | + self.iface.mapCanvas().viewport().resize(400, 400) |
| 51 | + # For some reason the resizeEvent is not delivered, fake it |
| 52 | + self.iface.mapCanvas().resizeEvent(QResizeEvent(QSize(400, 400), self.iface.mapCanvas().size())) |
| 53 | + |
| 54 | + def tearDown(self): |
| 55 | + QgsProject.instance().removeAllMapLayers() |
| 56 | + |
| 57 | + def runTestForLayer(self, layer, testname): |
| 58 | + tempdir = tempfile.mkdtemp() |
| 59 | + |
| 60 | + layer = QgsVectorLayer(layer, 'Layer', 'ogr') |
| 61 | + QgsProject.instance().addMapLayer(layer) |
| 62 | + self.iface.mapCanvas().setExtent(layer.extent()) |
| 63 | + |
| 64 | + geom = next(layer.getFeatures()).geometry() |
| 65 | + |
| 66 | + highlight = QgsHighlight(self.iface.mapCanvas(), geom, layer) |
| 67 | + color = QColor(Qt.red) |
| 68 | + highlight.setColor(color) |
| 69 | + highlight.setWidth(2) |
| 70 | + color.setAlpha(50) |
| 71 | + highlight.setFillColor(color) |
| 72 | + highlight.show() |
| 73 | + |
| 74 | + image = QImage(QSize(400, 400), QImage.Format_ARGB32) |
| 75 | + image.fill(Qt.white) |
| 76 | + painter = QPainter() |
| 77 | + painter.begin(image) |
| 78 | + self.iface.mapCanvas().render(painter) |
| 79 | + painter.end() |
| 80 | + control_image = os.path.join(tempdir, 'highlight_{}.png'.format(testname)) |
| 81 | + image.save(control_image) |
| 82 | + checker = QgsRenderChecker() |
| 83 | + checker.setControlPathPrefix("highlight") |
| 84 | + checker.setControlName("expected_highlight_{}".format(testname)) |
| 85 | + checker.setRenderedImage(control_image) |
| 86 | + self.assertTrue(checker.compareImages("highlight_{}".format(testname))) |
| 87 | + shutil.rmtree(tempdir) |
| 88 | + |
| 89 | + def testLine(self): |
| 90 | + lines_shp = os.path.join(TEST_DATA_DIR, 'lines.shp') |
| 91 | + self.runTestForLayer(lines_shp, 'lines') |
| 92 | + |
| 93 | + def testPolygon(self): |
| 94 | + polys_shp = os.path.join(TEST_DATA_DIR, 'polys.shp') |
| 95 | + self.runTestForLayer(polys_shp, 'polygons') |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == '__main__': |
| 99 | + unittest.main() |
0 commit comments