/
testqgsrangewidgetwrapper.cpp
279 lines (244 loc) · 11.1 KB
/
testqgsrangewidgetwrapper.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/***************************************************************************
testqgsrangewidget.cpp
---------------------------
begin : Jan 2018
copyright : (C) 2018 by Alessandro Pasotti
email : elpaso at itopen dot it
***************************************************************************/
/***************************************************************************
* *
* 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 "qgsrangewidgetwrapper.h"
#include "qgsrangeconfigdlg.h"
#include "qgsdoublespinbox.h"
#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgsvectorlayer.h"
#include "qgsdataprovider.h"
#include <QObject>
#include <QtTest/QSignalSpy>
#include <memory>
/**
* @ingroup UnitTests
* This is a unit test for the range widget
*
* \see QgsRangeWidgetWrapper
*/
class TestQgsRangeWidgetWrapper : public QObject
{
Q_OBJECT
private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.
void test_setDoubleRange();
void test_setDoubleSmallerRange();
void test_setDoubleLimits();
private:
std::unique_ptr<QgsRangeWidgetWrapper> widget; // For field 1
std::unique_ptr<QgsRangeWidgetWrapper> widget2; // For field 2
std::unique_ptr<QgsVectorLayer> vl;
};
void TestQgsRangeWidgetWrapper::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
}
void TestQgsRangeWidgetWrapper::cleanupTestCase()
{
QgsApplication::exitQgis();
}
void TestQgsRangeWidgetWrapper::init()
{
vl = qgis::make_unique<QgsVectorLayer>( QStringLiteral( "Point?crs=epsg:4326" ),
QStringLiteral( "myvl" ),
QLatin1Literal( "memory" ) );
// add fields
QList<QgsField> fields;
fields.append( QgsField( "id", QVariant::Int ) );
// 9 precision
QgsField dfield( "number", QVariant::Double );
dfield.setPrecision( 9 );
fields.append( dfield );
// default precision
QgsField dfield2( "number_def", QVariant::Double );
fields.append( dfield2 );
vl->dataProvider()->addAttributes( fields );
vl->updateFields();
QVERIFY( vl.get() );
QVERIFY( vl->isValid() );
// Add feature 1:1:123.123456789:123.123456789:POINT( 1 1 )
QgsFeature feat1( vl->fields(), 1 );
feat1.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "POINT( 1 1 )" ) ) );
feat1.setAttribute( QStringLiteral( "id" ), 1 );
feat1.setAttribute( QStringLiteral( "number" ), 123.123456789 );
feat1.setAttribute( QStringLiteral( "number_def" ), 123.123456789 );
vl->dataProvider()->addFeature( feat1 );
// Add feature 2:2:NULL:NULL:POINT( 2 2 )
QgsFeature feat2( vl->fields(), 2 );
feat2.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "POINT( 2 2 )" ) ) );
feat2.setAttribute( QStringLiteral( "id" ), 2 );
vl->dataProvider()->addFeature( feat2 );
// Add feature 3:3:-123.123456789:-123.123456789:POINT( 3 3 )
QgsFeature feat3( vl->fields(), 3 );
feat3.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "POINT( 3 3 )" ) ) );
feat3.setAttribute( QStringLiteral( "number" ), -123.123456789 );
feat3.setAttribute( QStringLiteral( "number_def" ), -123.123456789 );
feat3.setAttribute( QStringLiteral( "id" ), 3 );
vl->dataProvider()->addFeature( feat3 );
// Verify feat 1 was added
QCOMPARE( vl->featureCount( ), ( long )3 );
QgsFeature _feat1( vl->getFeature( 1 ) );
QCOMPARE( _feat1, feat1 );
widget = qgis::make_unique<QgsRangeWidgetWrapper>( vl.get(), 1, nullptr, nullptr );
widget2 = qgis::make_unique<QgsRangeWidgetWrapper>( vl.get(), 2, nullptr, nullptr );
QVERIFY( widget.get() );
}
void TestQgsRangeWidgetWrapper::cleanup()
{
}
void TestQgsRangeWidgetWrapper::test_setDoubleRange()
{
// Test setting scale range with doubles and NULL values, default range
// See https://issues.qgis.org/issues/17878
// QGIS 3 Vector Layer Fields Garbled when Clicking the Toggle Editing Icon
QgsDoubleSpinBox *editor = qobject_cast<QgsDoubleSpinBox *>( widget->createWidget( nullptr ) );
QVERIFY( editor );
widget->initWidget( editor );
QgsDoubleSpinBox *editor2 = qobject_cast<QgsDoubleSpinBox *>( widget2->createWidget( nullptr ) );
QVERIFY( editor2 );
widget2->initWidget( editor2 );
QgsFeature feat( vl->getFeature( 1 ) );
QVERIFY( feat.isValid() );
QCOMPARE( feat.attribute( 1 ).toDouble(), 123.123456789 );
widget->setFeature( vl->getFeature( 1 ) );
widget2->setFeature( vl->getFeature( 1 ) );
QCOMPARE( vl->fields().at( 1 ).precision(), 9 );
// Default is 0 !!! for double, really ?
// btw if it is 0 the decimals property in the spinbox is left to the default value of 2
QCOMPARE( vl->fields().at( 2 ).precision(), 0 );
QCOMPARE( editor->decimals(), vl->fields().at( 1 ).precision() );
QCOMPARE( editor->decimals(), 9 );
// This fails: QCOMPARE( editor2->decimals(), vl->fields().at( 2 ).precision() );
QCOMPARE( editor2->decimals(), 2 ); // Default spinbox decimals is 2
QCOMPARE( editor->valueFromText( feat.attribute( 1 ).toString() ), 123.123456789 );
QCOMPARE( feat.attribute( 1 ).toString(), QStringLiteral( "123.123456789" ) );
QCOMPARE( editor2->valueFromText( feat.attribute( 1 ).toString() ), 123.123456789 );
QCOMPARE( editor->value( ), 123.123456789 );
QCOMPARE( editor2->value( ), 123.12 );
QCOMPARE( editor->minimum( ), std::numeric_limits<double>::lowest() );
QCOMPARE( editor2->minimum( ), std::numeric_limits<double>::lowest() );
QCOMPARE( editor->maximum( ), std::numeric_limits<double>::max() );
QCOMPARE( editor2->maximum( ), std::numeric_limits<double>::max() );
widget->setFeature( vl->getFeature( 2 ) );
widget2->setFeature( vl->getFeature( 2 ) );
QCOMPARE( editor->value( ), editor->minimum() );
QCOMPARE( editor2->value( ), editor->minimum() );
widget->setFeature( vl->getFeature( 3 ) );
widget2->setFeature( vl->getFeature( 3 ) );
QCOMPARE( editor->value( ), -123.123456789 );
QCOMPARE( editor2->value( ), -123.12 );
}
void TestQgsRangeWidgetWrapper::test_setDoubleSmallerRange()
{
// Same test but we set a smaller validity range for the widget
QVariantMap cfg;
cfg.insert( QStringLiteral( "Min" ), -100.0 );
cfg.insert( QStringLiteral( "Max" ), 100.0 );
cfg.insert( QStringLiteral( "Step" ), 1 );
widget->setConfig( cfg );
QgsDoubleSpinBox *editor = qobject_cast<QgsDoubleSpinBox *>( widget->createWidget( nullptr ) );
QVERIFY( editor );
widget->initWidget( editor );
widget2->setConfig( cfg );
QgsDoubleSpinBox *editor2 = qobject_cast<QgsDoubleSpinBox *>( widget2->createWidget( nullptr ) );
QVERIFY( editor2 );
widget2->initWidget( editor2 );
QgsFeature feat( vl->getFeature( 1 ) );
QVERIFY( feat.isValid() );
QCOMPARE( feat.attribute( 1 ).toDouble(), 123.123456789 );
widget->setFeature( vl->getFeature( 1 ) );
widget2->setFeature( vl->getFeature( 1 ) );
QCOMPARE( vl->fields().at( 1 ).precision(), 9 );
// Default is 0 !!! for double, really ?
// btw if it is 0 the decimals property in the spinbox is left to the default value of 2
QCOMPARE( vl->fields().at( 2 ).precision(), 0 );
QCOMPARE( editor->decimals(), vl->fields().at( 1 ).precision() );
// This fails: QCOMPARE( editor2->decimals(), vl->fields().at( 2 ).precision() );
QCOMPARE( editor2->decimals(), 2 ); // Default spinbox decimals is 2
// value was changed to the maximum (not NULL) accepted value
QCOMPARE( editor->value( ), 100.0 );
// value was changed to the maximum (not NULL) accepted value
QCOMPARE( editor2->value( ), 100.0 );
// minimum was lowered by the precision (10e-9)
QCOMPARE( editor->minimum( ), -100.000000001 );
// minimum was lowered by step (1)
QCOMPARE( editor2->minimum( ), ( double ) - 101 );
QCOMPARE( editor->maximum( ), ( double )100 );
QCOMPARE( editor2->maximum( ), ( double )100 );
// NULL, NULL
widget->setFeature( vl->getFeature( 2 ) );
widget2->setFeature( vl->getFeature( 2 ) );
QCOMPARE( editor->value( ), editor->minimum() );
QCOMPARE( editor2->value( ), editor2->minimum() );
// negative, negative
widget->setFeature( vl->getFeature( 3 ) );
widget2->setFeature( vl->getFeature( 3 ) );
// value was changed to the minimum
QCOMPARE( editor->value( ), editor->minimum() );
QCOMPARE( editor2->value( ), editor2->minimum() );
}
void TestQgsRangeWidgetWrapper::test_setDoubleLimits()
{
// Same test but check double numeric limits
QVariantMap cfg;
cfg.insert( QStringLiteral( "Min" ), std::numeric_limits<double>::lowest() );
cfg.insert( QStringLiteral( "Max" ), std::numeric_limits<double>::max() );
cfg.insert( QStringLiteral( "Step" ), 1 );
widget->setConfig( cfg );
QgsDoubleSpinBox *editor = qobject_cast<QgsDoubleSpinBox *>( widget->createWidget( nullptr ) );
QVERIFY( editor );
widget->initWidget( editor );
widget2->setConfig( cfg );
QgsDoubleSpinBox *editor2 = qobject_cast<QgsDoubleSpinBox *>( widget2->createWidget( nullptr ) );
QVERIFY( editor2 );
widget2->initWidget( editor2 );
QCOMPARE( editor->minimum( ), std::numeric_limits<double>::lowest() );
QCOMPARE( editor2->minimum( ), std::numeric_limits<double>::lowest() );
QCOMPARE( editor->maximum( ), std::numeric_limits<double>::max() );
QCOMPARE( editor2->maximum( ), std::numeric_limits<double>::max() );
QgsFeature feat( vl->getFeature( 1 ) );
QVERIFY( feat.isValid() );
QCOMPARE( feat.attribute( 1 ).toDouble(), 123.123456789 );
widget->setFeature( vl->getFeature( 1 ) );
widget2->setFeature( vl->getFeature( 1 ) );
QCOMPARE( vl->fields().at( 1 ).precision(), 9 );
// Default is 0 !!! for double, really ?
// btw if it is 0 the decimals property in the spinbox is left to the default value of 2
QCOMPARE( vl->fields().at( 2 ).precision(), 0 );
QCOMPARE( editor->decimals(), vl->fields().at( 1 ).precision() );
// This fails: QCOMPARE( editor2->decimals(), vl->fields().at( 2 ).precision() );
QCOMPARE( editor2->decimals(), 2 ); // Default spinbox decimals is 2
QCOMPARE( editor->value( ), 123.123456789 );
QCOMPARE( editor2->value( ), 123.12 );
// NULL, NULL
widget->setFeature( vl->getFeature( 2 ) );
widget2->setFeature( vl->getFeature( 2 ) );
QCOMPARE( editor->value( ), editor->minimum() );
QCOMPARE( editor2->value( ), editor2->minimum() );
// negative, negative
widget->setFeature( vl->getFeature( 3 ) );
widget2->setFeature( vl->getFeature( 3 ) );
// value was changed to the minimum
QCOMPARE( editor->value( ), -123.123456789 );
QCOMPARE( editor2->value( ), -123.12 );
}
QGSTEST_MAIN( TestQgsRangeWidgetWrapper )
#include "testqgsrangewidgetwrapper.moc"