|
17 | 17 | #include "inpututils.h" |
18 | 18 | #include "merginapi.h" |
19 | 19 |
|
| 20 | +#include "qgsvectorlayer.h" |
| 21 | +#include "qgsproject.h" |
| 22 | +#include "qgslayertree.h" |
| 23 | +#include "qgslayertreelayer.h" |
| 24 | + |
20 | 25 | void TestUtils::merginGetAuthCredentials( MerginApi *api, QString &apiRoot, QString &username, QString &password ) |
21 | 26 | { |
22 | 27 | Q_ASSERT( api ); |
@@ -183,3 +188,139 @@ QgsProject *TestUtils::loadPlanesTestProject() |
183 | 188 |
|
184 | 189 | return project; |
185 | 190 | } |
| 191 | + |
| 192 | +void TestUtils::testLayerHasGeometry() |
| 193 | +{ |
| 194 | + // null layer => should be false |
| 195 | + QCOMPARE( InputUtils::layerHasGeometry( nullptr ), false ); |
| 196 | + |
| 197 | + // invalid layer => should be false |
| 198 | + QgsVectorLayer *invalidLayer = new QgsVectorLayer( "", "InvalidLayer", "none" ); |
| 199 | + QVERIFY( invalidLayer->isValid() == false ); |
| 200 | + QCOMPARE( InputUtils::layerHasGeometry( invalidLayer ), false ); |
| 201 | + delete invalidLayer; |
| 202 | + |
| 203 | + // valid memory layer with geometry |
| 204 | + QgsVectorLayer *pointLayer = new QgsVectorLayer( "Point?crs=EPSG:4326", "ValidPointLayer", "memory" ); |
| 205 | + QVERIFY( pointLayer->isValid() ); |
| 206 | + QCOMPARE( InputUtils::layerHasGeometry( pointLayer ), true ); |
| 207 | + |
| 208 | + // layer with NoGeo => should be false |
| 209 | + QgsVectorLayer *noGeomLayer = new QgsVectorLayer( "None", "NoGeometryLayer", "memory" ); |
| 210 | + QVERIFY( noGeomLayer->isValid() ); |
| 211 | + QCOMPARE( InputUtils::layerHasGeometry( noGeomLayer ), false ); |
| 212 | + |
| 213 | + delete pointLayer; |
| 214 | + delete noGeomLayer; |
| 215 | +} |
| 216 | + |
| 217 | +void TestUtils::testLayerVisible() |
| 218 | +{ |
| 219 | + // null layer => should be false |
| 220 | + QCOMPARE( InputUtils::layerVisible( nullptr ), false ); |
| 221 | + |
| 222 | + QgsProject *project = new QgsProject(); |
| 223 | + project->clear(); |
| 224 | + |
| 225 | + // valid memory layer |
| 226 | + QgsVectorLayer *layer = new QgsVectorLayer( "LineString?crs=EPSG:4326", "VisibleLineLayer", "memory" ); |
| 227 | + QVERIFY( layer->isValid() ); |
| 228 | + |
| 229 | + // won't appear in the layer tree => false |
| 230 | + QCOMPARE( InputUtils::layerVisible( layer ), false ); |
| 231 | + |
| 232 | + // added to project => true |
| 233 | + project->addMapLayer( layer ); |
| 234 | + QCOMPARE( InputUtils::layerVisible( layer ), true ); |
| 235 | + |
| 236 | + // hide layer => false |
| 237 | + QgsLayerTree *root = project->layerTreeRoot(); |
| 238 | + QgsLayerTreeLayer *layerTree = root->findLayer( layer ); |
| 239 | + QVERIFY( layerTree ); |
| 240 | + layerTree->setItemVisibilityChecked( false ); |
| 241 | + QCOMPARE( InputUtils::layerVisible( layer ), false ); |
| 242 | + |
| 243 | + delete project; |
| 244 | +} |
| 245 | + |
| 246 | +void TestUtils::testIsPositionTrackingLayer() |
| 247 | +{ |
| 248 | + QCOMPARE( InputUtils::isPositionTrackingLayer( nullptr, nullptr ), false ); |
| 249 | + |
| 250 | + QgsProject *project = new QgsProject(); |
| 251 | + QgsVectorLayer *layer = new QgsVectorLayer( "Point?crs=EPSG:4326", "TrackingLayer", "memory" ); |
| 252 | + project->addMapLayer( layer ); |
| 253 | + QCOMPARE( InputUtils::isPositionTrackingLayer( layer, project ), false ); |
| 254 | + |
| 255 | + // tracking layer ID => true |
| 256 | + QString layerId = layer->id(); |
| 257 | + project->writeEntry( QStringLiteral( "Mergin" ), QStringLiteral( "PositionTracking/TrackingLayer" ), layerId ); |
| 258 | + QCOMPARE( InputUtils::isPositionTrackingLayer( layer, project ), true ); |
| 259 | + |
| 260 | + // not tracking layer ID => false |
| 261 | + project->writeEntry( QStringLiteral( "Mergin" ), QStringLiteral( "PositionTracking/TrackingLayer" ), QString( "some-other-id" ) ); |
| 262 | + QCOMPARE( InputUtils::isPositionTrackingLayer( layer, project ), false ); |
| 263 | + |
| 264 | + delete project; |
| 265 | +} |
| 266 | + |
| 267 | +void TestUtils::testRecordingAllowed() |
| 268 | +{ |
| 269 | + QCOMPARE( InputUtils::recordingAllowed( nullptr, nullptr ), false ); |
| 270 | + |
| 271 | + QgsProject *project = new QgsProject(); |
| 272 | + |
| 273 | + //valid vector layer => true |
| 274 | + QgsVectorLayer *validLayer = new QgsVectorLayer( "Polygon?crs=EPSG:4326", "PolygonLayer", "memory" ); |
| 275 | + project->addMapLayer( validLayer ); |
| 276 | + QCOMPARE( InputUtils::recordingAllowed( validLayer, project ), true ); |
| 277 | + |
| 278 | + // not visible => false |
| 279 | + QgsLayerTreeLayer *layerNode = project->layerTreeRoot()->findLayer( validLayer ); |
| 280 | + QVERIFY( layerNode ); |
| 281 | + layerNode->setItemVisibilityChecked( false ); |
| 282 | + QCOMPARE( InputUtils::recordingAllowed( validLayer, project ), false ); |
| 283 | + layerNode->setItemVisibilityChecked( true ); // restore |
| 284 | + |
| 285 | + // read-only layer => false |
| 286 | + validLayer->setReadOnly( true ); |
| 287 | + QCOMPARE( InputUtils::recordingAllowed( validLayer, project ), false ); |
| 288 | + validLayer->setReadOnly( false ); // restore |
| 289 | + |
| 290 | + // noGeo => false |
| 291 | + QgsVectorLayer *noGeomLayer = new QgsVectorLayer( "None", "NoGeomLayer", "memory" ); |
| 292 | + project->addMapLayer( noGeomLayer ); |
| 293 | + QCOMPARE( InputUtils::recordingAllowed( noGeomLayer, project ), false ); |
| 294 | + |
| 295 | + // position tracking layer => false |
| 296 | + project->writeEntry( "Mergin", "PositionTracking/TrackingLayer", validLayer->id() ); |
| 297 | + QCOMPARE( InputUtils::recordingAllowed( validLayer, project ), false ); |
| 298 | + |
| 299 | + // restore valid layer => true |
| 300 | + project->writeEntry( "Mergin", "PositionTracking/TrackingLayer", QString() ); |
| 301 | + QCOMPARE( InputUtils::recordingAllowed( validLayer, project ), true ); |
| 302 | + |
| 303 | + delete project; |
| 304 | +} |
| 305 | + |
| 306 | +void TestUtils::testMapLayerFromName() |
| 307 | +{ |
| 308 | + QCOMPARE( InputUtils::mapLayerFromName( "Anything", nullptr ), static_cast<QgsMapLayer *>( nullptr ) ); |
| 309 | + |
| 310 | + // empty layerName => nullptr |
| 311 | + QgsProject *project = new QgsProject(); |
| 312 | + QCOMPARE( InputUtils::mapLayerFromName( "", project ), static_cast<QgsMapLayer *>( nullptr ) ); |
| 313 | + |
| 314 | + // ddd a named layer to project and check => should succeed |
| 315 | + QgsVectorLayer *layer = new QgsVectorLayer( "Point?crs=EPSG:4326", "MyTestLayer", "memory" ); |
| 316 | + QVERIFY( layer->isValid() ); |
| 317 | + project->addMapLayer( layer ); |
| 318 | + QgsMapLayer *found = InputUtils::mapLayerFromName( "MyTestLayer", project ); |
| 319 | + QVERIFY( found != nullptr ); |
| 320 | + QCOMPARE( found->name(), QString( "MyTestLayer" ) ); |
| 321 | + |
| 322 | + // non-existing name => nullptr |
| 323 | + QCOMPARE( InputUtils::mapLayerFromName( "NoSuchName", project ), static_cast<QgsMapLayer *>( nullptr ) ); |
| 324 | + |
| 325 | + delete project; |
| 326 | +} |
0 commit comments