Skip to content

Commit

Permalink
Add some tests for ellipse map tools
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Feb 16, 2018
1 parent 868c869 commit d7860e2
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/app/qgsmaptooladdrectangle.h
Expand Up @@ -19,10 +19,11 @@
#include "qgspolygon.h"
#include "qgsmaptoolcapture.h"
#include "qgsbox3d.h"
#include "qgis_app.h"

class QgsPolygon;

class QgsMapToolAddRectangle: public QgsMapToolCapture
class APP_EXPORT QgsMapToolAddRectangle: public QgsMapToolCapture
{
Q_OBJECT

Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsmaptoolrectangle3points.h
Expand Up @@ -18,8 +18,9 @@
#define QGSMAPTOOLRECTANGLE3POINTS_H

#include "qgsmaptooladdrectangle.h"
#include "qgis_app.h"

class QgsMapToolRectangle3Points: public QgsMapToolAddRectangle
class APP_EXPORT QgsMapToolRectangle3Points: public QgsMapToolAddRectangle
{
Q_OBJECT

Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsmaptoolrectanglecenter.h
Expand Up @@ -18,8 +18,9 @@
#define QGSMAPTOOLRECTANGLECENTER_H

#include "qgsmaptooladdrectangle.h"
#include "qgis_app.h"

class QgsMapToolRectangleCenter: public QgsMapToolAddRectangle
class APP_EXPORT QgsMapToolRectangleCenter: public QgsMapToolAddRectangle
{
Q_OBJECT

Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsmaptoolrectangleextent.h
Expand Up @@ -18,8 +18,9 @@
#define QGSMAPTOOLRECTANGLEEXTENT_H

#include "qgsmaptooladdrectangle.h"
#include "qgis_app.h"

class QgsMapToolRectangleExtent: public QgsMapToolAddRectangle
class APP_EXPORT QgsMapToolRectangleExtent: public QgsMapToolAddRectangle
{
Q_OBJECT

Expand Down
1 change: 1 addition & 0 deletions tests/src/app/CMakeLists.txt
Expand Up @@ -102,6 +102,7 @@ ADD_QGIS_TEST(maptoolreshape testqgsmaptoolreshape.cpp)
ADD_QGIS_TEST(maptoolcircularstringtest testqgsmaptoolcircularstring.cpp)
ADD_QGIS_TEST(maptoolcircletest testqgsmaptoolcircle.cpp)
ADD_QGIS_TEST(maptoolellipsetest testqgsmaptoolellipse.cpp)
ADD_QGIS_TEST(maptoolrectangletest testqgsmaptoolrectangle.cpp)
ADD_QGIS_TEST(measuretool testqgsmeasuretool.cpp)
ADD_QGIS_TEST(vertextool testqgsvertextool.cpp)
ADD_QGIS_TEST(vectorlayersaveasdialogtest testqgsvectorlayersaveasdialog.cpp)
160 changes: 160 additions & 0 deletions tests/src/app/testqgsmaptoolrectangle.cpp
@@ -0,0 +1,160 @@
/***************************************************************************
testqgsmaptoolrectangle.cpp
---------------------------
Date : January 2018
Copyright : (C) 2018 by Paul Blottiere
Email : paul.blottiere@oslandia.com
***************************************************************************
* *
* 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 "qgstest.h"

#include "qgisapp.h"
#include "qgsgeometry.h"
#include "qgsmapcanvas.h"
#include "qgssettings.h"
#include "qgsvectorlayer.h"
#include "qgsmaptooladdfeature.h"
#include "qgsgeometryutils.h"

#include "testqgsmaptoolutils.h"
#include "qgsmaptoolrectanglecenter.h"
#include "qgsmaptoolrectangleextent.h"
#include "qgsmaptoolrectangle3points.h"


class TestQgsMapToolRectangle : public QObject
{
Q_OBJECT

public:
TestQgsMapToolRectangle();

private slots:
void initTestCase();
void cleanupTestCase();

void testRectangleFromCenter();
void testRectangleFromExtent();
void testRectangleFrom3Points();

private:
QgisApp *mQgisApp = nullptr;
QgsMapToolCapture *mParentTool = nullptr;
QgsMapCanvas *mCanvas = nullptr;
QgsVectorLayer *mLayer = nullptr;
};

TestQgsMapToolRectangle::TestQgsMapToolRectangle() = default;


//runs before all tests
void TestQgsMapToolRectangle::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();

mQgisApp = new QgisApp();

mCanvas = new QgsMapCanvas();
mCanvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:27700" ) ) );

// make testing layers
mLayer = new QgsVectorLayer( QStringLiteral( "LineStringZ?crs=EPSG:27700" ), QStringLiteral( "layer line Z" ), QStringLiteral( "memory" ) );
QVERIFY( mLayer->isValid() );
QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayer );

// set layers in canvas
mCanvas->setLayers( QList<QgsMapLayer *>() << mLayer );
mCanvas->setCurrentLayer( mLayer );

mParentTool = new QgsMapToolAddFeature( mCanvas, QgsMapToolCapture::CaptureLine );
}

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

void TestQgsMapToolRectangle::testRectangleFromCenter()
{
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 333 );
mLayer->startEditing();

QgsMapToolRectangleCenter mapTool( mParentTool, mCanvas );
mCanvas->setMapTool( &mapTool );

TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
utils.mouseClick( 0, 0, Qt::LeftButton );
utils.mouseMove( 2, 1 );
utils.mouseClick( 2, 1, Qt::RightButton );
QgsFeatureId newFid = utils.newFeatureId();

QCOMPARE( mLayer->featureCount(), ( long )1 );
QgsFeature f = mLayer->getFeature( newFid );

QString wkt = "LineStringZ (-2 -1 333, -2 1 333, 2 1 333, 2 -1 333, -2 -1 333)";
QCOMPARE( f.geometry().asWkt(), wkt );

mLayer->rollBack();
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
}

void TestQgsMapToolRectangle::testRectangleFromExtent()
{
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 222 );
mLayer->startEditing();

QgsMapToolRectangleExtent mapTool( mParentTool, mCanvas );
mCanvas->setMapTool( &mapTool );

TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
utils.mouseClick( 0, 0, Qt::LeftButton );
utils.mouseMove( 2, 1 );
utils.mouseClick( 2, 1, Qt::RightButton );
QgsFeatureId newFid = utils.newFeatureId();

QCOMPARE( mLayer->featureCount(), ( long )1 );
QgsFeature f = mLayer->getFeature( newFid );

QString wkt = "LineStringZ (0 0 222, 0 1 222, 2 1 222, 2 0 222, 0 0 222)";
QCOMPARE( f.geometry().asWkt(), wkt );

mLayer->rollBack();
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
}

void TestQgsMapToolRectangle::testRectangleFrom3Points()
{
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 111 );
mLayer->startEditing();

QgsMapToolRectangle3Points mapTool( mParentTool, mCanvas );
mCanvas->setMapTool( &mapTool );

TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
utils.mouseClick( 0, 0, Qt::LeftButton );
utils.mouseMove( 2, 0 );
utils.mouseClick( 2, 0, Qt::LeftButton );
utils.mouseMove( 2, 1 );
utils.mouseClick( 2, 1, Qt::RightButton );
QgsFeatureId newFid = utils.newFeatureId();

QCOMPARE( mLayer->featureCount(), ( long )1 );
QgsFeature f = mLayer->getFeature( newFid );

QString wkt = "LineStringZ (0 0 111, 2 0 111, 2 1 111, 0 1 111, 0 0 111)";
QCOMPARE( f.geometry().asWkt( 0 ), wkt );

mLayer->rollBack();
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
}

QGSTEST_MAIN( TestQgsMapToolRectangle )
#include "testqgsmaptoolrectangle.moc"

0 comments on commit d7860e2

Please sign in to comment.