| 1 |
# Unit tests for bypass_mode_manager.py methods. |
| 2 |
# |
| 3 |
# Copyright 2025 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=too-many-public-methods |
| 22 |
# pylint: disable=too-many-locals |
| 23 |
# pylint: disable=import-outside-toplevel |
| 24 |
# pylint: disable=protected-access |
| 25 |
# pylint: disable=no-member |
| 26 |
|
| 27 |
"""Unit tests for bypass_mode_manager.py methods.""" |
| 28 |
|
| 29 |
from __future__ import annotations |
| 30 |
|
| 31 |
from typing import TYPE_CHECKING |
| 32 |
|
| 33 |
import pytest |
| 34 |
|
| 35 |
if TYPE_CHECKING: |
| 36 |
from unittest.mock import MagicMock |
| 37 |
|
| 38 |
from .orca_test_context import OrcaTestContext |
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.unit |
| 42 |
class TestBypassModeManager: |
| 43 |
"""Test BypassModeManager class methods.""" |
| 44 |
|
| 45 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 46 |
"""Returns dependencies for bypass_mode_manager module testing.""" |
| 47 |
|
| 48 |
additional_modules = [ |
| 49 |
"orca.orca_modifier_manager", |
| 50 |
"orca.command_manager", |
| 51 |
"orca.braille_presenter", |
| 52 |
"orca.presentation_manager", |
| 53 |
] |
| 54 |
essential_modules = test_context.setup_shared_dependencies(additional_modules) |
| 55 |
|
| 56 |
debug_mock = essential_modules["orca.debug"] |
| 57 |
debug_mock.print_message = test_context.Mock() |
| 58 |
debug_mock.LEVEL_INFO = 800 |
| 59 |
|
| 60 |
cmdnames_mock = essential_modules["orca.cmdnames"] |
| 61 |
cmdnames_mock.BYPASS_MODE_TOGGLE = "Toggle all Orca command keys" |
| 62 |
|
| 63 |
input_event_mock = essential_modules["orca.input_event"] |
| 64 |
input_event_handler_mock = test_context.Mock() |
| 65 |
input_event_mock.InputEventHandler = test_context.Mock( |
| 66 |
return_value=input_event_handler_mock, |
| 67 |
) |
| 68 |
|
| 69 |
keybindings_mock = essential_modules["orca.keybindings"] |
| 70 |
key_bindings_instance = test_context.Mock() |
| 71 |
key_bindings_instance.is_empty = test_context.Mock(return_value=False) |
| 72 |
key_bindings_instance.key_bindings = [] |
| 73 |
key_bindings_instance.add = test_context.Mock(return_value=None) |
| 74 |
keybindings_mock.KeyBindings = test_context.Mock(return_value=key_bindings_instance) |
| 75 |
keybindings_mock.KeyBinding = test_context.Mock() |
| 76 |
keybindings_mock.DEFAULT_MODIFIER_MASK = 1 |
| 77 |
keybindings_mock.ALT_MODIFIER_MASK = 2 |
| 78 |
|
| 79 |
messages_mock = essential_modules["orca.messages"] |
| 80 |
messages_mock.BYPASS_MODE_ENABLED = "Orca command keys off." |
| 81 |
messages_mock.BYPASS_MODE_DISABLED = "Orca command keys on." |
| 82 |
|
| 83 |
orca_modifier_manager_mock = essential_modules["orca.orca_modifier_manager"] |
| 84 |
modifier_manager_instance = test_context.Mock() |
| 85 |
modifier_manager_instance.unset_orca_modifiers = test_context.Mock() |
| 86 |
modifier_manager_instance.refresh_orca_modifiers = test_context.Mock() |
| 87 |
modifier_manager_instance.add_grabs_for_orca_modifiers = test_context.Mock() |
| 88 |
modifier_manager_instance.remove_grabs_for_orca_modifiers = test_context.Mock() |
| 89 |
orca_modifier_manager_mock.get_manager = test_context.Mock( |
| 90 |
return_value=modifier_manager_instance, |
| 91 |
) |
| 92 |
|
| 93 |
command_manager_mock = essential_modules["orca.command_manager"] |
| 94 |
command_manager_instance = test_context.Mock() |
| 95 |
command_manager_instance.set_all_suspended = test_context.Mock() |
| 96 |
command_manager_instance.get_command = test_context.Mock(return_value=None) |
| 97 |
command_manager_mock.get_manager = test_context.Mock(return_value=command_manager_instance) |
| 98 |
|
| 99 |
essential_modules["input_event_handler"] = input_event_handler_mock |
| 100 |
essential_modules["key_bindings_instance"] = key_bindings_instance |
| 101 |
essential_modules["modifier_manager_instance"] = modifier_manager_instance |
| 102 |
essential_modules["command_manager_instance"] = command_manager_instance |
| 103 |
|
| 104 |
return essential_modules |
| 105 |
|
| 106 |
def test_init(self, test_context: OrcaTestContext) -> None: |
| 107 |
"""Test BypassModeManager initialization.""" |
| 108 |
|
| 109 |
self._setup_dependencies(test_context) |
| 110 |
from orca import command_manager |
| 111 |
from orca.bypass_mode_manager import BypassModeManager |
| 112 |
|
| 113 |
manager = BypassModeManager() |
| 114 |
assert manager._is_active is False |
| 115 |
# Commands are registered during setup(), not __init__() |
| 116 |
manager.set_up_commands() |
| 117 |
cmd_manager = command_manager.get_manager() |
| 118 |
assert cmd_manager.get_keyboard_command("bypass_mode_toggle") is not None |
| 119 |
|
| 120 |
def test_setup_handlers(self, test_context: OrcaTestContext) -> None: |
| 121 |
"""Test that commands are registered with CommandManager during setup.""" |
| 122 |
|
| 123 |
self._setup_dependencies(test_context) |
| 124 |
from orca import command_manager |
| 125 |
from orca.bypass_mode_manager import BypassModeManager |
| 126 |
|
| 127 |
manager = BypassModeManager() |
| 128 |
manager.set_up_commands() |
| 129 |
# Commands are now registered with CommandManager during setup() |
| 130 |
cmd_manager = command_manager.get_manager() |
| 131 |
cmd = cmd_manager.get_keyboard_command("bypass_mode_toggle") |
| 132 |
assert cmd is not None |
| 133 |
|
| 134 |
def test_setup_bindings(self, test_context: OrcaTestContext) -> None: |
| 135 |
"""Test that keybindings are created via Command.set_keybinding.""" |
| 136 |
|
| 137 |
self._setup_dependencies(test_context) |
| 138 |
from orca import command_manager |
| 139 |
from orca.bypass_mode_manager import BypassModeManager |
| 140 |
|
| 141 |
manager = BypassModeManager() |
| 142 |
manager.set_up_commands() |
| 143 |
# Verify the command has a keybinding |
| 144 |
cmd_manager = command_manager.get_manager() |
| 145 |
cmd = cmd_manager.get_keyboard_command("bypass_mode_toggle") |
| 146 |
assert cmd is not None |
| 147 |
keybinding = cmd.get_keybinding() |
| 148 |
assert keybinding is not None |
| 149 |
|
| 150 |
@pytest.mark.parametrize( |
| 151 |
"initial_active,has_event,expected_active,expected_message,expected_modifier_call", |
| 152 |
[ |
| 153 |
( |
| 154 |
False, |
| 155 |
True, |
| 156 |
True, |
| 157 |
"Orca command keys off.", |
| 158 |
"unset_orca_modifiers", |
| 159 |
), |
| 160 |
( |
| 161 |
False, |
| 162 |
False, |
| 163 |
True, |
| 164 |
None, |
| 165 |
"unset_orca_modifiers", |
| 166 |
), |
| 167 |
( |
| 168 |
True, |
| 169 |
True, |
| 170 |
False, |
| 171 |
"Orca command keys on.", |
| 172 |
"refresh_orca_modifiers", |
| 173 |
), |
| 174 |
( |
| 175 |
True, |
| 176 |
False, |
| 177 |
False, |
| 178 |
None, |
| 179 |
"refresh_orca_modifiers", |
| 180 |
), |
| 181 |
], |
| 182 |
) |
| 183 |
def test_toggle_enabled( # pylint: disable=too-many-arguments,too-many-positional-arguments |
| 184 |
self, |
| 185 |
test_context: OrcaTestContext, |
| 186 |
initial_active: bool, |
| 187 |
has_event: bool, |
| 188 |
expected_active: bool, |
| 189 |
expected_message: str | None, |
| 190 |
expected_modifier_call: str, |
| 191 |
) -> None: |
| 192 |
"""Test toggle_enabled with various initial states and event conditions.""" |
| 193 |
|
| 194 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 195 |
|
| 196 |
if not initial_active: |
| 197 |
mock_binding1 = test_context.Mock() |
| 198 |
mock_binding2 = test_context.Mock() |
| 199 |
key_bindings = [mock_binding1, mock_binding2] |
| 200 |
else: |
| 201 |
key_bindings = [] |
| 202 |
|
| 203 |
key_bindings_instance = essential_modules["key_bindings_instance"] |
| 204 |
key_bindings_instance.key_bindings = key_bindings |
| 205 |
from orca.bypass_mode_manager import BypassModeManager |
| 206 |
|
| 207 |
manager = BypassModeManager() |
| 208 |
manager._is_active = initial_active |
| 209 |
mock_script = test_context.Mock() |
| 210 |
mock_event = test_context.Mock() if has_event else None |
| 211 |
|
| 212 |
result = manager.toggle_enabled(mock_script, mock_event) |
| 213 |
assert result is True |
| 214 |
assert manager._is_active is expected_active |
| 215 |
|
| 216 |
pres_manager = essential_modules["orca.presentation_manager"].get_manager() |
| 217 |
if expected_message: |
| 218 |
pres_manager.present_message.assert_called_with(expected_message) |
| 219 |
else: |
| 220 |
pres_manager.present_message.assert_not_called() |
| 221 |
|
| 222 |
cmd_manager = essential_modules["command_manager_instance"] |
| 223 |
cmd_manager.set_all_suspended.assert_called() |
| 224 |
call_args = cmd_manager.set_all_suspended.call_args |
| 225 |
if expected_active: |
| 226 |
assert call_args[0][0] is True |
| 227 |
assert call_args[0][1] == frozenset({"bypass_mode_toggle"}) |
| 228 |
else: |
| 229 |
assert call_args[0][0] is False |
| 230 |
|
| 231 |
modifier_manager = essential_modules["modifier_manager_instance"] |
| 232 |
modifier_method = getattr(modifier_manager, expected_modifier_call) |
| 233 |
if expected_active: |
| 234 |
modifier_method.assert_called_with("bypass mode enabled") |
| 235 |
else: |
| 236 |
modifier_method.assert_called_with("bypass mode disabled") |
| 237 |
|
| 238 |
def test_toggle_enabled_multiple_times(self, test_context: OrcaTestContext) -> None: |
| 239 |
"""Test toggle_enabled works correctly when called multiple times.""" |
| 240 |
|
| 241 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 242 |
|
| 243 |
key_bindings_instance = essential_modules["key_bindings_instance"] |
| 244 |
key_bindings_instance.key_bindings = [] |
| 245 |
from orca.bypass_mode_manager import BypassModeManager |
| 246 |
|
| 247 |
manager = BypassModeManager() |
| 248 |
mock_script = test_context.Mock() |
| 249 |
|
| 250 |
assert manager.is_active() is False |
| 251 |
|
| 252 |
result1 = manager.toggle_enabled(mock_script, None) |
| 253 |
assert result1 is True |
| 254 |
assert manager.is_active() is True |
| 255 |
|
| 256 |
result2 = manager.toggle_enabled(mock_script, None) |
| 257 |
assert result2 is True |
| 258 |
assert manager.is_active() is False |
| 259 |
|
| 260 |
result3 = manager.toggle_enabled(mock_script, None) |
| 261 |
assert result3 is True |
| 262 |
assert manager.is_active() is True |
| 263 |
|
| 264 |
def test_bindings_created_correctly(self, test_context: OrcaTestContext) -> None: |
| 265 |
"""Test that bindings are properly configured from commands.""" |
| 266 |
|
| 267 |
self._setup_dependencies(test_context) |
| 268 |
from orca import command_manager |
| 269 |
from orca.bypass_mode_manager import BypassModeManager |
| 270 |
|
| 271 |
manager = BypassModeManager() |
| 272 |
manager.set_up_commands() |
| 273 |
# Verify bypass_mode_toggle command has keybinding set |
| 274 |
cmd = command_manager.get_manager().get_keyboard_command("bypass_mode_toggle") |
| 275 |
assert cmd is not None |
| 276 |
assert cmd.get_keybinding() is not None |
| 277 |
|
| 278 |
def test_toggle_enabled_with_minimal_script_interface( |
| 279 |
self, |
| 280 |
test_context: OrcaTestContext, |
| 281 |
) -> None: |
| 282 |
"""Test toggle_enabled method with script that has minimal interface.""" |
| 283 |
|
| 284 |
self._setup_dependencies(test_context) |
| 285 |
from orca.bypass_mode_manager import BypassModeManager |
| 286 |
|
| 287 |
manager = BypassModeManager() |
| 288 |
mock_script = test_context.Mock(spec=[]) |
| 289 |
mock_script.add_key_grabs = test_context.Mock() |
| 290 |
mock_script.remove_key_grabs = test_context.Mock() |
| 291 |
mock_script.present_message = test_context.Mock() |
| 292 |
mock_script.key_bindings = test_context.Mock() |
| 293 |
mock_script.key_bindings.add = test_context.Mock() |
| 294 |
|
| 295 |
result = manager.toggle_enabled(mock_script) |
| 296 |
assert result is True |
| 297 |
assert manager.is_active() is True |