| 1 |
# Unit tests for focus_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=wrong-import-position |
| 22 |
# pylint: disable=import-outside-toplevel |
| 23 |
# pylint: disable=too-many-public-methods |
| 24 |
# pylint: disable=too-many-statements |
| 25 |
# pylint: disable=protected-access |
| 26 |
# pylint: disable=too-many-arguments |
| 27 |
# pylint: disable=too-many-positional-arguments |
| 28 |
# pylint: disable=too-many-locals |
| 29 |
|
| 30 |
"""Unit tests for focus_manager.py methods.""" |
| 31 |
|
| 32 |
from __future__ import annotations |
| 33 |
|
| 34 |
from typing import TYPE_CHECKING |
| 35 |
|
| 36 |
import gi |
| 37 |
import pytest |
| 38 |
|
| 39 |
gi.require_version("Atspi", "2.0") |
| 40 |
from gi.repository import Atspi |
| 41 |
|
| 42 |
if TYPE_CHECKING: |
| 43 |
from unittest.mock import MagicMock |
| 44 |
|
| 45 |
from .orca_test_context import OrcaTestContext |
| 46 |
|
| 47 |
|
| 48 |
@pytest.mark.unit |
| 49 |
class TestFocusManager: |
| 50 |
"""Test FocusManager class methods.""" |
| 51 |
|
| 52 |
def _setup_locus_of_focus_ax_mocks( |
| 53 |
self, |
| 54 |
test_context, |
| 55 |
mock_app=None, |
| 56 |
is_dead=False, |
| 57 |
cell_coords=(-1, -1), |
| 58 |
caret_offset=-1, |
| 59 |
): |
| 60 |
"""Set up common AX object mocks for set_locus_of_focus testing scenarios. |
| 61 |
|
| 62 |
Returns: |
| 63 |
tuple: (mock_get_cell_coordinates, mock_get_caret_offset, mock_update_cached_text) |
| 64 |
""" |
| 65 |
|
| 66 |
from orca.ax_object import AXObject |
| 67 |
from orca.ax_table import AXTable |
| 68 |
from orca.ax_text import AXText |
| 69 |
from orca.ax_utilities import AXUtilities |
| 70 |
|
| 71 |
test_context.patch_object(AXObject, "clear_cache", new=test_context.Mock()) |
| 72 |
test_context.patch_object(AXObject, "is_dead", return_value=is_dead) |
| 73 |
test_context.patch_object(AXObject, "is_valid", side_effect=lambda obj: obj is not None) |
| 74 |
|
| 75 |
if mock_app is not None: |
| 76 |
test_context.patch_object(AXUtilities, "get_application", return_value=mock_app) |
| 77 |
else: |
| 78 |
test_context.patch_object(AXUtilities, "get_application", new=test_context.Mock()) |
| 79 |
|
| 80 |
test_context.patch_object( |
| 81 |
AXUtilities, |
| 82 |
"save_object_info_for_events", |
| 83 |
new=test_context.Mock(), |
| 84 |
) |
| 85 |
|
| 86 |
mock_get_cell_coordinates = test_context.Mock(return_value=cell_coords) |
| 87 |
test_context.patch_object(AXTable, "get_cell_coordinates", new=mock_get_cell_coordinates) |
| 88 |
|
| 89 |
mock_get_caret_offset = test_context.Mock(return_value=caret_offset) |
| 90 |
test_context.patch_object(AXText, "get_caret_offset", new=mock_get_caret_offset) |
| 91 |
|
| 92 |
mock_update_cached_text = test_context.Mock() |
| 93 |
test_context.patch_object( |
| 94 |
AXUtilities, |
| 95 |
"update_cached_selected_text", |
| 96 |
new=mock_update_cached_text, |
| 97 |
) |
| 98 |
|
| 99 |
return mock_get_cell_coordinates, mock_get_caret_offset, mock_update_cached_text |
| 100 |
|
| 101 |
def _setup_dependencies(self, test_context) -> dict[str, MagicMock]: |
| 102 |
"""Set up mocks for focus_manager dependencies.""" |
| 103 |
|
| 104 |
all_modules = [ |
| 105 |
"orca.debug", |
| 106 |
"orca.messages", |
| 107 |
"orca.input_event", |
| 108 |
"orca.keybindings", |
| 109 |
"orca.cmdnames", |
| 110 |
"orca.ax_object", |
| 111 |
"orca.dbus_service", |
| 112 |
"orca.script_manager", |
| 113 |
"orca.orca_i18n", |
| 114 |
"orca.guilabels", |
| 115 |
"orca.text_attribute_names", |
| 116 |
"orca.braille_presenter", |
| 117 |
"orca.ax_table", |
| 118 |
"orca.ax_text", |
| 119 |
"orca.ax_utilities", |
| 120 |
"orca.document_presenter", |
| 121 |
] |
| 122 |
essential_modules = test_context._setup_essential_modules(all_modules) |
| 123 |
|
| 124 |
i18n_mock = essential_modules["orca.orca_i18n"] |
| 125 |
i18n_mock._ = lambda x: x # Identity function for translations |
| 126 |
i18n_mock.C_ = lambda c, x: x # Context-aware translations |
| 127 |
i18n_mock.ngettext = lambda s, p, n: s if n == 1 else p # Plural forms |
| 128 |
|
| 129 |
debug_mock = essential_modules["orca.debug"] |
| 130 |
debug_mock.LEVEL_INFO = 800 |
| 131 |
debug_mock.LEVEL_FINEST = 2 |
| 132 |
debug_mock.LEVEL_SEVERE = 0 |
| 133 |
debug_mock.print_message = test_context.Mock() |
| 134 |
debug_mock.println = test_context.Mock() |
| 135 |
|
| 136 |
keybindings_mock = essential_modules["orca.keybindings"] |
| 137 |
bindings_instance = test_context.Mock() |
| 138 |
bindings_instance.is_empty = test_context.Mock(return_value=True) |
| 139 |
bindings_instance.add = test_context.Mock() |
| 140 |
keybindings_mock.KeyBindings = test_context.Mock(return_value=bindings_instance) |
| 141 |
keybindings_mock.KeyBinding = test_context.Mock(return_value=test_context.Mock()) |
| 142 |
|
| 143 |
braille_presenter_mock = essential_modules["orca.braille_presenter"] |
| 144 |
presenter_instance = test_context.Mock() |
| 145 |
presenter_instance.set_brlapi_priority = test_context.Mock() |
| 146 |
braille_presenter_mock.get_presenter = test_context.Mock(return_value=presenter_instance) |
| 147 |
|
| 148 |
script_manager_mock = essential_modules["orca.script_manager"] |
| 149 |
script_mgr_instance = test_context.Mock() |
| 150 |
script_instance = test_context.Mock() |
| 151 |
script_instance.locus_of_focus_changed = test_context.Mock() |
| 152 |
script_mgr_instance.get_active_script = test_context.Mock(return_value=script_instance) |
| 153 |
script_mgr_instance.get_script = test_context.Mock(return_value=script_instance) |
| 154 |
script_mgr_instance.set_active_script = test_context.Mock() |
| 155 |
script_manager_mock.get_manager = test_context.Mock(return_value=script_mgr_instance) |
| 156 |
|
| 157 |
ax_object_mock = essential_modules["orca.ax_object"] |
| 158 |
ax_object_mock.AXObject = test_context.Mock() |
| 159 |
ax_object_mock.AXObject.is_dead = test_context.Mock(return_value=False) |
| 160 |
ax_object_mock.AXObject.is_valid = test_context.Mock(return_value=True) |
| 161 |
ax_object_mock.AXObject.clear_cache = test_context.Mock() |
| 162 |
ax_object_mock.AXObject.has_broken_ancestry = test_context.Mock(return_value=False) |
| 163 |
|
| 164 |
ax_text_mock = essential_modules["orca.ax_text"] |
| 165 |
ax_text_mock.AXText = test_context.Mock() |
| 166 |
ax_text_mock.AXText.get_caret_offset = test_context.Mock(return_value=-1) |
| 167 |
|
| 168 |
ax_table_mock = essential_modules["orca.ax_table"] |
| 169 |
ax_table_mock.AXTable = test_context.Mock() |
| 170 |
ax_table_mock.AXTable.get_cell_coordinates = test_context.Mock(return_value=(-1, -1)) |
| 171 |
|
| 172 |
ax_utilities_mock = essential_modules["orca.ax_utilities"] |
| 173 |
ax_utilities_mock.AXUtilities = test_context.Mock() |
| 174 |
ax_utilities_mock.AXUtilities.is_table_cell = test_context.Mock(return_value=False) |
| 175 |
ax_utilities_mock.AXUtilities.get_application = test_context.Mock() |
| 176 |
ax_utilities_mock.AXUtilities.save_object_info_for_events = test_context.Mock() |
| 177 |
ax_utilities_mock.AXUtilities.is_active = test_context.Mock(return_value=True) |
| 178 |
ax_utilities_mock.AXUtilities.is_ancestor = test_context.Mock(return_value=False) |
| 179 |
ax_utilities_mock.AXUtilities.get_focused_object = test_context.Mock() |
| 180 |
ax_utilities_mock.AXUtilities.update_cached_selected_text = test_context.Mock() |
| 181 |
|
| 182 |
document_presenter_mock = essential_modules["orca.document_presenter"] |
| 183 |
presenter_instance = test_context.Mock() |
| 184 |
presenter_instance.in_focus_mode = test_context.Mock(return_value=False) |
| 185 |
document_presenter_mock.get_presenter = test_context.Mock(return_value=presenter_instance) |
| 186 |
|
| 187 |
return essential_modules |
| 188 |
|
| 189 |
def test_init(self, test_context) -> None: |
| 190 |
"""Test FocusManager.__init__.""" |
| 191 |
|
| 192 |
self._setup_dependencies(test_context) |
| 193 |
from orca.focus_manager import FocusManager |
| 194 |
|
| 195 |
manager = FocusManager() |
| 196 |
assert manager._focus is None |
| 197 |
assert manager._window is None |
| 198 |
assert manager._object_of_interest is None |
| 199 |
assert manager._active_mode is None |
| 200 |
|
| 201 |
def test_clear_state(self, test_context: OrcaTestContext) -> None: |
| 202 |
"""Test FocusManager.clear_state.""" |
| 203 |
|
| 204 |
self._setup_dependencies(test_context) |
| 205 |
from orca.focus_manager import FocusManager |
| 206 |
|
| 207 |
manager = FocusManager() |
| 208 |
test_context.patch_object(manager, "_focus", new=test_context.Mock(spec=Atspi.Accessible)) |
| 209 |
test_context.patch_object(manager, "_window", new=test_context.Mock(spec=Atspi.Accessible)) |
| 210 |
test_context.patch_object( |
| 211 |
manager, |
| 212 |
"_object_of_interest", |
| 213 |
new=test_context.Mock(spec=Atspi.Accessible), |
| 214 |
) |
| 215 |
manager._active_mode = "focus-tracking" |
| 216 |
|
| 217 |
manager.clear_state() |
| 218 |
assert manager._focus is None |
| 219 |
assert manager._window is None |
| 220 |
assert manager._object_of_interest is None |
| 221 |
assert manager._active_mode is None |
| 222 |
test_context.patch_object(manager, "_focus", new=test_context.Mock(spec=Atspi.Accessible)) |
| 223 |
manager.clear_state("test reason") |
| 224 |
assert manager._focus is None |
| 225 |
|
| 226 |
def test_find_focused_object(self, test_context: OrcaTestContext) -> None: |
| 227 |
"""Test FocusManager.find_focused_object.""" |
| 228 |
|
| 229 |
self._setup_dependencies(test_context) |
| 230 |
from orca.ax_utilities import AXUtilities |
| 231 |
from orca.focus_manager import FocusManager |
| 232 |
|
| 233 |
manager = FocusManager() |
| 234 |
mock_window = test_context.Mock(spec=Atspi.Accessible) |
| 235 |
mock_focused = test_context.Mock(spec=Atspi.Accessible) |
| 236 |
manager._window = mock_window |
| 237 |
mock_get_focused = test_context.Mock(return_value=mock_focused) |
| 238 |
test_context.patch_object(AXUtilities, "get_focused_object", new=mock_get_focused) |
| 239 |
result = manager.find_focused_object() |
| 240 |
assert result == mock_focused |
| 241 |
mock_get_focused.assert_called_once_with(mock_window) |
| 242 |
|
| 243 |
@pytest.mark.parametrize( |
| 244 |
"focus, window, expected", |
| 245 |
[ |
| 246 |
pytest.param(None, None, True, id="both_none"), |
| 247 |
pytest.param(None, "window", False, id="focus_none_window_set"), |
| 248 |
pytest.param("focus", None, False, id="focus_set_window_none"), |
| 249 |
pytest.param("focus", "window", False, id="both_set"), |
| 250 |
], |
| 251 |
) |
| 252 |
def test_focus_and_window_are_unknown(self, test_context, focus, window, expected) -> None: |
| 253 |
"""Test FocusManager.focus_and_window_are_unknown.""" |
| 254 |
|
| 255 |
self._setup_dependencies(test_context) |
| 256 |
from orca.focus_manager import FocusManager |
| 257 |
|
| 258 |
manager = FocusManager() |
| 259 |
focus_obj = test_context.Mock(spec=Atspi.Accessible) if focus else None |
| 260 |
window_obj = test_context.Mock(spec=Atspi.Accessible) if window else None |
| 261 |
test_context.patch_object(manager, "_focus", new=focus_obj) |
| 262 |
test_context.patch_object(manager, "_window", new=window_obj) |
| 263 |
result = manager.focus_and_window_are_unknown() |
| 264 |
assert result == expected |
| 265 |
|
| 266 |
@pytest.mark.parametrize( |
| 267 |
"focus_obj,focus_is_dead,expected", |
| 268 |
[ |
| 269 |
pytest.param("mock_focus", True, True, id="focus_is_dead"), |
| 270 |
pytest.param("mock_focus", False, False, id="focus_is_alive"), |
| 271 |
pytest.param(None, False, False, id="no_focus_set"), |
| 272 |
], |
| 273 |
) |
| 274 |
def test_focus_is_dead(self, test_context, focus_obj, focus_is_dead, expected) -> None: |
| 275 |
"""Test FocusManager.focus_is_dead with various focus states.""" |
| 276 |
|
| 277 |
self._setup_dependencies(test_context) |
| 278 |
from orca.focus_manager import FocusManager |
| 279 |
|
| 280 |
manager = FocusManager() |
| 281 |
|
| 282 |
if focus_obj == "mock_focus": |
| 283 |
mock_focus = test_context.Mock(spec=Atspi.Accessible) |
| 284 |
manager._focus = mock_focus |
| 285 |
expected_call_arg = mock_focus |
| 286 |
else: |
| 287 |
manager._focus = None |
| 288 |
expected_call_arg = None |
| 289 |
|
| 290 |
from orca.ax_object import AXObject |
| 291 |
|
| 292 |
mock_is_dead = test_context.Mock(return_value=focus_is_dead) |
| 293 |
test_context.patch_object(AXObject, "is_dead", new=mock_is_dead) |
| 294 |
|
| 295 |
result = manager.focus_is_dead() |
| 296 |
assert result == expected |
| 297 |
mock_is_dead.assert_called_once_with(expected_call_arg) |
| 298 |
|
| 299 |
@pytest.mark.parametrize( |
| 300 |
"focus, window, expected", |
| 301 |
[ |
| 302 |
pytest.param(None, "window", False, id="no_focus"), |
| 303 |
pytest.param("same_obj", "same_obj", True, id="focus_equals_window"), |
| 304 |
pytest.param("focus_obj", "window_obj", False, id="focus_not_window"), |
| 305 |
], |
| 306 |
) |
| 307 |
def test_focus_is_active_window(self, test_context, focus, window, expected) -> None: |
| 308 |
"""Test FocusManager.focus_is_active_window.""" |
| 309 |
|
| 310 |
self._setup_dependencies(test_context) |
| 311 |
from orca.focus_manager import FocusManager |
| 312 |
|
| 313 |
manager = FocusManager() |
| 314 |
if focus == "same_obj" and window == "same_obj": |
| 315 |
same_obj = test_context.Mock(spec=Atspi.Accessible) |
| 316 |
manager._focus = same_obj |
| 317 |
manager._window = same_obj |
| 318 |
else: |
| 319 |
focus_obj = test_context.Mock(spec=Atspi.Accessible) if focus else None |
| 320 |
window_obj = test_context.Mock(spec=Atspi.Accessible) if window else None |
| 321 |
test_context.patch_object(manager, "_focus", new=focus_obj) |
| 322 |
test_context.patch_object(manager, "_window", new=window_obj) |
| 323 |
result = manager.focus_is_active_window() |
| 324 |
assert result == expected |
| 325 |
|
| 326 |
@pytest.mark.parametrize( |
| 327 |
"focus, window, is_ancestor, expected", |
| 328 |
[ |
| 329 |
pytest.param(None, "window", False, False, id="no_focus"), |
| 330 |
pytest.param("focus", "window", True, True, id="focus_in_window"), |
| 331 |
pytest.param("focus", "window", False, False, id="focus_not_in_window"), |
| 332 |
], |
| 333 |
) |
| 334 |
def test_focus_is_in_active_window( |
| 335 |
self, |
| 336 |
test_context, |
| 337 |
focus, |
| 338 |
window, |
| 339 |
is_ancestor, |
| 340 |
expected, |
| 341 |
) -> None: |
| 342 |
"""Test FocusManager.focus_is_in_active_window.""" |
| 343 |
|
| 344 |
self._setup_dependencies(test_context) |
| 345 |
from orca.focus_manager import FocusManager |
| 346 |
|
| 347 |
manager = FocusManager() |
| 348 |
focus_obj = test_context.Mock(spec=Atspi.Accessible) if focus else None |
| 349 |
window_obj = test_context.Mock(spec=Atspi.Accessible) if window else None |
| 350 |
test_context.patch_object(manager, "_focus", new=focus_obj) |
| 351 |
test_context.patch_object(manager, "_window", new=window_obj) |
| 352 |
from orca.ax_utilities import AXUtilities |
| 353 |
|
| 354 |
mock_is_ancestor = test_context.Mock(return_value=is_ancestor) |
| 355 |
test_context.patch_object(AXUtilities, "is_ancestor", new=mock_is_ancestor) |
| 356 |
result = manager.focus_is_in_active_window() |
| 357 |
assert result == expected |
| 358 |
if focus and window: |
| 359 |
mock_is_ancestor.assert_called_once_with(manager._focus, manager._window) |
| 360 |
|
| 361 |
@pytest.mark.parametrize( |
| 362 |
"start_offset, end_offset, mode, use_null_object", |
| 363 |
[ |
| 364 |
pytest.param(None, None, None, False, id="all_defaults"), |
| 365 |
pytest.param(5, None, None, False, id="start_only"), |
| 366 |
pytest.param(5, 10, None, False, id="start_and_end"), |
| 367 |
pytest.param(5, 10, "caret-tracking", False, id="all_specified"), |
| 368 |
pytest.param(None, None, None, True, id="null_object"), |
| 369 |
], |
| 370 |
) |
| 371 |
def test_emit_region_changed_scenarios( # pylint: disable=too-many-arguments |
| 372 |
self, |
| 373 |
test_context, |
| 374 |
start_offset, |
| 375 |
end_offset, |
| 376 |
mode, |
| 377 |
use_null_object, |
| 378 |
) -> None: |
| 379 |
"""Test FocusManager.emit_region_changed with various scenarios.""" |
| 380 |
essential_modules = self._setup_dependencies(test_context) |
| 381 |
from orca.focus_manager import FLAT_REVIEW, FOCUS_TRACKING, FocusManager |
| 382 |
|
| 383 |
manager = FocusManager() |
| 384 |
|
| 385 |
if use_null_object: |
| 386 |
manager.emit_region_changed(None) |
| 387 |
assert manager._object_of_interest is None |
| 388 |
else: |
| 389 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 390 |
test_context.patch_object(mock_obj, "emit", new=test_context.Mock()) |
| 391 |
expected_start = start_offset if start_offset is not None else 0 |
| 392 |
expected_end = end_offset if end_offset is not None else expected_start |
| 393 |
expected_mode = mode if mode is not None else FOCUS_TRACKING |
| 394 |
|
| 395 |
manager.emit_region_changed(mock_obj, start_offset, end_offset, mode) |
| 396 |
mock_obj.emit.assert_any_call("mode-changed::" + expected_mode, 1, "") |
| 397 |
mock_obj.emit.assert_any_call("region-changed", expected_start, expected_end) |
| 398 |
assert manager._active_mode == expected_mode |
| 399 |
assert manager._object_of_interest == mock_obj |
| 400 |
|
| 401 |
manager.emit_region_changed(mock_obj, mode=FLAT_REVIEW) |
| 402 |
braille_presenter_mock = essential_modules["orca.braille_presenter"] |
| 403 |
presenter = braille_presenter_mock.get_presenter.return_value |
| 404 |
presenter.set_brlapi_priority.assert_called_with(high=True) |
| 405 |
|
| 406 |
manager.emit_region_changed(mock_obj, mode=FOCUS_TRACKING) |
| 407 |
presenter.set_brlapi_priority.assert_called_with() |
| 408 |
|
| 409 |
@pytest.mark.parametrize( |
| 410 |
"active_mode, expected", |
| 411 |
[ |
| 412 |
pytest.param("say-all", True, id="in_say_all"), |
| 413 |
pytest.param("focus-tracking", False, id="not_in_say_all"), |
| 414 |
pytest.param(None, False, id="no_mode"), |
| 415 |
], |
| 416 |
) |
| 417 |
def test_in_say_all(self, test_context, active_mode, expected) -> None: |
| 418 |
"""Test FocusManager.in_say_all.""" |
| 419 |
|
| 420 |
self._setup_dependencies(test_context) |
| 421 |
from orca.focus_manager import FocusManager |
| 422 |
|
| 423 |
manager = FocusManager() |
| 424 |
manager._active_mode = active_mode |
| 425 |
result = manager.in_say_all() |
| 426 |
assert result == expected |
| 427 |
|
| 428 |
def test_get_active_mode_and_object_of_interest(self, test_context: OrcaTestContext) -> None: |
| 429 |
"""Test FocusManager.get_active_mode_and_object_of_interest.""" |
| 430 |
|
| 431 |
self._setup_dependencies(test_context) |
| 432 |
from orca.focus_manager import FocusManager |
| 433 |
|
| 434 |
manager = FocusManager() |
| 435 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 436 |
mode = "caret-tracking" |
| 437 |
manager._active_mode = mode |
| 438 |
manager._object_of_interest = mock_obj |
| 439 |
result_mode, result_obj = manager.get_active_mode_and_object_of_interest() |
| 440 |
assert result_mode == mode |
| 441 |
assert result_obj == mock_obj |
| 442 |
|
| 443 |
def test_get_locus_of_focus(self, test_context: OrcaTestContext) -> None: |
| 444 |
"""Test FocusManager.get_locus_of_focus.""" |
| 445 |
|
| 446 |
self._setup_dependencies(test_context) |
| 447 |
from orca.focus_manager import FocusManager |
| 448 |
|
| 449 |
manager = FocusManager() |
| 450 |
mock_focus = test_context.Mock(spec=Atspi.Accessible) |
| 451 |
manager._focus = mock_focus |
| 452 |
result = manager.get_locus_of_focus() |
| 453 |
assert result == mock_focus |
| 454 |
|
| 455 |
@pytest.mark.parametrize( |
| 456 |
"force, obj_equals_current_focus, notify_script", |
| 457 |
[ |
| 458 |
pytest.param(True, True, True, id="force_same_object"), |
| 459 |
pytest.param(False, True, True, id="no_force_same_object"), |
| 460 |
pytest.param(False, False, True, id="different_object"), |
| 461 |
pytest.param(False, False, False, id="different_object_no_notify"), |
| 462 |
], |
| 463 |
) |
| 464 |
def test_set_locus_of_focus( |
| 465 |
self, |
| 466 |
test_context, |
| 467 |
force, |
| 468 |
obj_equals_current_focus, |
| 469 |
notify_script, |
| 470 |
) -> None: |
| 471 |
"""Test FocusManager.set_locus_of_focus.""" |
| 472 |
|
| 473 |
essential_modules = self._setup_dependencies(test_context) |
| 474 |
from orca.focus_manager import FocusManager |
| 475 |
|
| 476 |
manager = FocusManager() |
| 477 |
mock_event = test_context.Mock(spec=Atspi.Event) |
| 478 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 479 |
mock_app = test_context.Mock(spec=Atspi.Accessible) |
| 480 |
if obj_equals_current_focus: |
| 481 |
manager._focus = mock_obj |
| 482 |
else: |
| 483 |
test_context.patch_object( |
| 484 |
manager, |
| 485 |
"_focus", |
| 486 |
new=test_context.Mock(spec=Atspi.Accessible), |
| 487 |
) |
| 488 |
script_manager_mock = essential_modules["orca.script_manager"] |
| 489 |
manager_instance = script_manager_mock.get_manager.return_value |
| 490 |
script_instance = manager_instance.get_active_script.return_value |
| 491 |
self._setup_locus_of_focus_ax_mocks(test_context, mock_app=mock_app) |
| 492 |
mock_emit = test_context.Mock() |
| 493 |
test_context.patch_object(manager, "emit_region_changed", new=mock_emit) |
| 494 |
manager.set_locus_of_focus(mock_event, mock_obj, notify_script, force) |
| 495 |
if not force and obj_equals_current_focus: |
| 496 |
mock_emit.assert_not_called() |
| 497 |
else: |
| 498 |
assert manager._focus == mock_obj |
| 499 |
if notify_script: |
| 500 |
mock_emit.assert_called_once() |
| 501 |
script_instance.locus_of_focus_changed.assert_called_once() |
| 502 |
else: |
| 503 |
mock_emit.assert_not_called() |
| 504 |
|
| 505 |
def test_set_locus_of_focus_null_object(self, test_context: OrcaTestContext) -> None: |
| 506 |
"""Test FocusManager.set_locus_of_focus with null object.""" |
| 507 |
|
| 508 |
self._setup_dependencies(test_context) |
| 509 |
from orca.ax_object import AXObject |
| 510 |
from orca.focus_manager import FocusManager |
| 511 |
|
| 512 |
test_context.patch_object(AXObject, "is_valid", side_effect=lambda obj: obj is not None) |
| 513 |
manager = FocusManager() |
| 514 |
mock_focus = test_context.Mock(spec=Atspi.Accessible) |
| 515 |
manager._focus = mock_focus |
| 516 |
manager.set_locus_of_focus(None, None) |
| 517 |
assert manager._focus is None |
| 518 |
|
| 519 |
def test_set_locus_of_focus_dead_object(self, test_context: OrcaTestContext) -> None: |
| 520 |
"""Test FocusManager.set_locus_of_focus with dead object.""" |
| 521 |
|
| 522 |
self._setup_dependencies(test_context) |
| 523 |
from orca.focus_manager import FocusManager |
| 524 |
|
| 525 |
self._setup_locus_of_focus_ax_mocks(test_context, is_dead=True) |
| 526 |
manager = FocusManager() |
| 527 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 528 |
old_focus = test_context.Mock(spec=Atspi.Accessible) |
| 529 |
manager._focus = old_focus |
| 530 |
manager.set_locus_of_focus(None, mock_obj) |
| 531 |
|
| 532 |
# Focus should not change since object is dead |
| 533 |
assert manager._focus == old_focus |
| 534 |
|
| 535 |
def test_active_window_is_active(self, test_context: OrcaTestContext) -> None: |
| 536 |
"""Test FocusManager.active_window_is_active.""" |
| 537 |
|
| 538 |
self._setup_dependencies(test_context) |
| 539 |
from orca.focus_manager import FocusManager |
| 540 |
|
| 541 |
manager = FocusManager() |
| 542 |
mock_window = test_context.Mock(spec=Atspi.Accessible) |
| 543 |
manager._window = mock_window |
| 544 |
from orca.ax_object import AXObject |
| 545 |
from orca.ax_utilities import AXUtilities |
| 546 |
|
| 547 |
mock_clear = test_context.Mock() |
| 548 |
test_context.patch_object(AXObject, "clear_cache", new=mock_clear) |
| 549 |
mock_is_active = test_context.Mock(return_value=True) |
| 550 |
test_context.patch_object(AXUtilities, "is_active", new=mock_is_active) |
| 551 |
result = manager.active_window_is_active() |
| 552 |
assert result is True |
| 553 |
mock_clear.assert_called_once_with( |
| 554 |
mock_window, |
| 555 |
False, |
| 556 |
"Ensuring the active window is really active.", |
| 557 |
) |
| 558 |
mock_is_active.assert_called_once_with(mock_window) |
| 559 |
|
| 560 |
def test_get_active_window(self, test_context: OrcaTestContext) -> None: |
| 561 |
"""Test FocusManager.get_active_window.""" |
| 562 |
|
| 563 |
self._setup_dependencies(test_context) |
| 564 |
from orca.focus_manager import FocusManager |
| 565 |
|
| 566 |
manager = FocusManager() |
| 567 |
mock_window = test_context.Mock(spec=Atspi.Accessible) |
| 568 |
manager._window = mock_window |
| 569 |
result = manager.get_active_window() |
| 570 |
assert result == mock_window |
| 571 |
|
| 572 |
@pytest.mark.parametrize( |
| 573 |
"frame_equals_current, set_window_as_focus, focus_is_in_window, has_broken_ancestry", |
| 574 |
[ |
| 575 |
pytest.param(True, False, True, False, id="same_window"), |
| 576 |
pytest.param(False, True, False, False, id="set_as_focus"), |
| 577 |
pytest.param(False, False, False, False, id="focus_not_in_window"), |
| 578 |
pytest.param(False, False, False, True, id="focus_has_broken_ancestry"), |
| 579 |
], |
| 580 |
) |
| 581 |
def test_set_active_window( |
| 582 |
self, |
| 583 |
test_context, |
| 584 |
frame_equals_current, |
| 585 |
set_window_as_focus, |
| 586 |
focus_is_in_window, |
| 587 |
has_broken_ancestry, |
| 588 |
) -> None: |
| 589 |
"""Test FocusManager.set_active_window.""" |
| 590 |
|
| 591 |
self._setup_dependencies(test_context) |
| 592 |
from orca.focus_manager import FocusManager |
| 593 |
|
| 594 |
manager = FocusManager() |
| 595 |
mock_frame = test_context.Mock(spec=Atspi.Accessible) |
| 596 |
mock_app = test_context.Mock(spec=Atspi.Accessible) |
| 597 |
mock_focus = test_context.Mock(spec=Atspi.Accessible) |
| 598 |
if frame_equals_current: |
| 599 |
manager._window = mock_frame |
| 600 |
else: |
| 601 |
test_context.patch_object( |
| 602 |
manager, |
| 603 |
"_window", |
| 604 |
new=test_context.Mock(spec=Atspi.Accessible), |
| 605 |
) |
| 606 |
manager._focus = mock_focus |
| 607 |
from orca.ax_object import AXObject |
| 608 |
from orca.ax_utilities import AXUtilities |
| 609 |
|
| 610 |
mock_set_focus = test_context.Mock() |
| 611 |
test_context.patch_object(manager, "set_locus_of_focus", new=mock_set_focus) |
| 612 |
test_context.patch_object( |
| 613 |
manager, |
| 614 |
"focus_is_active_window", |
| 615 |
return_value=focus_is_in_window, |
| 616 |
) |
| 617 |
test_context.patch_object( |
| 618 |
manager, |
| 619 |
"focus_is_in_active_window", |
| 620 |
return_value=focus_is_in_window, |
| 621 |
) |
| 622 |
test_context.patch_object(AXObject, "has_broken_ancestry", return_value=has_broken_ancestry) |
| 623 |
test_context.patch_object(AXUtilities, "is_ancestor", return_value=False) |
| 624 |
test_context.patch_object(AXUtilities, "get_application", return_value=mock_app) |
| 625 |
test_context.patch_object(AXUtilities, "is_combo_box_popup", return_value=False) |
| 626 |
test_context.patch_object(AXUtilities, "is_combo_box", return_value=False) |
| 627 |
manager.set_active_window(mock_frame, mock_app, set_window_as_focus, False) |
| 628 |
if not frame_equals_current: |
| 629 |
assert manager._window == mock_frame |
| 630 |
if set_window_as_focus: |
| 631 |
mock_set_focus.assert_called_with(None, mock_frame, False) |
| 632 |
elif not focus_is_in_window and not has_broken_ancestry: |
| 633 |
mock_set_focus.assert_called_with(None, mock_frame, notify_script=True) |
| 634 |
|
| 635 |
def test_set_active_window_null_frame(self, test_context: OrcaTestContext) -> None: |
| 636 |
"""Test FocusManager.set_active_window with null frame.""" |
| 637 |
|
| 638 |
self._setup_dependencies(test_context) |
| 639 |
from orca.focus_manager import FocusManager |
| 640 |
|
| 641 |
manager = FocusManager() |
| 642 |
mock_focus = test_context.Mock(spec=Atspi.Accessible) |
| 643 |
manager._focus = mock_focus |
| 644 |
manager.set_active_window(None) |
| 645 |
assert manager._window is None |
| 646 |
|
| 647 |
def test_get_manager(self, test_context: OrcaTestContext) -> None: |
| 648 |
"""Test focus_manager.get_manager.""" |
| 649 |
|
| 650 |
self._setup_dependencies(test_context) |
| 651 |
from orca import focus_manager |
| 652 |
|
| 653 |
manager1 = focus_manager.get_manager() |
| 654 |
manager2 = focus_manager.get_manager() |
| 655 |
assert manager1 is manager2 |
| 656 |
assert isinstance(manager1, focus_manager.FocusManager) |
| 657 |
|
| 658 |
def test_set_locus_of_focus_table_cell_coordinates(self, test_context: OrcaTestContext) -> None: |
| 659 |
"""Test FocusManager.set_locus_of_focus saves table cell coordinates.""" |
| 660 |
|
| 661 |
self._setup_dependencies(test_context) |
| 662 |
from orca.ax_table import AXTable |
| 663 |
from orca.focus_manager import FocusManager |
| 664 |
|
| 665 |
self._setup_locus_of_focus_ax_mocks(test_context, cell_coords=(5, 3)) |
| 666 |
manager = FocusManager() |
| 667 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 668 |
manager.set_locus_of_focus(None, mock_obj) |
| 669 |
|
| 670 |
AXTable.save_last_cell_coordinates.assert_called_once_with(mock_obj) |
| 671 |
|
| 672 |
def test_set_locus_of_focus_text_cursor_position(self, test_context: OrcaTestContext) -> None: |
| 673 |
"""Test FocusManager.set_locus_of_focus saves text cursor position.""" |
| 674 |
|
| 675 |
self._setup_dependencies(test_context) |
| 676 |
from orca.focus_manager import FocusManager |
| 677 |
|
| 678 |
mocks = self._setup_locus_of_focus_ax_mocks(test_context, caret_offset=42) |
| 679 |
mock_get_caret_offset = mocks[1] |
| 680 |
mock_update_cached_text = mocks[2] |
| 681 |
manager = FocusManager() |
| 682 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 683 |
manager.set_locus_of_focus(None, mock_obj) |
| 684 |
|
| 685 |
mock_get_caret_offset.assert_called_once_with(mock_obj) |
| 686 |
mock_update_cached_text.assert_called_once_with(mock_obj) |
| 687 |
obj, offset = manager.get_last_cursor_position() |
| 688 |
assert obj == mock_obj |
| 689 |
assert offset == 42 |
| 690 |
|
| 691 |
def test_set_locus_of_focus_none_object_coordinates( |
| 692 |
self, |
| 693 |
test_context: OrcaTestContext, |
| 694 |
) -> None: |
| 695 |
"""Test FocusManager.set_locus_of_focus handles None object coordinates properly.""" |
| 696 |
|
| 697 |
self._setup_dependencies(test_context) |
| 698 |
from orca.ax_object import AXObject |
| 699 |
from orca.ax_table import AXTable |
| 700 |
from orca.ax_text import AXText |
| 701 |
from orca.ax_utilities import AXUtilities |
| 702 |
from orca.focus_manager import FocusManager |
| 703 |
|
| 704 |
test_context.patch_object(AXObject, "is_valid", side_effect=lambda obj: obj is not None) |
| 705 |
manager = FocusManager() |
| 706 |
|
| 707 |
mock_get_cell_coordinates = test_context.Mock() |
| 708 |
test_context.patch_object( |
| 709 |
AXUtilities, "get_cell_coordinates", new=mock_get_cell_coordinates |
| 710 |
) |
| 711 |
mock_get_caret_offset = test_context.Mock() |
| 712 |
test_context.patch_object(AXText, "get_caret_offset", new=mock_get_caret_offset) |
| 713 |
mock_update_cached_selected_text = test_context.Mock() |
| 714 |
test_context.patch_object( |
| 715 |
AXUtilities, |
| 716 |
"update_cached_selected_text", |
| 717 |
new=mock_update_cached_selected_text, |
| 718 |
) |
| 719 |
manager.set_locus_of_focus(None, None) |
| 720 |
|
| 721 |
AXTable.save_last_cell_coordinates.assert_not_called() |
| 722 |
mock_get_caret_offset.assert_not_called() |
| 723 |
mock_update_cached_selected_text.assert_not_called() |
| 724 |
|
| 725 |
@pytest.mark.parametrize( |
| 726 |
"method_name,attribute_name,test_offset", |
| 727 |
[ |
| 728 |
pytest.param( |
| 729 |
"get_penultimate_cursor_position", |
| 730 |
"_penultimate_cursor_position", |
| 731 |
42, |
| 732 |
id="penultimate", |
| 733 |
), |
| 734 |
pytest.param("get_last_cursor_position", "_last_cursor_position", 123, id="last"), |
| 735 |
], |
| 736 |
) |
| 737 |
def test_cursor_position_debug_logging( |
| 738 |
self, |
| 739 |
test_context: OrcaTestContext, |
| 740 |
method_name, |
| 741 |
attribute_name, |
| 742 |
test_offset, |
| 743 |
) -> None: |
| 744 |
"""Test cursor position methods log debug information.""" |
| 745 |
|
| 746 |
essential_modules = self._setup_dependencies(test_context) |
| 747 |
from orca.focus_manager import FocusManager |
| 748 |
|
| 749 |
focus_manager = FocusManager() |
| 750 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 751 |
setattr(focus_manager, attribute_name, (mock_obj, test_offset)) |
| 752 |
|
| 753 |
method = getattr(focus_manager, method_name) |
| 754 |
obj, offset = method() |
| 755 |
|
| 756 |
assert obj is mock_obj |
| 757 |
assert offset == test_offset |
| 758 |
essential_modules["orca.debug"].print_tokens.assert_called() |
| 759 |
|
| 760 |
def test_set_locus_of_focus_null_object_with_debug(self, test_context: OrcaTestContext) -> None: |
| 761 |
"""Test FocusManager.set_locus_of_focus with null object logs debug message.""" |
| 762 |
|
| 763 |
essential_modules = self._setup_dependencies(test_context) |
| 764 |
from orca.focus_manager import FocusManager |
| 765 |
|
| 766 |
focus_manager = FocusManager() |
| 767 |
initial_focus = test_context.Mock(spec=Atspi.Accessible) |
| 768 |
focus_manager._focus = initial_focus |
| 769 |
focus_manager.set_locus_of_focus(None, None) |
| 770 |
|
| 771 |
assert focus_manager._focus is None |
| 772 |
essential_modules["orca.debug"].print_message.assert_called() |
| 773 |
|
| 774 |
def test_set_locus_of_focus_with_event_and_force_flag( |
| 775 |
self, |
| 776 |
test_context: OrcaTestContext, |
| 777 |
) -> None: |
| 778 |
"""Test FocusManager.set_locus_of_focus with event and force flag.""" |
| 779 |
|
| 780 |
essential_modules = self._setup_dependencies(test_context) |
| 781 |
from orca.focus_manager import FocusManager |
| 782 |
|
| 783 |
focus_manager = FocusManager() |
| 784 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 785 |
mock_event = test_context.Mock() |
| 786 |
focus_manager.set_locus_of_focus(mock_event, mock_obj, force=True) |
| 787 |
essential_modules["orca.debug"].print_tokens.assert_called() |
| 788 |
|
| 789 |
def test_reset_active_mode_with_focus(self, test_context: OrcaTestContext) -> None: |
| 790 |
"""Test reset_active_mode sets FOCUS_TRACKING and emits signal when focus exists.""" |
| 791 |
|
| 792 |
self._setup_dependencies(test_context) |
| 793 |
from orca.focus_manager import FOCUS_TRACKING, FocusManager |
| 794 |
|
| 795 |
manager = FocusManager() |
| 796 |
mock_focus = test_context.Mock(spec=Atspi.Accessible) |
| 797 |
mock_focus.emit = test_context.Mock() |
| 798 |
manager._focus = mock_focus |
| 799 |
manager._active_mode = "say-all" |
| 800 |
|
| 801 |
manager.reset_active_mode("test reason") |
| 802 |
|
| 803 |
assert manager._active_mode == FOCUS_TRACKING |
| 804 |
mock_focus.emit.assert_called_once_with("mode-changed::" + FOCUS_TRACKING, 1, "test reason") |
| 805 |
|
| 806 |
def test_reset_active_mode_without_focus(self, test_context: OrcaTestContext) -> None: |
| 807 |
"""Test reset_active_mode sets None when focus is None.""" |
| 808 |
|
| 809 |
self._setup_dependencies(test_context) |
| 810 |
from orca.focus_manager import FocusManager |
| 811 |
|
| 812 |
manager = FocusManager() |
| 813 |
manager._focus = None |
| 814 |
manager._active_mode = "say-all" |
| 815 |
|
| 816 |
manager.reset_active_mode() |
| 817 |
|
| 818 |
assert manager._active_mode is None |
| 819 |
|
| 820 |
def test_reset_active_mode_without_reason(self, test_context: OrcaTestContext) -> None: |
| 821 |
"""Test reset_active_mode works without a reason string.""" |
| 822 |
|
| 823 |
self._setup_dependencies(test_context) |
| 824 |
from orca.focus_manager import FOCUS_TRACKING, FocusManager |
| 825 |
|
| 826 |
manager = FocusManager() |
| 827 |
mock_focus = test_context.Mock(spec=Atspi.Accessible) |
| 828 |
mock_focus.emit = test_context.Mock() |
| 829 |
manager._focus = mock_focus |
| 830 |
manager._active_mode = "say-all" |
| 831 |
|
| 832 |
manager.reset_active_mode() |
| 833 |
|
| 834 |
assert manager._active_mode == FOCUS_TRACKING |
| 835 |
mock_focus.emit.assert_called_once_with("mode-changed::" + FOCUS_TRACKING, 1, "") |