Merge remote-tracking branch 'origin/pr/3352'

This commit is contained in:
Florian Bruhin 2017-12-02 14:37:01 +01:00
commit e9be357104
4 changed files with 64 additions and 15 deletions

View File

@ -60,8 +60,6 @@ class CompletionModel(QAbstractItemModel):
def add_category(self, cat): def add_category(self, cat):
"""Add a completion category to the model.""" """Add a completion category to the model."""
self._categories.append(cat) self._categories.append(cat)
cat.layoutAboutToBeChanged.connect(self.layoutAboutToBeChanged)
cat.layoutChanged.connect(self.layoutChanged)
def data(self, index, role=Qt.DisplayRole): def data(self, index, role=Qt.DisplayRole):
"""Return the item data for index. """Return the item data for index.
@ -179,8 +177,12 @@ class CompletionModel(QAbstractItemModel):
pattern: The filter pattern to set. pattern: The filter pattern to set.
""" """
log.completion.debug("Setting completion pattern '{}'".format(pattern)) log.completion.debug("Setting completion pattern '{}'".format(pattern))
# layoutChanged is broken in pyqt-5.7.1, so we must use metaObject
# https://www.riverbankcomputing.com/pipermail/pyqt/2017-January/038483.html
self.metaObject().invokeMethod(self, "layoutAboutToBeChanged")
for cat in self._categories: for cat in self._categories:
cat.set_pattern(pattern) cat.set_pattern(pattern)
self.metaObject().invokeMethod(self, "layoutChanged")
def first_item(self): def first_item(self):
"""Return the index of the first child (non-category) in the model.""" """Return the index of the first child (non-category) in the model."""

View File

@ -56,12 +56,15 @@ def url(*, info):
""" """
model = completionmodel.CompletionModel(column_widths=(40, 50, 10)) model = completionmodel.CompletionModel(column_widths=(40, 50, 10))
quickmarks = ((url, name) for (name, url) quickmarks = [(url, name) for (name, url)
in objreg.get('quickmark-manager').marks.items()) in objreg.get('quickmark-manager').marks.items()]
bookmarks = objreg.get('bookmark-manager').marks.items() bookmarks = objreg.get('bookmark-manager').marks.items()
if quickmarks:
model.add_category(listcategory.ListCategory( model.add_category(listcategory.ListCategory(
'Quickmarks', quickmarks, delete_func=_delete_quickmark, sort=False)) 'Quickmarks', quickmarks, delete_func=_delete_quickmark,
sort=False))
if bookmarks:
model.add_category(listcategory.ListCategory( model.add_category(listcategory.ListCategory(
'Bookmarks', bookmarks, delete_func=_delete_bookmark, sort=False)) 'Bookmarks', bookmarks, delete_func=_delete_bookmark, sort=False))

View File

@ -68,16 +68,16 @@ def test_count(counts):
assert model.count() == sum(counts) assert model.count() == sum(counts)
@hypothesis.given(strategies.text()) @hypothesis.given(pat=strategies.text())
def test_set_pattern(pat): def test_set_pattern(pat, qtbot):
"""Validate the filtering and sorting results of set_pattern.""" """Validate the filtering and sorting results of set_pattern."""
model = completionmodel.CompletionModel() model = completionmodel.CompletionModel()
cats = [mock.Mock(spec=['set_pattern', 'layoutChanged', cats = [mock.Mock(spec=['set_pattern']) for _ in range(3)]
'layoutAboutToBeChanged'])
for _ in range(3)]
for c in cats: for c in cats:
c.set_pattern = mock.Mock(spec=[]) c.set_pattern = mock.Mock(spec=[])
model.add_category(c) model.add_category(c)
with qtbot.waitSignals([model.layoutAboutToBeChanged, model.layoutChanged],
order='strict'):
model.set_pattern(pat) model.set_pattern(pat)
for c in cats: for c in cats:
c.set_pattern.assert_called_with(pat) c.set_pattern.assert_called_with(pat)

View File

@ -363,6 +363,50 @@ def test_url_completion(qtmodeltester, web_history_populated,
}) })
def test_url_completion_no_quickmarks(qtmodeltester, web_history_populated,
quickmark_manager_stub, bookmarks, info):
"""Test that the quickmark category is gone with no quickmarks."""
model = urlmodel.url(info=info)
model.set_pattern('')
qtmodeltester.data_display_may_return_none = True
qtmodeltester.check(model)
_check_completions(model, {
"Bookmarks": [
('https://github.com', 'GitHub', None),
('https://python.org', 'Welcome to Python.org', None),
('http://qutebrowser.org', 'qutebrowser | qutebrowser', None),
],
"History": [
('https://github.com', 'https://github.com', '2016-05-01'),
('https://python.org', 'Welcome to Python.org', '2016-03-08'),
('http://qutebrowser.org', 'qutebrowser', '2015-09-05'),
],
})
def test_url_completion_no_bookmarks(qtmodeltester, web_history_populated,
quickmarks, bookmark_manager_stub, info):
"""Test that the bookmarks category is gone with no bookmarks."""
model = urlmodel.url(info=info)
model.set_pattern('')
qtmodeltester.data_display_may_return_none = True
qtmodeltester.check(model)
_check_completions(model, {
"Quickmarks": [
('https://wiki.archlinux.org', 'aw', None),
('https://wikipedia.org', 'wiki', None),
('https://duckduckgo.com', 'ddg', None),
],
"History": [
('https://github.com', 'https://github.com', '2016-05-01'),
('https://python.org', 'Welcome to Python.org', '2016-03-08'),
('http://qutebrowser.org', 'qutebrowser', '2015-09-05'),
],
})
@pytest.mark.parametrize('url, title, pattern, rowcount', [ @pytest.mark.parametrize('url, title, pattern, rowcount', [
('example.com', 'Site Title', '', 1), ('example.com', 'Site Title', '', 1),
('example.com', 'Site Title', 'ex', 1), ('example.com', 'Site Title', 'ex', 1),
@ -389,7 +433,7 @@ def test_url_completion_pattern(web_history, quickmark_manager_stub,
model = urlmodel.url(info=info) model = urlmodel.url(info=info)
model.set_pattern(pattern) model.set_pattern(pattern)
# 2, 0 is History # 2, 0 is History
assert model.rowCount(model.index(2, 0)) == rowcount assert model.rowCount(model.index(0, 0)) == rowcount
def test_url_completion_delete_bookmark(qtmodeltester, bookmarks, def test_url_completion_delete_bookmark(qtmodeltester, bookmarks,