2015-03-01 21:35:14 +01:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2019-02-23 06:34:17 +01:00
|
|
|
# Copyright 2015-2019 Florian Bruhin (The Compiler) <mail@qutebrowser.org>:
|
2015-03-01 21:35:14 +01: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 mode parsers."""
|
|
|
|
|
2015-05-19 07:44:45 +02:00
|
|
|
from unittest import mock
|
|
|
|
|
2015-03-01 21:35:14 +01:00
|
|
|
from PyQt5.QtCore import Qt
|
2018-03-09 07:38:16 +01:00
|
|
|
from PyQt5.QtGui import QKeySequence
|
2015-03-01 21:35:14 +01:00
|
|
|
|
2015-04-04 17:57:54 +02:00
|
|
|
import pytest
|
2015-03-01 21:35:14 +01:00
|
|
|
|
2018-02-27 09:22:01 +01:00
|
|
|
from qutebrowser.keyinput import modeparsers, keyutils
|
2015-03-01 21:35:14 +01:00
|
|
|
|
|
|
|
|
2015-04-04 17:57:54 +02:00
|
|
|
class TestsNormalKeyParser:
|
2015-03-01 21:35:14 +01:00
|
|
|
|
2015-12-01 22:40:58 +01:00
|
|
|
@pytest.fixture(autouse=True)
|
2017-07-03 21:13:33 +02:00
|
|
|
def patch_stuff(self, monkeypatch, stubs, keyinput_bindings):
|
2015-03-01 21:35:14 +01:00
|
|
|
"""Set up mocks and read the test config."""
|
2015-05-18 23:32:01 +02:00
|
|
|
monkeypatch.setattr(
|
|
|
|
'qutebrowser.keyinput.basekeyparser.usertypes.Timer',
|
|
|
|
stubs.FakeTimer)
|
2015-04-04 17:57:54 +02:00
|
|
|
|
2015-12-01 22:40:58 +01:00
|
|
|
@pytest.fixture
|
|
|
|
def keyparser(self):
|
|
|
|
kp = modeparsers.NormalKeyParser(0)
|
|
|
|
kp.execute = mock.Mock()
|
|
|
|
return kp
|
2015-03-01 21:35:14 +01:00
|
|
|
|
2018-03-04 19:38:15 +01:00
|
|
|
def test_keychain(self, keyparser, fake_keyevent):
|
2015-03-01 21:35:14 +01:00
|
|
|
"""Test valid keychain."""
|
|
|
|
# Press 'x' which is ignored because of no match
|
2018-03-04 19:38:15 +01:00
|
|
|
keyparser.handle(fake_keyevent(Qt.Key_X))
|
2015-03-01 21:35:14 +01:00
|
|
|
# Then start the real chain
|
2018-03-04 19:38:15 +01:00
|
|
|
keyparser.handle(fake_keyevent(Qt.Key_B))
|
|
|
|
keyparser.handle(fake_keyevent(Qt.Key_A))
|
2018-02-27 10:35:19 +01:00
|
|
|
keyparser.execute.assert_called_with('message-info ba', None)
|
2018-02-27 09:22:01 +01:00
|
|
|
assert not keyparser._sequence
|
2015-03-01 21:35:14 +01:00
|
|
|
|
2017-07-03 21:13:33 +02:00
|
|
|
def test_partial_keychain_timeout(self, keyparser, config_stub,
|
2018-03-04 19:38:15 +01:00
|
|
|
fake_keyevent):
|
2015-03-01 21:35:14 +01:00
|
|
|
"""Test partial keychain timeout."""
|
2017-07-03 21:13:33 +02:00
|
|
|
config_stub.val.input.partial_timeout = 100
|
2015-12-01 22:40:58 +01:00
|
|
|
timer = keyparser._partial_timer
|
2015-04-04 17:57:54 +02:00
|
|
|
assert not timer.isActive()
|
2015-03-01 21:35:14 +01:00
|
|
|
# Press 'b' for a partial match.
|
|
|
|
# Then we check if the timer has been set up correctly
|
2018-03-04 19:38:15 +01:00
|
|
|
keyparser.handle(fake_keyevent(Qt.Key_B))
|
2015-04-04 17:57:54 +02:00
|
|
|
assert timer.isSingleShot()
|
|
|
|
assert timer.interval() == 100
|
|
|
|
assert timer.isActive()
|
2015-03-01 21:35:14 +01:00
|
|
|
|
2015-12-01 22:40:58 +01:00
|
|
|
assert not keyparser.execute.called
|
2018-02-27 09:22:01 +01:00
|
|
|
assert keyparser._sequence == keyutils.KeySequence.parse('b')
|
2015-03-01 21:35:14 +01:00
|
|
|
# Now simulate a timeout and check the keystring has been cleared.
|
|
|
|
keystring_updated_mock = mock.Mock()
|
2015-12-01 22:40:58 +01:00
|
|
|
keyparser.keystring_updated.connect(keystring_updated_mock)
|
2015-03-01 21:35:14 +01:00
|
|
|
timer.timeout.emit()
|
2015-12-01 22:40:58 +01:00
|
|
|
assert not keyparser.execute.called
|
2018-02-27 09:22:01 +01:00
|
|
|
assert not keyparser._sequence
|
2015-03-01 21:35:14 +01:00
|
|
|
keystring_updated_mock.assert_called_once_with('')
|
2018-03-09 07:38:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestHintKeyParser:
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def keyparser(self, config_stub, key_config_stub):
|
|
|
|
kp = modeparsers.HintKeyParser(0)
|
|
|
|
kp.execute = mock.Mock()
|
|
|
|
kp.keystring_updated.disconnect() # Don't try to update HintManager
|
|
|
|
return kp
|
|
|
|
|
|
|
|
def test_simple_hint_match(self, keyparser, fake_keyevent):
|
|
|
|
keyparser.update_bindings(['aa', 'as'])
|
|
|
|
|
|
|
|
match = keyparser.handle(fake_keyevent(Qt.Key_A))
|
|
|
|
assert match == QKeySequence.PartialMatch
|
|
|
|
match = keyparser.handle(fake_keyevent(Qt.Key_S))
|
|
|
|
assert match == QKeySequence.ExactMatch
|
|
|
|
|
|
|
|
keyparser.execute.assert_called_with('follow-hint -s as', None)
|
2018-03-21 18:06:13 +01:00
|
|
|
|
|
|
|
def test_numberkey_hint_match(self, keyparser, fake_keyevent):
|
|
|
|
keyparser.update_bindings(['21', '22'])
|
|
|
|
|
|
|
|
match = keyparser.handle(fake_keyevent(Qt.Key_2, Qt.KeypadModifier))
|
|
|
|
assert match == QKeySequence.PartialMatch
|
|
|
|
match = keyparser.handle(fake_keyevent(Qt.Key_2, Qt.KeypadModifier))
|
|
|
|
assert match == QKeySequence.ExactMatch
|