Skip to content

Commit 236cb08

Browse files
committedMar 22, 2019
Unit tests for QgsDatumTransformDialog
1 parent 8d3f710 commit 236cb08

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
 

‎src/gui/qgsdatumtransformdialog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ class GUI_EXPORT QgsDatumTransformDialog : public QDialog, private Ui::QgsDatumT
128128
QgsCoordinateReferenceSystem mSourceCrs;
129129
QgsCoordinateReferenceSystem mDestinationCrs;
130130
std::unique_ptr< QgsTemporaryCursorRestoreOverride > mPreviousCursorOverride;
131+
132+
friend class TestQgsDatumTransformDialog;
131133
};
132134

133135
#endif // QGSDATUMTRANSFORMDIALOG_H

‎tests/src/gui/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ ADD_QGIS_TEST(edittooltest testqgsmaptooledit.cpp)
121121

122122
#ADD_QGIS_TEST(histogramtest testqgsrasterhistogram.cpp)
123123
ADD_QGIS_TEST(categorizedrendererwidget testqgscategorizedrendererwidget.cpp)
124+
ADD_QGIS_TEST(datumtransformdialog testqgsdatumtransformdialog.cpp)
124125
ADD_QGIS_TEST(doublespinbox testqgsdoublespinbox.cpp)
125126
ADD_QGIS_TEST(dualviewtest testqgsdualview.cpp)
126127
ADD_QGIS_TEST(attributeformtest testqgsattributeform.cpp)
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/***************************************************************************
2+
testqgsdatumtransformdialog.cpp
3+
--------------------------------------
4+
Date : March 2019
5+
Copyright : (C) 2019 Nyall Dawson
6+
Email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
17+
#include "qgstest.h"
18+
19+
#include "qgsdatumtransformdialog.h"
20+
#include "qgssettings.h"
21+
#include "qgsproject.h"
22+
23+
class TestQgsDatumTransformDialog: public QObject
24+
{
25+
Q_OBJECT
26+
private slots:
27+
void initTestCase(); // will be called before the first testfunction is executed.
28+
void cleanupTestCase(); // will be called after the last testfunction was executed.
29+
void init(); // will be called before each testfunction is executed.
30+
void cleanup(); // will be called after every testfunction.
31+
32+
void defaultTransform();
33+
void shouldAskUser();
34+
void applyDefaultTransform();
35+
void runDialog();
36+
37+
private:
38+
39+
};
40+
41+
void TestQgsDatumTransformDialog::initTestCase()
42+
{
43+
// initialize with test settings directory so we don't mess with user's stuff
44+
QgsApplication::init( QDir::tempPath() + "/dot-qgis" );
45+
QgsApplication::initQgis();
46+
QgsApplication::createDatabase();
47+
// output test environment
48+
QgsApplication::showSettings();
49+
50+
// Set up the QgsSettings environment
51+
QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
52+
QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
53+
QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );
54+
}
55+
56+
void TestQgsDatumTransformDialog::cleanupTestCase()
57+
{
58+
}
59+
60+
void TestQgsDatumTransformDialog::init()
61+
{
62+
}
63+
64+
void TestQgsDatumTransformDialog::cleanup()
65+
{
66+
}
67+
68+
void TestQgsDatumTransformDialog::defaultTransform()
69+
{
70+
QgsDatumTransformDialog dlg( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
71+
72+
QgsDatumTransformDialog::TransformInfo def = dlg.defaultDatumTransform();
73+
QCOMPARE( def.sourceCrs.authid(), QStringLiteral( "EPSG:26742" ) );
74+
QCOMPARE( def.destinationCrs.authid(), QStringLiteral( "EPSG:4326" ) );
75+
QCOMPARE( QgsDatumTransform::datumTransformToProj( def.sourceTransformId ), QStringLiteral( "+towgs84=-10,158,187" ) );
76+
QCOMPARE( QgsDatumTransform::datumTransformToProj( def.destinationTransformId ), QString() );
77+
78+
// default should be initially selected
79+
def = dlg.selectedDatumTransform();
80+
QCOMPARE( def.sourceCrs.authid(), QStringLiteral( "EPSG:26742" ) );
81+
QCOMPARE( def.destinationCrs.authid(), QStringLiteral( "EPSG:4326" ) );
82+
QCOMPARE( QgsDatumTransform::datumTransformToProj( def.sourceTransformId ), QStringLiteral( "+towgs84=-10,158,187" ) );
83+
QCOMPARE( QgsDatumTransform::datumTransformToProj( def.destinationTransformId ), QString() );
84+
85+
QgsDatumTransformDialog dlg2( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ) );
86+
def = dlg2.defaultDatumTransform();
87+
QCOMPARE( def.sourceCrs.authid(), QStringLiteral( "EPSG:4326" ) );
88+
QCOMPARE( def.destinationCrs.authid(), QStringLiteral( "EPSG:26742" ) );
89+
QCOMPARE( QgsDatumTransform::datumTransformToProj( def.sourceTransformId ), QString() );
90+
QCOMPARE( QgsDatumTransform::datumTransformToProj( def.destinationTransformId ), QStringLiteral( "+towgs84=-10,158,187" ) );
91+
92+
}
93+
94+
void TestQgsDatumTransformDialog::shouldAskUser()
95+
{
96+
// no prompts!
97+
QgsSettings().setValue( QStringLiteral( "/projections/promptWhenMultipleTransformsExist" ), false, QgsSettings::App );
98+
QgsDatumTransformDialog dlg( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
99+
QVERIFY( !dlg.shouldAskUserForSelection() );
100+
QgsDatumTransformDialog dlg2( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:7406" ) ) );
101+
QVERIFY( !dlg2.shouldAskUserForSelection() );
102+
103+
//prompts
104+
QgsSettings().setValue( QStringLiteral( "/projections/promptWhenMultipleTransformsExist" ), true, QgsSettings::App );
105+
QgsDatumTransformDialog dlg3( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
106+
QVERIFY( dlg3.shouldAskUserForSelection() );
107+
QgsDatumTransformDialog dlg4( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:7406" ) ) );
108+
QVERIFY( !dlg4.shouldAskUserForSelection() );
109+
}
110+
111+
void TestQgsDatumTransformDialog::applyDefaultTransform()
112+
{
113+
QgsSettings().setValue( QStringLiteral( "/projections/promptWhenMultipleTransformsExist" ), false, QgsSettings::App );
114+
115+
QgsDatumTransformDialog dlg( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:7406" ) ) );
116+
dlg.applyDefaultTransform();
117+
QVERIFY( QgsProject::instance()->transformContext().sourceDestinationDatumTransforms().isEmpty() );
118+
119+
QgsDatumTransformDialog dlg2( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
120+
dlg2.applyDefaultTransform();
121+
122+
QVERIFY( !QgsProject::instance()->transformContext().sourceDestinationDatumTransforms().isEmpty() );
123+
QCOMPARE( QgsDatumTransform::datumTransformToProj( QgsProject::instance()->transformContext().calculateDatumTransforms( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) ).sourceTransformId ), QStringLiteral( "+towgs84=-10,158,187" ) );
124+
QgsProject::instance()->clear();
125+
}
126+
127+
void TestQgsDatumTransformDialog::runDialog()
128+
{
129+
QgsSettings().setValue( QStringLiteral( "/projections/promptWhenMultipleTransformsExist" ), false, QgsSettings::App );
130+
131+
QVERIFY( QgsDatumTransformDialog::run( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:7406" ) ) ) );
132+
QVERIFY( QgsProject::instance()->transformContext().sourceDestinationDatumTransforms().isEmpty() );
133+
134+
QVERIFY( QgsDatumTransformDialog::run( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) ) );
135+
136+
QVERIFY( !QgsProject::instance()->transformContext().sourceDestinationDatumTransforms().isEmpty() );
137+
QCOMPARE( QgsDatumTransform::datumTransformToProj( QgsProject::instance()->transformContext().calculateDatumTransforms( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) ).sourceTransformId ), QStringLiteral( "+towgs84=-10,158,187" ) );
138+
QgsProject::instance()->clear();
139+
QVERIFY( QgsDatumTransformDialog::run( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) ) );
140+
}
141+
142+
143+
QGSTEST_MAIN( TestQgsDatumTransformDialog )
144+
#include "testqgsdatumtransformdialog.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.