Skip to content

Commit

Permalink
Add some unit tests for label settings widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 4, 2019
1 parent ed93ebd commit cf43104
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -90,6 +90,7 @@ ADD_PYTHON_TEST(PyQgsImageCache test_qgsimagecache.py)
ADD_PYTHON_TEST(PyQgsImageSourceLineEdit test_qgsimagesourcelineedit.py)
ADD_PYTHON_TEST(PyQgsInterval test_qgsinterval.py)
ADD_PYTHON_TEST(PyQgsJsonUtils test_qgsjsonutils.py)
ADD_PYTHON_TEST(PyQgsLabelSettingsWidget test_qgslabelsettingswidget.py)
ADD_PYTHON_TEST(PyQgsLayerMetadata test_qgslayermetadata.py)
ADD_PYTHON_TEST(PyQgsLayerTreeMapCanvasBridge test_qgslayertreemapcanvasbridge.py)
ADD_PYTHON_TEST(PyQgsLayerTree test_qgslayertree.py)
Expand Down
64 changes: 64 additions & 0 deletions tests/src/python/test_qgslabelsettingswidget.py
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsLabelSettingsWidget and subclasses.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Nyall Dawson'
__date__ = '04/02/2019'
__copyright__ = 'Copyright 2019, The QGIS Project'

import qgis # NOQA

from qgis.core import (QgsPropertyCollection,
QgsPalLayerSettings,
QgsLabelObstacleSettings,
QgsProperty)
from qgis.gui import (QgsLabelSettingsWidgetBase,
QgsLabelObstacleSettingsWidget)

from qgis.testing import start_app, unittest
from qgis.PyQt.QtTest import QSignalSpy

start_app()


class TestQgsLabelSettingsWidget(unittest.TestCase):

def testBase(self):
""" test base class """
w = QgsLabelSettingsWidgetBase()
spy = QSignalSpy(w.changed)

props = QgsPropertyCollection()
props.setProperty(QgsPalLayerSettings.ObstacleFactor, QgsProperty.fromValue(5))
props.setProperty(QgsPalLayerSettings.IsObstacle, QgsProperty.fromValue(True))
w.setDataDefinedProperties(props)
self.assertEqual(len(spy), 0)
self.assertEqual(w.dataDefinedProperties().property(QgsPalLayerSettings.ObstacleFactor).asExpression(), '5')
self.assertEqual(w.dataDefinedProperties().property(QgsPalLayerSettings.IsObstacle).asExpression(), 'TRUE')

def testObstacles(self):
w = QgsLabelObstacleSettingsWidget()
settings = QgsLabelObstacleSettings()
settings.setFactor(0.4)
settings.setType(QgsLabelObstacleSettings.PolygonBoundary)
spy = QSignalSpy(w.changed)
w.setObstacleSettings(settings)
self.assertEqual(len(spy), 0)
settings = w.settings()
self.assertEqual(settings.factor(), 0.4)
self.assertEqual(settings.type(), QgsLabelObstacleSettings.PolygonBoundary)
settings.setFactor(1.2)
settings.setType(QgsLabelObstacleSettings.PolygonInterior)
w.setObstacleSettings(settings)
self.assertEqual(len(spy), 0)
settings = w.settings()
self.assertEqual(settings.factor(), 1.2)
self.assertEqual(settings.type(), QgsLabelObstacleSettings.PolygonInterior)


if __name__ == '__main__':
unittest.main()

0 comments on commit cf43104

Please sign in to comment.