Skip to content

Commit 3d319d9

Browse files
authored
Add test for selecting invalid geometries (#3246)
See #13635 The test passes
1 parent 21b8ef0 commit 3d319d9

File tree

2 files changed

+157
-2
lines changed

2 files changed

+157
-2
lines changed

tests/src/app/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#####################################################
22
# Don't forget to include output directory, otherwise
33
# the UI file won't be wrapped!
4-
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
4+
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
55
${CMAKE_CURRENT_BINARY_DIR}
66
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/core
77
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/core/auth
@@ -58,7 +58,7 @@ ENDIF (APPLE)
5858
#qtests in the executable file list as the moc is
5959
#directly included in the sources
6060
#and should not be compiled twice. Trying to include
61-
#them in will cause an error at build time
61+
#them in will cause an error at build time
6262
6363
#No relinking and full RPATH for the install tree
6464
#See: http://www.cmake.org/Wiki/CMake_RPATH_handling#No_relinking_and_full_RPATH_for_the_install_tree
@@ -106,5 +106,6 @@ ADD_QGIS_TEST(qgisappclipboard testqgisappclipboard.cpp)
106106
ADD_QGIS_TEST(attributetabletest testqgsattributetable.cpp)
107107
ADD_QGIS_TEST(fieldcalculatortest testqgsfieldcalculator.cpp)
108108
ADD_QGIS_TEST(maptoolidentifyaction testqgsmaptoolidentifyaction.cpp)
109+
ADD_QGIS_TEST(maptoolselect testqgsmaptoolselect.cpp)
109110
ADD_QGIS_TEST(measuretool testqgsmeasuretool.cpp)
110111
ADD_QGIS_TEST(vectorlayersaveasdialogtest testqgsvectorlayersaveasdialog.cpp)
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/***************************************************************************
2+
testqgsmaptoolselect.cpp
3+
--------------------------------
4+
Date : 2016-06-23
5+
Copyright : (C) 2016 by Sandro Santilli
6+
Email : strk@kbt.io
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 <QtTest/QtTest>
17+
#include "qgsapplication.h"
18+
#include "qgsvectorlayer.h"
19+
#include "qgsrasterlayer.h"
20+
#include "qgsfeature.h"
21+
#include "qgsgeometry.h"
22+
#include "qgsvectordataprovider.h"
23+
#include "qgsproject.h"
24+
#include "qgsmapcanvas.h"
25+
#include "qgsunittypes.h"
26+
#include "qgsmaptoolselect.h"
27+
#include "qgsmaptoolselectutils.h"
28+
29+
#include "cpl_conv.h"
30+
31+
class TestQgsMapToolSelect : public QObject
32+
{
33+
Q_OBJECT
34+
public:
35+
TestQgsMapToolSelect()
36+
: canvas( 0 )
37+
{}
38+
39+
private slots:
40+
void initTestCase(); // will be called before the first testfunction is executed.
41+
void cleanupTestCase(); // will be called after the last testfunction was executed.
42+
void init(); // will be called before each testfunction is executed.
43+
void cleanup(); // will be called after every testfunction.
44+
45+
void selectInvalidPolygons(); // test selecting invalid polygons
46+
47+
private:
48+
QgsMapCanvas* canvas;
49+
50+
QgsFeatureList testSelectVector( QgsVectorLayer* layer, double xGeoref, double yGeoref );
51+
52+
// Release return with delete []
53+
unsigned char *
54+
hex2bytes( const char *hex, int *size )
55+
{
56+
QByteArray ba = QByteArray::fromHex( hex );
57+
unsigned char *out = new unsigned char[ba.size()];
58+
memcpy( out, ba.data(), ba.size() );
59+
*size = ba.size();
60+
return out;
61+
}
62+
63+
// TODO: make this a QgsGeometry member...
64+
QgsGeometry geomFromHexWKB( const char *hexwkb )
65+
{
66+
int wkbsize;
67+
unsigned char *wkb = hex2bytes( hexwkb, &wkbsize );
68+
QgsGeometry geom;
69+
// NOTE: QgsGeometry takes ownership of wkb
70+
geom.fromWkb( wkb, wkbsize );
71+
return geom;
72+
}
73+
74+
};
75+
76+
void TestQgsMapToolSelect::initTestCase()
77+
{
78+
QgsApplication::init();
79+
QgsApplication::initQgis();
80+
// Set up the QSettings environment
81+
QCoreApplication::setOrganizationName( "QGIS" );
82+
QCoreApplication::setOrganizationDomain( "qgis.org" );
83+
QCoreApplication::setApplicationName( "QGIS-TEST" );
84+
85+
QgsApplication::showSettings();
86+
87+
// enforce C locale because the tests expect it
88+
// (decimal separators / thousand separators)
89+
QLocale::setDefault( QLocale::c() );
90+
}
91+
92+
void TestQgsMapToolSelect::cleanupTestCase()
93+
{
94+
QgsApplication::exitQgis();
95+
}
96+
97+
void TestQgsMapToolSelect::init()
98+
{
99+
canvas = new QgsMapCanvas();
100+
}
101+
102+
void TestQgsMapToolSelect::cleanup()
103+
{
104+
delete canvas;
105+
}
106+
107+
// private
108+
QgsFeatureList
109+
TestQgsMapToolSelect::testSelectVector( QgsVectorLayer* layer, double xGeoref, double yGeoref )
110+
{
111+
QScopedPointer< QgsMapToolSelect > tool( new QgsMapToolSelect( canvas ) );
112+
QgsPoint mapPoint = canvas->getCoordinateTransform()->transform( xGeoref, yGeoref );
113+
114+
// make given vector layer current
115+
canvas->setCurrentLayer( layer );
116+
117+
QScopedPointer< QgsMapMouseEvent > event( new QgsMapMouseEvent(
118+
canvas,
119+
QEvent::MouseButtonRelease,
120+
QPoint( mapPoint.x(), mapPoint.y() )
121+
) );
122+
123+
// trigger mouseRelease handler
124+
tool->canvasReleaseEvent( event.data() );
125+
126+
// return selected features
127+
return layer->selectedFeatures();
128+
}
129+
130+
void TestQgsMapToolSelect::selectInvalidPolygons()
131+
{
132+
//create a temporary layer
133+
QScopedPointer< QgsVectorLayer > memoryLayer( new QgsVectorLayer( "Polygon?field=pk:int", "vl", "memory" ) );
134+
QVERIFY( memoryLayer->isValid() );
135+
QgsFeature f1( memoryLayer->dataProvider()->fields(), 1 );
136+
f1.setAttribute( "pk", 1 );
137+
f1.setGeometry( geomFromHexWKB(
138+
"010300000001000000030000000000000000000000000000000000000000000000000024400000000000000000000000000000244000000000000024400000000000000000"
139+
) );
140+
memoryLayer->dataProvider()->addFeatures( QgsFeatureList() << f1 );
141+
142+
canvas->setExtent( QgsRectangle( 0, 0, 10, 10 ) );
143+
QgsFeatureList selected;
144+
selected = testSelectVector( memoryLayer.data(), 4, 6 );
145+
QCOMPARE( selected.length(), 0 );
146+
selected = testSelectVector( memoryLayer.data(), 6, 4 );
147+
QCOMPARE( selected.length(), 1 );
148+
QCOMPARE( selected[0].attribute( "pk" ), QVariant( 1 ) );
149+
150+
}
151+
152+
153+
QTEST_MAIN( TestQgsMapToolSelect )
154+
#include "testqgsmaptoolselect.moc"

0 commit comments

Comments
 (0)