diff --git a/tests/src/app/testqgsmaptooladdfeature.cpp b/tests/src/app/testqgsmaptooladdfeature.cpp index e490fbda3d4..547d6337658 100644 --- a/tests/src/app/testqgsmaptooladdfeature.cpp +++ b/tests/src/app/testqgsmaptooladdfeature.cpp @@ -25,6 +25,7 @@ #include "qgsproject.h" #include "qgssettings.h" #include "qgsvectorlayer.h" +#include "testqgsmaptoolutils.h" bool operator==( const QgsGeometry &g1, const QgsGeometry &g2 ) @@ -45,25 +46,6 @@ namespace QTest } } -static QSet _existingFeatureIds( QgsVectorLayer *layer ) -{ - QSet fids; - QgsFeature f; - QgsFeatureIterator it = layer->getFeatures(); - while ( it.nextFeature( f ) ) - fids << f.id(); - return fids; -} - -static QgsFeatureId _newFeatureId( QgsVectorLayer *layer, QSet oldFids ) -{ - QSet newFids = _existingFeatureIds( layer ); - QSet diffFids = newFids.subtract( oldFids ); - Q_ASSERT( diffFids.count() == 1 ); - return *diffFids.constBegin(); -} - - /** * \ingroup UnitTests @@ -84,46 +66,6 @@ class TestQgsMapToolAddFeature : public QObject void testTracingWithOffset(); void testZ(); - private: - QPoint mapToScreen( double mapX, double mapY ) - { - QgsPointXY pt = mCanvas->mapSettings().mapToPixel().transform( mapX, mapY ); - return QPoint( std::round( pt.x() ), std::round( pt.y() ) ); - } - - void mouseMove( double mapX, double mapY ) - { - QgsMapMouseEvent e( mCanvas, QEvent::MouseMove, mapToScreen( mapX, mapY ) ); - mCaptureTool->cadCanvasMoveEvent( &e ); - } - - void mousePress( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() ) - { - QgsMapMouseEvent e1( mCanvas, QEvent::MouseButtonPress, mapToScreen( mapX, mapY ), button, button, stateKey ); - mCaptureTool->cadCanvasPressEvent( &e1 ); - } - - void mouseRelease( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() ) - { - QgsMapMouseEvent e2( mCanvas, QEvent::MouseButtonRelease, mapToScreen( mapX, mapY ), button, Qt::MouseButton(), stateKey ); - mCaptureTool->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() ); - mCaptureTool->keyPressEvent( &e1 ); - - QKeyEvent e2( QEvent::KeyRelease, key, Qt::KeyboardModifiers() ); - mCaptureTool->keyReleaseEvent( &e2 ); - } - private: QgisApp *mQgisApp = nullptr; QgsMapCanvas *mCanvas = nullptr; @@ -227,15 +169,17 @@ void TestQgsMapToolAddFeature::cleanupTestCase() void TestQgsMapToolAddFeature::testNoTracing() { + TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool ); + // tracing not enabled - will be straight line - QSet oldFids = _existingFeatureIds( mLayerLine ); + QSet oldFids = utils.existingFeatureIds(); - mouseClick( 1, 1, Qt::LeftButton ); - mouseClick( 3, 2, Qt::LeftButton ); - mouseClick( 3, 2, Qt::RightButton ); + utils.mouseClick( 1, 1, Qt::LeftButton ); + utils.mouseClick( 3, 2, Qt::LeftButton ); + utils.mouseClick( 3, 2, Qt::RightButton ); - QgsFeatureId newFid = _newFeatureId( mLayerLine, oldFids ); + QgsFeatureId newFid = utils.newFeatureId( oldFids ); QCOMPARE( mLayerLine->undoStack()->index(), 2 ); QCOMPARE( mLayerLine->getFeature( newFid ).geometry(), QgsGeometry::fromWkt( "LINESTRING(1 1, 3 2)" ) ); @@ -246,17 +190,19 @@ void TestQgsMapToolAddFeature::testNoTracing() void TestQgsMapToolAddFeature::testTracing() { + TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool ); + // tracing enabled - same clicks - now following line mEnableTracingAction->setChecked( true ); - QSet oldFids = _existingFeatureIds( mLayerLine ); + QSet oldFids = utils.existingFeatureIds(); - mouseClick( 1, 1, Qt::LeftButton ); - mouseClick( 3, 2, Qt::LeftButton ); - mouseClick( 3, 2, Qt::RightButton ); + utils.mouseClick( 1, 1, Qt::LeftButton ); + utils.mouseClick( 3, 2, Qt::LeftButton ); + utils.mouseClick( 3, 2, Qt::RightButton ); - QgsFeatureId newFid = _newFeatureId( mLayerLine, oldFids ); + QgsFeatureId newFid = utils.newFeatureId( oldFids ); QCOMPARE( mLayerLine->undoStack()->index(), 2 ); QCOMPARE( mLayerLine->getFeature( newFid ).geometry(), QgsGeometry::fromWkt( "LINESTRING(1 1, 2 1, 3 2)" ) ); @@ -268,13 +214,13 @@ void TestQgsMapToolAddFeature::testTracing() // tracing enabled - combined with first and last segments that are not traced - mouseClick( 0, 2, Qt::LeftButton ); - mouseClick( 1, 1, Qt::LeftButton ); - mouseClick( 3, 2, Qt::LeftButton ); - mouseClick( 4, 1, Qt::LeftButton ); - mouseClick( 4, 1, Qt::RightButton ); + utils.mouseClick( 0, 2, Qt::LeftButton ); + utils.mouseClick( 1, 1, Qt::LeftButton ); + utils.mouseClick( 3, 2, Qt::LeftButton ); + utils.mouseClick( 4, 1, Qt::LeftButton ); + utils.mouseClick( 4, 1, Qt::RightButton ); - QgsFeatureId newFid2 = _newFeatureId( mLayerLine, oldFids ); + QgsFeatureId newFid2 = utils.newFeatureId( oldFids ); QCOMPARE( mLayerLine->undoStack()->index(), 2 ); QCOMPARE( mLayerLine->getFeature( newFid2 ).geometry(), QgsGeometry::fromWkt( "LINESTRING(0 2, 1 1, 2 1, 3 2, 4 1)" ) ); @@ -289,18 +235,20 @@ void TestQgsMapToolAddFeature::testTracing() void TestQgsMapToolAddFeature::testTracingWithOffset() { + TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool ); + // tracing enabled + offset enabled mEnableTracingAction->setChecked( true ); mTracer->setOffset( 0.1 ); - QSet oldFids = _existingFeatureIds( mLayerLine ); + QSet oldFids = utils.existingFeatureIds(); - mouseClick( 2, 1, Qt::LeftButton ); - mouseClick( 1, 2, Qt::LeftButton ); - mouseClick( 1, 2, Qt::RightButton ); + utils.mouseClick( 2, 1, Qt::LeftButton ); + utils.mouseClick( 1, 2, Qt::LeftButton ); + utils.mouseClick( 1, 2, Qt::RightButton ); - QgsFeatureId newFid = _newFeatureId( mLayerLine, oldFids ); + QgsFeatureId newFid = utils.newFeatureId( oldFids ); QCOMPARE( mLayerLine->undoStack()->index(), 2 ); @@ -319,11 +267,11 @@ void TestQgsMapToolAddFeature::testTracingWithOffset() // use negative offset mTracer->setOffset( -0.1 ); - mouseClick( 2, 1, Qt::LeftButton ); - mouseClick( 1, 2, Qt::LeftButton ); - mouseClick( 1, 2, Qt::RightButton ); + utils.mouseClick( 2, 1, Qt::LeftButton ); + utils.mouseClick( 1, 2, Qt::LeftButton ); + utils.mouseClick( 1, 2, Qt::RightButton ); - QgsFeatureId newFid2 = _newFeatureId( mLayerLine, oldFids ); + QgsFeatureId newFid2 = utils.newFeatureId( oldFids ); QgsGeometry g2 = mLayerLine->getFeature( newFid2 ).geometry(); QgsPolylineXY poly2 = g2.asPolyline(); @@ -336,13 +284,13 @@ void TestQgsMapToolAddFeature::testTracingWithOffset() // tracing enabled + offset enabled - combined with first and last segments that are not traced - mouseClick( 3, 0, Qt::LeftButton ); - mouseClick( 2, 1, Qt::LeftButton ); - mouseClick( 1, 2, Qt::LeftButton ); - mouseClick( 0, 1, Qt::LeftButton ); - mouseClick( 0, 1, Qt::RightButton ); + utils.mouseClick( 3, 0, Qt::LeftButton ); + utils.mouseClick( 2, 1, Qt::LeftButton ); + utils.mouseClick( 1, 2, Qt::LeftButton ); + utils.mouseClick( 0, 1, Qt::LeftButton ); + utils.mouseClick( 0, 1, Qt::RightButton ); - QgsFeatureId newFid3 = _newFeatureId( mLayerLine, oldFids ); + QgsFeatureId newFid3 = utils.newFeatureId( oldFids ); QCOMPARE( mLayerLine->undoStack()->index(), 2 ); QgsGeometry g3 = mLayerLine->getFeature( newFid3 ).geometry(); @@ -365,18 +313,20 @@ void TestQgsMapToolAddFeature::testTracingWithOffset() void TestQgsMapToolAddFeature::testZ() { + TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool ); + mCanvas->setCurrentLayer( mLayerLineZ ); // test with default Z value = 333 QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 333 ); - QSet oldFids = _existingFeatureIds( mLayerLineZ ); - mouseClick( 4, 0, Qt::LeftButton ); - mouseClick( 5, 0, Qt::LeftButton ); - mouseClick( 5, 1, Qt::LeftButton ); - mouseClick( 4, 1, Qt::LeftButton ); - mouseClick( 4, 1, Qt::RightButton ); - QgsFeatureId newFid = _newFeatureId( mLayerLineZ, oldFids ); + QSet oldFids = utils.existingFeatureIds(); + utils.mouseClick( 4, 0, Qt::LeftButton ); + utils.mouseClick( 5, 0, Qt::LeftButton ); + utils.mouseClick( 5, 1, Qt::LeftButton ); + utils.mouseClick( 4, 1, Qt::LeftButton ); + utils.mouseClick( 4, 1, Qt::RightButton ); + QgsFeatureId newFid = utils.newFeatureId( oldFids ); QString wkt = "LineStringZ (4 0 333, 5 0 333, 5 1 333, 4 1 333)"; QCOMPARE( mLayerLineZ->getFeature( newFid ).geometry(), QgsGeometry::fromWkt( wkt ) ); @@ -386,13 +336,13 @@ void TestQgsMapToolAddFeature::testZ() // test with default Z value = 222 QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 222 ); - oldFids = _existingFeatureIds( mLayerLineZ ); - mouseClick( 4, 0, Qt::LeftButton ); - mouseClick( 5, 0, Qt::LeftButton ); - mouseClick( 5, 1, Qt::LeftButton ); - mouseClick( 4, 1, Qt::LeftButton ); - mouseClick( 4, 1, Qt::RightButton ); - newFid = _newFeatureId( mLayerLineZ, oldFids ); + oldFids = utils.existingFeatureIds(); + utils.mouseClick( 4, 0, Qt::LeftButton ); + utils.mouseClick( 5, 0, Qt::LeftButton ); + utils.mouseClick( 5, 1, Qt::LeftButton ); + utils.mouseClick( 4, 1, Qt::LeftButton ); + utils.mouseClick( 4, 1, Qt::RightButton ); + newFid = utils.newFeatureId( oldFids ); wkt = "LineStringZ (4 0 222, 5 0 222, 5 1 222, 4 1 222)"; QCOMPARE( mLayerLineZ->getFeature( newFid ).geometry(), QgsGeometry::fromWkt( wkt ) ); diff --git a/tests/src/app/testqgsmaptoolutils.h b/tests/src/app/testqgsmaptoolutils.h index a4ca407eb3c..45f3a025b37 100644 --- a/tests/src/app/testqgsmaptoolutils.h +++ b/tests/src/app/testqgsmaptoolutils.h @@ -2,8 +2,10 @@ testqgsmaptoolutils.h --------------------- Date : January 2018 - Copyright : (C) 2018 by Paul Blottiere - Email : paul.blottiere@oslandia.com + Copyright : (C) 2017 by Martin Dobias + (C) 2018 by Paul Blottiere + Email : wonder dot sk at gmail dot com + paul.blottiere@oslandia.com *************************************************************************** * * * This program is free software; you can redistribute it and/or modify *