qutebrowser/tests/keyinput/test_basekeyparser.py

274 lines
10 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:
2015-01-03 15:51:31 +01:00
# Copyright 2014-2015 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/>.
2014-07-01 07:57:19 +02:00
# pylint: disable=protected-access
2014-05-12 17:39:37 +02:00
"""Tests for BaseKeyParser."""
2015-05-17 19:04:07 +02:00
import sys
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
from qutebrowser.utils import log
2014-08-26 19:10:14 +02:00
CONFIG = {'input': {'timeout': 100}}
2015-04-04 17:29:17 +02:00
@pytest.fixture
def mock_timer(monkeypatch, stubs):
2015-04-05 20:30:31 +02:00
"""Mock the Timer class used by the usertypes module with a stub."""
monkeypatch.setattr('qutebrowser.keyinput.basekeyparser.usertypes.Timer',
stubs.FakeTimer)
2014-09-24 23:11:17 +02:00
2015-04-04 17:29:17 +02:00
class TestSplitCount:
2014-05-12 17:39:37 +02:00
2014-05-27 13:06:13 +02:00
"""Test the _split_count method.
Class Attributes:
TESTS: list of parameters for the tests, as tuples of
(input_key, supports_count, expected)
2014-05-27 13:06:13 +02:00
"""
2014-05-12 17:39:37 +02:00
TESTS = [
# (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')),
]
@pytest.mark.parametrize('input_key, supports_count, expected', TESTS)
def test_splitcount(self, input_key, supports_count, expected):
2014-05-27 13:06:13 +02:00
"""Test split_count with only a count."""
kp = basekeyparser.BaseKeyParser(0, supports_count=supports_count)
kp._keystring = input_key
assert kp._split_count() == expected
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
@pytest.mark.usefixtures('fake_keyconfig', 'mock_timer')
class TestReadConfig:
2014-05-12 17:39:37 +02:00
"""Test reading the config."""
def test_read_config_invalid(self):
2014-05-27 13:06:13 +02:00
"""Test reading config without setting it before."""
2014-09-28 22:24:49 +02:00
kp = basekeyparser.BaseKeyParser(0)
2015-04-04 17:29:17 +02:00
with pytest.raises(ValueError):
2014-05-12 17:39:37 +02:00
kp.read_config()
def test_read_config_valid(self):
2014-05-27 13:06:13 +02:00
"""Test reading config."""
2014-09-28 22:24:49 +02:00
kp = basekeyparser.BaseKeyParser(0, supports_count=True,
2014-05-12 17:39:37 +02:00
supports_chains=True)
kp.read_config('test')
2015-04-04 17:29:17 +02:00
assert 'ccc' in kp.bindings
assert 'ctrl+a' in kp.special_bindings
2014-05-12 17:39:37 +02:00
kp.read_config('test2')
2015-04-04 17:29:17 +02:00
assert 'ccc' not in kp.bindings
assert 'ctrl+a' not in kp.special_bindings
assert 'foo' in kp.bindings
assert 'ctrl+x' in kp.special_bindings
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
@pytest.mark.usefixtures('mock_timer')
class TestSpecialKeys:
2014-05-12 17:39:37 +02:00
2014-05-27 13:06:13 +02:00
"""Check execute() with special keys.
Attributes:
kp: The BaseKeyParser to be tested.
"""
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
@pytest.fixture(autouse=True)
def setup(self, caplog, fake_keyconfig):
2014-09-28 22:24:49 +02:00
self.kp = basekeyparser.BaseKeyParser(0)
2014-08-26 19:10:14 +02:00
self.kp.execute = mock.Mock()
2015-04-04 17:29:17 +02:00
with caplog.atLevel(logging.WARNING, log.keyboard.name):
# Ignoring keychain 'ccc' in mode 'test' because keychains are not
# supported there.
self.kp.read_config('test')
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
def test_valid_key(self, fake_keyevent_factory):
2014-05-27 13:06:13 +02:00
"""Test a valid special keyevent."""
2015-05-17 19:04:07 +02:00
if sys.platform == 'darwin':
modifier = Qt.MetaModifier
else:
modifier = Qt.ControlModifier
self.kp.handle(fake_keyevent_factory(Qt.Key_A, modifier))
self.kp.handle(fake_keyevent_factory(Qt.Key_X, modifier))
2014-05-12 17:39:37 +02:00
self.kp.execute.assert_called_once_with('ctrla', self.kp.Type.special)
2015-04-04 17:29:17 +02:00
def test_invalid_key(self, fake_keyevent_factory):
2014-05-27 13:06:13 +02:00
"""Test an invalid special keyevent."""
2015-04-04 17:29:17 +02:00
self.kp.handle(fake_keyevent_factory(Qt.Key_A, (Qt.ControlModifier |
2014-08-26 19:10:14 +02:00
Qt.AltModifier)))
2015-04-04 17:29:17 +02:00
assert not self.kp.execute.called
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
def test_keychain(self, fake_keyevent_factory):
2014-05-27 13:06:13 +02:00
"""Test a keychain."""
2015-04-04 17:29:17 +02:00
self.kp.handle(fake_keyevent_factory(Qt.Key_B))
self.kp.handle(fake_keyevent_factory(Qt.Key_A))
assert not self.kp.execute.called
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
@pytest.mark.usefixtures('mock_timer')
class TestKeyChain:
2014-05-12 17:39:37 +02:00
2014-05-27 13:06:13 +02:00
"""Test execute() with keychain support.
Attributes:
kp: The BaseKeyParser to be tested.
"""
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
@pytest.fixture(autouse=True)
def setup(self, fake_keyconfig):
2014-05-27 13:06:13 +02:00
"""Set up mocks and read the test config."""
2014-09-28 22:24:49 +02:00
self.kp = basekeyparser.BaseKeyParser(0, supports_chains=True,
2014-05-12 17:39:37 +02:00
supports_count=False)
2014-08-26 19:10:14 +02:00
self.kp.execute = mock.Mock()
2014-05-12 17:39:37 +02:00
self.kp.read_config('test')
2015-04-04 17:29:17 +02:00
def test_valid_special_key(self, fake_keyevent_factory):
2014-05-27 13:06:13 +02:00
"""Test valid special key."""
2015-05-17 19:04:07 +02:00
if sys.platform == 'darwin':
modifier = Qt.MetaModifier
else:
modifier = Qt.ControlModifier
self.kp.handle(fake_keyevent_factory(Qt.Key_A, modifier))
self.kp.handle(fake_keyevent_factory(Qt.Key_X, modifier))
2014-05-12 17:39:37 +02:00
self.kp.execute.assert_called_once_with('ctrla', self.kp.Type.special)
2015-04-04 17:29:17 +02:00
assert self.kp._keystring == ''
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
def test_invalid_special_key(self, fake_keyevent_factory):
2014-05-27 13:06:13 +02:00
"""Test invalid special key."""
2015-04-04 17:29:17 +02:00
self.kp.handle(fake_keyevent_factory(Qt.Key_A, (Qt.ControlModifier |
2014-08-26 19:10:14 +02:00
Qt.AltModifier)))
2015-04-04 17:29:17 +02:00
assert not self.kp.execute.called
assert self.kp._keystring == ''
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
def test_keychain(self, fake_keyevent_factory):
2014-05-27 13:06:13 +02:00
"""Test valid keychain."""
# Press 'x' which is ignored because of no match
2015-04-04 17:29:17 +02:00
self.kp.handle(fake_keyevent_factory(Qt.Key_X, text='x'))
# Then start the real chain
2015-04-04 17:29:17 +02:00
self.kp.handle(fake_keyevent_factory(Qt.Key_B, text='b'))
self.kp.handle(fake_keyevent_factory(Qt.Key_A, text='a'))
2014-05-12 17:39:37 +02:00
self.kp.execute.assert_called_once_with('ba', self.kp.Type.chain, None)
2015-04-04 17:29:17 +02:00
assert self.kp._keystring == ''
2014-05-12 17:39:37 +02:00
def test_0(self, fake_keyevent_factory):
"""Test with 0 keypress."""
self.kp.handle(fake_keyevent_factory(Qt.Key_0, text='0'))
self.kp.execute.assert_called_once_with('0', self.kp.Type.chain, None)
assert self.kp._keystring == ''
def test_ambiguous_keychain(self, fake_keyevent_factory, config_stub,
monkeypatch):
2015-04-05 20:30:31 +02:00
"""Test ambiguous keychain."""
config_stub.data = CONFIG
monkeypatch.setattr('qutebrowser.keyinput.basekeyparser.config',
config_stub)
timer = self.kp._ambiguous_timer
2015-04-04 17:29:17 +02:00
assert not timer.isActive()
2015-04-05 20:30:31 +02:00
# We start with 'a' where the keychain gives us an ambiguous result.
2014-05-12 17:39:37 +02:00
# Then we check if the timer has been set up correctly
2015-04-04 17:29:17 +02:00
self.kp.handle(fake_keyevent_factory(Qt.Key_A, text='a'))
assert not self.kp.execute.called
assert timer.isSingleShot()
assert timer.interval() == 100
assert timer.isActive()
2014-05-12 17:39:37 +02:00
# Now we type an 'x' and check 'ax' has been executed and the timer
# stopped.
2015-04-04 17:29:17 +02:00
self.kp.handle(fake_keyevent_factory(Qt.Key_X, text='x'))
2014-05-12 17:39:37 +02:00
self.kp.execute.assert_called_once_with('ax', self.kp.Type.chain, None)
2015-04-04 17:29:17 +02:00
assert not timer.isActive()
assert self.kp._keystring == ''
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
def test_invalid_keychain(self, fake_keyevent_factory):
2014-05-27 13:06:13 +02:00
"""Test invalid keychain."""
2015-04-04 17:29:17 +02:00
self.kp.handle(fake_keyevent_factory(Qt.Key_B, text='b'))
self.kp.handle(fake_keyevent_factory(Qt.Key_C, text='c'))
assert self.kp._keystring == ''
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
@pytest.mark.usefixtures('mock_timer')
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 setup(self, fake_keyconfig):
2014-09-28 22:24:49 +02:00
self.kp = basekeyparser.BaseKeyParser(0, supports_chains=True,
2014-05-12 17:39:37 +02:00
supports_count=True)
2014-08-26 19:10:14 +02:00
self.kp.execute = mock.Mock()
2014-05-12 17:39:37 +02:00
self.kp.read_config('test')
2015-04-04 17:29:17 +02:00
def test_no_count(self, fake_keyevent_factory):
2014-05-27 13:06:13 +02:00
"""Test with no count added."""
2015-04-04 17:29:17 +02:00
self.kp.handle(fake_keyevent_factory(Qt.Key_B, text='b'))
self.kp.handle(fake_keyevent_factory(Qt.Key_A, text='a'))
2014-05-12 17:39:37 +02:00
self.kp.execute.assert_called_once_with('ba', self.kp.Type.chain, None)
2015-04-04 17:29:17 +02:00
assert self.kp._keystring == ''
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
def test_count_0(self, fake_keyevent_factory):
2014-05-27 13:06:13 +02:00
"""Test with count=0."""
2015-04-04 17:29:17 +02:00
self.kp.handle(fake_keyevent_factory(Qt.Key_0, text='0'))
self.kp.handle(fake_keyevent_factory(Qt.Key_B, text='b'))
self.kp.handle(fake_keyevent_factory(Qt.Key_A, text='a'))
calls = [mock.call('0', self.kp.Type.chain, None),
mock.call('ba', self.kp.Type.chain, None)]
self.kp.execute.assert_has_calls(calls)
2015-04-04 17:29:17 +02:00
assert self.kp._keystring == ''
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
def test_count_42(self, fake_keyevent_factory):
2014-05-27 13:06:13 +02:00
"""Test with count=42."""
2015-04-04 17:29:17 +02:00
self.kp.handle(fake_keyevent_factory(Qt.Key_4, text='4'))
self.kp.handle(fake_keyevent_factory(Qt.Key_2, text='2'))
self.kp.handle(fake_keyevent_factory(Qt.Key_B, text='b'))
self.kp.handle(fake_keyevent_factory(Qt.Key_A, text='a'))
2014-05-12 17:39:37 +02:00
self.kp.execute.assert_called_once_with('ba', self.kp.Type.chain, 42)
2015-04-04 17:29:17 +02:00
assert self.kp._keystring == ''
2014-05-12 17:39:37 +02:00
2015-04-04 17:29:17 +02:00
def test_count_42_invalid(self, fake_keyevent_factory):
2014-05-27 13:06:13 +02:00
"""Test with count=42 and invalid command."""
2014-05-12 17:39:37 +02:00
# Invalid call with ccx gets ignored
2015-04-04 17:29:17 +02:00
self.kp.handle(fake_keyevent_factory(Qt.Key_4, text='4'))
self.kp.handle(fake_keyevent_factory(Qt.Key_2, text='2'))
self.kp.handle(fake_keyevent_factory(Qt.Key_B, text='c'))
self.kp.handle(fake_keyevent_factory(Qt.Key_A, text='c'))
self.kp.handle(fake_keyevent_factory(Qt.Key_A, text='x'))
assert not self.kp.execute.called
assert self.kp._keystring == ''
2014-05-12 17:39:37 +02:00
# Valid call with ccc gets the correct count
2015-04-04 17:29:17 +02:00
self.kp.handle(fake_keyevent_factory(Qt.Key_4, text='2'))
self.kp.handle(fake_keyevent_factory(Qt.Key_2, text='3'))
self.kp.handle(fake_keyevent_factory(Qt.Key_B, text='c'))
self.kp.handle(fake_keyevent_factory(Qt.Key_A, text='c'))
self.kp.handle(fake_keyevent_factory(Qt.Key_A, text='c'))
2014-05-12 17:39:37 +02:00
self.kp.execute.assert_called_once_with('ccc', self.kp.Type.chain, 23)
2015-04-04 17:29:17 +02:00
assert self.kp._keystring == ''