|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsEditFormConfig. |
| 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__ = 'Nyall Dawson' |
| 10 | +__date__ = '11/04/2017' |
| 11 | +__copyright__ = 'Copyright 2018, 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 | + |
| 17 | +from qgis.core import (QgsVectorLayer, |
| 18 | + QgsReadWriteContext) |
| 19 | +from qgis.gui import QgsGui |
| 20 | + |
| 21 | +from qgis.testing import start_app, unittest |
| 22 | +from qgis.PyQt.QtXml import QDomDocument, QDomElement |
| 23 | + |
| 24 | +start_app() |
| 25 | + |
| 26 | + |
| 27 | +class TestQgsEditFormConfig(unittest.TestCase): |
| 28 | + |
| 29 | + @classmethod |
| 30 | + def setUpClass(cls): |
| 31 | + QgsGui.editorWidgetRegistry().initEditors() |
| 32 | + |
| 33 | + def createLayer(self): |
| 34 | + self.layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer", |
| 35 | + "addfeat", "memory") |
| 36 | + return self.layer |
| 37 | + |
| 38 | + def testReadWriteXml(self): |
| 39 | + layer = self.createLayer() |
| 40 | + config = layer.editFormConfig() |
| 41 | + |
| 42 | + config.setReadOnly(0, True) |
| 43 | + config.setReadOnly(1, False) |
| 44 | + config.setLabelOnTop(0, False) |
| 45 | + config.setLabelOnTop(1, True) |
| 46 | + |
| 47 | + doc = QDomDocument("testdoc") |
| 48 | + elem = doc.createElement('edit') |
| 49 | + config.writeXml(elem, QgsReadWriteContext()) |
| 50 | + |
| 51 | + layer2 = self.createLayer() |
| 52 | + config2 = layer2.editFormConfig() |
| 53 | + config2.readXml(elem, QgsReadWriteContext()) |
| 54 | + |
| 55 | + self.assertTrue(config2.readOnly(0)) |
| 56 | + self.assertFalse(config2.readOnly(1)) |
| 57 | + self.assertFalse(config2.labelOnTop(0)) |
| 58 | + self.assertTrue(config2.labelOnTop(1)) |
| 59 | + |
| 60 | + def testReadOnly(self): |
| 61 | + layer = self.createLayer() |
| 62 | + config = layer.editFormConfig() |
| 63 | + |
| 64 | + # safety checks |
| 65 | + config.setReadOnly(-1, True) |
| 66 | + config.setReadOnly(100, True) |
| 67 | + |
| 68 | + # real checks |
| 69 | + config.setReadOnly(0, True) |
| 70 | + config.setReadOnly(1, True) |
| 71 | + self.assertTrue(config.readOnly(0)) |
| 72 | + self.assertTrue(config.readOnly(1)) |
| 73 | + |
| 74 | + config.setReadOnly(0, False) |
| 75 | + config.setReadOnly(1, False) |
| 76 | + self.assertFalse(config.readOnly(0)) |
| 77 | + self.assertFalse(config.readOnly(1)) |
| 78 | + |
| 79 | + def testLabelOnTop(self): |
| 80 | + layer = self.createLayer() |
| 81 | + config = layer.editFormConfig() |
| 82 | + |
| 83 | + # safety checks |
| 84 | + config.setLabelOnTop(-1, True) |
| 85 | + config.setLabelOnTop(100, True) |
| 86 | + |
| 87 | + # real checks |
| 88 | + config.setLabelOnTop(0, True) |
| 89 | + config.setLabelOnTop(1, True) |
| 90 | + self.assertTrue(config.labelOnTop(0)) |
| 91 | + self.assertTrue(config.labelOnTop(1)) |
| 92 | + |
| 93 | + config.setLabelOnTop(0, False) |
| 94 | + config.setLabelOnTop(1, False) |
| 95 | + self.assertFalse(config.labelOnTop(0)) |
| 96 | + self.assertFalse(config.labelOnTop(1)) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == '__main__': |
| 100 | + unittest.main() |
0 commit comments