2016-07-13 02:04:56 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2018-02-05 12:19:50 +01:00
|
|
|
# Copyright 2016-2018 Ryan Roden-Corrent (rcorre) <ryan@rcorre.net>
|
2016-07-13 02:04:56 +02:00
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""Tests for the CompletionView Object."""
|
|
|
|
|
2017-03-03 14:31:28 +01:00
|
|
|
from unittest import mock
|
2016-07-13 02:04:56 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from qutebrowser.completion import completionwidget
|
2017-03-03 14:31:28 +01:00
|
|
|
from qutebrowser.completion.models import completionmodel, listcategory
|
2018-11-29 14:09:06 +01:00
|
|
|
from qutebrowser.api import cmdutils
|
2016-07-13 02:04:56 +02:00
|
|
|
|
|
|
|
|
2016-07-27 13:39:25 +02:00
|
|
|
@pytest.fixture
|
2016-07-13 02:04:56 +02:00
|
|
|
def completionview(qtbot, status_command_stub, config_stub, win_registry,
|
2016-07-25 13:07:47 +02:00
|
|
|
mocker):
|
2016-07-13 02:04:56 +02:00
|
|
|
"""Create the CompletionView used for testing."""
|
|
|
|
# mock the Completer that the widget creates in its constructor
|
2016-07-25 13:07:47 +02:00
|
|
|
mocker.patch('qutebrowser.completion.completer.Completer', autospec=True)
|
2017-07-24 01:32:24 +02:00
|
|
|
mocker.patch(
|
|
|
|
'qutebrowser.completion.completiondelegate.CompletionItemDelegate',
|
|
|
|
new=lambda *_: None)
|
2016-07-13 02:04:56 +02:00
|
|
|
view = completionwidget.CompletionView(win_id=0)
|
|
|
|
qtbot.addWidget(view)
|
2016-07-27 13:39:25 +02:00
|
|
|
return view
|
2016-07-13 02:04:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_set_model(completionview):
|
|
|
|
"""Ensure set_model actually sets the model and expands all categories."""
|
2017-02-23 04:25:11 +01:00
|
|
|
model = completionmodel.CompletionModel()
|
2017-11-26 21:49:37 +01:00
|
|
|
for _i in range(3):
|
2017-03-22 01:42:32 +01:00
|
|
|
model.add_category(listcategory.ListCategory('', [('foo',)]))
|
2017-02-23 04:25:11 +01:00
|
|
|
completionview.set_model(model)
|
|
|
|
assert completionview.model() is model
|
2017-03-22 01:42:32 +01:00
|
|
|
for i in range(3):
|
2017-02-23 04:25:11 +01:00
|
|
|
assert completionview.isExpanded(model.index(i, 0))
|
2016-07-13 02:04:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_set_pattern(completionview):
|
2017-02-23 04:25:11 +01:00
|
|
|
model = completionmodel.CompletionModel()
|
2017-07-03 14:20:31 +02:00
|
|
|
model.set_pattern = mock.Mock(spec=[])
|
2017-05-31 18:35:43 +02:00
|
|
|
completionview.set_model(model)
|
|
|
|
completionview.set_pattern('foo')
|
2016-07-13 02:04:56 +02:00
|
|
|
model.set_pattern.assert_called_with('foo')
|
2017-07-23 00:06:16 +02:00
|
|
|
assert not completionview.selectionModel().currentIndex().isValid()
|
2016-07-13 02:04:56 +02:00
|
|
|
|
|
|
|
|
2017-07-12 14:19:31 +02:00
|
|
|
def test_set_pattern_no_model(completionview):
|
|
|
|
"""Ensure that setting a pattern with no model does not fail."""
|
|
|
|
completionview.set_pattern('foo')
|
2016-07-13 02:04:56 +02:00
|
|
|
|
|
|
|
|
2016-09-22 17:04:39 +02:00
|
|
|
def test_maybe_update_geometry(completionview, config_stub, qtbot):
|
2016-07-13 02:04:56 +02:00
|
|
|
"""Ensure completion is resized only if shrink is True."""
|
2016-09-22 17:04:39 +02:00
|
|
|
with qtbot.assertNotEmitted(completionview.update_geometry):
|
|
|
|
completionview._maybe_update_geometry()
|
2017-07-03 18:42:41 +02:00
|
|
|
config_stub.val.completion.shrink = True
|
2016-09-22 17:04:39 +02:00
|
|
|
with qtbot.waitSignal(completionview.update_geometry):
|
|
|
|
completionview._maybe_update_geometry()
|
2016-07-13 02:04:56 +02:00
|
|
|
|
|
|
|
|
2016-09-15 13:41:56 +02:00
|
|
|
@pytest.mark.parametrize('which, tree, expected', [
|
|
|
|
('next', [['Aa']], ['Aa', None, None]),
|
|
|
|
('prev', [['Aa']], ['Aa', None, None]),
|
|
|
|
('next', [['Aa'], ['Ba']], ['Aa', 'Ba', 'Aa']),
|
|
|
|
('prev', [['Aa'], ['Ba']], ['Ba', 'Aa', 'Ba']),
|
|
|
|
('next', [['Aa', 'Ab', 'Ac'], ['Ba', 'Bb'], ['Ca']],
|
2017-12-15 19:08:15 +01:00
|
|
|
['Aa', 'Ab', 'Ac', 'Ba', 'Bb', 'Ca', 'Aa']),
|
2016-09-15 13:41:56 +02:00
|
|
|
('prev', [['Aa', 'Ab', 'Ac'], ['Ba', 'Bb'], ['Ca']],
|
2017-12-15 19:08:15 +01:00
|
|
|
['Ca', 'Bb', 'Ba', 'Ac', 'Ab', 'Aa', 'Ca']),
|
2016-09-15 13:41:56 +02:00
|
|
|
('next', [[], ['Ba', 'Bb']], ['Ba', 'Bb', 'Ba']),
|
|
|
|
('prev', [[], ['Ba', 'Bb']], ['Bb', 'Ba', 'Bb']),
|
|
|
|
('next', [[], [], ['Ca', 'Cb']], ['Ca', 'Cb', 'Ca']),
|
|
|
|
('prev', [[], [], ['Ca', 'Cb']], ['Cb', 'Ca', 'Cb']),
|
|
|
|
('next', [['Aa'], []], ['Aa', None]),
|
|
|
|
('prev', [['Aa'], []], ['Aa', None]),
|
|
|
|
('next', [['Aa'], [], []], ['Aa', None]),
|
|
|
|
('prev', [['Aa'], [], []], ['Aa', None]),
|
|
|
|
('next', [['Aa'], [], ['Ca', 'Cb']], ['Aa', 'Ca', 'Cb', 'Aa']),
|
|
|
|
('prev', [['Aa'], [], ['Ca', 'Cb']], ['Cb', 'Ca', 'Aa', 'Cb']),
|
|
|
|
('next', [[]], [None, None]),
|
|
|
|
('prev', [[]], [None, None]),
|
|
|
|
('next-category', [['Aa']], ['Aa', None, None]),
|
|
|
|
('prev-category', [['Aa']], ['Aa', None, None]),
|
|
|
|
('next-category', [['Aa'], ['Ba']], ['Aa', 'Ba', 'Aa']),
|
|
|
|
('prev-category', [['Aa'], ['Ba']], ['Ba', 'Aa', 'Ba']),
|
|
|
|
('next-category', [['Aa', 'Ab', 'Ac'], ['Ba', 'Bb'], ['Ca']],
|
2017-12-15 19:08:15 +01:00
|
|
|
['Aa', 'Ba', 'Ca', 'Aa']),
|
2016-09-15 13:41:56 +02:00
|
|
|
('prev-category', [['Aa', 'Ab', 'Ac'], ['Ba', 'Bb'], ['Ca']],
|
2017-12-15 19:08:15 +01:00
|
|
|
['Ca', 'Ba', 'Aa', 'Ca']),
|
2016-09-15 13:41:56 +02:00
|
|
|
('next-category', [[], ['Ba', 'Bb']], ['Ba', None, None]),
|
|
|
|
('prev-category', [[], ['Ba', 'Bb']], ['Ba', None, None]),
|
|
|
|
('next-category', [[], [], ['Ca', 'Cb']], ['Ca', None, None]),
|
|
|
|
('prev-category', [[], [], ['Ca', 'Cb']], ['Ca', None, None]),
|
|
|
|
('next-category', [['Aa'], [], []], ['Aa', None, None]),
|
|
|
|
('prev-category', [['Aa'], [], []], ['Aa', None, None]),
|
|
|
|
('next-category', [['Aa'], [], ['Ca', 'Cb']], ['Aa', 'Ca', 'Aa']),
|
|
|
|
('prev-category', [['Aa'], [], ['Ca', 'Cb']], ['Ca', 'Aa', 'Ca']),
|
|
|
|
('next-category', [[]], [None, None]),
|
|
|
|
('prev-category', [[]], [None, None]),
|
2016-07-13 02:04:56 +02:00
|
|
|
])
|
2016-09-15 13:41:56 +02:00
|
|
|
def test_completion_item_focus(which, tree, expected, completionview, qtbot):
|
2016-07-13 02:04:56 +02:00
|
|
|
"""Test that on_next_prev_item moves the selection properly.
|
|
|
|
|
|
|
|
Args:
|
2016-09-15 13:41:56 +02:00
|
|
|
which: the direction in which to move the selection.
|
2016-07-25 13:07:47 +02:00
|
|
|
tree: Each list represents a completion category, with each string
|
|
|
|
being an item under that category.
|
2016-09-15 13:41:56 +02:00
|
|
|
expected: expected argument from on_selection_changed for each
|
|
|
|
successive movement. None implies no signal should be
|
|
|
|
emitted.
|
2016-07-13 02:04:56 +02:00
|
|
|
"""
|
2017-02-23 04:25:11 +01:00
|
|
|
model = completionmodel.CompletionModel()
|
2016-07-13 02:04:56 +02:00
|
|
|
for catdata in tree:
|
2017-03-03 14:31:28 +01:00
|
|
|
cat = listcategory.ListCategory('', ((x,) for x in catdata))
|
|
|
|
model.add_category(cat)
|
2017-02-23 04:25:11 +01:00
|
|
|
completionview.set_model(model)
|
2016-09-15 13:41:56 +02:00
|
|
|
for entry in expected:
|
|
|
|
if entry is None:
|
|
|
|
with qtbot.assertNotEmitted(completionview.selection_changed):
|
2016-08-31 18:43:23 +02:00
|
|
|
completionview.completion_item_focus(which)
|
2016-09-15 13:41:56 +02:00
|
|
|
else:
|
|
|
|
with qtbot.waitSignal(completionview.selection_changed) as sig:
|
|
|
|
completionview.completion_item_focus(which)
|
|
|
|
assert sig.args == [entry]
|
2016-08-20 07:01:38 +02:00
|
|
|
|
|
|
|
|
2016-08-31 18:43:23 +02:00
|
|
|
@pytest.mark.parametrize('which', ['next', 'prev', 'next-category',
|
|
|
|
'prev-category'])
|
|
|
|
def test_completion_item_focus_no_model(which, completionview, qtbot):
|
|
|
|
"""Test that selectionChanged is not fired when the model is None.
|
|
|
|
|
|
|
|
Validates #1812: help completion repeatedly completes
|
|
|
|
"""
|
|
|
|
with qtbot.assertNotEmitted(completionview.selection_changed):
|
|
|
|
completionview.completion_item_focus(which)
|
2017-02-23 04:25:11 +01:00
|
|
|
model = completionmodel.CompletionModel()
|
|
|
|
completionview.set_model(model)
|
2016-08-31 18:43:23 +02:00
|
|
|
completionview.set_model(None)
|
|
|
|
with qtbot.assertNotEmitted(completionview.selection_changed):
|
|
|
|
completionview.completion_item_focus(which)
|
|
|
|
|
2016-09-01 07:09:06 +02:00
|
|
|
|
2017-07-24 01:32:24 +02:00
|
|
|
def test_completion_item_focus_fetch(completionview, qtbot):
|
|
|
|
"""Test that on_next_prev_item moves the selection properly.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
which: the direction in which to move the selection.
|
|
|
|
tree: Each list represents a completion category, with each string
|
|
|
|
being an item under that category.
|
|
|
|
expected: expected argument from on_selection_changed for each
|
|
|
|
successive movement. None implies no signal should be
|
|
|
|
emitted.
|
|
|
|
"""
|
|
|
|
model = completionmodel.CompletionModel()
|
2017-12-15 19:08:15 +01:00
|
|
|
cat = mock.Mock(spec=[
|
|
|
|
'layoutChanged', 'layoutAboutToBeChanged', 'canFetchMore',
|
|
|
|
'fetchMore', 'rowCount', 'index', 'data'])
|
2017-07-24 01:32:24 +02:00
|
|
|
cat.canFetchMore = lambda *_: True
|
|
|
|
cat.rowCount = lambda *_: 2
|
|
|
|
cat.fetchMore = mock.Mock()
|
|
|
|
model.add_category(cat)
|
|
|
|
completionview.set_model(model)
|
|
|
|
# clear the fetchMore call that happens on set_model
|
|
|
|
cat.reset_mock()
|
|
|
|
|
|
|
|
# not at end, fetchMore shouldn't be called
|
|
|
|
completionview.completion_item_focus('next')
|
|
|
|
assert not cat.fetchMore.called
|
|
|
|
|
|
|
|
# at end, fetchMore should be called
|
|
|
|
completionview.completion_item_focus('next')
|
|
|
|
assert cat.fetchMore.called
|
|
|
|
|
|
|
|
|
2016-08-20 07:01:38 +02:00
|
|
|
@pytest.mark.parametrize('show', ['always', 'auto', 'never'])
|
2016-08-22 04:57:16 +02:00
|
|
|
@pytest.mark.parametrize('rows', [[], ['Aa'], ['Aa', 'Bb']])
|
2016-08-20 07:01:38 +02:00
|
|
|
@pytest.mark.parametrize('quick_complete', [True, False])
|
2016-08-20 07:13:32 +02:00
|
|
|
def test_completion_show(show, rows, quick_complete, completionview,
|
|
|
|
config_stub):
|
|
|
|
"""Test that the completion widget is shown at appropriate times.
|
2016-08-20 07:01:38 +02:00
|
|
|
|
|
|
|
Args:
|
2016-08-20 07:13:32 +02:00
|
|
|
show: The completion show config setting.
|
|
|
|
rows: Each entry represents a completion category with only one item.
|
2017-07-04 15:43:54 +02:00
|
|
|
quick_complete: The `completion.quick` config setting.
|
2016-08-20 07:01:38 +02:00
|
|
|
"""
|
2017-07-03 18:42:41 +02:00
|
|
|
config_stub.val.completion.show = show
|
|
|
|
config_stub.val.completion.quick = quick_complete
|
2016-08-20 07:01:38 +02:00
|
|
|
|
2017-02-23 04:25:11 +01:00
|
|
|
model = completionmodel.CompletionModel()
|
2016-08-20 07:01:38 +02:00
|
|
|
for name in rows:
|
2017-03-03 14:31:28 +01:00
|
|
|
cat = listcategory.ListCategory('', [(name,)])
|
|
|
|
model.add_category(cat)
|
2016-08-20 07:01:38 +02:00
|
|
|
|
|
|
|
assert not completionview.isVisible()
|
2017-02-23 04:25:11 +01:00
|
|
|
completionview.set_model(model)
|
2016-08-22 04:57:16 +02:00
|
|
|
assert completionview.isVisible() == (show == 'always' and len(rows) > 0)
|
2016-08-20 07:01:38 +02:00
|
|
|
completionview.completion_item_focus('next')
|
2016-08-22 04:57:16 +02:00
|
|
|
expected = (show != 'never' and len(rows) > 0 and
|
|
|
|
not (quick_complete and len(rows) == 1))
|
2016-08-20 07:01:38 +02:00
|
|
|
assert completionview.isVisible() == expected
|
|
|
|
completionview.set_model(None)
|
2016-08-22 05:05:56 +02:00
|
|
|
completionview.completion_item_focus('next')
|
2016-08-20 07:01:38 +02:00
|
|
|
assert not completionview.isVisible()
|
2017-02-13 00:03:00 +01:00
|
|
|
|
|
|
|
|
2017-03-03 14:31:28 +01:00
|
|
|
def test_completion_item_del(completionview):
|
2017-02-13 00:03:00 +01:00
|
|
|
"""Test that completion_item_del invokes delete_cur_item in the model."""
|
2017-07-03 14:20:31 +02:00
|
|
|
func = mock.Mock(spec=[])
|
2017-06-21 05:04:03 +02:00
|
|
|
model = completionmodel.CompletionModel()
|
|
|
|
cat = listcategory.ListCategory('', [('foo', 'bar')], delete_func=func)
|
|
|
|
model.add_category(cat)
|
2017-03-03 14:31:28 +01:00
|
|
|
completionview.set_model(model)
|
2017-02-13 00:03:00 +01:00
|
|
|
completionview.completion_item_focus('next')
|
|
|
|
completionview.completion_item_del()
|
2017-06-21 05:04:03 +02:00
|
|
|
func.assert_called_once_with(['foo', 'bar'])
|
2017-02-13 00:03:00 +01:00
|
|
|
|
|
|
|
|
2017-03-03 14:31:28 +01:00
|
|
|
def test_completion_item_del_no_selection(completionview):
|
2017-06-21 05:04:03 +02:00
|
|
|
"""Test that completion_item_del with an invalid index."""
|
2017-07-03 14:20:31 +02:00
|
|
|
func = mock.Mock(spec=[])
|
2017-03-03 14:31:28 +01:00
|
|
|
model = completionmodel.CompletionModel()
|
2017-06-21 05:04:03 +02:00
|
|
|
cat = listcategory.ListCategory('', [('foo',)], delete_func=func)
|
|
|
|
model.add_category(cat)
|
2017-03-03 14:31:28 +01:00
|
|
|
completionview.set_model(model)
|
2018-11-29 14:09:06 +01:00
|
|
|
with pytest.raises(cmdutils.CommandError, match='No item selected!'):
|
2017-02-13 00:03:00 +01:00
|
|
|
completionview.completion_item_del()
|
2017-09-22 19:58:38 +02:00
|
|
|
func.assert_not_called()
|
2017-07-24 14:14:34 +02:00
|
|
|
|
|
|
|
|
2017-11-05 13:39:20 +01:00
|
|
|
@pytest.mark.parametrize('sel', [True, False])
|
|
|
|
def test_completion_item_yank(completionview, mocker, sel):
|
|
|
|
"""Test that completion_item_yank invokes delete_cur_item in the model."""
|
|
|
|
m = mocker.patch(
|
|
|
|
'qutebrowser.completion.completionwidget.utils',
|
|
|
|
autospec=True)
|
|
|
|
model = completionmodel.CompletionModel()
|
|
|
|
cat = listcategory.ListCategory('', [('foo', 'bar')])
|
|
|
|
model.add_category(cat)
|
|
|
|
|
|
|
|
completionview.set_model(model)
|
|
|
|
completionview.completion_item_focus('next')
|
|
|
|
completionview.completion_item_yank(sel)
|
|
|
|
|
|
|
|
m.set_clipboard.assert_called_once_with('foo', sel)
|
|
|
|
|
|
|
|
|
2017-11-13 19:25:18 +01:00
|
|
|
@pytest.mark.parametrize('sel', [True, False])
|
|
|
|
def test_completion_item_yank_selected(completionview, status_command_stub,
|
|
|
|
mocker, sel):
|
|
|
|
"""Test that completion_item_yank yanks selected text."""
|
|
|
|
m = mocker.patch(
|
|
|
|
'qutebrowser.completion.completionwidget.utils',
|
|
|
|
autospec=True)
|
|
|
|
model = completionmodel.CompletionModel()
|
|
|
|
cat = listcategory.ListCategory('', [('foo', 'bar')])
|
|
|
|
model.add_category(cat)
|
|
|
|
|
|
|
|
completionview.set_model(model)
|
|
|
|
completionview.completion_item_focus('next')
|
|
|
|
|
|
|
|
status_command_stub.selectedText = mock.Mock(return_value='something')
|
|
|
|
completionview.completion_item_yank(sel)
|
|
|
|
|
|
|
|
m.set_clipboard.assert_called_once_with('something', sel)
|
|
|
|
|
|
|
|
|
2017-07-24 14:14:34 +02:00
|
|
|
def test_resize_no_model(completionview, qtbot):
|
|
|
|
"""Ensure no crash if resizeEvent is triggered with no model (#2854)."""
|
|
|
|
completionview.resizeEvent(None)
|