1 /***************************************************************************
2 testqgsdecorationscalebar.cpp
3 ----------------------
4 Date : 2021-01-19
5 Copyright : (C) 2021 by 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 #include "qgstest.h"
16 #include "qgisapp.h"
17 #include "qgsapplication.h"
18 #include "qgsmapsettings.h"
19 #include "qgsdecorationscalebar.h"
20
21 class TestQgsDecorationScalebar : public QObject
22 {
23 Q_OBJECT
24 public:
25 TestQgsDecorationScalebar();
26
27 private slots:
28 void initTestCase();// will be called before the first testfunction is executed.
29 void cleanupTestCase();// will be called after the last testfunction was executed.
init()30 void init() {} // will be called before each testfunction is executed.
cleanup()31 void cleanup() {} // will be called after every testfunction.
32 void mapWidth();
33
34 private:
35 QgisApp *mQgisApp = nullptr;
36 };
37
38 TestQgsDecorationScalebar::TestQgsDecorationScalebar() = default;
39
40 //runs before all tests
initTestCase()41 void TestQgsDecorationScalebar::initTestCase()
42 {
43 qDebug() << "TestQgisAppClipboard::initTestCase()";
44 // init QGIS's paths - true means that all path will be inited from prefix
45 QgsApplication::init();
46 QgsApplication::initQgis();
47
48 // Set up the QgsSettings environment
49 QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
50 QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
51 QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );
52
53 mQgisApp = new QgisApp();
54
55 // enforce C locale because the tests expect it
56 // (decimal separators / thousand separators)
57 QLocale::setDefault( QLocale::c() );
58 }
59
60 //runs after all tests
cleanupTestCase()61 void TestQgsDecorationScalebar::cleanupTestCase()
62 {
63 QgsApplication::exitQgis();
64 }
65
mapWidth()66 void TestQgsDecorationScalebar::mapWidth()
67 {
68 QgsProject::instance()->setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ) );
69 QgsMapSettings settings;
70 settings.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ) );
71 settings.setOutputSize( QSize( 800, 400 ) );
72 // same aspect ratio as output size
73 settings.setExtent( QgsRectangle( 16700000, -4210000, 16708000, -4206000 ) );
74
75 // unknown units, no conversion
76 QgsDecorationScaleBar scalebar;
77 QGSCOMPARENEAR( scalebar.mapWidth( settings ), 8000, 0.000001 );
78
79 // Cartesian measure
80 QgsProject::instance()->setEllipsoid( QString() );
81 scalebar.mSettings.setUnits( QgsUnitTypes::DistanceMiles );
82 QGSCOMPARENEAR( scalebar.mapWidth( settings ), 4.97097, 0.0001 );
83
84 // ellipsoidal measure
85 QgsProject::instance()->setEllipsoid( QStringLiteral( "EPSG:7030" ) );
86 QGSCOMPARENEAR( scalebar.mapWidth( settings ), 4.060337, 0.0001 );
87 QgsProject::instance()->setEllipsoid( QString() );
88
89 // with non-uniform output size vs extent aspect ratio
90 settings.setExtent( QgsRectangle( 16700000, -4212000, 16708000, -4204000 ) );
91 QGSCOMPARENEAR( scalebar.mapWidth( settings ), 9.941939, 0.0001 );
92 settings.setExtent( settings.visibleExtent() );
93 QGSCOMPARENEAR( scalebar.mapWidth( settings ), 9.941939, 0.0001 );
94 }
95
96 QGSTEST_MAIN( TestQgsDecorationScalebar )
97 #include "testqgsdecorationscalebar.moc"
98