Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 7, 2022
1 parent d0b4bcf commit a4b3616
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -1964,6 +1964,8 @@ QgisApp::QgisApp()
mDigitizingTechniqueManager = new QgsMapToolsDigitizingTechniqueManager( this );

mBearingNumericFormat.reset( QgsLocalDefaultSettings::bearingFormat() );

connect( mLayerTreeView, &QgsLayerTreeView::currentLayerChanged, this, &QgisApp::onActiveLayerChanged );
// More tests may need more members to be initialized
}

Expand Down
65 changes: 50 additions & 15 deletions tests/src/app/testqgsgpsintegration.cpp
Expand Up @@ -22,10 +22,10 @@
#include "qgsmapcanvas.h"
#include "qgssettingsregistrycore.h"
#include "qgsappgpsconnection.h"
#include "gps/qgsgpsinformationwidget.h"
#include "gps/qgsappgpsdigitizing.h"
#include "gps/qgsappgpssettingsmenu.h"
#include "options/qgsgpsoptions.h"
#include "qgsprojectgpssettings.h"
#include "nmeatime.h"

#include <QSignalSpy>
Expand All @@ -49,6 +49,7 @@ class TestQgsGpsIntegration : public QgsTest
void testTimestamp();
void testTimestampWrite();
void testMultiPartLayers();
void testFollowActiveLayer();

private:
QDateTime _testWrite( QgsVectorLayer *vlayer, QgsAppGpsDigitizing &gpsDigitizing, const QString &fieldName, Qt::TimeSpec timeSpec, bool commit = false );
Expand Down Expand Up @@ -109,7 +110,7 @@ void TestQgsGpsIntegration::cleanupTestCase()

