2014-06-19 09:04:37 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2017-05-09 21:37:03 +02:00
|
|
|
# Copyright 2014-2017 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-12 16:28:32 +02:00
|
|
|
import logging
|
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
|
|
|
|
2014-08-26 19:10:14 +02:00
|
|
|
from qutebrowser.keyinput import basekeyparser
|
2015-08-22 23:26:13 +02:00
|
|
|
from qutebrowser.utils import utils
|
2014-08-26 19:10:14 +02:00
|
|
|
|
|
|
|
|
2016-08-22 07:40:24 +02:00
|
|
|
@pytest.fixture
|
2017-07-03 21:09:16 +02:00
|
|
|
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, supports_chains=True)
|
|
|
|
kp.execute = mock.Mock()
|
|
|
|
yield kp
|
2014-09-09 21:40:16 +02:00
|
|
|
|
|
|
|
|
2015-04-04 17:29:17 +02:00
|
|
|
@pytest.fixture
|
2015-08-26 21:39:28 +02:00
|
|
|
def handle_text(fake_keyevent_factory, keyparser):
|
|
|
|
"""Helper function to handle multiple fake keypresses.
|
|
|
|
|
|
|
|
Automatically uses the keyparser of the current test via the keyparser
|
|
|
|
fixture.
|
|
|
|
"""
|
|
|
|
def func(*args):
|
|
|
|
for enumval, text in args:
|
|
|
|
keyparser.handle(fake_keyevent_factory(enumval, text=text))
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('count, chains, count_expected, chains_expected', [
|
|
|
|
(True, False, True, False),
|
|
|
|
(False, True, False, True),
|
|
|
|
(None, True, True, True),
|
|
|
|
])
|
2017-07-03 21:09:16 +02:00
|
|
|
def test_supports_args(config_stub, count, chains, count_expected,
|
|
|
|
chains_expected):
|
2015-08-26 21:39:28 +02:00
|
|
|
kp = basekeyparser.BaseKeyParser(
|
|
|
|
0, supports_count=count, supports_chains=chains)
|
|
|
|
assert kp._supports_count == count_expected
|
|
|
|
assert kp._supports_chains == chains_expected
|
2014-09-24 23:11:17 +02:00
|
|
|
|
|
|
|
|
2015-08-26 20:38:29 +02:00
|
|
|
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')
|
2015-11-11 19:57:03 +01:00
|
|
|
assert len(caplog.records) == 1
|
|
|
|
record = caplog.records[0]
|
2015-08-26 20:38:29 +02:00
|
|
|
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')
|
2015-11-11 19:57:03 +01:00
|
|
|
assert not caplog.records
|
2015-08-26 20:38:29 +02:00
|
|
|
|
|
|
|
|
2015-08-26 21:39:28 +02:00
|
|
|
@pytest.mark.parametrize('input_key, supports_count, expected', [
|
|
|
|
# (input_key, supports_count, expected)
|
|
|
|
('10', True, (10, '')),
|
|
|
|
('10foo', True, (10, 'foo')),
|
|
|
|
('-1foo', True, (None, '-1foo')),
|
|
|
|
('10e4foo', True, (10, 'e4foo')),
|
|
|
|
('foo', True, (None, 'foo')),
|
|
|
|
('10foo', False, (None, '10foo')),
|
|
|
|
])
|
2017-07-03 21:09:16 +02:00
|
|
|
def test_split_count(config_stub, input_key, supports_count, expected):
|
2015-08-26 21:39:28 +02:00
|
|
|
kp = basekeyparser.BaseKeyParser(0, supports_count=supports_count)
|
2017-09-22 15:28:45 +02:00
|
|
|
assert kp._split_count(input_key) == expected
|
2014-05-27 13:06:13 +02:00
|
|
|
|
2014-05-12 17:39:37 +02:00
|
|
|
|
2017-07-03 21:09:16 +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):
|
2017-07-03 21:09:16 +02:00
|
|
|
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):
|
2015-08-25 12:04:22 +02:00
|
|
|
"""Test reading config with _modename set."""
|
2015-08-26 21:39:28 +02:00
|
|
|
keyparser._modename = 'normal'
|
2017-07-03 21:09:16 +02:00
|
|
|
keyparser._read_config()
|
2015-08-26 21:39:28 +02:00
|
|
|
assert 'a' in keyparser.bindings
|
2015-08-25 12:04:22 +02:00
|
|
|
|
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."""
|
2017-07-03 21:09:16 +02:00
|
|
|
keyparser._read_config('prompt')
|
2015-08-26 21:39:28 +02:00
|
|
|
assert 'ccc' in keyparser.bindings
|
|
|
|
assert 'ctrl+a' in keyparser.special_bindings
|
2017-07-03 21:09:16 +02:00
|
|
|
keyparser._read_config('command')
|
2015-08-26 21:39:28 +02:00
|
|
|
assert 'ccc' not in keyparser.bindings
|
|
|
|
assert 'ctrl+a' not in keyparser.special_bindings
|
|
|
|
assert 'foo' in keyparser.bindings
|
|
|
|
assert 'ctrl+x' in keyparser.special_bindings
|
|
|
|
|
2017-07-03 21:09:16 +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
|
2017-07-03 21:09:16 +02:00
|
|
|
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
|
|
|
|
2017-07-03 21:09:16 +02:00
|
|
|
@pytest.mark.parametrize('mode, changed_mode, expected', [
|
|
|
|
('normal', 'normal', True), ('normal', 'command', False),
|
2015-08-26 21:39:28 +02:00
|
|
|
])
|
2017-07-03 21:09:16 +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 'a' in keyparser.bindings
|
|
|
|
assert 'new' not in keyparser.bindings
|
|
|
|
|
2017-07-03 21:09:16 +02:00
|
|
|
key_config_stub.bind('new', 'message-info new', mode=changed_mode)
|
2015-08-26 21:39:28 +02:00
|
|
|
|
2017-07-03 21:09:16 +02:00
|
|
|
assert 'a' in keyparser.bindings
|
|
|
|
assert ('new' in keyparser.bindings) == expected
|
2015-08-24 18:12:12 +02:00
|
|
|
|
2015-08-26 21:39:28 +02:00
|
|
|
@pytest.mark.parametrize('warn_on_keychains', [True, False])
|
|
|
|
def test_warn_on_keychains(self, caplog, warn_on_keychains):
|
2015-08-25 16:28:02 +02:00
|
|
|
"""Test _warn_on_keychains."""
|
2015-08-26 21:39:28 +02:00
|
|
|
kp = basekeyparser.BaseKeyParser(
|
|
|
|
0, supports_count=False, supports_chains=False)
|
|
|
|
kp._warn_on_keychains = warn_on_keychains
|
2015-08-25 16:28:02 +02:00
|
|
|
|
2015-11-11 19:57:03 +01:00
|
|
|
with caplog.at_level(logging.WARNING):
|
2017-07-03 21:09:16 +02:00
|
|
|
kp._read_config('normal')
|
2015-08-25 16:28:02 +02:00
|
|
|
|
2015-11-11 19:57:03 +01:00
|
|
|
assert bool(caplog.records) == warn_on_keychains
|
2015-08-25 16:28:02 +02:00
|
|
|
|
|
|
|
|
2015-04-04 17:29:17 +02:00
|
|
|
class TestSpecialKeys:
|
2014-05-12 17:39:37 +02:00
|
|
|
|
2015-08-26 21:39:28 +02:00
|
|
|
"""Check execute() with special keys."""
|
2014-05-12 17:39:37 +02:00
|
|
|
|
2015-04-04 17:29:17 +02:00
|
|
|
@pytest.fixture(autouse=True)
|
2017-07-03 21:09:16 +02:00
|
|
|
def read_config(self, keyinput_bindings, keyparser):
|
|
|
|
keyparser._read_config('prompt')
|
2015-08-26 21:39:28 +02:00
|
|
|
|
|
|
|
def test_valid_key(self, fake_keyevent_factory, keyparser):
|
2017-09-22 15:28:45 +02:00
|
|
|
modifier = Qt.MetaModifier if utils.is_mac else Qt.ControlModifier
|
2015-08-26 21:39:28 +02:00
|
|
|
keyparser.handle(fake_keyevent_factory(Qt.Key_A, modifier))
|
|
|
|
keyparser.handle(fake_keyevent_factory(Qt.Key_X, modifier))
|
|
|
|
keyparser.execute.assert_called_once_with(
|
2017-07-03 21:09:16 +02:00
|
|
|
'message-info ctrla', keyparser.Type.special, None)
|
2016-08-16 13:44:28 +02:00
|
|
|
|
|
|
|
def test_valid_key_count(self, fake_keyevent_factory, keyparser):
|
2017-09-22 15:28:45 +02:00
|
|
|
modifier = Qt.MetaModifier if utils.is_mac else Qt.ControlModifier
|
2016-08-16 13:44:28 +02:00
|
|
|
keyparser.handle(fake_keyevent_factory(5, text='5'))
|
|
|
|
keyparser.handle(fake_keyevent_factory(Qt.Key_A, modifier, text='A'))
|
|
|
|
keyparser.execute.assert_called_once_with(
|
2017-07-03 21:09:16 +02:00
|
|
|
'message-info ctrla', keyparser.Type.special, 5)
|
2015-08-26 21:39:28 +02:00
|
|
|
|
|
|
|
def test_invalid_key(self, fake_keyevent_factory, keyparser):
|
|
|
|
keyparser.handle(fake_keyevent_factory(
|
|
|
|
Qt.Key_A, (Qt.ControlModifier | Qt.AltModifier)))
|
|
|
|
assert not keyparser.execute.called
|
|
|
|
|
|
|
|
def test_keychain(self, fake_keyevent_factory, keyparser):
|
|
|
|
keyparser.handle(fake_keyevent_factory(Qt.Key_B))
|
|
|
|
keyparser.handle(fake_keyevent_factory(Qt.Key_A))
|
|
|
|
assert not keyparser.execute.called
|
|
|
|
|
|
|
|
def test_no_binding(self, monkeypatch, fake_keyevent_factory, keyparser):
|
2015-08-25 10:28:46 +02:00
|
|
|
monkeypatch.setattr(utils, 'keyevent_to_string', lambda binding: None)
|
2015-08-26 21:39:28 +02:00
|
|
|
keyparser.handle(fake_keyevent_factory(Qt.Key_A, Qt.NoModifier))
|
|
|
|
assert not keyparser.execute.called
|
2015-08-22 23:26:13 +02:00
|
|
|
|
2017-09-22 15:28:45 +02:00
|
|
|
def test_mapping(self, config_stub, fake_keyevent_factory, keyparser):
|
|
|
|
modifier = Qt.MetaModifier if utils.is_mac else Qt.ControlModifier
|
|
|
|
|
|
|
|
keyparser.handle(fake_keyevent_factory(Qt.Key_B, modifier))
|
|
|
|
keyparser.execute.assert_called_once_with(
|
|
|
|
'message-info ctrla', keyparser.Type.special, None)
|
|
|
|
|
|
|
|
def test_binding_and_mapping(self, config_stub, fake_keyevent_factory,
|
|
|
|
keyparser):
|
|
|
|
"""with a conflicting binding/mapping, the binding should win."""
|
|
|
|
modifier = Qt.MetaModifier if utils.is_mac else Qt.ControlModifier
|
|
|
|
|
|
|
|
keyparser.handle(fake_keyevent_factory(Qt.Key_A, modifier))
|
|
|
|
keyparser.execute.assert_called_once_with(
|
|
|
|
'message-info ctrla', keyparser.Type.special, None)
|
|
|
|
|
2014-05-12 17:39:37 +02:00
|
|
|
|
2015-04-04 17:29:17 +02:00
|
|
|
class TestKeyChain:
|
2014-05-12 17:39:37 +02:00
|
|
|
|
2015-08-26 21:39:28 +02:00
|
|
|
"""Test execute() with keychain support."""
|
2014-05-12 17:39:37 +02:00
|
|
|
|
2015-04-04 17:29:17 +02:00
|
|
|
@pytest.fixture(autouse=True)
|
2017-07-03 21:09:16 +02:00
|
|
|
def read_config(self, keyinput_bindings, keyparser):
|
|
|
|
keyparser._read_config('prompt')
|
2015-08-26 21:39:28 +02:00
|
|
|
|
|
|
|
def test_valid_special_key(self, fake_keyevent_factory, keyparser):
|
2017-09-20 10:39:39 +02:00
|
|
|
if utils.is_mac:
|
2015-05-17 19:04:07 +02:00
|
|
|
modifier = Qt.MetaModifier
|
|
|
|
else:
|
|
|
|
modifier = Qt.ControlModifier
|
2015-08-26 21:39:28 +02:00
|
|
|
keyparser.handle(fake_keyevent_factory(Qt.Key_A, modifier))
|
|
|
|
keyparser.handle(fake_keyevent_factory(Qt.Key_X, modifier))
|
|
|
|
keyparser.execute.assert_called_once_with(
|
2017-07-03 21:09:16 +02:00
|
|
|
'message-info ctrla', keyparser.Type.special, None)
|
2015-08-26 21:39:28 +02:00
|
|
|
assert keyparser._keystring == ''
|
|
|
|
|
|
|
|
def test_invalid_special_key(self, fake_keyevent_factory, keyparser):
|
|
|
|
keyparser.handle(fake_keyevent_factory(
|
|
|
|
Qt.Key_A, (Qt.ControlModifier | Qt.AltModifier)))
|
|
|
|
assert not keyparser.execute.called
|
|
|
|
assert keyparser._keystring == ''
|
|
|
|
|
|
|
|
def test_valid_keychain(self, handle_text, keyparser):
|
2014-05-12 17:43:12 +02:00
|
|
|
# Press 'x' which is ignored because of no match
|
2015-08-26 21:39:28 +02:00
|
|
|
handle_text((Qt.Key_X, 'x'),
|
|
|
|
# Then start the real chain
|
|
|
|
(Qt.Key_B, 'b'), (Qt.Key_A, 'a'))
|
2017-09-22 15:28:45 +02:00
|
|
|
keyparser.execute.assert_called_with(
|
2017-07-03 21:09:16 +02:00
|
|
|
'message-info ba', keyparser.Type.chain, None)
|
2015-08-26 21:39:28 +02:00
|
|
|
assert keyparser._keystring == ''
|
|
|
|
|
2016-11-23 08:34:21 +01:00
|
|
|
def test_0_press(self, handle_text, keyparser):
|
|
|
|
handle_text((Qt.Key_0, '0'))
|
|
|
|
keyparser.execute.assert_called_once_with(
|
2017-07-03 21:09:16 +02:00
|
|
|
'message-info 0', keyparser.Type.chain, None)
|
2016-11-23 08:34:21 +01:00
|
|
|
assert keyparser._keystring == ''
|
|
|
|
|
2017-09-13 21:15:02 +02:00
|
|
|
def test_ambiguous_keychain(self, handle_text, keyparser):
|
2015-08-26 21:39:28 +02:00
|
|
|
handle_text((Qt.Key_A, 'a'))
|
|
|
|
assert keyparser.execute.called
|
|
|
|
|
|
|
|
def test_invalid_keychain(self, handle_text, keyparser):
|
|
|
|
handle_text((Qt.Key_B, 'b'))
|
|
|
|
handle_text((Qt.Key_C, 'c'))
|
|
|
|
assert keyparser._keystring == ''
|
|
|
|
|
2017-09-22 15:28:45 +02:00
|
|
|
def test_mapping(self, config_stub, handle_text, keyparser):
|
|
|
|
handle_text((Qt.Key_X, 'x'))
|
|
|
|
keyparser.execute.assert_called_once_with(
|
|
|
|
'message-info a', keyparser.Type.chain, None)
|
|
|
|
|
|
|
|
def test_binding_and_mapping(self, config_stub, handle_text, keyparser):
|
|
|
|
"""with a conflicting binding/mapping, the binding should win."""
|
|
|
|
handle_text((Qt.Key_B, 'b'))
|
|
|
|
assert not keyparser.execute.called
|
|
|
|
|
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)
|
2017-07-03 21:09:16 +02:00
|
|
|
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."""
|
2015-08-26 21:39:28 +02:00
|
|
|
handle_text((Qt.Key_B, 'b'), (Qt.Key_A, 'a'))
|
|
|
|
keyparser.execute.assert_called_once_with(
|
2017-07-03 21:09:16 +02:00
|
|
|
'message-info ba', keyparser.Type.chain, None)
|
2015-08-26 21:39:28 +02:00
|
|
|
assert keyparser._keystring == ''
|
|
|
|
|
|
|
|
def test_count_0(self, handle_text, keyparser):
|
|
|
|
handle_text((Qt.Key_0, '0'), (Qt.Key_B, 'b'), (Qt.Key_A, 'a'))
|
2017-07-03 21:09:16 +02:00
|
|
|
calls = [mock.call('message-info 0', keyparser.Type.chain, None),
|
|
|
|
mock.call('message-info ba', keyparser.Type.chain, None)]
|
2016-11-23 08:34:21 +01:00
|
|
|
keyparser.execute.assert_has_calls(calls)
|
2015-08-26 21:39:28 +02:00
|
|
|
assert keyparser._keystring == ''
|
|
|
|
|
|
|
|
def test_count_42(self, handle_text, keyparser):
|
|
|
|
handle_text((Qt.Key_4, '4'), (Qt.Key_2, '2'), (Qt.Key_B, 'b'),
|
|
|
|
(Qt.Key_A, 'a'))
|
|
|
|
keyparser.execute.assert_called_once_with(
|
2017-07-03 21:09:16 +02:00
|
|
|
'message-info ba', keyparser.Type.chain, 42)
|
2015-08-26 21:39:28 +02:00
|
|
|
assert keyparser._keystring == ''
|
|
|
|
|
|
|
|
def test_count_42_invalid(self, handle_text, keyparser):
|
2014-05-12 17:39:37 +02:00
|
|
|
# Invalid call with ccx gets ignored
|
2015-08-26 21:39:28 +02:00
|
|
|
handle_text((Qt.Key_4, '4'), (Qt.Key_2, '2'), (Qt.Key_C, 'c'),
|
|
|
|
(Qt.Key_C, 'c'), (Qt.Key_X, 'x'))
|
|
|
|
assert not keyparser.execute.called
|
|
|
|
assert keyparser._keystring == ''
|
2014-05-12 17:39:37 +02:00
|
|
|
# Valid call with ccc gets the correct count
|
2015-08-26 21:39:28 +02:00
|
|
|
handle_text((Qt.Key_6, '2'), (Qt.Key_2, '3'), (Qt.Key_C, 'c'),
|
|
|
|
(Qt.Key_C, 'c'), (Qt.Key_C, 'c'))
|
|
|
|
keyparser.execute.assert_called_once_with(
|
2017-07-03 21:09:16 +02:00
|
|
|
'message-info ccc', keyparser.Type.chain, 23)
|
2015-08-26 21:39:28 +02:00
|
|
|
assert keyparser._keystring == ''
|
2015-08-25 10:28:46 +02:00
|
|
|
|
|
|
|
|
2015-08-26 21:39:28 +02:00
|
|
|
def test_clear_keystring(qtbot, keyparser):
|
2016-02-10 19:18:47 +01:00
|
|
|
"""Test that the keystring is cleared and the signal is emitted."""
|
2015-08-26 21:39:28 +02:00
|
|
|
keyparser._keystring = 'test'
|
|
|
|
with qtbot.waitSignal(keyparser.keystring_updated):
|
|
|
|
keyparser.clear_keystring()
|
|
|
|
assert keyparser._keystring == ''
|