Skip to content
Permalink
Browse files

add test for 'search all layers' locator

  • Loading branch information
3nids committed Sep 7, 2018
1 parent f8b8a21 commit 5c172a6f9d65a05ad817b9e61b83beb9b36daff2
Showing with 41 additions and 1 deletion.
  1. +1 −1 src/app/locator/qgsinbuiltlocatorfilters.h
  2. +40 −0 tests/src/app/testqgsapplocatorfilters.cpp
@@ -113,7 +113,7 @@ class QgsActiveLayerFeaturesLocatorFilter : public QgsLocatorFilter
QIcon mLayerIcon;
};

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

@@ -36,6 +36,7 @@ class TestQgsAppLocatorFilters : public QObject
void testCalculator();
void testLayers();
void testLayouts();
void testSearchAllLayers();

private:
QgisApp *mQgisApp = nullptr;
@@ -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;

0 comments on commit 5c172a6

Please sign in to comment.
You can’t perform that action at this time.