Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Nov 20, 2017
1 parent c969295 commit 35459c4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -1190,6 +1190,8 @@ QgisApp::QgisApp()
mLayerTreeView = new QgsLayerTreeView( this );
mUndoWidget = new QgsUndoWidget( nullptr, mMapCanvas );
mInfoBar = new QgsMessageBar( centralWidget() );
mPanelMenu = new QMenu( this );
mProgressBar = new QProgressBar( this );
// More tests may need more members to be initialized
}

Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsmaptoolidentifyaction.h
Expand Up @@ -83,6 +83,8 @@ class APP_EXPORT QgsMapToolIdentifyAction : public QgsMapToolIdentify
virtual QGis::UnitType displayDistanceUnits() const override;
virtual QgsUnitTypes::AreaUnit displayAreaUnits() const override;
void setClickContextScope( const QgsPoint &point );

friend class TestQgsMapToolIdentifyAction;
};

#endif
1 change: 1 addition & 0 deletions tests/src/app/CMakeLists.txt
Expand Up @@ -23,6 +23,7 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
)
INCLUDE_DIRECTORIES(SYSTEM
${QT_INCLUDE_DIR}
${QWT_INCLUDE_DIR}
${GDAL_INCLUDE_DIR}
${PROJ_INCLUDE_DIR}
${GEOS_INCLUDE_DIR}
Expand Down
58 changes: 58 additions & 0 deletions tests/src/app/testqgsmaptoolidentifyaction.cpp
Expand Up @@ -24,6 +24,9 @@
#include "qgsmapcanvas.h"
#include "qgsunittypes.h"
#include "qgsmaptoolidentifyaction.h"
#include "qgisapp.h"
#include "qgsidentifymenu.h"
#include "qgsidentifyresultsdialog.h"

#include "cpl_conv.h"

Expand All @@ -46,9 +49,11 @@ class TestQgsMapToolIdentifyAction : public QObject
void identifyRasterFloat32(); // test pixel identification and decimal precision
void identifyRasterFloat64(); // test pixel identification and decimal precision
void identifyInvalidPolygons(); // test selecting invalid polygons
void clickxy(); // test if click_x and click_y variables are propagated

private:
QgsMapCanvas* canvas;
QgisApp *mQgisApp;

QString testIdentifyRaster( QgsRasterLayer* layer, double xGeoref, double yGeoref );
QList<QgsMapToolIdentify::IdentifyResult> testIdentifyVector( QgsVectorLayer* layer, double xGeoref, double yGeoref );
Expand Down Expand Up @@ -91,6 +96,8 @@ void TestQgsMapToolIdentifyAction::initTestCase()
// enforce C locale because the tests expect it
// (decimal separators / thousand separators)
QLocale::setDefault( QLocale::c() );

mQgisApp = new QgisApp();
}

void TestQgsMapToolIdentifyAction::cleanupTestCase()
Expand All @@ -108,6 +115,57 @@ void TestQgsMapToolIdentifyAction::cleanup()
delete canvas;
}

void TestQgsMapToolIdentifyAction::clickxy()
{
int clickxOk = 2484588;
int clickyOk = 2425722;

// create temp layer
QScopedPointer<QgsVectorLayer> tempLayer( new QgsVectorLayer( QString( "Point?crs=epsg:3111" ), QString( "vl" ), QString( "memory" ) ) );
QVERIFY( tempLayer->isValid() );

// add feature
QgsFeature f1( tempLayer->dataProvider()->fields(), 1 );
QgsPoint wordPoint( clickxOk, clickyOk );
QgsGeometry *geom = QgsGeometry::fromPoint( wordPoint ) ;
f1.setGeometry( geom );
tempLayer->dataProvider()->addFeatures( QgsFeatureList() << f1 );

// prepare canvas
QgsCoordinateReferenceSystem srs( 3111, QgsCoordinateReferenceSystem::EpsgCrsId );
canvas->setDestinationCrs( srs );
canvas->setCurrentLayer( tempLayer.data() );

// init map tool identify action
QScopedPointer<QgsMapToolIdentifyAction> identifyAction( new QgsMapToolIdentifyAction( canvas ) );

// simulate a click on the canvas
QgsPoint mapPoint = canvas->getCoordinateTransform()->transform( 2484588, 2425722 );
QPoint point = QPoint( mapPoint.x(), mapPoint.y() );
QMouseEvent releases( QEvent::MouseButtonRelease, point,
Qt::RightButton, Qt::LeftButton, Qt::NoModifier );
QgsMapMouseEvent mapReleases( 0, &releases );

identifyAction->canvasReleaseEvent( &mapReleases );

// test QgsIdentifyMenu expression context scope
bool ok;
QgsIdentifyMenu *menu = identifyAction->identifyMenu();
int clickx = menu->expressionContextScope().variable( "click_x" ).toString().toInt( &ok, 10 );
QCOMPARE( clickx, clickxOk );

int clicky = menu->expressionContextScope().variable( "click_y" ).toString().toInt( &ok, 10 );
QCOMPARE( clicky, clickyOk );

// test QgsIdentifyResultsDialog expression context scope
QgsIdentifyResultsDialog *dlg = identifyAction->resultsDialog();
clickx = dlg->expressionContextScope().variable( "click_x" ).toString().toInt( &ok, 10 );
QCOMPARE( clickx, clickxOk );

clicky = dlg->expressionContextScope().variable( "click_y" ).toString().toInt( &ok, 10 );
QCOMPARE( clicky, clickyOk );
}

void TestQgsMapToolIdentifyAction::lengthCalculation()
{
QSettings s;
Expand Down

0 comments on commit 35459c4

Please sign in to comment.