Skip to content

Commit a58a2f2

Browse files
committedMay 20, 2015
Add unit test for edit widget "TextEdit"
QGIS Dev conference Denmark
1 parent 1e46196 commit a58a2f2

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
 

‎python/gui/editorwidgets/core/qgseditorwidgetregistry.sip

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ class QgsEditorWidgetRegistry : QObject
3131
*/
3232
static QgsEditorWidgetRegistry* instance();
3333

34+
/**
35+
* Registers all the default widgets.
36+
* Only call this once on startup of an application.
37+
*
38+
* @param mapCanvas Specify a map canvas with which the widgets (relation reference) work
39+
* @param messageBar Specify a message bar on which messages by widgets will be shown while working with the map canvas
40+
*
41+
* @note Added in QGIS 2.8
42+
* @note Not required for plugins, the QGIS application does that already
43+
*/
44+
static void initEditors( QgsMapCanvas* mapCanvas = 0, QgsMessageBar* messageBar = 0 );
45+
3446
/**
3547
* Create an attribute editor widget wrapper of a given type for a given field.
3648
* The editor may be NULL if you want the widget wrapper to create a default widget.

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ ADD_PYTHON_TEST(PyQgsAppStartup test_qgsappstartup.py)
4242
ADD_PYTHON_TEST(PyQgsDistanceArea test_qgsdistancearea.py)
4343
ADD_PYTHON_TEST(PyQgsGraduatedSymbolRendererV2 test_qgsgraduatedsymbolrendererv2.py)
4444
ADD_PYTHON_TEST(PyQgsNetworkContentFetcher test_qgsnetworkcontentfetcher.py)
45+
ADD_PYTHON_TEST(PyQgsEditWidgets test_qgseditwidgets.py)
4546
IF (WITH_APIDOC)
4647
ADD_PYTHON_TEST(PyQgsDocCoverage test_qgsdoccoverage.py)
4748
ENDIF (WITH_APIDOC)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)
Please sign in to comment.