Skip to content

Commit

Permalink
Add unit test for edit widget "TextEdit"
Browse files Browse the repository at this point in the history
QGIS Dev conference Denmark
  • Loading branch information
m-kuhn committed May 20, 2015
1 parent 1e46196 commit a58a2f2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
12 changes: 12 additions & 0 deletions python/gui/editorwidgets/core/qgseditorwidgetregistry.sip
Expand Up @@ -31,6 +31,18 @@ class QgsEditorWidgetRegistry : QObject
*/
static QgsEditorWidgetRegistry* instance();

/**
* Registers all the default widgets.
* Only call this once on startup of an application.
*
* @param mapCanvas Specify a map canvas with which the widgets (relation reference) work
* @param messageBar Specify a message bar on which messages by widgets will be shown while working with the map canvas
*
* @note Added in QGIS 2.8
* @note Not required for plugins, the QGIS application does that already
*/
static void initEditors( QgsMapCanvas* mapCanvas = 0, QgsMessageBar* messageBar = 0 );

/**
* Create an attribute editor widget wrapper of a given type for a given field.
* The editor may be NULL if you want the widget wrapper to create a default widget.
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -42,6 +42,7 @@ ADD_PYTHON_TEST(PyQgsAppStartup test_qgsappstartup.py)
ADD_PYTHON_TEST(PyQgsDistanceArea test_qgsdistancearea.py)
ADD_PYTHON_TEST(PyQgsGraduatedSymbolRendererV2 test_qgsgraduatedsymbolrendererv2.py)
ADD_PYTHON_TEST(PyQgsNetworkContentFetcher test_qgsnetworkcontentfetcher.py)
ADD_PYTHON_TEST(PyQgsEditWidgets test_qgseditwidgets.py)
IF (WITH_APIDOC)
ADD_PYTHON_TEST(PyQgsDocCoverage test_qgsdoccoverage.py)
ENDIF (WITH_APIDOC)
78 changes: 78 additions & 0 deletions tests/src/python/test_qgseditwidgets.py
@@ -0,0 +1,78 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for edit widgets.
.. 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__ = 'Matthias Kuhn'
__date__ = '20/05/2015'
__copyright__ = 'Copyright 2015, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis
import os

from qgis.core import QgsFeature, QgsGeometry, QgsPoint, QgsVectorLayer, NULL

from qgis.gui import QgsEditorWidgetRegistry

from PyQt4 import QtCore

from utilities import (unitTestDataPath,
getQgisTestApp,
TestCase,
unittest
)
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()


class TestQgsTextEditWidget(TestCase):

@classmethod
def setUpClass(cls):
QgsEditorWidgetRegistry.initEditors()



def createLayerWithOnePoint(self):
self.layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")
pr = self.layer.dataProvider()
f = QgsFeature()
f.setAttributes(["test", 123])
f.setGeometry(QgsGeometry.fromPoint(QgsPoint(100,200)))
assert pr.addFeatures([f])
assert self.layer.pendingFeatureCount() == 1
return self.layer

def doAttributeTest(self,idx,expected):
reg = QgsEditorWidgetRegistry.instance()
configWdg = reg.createConfigWidget('TextEdit', self.layer, idx, None)
config = configWdg.config()
editwidget = reg.create('TextEdit', self.layer, idx, config, None, None )

editwidget.setValue('value')
assert editwidget.value() == expected[0]

editwidget.setValue(123)
assert editwidget.value() == expected[1]

editwidget.setValue(None)
assert editwidget.value() == expected[2]

editwidget.setValue(NULL)
assert editwidget.value() == expected[3]

def test_SetValue(self):
self.createLayerWithOnePoint()

self.doAttributeTest(0, ['value','123',NULL, NULL])
self.doAttributeTest(1, [NULL,123,NULL, NULL])



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

0 comments on commit a58a2f2

Please sign in to comment.