QDateTime TestQgsGpsIntegration::_testWrite( QgsVectorLayer *vlayer, QgsAppGpsDigitizing &gpsDigitizing, const QString &fieldName, Qt::TimeSpec timeSpec, bool commit )
{
mQgisApp->mapCanvas()->setCurrentLayer( vlayer );
mQgisApp->setActiveLayer( vlayer );
vlayer->startEditing();
gpsDigitizing.setTimeStampDestination( fieldName );
gpsDigitizing.mTimeStampSpec = timeSpec;
Expand All @@ -127,6 +128,8 @@ QDateTime TestQgsGpsIntegration::_testWrite( QgsVectorLayer *vlayer, QgsAppGpsDi
void TestQgsGpsIntegration::testTimeStampFields()
{
QgsAppGpsSettingsMenu settingsMenu( nullptr );
QgsAppGpsConnection connection( nullptr );
QgsAppGpsDigitizing gpsDigitizing( &connection, mQgisApp->mapCanvas() );
QSignalSpy spy( &settingsMenu, &QgsAppGpsSettingsMenu::timeStampDestinationChanged );

mQgisApp->setActiveLayer( tempLayer );
Expand Down Expand Up @@ -189,26 +192,26 @@ void TestQgsGpsIntegration::testStorePreferredFields()
QgsAppGpsSettingsMenu menu( nullptr );
QgsAppGpsDigitizing gpsDigitizing( &connection, canvas );

canvas->setCurrentLayer( tempLayerString );
mQgisApp->setActiveLayer( tempLayerString );
gpsDigitizing.setTimeStampDestination( QStringLiteral( "stringf" ) );
canvas->setCurrentLayer( tempLayerDateTime );
mQgisApp->setActiveLayer( tempLayerDateTime );
gpsDigitizing.setTimeStampDestination( QStringLiteral( "datetimef" ) );
canvas->setCurrentLayer( tempLayerLineString );
mQgisApp->setActiveLayer( tempLayerLineString );
gpsDigitizing.setTimeStampDestination( QStringLiteral( "stringf" ) );
canvas->setCurrentLayer( tempGpkgLayerPointString );
mQgisApp->setActiveLayer( tempGpkgLayerPointString );
gpsDigitizing.setTimeStampDestination( QStringLiteral( "datetimef" ) );

canvas->setCurrentLayer( tempLayer );
mQgisApp->setActiveLayer( tempLayer );
QVERIFY( gpsDigitizing.mPreferredTimestampFields.contains( tempLayerString->id() ) );
QCOMPARE( gpsDigitizing.mPreferredTimestampFields[ tempLayerString->id() ], QStringLiteral( "stringf" ) );
QVERIFY( gpsDigitizing.mPreferredTimestampFields.contains( tempLayerDateTime->id() ) );
QCOMPARE( gpsDigitizing.mPreferredTimestampFields[ tempLayerDateTime->id() ], QStringLiteral( "datetimef" ) );

QSignalSpy spy( &gpsDigitizing, &QgsAppGpsDigitizing::timeStampDestinationChanged );
canvas->setCurrentLayer( tempLayerDateTime );
mQgisApp->setActiveLayer( tempLayerDateTime );
QCOMPARE( spy.count(), 1 );
QCOMPARE( spy.at( 0 ).at( 0 ).toString(), QStringLiteral( "datetimef" ) );
canvas->setCurrentLayer( tempLayerString );
mQgisApp->setActiveLayer( tempLayerString );
QCOMPARE( spy.count(), 2 );
QCOMPARE( spy.at( 1 ).at( 0 ).toString(), QStringLiteral( "stringf" ) );
}
Expand All @@ -232,7 +235,7 @@ void TestQgsGpsIntegration::testTimestamp()

///////////////////////////////////////////
// Test datetime layer
canvas->setCurrentLayer( tempLayerDateTime );
mQgisApp->setActiveLayer( tempLayerDateTime );
int fieldIdx = tempLayerDateTime->fields().indexOf( QLatin1String( "datetimef" ) );
gpsDigitizing.setTimeStampDestination( QStringLiteral( "datetimef" ) );
// UTC
Expand All @@ -253,7 +256,7 @@ void TestQgsGpsIntegration::testTimestamp()

///////////////////////////////////////////
// Test string
canvas->setCurrentLayer( tempLayerString );
mQgisApp->setActiveLayer( tempLayerString );
fieldIdx = tempLayerString->fields().indexOf( QLatin1String( "stringf" ) );
gpsDigitizing.setTimeStampDestination( QStringLiteral( "stringf" ) );

Expand Down Expand Up @@ -333,15 +336,17 @@ void TestQgsGpsIntegration::testTimestampWrite()

void TestQgsGpsIntegration::testMultiPartLayers()
{
std::unique_ptr< QgsVectorLayer >multiLineString = std::make_unique< QgsVectorLayer >( QStringLiteral( "MultiLinestring?crs=epsg:4326&field=intf:int&field=stringf:string" ),
QgsVectorLayer *multiLineString = new QgsVectorLayer( QStringLiteral( "MultiLinestring?crs=epsg:4326&field=intf:int&field=stringf:string" ),
QStringLiteral( "vl4" ),
QStringLiteral( "memory" ) );
QgsProject::instance()->addMapLayer( multiLineString );

QgsMapCanvas *canvas = mQgisApp->mapCanvas();
QgsAppGpsConnection connection( nullptr );

QgsAppGpsDigitizing gpsDigitizing( &connection, canvas );
canvas->setCurrentLayer( multiLineString.get() );
mQgisApp->setActiveLayer( multiLineString );
QCOMPARE( QgsProject::instance()->gpsSettings()->destinationLayer(), multiLineString );
multiLineString->startEditing();

// not possible, no points
Expand All @@ -361,11 +366,14 @@ void TestQgsGpsIntegration::testMultiPartLayers()
multiLineString->rollBack();

// multipolygon
std::unique_ptr< QgsVectorLayer >multiPolygon = std::make_unique< QgsVectorLayer >( QStringLiteral( "MultiPolygon?crs=epsg:4326&field=intf:int&field=stringf:string" ),
QgsVectorLayer *multiPolygon = new QgsVectorLayer( QStringLiteral( "MultiPolygon?crs=epsg:4326&field=intf:int&field=stringf:string" ),
QStringLiteral( "vl4" ),
QStringLiteral( "memory" ) );

canvas->setCurrentLayer( multiPolygon.get() );
QgsProject::instance()->addMapLayer( multiPolygon );
mQgisApp->setActiveLayer( multiPolygon );
QCOMPARE( QgsProject::instance()->gpsSettings()->destinationLayer(), multiPolygon );

multiPolygon->startEditing();

// not possible, no points
Expand All @@ -388,6 +396,33 @@ void TestQgsGpsIntegration::testMultiPartLayers()
multiPolygon->rollBack();
}

void TestQgsGpsIntegration::testFollowActiveLayer()
{
QgsProject::instance()->gpsSettings()->setDestinationFollowsActiveLayer( true );

QgsMapCanvas *canvas = mQgisApp->mapCanvas();
QgsAppGpsConnection connection( nullptr );

QgsAppGpsSettingsMenu menu( nullptr );
QgsAppGpsDigitizing gpsDigitizing( &connection, canvas );

mQgisApp->setActiveLayer( tempLayerString );
QCOMPARE( QgsProject::instance()->gpsSettings()->destinationLayer(), tempLayerString );

mQgisApp->setActiveLayer( tempLayerDateTime );
QCOMPARE( QgsProject::instance()->gpsSettings()->destinationLayer(), tempLayerDateTime );

// disable auto follow active layer
QgsProject::instance()->gpsSettings()->setDestinationFollowsActiveLayer( false );

QgsProject::instance()->gpsSettings()->setDestinationLayer( tempLayerLineString );
QCOMPARE( QgsProject::instance()->gpsSettings()->destinationLayer(), tempLayerLineString );

mQgisApp->setActiveLayer( tempLayerString );
//should be unchanged
QCOMPARE( QgsProject::instance()->gpsSettings()->destinationLayer(), tempLayerLineString );
}


QGSTEST_MAIN( TestQgsGpsIntegration )
#include "testqgsgpsintegration.moc"

0 comments on commit a4b3616

Please sign in to comment.