qutebrowser/test/keyinput/test_basekeyparser.py

300 lines
11 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."""
2014-08-12 16:28:32 +02:00
import logging
2014-05-12 17:39:37 +02:00
import unittest
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
2014-08-26 19:10:14 +02:00
from qutebrowser.keyinput import basekeyparser
from qutebrowser.test import stubs, helpers
from qutebrowser.utils import objreg, log
2014-08-26 19:10:14 +02:00
CONFIG = {'input': {'timeout': 100}}
BINDINGS = {'test': {'<Ctrl-a>': 'ctrla',
'a': 'a',
'ba': 'ba',
'ax': 'ax',
'ccc': 'ccc'},
'test2': {'foo': 'bar', '<Ctrl+X>': 'ctrlx'}}
2014-06-23 19:44:21 +02:00
2014-05-12 17:39:37 +02:00
2014-09-24 23:11:17 +02:00
fake_keyconfig = mock.Mock(spec=['get_bindings_for'])
fake_keyconfig.get_bindings_for.side_effect = lambda s: BINDINGS[s]
2014-06-23 19:44:21 +02:00
class SplitCountTests(unittest.TestCase):
2014-05-12 17:39:37 +02:00
2014-05-27 13:06:13 +02:00
"""Test the _split_count method.
Attributes:
kp: The BaseKeyParser we're testing.
"""
2014-05-12 17:39:37 +02:00
def setUp(self):
2014-09-28 22:24:49 +02:00
self.kp = basekeyparser.BaseKeyParser(0, supports_count=True)
2014-05-12 17:39:37 +02:00
def test_onlycount(self):
2014-05-27 13:06:13 +02:00
"""Test split_count with only a count."""
2014-05-12 17:39:37 +02:00
self.kp._keystring = '10'
self.assertEqual(self.kp._split_count(), (10, ''))
def test_normalcount(self):
2014-05-27 13:06:13 +02:00
"""Test split_count with count and text."""
2014-05-12 17:39:37 +02:00
self.kp._keystring = '10foo'
self.assertEqual(self.kp._split_count(), (10, 'foo'))
def test_minuscount(self):
2014-05-27 13:06:13 +02:00
"""Test split_count with a negative count."""
2014-05-12 17:39:37 +02:00
self.kp._keystring = '-1foo'
self.assertEqual(self.kp._split_count(), (None, '-1foo'))
def test_expcount(self):
2014-05-27 13:06:13 +02:00
"""Test split_count with an exponential count."""
2014-05-12 17:39:37 +02:00
self.kp._keystring = '10e4foo'
self.assertEqual(self.kp._split_count(), (10, 'e4foo'))
def test_nocount(self):
2014-05-27 13:06:13 +02:00
"""Test split_count with only a command."""
2014-05-12 17:39:37 +02:00
self.kp._keystring = 'foo'
self.assertEqual(self.kp._split_count(), (None, 'foo'))
def test_nosupport(self):
2014-05-27 13:06:13 +02:00
"""Test split_count with a count when counts aren't supported."""
2014-05-12 17:39:37 +02:00
self.kp._supports_count = False
self.kp._keystring = '10foo'
self.assertEqual(self.kp._split_count(), (None, '10foo'))
@mock.patch('qutebrowser.keyinput.basekeyparser.usertypes.Timer',
new=stubs.FakeTimer)
2014-06-23 19:44:21 +02:00
class ReadConfigTests(unittest.TestCase):
2014-05-12 17:39:37 +02:00
"""Test reading the config."""
def setUp(self):
2014-09-24 23:11:17 +02:00
objreg.register('key-config', fake_keyconfig)
2014-05-12 17:39:37 +02:00
2014-09-24 23:11:17 +02:00
def tearDown(self):
objreg.delete('key-config')
2014-05-12 17:39:37 +02:00
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)
2014-05-12 17:39:37 +02:00
with self.assertRaises(ValueError):
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')
self.assertIn('ccc', kp.bindings)
2014-07-03 07:46:14 +02:00
self.assertIn('ctrl+a', kp.special_bindings)
2014-05-12 17:39:37 +02:00
kp.read_config('test2')
self.assertNotIn('ccc', kp.bindings)
2014-07-03 07:46:14 +02:00
self.assertNotIn('ctrl+a', kp.special_bindings)
2014-05-12 17:39:37 +02:00
self.assertIn('foo', kp.bindings)
2014-07-03 07:46:14 +02:00
self.assertIn('ctrl+x', kp.special_bindings)
2014-05-12 17:39:37 +02:00
@mock.patch('qutebrowser.keyinput.basekeyparser.usertypes.Timer',
new=stubs.FakeTimer)
2014-06-23 19:44:21 +02:00
class SpecialKeysTests(unittest.TestCase):
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
def setUp(self):
2014-09-24 23:11:17 +02:00
objreg.register('key-config', 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()
with self.assertLogs(log.keyboard, logging.WARNING):
# 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
2014-09-24 23:11:17 +02:00
def tearDown(self):
objreg.delete('key-config')
2014-05-12 17:39:37 +02:00
def test_valid_key(self):
2014-05-27 13:06:13 +02:00
"""Test a valid special keyevent."""
2014-08-26 19:10:14 +02:00
self.kp.handle(helpers.fake_keyevent(Qt.Key_A, Qt.ControlModifier))
self.kp.handle(helpers.fake_keyevent(Qt.Key_X, Qt.ControlModifier))
2014-05-12 17:39:37 +02:00
self.kp.execute.assert_called_once_with('ctrla', self.kp.Type.special)
def test_invalid_key(self):
2014-05-27 13:06:13 +02:00
"""Test an invalid special keyevent."""
2014-08-26 19:10:14 +02:00
self.kp.handle(helpers.fake_keyevent(Qt.Key_A, (Qt.ControlModifier |
Qt.AltModifier)))
2014-05-12 17:39:37 +02:00
self.assertFalse(self.kp.execute.called)
def test_keychain(self):
2014-05-27 13:06:13 +02:00
"""Test a keychain."""
2014-08-26 19:10:14 +02:00
self.kp.handle(helpers.fake_keyevent(Qt.Key_B))
self.kp.handle(helpers.fake_keyevent(Qt.Key_A))
2014-05-12 17:39:37 +02:00
self.assertFalse(self.kp.execute.called)
@mock.patch('qutebrowser.keyinput.basekeyparser.usertypes.Timer',
new=stubs.FakeTimer)
2014-06-23 19:44:21 +02:00
class KeyChainTests(unittest.TestCase):
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
def setUp(self):
2014-05-27 13:06:13 +02:00
"""Set up mocks and read the test config."""
2014-09-24 23:11:17 +02:00
objreg.register('key-config', 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=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')
2014-09-24 23:11:17 +02:00
def tearDown(self):
objreg.delete('key-config')
2014-05-12 17:39:37 +02:00
def test_valid_special_key(self):
2014-05-27 13:06:13 +02:00
"""Test valid special key."""
2014-08-26 19:10:14 +02:00
self.kp.handle(helpers.fake_keyevent(Qt.Key_A, Qt.ControlModifier))
self.kp.handle(helpers.fake_keyevent(Qt.Key_X, Qt.ControlModifier))
2014-05-12 17:39:37 +02:00
self.kp.execute.assert_called_once_with('ctrla', self.kp.Type.special)
self.assertEqual(self.kp._keystring, '')
def test_invalid_special_key(self):
2014-05-27 13:06:13 +02:00
"""Test invalid special key."""
2014-08-26 19:10:14 +02:00
self.kp.handle(helpers.fake_keyevent(Qt.Key_A, (Qt.ControlModifier |
Qt.AltModifier)))
2014-05-12 17:39:37 +02:00
self.assertFalse(self.kp.execute.called)
self.assertEqual(self.kp._keystring, '')
def test_keychain(self):
2014-05-27 13:06:13 +02:00
"""Test valid keychain."""
# Press 'x' which is ignored because of no match
2014-08-26 19:10:14 +02:00
self.kp.handle(helpers.fake_keyevent(Qt.Key_X, text='x'))
# Then start the real chain
2014-08-26 19:10:14 +02:00
self.kp.handle(helpers.fake_keyevent(Qt.Key_B, text='b'))
self.kp.handle(helpers.fake_keyevent(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)
self.assertEqual(self.kp._keystring, '')
@mock.patch('qutebrowser.keyinput.basekeyparser.config',
new=stubs.ConfigStub(CONFIG))
2014-05-12 17:39:37 +02:00
def test_ambigious_keychain(self):
2014-05-27 13:06:13 +02:00
"""Test ambigious keychain."""
timer = self.kp._ambigious_timer
self.assertFalse(timer.isActive())
2014-05-12 17:39:37 +02:00
# We start with 'a' where the keychain gives us an ambigious result.
# Then we check if the timer has been set up correctly
2014-08-26 19:10:14 +02:00
self.kp.handle(helpers.fake_keyevent(Qt.Key_A, text='a'))
2014-05-12 17:39:37 +02:00
self.assertFalse(self.kp.execute.called)
self.assertTrue(timer.isSingleShot())
self.assertEqual(timer.interval(), 100)
self.assertTrue(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.
2014-08-26 19:10:14 +02:00
self.kp.handle(helpers.fake_keyevent(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)
self.assertFalse(timer.isActive())
2014-05-12 17:39:37 +02:00
self.assertEqual(self.kp._keystring, '')
def test_invalid_keychain(self):
2014-05-27 13:06:13 +02:00
"""Test invalid keychain."""
2014-08-26 19:10:14 +02:00
self.kp.handle(helpers.fake_keyevent(Qt.Key_B, text='b'))
self.kp.handle(helpers.fake_keyevent(Qt.Key_C, text='c'))
2014-05-12 17:39:37 +02:00
self.assertEqual(self.kp._keystring, '')
@mock.patch('qutebrowser.keyinput.basekeyparser.usertypes.Timer',
new=stubs.FakeTimer)
2014-06-23 19:44:21 +02:00
class CountTests(unittest.TestCase):
2014-05-12 17:39:37 +02:00
"""Test execute() with counts."""
def setUp(self):
2014-09-24 23:11:17 +02:00
objreg.register('key-config', 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')
def test_no_count(self):
2014-05-27 13:06:13 +02:00
"""Test with no count added."""
2014-08-26 19:10:14 +02:00
self.kp.handle(helpers.fake_keyevent(Qt.Key_B, text='b'))
self.kp.handle(helpers.fake_keyevent(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)
self.assertEqual(self.kp._keystring, '')
def test_count_0(self):
2014-05-27 13:06:13 +02:00
"""Test with count=0."""
2014-08-26 19:10:14 +02:00
self.kp.handle(helpers.fake_keyevent(Qt.Key_0, text='0'))
self.kp.handle(helpers.fake_keyevent(Qt.Key_B, text='b'))
self.kp.handle(helpers.fake_keyevent(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, 0)
self.assertEqual(self.kp._keystring, '')
def test_count_42(self):
2014-05-27 13:06:13 +02:00
"""Test with count=42."""
2014-08-26 19:10:14 +02:00
self.kp.handle(helpers.fake_keyevent(Qt.Key_4, text='4'))
self.kp.handle(helpers.fake_keyevent(Qt.Key_2, text='2'))
self.kp.handle(helpers.fake_keyevent(Qt.Key_B, text='b'))
self.kp.handle(helpers.fake_keyevent(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)
self.assertEqual(self.kp._keystring, '')
def test_count_42_invalid(self):
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
2014-08-26 19:10:14 +02:00
self.kp.handle(helpers.fake_keyevent(Qt.Key_4, text='4'))
self.kp.handle(helpers.fake_keyevent(Qt.Key_2, text='2'))
self.kp.handle(helpers.fake_keyevent(Qt.Key_B, text='c'))
self.kp.handle(helpers.fake_keyevent(Qt.Key_A, text='c'))
self.kp.handle(helpers.fake_keyevent(Qt.Key_A, text='x'))
2014-05-12 17:39:37 +02:00
self.assertFalse(self.kp.execute.called)
self.assertEqual(self.kp._keystring, '')
# Valid call with ccc gets the correct count
2014-08-26 19:10:14 +02:00
self.kp.handle(helpers.fake_keyevent(Qt.Key_4, text='2'))
self.kp.handle(helpers.fake_keyevent(Qt.Key_2, text='3'))
self.kp.handle(helpers.fake_keyevent(Qt.Key_B, text='c'))
self.kp.handle(helpers.fake_keyevent(Qt.Key_A, text='c'))
self.kp.handle(helpers.fake_keyevent(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)
self.assertEqual(self.kp._keystring, '')
2014-09-24 23:11:17 +02:00
def tearDown(self):
2015-02-20 09:09:35 +01:00
objreg.global_registry.clear()
2014-09-24 23:11:17 +02:00
2014-05-12 17:39:37 +02:00
if __name__ == '__main__':
unittest.main()