summaryrefslogtreecommitdiff
path: root/tests/unit/keyinput/test_modeparsers.py
blob: c3be9d70fcea30fe257e831b133fee0c4a209864 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2015-2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org>:
#
# 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."""

from unittest import mock

from PyQt5.QtCore import Qt

import pytest

from qutebrowser.keyinput import modeparsers, keyutils


class TestsNormalKeyParser:

    """Tests for NormalKeyParser.

    Attributes:
        kp: The NormalKeyParser to be tested.
    """

    @pytest.fixture(autouse=True)
    def patch_stuff(self, monkeypatch, stubs, keyinput_bindings):
        """Set up mocks and read the test config."""
        monkeypatch.setattr(
            'qutebrowser.keyinput.basekeyparser.usertypes.Timer',
            stubs.FakeTimer)

    @pytest.fixture
    def keyparser(self):
        kp = modeparsers.NormalKeyParser(0)
        kp.execute = mock.Mock()
        return kp

    def test_keychain(self, keyparser, fake_keyevent):
        """Test valid keychain."""
        # Press 'x' which is ignored because of no match
        keyparser.handle(fake_keyevent(Qt.Key_X))
        # Then start the real chain
        keyparser.handle(fake_keyevent(Qt.Key_B))
        keyparser.handle(fake_keyevent(Qt.Key_A))
        keyparser.execute.assert_called_with('message-info ba', None)
        assert not keyparser._sequence

    def test_partial_keychain_timeout(self, keyparser, config_stub,
                                      fake_keyevent):
        """Test partial keychain timeout."""
        config_stub.val.input.partial_timeout = 100
        timer = keyparser._partial_timer
        assert not timer.isActive()
        # Press 'b' for a partial match.
        # Then we check if the timer has been set up correctly
        keyparser.handle(fake_keyevent(Qt.Key_B))
        assert timer.isSingleShot()
        assert timer.interval() == 100
        assert timer.isActive()

        assert not keyparser.execute.called
        assert keyparser._sequence == keyutils.KeySequence.parse('b')
        # Now simulate a timeout and check the keystring has been cleared.
        keystring_updated_mock = mock.Mock()
        keyparser.keystring_updated.connect(keystring_updated_mock)
        timer.timeout.emit()
        assert not keyparser.execute.called
        assert not keyparser._sequence
        keystring_updated_mock.assert_called_once_with('')