Skip to content
Permalink
Browse files

Add some tests

  • Loading branch information
pblottiere committed Nov 10, 2017
1 parent c9c1c34 commit 96e597a8e2d71f2840b9ae3025b2736672176686
Showing with 105 additions and 0 deletions.
  1. +3 −0 src/app/qgisapp.cpp
  2. +1 −0 tests/src/app/CMakeLists.txt
  3. +101 −0 tests/src/app/testqgsmaptoolidentifyaction.cpp
@@ -1273,6 +1273,9 @@ QgisApp::QgisApp()
mUndoWidget = new QgsUndoWidget( nullptr, mMapCanvas );
mInfoBar = new QgsMessageBar( centralWidget() );
mAdvancedDigitizingDockWidget = new QgsAdvancedDigitizingDockWidget( mMapCanvas, this );
mPanelMenu = new QMenu( this );
mProgressBar = new QProgressBar( this );
mStatusBar = new QgsStatusBar( this );
// More tests may need more members to be initialized
}

@@ -32,6 +32,7 @@ INCLUDE_DIRECTORIES(
)
INCLUDE_DIRECTORIES(SYSTEM
${QT_INCLUDE_DIR}
${QWT_INCLUDE_DIR}
${GDAL_INCLUDE_DIR}
${PROJ_INCLUDE_DIR}
${GEOS_INCLUDE_DIR}
@@ -26,6 +26,14 @@
#include "qgsunittypes.h"
#include "qgsmaptoolidentifyaction.h"
#include "qgssettings.h"
#include "qgsidentifymenu.h"
#include "qgisapp.h"
#include "qgsaction.h"
#include "qgsactionmanager.h"
#include "qgsactionmenu.h"
#include "qgsidentifyresultsdialog.h"

#include <QTimer>

#include "cpl_conv.h"

@@ -46,9 +54,14 @@ 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 clicked_x and clicked_y variables are propagated

private:
void doAction();

QgsMapCanvas *canvas = nullptr;
QgsMapToolIdentifyAction *mIdentifyAction = nullptr;
QgisApp *mQgisApp = nullptr;

QString testIdentifyRaster( QgsRasterLayer *layer, double xGeoref, double yGeoref );
QList<QgsMapToolIdentify::IdentifyResult> testIdentifyVector( QgsVectorLayer *layer, double xGeoref, double yGeoref );
@@ -91,6 +104,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()
@@ -108,6 +123,92 @@ void TestQgsMapToolIdentifyAction::cleanup()
delete canvas;
}

void TestQgsMapToolIdentifyAction::doAction()
{
bool ok = false;
int clickxOk = 2484588;
int clickyOk = 2425722;

// test QActionMenu
QList<QAction *> actions = mIdentifyAction->identifyMenu()->actions();
bool testDone = false;

for ( int i = 0; i < actions.count(); i++ )
{
if ( actions[i]->text().compare( "MyAction" ) == 0 )
{
QgsActionMenu::ActionData data = actions[i]->data().value<QgsActionMenu::ActionData>();
QgsAction act = data.actionData.value<QgsAction>();

int clickx = act.expressionContextScope().variable( "click_x" ).toString().toInt( &ok, 10 );
QCOMPARE( clickx, clickxOk );

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

testDone = true;
}
}

QCOMPARE( testDone, true );

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

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

// close
mIdentifyAction->identifyMenu()->close();
}

void TestQgsMapToolIdentifyAction::clickxy()
{
// create temp layer
std::unique_ptr< QgsVectorLayer> tempLayer( new QgsVectorLayer( QStringLiteral( "Point?crs=epsg:3111" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) ) );
QVERIFY( tempLayer->isValid() );

// add feature
QgsFeature f1( tempLayer->dataProvider()->fields(), 1 );
QgsPointXY wordPoint( 2484588, 2425722 );
QgsGeometry geom = QgsGeometry::fromPointXY( wordPoint ) ;
f1.setGeometry( geom );
tempLayer->dataProvider()->addFeatures( QgsFeatureList() << f1 );

// prepare canvas
QList<QgsMapLayer *> layers;
layers.append( tempLayer.get() );

QgsCoordinateReferenceSystem srs( 3111, QgsCoordinateReferenceSystem::EpsgCrsId );
canvas->setDestinationCrs( srs );
canvas->setLayers( layers );
canvas->setCurrentLayer( tempLayer.get() );

// create/add action
QgsAction act( QgsAction::GenericPython, "MyAction", "", true );

QSet<QString> scopes;
scopes << "Feature";
act.setActionScopes( scopes );
tempLayer->actions()->addAction( act );

// init map tool identify action
mIdentifyAction = new QgsMapToolIdentifyAction( canvas );

// simulate a click on the canvas
QgsPointXY 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 );

// simulate a click on the corresponding action
QTimer::singleShot( 2000, this, &TestQgsMapToolIdentifyAction::doAction );
mIdentifyAction->canvasReleaseEvent( &mapReleases );
}

void TestQgsMapToolIdentifyAction::lengthCalculation()
{
QgsSettings s;

0 comments on commit 96e597a

Please sign in to comment.
You can’t perform that action at this time.