Skip to content

Commit cc660b1

Browse files
committedDec 6, 2017
Add some tests
1 parent c9038f4 commit cc660b1

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
 

‎tests/src/app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ ADD_QGIS_TEST(fieldcalculatortest testqgsfieldcalculator.cpp)
9898
ADD_QGIS_TEST(maptooladdfeature testqgsmaptooladdfeature.cpp)
9999
ADD_QGIS_TEST(maptoolidentifyaction testqgsmaptoolidentifyaction.cpp)
100100
ADD_QGIS_TEST(maptoolselect testqgsmaptoolselect.cpp)
101+
ADD_QGIS_TEST(maptoolreshape testqgsmaptoolreshape.cpp)
101102
ADD_QGIS_TEST(measuretool testqgsmeasuretool.cpp)
102103
ADD_QGIS_TEST(nodetool testqgsnodetool.cpp)
103104
ADD_QGIS_TEST(vectorlayersaveasdialogtest testqgsvectorlayersaveasdialog.cpp)
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/***************************************************************************
2+
testqgsmaptoolreshape.cpp
3+
--------------------------------
4+
Date : 2017-121
5+
Copyright : (C) 2017 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+
#include "qgsapplication.h"
18+
#include "qgsmapcanvas.h"
19+
#include "qgsvectorlayer.h"
20+
#include "qgslinestring.h"
21+
#include "qgsmaptoolreshape.h"
22+
#include "qgisapp.h"
23+
24+
class TestQgsMapToolReshape : public QObject
25+
{
26+
Q_OBJECT
27+
public:
28+
TestQgsMapToolReshape() = default;
29+
30+
private slots:
31+
void initTestCase(); // will be called before the first testfunction is executed.
32+
void cleanupTestCase(); // will be called after the last testfunction was executed.
33+
void init(); // will be called before each testfunction is executed.
34+
void cleanup(); // will be called after every testfunction.
35+
36+
void reshapeWithBindingLine();
37+
38+
private:
39+
QgisApp *mQgisApp = nullptr;
40+
};
41+
42+
void TestQgsMapToolReshape::initTestCase()
43+
{
44+
QgsApplication::init();
45+
QgsApplication::initQgis();
46+
47+
// Set up the QgsSettings environment
48+
QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
49+
QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
50+
QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );
51+
52+
QgsApplication::showSettings();
53+
54+
// enforce C locale because the tests expect it
55+
// (decimal separators / thousand separators)
56+
QLocale::setDefault( QLocale::c() );
57+
58+
mQgisApp = new QgisApp();
59+
}
60+
61+
void TestQgsMapToolReshape::cleanupTestCase()
62+
{
63+
QgsApplication::exitQgis();
64+
}
65+
66+
void TestQgsMapToolReshape::init()
67+
{
68+
}
69+
70+
void TestQgsMapToolReshape::cleanup()
71+
{
72+
}
73+
74+
void TestQgsMapToolReshape::reshapeWithBindingLine()
75+
{
76+
// prepare vector layer
77+
std::unique_ptr<QgsVectorLayer> vl;
78+
vl.reset( new QgsVectorLayer( QStringLiteral( "LineString?crs=epsg:4326&field=name:string(20)" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) ) );
79+
80+
QgsGeometry g0 = QgsGeometry::fromWkt( "LineString (0 0, 1 1, 1 2)" );
81+
QgsFeature f0;
82+
f0.setGeometry( g0 );
83+
f0.setAttribute( 0, "polyline0" );
84+
85+
QgsGeometry g1 = QgsGeometry::fromWkt( "LineString (2 1, 3 2, 3 3, 2 2)" );
86+
QgsFeature f1;
87+
f1.setGeometry( g1 );
88+
f1.setAttribute( 0, "polyline1" );
89+
90+
vl->dataProvider()->addFeatures( QgsFeatureList() << f0 << f1 );
91+
92+
// prepare canvas
93+
QList<QgsMapLayer *> layers;
94+
layers.append( vl.get() );
95+
96+
QgsCoordinateReferenceSystem srs( 4326, QgsCoordinateReferenceSystem::EpsgCrsId );
97+
mQgisApp->mapCanvas()->setDestinationCrs( srs );
98+
mQgisApp->mapCanvas()->setLayers( layers );
99+
mQgisApp->mapCanvas()->setCurrentLayer( vl.get() );
100+
101+
// reshape to add line to polyline0
102+
QgsLineString cl0;
103+
cl0.setPoints( QgsPointSequence() << QgsPoint( 1, 2 ) << QgsPoint( 2, 1 ) );
104+
105+
QgsCompoundCurve curve0( *cl0.toCurveType() );
106+
107+
QgsMapToolReshape tool0( mQgisApp->mapCanvas() );
108+
tool0.mCaptureCurve = curve0;
109+
110+
vl->startEditing();
111+
tool0.reshape( vl.get() );
112+
113+
f0 = vl->getFeature( 1 );
114+
QCOMPARE( f0.geometry().asJson(), QStringLiteral( "{\"type\": \"LineString\", \"coordinates\": [ [0, 0], [1, 1], [1, 2], [2, 1]]}" ) );
115+
116+
f1 = vl->getFeature( 2 );
117+
QCOMPARE( f1.geometry().asJson(), QStringLiteral( "{\"type\": \"LineString\", \"coordinates\": [ [2, 1], [3, 2], [3, 3], [2, 2]]}" ) );
118+
119+
vl->rollBack();
120+
121+
// reshape to add line to polyline1
122+
QgsLineString cl1;
123+
cl1.setPoints( QgsPointSequence() << QgsPoint( 2, 1 ) << QgsPoint( 1, 2 ) );
124+
125+
QgsCompoundCurve curve1( *cl1.toCurveType() );
126+
127+
QgsMapToolReshape tool1( mQgisApp->mapCanvas() );
128+
tool1.mCaptureCurve = curve1;
129+
130+
vl->startEditing();
131+
tool1.reshape( vl.get() );
132+
133+
f0 = vl->getFeature( 1 );
134+
QCOMPARE( f0.geometry().asJson(), QStringLiteral( "{\"type\": \"LineString\", \"coordinates\": [ [0, 0], [1, 1], [1, 2]]}" ) );
135+
136+
f1 = vl->getFeature( 2 );
137+
QCOMPARE( f1.geometry().asJson(), QStringLiteral( "{\"type\": \"LineString\", \"coordinates\": [ [1, 2], [2, 1], [3, 2], [3, 3], [2, 2]]}" ) );
138+
139+
vl->rollBack();
140+
}
141+
142+
QGSTEST_MAIN( TestQgsMapToolReshape )
143+
#include "testqgsmaptoolreshape.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.