Skip to content

Commit d7860e2

Browse files
committedFeb 16, 2018
Add some tests for ellipse map tools
1 parent 868c869 commit d7860e2

File tree

6 files changed

+169
-4
lines changed

6 files changed

+169
-4
lines changed
 

‎src/app/qgsmaptooladdrectangle.h

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

2324
class QgsPolygon;
2425

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

‎src/app/qgsmaptoolrectangle3points.h

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

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

22-
class QgsMapToolRectangle3Points: public QgsMapToolAddRectangle
23+
class APP_EXPORT QgsMapToolRectangle3Points: public QgsMapToolAddRectangle
2324
{
2425
Q_OBJECT
2526

‎src/app/qgsmaptoolrectanglecenter.h

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

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

22-
class QgsMapToolRectangleCenter: public QgsMapToolAddRectangle
23+
class APP_EXPORT QgsMapToolRectangleCenter: public QgsMapToolAddRectangle
2324
{
2425
Q_OBJECT
2526

‎src/app/qgsmaptoolrectangleextent.h

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

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

22-
class QgsMapToolRectangleExtent: public QgsMapToolAddRectangle
23+
class APP_EXPORT QgsMapToolRectangleExtent: public QgsMapToolAddRectangle
2324
{
2425
Q_OBJECT
2526

‎tests/src/app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ ADD_QGIS_TEST(maptoolreshape testqgsmaptoolreshape.cpp)
102102
ADD_QGIS_TEST(maptoolcircularstringtest testqgsmaptoolcircularstring.cpp)
103103
ADD_QGIS_TEST(maptoolcircletest testqgsmaptoolcircle.cpp)
104104
ADD_QGIS_TEST(maptoolellipsetest testqgsmaptoolellipse.cpp)
105+
ADD_QGIS_TEST(maptoolrectangletest testqgsmaptoolrectangle.cpp)
105106
ADD_QGIS_TEST(measuretool testqgsmeasuretool.cpp)
106107
ADD_QGIS_TEST(vertextool testqgsvertextool.cpp)
107108
ADD_QGIS_TEST(vectorlayersaveasdialogtest testqgsvectorlayersaveasdialog.cpp)
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/***************************************************************************
2+
testqgsmaptoolrectangle.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 "qgsmaptoolrectanglecenter.h"
28+
#include "qgsmaptoolrectangleextent.h"
29+
#include "qgsmaptoolrectangle3points.h"
30+
31+
32+
class TestQgsMapToolRectangle : public QObject
33+
{
34+
Q_OBJECT
35+
36+
public:
37+
TestQgsMapToolRectangle();
38+
39+
private slots:
40+
void initTestCase();
41+
void cleanupTestCase();
42+
43+
void testRectangleFromCenter();
44+
void testRectangleFromExtent();
45+
void testRectangleFrom3Points();
46+
47+
private:
48+
QgisApp *mQgisApp = nullptr;
49+
QgsMapToolCapture *mParentTool = nullptr;
50+
QgsMapCanvas *mCanvas = nullptr;
51+
QgsVectorLayer *mLayer = nullptr;
52+
};
53+
54+
TestQgsMapToolRectangle::TestQgsMapToolRectangle() = default;
55+
56+
57+
//runs before all tests
58+
void TestQgsMapToolRectangle::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 TestQgsMapToolRectangle::cleanupTestCase()
81+
{
82+
QgsApplication::exitQgis();
83+
}
84+
85+
void TestQgsMapToolRectangle::testRectangleFromCenter()
86+
{
87+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 333 );
88+
mLayer->startEditing();
89+
90+
QgsMapToolRectangleCenter 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 (-2 -1 333, -2 1 333, 2 1 333, 2 -1 333, -2 -1 333)";
103+
QCOMPARE( f.geometry().asWkt(), wkt );
104+
105+
mLayer->rollBack();
106+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
107+
}
108+
109+
void TestQgsMapToolRectangle::testRectangleFromExtent()
110+
{
111+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 222 );
112+
mLayer->startEditing();
113+
114+
QgsMapToolRectangleExtent 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 (0 0 222, 0 1 222, 2 1 222, 2 0 222, 0 0 222)";
127+
QCOMPARE( f.geometry().asWkt(), wkt );
128+
129+
mLayer->rollBack();
130+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
131+
}
132+
133+
void TestQgsMapToolRectangle::testRectangleFrom3Points()
134+
{
135+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 111 );
136+
mLayer->startEditing();
137+
138+
QgsMapToolRectangle3Points mapTool( mParentTool, mCanvas );
139+
mCanvas->setMapTool( &mapTool );
140+
141+
TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
142+
utils.mouseClick( 0, 0, Qt::LeftButton );
143+
utils.mouseMove( 2, 0 );
144+
utils.mouseClick( 2, 0, Qt::LeftButton );
145+
utils.mouseMove( 2, 1 );
146+
utils.mouseClick( 2, 1, Qt::RightButton );
147+
QgsFeatureId newFid = utils.newFeatureId();
148+
149+
QCOMPARE( mLayer->featureCount(), ( long )1 );
150+
QgsFeature f = mLayer->getFeature( newFid );
151+
152+
QString wkt = "LineStringZ (0 0 111, 2 0 111, 2 1 111, 0 1 111, 0 0 111)";
153+
QCOMPARE( f.geometry().asWkt( 0 ), wkt );
154+
155+
mLayer->rollBack();
156+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
157+
}
158+
159+
QGSTEST_MAIN( TestQgsMapToolRectangle )
160+
#include "testqgsmaptoolrectangle.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.