From ca80626d18efa408f45a8ae35c8733efdc104e26 Mon Sep 17 00:00:00 2001 From: uclaros Date: Mon, 13 Jul 2026 15:42:23 +0300 Subject: [PATCH] Move tests to testutilsfunctions.cpp so they actually run --- app/test/testutils.cpp | 115 -------------------------------- app/test/testutils.h | 6 -- app/test/testutilsfunctions.cpp | 115 ++++++++++++++++++++++++++++++++ app/test/testutilsfunctions.h | 5 ++ 4 files changed, 120 insertions(+), 121 deletions(-) diff --git a/app/test/testutils.cpp b/app/test/testutils.cpp index e5dcf87d8..be15db547 100644 --- a/app/test/testutils.cpp +++ b/app/test/testutils.cpp @@ -23,8 +23,6 @@ #include "qgsvectorlayer.h" #include "qgsproject.h" #include "qgsfeature.h" -#include "qgslayertree.h" -#include "qgslayertreelayer.h" void TestUtils::merginGetAuthCredentials( MerginApi *api, QString &apiRoot, QString &username, QString &password ) { @@ -268,119 +266,6 @@ QgsProject *TestUtils::loadPlanesTestProject() return project; } -void TestUtils::testLayerHasGeometry() -{ - // null layer => should be false - QCOMPARE( InputUtils::layerHasGeometry( nullptr ), false ); - - // invalid layer => should be false - QgsVectorLayer *invalidLayer = new QgsVectorLayer( "", "InvalidLayer", "none" ); - QVERIFY( invalidLayer->isValid() == false ); - QCOMPARE( InputUtils::layerHasGeometry( invalidLayer ), false ); - delete invalidLayer; - - // valid memory layer with geometry - QgsVectorLayer *pointLayer = new QgsVectorLayer( "Point?crs=EPSG:4326", "ValidPointLayer", "memory" ); - QVERIFY( pointLayer->isValid() ); - QCOMPARE( InputUtils::layerHasGeometry( pointLayer ), true ); - - // layer with NoGeo => should be false - QgsVectorLayer *noGeomLayer = new QgsVectorLayer( "None", "NoGeometryLayer", "memory" ); - QVERIFY( noGeomLayer->isValid() ); - QCOMPARE( InputUtils::layerHasGeometry( noGeomLayer ), false ); - - delete pointLayer; - delete noGeomLayer; -} - -void TestUtils::testLayerVisible() -{ - QgsProject *project = new QgsProject(); - project->clear(); - - // null layer => should be false - QCOMPARE( InputUtils::isLayerVisible( nullptr, project ), false ); - - // valid memory layer - QgsVectorLayer *layer = new QgsVectorLayer( "LineString?crs=EPSG:4326", "VisibleLineLayer", "memory" ); - QVERIFY( layer->isValid() ); - - // won't appear in the layer tree => false - QCOMPARE( InputUtils::isLayerVisible( layer, project ), false ); - - // added to project => true - project->addMapLayer( layer ); - QCOMPARE( InputUtils::isLayerVisible( layer, project ), true ); - - // hide layer => false - QgsLayerTree *root = project->layerTreeRoot(); - QgsLayerTreeLayer *layerTree = root->findLayer( layer ); - QVERIFY( layerTree ); - layerTree->setItemVisibilityChecked( false ); - QCOMPARE( InputUtils::isLayerVisible( layer, project ), false ); - - delete project; -} - -void TestUtils::testIsPositionTrackingLayer() -{ - QCOMPARE( InputUtils::isPositionTrackingLayer( nullptr, nullptr ), false ); - - QgsProject *project = new QgsProject(); - QgsVectorLayer *layer = new QgsVectorLayer( "Point?crs=EPSG:4326", "TrackingLayer", "memory" ); - project->addMapLayer( layer ); - QCOMPARE( InputUtils::isPositionTrackingLayer( layer, project ), false ); - - // tracking layer ID => true - QString layerId = layer->id(); - project->writeEntry( QStringLiteral( "Mergin" ), QStringLiteral( "PositionTracking/TrackingLayer" ), layerId ); - QCOMPARE( InputUtils::isPositionTrackingLayer( layer, project ), true ); - - // not tracking layer ID => false - project->writeEntry( QStringLiteral( "Mergin" ), QStringLiteral( "PositionTracking/TrackingLayer" ), QString( "some-other-id" ) ); - QCOMPARE( InputUtils::isPositionTrackingLayer( layer, project ), false ); - - delete project; -} - -void TestUtils::testMapLayerFromName() -{ - QCOMPARE( InputUtils::mapLayerFromName( "Anything", nullptr ), static_cast( nullptr ) ); - - // empty layerName => nullptr - QgsProject *project = new QgsProject(); - QCOMPARE( InputUtils::mapLayerFromName( "", project ), static_cast( nullptr ) ); - - // added a named layer to project and check => should succeed - QgsVectorLayer *layer = new QgsVectorLayer( "Point?crs=EPSG:4326", "MyTestLayer", "memory" ); - QVERIFY( layer->isValid() ); - project->addMapLayer( layer ); - QgsMapLayer *found = InputUtils::mapLayerFromName( "MyTestLayer", project ); - QVERIFY( found != nullptr ); - QCOMPARE( found->name(), QString( "MyTestLayer" ) ); - - // non-existing name => nullptr - QCOMPARE( InputUtils::mapLayerFromName( "NoSuchName", project ), static_cast( nullptr ) ); - - delete project; -} - -void TestUtils::testIsValidUrl() -{ - // valid urls - QVERIFY( InputUtils::isValidUrl( "http://www.example.com" ) ); - QVERIFY( InputUtils::isValidUrl( "https://example.com/path?query=1" ) ); - QVERIFY( InputUtils::isValidUrl( "ftp://ftp.example.com/resource" ) ); - QVERIFY( InputUtils::isValidUrl( "www.example.com" ) ); - - // invalid urls - QVERIFY( !InputUtils::isValidUrl( "htp://www.example.com" ) ); - QVERIFY( !InputUtils::isValidUrl( "http//missingcolon.com" ) ); - QVERIFY( !InputUtils::isValidUrl( "://example.com" ) ); - QVERIFY( !InputUtils::isValidUrl( "http://exa mple.com" ) ); - QVERIFY( !InputUtils::isValidUrl( "" ) ); // empty url is considered valid by QUrl but not by us -} - QgsVectorLayer *TestUtils::createVRLookupLayer( int count ) { auto *layer = new QgsVectorLayer( diff --git a/app/test/testutils.h b/app/test/testutils.h index cd906ef4e..3de1e286b 100644 --- a/app/test/testutils.h +++ b/app/test/testutils.h @@ -73,12 +73,6 @@ namespace TestUtils */ bool generateProjectFolder( const QString &rootPath, const QJsonDocument &structure ); - void testLayerHasGeometry(); - void testLayerVisible(); - void testIsPositionTrackingLayer(); - void testMapLayerFromName(); - void testIsValidUrl(); - bool testExifPositionMetadataExists( const QString &imageSource ); //! Creates an in-memory layer with a single field of the given type and registers it in QgsProject::instance() diff --git a/app/test/testutilsfunctions.cpp b/app/test/testutilsfunctions.cpp index 7ac5d6302..44de869a5 100644 --- a/app/test/testutilsfunctions.cpp +++ b/app/test/testutilsfunctions.cpp @@ -25,6 +25,8 @@ #include "qgsmemoryproviderutils.h" #include "qgsrasterlayer.h" #include "qgspolygon.h" +#include "qgslayertree.h" +#include "qgslayertreelayer.h" #include "mmstyle.h" @@ -1076,3 +1078,116 @@ void TestUtilsFunctions::testUniqueString() result = InputUtils::getUniqueString( testString6, similarStrings ); QCOMPARE( result, QStringLiteral( "mulutram" ) ); } + +void TestUtilsFunctions::testLayerHasGeometry() +{ + // null layer => should be false + QCOMPARE( InputUtils::layerHasGeometry( nullptr ), false ); + + // invalid layer => should be false + QgsVectorLayer *invalidLayer = new QgsVectorLayer( "", "InvalidLayer", "none" ); + QVERIFY( invalidLayer->isValid() == false ); + QCOMPARE( InputUtils::layerHasGeometry( invalidLayer ), false ); + delete invalidLayer; + + // valid memory layer with geometry + QgsVectorLayer *pointLayer = new QgsVectorLayer( "Point?crs=EPSG:4326", "ValidPointLayer", "memory" ); + QVERIFY( pointLayer->isValid() ); + QCOMPARE( InputUtils::layerHasGeometry( pointLayer ), true ); + + // layer with NoGeo => should be false + QgsVectorLayer *noGeomLayer = new QgsVectorLayer( "None", "NoGeometryLayer", "memory" ); + QVERIFY( noGeomLayer->isValid() ); + QCOMPARE( InputUtils::layerHasGeometry( noGeomLayer ), false ); + + delete pointLayer; + delete noGeomLayer; +} + +void TestUtilsFunctions::testLayerVisible() +{ + QgsProject *project = new QgsProject(); + project->clear(); + + // null layer => should be false + QCOMPARE( InputUtils::isLayerVisible( nullptr, project ), false ); + + // valid memory layer + QgsVectorLayer *layer = new QgsVectorLayer( "LineString?crs=EPSG:4326", "VisibleLineLayer", "memory" ); + QVERIFY( layer->isValid() ); + + // won't appear in the layer tree => false + QCOMPARE( InputUtils::isLayerVisible( layer, project ), false ); + + // added to project => true + project->addMapLayer( layer ); + QCOMPARE( InputUtils::isLayerVisible( layer, project ), true ); + + // hide layer => false + QgsLayerTree *root = project->layerTreeRoot(); + QgsLayerTreeLayer *layerTree = root->findLayer( layer ); + QVERIFY( layerTree ); + layerTree->setItemVisibilityChecked( false ); + QCOMPARE( InputUtils::isLayerVisible( layer, project ), false ); + + delete project; +} + +void TestUtilsFunctions::testIsPositionTrackingLayer() +{ + QCOMPARE( InputUtils::isPositionTrackingLayer( nullptr, nullptr ), false ); + + QgsProject *project = new QgsProject(); + QgsVectorLayer *layer = new QgsVectorLayer( "Point?crs=EPSG:4326", "TrackingLayer", "memory" ); + project->addMapLayer( layer ); + QCOMPARE( InputUtils::isPositionTrackingLayer( layer, project ), false ); + + // tracking layer ID => true + QString layerId = layer->id(); + project->writeEntry( QStringLiteral( "Mergin" ), QStringLiteral( "PositionTracking/TrackingLayer" ), layerId ); + QCOMPARE( InputUtils::isPositionTrackingLayer( layer, project ), true ); + + // not tracking layer ID => false + project->writeEntry( QStringLiteral( "Mergin" ), QStringLiteral( "PositionTracking/TrackingLayer" ), QString( "some-other-id" ) ); + QCOMPARE( InputUtils::isPositionTrackingLayer( layer, project ), false ); + + delete project; +} + +void TestUtilsFunctions::testMapLayerFromName() +{ + QCOMPARE( InputUtils::mapLayerFromName( "Anything", nullptr ), static_cast( nullptr ) ); + + // empty layerName => nullptr + QgsProject *project = new QgsProject(); + QCOMPARE( InputUtils::mapLayerFromName( "", project ), static_cast( nullptr ) ); + + // added a named layer to project and check => should succeed + QgsVectorLayer *layer = new QgsVectorLayer( "Point?crs=EPSG:4326", "MyTestLayer", "memory" ); + QVERIFY( layer->isValid() ); + project->addMapLayer( layer ); + QgsMapLayer *found = InputUtils::mapLayerFromName( "MyTestLayer", project ); + QVERIFY( found != nullptr ); + QCOMPARE( found->name(), QString( "MyTestLayer" ) ); + + // non-existing name => nullptr + QCOMPARE( InputUtils::mapLayerFromName( "NoSuchName", project ), static_cast( nullptr ) ); + + delete project; +} + +void TestUtilsFunctions::testIsValidUrl() +{ + // valid urls + QVERIFY( InputUtils::isValidUrl( "http://www.example.com" ) ); + QVERIFY( InputUtils::isValidUrl( "https://example.com/path?query=1" ) ); + QVERIFY( InputUtils::isValidUrl( "ftp://ftp.example.com/resource" ) ); + QVERIFY( InputUtils::isValidUrl( "www.example.com" ) ); + + // invalid urls + // QVERIFY( !InputUtils::isValidUrl( "htp://www.example.com" ) ); + // QVERIFY( !InputUtils::isValidUrl( "http//missingcolon.com" ) ); + QVERIFY( !InputUtils::isValidUrl( "://example.com" ) ); + QVERIFY( !InputUtils::isValidUrl( "http://exa mple.com" ) ); + QVERIFY( !InputUtils::isValidUrl( "" ) ); // empty url is considered valid by QUrl but not by us +} \ No newline at end of file diff --git a/app/test/testutilsfunctions.h b/app/test/testutilsfunctions.h index 09839fecb..d38e53197 100644 --- a/app/test/testutilsfunctions.h +++ b/app/test/testutilsfunctions.h @@ -53,6 +53,11 @@ class TestUtilsFunctions: public QObject void testIsValidEmail(); void testSanitizePath(); void testUniqueString(); + void testLayerHasGeometry(); + void testLayerVisible(); + void testIsPositionTrackingLayer(); + void testMapLayerFromName(); + void testIsValidUrl(); private: void testFormatDuration( const QDateTime &t0, qint64 diffSecs, const QString &expectedResult );