Skip to content
Permalink
Browse files
Adds and reorganizes tests for QgsMapToolAddFeaturePoint
  • Loading branch information
lbartoletti committed Apr 30, 2021
1 parent b8bd65c commit 086b5e83f96918d380de5d2b045ba0c1a9244127
@@ -65,8 +65,6 @@ ADD_QGIS_TEST(applocatorfilters testqgsapplocatorfilters.cpp)
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)
@@ -91,3 +89,5 @@ ADD_QGIS_TEST(layoutvaliditychecks testqgsapplayoutvaliditychecks.cpp)
ADD_QGIS_TEST(meshcalculator testqgsmeshcalculatordialog.cpp)
ADD_QGIS_TEST(gpsinformationwidget testqgsgpsinformationwidget.cpp)
ADD_QGIS_TEST(labelpropertydialog testqgslabelpropertydialog.cpp)

add_subdirectory(maptooladdfeaturepoint)
@@ -0,0 +1,61 @@
#####################################################
# Don't forget to include output directory, otherwise
# the UI file won't be wrapped!
get_filename_component(PARENT_DIR ../ ABSOLUTE)

include_directories(
${PARENT_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/src/test
${CMAKE_CURRENT_BINARY_DIR}
)

#note for tests we should not include the moc of our
#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

#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

macro (ADD_QGIS_TEST TESTSRC)
set (TESTNAME ${TESTSRC})
string(REPLACE "test" "" TESTNAME ${TESTNAME})
string(REPLACE "qgs" "qgis_" TESTNAME ${TESTNAME})
string(REPLACE ".cpp" "" TESTNAME ${TESTNAME})
add_executable(${TESTNAME} ${TESTSRC} ${util_SRCS})
target_compile_features(${TESTNAME} PRIVATE cxx_std_17)
target_link_libraries(${TESTNAME}
${Qt5Core_LIBRARIES}
${Qt5Xml_LIBRARIES}
${Qt5Sql_LIBRARIES}
${Qt5Svg_LIBRARIES}
${Qt5Test_LIBRARIES}
${PROJ_LIBRARY}
${GEOS_LIBRARY}
${GDAL_LIBRARY}
${QWT_LIBRARY}
qgis_core
qgis_gui
qgis_analysis
qgis_app)
add_test(${TESTNAME} ${CMAKE_BINARY_DIR}/output/bin/${TESTNAME} -maxwarnings 10000)
#set_target_properties(qgis_${testname} PROPERTIES
# INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/${QGIS_LIB_DIR}
# INSTALL_RPATH_USE_LINK_PATH true )
endmacro (ADD_QGIS_TEST)

#############################################################
# Tests:

set(TESTS
testqgsmaptooladdfeaturepoint.cpp
testqgsmaptooladdfeaturepointz.cpp
testqgsmaptooladdfeaturepointm.cpp
testqgsmaptooladdfeaturepointzm.cpp
)

foreach(TESTSRC ${TESTS})
ADD_QGIS_TEST(${TESTSRC})
endforeach(TESTSRC)
@@ -0,0 +1,142 @@
/***************************************************************************
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 testPoint();

private:
QgisApp *mQgisApp = nullptr;
QgsMapCanvas *mCanvas = nullptr;
QgsMapToolAddFeature *mCaptureTool = nullptr;
QgsVectorLayer *mLayerPoint = 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
mLayerPoint = new QgsVectorLayer( QStringLiteral( "Point?crs=EPSG:27700" ), QStringLiteral( "layer point" ), QStringLiteral( "memory" ) );
QVERIFY( mLayerPoint->isValid() );
QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayerPoint );

mLayerPoint->startEditing();
QgsFeature pointF;
QString pointWkt = "Point(7 7)";
pointF.setGeometry( QgsGeometry::fromWkt( pointWkt ) );

mLayerPoint->addFeature( pointF );
QCOMPARE( mLayerPoint->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::testPoint()
{
TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );
mCanvas->setCurrentLayer( mLayerPoint );

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

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

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

QString wkt = "Point (4 0)";
QCOMPARE( mLayerPoint->getFeature( newFid ).geometry().asWkt(), wkt );

mLayerPoint->undoStack()->undo();

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

wkt = "Point (6 6)";
QCOMPARE( mLayerPoint->getFeature( newFid ).geometry().asWkt(), wkt );

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

QGSTEST_MAIN( TestQgsMapToolAddFeaturePoint )
#include "testqgsmaptooladdfeaturepoint.moc"
@@ -35,11 +35,11 @@
* \ingroup UnitTests
* This is a unit test for the vertex tool
*/
class TestQgsMapToolAddFeaturePoint : public QObject
class TestQgsMapToolAddFeaturePointM : public QObject
{
Q_OBJECT
public:
TestQgsMapToolAddFeaturePoint();
TestQgsMapToolAddFeaturePointM();

private slots:
void initTestCase();// will be called before the first testfunction is executed.
@@ -54,11 +54,11 @@ class TestQgsMapToolAddFeaturePoint : public QObject
QgsVectorLayer *mLayerPointM = nullptr;
};

TestQgsMapToolAddFeaturePoint::TestQgsMapToolAddFeaturePoint() = default;
TestQgsMapToolAddFeaturePointM::TestQgsMapToolAddFeaturePointM() = default;


//runs before all tests
void TestQgsMapToolAddFeaturePoint::initTestCase()
void TestQgsMapToolAddFeaturePointM::initTestCase()
{
qDebug() << "TestMapToolCapture::initTestCase()";
// init QGIS's paths - true means that all path will be inited from prefix
@@ -104,14 +104,14 @@ void TestQgsMapToolAddFeaturePoint::initTestCase()
}

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

void TestQgsMapToolAddFeaturePoint::testPointM()
void TestQgsMapToolAddFeaturePointM::testPointM()
{
TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );
mCanvas->setCurrentLayer( mLayerPointM );
@@ -143,5 +143,5 @@ void TestQgsMapToolAddFeaturePoint::testPointM()
mLayerPointM->undoStack()->undo();
}

