Skip to content

Commit

Permalink
Add unit test for scalebar decoration map width calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 19, 2021
1 parent 4caecdc commit 81c5770
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/app/decorations/qgsdecorationscalebar.h
Expand Up @@ -79,6 +79,7 @@ class APP_EXPORT QgsDecorationScaleBar: public QgsDecorationItem
double mapWidth( const QgsMapSettings &settings ) const;

friend class QgsDecorationScaleBarDialog;
friend class TestQgsDecorationScalebar;
};

#endif
1 change: 1 addition & 0 deletions tests/src/app/CMakeLists.txt
Expand Up @@ -61,6 +61,7 @@ ADD_QGIS_TEST(qgisappclipboard testqgisappclipboard.cpp)
ADD_QGIS_TEST(appdockwidgets testqgisappdockwidgets.cpp)
ADD_QGIS_TEST(attributetabletest testqgsattributetable.cpp)
ADD_QGIS_TEST(applocatorfilters testqgsapplocatorfilters.cpp)
ADD_QGIS_TEST(decorationscalebar testqgsdecorationscalebar.cpp)
ADD_QGIS_TEST(fieldcalculatortest testqgsfieldcalculator.cpp)
ADD_QGIS_TEST(maptooladdfeatureline testqgsmaptooladdfeatureline.cpp)
ADD_QGIS_TEST(maptooladdfeaturepoint testqgsmaptooladdfeaturepoint.cpp)
Expand Down
99 changes: 99 additions & 0 deletions tests/src/app/testqgsdecorationscalebar.cpp
@@ -0,0 +1,99 @@
/***************************************************************************
testqgsdecorationscalebar.cpp
----------------------
Date : 2021-01-19
Copyright : (C) 2021 by 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 "qgisapp.h"
#include "qgsapplication.h"
#include "qgsmapsettings.h"
#include "qgsdecorationscalebar.h"

class TestQgsDecorationScalebar : public QObject
{
Q_OBJECT
public:
TestQgsDecorationScalebar();

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 mapWidth();

private:
QgisApp *mQgisApp = nullptr;
};

TestQgsDecorationScalebar::TestQgsDecorationScalebar() = default;

//runs before all tests
void TestQgsDecorationScalebar::initTestCase()
{
qDebug() << "TestQgisAppClipboard::initTestCase()";
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();

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

mQgisApp = new QgisApp();

// enforce C locale because the tests expect it
// (decimal separators / thousand separators)
QLocale::setDefault( QLocale::c() );
}

//runs after all tests
void TestQgsDecorationScalebar::cleanupTestCase()
{
QgsApplication::exitQgis();
}

void TestQgsDecorationScalebar::mapWidth()
{
QgsProject::instance()->setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ) );
QgsMapSettings settings;
settings.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ) );
settings.setOutputSize( QSize( 800, 400 ) );
// same aspect ratio as output size
settings.setExtent( QgsRectangle( 16700000, -4210000, 16708000, -4206000 ) );

// unknown units, no conversion
QgsDecorationScaleBar scalebar;
QGSCOMPARENEAR( scalebar.mapWidth( settings ), 8000, 0.000001 );

// Cartesian measure
QgsProject::instance()->setEllipsoid( QString() );
scalebar.mSettings.setUnits( QgsUnitTypes::DistanceMiles );
QGSCOMPARENEAR( scalebar.mapWidth( settings ), 4.97097, 0.0001 );

#if PROJ_VERSION_MAJOR>=6
// ellipsoidal measure
QgsProject::instance()->setEllipsoid( QStringLiteral( "EPSG:7030" ) );
QGSCOMPARENEAR( scalebar.mapWidth( settings ), 4.060337, 0.0001 );
QgsProject::instance()->setEllipsoid( QString() );
#endif

// with non-uniform output size vs extent aspect ratio
settings.setExtent( QgsRectangle( 16700000, -4212000, 16708000, -4204000 ) );
QGSCOMPARENEAR( scalebar.mapWidth( settings ), 9.941939, 0.0001 );
settings.setExtent( settings.visibleExtent() );
QGSCOMPARENEAR( scalebar.mapWidth( settings ), 9.941939, 0.0001 );
}

QGSTEST_MAIN( TestQgsDecorationScalebar )
#include "testqgsdecorationscalebar.moc"

0 comments on commit 81c5770

Please sign in to comment.