Skip to content
Permalink
Browse files

Add some tests for circular string map tools

  • Loading branch information
pblottiere committed Feb 16, 2018
1 parent 47ba3a0 commit 916e4bae2ed0ef550f6a3b12a6035a3b9721f903
@@ -1311,6 +1311,7 @@ QgisApp::QgisApp()
mMapCanvas->freeze();
mLayerTreeView = new QgsLayerTreeView( this );
mUndoWidget = new QgsUndoWidget( nullptr, mMapCanvas );
mUserInputDockWidget = new QgsUserInputDockWidget( this );
mInfoBar = new QgsMessageBar( centralWidget() );
mAdvancedDigitizingDockWidget = new QgsAdvancedDigitizingDockWidget( mMapCanvas, this );
mPanelMenu = new QMenu( this );
@@ -17,10 +17,11 @@
#define QGSMAPTOOLADDCIRCULARSTRING_H

#include "qgsmaptoolcapture.h"
#include "qgis_app.h"

class QgsGeometryRubberBand;

class QgsMapToolAddCircularString: public QgsMapToolCapture
class APP_EXPORT QgsMapToolAddCircularString: public QgsMapToolCapture
{
Q_OBJECT
public:
@@ -18,8 +18,9 @@
#define QGSMAPTOOLCIRCULARSTRINGCURVEPOINT_H

#include "qgsmaptooladdcircularstring.h"
#include "qgis_app.h"

class QgsMapToolCircularStringCurvePoint: public QgsMapToolAddCircularString
class APP_EXPORT QgsMapToolCircularStringCurvePoint: public QgsMapToolAddCircularString
{
Q_OBJECT

@@ -19,10 +19,11 @@

#include "qgsmaptooladdcircularstring.h"
#include "qgspoint.h"
#include "qgis_app.h"

class QDoubleSpinBox;

class QgsMapToolCircularStringRadius: public QgsMapToolAddCircularString
class APP_EXPORT QgsMapToolCircularStringRadius: public QgsMapToolAddCircularString
{
Q_OBJECT
public:
@@ -99,6 +99,7 @@ ADD_QGIS_TEST(maptooladdfeature testqgsmaptooladdfeature.cpp)
ADD_QGIS_TEST(maptoolidentifyaction testqgsmaptoolidentifyaction.cpp)
ADD_QGIS_TEST(maptoolselect testqgsmaptoolselect.cpp)
ADD_QGIS_TEST(maptoolreshape testqgsmaptoolreshape.cpp)
ADD_QGIS_TEST(maptoolcircularstringtest testqgsmaptoolcircularstring.cpp)
ADD_QGIS_TEST(measuretool testqgsmeasuretool.cpp)
ADD_QGIS_TEST(vertextool testqgsvertextool.cpp)
ADD_QGIS_TEST(vectorlayersaveasdialogtest testqgsvectorlayersaveasdialog.cpp)
@@ -0,0 +1,133 @@
/***************************************************************************
testqgsmaptoolcircularstring.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 "testqgsmaptoolutils.h"
#include "qgsmaptoolcircularstringcurvepoint.h"
#include "qgsmaptoolcircularstringradius.h"


class TestQgsMapToolCircularString : public QObject
{
Q_OBJECT

public:
TestQgsMapToolCircularString();

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

void testAddCircularStringCurvePoint();
void testAddCircularStringRadius();

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

TestQgsMapToolCircularString::TestQgsMapToolCircularString() = default;


//runs before all tests
void TestQgsMapToolCircularString::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 TestQgsMapToolCircularString::cleanupTestCase()
{
QgsApplication::exitQgis();
}

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

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

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

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

QString wkt = "CompoundCurveZ (CircularStringZ (0 0 333, 1 1 333, 0 2 333))";
QCOMPARE( f.geometry().asWkt(), wkt );

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

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

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

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

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

QString wkt = "CompoundCurveZ (CircularStringZ (0 0 111, 0.17912878474779187 0.82087121525220819 111, 1 1 111))";
QCOMPARE( f.geometry().asWkt(), wkt );

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

QGSTEST_MAIN( TestQgsMapToolCircularString )
#include "testqgsmaptoolcircularstring.moc"
@@ -0,0 +1,99 @@
/***************************************************************************
testqgsmaptoolutils.h
---------------------
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 "qgsvectorlayer.h"

/**
* \ingroup UnitTests
*/
class TestQgsMapToolAdvancedDigitizingUtils
{
public:
TestQgsMapToolAdvancedDigitizingUtils( QgsMapToolAdvancedDigitizing *mapTool )
: mMapTool( mapTool )
{
}

QSet<QgsFeatureId> existingFeatureIds()
{
QSet<QgsFeatureId> fids;
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mMapTool->canvas()->currentLayer() );

if ( vl )
{
QgsFeature f;
QgsFeatureIterator it = vl->getFeatures();
while ( it.nextFeature( f ) )
fids << f.id();
}

return fids;
}

QgsFeatureId newFeatureId( QSet<QgsFeatureId> oldFids = QSet<QgsFeatureId>() )
{
QSet<QgsFeatureId> newFids = existingFeatureIds();
QSet<QgsFeatureId> diffFids = newFids.subtract( oldFids );
Q_ASSERT( diffFids.count() == 1 );
return *diffFids.constBegin();
}

QPoint mapToScreen( double mapX, double mapY )
{
QgsPointXY pt = mMapTool->canvas()->mapSettings().mapToPixel().transform( mapX, mapY );
return QPoint( std::round( pt.x() ), std::round( pt.y() ) );
}

void mouseMove( double mapX, double mapY )
{
QgsMapMouseEvent e( mMapTool->canvas(), QEvent::MouseMove, mapToScreen( mapX, mapY ) );
mMapTool->cadCanvasMoveEvent( &e );
}

void mousePress( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() )
{
QgsMapMouseEvent e1( mMapTool->canvas(), QEvent::MouseButtonPress, mapToScreen( mapX, mapY ), button, button, stateKey );
mMapTool->cadCanvasPressEvent( &e1 );
}

void mouseRelease( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() )
{
QgsMapMouseEvent e2( mMapTool->canvas(), QEvent::MouseButtonRelease, mapToScreen( mapX, mapY ), button, Qt::MouseButton(), stateKey );
mMapTool->cadCanvasReleaseEvent( &e2 );
}

void mouseClick( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() )
{
mousePress( mapX, mapY, button, stateKey );
mouseRelease( mapX, mapY, button, stateKey );
}

void keyClick( int key )
{
QKeyEvent e1( QEvent::KeyPress, key, Qt::KeyboardModifiers() );
mMapTool->keyPressEvent( &e1 );

QKeyEvent e2( QEvent::KeyRelease, key, Qt::KeyboardModifiers() );
mMapTool->keyReleaseEvent( &e2 );
}

private:
QgsMapToolAdvancedDigitizing *mMapTool = nullptr;
};

0 comments on commit 916e4ba

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