Debian Codegraph @

tests/unit_tests/test_keybindings.py

raw
1 # Unit tests for keybindings.py methods.
2 #
3 # Copyright 2026 Igalia, S.L.
4 # Author: Joanmarie Diggs <[email protected]>
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the
18 # Free Software Foundation, Inc., Franklin Street, Fifth Floor,
19 # Boston MA 02110-1301 USA.
20
21 # pylint: disable=import-outside-toplevel
22 # pylint: disable=protected-access
23
24 """Unit tests for keybindings.py methods."""
25
26 from __future__ import annotations
27
28 from typing import TYPE_CHECKING
29
30 import pytest
31
32 if TYPE_CHECKING:
33 from .orca_test_context import OrcaTestContext
34
35
36 def _setup_keybindings_dependencies(test_context: OrcaTestContext) -> None:
37 """Mock only the dependencies keybindings.py needs, not keybindings itself."""
38
39 i18n_mock = test_context.Mock()
40 i18n_mock._ = lambda x: x
41 i18n_mock.C_ = lambda c, x: x
42 test_context.patch_module("orca.orca_i18n", i18n_mock)
43
44 input_event_manager_mock = test_context.Mock()
45 input_event_manager_mock.get_manager = test_context.Mock(
46 return_value=test_context.Mock(),
47 )
48 test_context.patch_module("orca.input_event_manager", input_event_manager_mock)
49
50
51 @pytest.mark.unit
52 class TestAltGrModifierSupport:
53 """Test AltGr (ISO_Level3_Shift) modifier support in keybindings."""
54
55 @staticmethod
56 def _create_binding(
57 keysymstring: str,
58 modifiers: int,
59 keyval: int = 0,
60 keycode: int = 0,
61 ):
62 """Creates a KeyBinding with pre-populated keyval/keycode to avoid Gdk.Keymap."""
63
64 from orca.keybindings import KeyBinding
65
66 kb = KeyBinding(keysymstring, modifiers)
67 kb.keyval = keyval
68 kb.keycode = keycode
69 return kb
70
71 def test_extra_altgr_prevents_match(self, test_context: OrcaTestContext) -> None:
72 """Test that AltGr in event modifiers prevents matching a non-AltGr binding."""
73
74 _setup_keybindings_dependencies(test_context)
75 from orca.keybindings import ALTGR_MODIFIER_MASK, ORCA_MODIFIER_MASK
76
77 kb = self._create_binding("z", ORCA_MODIFIER_MASK, keyval=122, keycode=52)
78 assert kb.matches(122, 52, ORCA_MODIFIER_MASK | ALTGR_MODIFIER_MASK) is False
79
80 def test_altgr_keycode_fallback(self, test_context: OrcaTestContext) -> None:
81 """Test keycode fallback when AltGr changes the keyval."""
82
83 _setup_keybindings_dependencies(test_context)
84 from orca.keybindings import ALTGR_MODIFIER_MASK, ORCA_MODIFIER_MASK
85
86 orca_altgr = ORCA_MODIFIER_MASK | ALTGR_MODIFIER_MASK
87 kb = self._create_binding("period", orca_altgr, keyval=46, keycode=60)
88 assert kb.matches(183, 60, orca_altgr) is True
89
90 def test_orca_modifier_enables_keycode_fallback(
91 self,
92 test_context: OrcaTestContext,
93 ) -> None:
94 """Test that Orca modifier enables keycode fallback for AZERTY-like layouts."""
95
96 _setup_keybindings_dependencies(test_context)
97 from orca.keybindings import ORCA_MODIFIER_MASK
98
99 kb = self._create_binding("9", ORCA_MODIFIER_MASK, keyval=57, keycode=18)
100 assert kb.matches(231, 18, ORCA_MODIFIER_MASK) is True