Skip to content
Permalink
Browse files

Add test for selecting invalid geometries (#3246)

See #13635
The test passes
  • Loading branch information
strk committed Jun 27, 2016
1 parent 21b8ef0 commit 3d319d90bd8d486e2f30512e03419a636a84c10a
Showing with 157 additions and 2 deletions.
  1. +3 −2 tests/src/app/CMakeLists.txt
  2. +154 −0 tests/src/app/testqgsmaptoolselect.cpp
@@ -1,7 +1,7 @@
#####################################################
# Don't forget to include output directory, otherwise
# the UI file won't be wrapped!
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/core
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/core/auth
@@ -58,7 +58,7 @@ ENDIF (APPLE)
#qtests in the executable file list as the moc is
#directly included in the sources
#and should not be compiled twice. Trying to include
#them in will cause an error at build time
#them in will cause an error at build time

#No relinking and full RPATH for the install tree
#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)
ADD_QGIS_TEST(attributetabletest testqgsattributetable.cpp)
ADD_QGIS_TEST(fieldcalculatortest testqgsfieldcalculator.cpp)
ADD_QGIS_TEST(maptoolidentifyaction testqgsmaptoolidentifyaction.cpp)
ADD_QGIS_TEST(maptoolselect testqgsmaptoolselect.cpp)
ADD_QGIS_TEST(measuretool testqgsmeasuretool.cpp)
ADD_QGIS_TEST(vectorlayersaveasdialogtest testqgsvectorlayersaveasdialog.cpp)
@@ -0,0 +1,154 @@
/***************************************************************************
testqgsmaptoolselect.cpp
--------------------------------
Date : 2016-06-23
Copyright : (C) 2016 by Sandro Santilli
Email : strk@kbt.io
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <QtTest/QtTest>
#include "qgsapplication.h"
#include "qgsvectorlayer.h"
#include "qgsrasterlayer.h"
#include "qgsfeature.h"
#include "qgsgeometry.h"
#include "qgsvectordataprovider.h"
#include "qgsproject.h"
#include "qgsmapcanvas.h"
#include "qgsunittypes.h"
#include "qgsmaptoolselect.h"
#include "qgsmaptoolselectutils.h"

#include "cpl_conv.h"

class TestQgsMapToolSelect : public QObject
{
Q_OBJECT
public:
TestQgsMapToolSelect()
: canvas( 0 )
{}

private slots:
void initTestCase(); // will be called before the first testfunction is executed.
void cleanupTestCase(); // will be called after the last testfunction was executed.
void init(); // will be called before each testfunction is executed.
void cleanup(); // will be called after every testfunction.

void selectInvalidPolygons(); // test selecting invalid polygons

private:
QgsMapCanvas* canvas;

QgsFeatureList testSelectVector( QgsVectorLayer* layer, double xGeoref, double yGeoref );

// Release return with delete []
unsigned char *
hex2bytes( const char *hex, int *size )
{
QByteArray ba = QByteArray::fromHex( hex );
unsigned char *out = new unsigned char[ba.size()];
memcpy( out, ba.data(), ba.size() );
*size = ba.size();
return out;
}

// TODO: make this a QgsGeometry member...
QgsGeometry geomFromHexWKB( const char *hexwkb )
{
int wkbsize;
unsigned char *wkb = hex2bytes( hexwkb, &wkbsize );
QgsGeometry geom;
// NOTE: QgsGeometry takes ownership of wkb
geom.fromWkb( wkb, wkbsize );
return geom;
}

};

void TestQgsMapToolSelect::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
// Set up the QSettings environment
QCoreApplication::setOrganizationName( "QGIS" );
QCoreApplication::setOrganizationDomain( "qgis.org" );
QCoreApplication::setApplicationName( "QGIS-TEST" );

QgsApplication::showSettings();

// enforce C locale because the tests expect it
// (decimal separators / thousand separators)
QLocale::setDefault( QLocale::c() );
}

void TestQgsMapToolSelect::cleanupTestCase()
{
QgsApplication::exitQgis();
}

void TestQgsMapToolSelect::init()
{
canvas = new QgsMapCanvas();
}

void TestQgsMapToolSelect::cleanup()
{
delete canvas;
}

// private
QgsFeatureList
TestQgsMapToolSelect::testSelectVector( QgsVectorLayer* layer, double xGeoref, double yGeoref )
{
QScopedPointer< QgsMapToolSelect > tool( new QgsMapToolSelect( canvas ) );
QgsPoint mapPoint = canvas->getCoordinateTransform()->transform( xGeoref, yGeoref );

// make given vector layer current
canvas->setCurrentLayer( layer );

QScopedPointer< QgsMapMouseEvent > event( new QgsMapMouseEvent(
canvas,
QEvent::MouseButtonRelease,
QPoint( mapPoint.x(), mapPoint.y() )
) );

// trigger mouseRelease handler
tool->canvasReleaseEvent( event.data() );

// return selected features
return layer->selectedFeatures();
}

void TestQgsMapToolSelect::selectInvalidPolygons()
{
//create a temporary layer
QScopedPointer< QgsVectorLayer > memoryLayer( new QgsVectorLayer( "Polygon?field=pk:int", "vl", "memory" ) );
QVERIFY( memoryLayer->isValid() );
QgsFeature f1( memoryLayer->dataProvider()->fields(), 1 );
f1.setAttribute( "pk", 1 );
f1.setGeometry( geomFromHexWKB(
"010300000001000000030000000000000000000000000000000000000000000000000024400000000000000000000000000000244000000000000024400000000000000000"
) );
memoryLayer->dataProvider()->addFeatures( QgsFeatureList() << f1 );

canvas->setExtent( QgsRectangle( 0, 0, 10, 10 ) );
QgsFeatureList selected;
selected = testSelectVector( memoryLayer.data(), 4, 6 );
QCOMPARE( selected.length(), 0 );
selected = testSelectVector( memoryLayer.data(), 6, 4 );
QCOMPARE( selected.length(), 1 );
QCOMPARE( selected[0].attribute( "pk" ), QVariant( 1 ) );

}


QTEST_MAIN( TestQgsMapToolSelect )
#include "testqgsmaptoolselect.moc"

0 comments on commit 3d319d9

Please sign in to comment.
You can’t perform that action at this time.