@@ -35,9 +35,11 @@ class TestQgsProcessingAlgs: public QObject
35
35
void cleanup () {} // will be called after every testfunction.
36
36
void packageAlg ();
37
37
void renameLayerAlg ();
38
+ void loadLayerAlg ();
38
39
39
40
private:
40
41
42
+ QString mPointLayerPath ;
41
43
QgsVectorLayer *mPointsLayer = nullptr ;
42
44
QgsVectorLayer *mPolygonLayer = nullptr ;
43
45
@@ -59,7 +61,8 @@ void TestQgsProcessingAlgs::initTestCase()
59
61
60
62
QString pointsFileName = dataDir + " /points.shp" ;
61
63
QFileInfo pointFileInfo ( pointsFileName );
62
- mPointsLayer = new QgsVectorLayer ( pointFileInfo.filePath (),
64
+ mPointLayerPath = pointFileInfo.filePath ();
65
+ mPointsLayer = new QgsVectorLayer ( mPointLayerPath ,
63
66
QStringLiteral ( " points" ), QStringLiteral ( " ogr" ) );
64
67
QVERIFY ( mPointsLayer ->isValid () );
65
68
// Register the layer with the registry
@@ -169,6 +172,51 @@ void TestQgsProcessingAlgs::renameLayerAlg()
169
172
QCOMPARE ( results.value ( " OUTPUT" ).toString (), QStringLiteral ( " new name2" ) );
170
173
}
171
174
175
+ void TestQgsProcessingAlgs::loadLayerAlg ()
176
+ {
177
+ const QgsProcessingAlgorithm *package ( QgsApplication::processingRegistry ()->algorithmById ( QStringLiteral ( " native:loadlayer" ) ) );
178
+ QVERIFY ( package );
179
+
180
+ std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >();
181
+ QgsProject p;
182
+ context->setProject ( &p );
183
+
184
+ QgsProcessingFeedback feedback;
185
+
186
+ QVariantMap parameters;
187
+
188
+ // bad layer
189
+ parameters.insert ( QStringLiteral ( " INPUT" ), QStringLiteral ( " bad layer" ) );
190
+ parameters.insert ( QStringLiteral ( " NAME" ), QStringLiteral ( " new name" ) );
191
+ bool ok = false ;
192
+ ( void )package->run ( parameters, *context, &feedback, &ok );
193
+ QVERIFY ( !ok );
194
+ QVERIFY ( context->layersToLoadOnCompletion ().empty () );
195
+
196
+ // invalid name
197
+ parameters.insert ( QStringLiteral ( " INPUT" ), mPointLayerPath );
198
+ parameters.insert ( QStringLiteral ( " NAME" ), QString () );
199
+ ok = false ;
200
+ ( void )package->run ( parameters, *context, &feedback, &ok );
201
+ QVERIFY ( !ok );
202
+ QVERIFY ( context->layersToLoadOnCompletion ().empty () );
203
+
204
+ // good params
205
+ parameters.insert ( QStringLiteral ( " INPUT" ), mPointLayerPath );
206
+ parameters.insert ( QStringLiteral ( " NAME" ), QStringLiteral ( " my layer" ) );
207
+ ok = false ;
208
+ QVariantMap results = package->run ( parameters, *context, &feedback, &ok );
209
+ QVERIFY ( ok );
210
+ QVERIFY ( !context->layersToLoadOnCompletion ().empty () );
211
+ QString layerId = context->layersToLoadOnCompletion ().keys ().at ( 0 );
212
+ QCOMPARE ( results.value ( QStringLiteral ( " OUTPUT" ) ).toString (), layerId );
213
+ QVERIFY ( !layerId.isEmpty () );
214
+ QVERIFY ( context->temporaryLayerStore ()->mapLayer ( layerId ) );
215
+ QCOMPARE ( context->layersToLoadOnCompletion ().value ( layerId, QgsProcessingContext::LayerDetails ( QString (), nullptr , QString () ) ).name , QStringLiteral ( " my layer" ) );
216
+ QCOMPARE ( context->layersToLoadOnCompletion ().value ( layerId, QgsProcessingContext::LayerDetails ( QString (), nullptr , QString () ) ).project , &p );
217
+ QCOMPARE ( context->layersToLoadOnCompletion ().value ( layerId, QgsProcessingContext::LayerDetails ( QString (), nullptr , QString () ) ).outputName , QStringLiteral ( " my layer" ) );
218
+ }
219
+
172
220
173
221
QGSTEST_MAIN ( TestQgsProcessingAlgs )
174
222
#include " testqgsprocessingalgs.moc"
0 commit comments