qutebrowser/tests/unit/keyinput/test_basekeyparser.py

309 lines
12 KiB
Python
Raw Normal View History

2014-06-19 09:04:37 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2018-02-05 12:19:50 +01:00
# Copyright 2014-2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org>:
2014-05-12 17:39:37 +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 BaseKeyParser."""
2014-08-26 19:10:14 +02:00
from unittest import mock
2014-05-12 17:39:37 +02:00
from PyQt5.QtCore import Qt
2015-04-04 17:29:17 +02:00
import pytest
2014-05-12 17:39:37 +02:00
from qutebrowser.keyinput import basekeyparser, keyutils
2014-08-26 19:10:14 +02:00
# Alias because we need this a lot in here.
def keyseq(s):
return keyutils.KeySequence.parse(s)
@pytest.fixture
def keyparser(key_config_stub):
2015-08-26 21:39:28 +02:00
"""Fixture providing a BaseKeyParser supporting count/chains."""
kp = basekeyparser.BaseKeyParser(0, supports_count=True)
2015-08-26 21:39:28 +02:00
kp.execute = mock.Mock()
yield kp
2015-04-04 17:29:17 +02:00
@pytest.fixture
2018-03-04 19:38:15 +01:00
def handle_text(fake_keyevent, keyparser):
2015-08-26 21:39:28 +02:00
"""Helper function to handle multiple fake keypresses.
Automatically uses the keyparser of the current test via the keyparser
fixture.
"""
def func(*args):
2018-03-04 19:38:15 +01:00
for enumval in args:
keyparser.handle(fake_keyevent(enumval))
2015-08-26 21:39:28 +02:00
return func
class TestDebugLog:
"""Make sure _debug_log only logs when do_log is set."""
2015-08-26 21:39:28 +02:00
def test_log(self, keyparser, caplog):
keyparser._debug_log('foo')
assert len(caplog.records) == 1
record = caplog.records[0]
assert record.message == 'foo'
2015-08-26 21:39:28 +02:00
def test_no_log(self, keyparser, caplog):
keyparser.do_log = False
keyparser._debug_log('foo')
assert not caplog.records
@pytest.mark.parametrize('input_key, supports_count, count, command', [
2015-08-26 21:39:28 +02:00
# (input_key, supports_count, expected)
('10', True, '10', ''),
('10g', True, '10', 'g'),
('10e4g', True, '4', 'g'),
('g', True, '', 'g'),
2018-03-04 19:38:15 +01:00
('0', True, '', ''),
('10g', False, '', 'g'),
2015-08-26 21:39:28 +02:00
])
def test_split_count(config_stub, key_config_stub,
input_key, supports_count, count, command):
2015-08-26 21:39:28 +02:00
kp = basekeyparser.BaseKeyParser(0, supports_count=supports_count)
kp._read_config('normal')
for info in keyseq(input_key):
kp.handle(info.to_event())
assert kp._count == count
assert kp._sequence == keyseq(command)
2014-05-27 13:06:13 +02:00
2014-05-12 17:39:37 +02:00
@pytest.mark.usefixtures('keyinput_bindings')
2015-04-04 17:29:17 +02:00
class TestReadConfig:
2014-05-12 17:39:37 +02:00
2015-08-26 21:39:28 +02:00
def test_read_config_invalid(self, keyparser):
"""Test reading config without setting modename before."""
2015-04-04 17:29:17 +02:00
with pytest.raises(ValueError):
keyparser._read_config()
2014-05-12 17:39:37 +02:00
2015-08-26 21:39:28 +02:00
def test_read_config_modename(self, keyparser):
"""Test reading config with _modename set."""
2015-08-26 21:39:28 +02:00
keyparser._modename = 'normal'
keyparser._read_config()
assert keyseq('a') in keyparser.bindings
2015-08-26 21:39:28 +02:00
def test_read_config_valid(self, keyparser):
2014-05-27 13:06:13 +02:00
"""Test reading config."""
keyparser._read_config('prompt')
assert keyseq('ccc') in keyparser.bindings
assert keyseq('<ctrl+a>') in keyparser.bindings
keyparser._read_config('command')
assert keyseq('ccc') not in keyparser.bindings
assert keyseq('<ctrl+a>') not in keyparser.bindings
assert keyseq('foo') in keyparser.bindings
assert keyseq('<ctrl+x>') in keyparser.bindings
2015-08-26 21:39:28 +02:00
def test_read_config_modename_none(self, keyparser):
2015-08-26 21:39:28 +02:00
assert keyparser._modename is None
2015-08-24 18:12:12 +02:00
# No config set so self._modename is None
with pytest.raises(ValueError, match="read_config called with no mode "
"given, but None defined so far!"):
keyparser._read_config(None)
2015-08-26 21:39:28 +02:00
@pytest.mark.parametrize('mode, changed_mode, expected', [
('normal', 'normal', True), ('normal', 'command', False),
2015-08-26 21:39:28 +02:00
])
def test_read_config(self, keyparser, key_config_stub,
mode, changed_mode, expected):
keyparser._read_config(mode)
2015-08-26 21:39:28 +02:00
# Sanity checks
assert keyseq('a') in keyparser.bindings
assert keyseq('new') not in keyparser.bindings
2015-08-26 21:39:28 +02:00
key_config_stub.bind(keyseq('new'), 'message-info new',
mode=changed_mode)
2015-08-26 21:39:28 +02:00
assert keyseq('a') in keyparser.bindings
assert (keyseq('new') in keyparser.bindings) == expected
2015-08-24 18:12:12 +02:00
2015-08-25 16:28:02 +02:00
2018-03-04 19:38:15 +01:00
class TestHandle:
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
@pytest.fixture(autouse=True)
def read_config(self, keyinput_bindings, keyparser):
keyparser._read_config('prompt')
2015-08-26 21:39:28 +02:00
2018-03-04 19:38:15 +01:00
def test_valid_key(self, fake_keyevent, keyparser):
2018-03-05 18:30:34 +01:00
keyparser.handle(fake_keyevent(Qt.Key_A, Qt.ControlModifier))
keyparser.handle(fake_keyevent(Qt.Key_X, Qt.ControlModifier))
2018-02-27 10:35:19 +01:00
keyparser.execute.assert_called_once_with('message-info ctrla', None)
2018-03-04 19:38:15 +01:00
assert not keyparser._sequence
2018-03-04 19:38:15 +01:00
def test_valid_key_count(self, fake_keyevent, keyparser):
keyparser.handle(fake_keyevent(Qt.Key_5))
2018-03-05 18:30:34 +01:00
keyparser.handle(fake_keyevent(Qt.Key_A, Qt.ControlModifier))
2018-02-27 10:35:19 +01:00
keyparser.execute.assert_called_once_with('message-info ctrla', 5)
2015-08-26 21:39:28 +02:00
2018-03-04 19:38:15 +01:00
@pytest.mark.parametrize('keys', [
[(Qt.Key_B, Qt.NoModifier), (Qt.Key_C, Qt.NoModifier)],
[(Qt.Key_A, Qt.ControlModifier | Qt.AltModifier)],
# Only modifier
[(Qt.Key_Shift, Qt.ShiftModifier)],
])
def test_invalid_keys(self, fake_keyevent, keyparser, keys):
for key, modifiers in keys:
keyparser.handle(fake_keyevent(key, modifiers))
2015-08-26 21:39:28 +02:00
assert not keyparser.execute.called
assert not keyparser._sequence
2015-08-26 21:39:28 +02:00
def test_dry_run(self, fake_keyevent, keyparser):
keyparser.handle(fake_keyevent(Qt.Key_B))
keyparser.handle(fake_keyevent(Qt.Key_A), dry_run=True)
assert not keyparser.execute.called
assert keyparser._sequence
def test_dry_run_count(self, fake_keyevent, keyparser):
2018-03-08 06:41:15 +01:00
keyparser.handle(fake_keyevent(Qt.Key_9), dry_run=True)
assert not keyparser._count
def test_invalid_key(self, fake_keyevent, keyparser):
keyparser.handle(fake_keyevent(Qt.Key_B))
keyparser.handle(fake_keyevent(0x0))
assert not keyparser._sequence
2015-08-26 21:39:28 +02:00
def test_valid_keychain(self, handle_text, keyparser):
# Press 'x' which is ignored because of no match
2018-03-04 19:38:15 +01:00
handle_text(Qt.Key_X,
2015-08-26 21:39:28 +02:00
# Then start the real chain
2018-03-04 19:38:15 +01:00
Qt.Key_B, Qt.Key_A)
2018-02-27 10:35:19 +01:00
keyparser.execute.assert_called_with('message-info ba', None)
assert not keyparser._sequence
2015-08-26 21:39:28 +02:00
@pytest.mark.parametrize('key, number', [(Qt.Key_0, 0), (Qt.Key_1, 1)])
def test_number_press(self, handle_text, keyparser, key, number):
handle_text(key)
command = 'message-info {}'.format(number)
keyparser.execute.assert_called_once_with(command, None)
assert not keyparser._sequence
def test_umlauts(self, handle_text, keyparser, config_stub):
config_stub.val.bindings.commands = {'normal': {'ü': 'message-info ü'}}
keyparser._read_config('normal')
handle_text(Qt.Key_Udiaeresis)
keyparser.execute.assert_called_once_with('message-info ü', None)
def test_mapping(self, config_stub, handle_text, keyparser):
2018-03-04 19:38:15 +01:00
handle_text(Qt.Key_X)
2018-02-27 10:35:19 +01:00
keyparser.execute.assert_called_once_with('message-info a', None)
def test_binding_and_mapping(self, config_stub, handle_text, keyparser):
"""with a conflicting binding/mapping, the binding should win."""
2018-03-04 19:38:15 +01:00
handle_text(Qt.Key_B)
assert not keyparser.execute.called
def test_mapping_in_key_chain(self, config_stub, handle_text, keyparser):
"""A mapping should work even as part of a keychain."""
config_stub.val.bindings.commands = {'normal':
{'aa': 'message-info aa'}}
keyparser._read_config('normal')
handle_text(Qt.Key_A, Qt.Key_X)
keyparser.execute.assert_called_once_with('message-info aa', None)
2018-03-04 19:38:15 +01:00
def test_binding_with_shift(self, keyparser, fake_keyevent):
"""Simulate a binding which involves shift."""
2018-03-04 19:38:15 +01:00
for key, modifiers in [(Qt.Key_Y, Qt.NoModifier),
(Qt.Key_Shift, Qt.ShiftModifier),
(Qt.Key_Y, Qt.ShiftModifier)]:
keyparser.handle(fake_keyevent(key, modifiers))
keyparser.execute.assert_called_once_with('yank -s', None)
def test_partial_before_full_match(self, keyparser, fake_keyevent,
config_stub):
"""Make sure full matches always take precedence over partial ones."""
config_stub.val.bindings.commands = {
'normal': {
'ab': 'message-info bar',
'a': 'message-info foo'
}
}
keyparser._read_config('normal')
keyparser.handle(fake_keyevent(Qt.Key_A))
keyparser.execute.assert_called_once_with('message-info foo', None)
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
class TestCount:
2014-05-12 17:39:37 +02:00
"""Test execute() with counts."""
2015-04-04 17:29:17 +02:00
@pytest.fixture(autouse=True)
def read_keyparser_config(self, keyinput_bindings, keyparser):
keyparser._read_config('prompt')
2014-05-12 17:39:37 +02:00
2015-08-26 21:39:28 +02:00
def test_no_count(self, handle_text, keyparser):
2014-05-27 13:06:13 +02:00
"""Test with no count added."""
2018-03-04 19:38:15 +01:00
handle_text(Qt.Key_B, Qt.Key_A)
2018-02-27 10:35:19 +01:00
keyparser.execute.assert_called_once_with('message-info ba', None)
assert not keyparser._sequence
2015-08-26 21:39:28 +02:00
def test_count_0(self, handle_text, keyparser):
2018-03-04 19:38:15 +01:00
handle_text(Qt.Key_0, Qt.Key_B, Qt.Key_A)
2018-02-27 10:35:19 +01:00
calls = [mock.call('message-info 0', None),
mock.call('message-info ba', None)]
keyparser.execute.assert_has_calls(calls)
assert not keyparser._sequence
2015-08-26 21:39:28 +02:00
def test_count_42(self, handle_text, keyparser):
2018-03-04 19:38:15 +01:00
handle_text(Qt.Key_4, Qt.Key_2, Qt.Key_B, Qt.Key_A)
2018-02-27 10:35:19 +01:00
keyparser.execute.assert_called_once_with('message-info ba', 42)
assert not keyparser._sequence
2015-08-26 21:39:28 +02:00
def test_count_42_invalid(self, handle_text, keyparser):
2014-05-12 17:39:37 +02:00
# Invalid call with ccx gets ignored
2018-03-04 19:38:15 +01:00
handle_text(Qt.Key_4, Qt.Key_2, Qt.Key_C, Qt.Key_C, Qt.Key_X)
2015-08-26 21:39:28 +02:00
assert not keyparser.execute.called
assert not keyparser._sequence
2014-05-12 17:39:37 +02:00
# Valid call with ccc gets the correct count
2018-03-04 19:38:15 +01:00
handle_text(Qt.Key_2, Qt.Key_3, Qt.Key_C, Qt.Key_C, Qt.Key_C)
2018-02-27 10:35:19 +01:00
keyparser.execute.assert_called_once_with('message-info ccc', 23)
assert not keyparser._sequence
2015-08-25 10:28:46 +02:00
def test_count_keystring_update(self, qtbot, handle_text, keyparser):
"""Make sure the keystring is updated correctly when entering count."""
2018-03-08 06:41:15 +01:00
with qtbot.waitSignals([keyparser.keystring_updated,
keyparser.keystring_updated]) as blocker:
handle_text(Qt.Key_4, Qt.Key_2)
sig1, sig2 = blocker.all_signals_and_args
assert sig1.args == ('4',)
assert sig2.args == ('42',)
2015-08-25 10:28:46 +02:00
2015-08-26 21:39:28 +02:00
def test_clear_keystring(qtbot, keyparser):
"""Test that the keystring is cleared and the signal is emitted."""
2018-02-27 08:53:28 +01:00
keyparser._sequence = keyseq('test')
2018-03-04 19:38:15 +01:00
keyparser._count = '23'
2015-08-26 21:39:28 +02:00
with qtbot.waitSignal(keyparser.keystring_updated):
keyparser.clear_keystring()
assert not keyparser._sequence
2018-03-04 19:38:15 +01:00
assert not keyparser._count
def test_clear_keystring_empty(qtbot, keyparser):
"""Test that no signal is emitted when clearing an empty keystring.."""
keyparser._sequence = keyseq('')
with qtbot.assert_not_emitted(keyparser.keystring_updated):
keyparser.clear_keystring()