Skip to content

Commit ce9e012

Browse files
committedFeb 16, 2018
Add some tests for regular polygon map tools
1 parent d7860e2 commit ce9e012

6 files changed

+167
-4
lines changed
 

‎src/app/qgsmaptooladdregularpolygon.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
#include "qgsregularpolygon.h"
2020
#include "qgsmaptoolcapture.h"
2121
#include "qgsspinbox.h"
22+
#include "qgis_app.h"
2223

2324
class QSpinBox;
2425

25-
class QgsMapToolAddRegularPolygon: public QgsMapToolCapture
26+
class APP_EXPORT QgsMapToolAddRegularPolygon: public QgsMapToolCapture
2627
{
2728
Q_OBJECT
2829

‎src/app/qgsmaptoolregularpolygon2points.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
#define QGSMAPTOOLREGULARPOLYGON2POINTS_H
1919

2020
#include "qgsmaptooladdregularpolygon.h"
21+
#include "qgis_app.h"
2122

22-
class QgsMapToolRegularPolygon2Points: public QgsMapToolAddRegularPolygon
23+
class APP_EXPORT QgsMapToolRegularPolygon2Points: public QgsMapToolAddRegularPolygon
2324
{
2425
Q_OBJECT
2526

‎src/app/qgsmaptoolregularpolygoncentercorner.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
#define QGSMAPTOOLREGULARPOLYGONCENTERCORNER_H
1919

2020
#include "qgsmaptooladdregularpolygon.h"
21+
#include "qgis_app.h"
2122

22-
class QgsMapToolRegularPolygonCenterCorner: public QgsMapToolAddRegularPolygon
23+
class APP_EXPORT QgsMapToolRegularPolygonCenterCorner: public QgsMapToolAddRegularPolygon
2324
{
2425
Q_OBJECT
2526

‎src/app/qgsmaptoolregularpolygoncenterpoint.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
#define QGSMAPTOOLREGULARPOLYGONCENTERPOINT_H
1919

2020
#include "qgsmaptooladdregularpolygon.h"
21+
#include "qgis_app.h"
2122

22-
class QgsMapToolRegularPolygonCenterPoint: public QgsMapToolAddRegularPolygon
23+
class APP_EXPORT QgsMapToolRegularPolygonCenterPoint: public QgsMapToolAddRegularPolygon
2324
{
2425
Q_OBJECT
2526

‎tests/src/app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ ADD_QGIS_TEST(maptoolcircularstringtest testqgsmaptoolcircularstring.cpp)
103103
ADD_QGIS_TEST(maptoolcircletest testqgsmaptoolcircle.cpp)
104104
ADD_QGIS_TEST(maptoolellipsetest testqgsmaptoolellipse.cpp)
105105
ADD_QGIS_TEST(maptoolrectangletest testqgsmaptoolrectangle.cpp)
106+
ADD_QGIS_TEST(maptoolregularpolygontest testqgsmaptoolregularpolygon.cpp)
106107
ADD_QGIS_TEST(measuretool testqgsmeasuretool.cpp)
107108
ADD_QGIS_TEST(vertextool testqgsvertextool.cpp)
108109
ADD_QGIS_TEST(vectorlayersaveasdialogtest testqgsvectorlayersaveasdialog.cpp)
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/***************************************************************************
2+
testqgsmaptoolregularpolygon.cpp
3+
---------------------------
4+
Date : January 2018
5+
Copyright : (C) 2018 by Paul Blottiere
6+
Email : paul.blottiere@oslandia.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+
#include "qgstest.h"
17+
18+
#include "qgisapp.h"
19+
#include "qgsgeometry.h"
20+
#include "qgsmapcanvas.h"
21+
#include "qgssettings.h"
22+
#include "qgsvectorlayer.h"
23+
#include "qgsmaptooladdfeature.h"
24+
#include "qgsgeometryutils.h"
25+
26+
#include "testqgsmaptoolutils.h"
27+
#include "qgsmaptoolregularpolygon2points.h"
28+
#include "qgsmaptoolregularpolygoncenterpoint.h"
29+
#include "qgsmaptoolregularpolygoncentercorner.h"
30+
31+
32+
class TestQgsMapToolRegularPolygon : public QObject
33+
{
34+
Q_OBJECT
35+
36+
public:
37+
TestQgsMapToolRegularPolygon();
38+
39+
private slots:
40+
void initTestCase();
41+
void cleanupTestCase();
42+
43+
void testRegularPolygonFrom2Points();
44+
void testRegularPolygonFromCenterAndPoint();
45+
void testRegularPolygonFromCenterAndCroner();
46+
47+
private:
48+
QgisApp *mQgisApp = nullptr;
49+
QgsMapToolCapture *mParentTool = nullptr;
50+
QgsMapCanvas *mCanvas = nullptr;
51+
QgsVectorLayer *mLayer = nullptr;
52+
};
53+
54+
TestQgsMapToolRegularPolygon::TestQgsMapToolRegularPolygon() = default;
55+
56+
57+
//runs before all tests
58+
void TestQgsMapToolRegularPolygon::initTestCase()
59+
{
60+
QgsApplication::init();
61+
QgsApplication::initQgis();
62+
63+
mQgisApp = new QgisApp();
64+
65+
mCanvas = new QgsMapCanvas();
66+
mCanvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:27700" ) ) );
67+
68+
// make testing layers
69+
mLayer = new QgsVectorLayer( QStringLiteral( "LineStringZ?crs=EPSG:27700" ), QStringLiteral( "layer line Z" ), QStringLiteral( "memory" ) );
70+
QVERIFY( mLayer->isValid() );
71+
QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayer );
72+
73+
// set layers in canvas
74+
mCanvas->setLayers( QList<QgsMapLayer *>() << mLayer );
75+
mCanvas->setCurrentLayer( mLayer );
76+
77+
mParentTool = new QgsMapToolAddFeature( mCanvas, QgsMapToolCapture::CaptureLine );
78+
}
79+
80+
void TestQgsMapToolRegularPolygon::cleanupTestCase()
81+
{
82+
QgsApplication::exitQgis();
83+
}
84+
85+
void TestQgsMapToolRegularPolygon::testRegularPolygonFrom2Points()
86+
{
87+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 333 );
88+
mLayer->startEditing();
89+
90+
QgsMapToolRegularPolygon2Points mapTool( mParentTool, mCanvas );
91+
mCanvas->setMapTool( &mapTool );
92+
93+
TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
94+
utils.mouseClick( 0, 0, Qt::LeftButton );
95+
utils.mouseMove( 2, 1 );
96+
utils.mouseClick( 2, 1, Qt::RightButton );
97+
QgsFeatureId newFid = utils.newFeatureId();
98+
99+
QCOMPARE( mLayer->featureCount(), ( long )1 );
100+
QgsFeature f = mLayer->getFeature( newFid );
101+
102+
QString wkt = "LineStringZ (0 0 333, 2 1 333, 4 -0 333, 4 -2 333, 2 -3 333, -0 -2 333, 0 0 333)";
103+
QCOMPARE( f.geometry().asWkt( 0 ), wkt );
104+
105+
mLayer->rollBack();
106+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
107+
}
108+
109+
void TestQgsMapToolRegularPolygon::testRegularPolygonFromCenterAndPoint()
110+
{
111+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 222 );
112+
mLayer->startEditing();
113+
114+
QgsMapToolRegularPolygonCenterPoint mapTool( mParentTool, mCanvas );
115+
mCanvas->setMapTool( &mapTool );
116+
117+
TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
118+
utils.mouseClick( 0, 0, Qt::LeftButton );
119+
utils.mouseMove( 2, 1 );
120+
utils.mouseClick( 2, 1, Qt::RightButton );
121+
QgsFeatureId newFid = utils.newFeatureId();
122+
123+
QCOMPARE( mLayer->featureCount(), ( long )1 );
124+
QgsFeature f = mLayer->getFeature( newFid );
125+
126+
QString wkt = "LineStringZ (1 2 222, 3 -0 222, 1 -2 222, -1 -2 222, -3 0 222, -1 2 222, 1 2 222)";
127+
QCOMPARE( f.geometry().asWkt( 0 ), wkt );
128+
129+
mLayer->rollBack();
130+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
131+
}
132+
133+
void TestQgsMapToolRegularPolygon::testRegularPolygonFromCenterAndCroner()
134+
{
135+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 111 );
136+
mLayer->startEditing();
137+
138+
QgsMapToolRegularPolygonCenterCorner mapTool( mParentTool, mCanvas );
139+
mCanvas->setMapTool( &mapTool );
140+
141+
TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
142+
utils.mouseClick( 0, 0, Qt::LeftButton );
143+
utils.mouseMove( 2, 1 );
144+
utils.mouseClick( 2, 1, Qt::RightButton );
145+
QgsFeatureId newFid = utils.newFeatureId();
146+
147+
QCOMPARE( mLayer->featureCount(), ( long )1 );
148+
QgsFeature f = mLayer->getFeature( newFid );
149+
150+
QString wkt = "LineStringZ (2 1 111, 2 -1 111, -0 -2 111, -2 -1 111, -2 1 111, 0 2 111, 2 1 111)";
151+
QCOMPARE( f.geometry().asWkt( 0 ), wkt );
152+
153+
mLayer->rollBack();
154+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
155+
}
156+
157+
QGSTEST_MAIN( TestQgsMapToolRegularPolygon )
158+
#include "testqgsmaptoolregularpolygon.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.