Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQDQ authored and nyalldawson committed Jan 24, 2023
1 parent 44861d6 commit 67a61ba
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app/qgsmeasuretool.h
Expand Up @@ -99,6 +99,8 @@ class APP_EXPORT QgsMeasureTool : public QgsMapTool

//! Removes the last vertex from mRubberBand
void undo();

friend class TestQgsMeasureTool;
};

#endif
61 changes: 61 additions & 0 deletions tests/src/app/testqgsmeasuretool.cpp
Expand Up @@ -24,6 +24,7 @@
#include "qgsproject.h"
#include "qgsmapcanvas.h"
#include "qgsunittypes.h"
#include "qgsrubberband.h"

/**
* \ingroup UnitTests
Expand All @@ -46,6 +47,7 @@ class TestQgsMeasureTool : public QObject
void testAreaCalculationCartesian();
void testAreaCalculationProjected();
void degreeDecimalPlaces();
void testToolDesactivationNoExtraPoint();

private:
QgisApp *mQgisApp = nullptr;
Expand Down Expand Up @@ -373,5 +375,64 @@ void TestQgsMeasureTool::degreeDecimalPlaces()

}

void TestQgsMeasureTool::testToolDesactivationNoExtraPoint()
{

// set project CRS and ellipsoid
const QgsCoordinateReferenceSystem srs( QStringLiteral( "EPSG:3111" ) );
mCanvas->setDestinationCrs( srs );
QgsProject::instance()->setCrs( srs );
QgsProject::instance()->setEllipsoid( QStringLiteral( "WGS84" ) );
QgsProject::instance()->setAreaUnits( QgsUnitTypes::AreaSquareMeters );

// run length calculation
std::unique_ptr< QgsMeasureTool > tool( new QgsMeasureTool( mCanvas, true ) );
std::unique_ptr< QgsMeasureDialog > dlg( new QgsMeasureDialog( tool.get() ) );

dlg->mEllipsoidal->setChecked( true );

auto moveCanvas = [this]( int x, int y, int delay = 0 )
{
auto widget = mCanvas->viewport();
QTest::mouseMove( widget, QPoint( x, y ), delay );
};

auto clickCanvas = [this]( int x, int y )
{
auto widget = mCanvas->viewport();
QTest::mouseMove( widget, QPoint( x, y ) );
QTest::mouseClick( mCanvas->viewport(), Qt::LeftButton, Qt::NoModifier, QPoint( x, y ), 50 );
};

mCanvas->setMapTool( tool.get() );


// Click on two points, then move the cursor
clickCanvas( 50, 50 );
clickCanvas( 50, 100 );
moveCanvas( 150, 150, 50 );

// Check number of vertices in the rubberbands
// The points Rubberband should have one vertice per click
QCOMPARE( tool->mRubberBandPoints->numberOfVertices(), 2 );
// The temp rubberband should have one more
QCOMPARE( tool->mRubberBand->numberOfVertices(), tool->mRubberBandPoints->numberOfVertices() + 1 );

// Deactivate & reactivate the tool, then move mouse cursor
mCanvas->unsetMapTool( tool.get() );
mCanvas->setMapTool( tool.get() );
moveCanvas( 100, 100, 50 );

// Check if both rubberbands still have the right number of vertices
QCOMPARE( tool->mRubberBandPoints->numberOfVertices(), 2 );
QCOMPARE( tool->mRubberBand->numberOfVertices(), tool->mRubberBandPoints->numberOfVertices() + 1 );



}




QGSTEST_MAIN( TestQgsMeasureTool )
#include "testqgsmeasuretool.moc"

0 comments on commit 67a61ba

Please sign in to comment.