Skip to content

Commit

Permalink
add test for (active) prefixes and fix bad prefix for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed May 11, 2018
1 parent 0a6f88a commit 2bb6149
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 11 deletions.
2 changes: 1 addition & 1 deletion python/core/locator/qgslocatorfilter.sip.in
Expand Up @@ -185,7 +185,7 @@ is entered.

QString activePrefix() const;
%Docstring
Returns true if the filter should be used when no prefix
Returns the prefix in use in the locator
is entered.

.. seealso:: :py:func:`setActivePrefix`
Expand Down
8 changes: 8 additions & 0 deletions src/core/locator/qgslocator.cpp
Expand Up @@ -104,8 +104,14 @@ void QgsLocator::registerFilter( QgsLocatorFilter *filter )
}
else if ( prefix.length() >= 3 || prefix != filter->prefix() )
{
// for plugins either the native prefix is >3 char or it has been customized by user
filter->setActivePrefix( prefix );
}
else
{
// otherwise set it to empty string (not NULL)
filter->setActivePrefix( QString( "" ) );
}
}

filter->setEnabled( enabled );
Expand Down Expand Up @@ -156,7 +162,9 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c
for ( QgsLocatorFilter *filter : qgis::as_const( mFilters ) )
{
if ( filter->useWithoutPrefix() && filter->enabled() )
{
activeFilters << filter;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/locator/qgslocatorfilter.cpp
Expand Up @@ -69,7 +69,7 @@ void QgsLocatorFilter::setUseWithoutPrefix( bool useWithoutPrefix )

QString QgsLocatorFilter::activePrefix() const
{
if ( mActivePrefifx.isEmpty() )
if ( mActivePrefifx.isNull() )
return prefix();
else
return mActivePrefifx;
Expand Down
6 changes: 3 additions & 3 deletions src/core/locator/qgslocatorfilter.h
Expand Up @@ -210,17 +210,17 @@ class CORE_EXPORT QgsLocatorFilter : public QObject
void setUseWithoutPrefix( bool useWithoutPrefix );

/**
* Returns true if the filter should be used when no prefix
* Returns the prefix in use in the locator
* is entered.
* \see setActivePrefix()
* \since QGIS 3.2
*/
QString activePrefix() const;

/**
* Sets whether the filter should be used when no prefix
* is entered.
* Sets the prefix as being used by the locator
* \see activePrefix()
* \note If activePrefix is empty, no prefix is used. If activePrefix is NULL, the default prefix is used.
* \since QGIS 3.2
*/
void setActivePrefix( const QString &activePrefix ) SIP_SKIP;
Expand Down
114 changes: 108 additions & 6 deletions tests/src/python/test_qgslocator.py
Expand Up @@ -20,7 +20,8 @@
QgsLocatorContext,
QgsLocatorResult,
QgsLocatorModel,
QgsLocatorAutomaticModel)
QgsLocatorAutomaticModel,
QgsSettings)
from qgis.PyQt.QtCore import QVariant, pyqtSignal, QCoreApplication
from time import sleep
from qgis.testing import start_app, unittest
Expand All @@ -30,26 +31,30 @@

class test_filter(QgsLocatorFilter):

def __init__(self, prefix, parent=None):
def __init__(self, identifier, prefix=None, parent=None):
super().__init__(parent)
self.prefix = prefix
self.identifier = identifier
self._prefix = prefix

def clone(self):
return test_filter(self.prefix)
return test_filter(self.identifier)

def name(self):
return 'test'
return 'test_' + self.identifier

def displayName(self):
return 'test'

def prefix(self):
return self._prefix

def fetchResults(self, string, context, feedback):
for i in range(3):
if feedback.isCanceled():
return
sleep(0.001)
result = QgsLocatorResult()
result.displayString = self.prefix + str(i)
result.displayString = self.identifier + str(i)
self.resultFetched.emit(result)

def triggerResult(self, result):
Expand Down Expand Up @@ -164,6 +169,103 @@ def got_hit(result):
l.fetchResults('a', context)
l.cancel()

def testPrefixes(self):
"""
Test custom (active) prefixes
"""

def got_hit(result):
got_hit._results_.append(result.displayString)

got_hit._results_ = []

context = QgsLocatorContext()

l = QgsLocator()

# filter with prefix
filter_a = test_filter('a', 'aaa')
l.registerFilter(filter_a)
self.assertEqual(filter_a.prefix(), 'aaa')
self.assertEqual(filter_a.activePrefix(), 'aaa')
self.assertEqual(filter_a.useWithoutPrefix(), True)
l.foundResult.connect(got_hit)
l.fetchResults('aaa a', context)
for i in range(100):
sleep(0.002)
QCoreApplication.processEvents()
self.assertEqual(set(got_hit._results_), {'a0', 'a1', 'a2'})
got_hit._results_ = []
l.fetchResults('bbb b', context)
for i in range(100):
sleep(0.002)
QCoreApplication.processEvents()
self.assertEqual(set(got_hit._results_), {'a0', 'a1', 'a2'})
got_hit._results_ = []
filter_a.setUseWithoutPrefix(False)
self.assertEqual(filter_a.useWithoutPrefix(), False)
l.fetchResults('bbb b', context)
for i in range(100):
sleep(0.002)
QCoreApplication.processEvents()
self.assertEqual(got_hit._results_, [])
got_hit._results_ = []

# test with two filters
filter_b = test_filter('b', 'bbb')
l.registerFilter(filter_b)
self.assertEqual(filter_b.prefix(), 'bbb')
self.assertEqual(filter_b.activePrefix(), 'bbb')
got_hit._results_ = []
l.fetchResults('bbb b', context)
for i in range(100):
sleep(0.002)
QCoreApplication.processEvents()
self.assertEqual(set(got_hit._results_), {'b0', 'b1', 'b2'})
l.deregisterFilter(filter_b)

# test with two filters with same prefix
filter_b = test_filter('b', 'aaa')
l.registerFilter(filter_b)
self.assertEqual(filter_b.prefix(), 'aaa')
self.assertEqual(filter_b.activePrefix(), 'aaa')
got_hit._results_ = []
l.fetchResults('aaa b', context)
for i in range(100):
sleep(0.002)
QCoreApplication.processEvents()
self.assertEqual(set(got_hit._results_), {'a0', 'a1', 'a2', 'b0', 'b1', 'b2'})
l.deregisterFilter(filter_b)

# filter with invalid prefix (less than 3 char)
filter_c = test_filter('c', 'bb')
l.registerFilter(filter_c)
self.assertEqual(filter_c.prefix(), 'bb')
self.assertEqual(filter_c.activePrefix(), '')
got_hit._results_ = []
l.fetchResults('b', context)
for i in range(100):
sleep(0.002)
QCoreApplication.processEvents()
self.assertEqual(set(got_hit._results_), {'c0', 'c1', 'c2'})
l.deregisterFilter(filter_c)

# filter with custom prefix
QgsSettings().setValue("locator_filters/prefix_test_custom", 'xyz', QgsSettings.Gui)
filter_c = test_filter('custom', 'abc')
l.registerFilter(filter_c)
self.assertEqual(filter_c.prefix(), 'abc')
self.assertEqual(filter_c.activePrefix(), 'xyz')
got_hit._results_ = []
l.fetchResults('b', context)
for i in range(100):
sleep(0.002)
QCoreApplication.processEvents()
self.assertEqual(set(got_hit._results_), {'custom0', 'custom1', 'custom2'})
l.deregisterFilter(filter_c)

del l

def testModel(self):
m = QgsLocatorModel()
l = QgsLocator()
Expand Down

0 comments on commit 2bb6149

Please sign in to comment.