Skip to content

Commit 5c172a6

Browse files
committed
add test for 'search all layers' locator
1 parent f8b8a21 commit 5c172a6

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/app/locator/qgsinbuiltlocatorfilters.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class QgsActiveLayerFeaturesLocatorFilter : public QgsLocatorFilter
113113
QIcon mLayerIcon;
114114
};
115115

116-
class QgsAllLayersFeaturesLocatorFilter : public QgsLocatorFilter
116+
class APP_EXPORT QgsAllLayersFeaturesLocatorFilter : public QgsLocatorFilter
117117
{
118118
Q_OBJECT
119119

tests/src/app/testqgsapplocatorfilters.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class TestQgsAppLocatorFilters : public QObject
3636
void testCalculator();
3737
void testLayers();
3838
void testLayouts();
39+
void testSearchAllLayers();
3940

4041
private:
4142
QgisApp *mQgisApp = nullptr;
@@ -145,13 +146,52 @@ void TestQgsAppLocatorFilters::testLayouts()
145146
QCOMPARE( results.at( 0 ).userData.toString(), pl1->name() );
146147
QCOMPARE( results.at( 1 ).userData.toString(), pl2->name() );
147148
QCOMPARE( results.at( 2 ).userData.toString(), pl3->name() );
149+
}
150+
151+
void TestQgsAppLocatorFilters::testSearchAllLayers()
152+
{
153+
QString layerDef = QStringLiteral( "Point?crs=epsg:4326&field=pk:integer&field=my_text:string&field=my_number:integer&key=pk" );
154+
QgsVectorLayer *l1 = new QgsVectorLayer( layerDef, QStringLiteral( "Layer 1" ), QStringLiteral( "memory" ) );
155+
QgsVectorLayer *l2 = new QgsVectorLayer( layerDef, QStringLiteral( "Layer 2" ), QStringLiteral( "memory" ) );
156+
157+
QgsProject::instance()->addMapLayers( QList< QgsMapLayer *>() << l1 << l2 );
158+
159+
QgsFeature f1;
160+
f1.setAttributes( QVector<QVariant>() << 1001 << "A nice feature" << 6789 );
161+
f1.setGeometry( QgsGeometry::fromWkt( "Point (-71.123 78.23)" ) );
162+
QgsFeature f2;
163+
f2.setAttributes( QVector<QVariant>() << 1002 << "Something crazy" << 2 );
164+
f2.setGeometry( QgsGeometry::fromWkt( "Point (-72.123 78.23)" ) );
165+
QgsFeature f3;
166+
f3.setAttributes( QVector<QVariant>() << 2001 << "Another feature" << 6789 );
167+
f3.setGeometry( QgsGeometry::fromWkt( "Point (-73.123 78.23)" ) );
168+
169+
l1->dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 );
170+
l2->dataProvider()->addFeatures( QgsFeatureList() << f3 );
171+
172+
QgsAllLayersFeaturesLocatorFilter filter;
173+
QgsLocatorContext context;
174+
175+
QList< QgsLocatorResult > results = gatherResults( &filter, QStringLiteral( "100" ), context );
176+
QCOMPARE( results.count(), 2 );
148177

178+
l1->setDisplayExpression( QStringLiteral( "\"my_text\" || ' is ' || \"my_number\"" ) );
179+
l2->setDisplayExpression( QStringLiteral( "\"my_text\" || ' is ' || \"my_number\"" ) );
180+
181+
results = gatherResults( &filter, QStringLiteral( "feature is 6789" ), context );
182+
QCOMPARE( results.count(), 2 );
183+
184+
l2->setFlags( l2->flags() & ~QgsMapLayer::Searchable );
185+
186+
results = gatherResults( &filter, QStringLiteral( "feature is 6789" ), context );
187+
QCOMPARE( results.count(), 1 );
149188
}
150189

151190
QList<QgsLocatorResult> TestQgsAppLocatorFilters::gatherResults( QgsLocatorFilter *filter, const QString &string, const QgsLocatorContext &context )
152191
{
153192
QSignalSpy spy( filter, &QgsLocatorFilter::resultFetched );
154193
QgsFeedback f;
194+
filter->prepare( string, context );
155195
filter->fetchResults( string, context, &f );
156196

157197
QList< QgsLocatorResult > results;

0 commit comments

Comments
 (0)