1 /***************************************************************************
2 testqgsmeasurebearingtool.cpp
3 ----------------------
4 Date : 2021-06-15
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 "qgsvectorlayer.h"
19 #include "qgsfeature.h"
20 #include "qgsgeometry.h"
21 #include "qgsvectordataprovider.h"
22 #include "qgsmeasuretool.h"
23 #include "qgsmeasuredialog.h"
24 #include "qgsproject.h"
25 #include "qgsmapcanvas.h"
26 #include "qgsunittypes.h"
27 #include "qgsmaptoolmeasurebearing.h"
28 #include "testqgsmaptoolutils.h"
29 #include "qgsdisplayangle.h"
30
31 /**
32 * \ingroup UnitTests
33 * This is a unit test for the measure bearing tool
34 */
35 class TestQgsMeasureBearingTool : public QObject
36 {
37 Q_OBJECT
38 public:
39 TestQgsMeasureBearingTool();
40
41 private slots:
42 void initTestCase();// will be called before the first testfunction is executed.
43 void cleanupTestCase();// will be called after the last testfunction was executed.
init()44 void init() {} // will be called before each testfunction is executed.
cleanup()45 void cleanup() {} // will be called after every testfunction.
46 void testBearingCartesian();
47 void testBearingEllipsoid();
48
49 private:
50 QgisApp *mQgisApp = nullptr;
51 QgsMapCanvas *mCanvas = nullptr;
52 };
53
54 TestQgsMeasureBearingTool::TestQgsMeasureBearingTool() = default;
55
56 //runs before all tests
initTestCase()57 void TestQgsMeasureBearingTool::initTestCase()
58 {
59 qDebug() << "TestQgisAppClipboard::initTestCase()";
60 // init QGIS's paths - true means that all path will be inited from prefix
61 QgsApplication::init();
62 QgsApplication::initQgis();
63
64 // Set up the QgsSettings environment
65 QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
66 QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
67 QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );
68
69 mQgisApp = new QgisApp();
70 mCanvas = new QgsMapCanvas();
71
72 mCanvas->setFrameStyle( QFrame::NoFrame );
73 mCanvas->resize( 50, 50 );
74 mCanvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ) );
75 mCanvas->setExtent( QgsRectangle( -11554312, -5082786, 15123372, 11957046 ) );
76 mCanvas->show(); // to make the canvas resize
77 mCanvas->hide();
78
79 // enforce C locale because the tests expect it
80 // (decimal separators / thousand separators)
81 QLocale::setDefault( QLocale::c() );
82 }
83
84 //runs after all tests
cleanupTestCase()85 void TestQgsMeasureBearingTool::cleanupTestCase()
86 {
87 delete mCanvas;
88 QgsApplication::exitQgis();
89 }
90
testBearingCartesian()91 void TestQgsMeasureBearingTool::testBearingCartesian()
92 {
93 // set project CRS and set ellipsoid to none, so that Cartesian calculations are performed
94 const QgsCoordinateReferenceSystem srs( QStringLiteral( "EPSG:3857" ) );
95 QgsProject::instance()->setCrs( srs );
96 QgsProject::instance()->setEllipsoid( QString() );
97
98 QgsMapToolMeasureBearing *mapTool = new QgsMapToolMeasureBearing( mCanvas ) ;
99 mCanvas->setMapTool( mapTool );
100
101 QVERIFY( !mapTool->mResultDisplay );
102
103 TestQgsMapToolUtils tools( mapTool );
104
105 tools.mouseMove( 8749058, 1916460 );
106 tools.mouseClick( 8749058, 1916460, Qt::LeftButton );
107 tools.mouseMove( 14498439, -2694154 );
108
109 QVERIFY( mapTool->mResultDisplay );
110 QGSCOMPARENEAR( mapTool->mResultDisplay->value(), 2.1995926132, 0.001 );
111 QCOMPARE( mapTool->mResultDisplay->text(), QStringLiteral( "126.027373\u00B0E" ) );
112
113 tools.mouseClick( 14498439, -2694154, Qt::LeftButton );
114 QGSCOMPARENEAR( mapTool->mResultDisplay->value(), 2.1995926132, 0.001 );
115 QCOMPARE( mapTool->mResultDisplay->text(), QStringLiteral( "126.027373\u00B0E" ) );
116
117 tools.mouseMove( 555496, 3291312 );
118 tools.mouseClick( 555496, 3291312, Qt::LeftButton );
119 tools.mouseMove( -611045, 5082786 );
120 tools.mouseClick( -611045, 5082786, Qt::LeftButton );
121 QGSCOMPARENEAR( mapTool->mResultDisplay->value(), -0.5880026035, 0.001 );
122 QCOMPARE( mapTool->mResultDisplay->text(), QStringLiteral( "33.690068\u00B0W" ) );
123 }
124
testBearingEllipsoid()125 void TestQgsMeasureBearingTool::testBearingEllipsoid()
126 {
127 // set project CRS and ellipsoid
128 const QgsCoordinateReferenceSystem srs( QStringLiteral( "EPSG:3857" ) );
129 QgsProject::instance()->setCrs( srs );
130 QgsProject::instance()->setEllipsoid( QStringLiteral( "EPSG:7030" ) );
131
132 QgsMapToolMeasureBearing *mapTool = new QgsMapToolMeasureBearing( mCanvas ) ;
133 mCanvas->setMapTool( mapTool );
134
135 QVERIFY( !mapTool->mResultDisplay );
136
137 TestQgsMapToolUtils tools( mapTool );
138
139 tools.mouseMove( 8749058, 1916460 );
140 tools.mouseClick( 8749058, 1916460, Qt::LeftButton );
141 tools.mouseMove( 14498439, -2694154 );
142
143 QVERIFY( mapTool->mResultDisplay );
144 QGSCOMPARENEAR( mapTool->mResultDisplay->value(), 2.1679949043, 0.001 );
145 QCOMPARE( mapTool->mResultDisplay->text(), QStringLiteral( "124.216958\u00B0E" ) );
146
147 tools.mouseClick( 14498439, -2694154, Qt::LeftButton );
148 QGSCOMPARENEAR( mapTool->mResultDisplay->value(), 2.1679949043, 0.001 );
149 QCOMPARE( mapTool->mResultDisplay->text(), QStringLiteral( "124.216958\u00B0E" ) );
150
151 tools.mouseMove( 555496, 3291312 );
152 tools.mouseClick( 555496, 3291312, Qt::LeftButton );
153 tools.mouseMove( -611045, 5082786 );
154 tools.mouseClick( -611045, 5082786, Qt::LeftButton );
155 QGSCOMPARENEAR( mapTool->mResultDisplay->value(), -0.5448498177, 0.001 );
156 QCOMPARE( mapTool->mResultDisplay->text(), QStringLiteral( "31.217595\u00B0W" ) );
157 }
158
159 QGSTEST_MAIN( TestQgsMeasureBearingTool )
160 #include "testqgsmeasurebearingtool.moc"
161