Skip to content

Commit

Permalink
Unit tests for QgsDatumTransformDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 22, 2019
1 parent 8d3f710 commit 236cb08
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/gui/qgsdatumtransformdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class GUI_EXPORT QgsDatumTransformDialog : public QDialog, private Ui::QgsDatumT
QgsCoordinateReferenceSystem mSourceCrs;
QgsCoordinateReferenceSystem mDestinationCrs;
std::unique_ptr< QgsTemporaryCursorRestoreOverride > mPreviousCursorOverride;

friend class TestQgsDatumTransformDialog;
};

#endif // QGSDATUMTRANSFORMDIALOG_H
1 change: 1 addition & 0 deletions tests/src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ ADD_QGIS_TEST(edittooltest testqgsmaptooledit.cpp)

#ADD_QGIS_TEST(histogramtest testqgsrasterhistogram.cpp)
ADD_QGIS_TEST(categorizedrendererwidget testqgscategorizedrendererwidget.cpp)
ADD_QGIS_TEST(datumtransformdialog testqgsdatumtransformdialog.cpp)
ADD_QGIS_TEST(doublespinbox testqgsdoublespinbox.cpp)
ADD_QGIS_TEST(dualviewtest testqgsdualview.cpp)
ADD_QGIS_TEST(attributeformtest testqgsattributeform.cpp)
Expand Down
144 changes: 144 additions & 0 deletions tests/src/gui/testqgsdatumtransformdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/***************************************************************************
testqgsdatumtransformdialog.cpp
--------------------------------------
Date : March 2019
Copyright : (C) 2019 Nyall Dawson
Email : nyall dot dawson at gmail dot 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 "qgsdatumtransformdialog.h"
#include "qgssettings.h"
#include "qgsproject.h"

class TestQgsDatumTransformDialog: 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 defaultTransform();
void shouldAskUser();
void applyDefaultTransform();
void runDialog();

private:

};

void TestQgsDatumTransformDialog::initTestCase()
{
// initialize with test settings directory so we don't mess with user's stuff
QgsApplication::init( QDir::tempPath() + "/dot-qgis" );
QgsApplication::initQgis();
QgsApplication::createDatabase();
// output test environment
QgsApplication::showSettings();

// Set up the QgsSettings environment
QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );
}

void TestQgsDatumTransformDialog::cleanupTestCase()
{
}

void TestQgsDatumTransformDialog::init()
{
}

void TestQgsDatumTransformDialog::cleanup()
{
}

void TestQgsDatumTransformDialog::defaultTransform()
{
QgsDatumTransformDialog dlg( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );

QgsDatumTransformDialog::TransformInfo def = dlg.defaultDatumTransform();
QCOMPARE( def.sourceCrs.authid(), QStringLiteral( "EPSG:26742" ) );
QCOMPARE( def.destinationCrs.authid(), QStringLiteral( "EPSG:4326" ) );
QCOMPARE( QgsDatumTransform::datumTransformToProj( def.sourceTransformId ), QStringLiteral( "+towgs84=-10,158,187" ) );
QCOMPARE( QgsDatumTransform::datumTransformToProj( def.destinationTransformId ), QString() );

// default should be initially selected
def = dlg.selectedDatumTransform();
QCOMPARE( def.sourceCrs.authid(), QStringLiteral( "EPSG:26742" ) );
QCOMPARE( def.destinationCrs.authid(), QStringLiteral( "EPSG:4326" ) );
QCOMPARE( QgsDatumTransform::datumTransformToProj( def.sourceTransformId ), QStringLiteral( "+towgs84=-10,158,187" ) );
QCOMPARE( QgsDatumTransform::datumTransformToProj( def.destinationTransformId ), QString() );

QgsDatumTransformDialog dlg2( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ) );
def = dlg2.defaultDatumTransform();
QCOMPARE( def.sourceCrs.authid(), QStringLiteral( "EPSG:4326" ) );
QCOMPARE( def.destinationCrs.authid(), QStringLiteral( "EPSG:26742" ) );
QCOMPARE( QgsDatumTransform::datumTransformToProj( def.sourceTransformId ), QString() );
QCOMPARE( QgsDatumTransform::datumTransformToProj( def.destinationTransformId ), QStringLiteral( "+towgs84=-10,158,187" ) );

}

void TestQgsDatumTransformDialog::shouldAskUser()
{
// no prompts!
QgsSettings().setValue( QStringLiteral( "/projections/promptWhenMultipleTransformsExist" ), false, QgsSettings::App );
QgsDatumTransformDialog dlg( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
QVERIFY( !dlg.shouldAskUserForSelection() );
QgsDatumTransformDialog dlg2( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:7406" ) ) );
QVERIFY( !dlg2.shouldAskUserForSelection() );

//prompts
QgsSettings().setValue( QStringLiteral( "/projections/promptWhenMultipleTransformsExist" ), true, QgsSettings::App );
QgsDatumTransformDialog dlg3( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
QVERIFY( dlg3.shouldAskUserForSelection() );
QgsDatumTransformDialog dlg4( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:7406" ) ) );
QVERIFY( !dlg4.shouldAskUserForSelection() );
}

void TestQgsDatumTransformDialog::applyDefaultTransform()
{
QgsSettings().setValue( QStringLiteral( "/projections/promptWhenMultipleTransformsExist" ), false, QgsSettings::App );

QgsDatumTransformDialog dlg( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:7406" ) ) );
dlg.applyDefaultTransform();
QVERIFY( QgsProject::instance()->transformContext().sourceDestinationDatumTransforms().isEmpty() );

QgsDatumTransformDialog dlg2( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
dlg2.applyDefaultTransform();

QVERIFY( !QgsProject::instance()->transformContext().sourceDestinationDatumTransforms().isEmpty() );
QCOMPARE( QgsDatumTransform::datumTransformToProj( QgsProject::instance()->transformContext().calculateDatumTransforms( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) ).sourceTransformId ), QStringLiteral( "+towgs84=-10,158,187" ) );
QgsProject::instance()->clear();
}

void TestQgsDatumTransformDialog::runDialog()
{
QgsSettings().setValue( QStringLiteral( "/projections/promptWhenMultipleTransformsExist" ), false, QgsSettings::App );

QVERIFY( QgsDatumTransformDialog::run( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:7406" ) ) ) );
QVERIFY( QgsProject::instance()->transformContext().sourceDestinationDatumTransforms().isEmpty() );

QVERIFY( QgsDatumTransformDialog::run( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) ) );

QVERIFY( !QgsProject::instance()->transformContext().sourceDestinationDatumTransforms().isEmpty() );
QCOMPARE( QgsDatumTransform::datumTransformToProj( QgsProject::instance()->transformContext().calculateDatumTransforms( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) ).sourceTransformId ), QStringLiteral( "+towgs84=-10,158,187" ) );
QgsProject::instance()->clear();
QVERIFY( QgsDatumTransformDialog::run( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) ) );
}


QGSTEST_MAIN( TestQgsDatumTransformDialog )
#include "testqgsdatumtransformdialog.moc"

0 comments on commit 236cb08

Please sign in to comment.