|
| 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