Skip to content
Permalink
Browse files
Add a dedicated test for pointM avoid a crash in testsuite
  • Loading branch information
lbartoletti committed Apr 30, 2021
1 parent d5629fc commit 6aa709512d9b12cf123708c91c5cd03e55e404e3
@@ -66,6 +66,7 @@ ADD_QGIS_TEST(decorationscalebar testqgsdecorationscalebar.cpp)
ADD_QGIS_TEST(fieldcalculatortest testqgsfieldcalculator.cpp)
ADD_QGIS_TEST(maptooladdfeatureline testqgsmaptooladdfeatureline.cpp)
ADD_QGIS_TEST(maptooladdfeaturepoint testqgsmaptooladdfeaturepoint.cpp)
ADD_QGIS_TEST(maptooladdfeaturepointm testqgsmaptooladdfeaturepointm.cpp)
ADD_QGIS_TEST(maptoolidentifyaction testqgsmaptoolidentifyaction.cpp)
ADD_QGIS_TEST(maptoollabel testqgsmaptoollabel.cpp)
ADD_QGIS_TEST(maptoolselect testqgsmaptoolselect.cpp)
@@ -45,7 +45,6 @@ class TestQgsMapToolAddFeaturePoint : public QObject
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.

void testPointM();
void testPointZ();
void testTopologicalEditingZ();

@@ -56,7 +55,6 @@ class TestQgsMapToolAddFeaturePoint : public QObject
QgsVectorLayer *mLayerPointZSnap = nullptr;
QgsVectorLayer *mLayerLineZSnap = nullptr;
QgsVectorLayer *mLayerPointZ = nullptr;
QgsVectorLayer *mLayerPointM = nullptr;
};

TestQgsMapToolAddFeaturePoint::TestQgsMapToolAddFeaturePoint() = default;
@@ -100,17 +98,6 @@ void TestQgsMapToolAddFeaturePoint::initTestCase()
mLayerPointZ->addFeature( pointFZ );
QCOMPARE( mLayerPointZ->featureCount(), ( long )1 );

mLayerPointM = new QgsVectorLayer( QStringLiteral( "PointM?crs=EPSG:27700" ), QStringLiteral( "layer point M" ), QStringLiteral( "memory" ) );
QVERIFY( mLayerPointM->isValid() );
QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayerPointM );
mLayerPointM->startEditing();
QgsFeature pointFM;
QString pointWktM = "PointM(7 7 4)";
pointFM.setGeometry( QgsGeometry::fromWkt( pointWktM ) );

mLayerPointM->addFeature( pointFM );
QCOMPARE( mLayerPointM->featureCount(), ( long )1 );

// make layer for snapping
mLayerPointZSnap = new QgsVectorLayer( QStringLiteral( "PointZ?crs=EPSG:27700" ), QStringLiteral( "Snap point" ), QStringLiteral( "memory" ) );
QVERIFY( mLayerPointZSnap->isValid() );
@@ -143,11 +130,10 @@ void TestQgsMapToolAddFeaturePoint::initTestCase()
cfg.setEnabled( true );
mCanvas->snappingUtils()->setConfig( cfg );

mCanvas->setLayers( QList<QgsMapLayer *>() << mLayerPointZ << mLayerPointM << mLayerPointZSnap << mLayerLineZSnap );
mCanvas->setCurrentLayer( mLayerPointM );
mCanvas->setLayers( QList<QgsMapLayer *>() << mLayerPointZ << mLayerPointZSnap << mLayerLineZSnap );
mCanvas->setCurrentLayer( mLayerPointZ );

mCanvas->snappingUtils()->locatorForLayer( mLayerPointZ )->init();
mCanvas->snappingUtils()->locatorForLayer( mLayerPointM )->init();
mCanvas->snappingUtils()->locatorForLayer( mLayerPointZSnap )->init();
mCanvas->snappingUtils()->locatorForLayer( mLayerLineZSnap )->init();

@@ -167,45 +153,9 @@ void TestQgsMapToolAddFeaturePoint::cleanupTestCase()
QgsApplication::exitQgis();
}

void TestQgsMapToolAddFeaturePoint::testPointM()
{
mCanvas->setCurrentLayer( mLayerPointM );

TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );

// test with default M value = 333
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_m_value" ), 333 );

QSet<QgsFeatureId> oldFids = utils.existingFeatureIds();

utils.mouseClick( 4, 0, Qt::LeftButton, Qt::KeyboardModifiers(), true );
QgsFeatureId newFid = utils.newFeatureId( oldFids );

QCOMPARE( mLayerPointM->featureCount(), ( long )2 );

QString wkt = "PointM (4 0 333)";
QCOMPARE( mLayerPointM->getFeature( newFid ).geometry().asWkt(), wkt );

mLayerPointM->undoStack()->undo();

oldFids = utils.existingFeatureIds();
utils.mouseClick( 6, 6, Qt::LeftButton, Qt::KeyboardModifiers(), true );
newFid = utils.newFeatureId( oldFids );

