2017-12-29 01:41:55 +01:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2018-02-27 14:16:59 +01:00
|
|
|
# Copyright 2014-2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2017-12-29 01:41:55 +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/>.
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from PyQt5.QtCore import Qt
|
|
|
|
|
2018-02-28 09:03:47 +01:00
|
|
|
from tests.unit.keyinput import key_data
|
2018-02-27 09:07:20 +01:00
|
|
|
from qutebrowser.utils import utils
|
2017-12-29 01:41:55 +01:00
|
|
|
from qutebrowser.keyinput import keyutils
|
|
|
|
|
|
|
|
|
2018-02-28 09:51:19 +01:00
|
|
|
@pytest.fixture(params=key_data.KEYS)
|
2018-02-28 09:03:47 +01:00
|
|
|
def qt_key(request):
|
2018-02-28 09:51:19 +01:00
|
|
|
key = request.param
|
|
|
|
member = getattr(Qt, 'Key_' + key.attribute, None)
|
2018-02-28 09:03:47 +01:00
|
|
|
if member is None:
|
2018-02-28 09:51:19 +01:00
|
|
|
pytest.skip("Did not find key {}".format(key.attribute))
|
2018-02-28 09:03:47 +01:00
|
|
|
key.member = member
|
|
|
|
return key
|
|
|
|
|
2017-12-29 01:41:55 +01:00
|
|
|
|
2018-02-28 09:03:47 +01:00
|
|
|
class TestKeyToString:
|
2017-12-29 01:41:55 +01:00
|
|
|
|
2018-02-28 10:21:02 +01:00
|
|
|
def test_to_string(self, qt_key):
|
|
|
|
name = qt_key.attribute if qt_key.name is None else qt_key.name
|
|
|
|
assert keyutils._key_to_string(qt_key.member) == name
|
2017-12-29 01:41:55 +01:00
|
|
|
|
|
|
|
def test_missing(self, monkeypatch):
|
|
|
|
monkeypatch.delattr(keyutils.Qt, 'Key_Blue')
|
|
|
|
# We don't want to test the key which is actually missing - we only
|
|
|
|
# want to know if the mapping still behaves properly.
|
2018-02-27 22:21:35 +01:00
|
|
|
assert keyutils._key_to_string(Qt.Key_A) == 'A'
|
2017-12-29 01:41:55 +01:00
|
|
|
|
|
|
|
def test_all(self):
|
2018-02-28 10:21:02 +01:00
|
|
|
"""Make sure all possible keys are in key_data.KEYS."""
|
|
|
|
key_names = {name[len("Key_"):]
|
|
|
|
for name, value in sorted(vars(Qt).items())
|
|
|
|
if isinstance(value, Qt.Key)}
|
|
|
|
key_data_names = {key.attribute for key in sorted(key_data.KEYS)}
|
|
|
|
diff = key_names - key_data_names
|
|
|
|
assert not diff
|
2017-12-29 01:41:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestKeyEventToString:
|
|
|
|
|
|
|
|
"""Test keyevent_to_string."""
|
|
|
|
|
|
|
|
def test_only_control(self, fake_keyevent_factory):
|
|
|
|
"""Test keyeevent when only control is pressed."""
|
|
|
|
evt = fake_keyevent_factory(key=Qt.Key_Control,
|
|
|
|
modifiers=Qt.ControlModifier)
|
2018-02-27 22:21:35 +01:00
|
|
|
assert not str(keyutils.KeyInfo.from_event(evt))
|
2017-12-29 01:41:55 +01:00
|
|
|
|
|
|
|
def test_only_hyper_l(self, fake_keyevent_factory):
|
|
|
|
"""Test keyeevent when only Hyper_L is pressed."""
|
|
|
|
evt = fake_keyevent_factory(key=Qt.Key_Hyper_L,
|
|
|
|
modifiers=Qt.MetaModifier)
|
2018-02-27 22:21:35 +01:00
|
|
|
assert not str(keyutils.KeyInfo.from_event(evt))
|
2017-12-29 01:41:55 +01:00
|
|
|
|
|
|
|
def test_only_key(self, fake_keyevent_factory):
|
|
|
|
"""Test with a simple key pressed."""
|
|
|
|
evt = fake_keyevent_factory(key=Qt.Key_A)
|
2018-02-27 22:21:35 +01:00
|
|
|
assert str(keyutils.KeyInfo.from_event(evt)) == 'a'
|
2017-12-29 01:41:55 +01:00
|
|
|
|
|
|
|
def test_key_and_modifier(self, fake_keyevent_factory):
|
|
|
|
"""Test with key and modifier pressed."""
|
|
|
|
evt = fake_keyevent_factory(key=Qt.Key_A, modifiers=Qt.ControlModifier)
|
2018-02-27 09:07:20 +01:00
|
|
|
expected = '<Meta+a>' if utils.is_mac else '<Ctrl+a>'
|
2018-02-27 22:21:35 +01:00
|
|
|
assert str(keyutils.KeyInfo.from_event(evt)) == expected
|
2017-12-29 01:41:55 +01:00
|
|
|
|
|
|
|
def test_key_and_modifiers(self, fake_keyevent_factory):
|
|
|
|
"""Test with key and multiple modifiers pressed."""
|
|
|
|
evt = fake_keyevent_factory(
|
|
|
|
key=Qt.Key_A, modifiers=(Qt.ControlModifier | Qt.AltModifier |
|
|
|
|
Qt.MetaModifier | Qt.ShiftModifier))
|
2018-02-27 22:21:35 +01:00
|
|
|
s = str(keyutils.KeyInfo.from_event(evt))
|
|
|
|
assert s == '<Ctrl+Alt+Meta+Shift+a>'
|
2017-12-29 01:41:55 +01:00
|
|
|
|
|
|
|
@pytest.mark.fake_os('mac')
|
|
|
|
def test_mac(self, fake_keyevent_factory):
|
|
|
|
"""Test with a simulated mac."""
|
|
|
|
evt = fake_keyevent_factory(key=Qt.Key_A, modifiers=Qt.ControlModifier)
|
2018-02-27 22:21:35 +01:00
|
|
|
assert str(keyutils.KeyInfo.from_event(evt)) == '<Meta+a>'
|
2017-12-29 01:41:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('keystr, expected', [
|
2017-12-29 13:23:38 +01:00
|
|
|
('<Control-x>', keyutils.KeySequence(Qt.ControlModifier | Qt.Key_X)),
|
|
|
|
('<Meta-x>', keyutils.KeySequence(Qt.MetaModifier | Qt.Key_X)),
|
2017-12-29 01:41:55 +01:00
|
|
|
('<Ctrl-Alt-y>',
|
2017-12-29 13:23:38 +01:00
|
|
|
keyutils.KeySequence(Qt.ControlModifier | Qt.AltModifier | Qt.Key_Y)),
|
|
|
|
('x', keyutils.KeySequence(Qt.Key_X)),
|
|
|
|
('X', keyutils.KeySequence(Qt.ShiftModifier | Qt.Key_X)),
|
|
|
|
('<Escape>', keyutils.KeySequence(Qt.Key_Escape)),
|
|
|
|
('xyz', keyutils.KeySequence(Qt.Key_X, Qt.Key_Y, Qt.Key_Z)),
|
|
|
|
('<Control-x><Meta-y>', keyutils.KeySequence(Qt.ControlModifier | Qt.Key_X,
|
|
|
|
Qt.MetaModifier | Qt.Key_Y)),
|
2018-02-27 14:10:55 +01:00
|
|
|
('<blub>', keyutils.KeyParseError),
|
|
|
|
('\U00010000', keyutils.KeyParseError),
|
2017-12-29 01:41:55 +01:00
|
|
|
])
|
2017-12-29 13:23:38 +01:00
|
|
|
def test_parse(keystr, expected):
|
2017-12-29 01:41:55 +01:00
|
|
|
if expected is keyutils.KeyParseError:
|
|
|
|
with pytest.raises(keyutils.KeyParseError):
|
2018-02-27 09:07:20 +01:00
|
|
|
keyutils.KeySequence.parse(keystr)
|
2017-12-29 01:41:55 +01:00
|
|
|
else:
|
2018-02-27 09:07:20 +01:00
|
|
|
assert keyutils.KeySequence.parse(keystr) == expected
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('orig, normalized', [
|
|
|
|
('<Control+x>', '<ctrl+x>'),
|
|
|
|
('<Windows+x>', '<meta+x>'),
|
|
|
|
('<Mod1+x>', '<alt+x>'),
|
|
|
|
('<Mod4+x>', '<meta+x>'),
|
|
|
|
('<Control-->', '<ctrl+->'),
|
|
|
|
('<Windows++>', '<meta++>'),
|
|
|
|
('<ctrl-x>', '<ctrl+x>'),
|
|
|
|
('<control+x>', '<ctrl+x>')
|
2017-12-29 01:41:55 +01:00
|
|
|
])
|
2018-02-27 09:07:20 +01:00
|
|
|
def test_normalize_keystr(orig, normalized):
|
|
|
|
expected = keyutils.KeySequence.parse(normalized)
|
|
|
|
assert keyutils.KeySequence.parse(orig) == expected
|