/
testqgseditorwidgetregistry.cpp
157 lines (133 loc) · 6.11 KB
/
testqgseditorwidgetregistry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/***************************************************************************
testqgseditorwidgetregistry.cpp
---------------------
begin : July 2016
copyright : (C) 2016 by Patrick Valsecchi
email : patrick.valsecchi at camptocamp.com
***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "qgstest.h"
#include "qgseditorwidgetregistry.h"
#include "qgseditorwidgetautoconf.h"
#include "qgsproject.h"
#include "qgsrelationmanager.h"
#include "qgsgui.h"
class TestQgsEditorWidgetRegistry: public QObject
{
Q_OBJECT
class DummyPlugin: public QgsEditorWidgetAutoConfPlugin
{
public:
QgsEditorWidgetSetup editorWidgetSetup( const QgsVectorLayer *vl, const QString &fieldName, int &score ) const override
{
Q_UNUSED( vl )
if ( fieldName == QLatin1String( "special" ) )
{
score = 100;
return QgsEditorWidgetSetup( QStringLiteral( "Special" ), QVariantMap() );
}
score = 0;
return QgsEditorWidgetSetup();
}
};
private slots:
void initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
QgsGui::editorWidgetRegistry()->initEditors();
QgsGui::editorWidgetRegistry()->registerAutoConfPlugin( new DummyPlugin() );
}
void cleanupTestCase()
{
QgsApplication::exitQgis();
}
void stringType()
{
checkSimple( QStringLiteral( "string" ), QStringLiteral( "TextEdit" ) );
}
void datetimeType()
{
checkSimple( QStringLiteral( "datetime" ), QStringLiteral( "DateTime" ) );
}
void integerType()
{
checkSimple( QStringLiteral( "integer" ), QStringLiteral( "Range" ) );
}
void longLongType()
{
checkSimple( QStringLiteral( "int8" ), QStringLiteral( "TextEdit" ) ); // no current widget supports 64 bit integers => default to TextEdit
}
void doubleType()
{
checkSimple( QStringLiteral( "double" ), QStringLiteral( "TextEdit" ) );
}
void arrayType()
{
checkSimple( QStringLiteral( "double[]" ), QStringLiteral( "List" ) );
checkSimple( QStringLiteral( "int[]" ), QStringLiteral( "List" ) );
checkSimple( QStringLiteral( "string[]" ), QStringLiteral( "List" ) );
}
void configuredType()
{
QgsVectorLayer vl( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=col1:string" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) );
QVariantMap config;
config[QStringLiteral( "a" )] = QVariant( 12 );
config[QStringLiteral( "b" )] = QVariant( "bar" );
vl.setEditorWidgetSetup( 1, QgsEditorWidgetSetup( QStringLiteral( "FooEdit" ), config ) );
const QgsEditorWidgetSetup setup = QgsGui::editorWidgetRegistry()->findBest( &vl, QStringLiteral( "col1" ) );
QCOMPARE( setup.type(), QString( "FooEdit" ) );
QCOMPARE( setup.config(), config );
}
void wrongFieldName()
{
const QgsVectorLayer vl( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=col1:string" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) );
const QgsEditorWidgetSetup setup = QgsGui::editorWidgetRegistry()->findBest( &vl, QStringLiteral( "col2" ) );
// an unknown fields leads to a default setup with a TextEdit
QCOMPARE( setup.type(), QString( "TextEdit" ) );
QCOMPARE( setup.config().count(), 0 );
}
void referencedLayers()
{
//build two layers
QgsVectorLayer vl1( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=name:string&field=fk:int" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
QgsVectorLayer vl2( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=col1:string" ), QStringLiteral( "vl2" ), QStringLiteral( "memory" ) );
QgsProject::instance()->addMapLayer( &vl1, false, false );
QgsProject::instance()->addMapLayer( &vl2, false, false );
//create a relation between them
QgsRelation relation;
relation.setId( QStringLiteral( "vl1->vl2" ) );
relation.setName( QStringLiteral( "vl1->vl2" ) );
relation.setReferencingLayer( vl1.id() );
relation.setReferencedLayer( vl2.id() );
relation.addFieldPair( QStringLiteral( "fk" ), QStringLiteral( "pk" ) );
QVERIFY( relation.isValid() );
QgsProject::instance()->relationManager()->addRelation( relation );
//check the guessed editor widget type for vl1.fk is RelationReference
const QgsEditorWidgetSetup setup = QgsGui::editorWidgetRegistry()->findBest( &vl1, QStringLiteral( "fk" ) );
QCOMPARE( setup.type(), QString( "RelationReference" ) );
QCOMPARE( setup.config(), QVariantMap() );
}
void typeFromPlugin()
{
const QgsVectorLayer vl( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=special:string" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) );
const QgsEditorWidgetSetup setup = QgsGui::editorWidgetRegistry()->findBest( &vl, QStringLiteral( "special" ) );
QCOMPARE( setup.type(), QString( "Special" ) );
}
private:
static void checkSimple( const QString &dataType, const QString &widgetType )
{
const QgsVectorLayer vl( "LineString?crs=epsg:3111&field=pk:int&field=col1:" + dataType, QStringLiteral( "vl" ), QStringLiteral( "memory" ) );
const QgsEditorWidgetSetup setup = QgsGui::editorWidgetRegistry()->findBest( &vl, QStringLiteral( "col1" ) );
QCOMPARE( setup.type(), widgetType );
QCOMPARE( setup.config().count(), 0 );
}
};
QGSTEST_MAIN( TestQgsEditorWidgetRegistry )
#include "testqgseditorwidgetregistry.moc"