|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for edit widgets. |
| 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__ = '20/05/2015' |
| 11 | +__copyright__ = 'Copyright 2015, 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 |
| 16 | +import os |
| 17 | + |
| 18 | +from qgis.core import QgsFeature, QgsGeometry, QgsPoint, QgsVectorLayer, NULL |
| 19 | + |
| 20 | +from qgis.gui import QgsEditorWidgetRegistry |
| 21 | + |
| 22 | +from PyQt4 import QtCore |
| 23 | + |
| 24 | +from utilities import (unitTestDataPath, |
| 25 | + getQgisTestApp, |
| 26 | + TestCase, |
| 27 | + unittest |
| 28 | + ) |
| 29 | +QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() |
| 30 | + |
| 31 | + |
| 32 | +class TestQgsTextEditWidget(TestCase): |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def setUpClass(cls): |
| 36 | + QgsEditorWidgetRegistry.initEditors() |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + def createLayerWithOnePoint(self): |
| 41 | + self.layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer", |
| 42 | + "addfeat", "memory") |
| 43 | + pr = self.layer.dataProvider() |
| 44 | + f = QgsFeature() |
| 45 | + f.setAttributes(["test", 123]) |
| 46 | + f.setGeometry(QgsGeometry.fromPoint(QgsPoint(100,200))) |
| 47 | + assert pr.addFeatures([f]) |
| 48 | + assert self.layer.pendingFeatureCount() == 1 |
| 49 | + return self.layer |
| 50 | + |
| 51 | + def doAttributeTest(self,idx,expected): |
| 52 | + reg = QgsEditorWidgetRegistry.instance() |
| 53 | + configWdg = reg.createConfigWidget('TextEdit', self.layer, idx, None) |
| 54 | + config = configWdg.config() |
| 55 | + editwidget = reg.create('TextEdit', self.layer, idx, config, None, None ) |
| 56 | + |
| 57 | + editwidget.setValue('value') |
| 58 | + assert editwidget.value() == expected[0] |
| 59 | + |
| 60 | + editwidget.setValue(123) |
| 61 | + assert editwidget.value() == expected[1] |
| 62 | + |
| 63 | + editwidget.setValue(None) |
| 64 | + assert editwidget.value() == expected[2] |
| 65 | + |
| 66 | + editwidget.setValue(NULL) |
| 67 | + assert editwidget.value() == expected[3] |
| 68 | + |
| 69 | + def test_SetValue(self): |
| 70 | + self.createLayerWithOnePoint() |
| 71 | + |
| 72 | + self.doAttributeTest(0, ['value','123',NULL, NULL]) |
| 73 | + self.doAttributeTest(1, [NULL,123,NULL, NULL]) |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + unittest.main() |
0 commit comments