Skip to content

Commit

Permalink
add test for 'search all layers' locator
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Sep 7, 2018
1 parent f8b8a21 commit 5c172a6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/app/locator/qgsinbuiltlocatorfilters.h
Expand Up @@ -113,7 +113,7 @@ class QgsActiveLayerFeaturesLocatorFilter : public QgsLocatorFilter
QIcon mLayerIcon;
};

class QgsAllLayersFeaturesLocatorFilter : public QgsLocatorFilter
class APP_EXPORT QgsAllLayersFeaturesLocatorFilter : public QgsLocatorFilter
{
Q_OBJECT

Expand Down
40 changes: 40 additions & 0 deletions tests/src/app/testqgsapplocatorfilters.cpp
Expand Up @@ -36,6 +36,7 @@ class TestQgsAppLocatorFilters : public QObject
void testCalculator();
void testLayers();
void testLayouts();
void testSearchAllLayers();

private:
QgisApp *mQgisApp = nullptr;
Expand Down Expand Up @@ -145,13 +146,52 @@ void TestQgsAppLocatorFilters::testLayouts()
QCOMPARE( results.at( 0 ).userData.toString(), pl1->name() );
QCOMPARE( results.at( 1 ).userData.toString(), pl2->name() );
QCOMPARE( results.at( 2 ).userData.toString(), pl3->name() );
}

void TestQgsAppLocatorFilters::testSearchAllLayers()
{
QString layerDef = QStringLiteral( "Point?crs=epsg:4326&field=pk:integer&field=my_text:string&field=my_number:integer&key=pk" );
QgsVectorLayer *l1 = new QgsVectorLayer( layerDef, QStringLiteral( "Layer 1" ), QStringLiteral( "memory" ) );
QgsVectorLayer *l2 = new QgsVectorLayer( layerDef, QStringLiteral( "Layer 2" ), QStringLiteral( "memory" ) );

QgsProject::instance()->addMapLayers( QList< QgsMapLayer *>() << l1 << l2 );

QgsFeature f1;
f1.setAttributes( QVector<QVariant>() << 1001 << "A nice feature" << 6789 );
f1.setGeometry( QgsGeometry::fromWkt( "Point (-71.123 78.23)" ) );
QgsFeature f2;
f2.setAttributes( QVector<QVariant>() << 1002 << "Something crazy" << 2 );
f2.setGeometry( QgsGeometry::fromWkt( "Point (-72.123 78.23)" ) );
QgsFeature f3;
f3.setAttributes( QVector<QVariant>() << 2001 << "Another feature" << 6789 );
f3.setGeometry( QgsGeometry::fromWkt( "Point (-73.123 78.23)" ) );

l1->dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 );
l2->dataProvider()->addFeatures( QgsFeatureList() << f3 );

QgsAllLayersFeaturesLocatorFilter filter;
QgsLocatorContext context;

QList< QgsLocatorResult > results = gatherResults( &filter, QStringLiteral( "100" ), context );
QCOMPARE( results.count(), 2 );

l1->setDisplayExpression( QStringLiteral( "\"my_text\" || ' is ' || \"my_number\"" ) );
l2->setDisplayExpression( QStringLiteral( "\"my_text\" || ' is ' || \"my_number\"" ) );

results = gatherResults( &filter, QStringLiteral( "feature is 6789" ), context );
QCOMPARE( results.count(), 2 );

l2->setFlags( l2->flags() & ~QgsMapLayer::Searchable );

results = gatherResults( &filter, QStringLiteral( "feature is 6789" ), context );
QCOMPARE( results.count(), 1 );
}

QList<QgsLocatorResult> TestQgsAppLocatorFilters::gatherResults( QgsLocatorFilter *filter, const QString &string, const QgsLocatorContext &context )
{
QSignalSpy spy( filter, &QgsLocatorFilter::resultFetched );
QgsFeedback f;
filter->prepare( string, context );
filter->fetchResults( string, context, &f );

QList< QgsLocatorResult > results;
Expand Down

0 comments on commit 5c172a6

Please sign in to comment.