wkt = "PointM (6 6 333)";
QCOMPARE( mLayerPointM->getFeature( newFid ).geometry().asWkt(), wkt );

mLayerPointM->undoStack()->undo();
mLayerPointM->rollBack();

mCanvas->setCurrentLayer( mLayerPointZ );
}


void TestQgsMapToolAddFeaturePoint::testPointZ()
{
TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );
mCanvas->setCurrentLayer( mLayerPointZ );

// test with default Z value = 333
QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 333 );
@@ -0,0 +1,147 @@
/***************************************************************************
testqgsmaptooladdfeaturepointm.cpp
----------------------
Date : April 2021
Copyright : (C) 2021 by Loïc Bartoletti
Email : loic dot bartoletti at oslandia dot 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 "qgsadvanceddigitizingdockwidget.h"
#include "qgsgeometry.h"
#include "qgsmapcanvas.h"
#include "qgsmapcanvassnappingutils.h"
#include "qgssnappingconfig.h"
#include "qgssnappingutils.h"
#include "qgsmaptooladdfeature.h"
#include "qgsmapcanvastracer.h"
#include "qgsproject.h"
#include "qgssettings.h"
#include "qgsvectorlayer.h"
#include "qgsmapmouseevent.h"
#include "testqgsmaptoolutils.h"


/**
* \ingroup UnitTests
* This is a unit test for the vertex tool
*/
class TestQgsMapToolAddFeaturePoint : public QObject
{
Q_OBJECT
public:
TestQgsMapToolAddFeaturePoint();

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 testPointM();

private:
QgisApp *mQgisApp = nullptr;
QgsMapCanvas *mCanvas = nullptr;
QgsMapToolAddFeature *mCaptureTool = nullptr;
QgsVectorLayer *mLayerPointM = nullptr;
};

TestQgsMapToolAddFeaturePoint::TestQgsMapToolAddFeaturePoint() = default;


//runs before all tests
void TestQgsMapToolAddFeaturePoint::initTestCase()
{
qDebug() << "TestMapToolCapture::initTestCase()";
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();

// Set up the QSettings environment
QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );

mQgisApp = new QgisApp();

mCanvas = new QgsMapCanvas();

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

mCanvas->setFrameStyle( QFrame::NoFrame );
mCanvas->resize( 512, 512 );
mCanvas->setExtent( QgsRectangle( 0, 0, 8, 8 ) );
mCanvas->show(); // to make the canvas resize
mCanvas->hide();

// make testing M layer
mLayerPointM = new QgsVectorLayer( QStringLiteral( "PointM?crs=EPSG:27700" ), QStringLiteral( "layer point M" ), QStringLiteral( "memory" ) );
QVERIFY( mLayerPointM->isValid() );
QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayerPointM );

mLayerPointM->startEditing();
QgsFeature pointFM;
QString pointWktM = "PointM(7 7 4)";
pointFM.setGeometry( QgsGeometry::fromWkt( pointWktM ) );

mLayerPointM->addFeature( pointFM );
QCOMPARE( mLayerPointM->featureCount(), ( long )1 );

// create the tool
mCaptureTool = new QgsMapToolAddFeature( mCanvas, /*mAdvancedDigitizingDockWidget, */ QgsMapToolCapture::CapturePoint );
mCanvas->setMapTool( mCaptureTool );

QCOMPARE( mCanvas->mapSettings().outputSize(), QSize( 512, 512 ) );
QCOMPARE( mCanvas->mapSettings().visibleExtent(), QgsRectangle( 0, 0, 8, 8 ) );
}

//runs after all tests
void TestQgsMapToolAddFeaturePoint::cleanupTestCase()
{
delete mCaptureTool;
delete mCanvas;
QgsApplication::exitQgis();
}

void TestQgsMapToolAddFeaturePoint::testPointM()
{
TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );
mCanvas->setCurrentLayer( mLayerPointM );
// test with default M value = 333
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_m_value" ), 333 );

QSet<QgsFeatureId> oldFids = utils.existingFeatureIds();

utils.mouseClick( 4, 0, Qt::LeftButton, Qt::KeyboardModifiers(), true );
QgsFeatureId newFid = utils.newFeatureId( oldFids );

QCOMPARE( mLayerPointM->featureCount(), ( long )2 );

QString wkt = "PointM (4 0 333)";
QCOMPARE( mLayerPointM->getFeature( newFid ).geometry().asWkt(), wkt );

mLayerPointM->undoStack()->undo();

// test with default M value = 123
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_m_value" ), 123 );

oldFids = utils.existingFeatureIds();
utils.mouseClick( 6, 6, Qt::LeftButton, Qt::KeyboardModifiers(), true );
newFid = utils.newFeatureId( oldFids );

wkt = "PointM (6 6 123)";
QCOMPARE( mLayerPointM->getFeature( newFid ).geometry().asWkt(), wkt );

mLayerPointM->undoStack()->undo();
}

QGSTEST_MAIN( TestQgsMapToolAddFeaturePoint )
#include "testqgsmaptooladdfeaturepointm.moc"

0 comments on commit 6aa7095

Please sign in to comment.