| 1 |
# Unit tests for caret_navigator.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 caret_navigator.py methods.""" |
| 31 |
|
| 32 |
from __future__ import annotations |
| 33 |
|
| 34 |
from typing import TYPE_CHECKING, Any |
| 35 |
|
| 36 |
import pytest |
| 37 |
|
| 38 |
if TYPE_CHECKING: |
| 39 |
from .orca_test_context import OrcaTestContext |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.unit |
| 43 |
class TestCaretNavigator: |
| 44 |
"""Test CaretNavigator class methods.""" |
| 45 |
|
| 46 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, Any]: |
| 47 |
"""Set up mocks for caret_navigator dependencies.""" |
| 48 |
|
| 49 |
additional_modules = [ |
| 50 |
"orca.command_manager", |
| 51 |
"orca.input_event_manager", |
| 52 |
"orca.keybindings", |
| 53 |
"orca.cmdnames", |
| 54 |
"orca.guilabels", |
| 55 |
"orca.debug", |
| 56 |
"orca.ax_object", |
| 57 |
"orca.ax_text", |
| 58 |
"orca.script_manager", |
| 59 |
"orca.messages", |
| 60 |
"orca.object_properties", |
| 61 |
"orca.orca_gui_navlist", |
| 62 |
"orca.orca_i18n", |
| 63 |
"orca.AXHypertext", |
| 64 |
"orca.AXObject", |
| 65 |
"orca.AXTable", |
| 66 |
"orca.AXText", |
| 67 |
"orca.AXUtilities", |
| 68 |
"orca.input_event", |
| 69 |
"orca.braille_presenter", |
| 70 |
"orca.presentation_manager", |
| 71 |
] |
| 72 |
essential_modules = test_context.setup_shared_dependencies(additional_modules) |
| 73 |
|
| 74 |
# Set up cmdnames with all required values for structural_navigator |
| 75 |
cmdnames = essential_modules["orca.cmdnames"] |
| 76 |
cmdnames.STRUCTURAL_NAVIGATION_MODE_CYCLE = "cycle_mode" |
| 77 |
cmdnames.BLOCKQUOTE_PREV = "previous_blockquote" |
| 78 |
cmdnames.BLOCKQUOTE_NEXT = "next_blockquote" |
| 79 |
cmdnames.BLOCKQUOTE_LIST = "list_blockquotes" |
| 80 |
cmdnames.BUTTON_PREV = "previous_button" |
| 81 |
cmdnames.BUTTON_NEXT = "next_button" |
| 82 |
cmdnames.BUTTON_LIST = "list_buttons" |
| 83 |
cmdnames.CHECK_BOX_PREV = "previous_checkbox" |
| 84 |
cmdnames.CHECK_BOX_NEXT = "next_checkbox" |
| 85 |
cmdnames.CHECK_BOX_LIST = "list_checkboxes" |
| 86 |
cmdnames.COMBO_BOX_PREV = "previous_combobox" |
| 87 |
cmdnames.COMBO_BOX_NEXT = "next_combobox" |
| 88 |
cmdnames.COMBO_BOX_LIST = "list_comboboxes" |
| 89 |
cmdnames.ENTRY_PREV = "previous_entry" |
| 90 |
cmdnames.ENTRY_NEXT = "next_entry" |
| 91 |
cmdnames.ENTRY_LIST = "list_entries" |
| 92 |
cmdnames.FORM_FIELD_PREV = "previous_form_field" |
| 93 |
cmdnames.FORM_FIELD_NEXT = "next_form_field" |
| 94 |
cmdnames.FORM_FIELD_LIST = "list_form_fields" |
| 95 |
cmdnames.HEADING_PREV = "previous_heading" |
| 96 |
cmdnames.HEADING_NEXT = "next_heading" |
| 97 |
cmdnames.HEADING_LIST = "list_headings" |
| 98 |
cmdnames.HEADING_AT_LEVEL_PREV = "previous_heading_level_%d" |
| 99 |
cmdnames.HEADING_AT_LEVEL_NEXT = "next_heading_level_%d" |
| 100 |
cmdnames.HEADING_AT_LEVEL_LIST = "list_headings_level_%d" |
| 101 |
cmdnames.IFRAME_PREV = "previous_iframe" |
| 102 |
cmdnames.IFRAME_NEXT = "next_iframe" |
| 103 |
cmdnames.IFRAME_LIST = "list_iframes" |
| 104 |
cmdnames.IMAGE_PREV = "previous_image" |
| 105 |
cmdnames.IMAGE_NEXT = "next_image" |
| 106 |
cmdnames.IMAGE_LIST = "list_images" |
| 107 |
cmdnames.LANDMARK_PREV = "previous_landmark" |
| 108 |
cmdnames.LANDMARK_NEXT = "next_landmark" |
| 109 |
cmdnames.LANDMARK_LIST = "list_landmarks" |
| 110 |
cmdnames.LIST_PREV = "previous_list" |
| 111 |
cmdnames.LIST_NEXT = "next_list" |
| 112 |
cmdnames.LIST_LIST = "list_lists" |
| 113 |
cmdnames.LIST_ITEM_PREV = "previous_list_item" |
| 114 |
cmdnames.LIST_ITEM_NEXT = "next_list_item" |
| 115 |
cmdnames.LIST_ITEM_LIST = "list_list_items" |
| 116 |
cmdnames.LIVE_REGION_PREV = "previous_live_region" |
| 117 |
cmdnames.LIVE_REGION_NEXT = "next_live_region" |
| 118 |
cmdnames.LIVE_REGION_LAST = "last_live_region" |
| 119 |
cmdnames.PARAGRAPH_PREV = "previous_paragraph" |
| 120 |
cmdnames.PARAGRAPH_NEXT = "next_paragraph" |
| 121 |
cmdnames.PARAGRAPH_LIST = "list_paragraphs" |
| 122 |
cmdnames.RADIO_BUTTON_PREV = "previous_radio_button" |
| 123 |
cmdnames.RADIO_BUTTON_NEXT = "next_radio_button" |
| 124 |
cmdnames.RADIO_BUTTON_LIST = "list_radio_buttons" |
| 125 |
cmdnames.SEPARATOR_PREV = "previous_separator" |
| 126 |
cmdnames.SEPARATOR_NEXT = "next_separator" |
| 127 |
cmdnames.TABLE_PREV = "previous_table" |
| 128 |
cmdnames.TABLE_NEXT = "next_table" |
| 129 |
cmdnames.TABLE_LIST = "list_tables" |
| 130 |
cmdnames.UNVISITED_LINK_PREV = "previous_unvisited_link" |
| 131 |
cmdnames.UNVISITED_LINK_NEXT = "next_unvisited_link" |
| 132 |
cmdnames.UNVISITED_LINK_LIST = "list_unvisited_links" |
| 133 |
cmdnames.VISITED_LINK_PREV = "previous_visited_link" |
| 134 |
cmdnames.VISITED_LINK_NEXT = "next_visited_link" |
| 135 |
cmdnames.VISITED_LINK_LIST = "list_visited_links" |
| 136 |
cmdnames.LINK_PREV = "previous_link" |
| 137 |
cmdnames.LINK_NEXT = "next_link" |
| 138 |
cmdnames.LINK_LIST = "list_links" |
| 139 |
cmdnames.CLICKABLE_PREV = "previous_clickable" |
| 140 |
cmdnames.CLICKABLE_NEXT = "next_clickable" |
| 141 |
cmdnames.CLICKABLE_LIST = "list_clickables" |
| 142 |
cmdnames.LARGE_OBJECT_PREV = "previous_large_object" |
| 143 |
cmdnames.LARGE_OBJECT_NEXT = "next_large_object" |
| 144 |
cmdnames.LARGE_OBJECT_LIST = "list_large_objects" |
| 145 |
cmdnames.CONTAINER_START = "container_start" |
| 146 |
cmdnames.CONTAINER_END = "container_end" |
| 147 |
|
| 148 |
essential_modules["orca.orca_i18n"]._ = lambda x: x |
| 149 |
essential_modules["orca.debug"].print_message = test_context.Mock() |
| 150 |
essential_modules["orca.debug"].LEVEL_INFO = 800 |
| 151 |
|
| 152 |
controller_mock = test_context.Mock() |
| 153 |
controller_mock.register_decorated_module.return_value = None |
| 154 |
essential_modules["orca.dbus_service"].get_remote_controller.return_value = controller_mock |
| 155 |
|
| 156 |
focus_manager_instance = test_context.Mock() |
| 157 |
focus_manager_instance.get_locus_of_focus.return_value = None |
| 158 |
essential_modules["orca.focus_manager"].get_manager.return_value = focus_manager_instance |
| 159 |
|
| 160 |
essential_modules["orca.AXObject"].supports_collection.return_value = True |
| 161 |
essential_modules["orca.AXUtilities"].is_heading.return_value = False |
| 162 |
|
| 163 |
return essential_modules |
| 164 |
|
| 165 |
@pytest.mark.parametrize( |
| 166 |
"direction,event_provided,context_available,expected_result", |
| 167 |
[ |
| 168 |
pytest.param("next", False, True, True, id="next_char_no_event_returns_true"), |
| 169 |
pytest.param("next", True, False, False, id="next_char_no_context_returns_false"), |
| 170 |
pytest.param("next", True, True, True, id="next_char_valid_navigation_succeeds"), |
| 171 |
pytest.param("previous", False, True, True, id="prev_char_no_event_returns_true"), |
| 172 |
pytest.param("previous", True, False, False, id="prev_char_no_context_returns_false"), |
| 173 |
pytest.param("previous", True, True, True, id="prev_char_valid_navigation_succeeds"), |
| 174 |
], |
| 175 |
) |
| 176 |
def test_character_navigation( |
| 177 |
self, |
| 178 |
test_context: OrcaTestContext, |
| 179 |
direction: str, |
| 180 |
event_provided: bool, |
| 181 |
context_available: bool, |
| 182 |
expected_result: bool, |
| 183 |
) -> None: |
| 184 |
"""Test character navigation (next/previous) with various conditions.""" |
| 185 |
|
| 186 |
essential_modules = self._setup_dependencies(test_context) |
| 187 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 188 |
|
| 189 |
ax_object_mock = essential_modules["orca.ax_object"] |
| 190 |
ax_object_mock.AXObject.supports_text.side_effect = lambda obj: obj is not None |
| 191 |
ax_object_mock.AXObject.is_valid.side_effect = lambda obj: obj is not None |
| 192 |
ax_object_mock.AXObject.is_ancestor.side_effect = ( |
| 193 |
lambda obj, root, same: obj is not None and root is not None |
| 194 |
) |
| 195 |
|
| 196 |
navigator = CaretNavigator() |
| 197 |
test_context.patch_object(navigator, "_get_root_object", return_value=None) |
| 198 |
mock_script = test_context.Mock() |
| 199 |
mock_event = test_context.Mock() if event_provided else None |
| 200 |
|
| 201 |
if context_available: |
| 202 |
mock_obj = test_context.Mock() |
| 203 |
if direction == "next": |
| 204 |
mock_script.utilities.next_context.return_value = (mock_obj, 10) |
| 205 |
else: |
| 206 |
mock_script.utilities.previous_context.return_value = (mock_obj, 5) |
| 207 |
elif direction == "next": |
| 208 |
mock_script.utilities.next_context.return_value = (None, 0) |
| 209 |
else: |
| 210 |
mock_script.utilities.previous_context.return_value = (None, 0) |
| 211 |
|
| 212 |
navigation_method = getattr(navigator, f"{direction}_character") |
| 213 |
result = navigation_method(mock_script, mock_event) |
| 214 |
assert result == expected_result |
| 215 |
|
| 216 |
if expected_result: |
| 217 |
pres_manager = essential_modules["orca.presentation_manager"].get_manager() |
| 218 |
assert navigator._last_input_event == mock_event |
| 219 |
mock_script.utilities.set_caret_position.assert_called_once() |
| 220 |
pres_manager.interrupt_presentation.assert_called_once() |
| 221 |
mock_script.update_braille.assert_called_once() |
| 222 |
mock_script.say_character.assert_called_once() |
| 223 |
|
| 224 |
@pytest.mark.parametrize( |
| 225 |
"direction,context_result,word_contents,expected_result", |
| 226 |
[ |
| 227 |
pytest.param("next", (None, 0), None, False, id="next_word_no_context"), |
| 228 |
pytest.param("next", ("obj", 20), [], False, id="next_word_no_contents"), |
| 229 |
pytest.param( |
| 230 |
"next", |
| 231 |
("obj", 20), |
| 232 |
[("obj", 20, 25, "word")], |
| 233 |
True, |
| 234 |
id="next_word_success", |
| 235 |
), |
| 236 |
pytest.param("previous", (None, 0), None, False, id="previous_word_no_context"), |
| 237 |
pytest.param("previous", ("obj", 15), [], False, id="previous_word_no_contents"), |
| 238 |
pytest.param( |
| 239 |
"previous", |
| 240 |
("obj", 15), |
| 241 |
[("obj", 10, 15, "word")], |
| 242 |
True, |
| 243 |
id="previous_word_success", |
| 244 |
), |
| 245 |
], |
| 246 |
) |
| 247 |
def test_word_navigation( |
| 248 |
self, |
| 249 |
test_context: OrcaTestContext, |
| 250 |
direction: str, |
| 251 |
context_result: tuple, |
| 252 |
word_contents: list | None, |
| 253 |
expected_result: bool, |
| 254 |
) -> None: |
| 255 |
"""Test word navigation (next/previous) with various error conditions.""" |
| 256 |
|
| 257 |
essential_modules = self._setup_dependencies(test_context) |
| 258 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 259 |
|
| 260 |
navigator = CaretNavigator() |
| 261 |
mock_script = test_context.Mock() |
| 262 |
mock_event = test_context.Mock() |
| 263 |
|
| 264 |
if direction == "next": |
| 265 |
mock_script.utilities.next_context.return_value = context_result |
| 266 |
else: |
| 267 |
mock_script.utilities.previous_context.return_value = context_result |
| 268 |
|
| 269 |
mock_script.utilities.get_word_contents_at_offset.return_value = word_contents or [] |
| 270 |
|
| 271 |
mock_script.utilities.set_caret_position = test_context.Mock() |
| 272 |
mock_script.update_braille = test_context.Mock() |
| 273 |
mock_script.say_word = test_context.Mock() |
| 274 |
|
| 275 |
navigation_method = getattr(navigator, f"{direction}_word") |
| 276 |
result = navigation_method(mock_script, mock_event) |
| 277 |
|
| 278 |
assert result == expected_result |
| 279 |
|
| 280 |
pres_manager = essential_modules["orca.presentation_manager"].get_manager() |
| 281 |
if expected_result: |
| 282 |
assert navigator._last_input_event == mock_event |
| 283 |
mock_script.utilities.set_caret_position.assert_called() |
| 284 |
pres_manager.interrupt_presentation.assert_called_once() |
| 285 |
mock_script.update_braille.assert_called_once() |
| 286 |
mock_script.say_word.assert_called_once() |
| 287 |
else: |
| 288 |
mock_script.utilities.set_caret_position.assert_not_called() |
| 289 |
pres_manager.interrupt_presentation.assert_not_called() |
| 290 |
|
| 291 |
@pytest.mark.parametrize( |
| 292 |
"test_method,expected_result", |
| 293 |
[ |
| 294 |
pytest.param("suspend_commands", True, id="suspend_commands"), |
| 295 |
pytest.param("toggle_enabled", True, id="toggle_enabled"), |
| 296 |
], |
| 297 |
) |
| 298 |
def test_navigator_control_methods( |
| 299 |
self, |
| 300 |
test_context: OrcaTestContext, |
| 301 |
test_method: str, |
| 302 |
expected_result: bool, |
| 303 |
) -> None: |
| 304 |
"""Test CaretNavigator control methods.""" |
| 305 |
|
| 306 |
essential_modules = self._setup_dependencies(test_context) |
| 307 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 308 |
|
| 309 |
navigator = CaretNavigator() |
| 310 |
mock_script = test_context.Mock() |
| 311 |
mock_event = test_context.Mock() |
| 312 |
|
| 313 |
if test_method == "suspend_commands": |
| 314 |
mock_cmd_mgr = test_context.Mock() |
| 315 |
essential_modules["orca.command_manager"].get_manager.return_value = mock_cmd_mgr |
| 316 |
test_context.patch_object(navigator, "_is_active_script", return_value=True) |
| 317 |
navigator._suspended = False |
| 318 |
navigator.suspend_commands(mock_script, True, "test reason") |
| 319 |
assert navigator._suspended == expected_result |
| 320 |
mock_cmd_mgr.set_group_suspended.assert_called_once() |
| 321 |
|
| 322 |
elif test_method == "toggle_enabled": |
| 323 |
mock_cmd_mgr = test_context.Mock() |
| 324 |
essential_modules["orca.command_manager"].get_manager.return_value = mock_cmd_mgr |
| 325 |
|
| 326 |
guilabels_mock = essential_modules["orca.guilabels"] |
| 327 |
guilabels_mock.CARET_NAVIGATION_ENABLED = "Caret navigation enabled" |
| 328 |
guilabels_mock.CARET_NAVIGATION_DISABLED = "Caret navigation disabled" |
| 329 |
|
| 330 |
result = navigator.toggle_enabled(mock_script, mock_event) |
| 331 |
assert result == expected_result |
| 332 |
mock_cmd_mgr.set_group_enabled.assert_called_once() |
| 333 |
|
| 334 |
def test_navigator_initialization(self, test_context: OrcaTestContext) -> None: |
| 335 |
"""Test CaretNavigator initialization.""" |
| 336 |
self._setup_dependencies(test_context) |
| 337 |
from orca import command_manager |
| 338 |
from orca.caret_navigator import CaretNavigator |
| 339 |
|
| 340 |
navigator = CaretNavigator() |
| 341 |
|
| 342 |
assert hasattr(navigator, "_last_input_event") |
| 343 |
assert hasattr(navigator, "_suspended") |
| 344 |
assert navigator._last_input_event is None |
| 345 |
assert navigator._suspended is False |
| 346 |
# Commands are registered in CommandManager |
| 347 |
cmd_manager = command_manager.get_manager() |
| 348 |
assert cmd_manager is not None |
| 349 |
|
| 350 |
@pytest.mark.parametrize( |
| 351 |
"navigation_type,in_say_all,current_line,next_prev_contents,expected_result", |
| 352 |
[ |
| 353 |
pytest.param( |
| 354 |
"next_line", |
| 355 |
True, |
| 356 |
[("obj", 0, 10, "text")], |
| 357 |
[], |
| 358 |
True, |
| 359 |
id="next_line_in_say_all", |
| 360 |
), |
| 361 |
pytest.param("next_line", False, [], [], False, id="next_line_no_current_line"), |
| 362 |
pytest.param( |
| 363 |
"next_line", |
| 364 |
False, |
| 365 |
[("obj", 0, 10, "text")], |
| 366 |
[], |
| 367 |
False, |
| 368 |
id="next_line_no_next_contents", |
| 369 |
), |
| 370 |
pytest.param( |
| 371 |
"next_line", |
| 372 |
False, |
| 373 |
[("obj", 0, 10, "text")], |
| 374 |
[("obj2", 11, 21, "next")], |
| 375 |
True, |
| 376 |
id="next_line_success", |
| 377 |
), |
| 378 |
pytest.param( |
| 379 |
"previous_line", |
| 380 |
True, |
| 381 |
[("obj", 0, 10, "text")], |
| 382 |
[], |
| 383 |
True, |
| 384 |
id="previous_line_in_say_all", |
| 385 |
), |
| 386 |
pytest.param("previous_line", False, [], [], False, id="previous_line_no_contents"), |
| 387 |
pytest.param( |
| 388 |
"previous_line", |
| 389 |
False, |
| 390 |
[("obj", 0, 10, "text")], |
| 391 |
[("obj", 0, 10, "prev")], |
| 392 |
True, |
| 393 |
id="previous_line_success", |
| 394 |
), |
| 395 |
], |
| 396 |
) |
| 397 |
def test_line_navigation( |
| 398 |
self, |
| 399 |
test_context: OrcaTestContext, |
| 400 |
navigation_type: str, |
| 401 |
in_say_all: bool, |
| 402 |
current_line: list | None, |
| 403 |
next_prev_contents: list, |
| 404 |
expected_result: bool, |
| 405 |
) -> None: |
| 406 |
"""Test line navigation including say-all mode handling.""" |
| 407 |
|
| 408 |
essential_modules = self._setup_dependencies(test_context) |
| 409 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 410 |
|
| 411 |
navigator = CaretNavigator() |
| 412 |
mock_script = test_context.Mock() |
| 413 |
mock_event = test_context.Mock() |
| 414 |
|
| 415 |
focus_manager_mock = essential_modules["orca.focus_manager"] |
| 416 |
manager_instance = test_context.Mock() |
| 417 |
focus_manager_mock.get_manager.return_value = manager_instance |
| 418 |
manager_instance.in_say_all.return_value = in_say_all |
| 419 |
|
| 420 |
if in_say_all: |
| 421 |
from orca import say_all_presenter # pylint: disable=import-outside-toplevel |
| 422 |
|
| 423 |
say_all_presenter.get_presenter().set_rewind_and_fast_forward_enabled(True) |
| 424 |
|
| 425 |
if navigation_type == "next_line" and not in_say_all: |
| 426 |
mock_script.utilities.get_caret_context.return_value = ("obj", 5) |
| 427 |
mock_script.utilities.get_line_contents_at_offset.return_value = current_line |
| 428 |
mock_script.utilities.get_next_line_contents.return_value = next_prev_contents |
| 429 |
test_context.patch_object(navigator, "_get_end_of_file", return_value=(None, -1)) |
| 430 |
test_context.patch_object(navigator, "_line_contains_context", return_value=False) |
| 431 |
test_context.patch_object(navigator, "_is_navigable_object", return_value=True) |
| 432 |
elif navigation_type == "previous_line" and not in_say_all: |
| 433 |
mock_script.utilities.get_caret_context.return_value = ("obj", 5) |
| 434 |
mock_script.utilities.get_line_contents_at_offset.return_value = current_line |
| 435 |
mock_script.utilities.get_previous_line_contents.return_value = next_prev_contents |
| 436 |
test_context.patch_object(navigator, "_get_start_of_file", return_value=(None, -1)) |
| 437 |
test_context.patch_object(navigator, "_line_contains_context", return_value=False) |
| 438 |
test_context.patch_object(navigator, "_is_navigable_object", return_value=True) |
| 439 |
|
| 440 |
mock_script.utilities.set_caret_position = test_context.Mock() |
| 441 |
|
| 442 |
pres_manager = essential_modules["orca.presentation_manager"].get_manager() |
| 443 |
pres_manager.interrupt_presentation.reset_mock() |
| 444 |
pres_manager.speak_contents.reset_mock() |
| 445 |
pres_manager.display_contents.reset_mock() |
| 446 |
|
| 447 |
navigation_method = getattr(navigator, f"{navigation_type}") |
| 448 |
result = navigation_method(mock_script, mock_event) |
| 449 |
|
| 450 |
assert result == expected_result |
| 451 |
|
| 452 |
if expected_result and not in_say_all: |
| 453 |
assert navigator._last_input_event == mock_event |
| 454 |
mock_script.utilities.set_caret_position.assert_called() |
| 455 |
pres_manager.interrupt_presentation.assert_called_once() |
| 456 |
pres_manager.speak_contents.assert_called_once() |
| 457 |
pres_manager.display_contents.assert_called_once() |
| 458 |
elif in_say_all: |
| 459 |
assert navigator._last_input_event != mock_event |
| 460 |
|
| 461 |
@pytest.mark.parametrize( |
| 462 |
"navigation_type,line_contents,expected_result", |
| 463 |
[ |
| 464 |
pytest.param("start_of_line", [], False, id="start_of_line_no_line"), |
| 465 |
pytest.param( |
| 466 |
"start_of_line", |
| 467 |
[("obj", 5, 15, "text")], |
| 468 |
True, |
| 469 |
id="start_of_line_success", |
| 470 |
), |
| 471 |
pytest.param("end_of_line", [], False, id="end_of_line_no_line"), |
| 472 |
pytest.param( |
| 473 |
"end_of_line", |
| 474 |
[("obj", 5, 15, "text ")], |
| 475 |
True, |
| 476 |
id="end_of_line_with_space", |
| 477 |
), |
| 478 |
pytest.param("end_of_line", [("obj", 5, 15, "text")], True, id="end_of_line_no_space"), |
| 479 |
], |
| 480 |
) |
| 481 |
def test_line_boundary_navigation( |
| 482 |
self, |
| 483 |
test_context: OrcaTestContext, |
| 484 |
navigation_type: str, |
| 485 |
line_contents: list, |
| 486 |
expected_result: bool, |
| 487 |
) -> None: |
| 488 |
"""Test start/end of line navigation.""" |
| 489 |
|
| 490 |
essential_modules = self._setup_dependencies(test_context) |
| 491 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 492 |
|
| 493 |
navigator = CaretNavigator() |
| 494 |
mock_script = test_context.Mock() |
| 495 |
mock_event = test_context.Mock() |
| 496 |
|
| 497 |
mock_script.utilities.get_caret_context.return_value = ("obj", 10) |
| 498 |
mock_script.utilities.get_line_contents_at_offset.return_value = line_contents |
| 499 |
|
| 500 |
mock_script.utilities.set_caret_position = test_context.Mock() |
| 501 |
mock_script.say_character = test_context.Mock() |
| 502 |
|
| 503 |
pres_manager = essential_modules["orca.presentation_manager"].get_manager() |
| 504 |
pres_manager.interrupt_presentation.reset_mock() |
| 505 |
pres_manager.display_contents.reset_mock() |
| 506 |
|
| 507 |
navigation_method = getattr(navigator, f"{navigation_type}") |
| 508 |
result = navigation_method(mock_script, mock_event) |
| 509 |
|
| 510 |
assert result == expected_result |
| 511 |
|
| 512 |
if expected_result: |
| 513 |
assert navigator._last_input_event == mock_event |
| 514 |
mock_script.utilities.set_caret_position.assert_called() |
| 515 |
pres_manager.interrupt_presentation.assert_called_once() |
| 516 |
mock_script.say_character.assert_called_once() |
| 517 |
pres_manager.display_contents.assert_called_once() |
| 518 |
|
| 519 |
@pytest.mark.parametrize( |
| 520 |
"script_is_active,expected_result", |
| 521 |
[ |
| 522 |
pytest.param(True, True, id="script_is_active"), |
| 523 |
pytest.param(False, False, id="script_is_not_active"), |
| 524 |
], |
| 525 |
) |
| 526 |
def test_is_active_script( |
| 527 |
self, |
| 528 |
test_context: OrcaTestContext, |
| 529 |
script_is_active: bool, |
| 530 |
expected_result: bool, |
| 531 |
) -> None: |
| 532 |
"""Test _is_active_script method with active and non-active scripts.""" |
| 533 |
|
| 534 |
essential_modules = self._setup_dependencies(test_context) |
| 535 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 536 |
|
| 537 |
navigator = CaretNavigator() |
| 538 |
mock_script = test_context.Mock() |
| 539 |
mock_active_script = test_context.Mock() |
| 540 |
|
| 541 |
script_manager_mock = essential_modules["orca.script_manager"] |
| 542 |
manager_instance = test_context.Mock() |
| 543 |
script_manager_mock.get_manager.return_value = manager_instance |
| 544 |
|
| 545 |
if script_is_active: |
| 546 |
manager_instance.get_active_script.return_value = mock_script |
| 547 |
else: |
| 548 |
manager_instance.get_active_script.return_value = mock_active_script |
| 549 |
|
| 550 |
debug_mock = essential_modules["orca.debug"] |
| 551 |
debug_mock.LEVEL_INFO = 800 |
| 552 |
debug_mock.print_tokens = test_context.Mock() |
| 553 |
|
| 554 |
result = navigator._is_active_script(mock_script) |
| 555 |
assert result == expected_result |
| 556 |
|
| 557 |
if not script_is_active: |
| 558 |
debug_mock.print_tokens.assert_called_once() |
| 559 |
|
| 560 |
def test_get_is_enabled(self, test_context: OrcaTestContext) -> None: |
| 561 |
"""Test get_is_enabled returns setting value.""" |
| 562 |
|
| 563 |
self._setup_dependencies(test_context) |
| 564 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 565 |
|
| 566 |
navigator = CaretNavigator() |
| 567 |
result = navigator.get_is_enabled() |
| 568 |
assert result is True |
| 569 |
|
| 570 |
def test_set_is_enabled_no_change(self, test_context: OrcaTestContext) -> None: |
| 571 |
"""Test set_is_enabled still calls set_group_enabled even if value unchanged.""" |
| 572 |
|
| 573 |
essential_modules = self._setup_dependencies(test_context) |
| 574 |
mock_cmd_mgr = test_context.Mock() |
| 575 |
essential_modules["orca.command_manager"].get_manager.return_value = mock_cmd_mgr |
| 576 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 577 |
|
| 578 |
navigator = CaretNavigator() |
| 579 |
result = navigator.set_is_enabled(True) |
| 580 |
assert result is True |
| 581 |
mock_cmd_mgr.set_group_enabled.assert_called_once() |
| 582 |
|
| 583 |
def test_set_is_enabled_updates_setting(self, test_context: OrcaTestContext) -> None: |
| 584 |
"""Test set_is_enabled updates setting and calls CommandManager.""" |
| 585 |
|
| 586 |
essential_modules = self._setup_dependencies(test_context) |
| 587 |
from orca import gsettings_registry |
| 588 |
|
| 589 |
gsettings_registry.get_registry().set_runtime_value("caret-navigation", "enabled", False) |
| 590 |
mock_script = test_context.Mock() |
| 591 |
essential_modules[ |
| 592 |
"orca.script_manager" |
| 593 |
].get_manager.return_value.get_active_script.return_value = mock_script |
| 594 |
mock_cmd_mgr = test_context.Mock() |
| 595 |
essential_modules["orca.command_manager"].get_manager.return_value = mock_cmd_mgr |
| 596 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 597 |
|
| 598 |
navigator = CaretNavigator() |
| 599 |
|
| 600 |
result = navigator.set_is_enabled(True) |
| 601 |
assert result is True |
| 602 |
assert navigator.get_is_enabled() is True |
| 603 |
assert navigator._last_input_event is None |
| 604 |
mock_cmd_mgr.set_group_enabled.assert_called_once() |
| 605 |
|
| 606 |
def test_set_is_enabled_no_active_script(self, test_context: OrcaTestContext) -> None: |
| 607 |
"""Test set_is_enabled updates state even with no active script.""" |
| 608 |
|
| 609 |
essential_modules = self._setup_dependencies(test_context) |
| 610 |
from orca import gsettings_registry |
| 611 |
|
| 612 |
gsettings_registry.get_registry().set_runtime_value("caret-navigation", "enabled", False) |
| 613 |
essential_modules[ |
| 614 |
"orca.script_manager" |
| 615 |
].get_manager.return_value.get_active_script.return_value = None |
| 616 |
mock_cmd_mgr = test_context.Mock() |
| 617 |
essential_modules["orca.command_manager"].get_manager.return_value = mock_cmd_mgr |
| 618 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 619 |
|
| 620 |
navigator = CaretNavigator() |
| 621 |
|
| 622 |
result = navigator.set_is_enabled(True) |
| 623 |
assert result is True |
| 624 |
mock_cmd_mgr.set_group_enabled.assert_called_once() |
| 625 |
|
| 626 |
def test_get_triggers_focus_mode(self, test_context: OrcaTestContext) -> None: |
| 627 |
"""Test get_triggers_focus_mode returns setting value.""" |
| 628 |
|
| 629 |
self._setup_dependencies(test_context) |
| 630 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 631 |
|
| 632 |
navigator = CaretNavigator() |
| 633 |
result = navigator.get_triggers_focus_mode() |
| 634 |
assert result is False |
| 635 |
|
| 636 |
def test_set_triggers_focus_mode(self, test_context: OrcaTestContext) -> None: |
| 637 |
"""Test set_triggers_focus_mode updates setting.""" |
| 638 |
|
| 639 |
self._setup_dependencies(test_context) |
| 640 |
from orca import gsettings_registry |
| 641 |
|
| 642 |
gsettings_registry.get_registry().set_runtime_value( |
| 643 |
"caret-navigation", |
| 644 |
"triggers-focus-mode", |
| 645 |
True, |
| 646 |
) |
| 647 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 648 |
|
| 649 |
navigator = CaretNavigator() |
| 650 |
result = navigator.set_triggers_focus_mode(False) |
| 651 |
assert result is True |
| 652 |
assert navigator.get_triggers_focus_mode() is False |
| 653 |
|
| 654 |
def test_set_triggers_focus_mode_no_change(self, test_context: OrcaTestContext) -> None: |
| 655 |
"""Test set_triggers_focus_mode returns early if unchanged.""" |
| 656 |
|
| 657 |
self._setup_dependencies(test_context) |
| 658 |
from orca import gsettings_registry |
| 659 |
|
| 660 |
gsettings_registry.get_registry().set_runtime_value( |
| 661 |
"caret-navigation", |
| 662 |
"triggers-focus-mode", |
| 663 |
True, |
| 664 |
) |
| 665 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 666 |
|
| 667 |
navigator = CaretNavigator() |
| 668 |
result = navigator.set_triggers_focus_mode(True) |
| 669 |
assert result is True |
| 670 |
# set_setting no longer used - settings are set directly |
| 671 |
|
| 672 |
def test_get_enabled_for_script(self, test_context: OrcaTestContext) -> None: |
| 673 |
"""Test get_enabled_for_script returns script-specific state.""" |
| 674 |
|
| 675 |
self._setup_dependencies(test_context) |
| 676 |
mock_script = test_context.Mock() |
| 677 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 678 |
|
| 679 |
navigator = CaretNavigator() |
| 680 |
navigator._enabled_for_script[mock_script] = True |
| 681 |
result = navigator.get_enabled_for_script(mock_script) |
| 682 |
assert result is True |
| 683 |
|
| 684 |
def test_get_enabled_for_script_default(self, test_context: OrcaTestContext) -> None: |
| 685 |
"""Test get_enabled_for_script returns False by default.""" |
| 686 |
|
| 687 |
self._setup_dependencies(test_context) |
| 688 |
mock_script = test_context.Mock() |
| 689 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 690 |
|
| 691 |
navigator = CaretNavigator() |
| 692 |
result = navigator.get_enabled_for_script(mock_script) |
| 693 |
assert result is False |
| 694 |
|
| 695 |
def test_set_enabled_for_script(self, test_context: OrcaTestContext) -> None: |
| 696 |
"""Test set_enabled_for_script updates script-specific state and calls set_is_enabled.""" |
| 697 |
|
| 698 |
essential_modules = self._setup_dependencies(test_context) |
| 699 |
mock_script = test_context.Mock() |
| 700 |
from orca import gsettings_registry |
| 701 |
|
| 702 |
gsettings_registry.get_registry().set_runtime_value("caret-navigation", "enabled", False) |
| 703 |
essential_modules[ |
| 704 |
"orca.script_manager" |
| 705 |
].get_manager.return_value.get_active_script.return_value = mock_script |
| 706 |
mock_cmd_mgr = test_context.Mock() |
| 707 |
essential_modules["orca.command_manager"].get_manager.return_value = mock_cmd_mgr |
| 708 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 709 |
|
| 710 |
navigator = CaretNavigator() |
| 711 |
test_context.patch_object(navigator, "_is_active_script", return_value=True) |
| 712 |
|
| 713 |
navigator.set_enabled_for_script(mock_script, True) |
| 714 |
assert navigator._enabled_for_script[mock_script] is True |
| 715 |
mock_cmd_mgr.set_group_enabled.assert_called_once() |
| 716 |
|
| 717 |
def test_set_enabled_for_script_inactive_script(self, test_context: OrcaTestContext) -> None: |
| 718 |
"""Test set_enabled_for_script doesn't call set_group_enabled for inactive script.""" |
| 719 |
|
| 720 |
essential_modules = self._setup_dependencies(test_context) |
| 721 |
mock_script = test_context.Mock() |
| 722 |
mock_cmd_mgr = test_context.Mock() |
| 723 |
essential_modules["orca.command_manager"].get_manager.return_value = mock_cmd_mgr |
| 724 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 725 |
|
| 726 |
navigator = CaretNavigator() |
| 727 |
test_context.patch_object(navigator, "_is_active_script", return_value=False) |
| 728 |
|
| 729 |
navigator.set_enabled_for_script(mock_script, True) |
| 730 |
assert navigator._enabled_for_script[mock_script] is True |
| 731 |
mock_cmd_mgr.set_group_enabled.assert_not_called() |
| 732 |
|
| 733 |
def test_set_enabled_for_script_always_calls_set_group_enabled( |
| 734 |
self, |
| 735 |
test_context: OrcaTestContext, |
| 736 |
) -> None: |
| 737 |
"""Test set_enabled_for_script always calls set_group_enabled even if setting matches. |
| 738 |
|
| 739 |
This is a regression test for issue #655 where caret navigation commands |
| 740 |
were not being enabled because set_is_enabled() would early-return when |
| 741 |
the setting already matched the desired value. |
| 742 |
""" |
| 743 |
|
| 744 |
essential_modules = self._setup_dependencies(test_context) |
| 745 |
mock_script = test_context.Mock() |
| 746 |
essential_modules[ |
| 747 |
"orca.script_manager" |
| 748 |
].get_manager.return_value.get_active_script.return_value = mock_script |
| 749 |
mock_cmd_mgr = test_context.Mock() |
| 750 |
essential_modules["orca.command_manager"].get_manager.return_value = mock_cmd_mgr |
| 751 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 752 |
|
| 753 |
navigator = CaretNavigator() |
| 754 |
test_context.patch_object(navigator, "_is_active_script", return_value=True) |
| 755 |
|
| 756 |
navigator.set_enabled_for_script(mock_script, True) |
| 757 |
assert navigator._enabled_for_script[mock_script] is True |
| 758 |
mock_cmd_mgr.set_group_enabled.assert_called_once() |
| 759 |
|
| 760 |
def test_last_command_prevents_focus_mode_true(self, test_context: OrcaTestContext) -> None: |
| 761 |
"""Test last_command_prevents_focus_mode returns True.""" |
| 762 |
|
| 763 |
self._setup_dependencies(test_context) |
| 764 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 765 |
|
| 766 |
navigator = CaretNavigator() |
| 767 |
mock_event = test_context.Mock() |
| 768 |
navigator._last_input_event = mock_event |
| 769 |
test_context.patch_object( |
| 770 |
navigator, |
| 771 |
"last_input_event_was_navigation_command", |
| 772 |
return_value=True, |
| 773 |
) |
| 774 |
result = navigator.last_command_prevents_focus_mode() |
| 775 |
assert result is True |
| 776 |
|
| 777 |
def test_last_command_prevents_focus_mode_false_no_event( |
| 778 |
self, |
| 779 |
test_context: OrcaTestContext, |
| 780 |
) -> None: |
| 781 |
"""Test last_command_prevents_focus_mode returns False if no event.""" |
| 782 |
|
| 783 |
self._setup_dependencies(test_context) |
| 784 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 785 |
|
| 786 |
navigator = CaretNavigator() |
| 787 |
navigator._last_input_event = None |
| 788 |
result = navigator.last_command_prevents_focus_mode() |
| 789 |
assert result is False |
| 790 |
|
| 791 |
def test_last_command_prevents_focus_mode_false_setting_true( |
| 792 |
self, |
| 793 |
test_context: OrcaTestContext, |
| 794 |
) -> None: |
| 795 |
"""Test last_command_prevents_focus_mode returns False if setting True.""" |
| 796 |
|
| 797 |
self._setup_dependencies(test_context) |
| 798 |
from orca.caret_navigator import CaretNavigator # pylint: disable=import-outside-toplevel |
| 799 |
|
| 800 |
navigator = CaretNavigator() |
| 801 |
navigator.set_triggers_focus_mode(True) |
| 802 |
mock_event = test_context.Mock() |
| 803 |
navigator._last_input_event = mock_event |
| 804 |
test_context.patch_object( |
| 805 |
navigator, |
| 806 |
"last_input_event_was_navigation_command", |
| 807 |
return_value=True, |
| 808 |
) |
| 809 |
result = navigator.last_command_prevents_focus_mode() |
| 810 |
assert result is False |
| 811 |
|
| 812 |
def test_successful_navigation_emits_region_changed( |
| 813 |
self, |
| 814 |
test_context: OrcaTestContext, |
| 815 |
) -> None: |
| 816 |
"""Test successful caret navigation emits region_changed with CARET_NAVIGATOR mode.""" |
| 817 |
|
| 818 |
essential_modules = self._setup_dependencies(test_context) |
| 819 |
from orca import focus_manager |
| 820 |
from orca.caret_navigator import CaretNavigator |
| 821 |
|
| 822 |
ax_object_mock = essential_modules["orca.ax_object"] |
| 823 |
ax_object_mock.AXObject.supports_text.side_effect = lambda obj: obj is not None |
| 824 |
ax_object_mock.AXObject.is_valid.side_effect = lambda obj: obj is not None |
| 825 |
ax_object_mock.AXObject.is_ancestor.side_effect = ( |
| 826 |
lambda obj, root, same: obj is not None and root is not None |
| 827 |
) |
| 828 |
|
| 829 |
focus_manager_mock = essential_modules["orca.focus_manager"] |
| 830 |
manager_instance = test_context.Mock() |
| 831 |
focus_manager_mock.get_manager.return_value = manager_instance |
| 832 |
focus_manager_mock.CARET_NAVIGATOR = focus_manager.CARET_NAVIGATOR |
| 833 |
|
| 834 |
navigator = CaretNavigator() |
| 835 |
test_context.patch_object(navigator, "_get_root_object", return_value=None) |
| 836 |
mock_script = test_context.Mock() |
| 837 |
mock_event = test_context.Mock() |
| 838 |
mock_obj = test_context.Mock() |
| 839 |
|
| 840 |
mock_script.utilities.next_context.return_value = (mock_obj, 10) |
| 841 |
|
| 842 |
result = navigator.next_character(mock_script, mock_event) |
| 843 |
|
| 844 |
assert result is True |
| 845 |
manager_instance.emit_region_changed.assert_called() |
| 846 |
call_kwargs = manager_instance.emit_region_changed.call_args |
| 847 |
assert call_kwargs.kwargs.get("mode") == focus_manager.CARET_NAVIGATOR |