QGSTEST_MAIN( TestQgsMapToolAddFeaturePoint )
QGSTEST_MAIN( TestQgsMapToolAddFeaturePointM )
#include "testqgsmaptooladdfeaturepointm.moc"
@@ -1,5 +1,5 @@
/***************************************************************************
testqgsmaptooladdfeaturepoint.cpp
testqgsmaptooladdfeaturepointz.cpp
----------------------
Date : October 2017
Copyright : (C) 2017 by Martin Dobias
@@ -35,11 +35,11 @@
* \ingroup UnitTests
* This is a unit test for the vertex tool
*/
class TestQgsMapToolAddFeaturePoint : public QObject
class TestQgsMapToolAddFeaturePointZ : public QObject
{
Q_OBJECT
public:
TestQgsMapToolAddFeaturePoint();
TestQgsMapToolAddFeaturePointZ();

private slots:
void initTestCase();// will be called before the first testfunction is executed.
@@ -57,11 +57,11 @@ class TestQgsMapToolAddFeaturePoint : public QObject
QgsVectorLayer *mLayerPointZ = nullptr;
};

TestQgsMapToolAddFeaturePoint::TestQgsMapToolAddFeaturePoint() = default;
TestQgsMapToolAddFeaturePointZ::TestQgsMapToolAddFeaturePointZ() = default;


//runs before all tests
void TestQgsMapToolAddFeaturePoint::initTestCase()
void TestQgsMapToolAddFeaturePointZ::initTestCase()
{
qDebug() << "TestMapToolCapture::initTestCase()";
// init QGIS's paths - true means that all path will be inited from prefix
@@ -146,14 +146,14 @@ void TestQgsMapToolAddFeaturePoint::initTestCase()
}

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

void TestQgsMapToolAddFeaturePoint::testPointZ()
void TestQgsMapToolAddFeaturePointZ::testPointZ()
{
TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );

@@ -182,7 +182,7 @@ void TestQgsMapToolAddFeaturePoint::testPointZ()
mLayerPointZ->undoStack()->undo();
}

void TestQgsMapToolAddFeaturePoint::testTopologicalEditingZ()
void TestQgsMapToolAddFeaturePointZ::testTopologicalEditingZ()
{
bool topologicalEditing = QgsProject::instance()->topologicalEditing();
QgsProject::instance()->setTopologicalEditing( true );
@@ -216,5 +216,5 @@ void TestQgsMapToolAddFeaturePoint::testTopologicalEditingZ()

}

QGSTEST_MAIN( TestQgsMapToolAddFeaturePoint )
#include "testqgsmaptooladdfeaturepoint.moc"
QGSTEST_MAIN( TestQgsMapToolAddFeaturePointZ )
#include "testqgsmaptooladdfeaturepointz.moc"

0 comments on commit 086b5e8

Please sign in to comment.