| 1 |
# Unit tests for say_all_presenter.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 say_all_presenter.py methods.""" |
| 31 |
|
| 32 |
from __future__ import annotations |
| 33 |
|
| 34 |
from typing import TYPE_CHECKING, Any |
| 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 .orca_test_context import OrcaTestContext |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.unit |
| 47 |
class TestSayAllPresenter: |
| 48 |
"""Test SayAllPresenter class methods.""" |
| 49 |
|
| 50 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, Any]: |
| 51 |
"""Set up mocks for say_all_presenter dependencies.""" |
| 52 |
|
| 53 |
additional_modules = [ |
| 54 |
"orca.ax_event_synthesizer", |
| 55 |
"orca.structural_navigator", |
| 56 |
"orca.input_event", |
| 57 |
"orca.keybindings", |
| 58 |
"orca.cmdnames", |
| 59 |
"orca.guilabels", |
| 60 |
"orca.ax_object", |
| 61 |
"orca.ax_text", |
| 62 |
"orca.ax_utilities", |
| 63 |
"orca.messages", |
| 64 |
"orca.input_event_manager", |
| 65 |
"orca.object_properties", |
| 66 |
"orca.orca_gui_navlist", |
| 67 |
"orca.orca_i18n", |
| 68 |
"orca.AXHypertext", |
| 69 |
"orca.AXObject", |
| 70 |
"orca.speech_presenter", |
| 71 |
"orca.AXTable", |
| 72 |
"orca.AXText", |
| 73 |
"orca.AXUtilities", |
| 74 |
"orca.braille_presenter", |
| 75 |
"orca.presentation_manager", |
| 76 |
] |
| 77 |
essential_modules = test_context.setup_shared_dependencies(additional_modules) |
| 78 |
|
| 79 |
# Set up cmdnames with all required values for structural_navigator |
| 80 |
cmdnames = essential_modules["orca.cmdnames"] |
| 81 |
cmdnames.STRUCTURAL_NAVIGATION_MODE_CYCLE = "cycle_mode" |
| 82 |
cmdnames.BLOCKQUOTE_PREV = "previous_blockquote" |
| 83 |
cmdnames.BLOCKQUOTE_NEXT = "next_blockquote" |
| 84 |
cmdnames.BLOCKQUOTE_LIST = "list_blockquotes" |
| 85 |
cmdnames.BUTTON_PREV = "previous_button" |
| 86 |
cmdnames.BUTTON_NEXT = "next_button" |
| 87 |
cmdnames.BUTTON_LIST = "list_buttons" |
| 88 |
cmdnames.CHECK_BOX_PREV = "previous_checkbox" |
| 89 |
cmdnames.CHECK_BOX_NEXT = "next_checkbox" |
| 90 |
cmdnames.CHECK_BOX_LIST = "list_checkboxes" |
| 91 |
cmdnames.COMBO_BOX_PREV = "previous_combobox" |
| 92 |
cmdnames.COMBO_BOX_NEXT = "next_combobox" |
| 93 |
cmdnames.COMBO_BOX_LIST = "list_comboboxes" |
| 94 |
cmdnames.ENTRY_PREV = "previous_entry" |
| 95 |
cmdnames.ENTRY_NEXT = "next_entry" |
| 96 |
cmdnames.ENTRY_LIST = "list_entries" |
| 97 |
cmdnames.FORM_FIELD_PREV = "previous_form_field" |
| 98 |
cmdnames.FORM_FIELD_NEXT = "next_form_field" |
| 99 |
cmdnames.FORM_FIELD_LIST = "list_form_fields" |
| 100 |
cmdnames.HEADING_PREV = "previous_heading" |
| 101 |
cmdnames.HEADING_NEXT = "next_heading" |
| 102 |
cmdnames.HEADING_LIST = "list_headings" |
| 103 |
cmdnames.HEADING_AT_LEVEL_PREV = "previous_heading_level_%d" |
| 104 |
cmdnames.HEADING_AT_LEVEL_NEXT = "next_heading_level_%d" |
| 105 |
cmdnames.HEADING_AT_LEVEL_LIST = "list_headings_level_%d" |
| 106 |
cmdnames.IFRAME_PREV = "previous_iframe" |
| 107 |
cmdnames.IFRAME_NEXT = "next_iframe" |
| 108 |
cmdnames.IFRAME_LIST = "list_iframes" |
| 109 |
cmdnames.IMAGE_PREV = "previous_image" |
| 110 |
cmdnames.IMAGE_NEXT = "next_image" |
| 111 |
cmdnames.IMAGE_LIST = "list_images" |
| 112 |
cmdnames.LANDMARK_PREV = "previous_landmark" |
| 113 |
cmdnames.LANDMARK_NEXT = "next_landmark" |
| 114 |
cmdnames.LANDMARK_LIST = "list_landmarks" |
| 115 |
cmdnames.LIST_PREV = "previous_list" |
| 116 |
cmdnames.LIST_NEXT = "next_list" |
| 117 |
cmdnames.LIST_LIST = "list_lists" |
| 118 |
cmdnames.LIST_ITEM_PREV = "previous_list_item" |
| 119 |
cmdnames.LIST_ITEM_NEXT = "next_list_item" |
| 120 |
cmdnames.LIST_ITEM_LIST = "list_list_items" |
| 121 |
cmdnames.LIVE_REGION_PREV = "previous_live_region" |
| 122 |
cmdnames.LIVE_REGION_NEXT = "next_live_region" |
| 123 |
cmdnames.LIVE_REGION_LAST = "last_live_region" |
| 124 |
cmdnames.PARAGRAPH_PREV = "previous_paragraph" |
| 125 |
cmdnames.PARAGRAPH_NEXT = "next_paragraph" |
| 126 |
cmdnames.PARAGRAPH_LIST = "list_paragraphs" |
| 127 |
cmdnames.RADIO_BUTTON_PREV = "previous_radio_button" |
| 128 |
cmdnames.RADIO_BUTTON_NEXT = "next_radio_button" |
| 129 |
cmdnames.RADIO_BUTTON_LIST = "list_radio_buttons" |
| 130 |
cmdnames.SEPARATOR_PREV = "previous_separator" |
| 131 |
cmdnames.SEPARATOR_NEXT = "next_separator" |
| 132 |
cmdnames.TABLE_PREV = "previous_table" |
| 133 |
cmdnames.TABLE_NEXT = "next_table" |
| 134 |
cmdnames.TABLE_LIST = "list_tables" |
| 135 |
cmdnames.UNVISITED_LINK_PREV = "previous_unvisited_link" |
| 136 |
cmdnames.UNVISITED_LINK_NEXT = "next_unvisited_link" |
| 137 |
cmdnames.UNVISITED_LINK_LIST = "list_unvisited_links" |
| 138 |
cmdnames.VISITED_LINK_PREV = "previous_visited_link" |
| 139 |
cmdnames.VISITED_LINK_NEXT = "next_visited_link" |
| 140 |
cmdnames.VISITED_LINK_LIST = "list_visited_links" |
| 141 |
cmdnames.LINK_PREV = "previous_link" |
| 142 |
cmdnames.LINK_NEXT = "next_link" |
| 143 |
cmdnames.LINK_LIST = "list_links" |
| 144 |
cmdnames.CLICKABLE_PREV = "previous_clickable" |
| 145 |
cmdnames.CLICKABLE_NEXT = "next_clickable" |
| 146 |
cmdnames.CLICKABLE_LIST = "list_clickables" |
| 147 |
cmdnames.LARGE_OBJECT_PREV = "previous_large_object" |
| 148 |
cmdnames.LARGE_OBJECT_NEXT = "next_large_object" |
| 149 |
cmdnames.LARGE_OBJECT_LIST = "list_large_objects" |
| 150 |
cmdnames.CONTAINER_START = "container_start" |
| 151 |
cmdnames.CONTAINER_END = "container_end" |
| 152 |
|
| 153 |
essential_modules["orca.orca_i18n"]._ = lambda x: x |
| 154 |
essential_modules["orca.debug"].print_message = test_context.Mock() |
| 155 |
essential_modules["orca.debug"].LEVEL_INFO = 800 |
| 156 |
|
| 157 |
controller_mock = test_context.Mock() |
| 158 |
controller_mock.register_decorated_module.return_value = None |
| 159 |
essential_modules["orca.dbus_service"].get_remote_controller.return_value = controller_mock |
| 160 |
|
| 161 |
focus_manager_instance = test_context.Mock() |
| 162 |
focus_manager_instance.get_locus_of_focus.return_value = None |
| 163 |
essential_modules["orca.focus_manager"].get_manager.return_value = focus_manager_instance |
| 164 |
|
| 165 |
essential_modules["orca.AXObject"].supports_collection.return_value = True |
| 166 |
essential_modules["orca.AXUtilities"].is_heading.return_value = False |
| 167 |
|
| 168 |
from orca import gsettings_registry |
| 169 |
|
| 170 |
registry = gsettings_registry.get_registry() |
| 171 |
registry.clear_runtime_values() |
| 172 |
|
| 173 |
return essential_modules |
| 174 |
|
| 175 |
def test_say_all_should_skip_content(self, test_context: OrcaTestContext) -> None: |
| 176 |
"""Test SayAllPresenter._say_all_should_skip_content empty content handling.""" |
| 177 |
|
| 178 |
self._setup_dependencies(test_context) |
| 179 |
from orca.say_all_presenter import SayAllPresenter |
| 180 |
|
| 181 |
presenter = SayAllPresenter() |
| 182 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 183 |
|
| 184 |
content = (mock_obj, 0, 0, "test text") |
| 185 |
should_skip, reason = presenter._say_all_should_skip_content(content, []) |
| 186 |
assert should_skip is True |
| 187 |
assert reason == "start_offset equals end_offset" |
| 188 |
|
| 189 |
def test_parse_utterances(self, test_context: OrcaTestContext) -> None: |
| 190 |
"""Test SayAllPresenter._parse_utterances with various input formats.""" |
| 191 |
|
| 192 |
self._setup_dependencies(test_context) |
| 193 |
from orca import speech |
| 194 |
from orca.say_all_presenter import SayAllPresenter |
| 195 |
|
| 196 |
presenter = SayAllPresenter() |
| 197 |
|
| 198 |
mock_acss = test_context.Mock(spec=speech.ACSS) |
| 199 |
|
| 200 |
elements, voices = presenter._parse_utterances([]) |
| 201 |
assert len(elements) == 0 |
| 202 |
assert len(voices) == 0 |
| 203 |
|
| 204 |
elements, voices = presenter._parse_utterances(["Hello world"]) |
| 205 |
assert len(elements) == 1 |
| 206 |
assert elements[0] == "Hello world" |
| 207 |
|
| 208 |
elements, voices = presenter._parse_utterances([["Hello"], ["world"]]) |
| 209 |
assert len(elements) == 2 |
| 210 |
assert elements[0] == "Hello" |
| 211 |
assert elements[1] == "world" |
| 212 |
|
| 213 |
elements, voices = presenter._parse_utterances(["Hello", mock_acss]) |
| 214 |
assert len(elements) == 1 |
| 215 |
assert len(voices) == 1 |
| 216 |
assert elements[0] == "Hello" |
| 217 |
assert voices[0] == mock_acss |
| 218 |
|
| 219 |
def test_get_presenter_singleton(self, test_context: OrcaTestContext) -> None: |
| 220 |
"""Test that get_presenter returns a singleton instance.""" |
| 221 |
|
| 222 |
self._setup_dependencies(test_context) |
| 223 |
from orca.say_all_presenter import get_presenter |
| 224 |
|
| 225 |
presenter1 = get_presenter() |
| 226 |
presenter2 = get_presenter() |
| 227 |
assert presenter1 is presenter2 |
| 228 |
assert presenter1 is not None |
| 229 |
|
| 230 |
def test_say_all_presenter_initialization(self, test_context: OrcaTestContext) -> None: |
| 231 |
"""Test SayAllPresenter initialization sets up required attributes.""" |
| 232 |
|
| 233 |
self._setup_dependencies(test_context) |
| 234 |
from orca import command_manager |
| 235 |
from orca.say_all_presenter import SayAllPresenter |
| 236 |
|
| 237 |
presenter = SayAllPresenter() |
| 238 |
assert presenter is not None |
| 239 |
|
| 240 |
# Verify commands are registered after setup |
| 241 |
presenter.set_up_commands() |
| 242 |
cmd_manager = command_manager.get_manager() |
| 243 |
assert cmd_manager.get_keyboard_command("sayAllHandler") is not None |
| 244 |
|
| 245 |
def test_say_all_no_object_scenario(self, test_context: OrcaTestContext) -> None: |
| 246 |
"""Test SayAllPresenter.say_all with no focus object scenario.""" |
| 247 |
|
| 248 |
essential_modules = self._setup_dependencies(test_context) |
| 249 |
from orca.say_all_presenter import SayAllPresenter |
| 250 |
|
| 251 |
presenter = SayAllPresenter() |
| 252 |
mock_script = test_context.Mock() |
| 253 |
|
| 254 |
focus_manager_mock = essential_modules["orca.focus_manager"] |
| 255 |
manager_instance = test_context.Mock() |
| 256 |
focus_manager_mock.get_manager.return_value = manager_instance |
| 257 |
manager_instance.get_locus_of_focus.return_value = None |
| 258 |
|
| 259 |
messages_mock = essential_modules["orca.messages"] |
| 260 |
messages_mock.LOCATION_NOT_FOUND_FULL = "Location not found" |
| 261 |
|
| 262 |
pres_manager = essential_modules["orca.presentation_manager"].get_manager() |
| 263 |
pres_manager.present_message.reset_mock() |
| 264 |
pres_manager.interrupt_presentation.reset_mock() |
| 265 |
result = presenter.say_all(mock_script, obj=None) |
| 266 |
assert result is True |
| 267 |
|
| 268 |
pres_manager.interrupt_presentation.assert_called_once() |
| 269 |
pres_manager.present_message.assert_called_once_with("Location not found") |
| 270 |
|
| 271 |
@pytest.mark.parametrize( |
| 272 |
"direction,enabled,contents_available,obj_valid,expected_result", |
| 273 |
[ |
| 274 |
pytest.param("rewind", False, True, True, False, id="rewind_disabled"), |
| 275 |
pytest.param("rewind", True, False, True, True, id="rewind_no_contents_valid_context"), |
| 276 |
pytest.param( |
| 277 |
"rewind", |
| 278 |
True, |
| 279 |
False, |
| 280 |
False, |
| 281 |
False, |
| 282 |
id="rewind_no_contents_invalid_context_obj", |
| 283 |
), |
| 284 |
pytest.param("rewind", True, True, True, True, id="rewind_success"), |
| 285 |
pytest.param("fast_forward", False, True, True, False, id="fast_forward_disabled"), |
| 286 |
pytest.param( |
| 287 |
"fast_forward", |
| 288 |
True, |
| 289 |
False, |
| 290 |
True, |
| 291 |
True, |
| 292 |
id="fast_forward_no_contents_valid_context", |
| 293 |
), |
| 294 |
pytest.param( |
| 295 |
"fast_forward", |
| 296 |
True, |
| 297 |
False, |
| 298 |
False, |
| 299 |
False, |
| 300 |
id="fast_forward_no_contents_invalid_context_obj", |
| 301 |
), |
| 302 |
pytest.param("fast_forward", True, True, True, True, id="fast_forward_success"), |
| 303 |
], |
| 304 |
) |
| 305 |
def test_navigation_controls( |
| 306 |
self, |
| 307 |
test_context: OrcaTestContext, |
| 308 |
direction: str, |
| 309 |
enabled: bool, |
| 310 |
contents_available: bool, |
| 311 |
obj_valid: bool, |
| 312 |
expected_result: bool, |
| 313 |
) -> None: |
| 314 |
"""Test _rewind and _fast_forward navigation controls with various conditions.""" |
| 315 |
|
| 316 |
essential_modules = self._setup_dependencies(test_context) |
| 317 |
from orca import speechserver |
| 318 |
from orca.say_all_presenter import SayAllPresenter |
| 319 |
|
| 320 |
presenter = SayAllPresenter() |
| 321 |
mock_script = test_context.Mock() |
| 322 |
presenter._script = mock_script |
| 323 |
|
| 324 |
presenter.set_rewind_and_fast_forward_enabled(enabled) |
| 325 |
|
| 326 |
mock_context = test_context.Mock(spec=speechserver.SayAllContext) |
| 327 |
mock_context.obj = "context_obj" if obj_valid else None |
| 328 |
|
| 329 |
if direction == "rewind": |
| 330 |
mock_context.start_offset = 15 |
| 331 |
else: |
| 332 |
mock_context.end_offset = 25 |
| 333 |
|
| 334 |
if contents_available: |
| 335 |
if direction == "rewind": |
| 336 |
presenter._contents = [("content_obj", 5, 10, "text")] |
| 337 |
else: |
| 338 |
presenter._contents = [("first_obj", 0, 5, "first"), ("last_obj", 20, 30, "last")] |
| 339 |
else: |
| 340 |
presenter._contents = [] |
| 341 |
|
| 342 |
focus_manager_mock = essential_modules["orca.focus_manager"] |
| 343 |
focus_instance = test_context.Mock() |
| 344 |
focus_manager_mock.get_manager.return_value = focus_instance |
| 345 |
|
| 346 |
mock_script.utilities.set_caret_context = test_context.Mock() |
| 347 |
|
| 348 |
if direction == "rewind": |
| 349 |
mock_script.utilities.previous_context.return_value = ("prev_obj", 8) |
| 350 |
else: |
| 351 |
mock_script.utilities.next_context.return_value = ("next_obj", 35) |
| 352 |
|
| 353 |
presenter.say_all = test_context.Mock(return_value=True) |
| 354 |
navigation_method = getattr(presenter, f"_{direction}") |
| 355 |
result = navigation_method(mock_context) |
| 356 |
assert result == expected_result |
| 357 |
|
| 358 |
if expected_result: |
| 359 |
focus_instance.set_locus_of_focus.assert_called() |
| 360 |
mock_script.utilities.set_caret_context.assert_called() |
| 361 |
if direction == "rewind": |
| 362 |
mock_script.utilities.previous_context.assert_called() |
| 363 |
else: |
| 364 |
mock_script.utilities.next_context.assert_called() |
| 365 |
presenter.say_all.assert_called_once() |
| 366 |
|
| 367 |
@pytest.mark.parametrize( |
| 368 |
"command_method", |
| 369 |
[ |
| 370 |
pytest.param("rewind", id="rewind_command"), |
| 371 |
pytest.param("fast_forward", id="fast_forward_command"), |
| 372 |
], |
| 373 |
) |
| 374 |
def test_dbus_navigation_commands( |
| 375 |
self, |
| 376 |
test_context: OrcaTestContext, |
| 377 |
command_method: str, |
| 378 |
) -> None: |
| 379 |
"""Test D-Bus navigation commands delegate to private methods.""" |
| 380 |
|
| 381 |
essential_modules = self._setup_dependencies(test_context) |
| 382 |
from orca.say_all_presenter import SayAllPresenter |
| 383 |
|
| 384 |
presenter = SayAllPresenter() |
| 385 |
mock_script = test_context.Mock() |
| 386 |
mock_event = test_context.Mock() |
| 387 |
|
| 388 |
debug_mock = essential_modules["orca.debug"] |
| 389 |
debug_mock.LEVEL_INFO = 800 |
| 390 |
debug_mock.print_tokens = test_context.Mock() |
| 391 |
|
| 392 |
private_method_name = f"_{command_method}" |
| 393 |
test_context.patch_object(presenter, private_method_name, return_value=True) |
| 394 |
|
| 395 |
command = getattr(presenter, command_method) |
| 396 |
result = command(mock_script, mock_event, notify_user=True) |
| 397 |
|
| 398 |
assert result is True |
| 399 |
debug_mock.print_tokens.assert_called_once() |
| 400 |
private_method = getattr(presenter, private_method_name) |
| 401 |
private_method.assert_called_once_with(None, True) |
| 402 |
|
| 403 |
@pytest.mark.parametrize( |
| 404 |
"case", |
| 405 |
[ |
| 406 |
{ |
| 407 |
"id": "rewind_setting_disabled_no_override", |
| 408 |
"direction": "rewind", |
| 409 |
"override_setting": False, |
| 410 |
"setting_enabled": False, |
| 411 |
"context_provided": False, |
| 412 |
"expected_result": False, |
| 413 |
}, |
| 414 |
{ |
| 415 |
"id": "rewind_setting_disabled_with_override", |
| 416 |
"direction": "rewind", |
| 417 |
"override_setting": True, |
| 418 |
"setting_enabled": False, |
| 419 |
"context_provided": False, |
| 420 |
"expected_result": True, |
| 421 |
}, |
| 422 |
{ |
| 423 |
"id": "rewind_setting_enabled_no_override", |
| 424 |
"direction": "rewind", |
| 425 |
"override_setting": False, |
| 426 |
"setting_enabled": True, |
| 427 |
"context_provided": False, |
| 428 |
"expected_result": True, |
| 429 |
}, |
| 430 |
{ |
| 431 |
"id": "rewind_setting_enabled_with_override", |
| 432 |
"direction": "rewind", |
| 433 |
"override_setting": True, |
| 434 |
"setting_enabled": True, |
| 435 |
"context_provided": False, |
| 436 |
"expected_result": True, |
| 437 |
}, |
| 438 |
{ |
| 439 |
"id": "rewind_with_provided_context", |
| 440 |
"direction": "rewind", |
| 441 |
"override_setting": False, |
| 442 |
"setting_enabled": True, |
| 443 |
"context_provided": True, |
| 444 |
"expected_result": True, |
| 445 |
}, |
| 446 |
{ |
| 447 |
"id": "fast_forward_setting_disabled_no_override", |
| 448 |
"direction": "fast_forward", |
| 449 |
"override_setting": False, |
| 450 |
"setting_enabled": False, |
| 451 |
"context_provided": False, |
| 452 |
"expected_result": False, |
| 453 |
}, |
| 454 |
{ |
| 455 |
"id": "fast_forward_setting_disabled_with_override", |
| 456 |
"direction": "fast_forward", |
| 457 |
"override_setting": True, |
| 458 |
"setting_enabled": False, |
| 459 |
"context_provided": False, |
| 460 |
"expected_result": True, |
| 461 |
}, |
| 462 |
{ |
| 463 |
"id": "fast_forward_setting_enabled_no_override", |
| 464 |
"direction": "fast_forward", |
| 465 |
"override_setting": False, |
| 466 |
"setting_enabled": True, |
| 467 |
"context_provided": False, |
| 468 |
"expected_result": True, |
| 469 |
}, |
| 470 |
{ |
| 471 |
"id": "fast_forward_setting_enabled_with_override", |
| 472 |
"direction": "fast_forward", |
| 473 |
"override_setting": True, |
| 474 |
"setting_enabled": True, |
| 475 |
"context_provided": False, |
| 476 |
"expected_result": True, |
| 477 |
}, |
| 478 |
{ |
| 479 |
"id": "fast_forward_with_provided_context", |
| 480 |
"direction": "fast_forward", |
| 481 |
"override_setting": False, |
| 482 |
"setting_enabled": True, |
| 483 |
"context_provided": True, |
| 484 |
"expected_result": True, |
| 485 |
}, |
| 486 |
], |
| 487 |
ids=lambda case: case["id"], |
| 488 |
) |
| 489 |
def test_navigation_methods_with_parameters( |
| 490 |
self, |
| 491 |
test_context: OrcaTestContext, |
| 492 |
case: dict, |
| 493 |
) -> None: |
| 494 |
"""Test _rewind and _fast_forward methods with override_setting parameter.""" |
| 495 |
|
| 496 |
direction = case["direction"] |
| 497 |
override_setting = case["override_setting"] |
| 498 |
setting_enabled = case["setting_enabled"] |
| 499 |
context_provided = case["context_provided"] |
| 500 |
expected_result = case["expected_result"] |
| 501 |
|
| 502 |
essential_modules = self._setup_dependencies(test_context) |
| 503 |
from orca import speechserver |
| 504 |
from orca.say_all_presenter import SayAllPresenter |
| 505 |
|
| 506 |
presenter = SayAllPresenter() |
| 507 |
mock_script = test_context.Mock() |
| 508 |
presenter._script = mock_script |
| 509 |
|
| 510 |
presenter.set_rewind_and_fast_forward_enabled(setting_enabled) |
| 511 |
|
| 512 |
mock_context = ( |
| 513 |
test_context.Mock(spec=speechserver.SayAllContext) if context_provided else None |
| 514 |
) |
| 515 |
if context_provided and mock_context is not None: |
| 516 |
mock_context.obj = "provided_obj" |
| 517 |
if direction == "rewind": |
| 518 |
mock_context.start_offset = 10 |
| 519 |
else: |
| 520 |
mock_context.end_offset = 20 |
| 521 |
|
| 522 |
current_context = test_context.Mock(spec=speechserver.SayAllContext) |
| 523 |
current_context.obj = "current_obj" |
| 524 |
if direction == "rewind": |
| 525 |
current_context.start_offset = 5 |
| 526 |
else: |
| 527 |
current_context.end_offset = 15 |
| 528 |
presenter._current_context = current_context |
| 529 |
|
| 530 |
if direction == "rewind": |
| 531 |
presenter._contents = [("content_obj", 0, 10, "text")] |
| 532 |
else: |
| 533 |
presenter._contents = [("first_obj", 0, 5, "first"), ("last_obj", 10, 20, "last")] |
| 534 |
|
| 535 |
focus_manager_mock = essential_modules["orca.focus_manager"] |
| 536 |
focus_instance = test_context.Mock() |
| 537 |
focus_manager_mock.get_manager.return_value = focus_instance |
| 538 |
|
| 539 |
mock_script.utilities.set_caret_context = test_context.Mock() |
| 540 |
if direction == "rewind": |
| 541 |
mock_script.utilities.previous_context.return_value = ("prev_obj", 3) |
| 542 |
else: |
| 543 |
mock_script.utilities.next_context.return_value = ("next_obj", 25) |
| 544 |
|
| 545 |
presenter.say_all = test_context.Mock(return_value=True) |
| 546 |
|
| 547 |
navigation_method = getattr(presenter, f"_{direction}") |
| 548 |
result = navigation_method(mock_context, override_setting) |
| 549 |
|
| 550 |
assert result == expected_result |
| 551 |
|
| 552 |
if expected_result: |
| 553 |
focus_instance.set_locus_of_focus.assert_called() |
| 554 |
mock_script.utilities.set_caret_context.assert_called() |
| 555 |
presenter.say_all.assert_called_once() |
| 556 |
|
| 557 |
def test_say_all_initialization_clears_state(self, test_context: OrcaTestContext) -> None: |
| 558 |
"""Test say_all method clears contexts, contents, and current_context at start.""" |
| 559 |
|
| 560 |
essential_modules = self._setup_dependencies(test_context) |
| 561 |
from orca import speechserver |
| 562 |
from orca.say_all_presenter import SayAllPresenter |
| 563 |
|
| 564 |
presenter = SayAllPresenter() |
| 565 |
mock_script = test_context.Mock() |
| 566 |
|
| 567 |
presenter._contexts = [test_context.Mock(spec=speechserver.SayAllContext)] |
| 568 |
presenter._contents = [("old_obj", 0, 5, "old")] |
| 569 |
presenter._current_context = test_context.Mock(spec=speechserver.SayAllContext) |
| 570 |
|
| 571 |
focus_manager_mock = essential_modules["orca.focus_manager"] |
| 572 |
manager_instance = test_context.Mock() |
| 573 |
focus_manager_mock.get_manager.return_value = manager_instance |
| 574 |
manager_instance.get_locus_of_focus.return_value = "focus_obj" |
| 575 |
|
| 576 |
debug_mock = essential_modules["orca.debug"] |
| 577 |
debug_mock.LEVEL_INFO = 800 |
| 578 |
debug_mock.print_tokens = test_context.Mock() |
| 579 |
|
| 580 |
from orca import speech |
| 581 |
|
| 582 |
test_context.patch_object(speech, "say_all", return_value=None) |
| 583 |
|
| 584 |
mock_script.utilities.get_caret_context.return_value = ("obj", 10) |
| 585 |
|
| 586 |
result = presenter.say_all(mock_script, None) |
| 587 |
assert result is True |
| 588 |
|
| 589 |
assert not presenter._contexts |
| 590 |
assert not presenter._contents |
| 591 |
assert presenter._current_context is None |
| 592 |
|
| 593 |
def test_progress_callback_sets_current_context(self, test_context: OrcaTestContext) -> None: |
| 594 |
"""Test that _progress_callback sets the current context.""" |
| 595 |
|
| 596 |
self._setup_dependencies(test_context) |
| 597 |
from orca import speechserver |
| 598 |
from orca.say_all_presenter import SayAllPresenter |
| 599 |
|
| 600 |
presenter = SayAllPresenter() |
| 601 |
|
| 602 |
mock_context = test_context.Mock(spec=speechserver.SayAllContext) |
| 603 |
assert presenter._current_context is None |
| 604 |
|
| 605 |
presenter._current_context = mock_context |
| 606 |
assert presenter._current_context is mock_context |
| 607 |
|
| 608 |
def test_say_all_is_running_initialized_to_false(self, test_context: OrcaTestContext) -> None: |
| 609 |
"""Test that _say_all_is_running is initialized to False.""" |
| 610 |
|
| 611 |
self._setup_dependencies(test_context) |
| 612 |
from orca.say_all_presenter import SayAllPresenter |
| 613 |
|
| 614 |
presenter = SayAllPresenter() |
| 615 |
assert presenter._say_all_is_running is False |
| 616 |
|
| 617 |
def test_say_all_clears_say_all_is_running(self, test_context: OrcaTestContext) -> None: |
| 618 |
"""Test that say_all resets _say_all_is_running to False.""" |
| 619 |
|
| 620 |
essential_modules = self._setup_dependencies(test_context) |
| 621 |
from orca.say_all_presenter import SayAllPresenter |
| 622 |
|
| 623 |
presenter = SayAllPresenter() |
| 624 |
mock_script = test_context.Mock() |
| 625 |
|
| 626 |
presenter._say_all_is_running = True |
| 627 |
|
| 628 |
focus_manager_mock = essential_modules["orca.focus_manager"] |
| 629 |
manager_instance = test_context.Mock() |
| 630 |
focus_manager_mock.get_manager.return_value = manager_instance |
| 631 |
manager_instance.get_locus_of_focus.return_value = None |
| 632 |
|
| 633 |
messages_mock = essential_modules["orca.messages"] |
| 634 |
messages_mock.LOCATION_NOT_FOUND_FULL = "Location not found" |
| 635 |
|
| 636 |
presenter.say_all(mock_script) |
| 637 |
assert presenter._say_all_is_running is False |
| 638 |
|
| 639 |
def test_progress_callback_sets_say_all_is_running_true( |
| 640 |
self, |
| 641 |
test_context: OrcaTestContext, |
| 642 |
) -> None: |
| 643 |
"""Test that _progress_callback sets _say_all_is_running to True.""" |
| 644 |
|
| 645 |
essential_modules = self._setup_dependencies(test_context) |
| 646 |
from orca import speechserver |
| 647 |
from orca.say_all_presenter import SayAllPresenter |
| 648 |
|
| 649 |
presenter = SayAllPresenter() |
| 650 |
mock_script = test_context.Mock() |
| 651 |
presenter._script = mock_script |
| 652 |
|
| 653 |
mock_context = test_context.Mock(spec=speechserver.SayAllContext) |
| 654 |
mock_context.obj = test_context.Mock() |
| 655 |
mock_context.current_offset = 5 |
| 656 |
mock_context.current_end_offset = 10 |
| 657 |
|
| 658 |
from orca.ax_utilities import AXUtilities |
| 659 |
|
| 660 |
test_context.patch_object(AXUtilities, "character_at_offset_is_eoc", return_value=False) |
| 661 |
|
| 662 |
focus_manager_mock = essential_modules["orca.focus_manager"] |
| 663 |
focus_instance = test_context.Mock() |
| 664 |
focus_manager_mock.get_manager.return_value = focus_instance |
| 665 |
focus_manager_mock.SAY_ALL = "say-all" |
| 666 |
|
| 667 |
assert presenter._say_all_is_running is False |
| 668 |
|
| 669 |
presenter._progress_callback(mock_context, speechserver.SayAllContext.PROGRESS) |
| 670 |
|
| 671 |
assert presenter._say_all_is_running is True |
| 672 |
|
| 673 |
def test_progress_callback_uses_say_all_mode_when_running( |
| 674 |
self, |
| 675 |
test_context: OrcaTestContext, |
| 676 |
) -> None: |
| 677 |
"""Test that _progress_callback uses SAY_ALL mode when _say_all_is_running is True.""" |
| 678 |
|
| 679 |
essential_modules = self._setup_dependencies(test_context) |
| 680 |
from orca import speechserver |
| 681 |
from orca.say_all_presenter import SayAllPresenter |
| 682 |
|
| 683 |
presenter = SayAllPresenter() |
| 684 |
mock_script = test_context.Mock() |
| 685 |
presenter._script = mock_script |
| 686 |
|
| 687 |
mock_context = test_context.Mock(spec=speechserver.SayAllContext) |
| 688 |
mock_context.obj = test_context.Mock() |
| 689 |
mock_context.current_offset = 5 |
| 690 |
mock_context.current_end_offset = 10 |
| 691 |
|
| 692 |
from orca.ax_utilities import AXUtilities |
| 693 |
|
| 694 |
test_context.patch_object(AXUtilities, "character_at_offset_is_eoc", return_value=False) |
| 695 |
|
| 696 |
focus_manager_mock = essential_modules["orca.focus_manager"] |
| 697 |
focus_instance = test_context.Mock() |
| 698 |
focus_manager_mock.get_manager.return_value = focus_instance |
| 699 |
focus_manager_mock.SAY_ALL = "say-all" |
| 700 |
|
| 701 |
presenter._progress_callback(mock_context, speechserver.SayAllContext.PROGRESS) |
| 702 |
|
| 703 |
focus_instance.emit_region_changed.assert_called_once_with( |
| 704 |
mock_context.obj, |
| 705 |
mock_context.current_offset, |
| 706 |
mock_context.current_end_offset, |
| 707 |
"say-all", |
| 708 |
) |
| 709 |
|
| 710 |
def test_progress_callback_uses_focus_tracking_mode_when_interrupted( |
| 711 |
self, |
| 712 |
test_context: OrcaTestContext, |
| 713 |
) -> None: |
| 714 |
"""Test that _progress_callback uses FOCUS_TRACKING mode when interrupted by keyboard.""" |
| 715 |
|
| 716 |
self._setup_dependencies(test_context) |
| 717 |
from orca import focus_manager as fm |
| 718 |
from orca import input_event_manager, speechserver |
| 719 |
from orca.ax_text import AXText |
| 720 |
from orca.say_all_presenter import SayAllPresenter |
| 721 |
|
| 722 |
presenter = SayAllPresenter() |
| 723 |
mock_script = test_context.Mock() |
| 724 |
presenter._script = mock_script |
| 725 |
|
| 726 |
mock_context = test_context.Mock(spec=speechserver.SayAllContext) |
| 727 |
mock_context.obj = test_context.Mock() |
| 728 |
mock_context.current_offset = 5 |
| 729 |
mock_context.current_end_offset = 10 |
| 730 |
|
| 731 |
from orca.ax_utilities import AXUtilities |
| 732 |
|
| 733 |
test_context.patch_object(AXUtilities, "character_at_offset_is_eoc", return_value=False) |
| 734 |
test_context.patch_object(AXText, "set_caret_offset", return_value=True) |
| 735 |
|
| 736 |
focus_instance = test_context.Mock() |
| 737 |
test_context.patch_object(fm, "get_manager", return_value=focus_instance) |
| 738 |
|
| 739 |
iem_instance = test_context.Mock() |
| 740 |
iem_instance.last_event_was_keyboard.return_value = True |
| 741 |
iem_instance.last_event_was_down.return_value = False |
| 742 |
iem_instance.last_event_was_up.return_value = False |
| 743 |
test_context.patch_object(input_event_manager, "get_manager", return_value=iem_instance) |
| 744 |
|
| 745 |
presenter._progress_callback(mock_context, speechserver.SayAllContext.INTERRUPTED) |
| 746 |
|
| 747 |
assert presenter._say_all_is_running is False |
| 748 |
focus_instance.emit_region_changed.assert_called_once_with( |
| 749 |
mock_context.obj, |
| 750 |
mock_context.current_offset, |
| 751 |
None, |
| 752 |
fm.FOCUS_TRACKING, |
| 753 |
) |
| 754 |
|
| 755 |
@pytest.mark.parametrize( |
| 756 |
"end_offset, expected_next_context_offset", |
| 757 |
[ |
| 758 |
pytest.param(16, 15, id="normal_offset_passes_end_minus_one"), |
| 759 |
pytest.param(1, 0, id="small_offset_passes_end_minus_one"), |
| 760 |
pytest.param(0, 0, id="zero_offset_passes_zero_not_negative"), |
| 761 |
pytest.param(100, 99, id="large_offset_passes_end_minus_one"), |
| 762 |
], |
| 763 |
) |
| 764 |
def test_say_all_iter_next_context_uses_end_offset_minus_one( |
| 765 |
self, |
| 766 |
test_context: OrcaTestContext, |
| 767 |
end_offset: int, |
| 768 |
expected_next_context_offset: int, |
| 769 |
) -> None: |
| 770 |
"""Test that _say_all_iter passes end_offset - 1 to next_context. |
| 771 |
|
| 772 |
The end offset from sentence contents is exclusive (position end is NOT |
| 773 |
part of the content). find_next_caret_in_order looks at offset + 1, so |
| 774 |
passing end directly would skip position end. We must pass end - 1 so |
| 775 |
that find_next_caret_in_order looks at position end, which is where |
| 776 |
embedded object characters (FFFC) representing child elements may be. |
| 777 |
""" |
| 778 |
|
| 779 |
essential_modules = self._setup_dependencies(test_context) |
| 780 |
from orca import gsettings_registry |
| 781 |
from orca.say_all_presenter import SayAllPresenter |
| 782 |
|
| 783 |
presenter = SayAllPresenter() |
| 784 |
mock_script = test_context.Mock() |
| 785 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 786 |
|
| 787 |
# Set up the presenter's script |
| 788 |
presenter._script = mock_script |
| 789 |
|
| 790 |
# Set up settings for sentence-by-sentence say all |
| 791 |
gsettings_registry.get_registry().set_runtime_value("say-all", "style", "sentence") |
| 792 |
|
| 793 |
# Mock utilities - return contents once, then return empty to exit loop |
| 794 |
call_count = [0] |
| 795 |
|
| 796 |
def mock_get_sentence_contents(_obj, _offset): |
| 797 |
call_count[0] += 1 |
| 798 |
if call_count[0] == 1: |
| 799 |
return [(mock_obj, 0, end_offset, "Test sentence.")] |
| 800 |
return [] |
| 801 |
|
| 802 |
mock_script.utilities.get_sentence_contents_at_offset.side_effect = ( |
| 803 |
mock_get_sentence_contents |
| 804 |
) |
| 805 |
mock_script.utilities.filter_contents_for_presentation.side_effect = lambda x: x |
| 806 |
|
| 807 |
# next_context returns None to end the loop |
| 808 |
mock_script.utilities.next_context.return_value = (None, -1) |
| 809 |
|
| 810 |
# Mock speech presenter to return something so the content is processed |
| 811 |
speech_pres = essential_modules["orca.speech_presenter"].get_presenter() |
| 812 |
speech_pres.generate_speech_contents.return_value = [["Test"], []] |
| 813 |
|
| 814 |
# Mock AXUtilities |
| 815 |
ax_utilities_mock = essential_modules["orca.ax_utilities"] |
| 816 |
ax_utilities_mock.is_text.return_value = False |
| 817 |
ax_utilities_mock.is_terminal.return_value = False |
| 818 |
|
| 819 |
# Mock _say_all_should_skip_content to avoid dependency issues |
| 820 |
test_context.patch_object( |
| 821 |
presenter, |
| 822 |
"_say_all_should_skip_content", |
| 823 |
return_value=(False, ""), |
| 824 |
) |
| 825 |
|
| 826 |
# Mock debug |
| 827 |
debug_mock = essential_modules["orca.debug"] |
| 828 |
debug_mock.LEVEL_INFO = 800 |
| 829 |
debug_mock.print_tokens = test_context.Mock() |
| 830 |
debug_mock.print_message = test_context.Mock() |
| 831 |
|
| 832 |
# Mock focus_manager for set_locus_of_focus |
| 833 |
focus_manager_mock = essential_modules["orca.focus_manager"] |
| 834 |
manager_instance = test_context.Mock() |
| 835 |
focus_manager_mock.get_manager.return_value = manager_instance |
| 836 |
|
| 837 |
# Mock event_synthesizer |
| 838 |
essential_modules[ |
| 839 |
"orca.ax_event_synthesizer" |
| 840 |
].get_synthesizer.return_value.scroll_into_view = test_context.Mock() |
| 841 |
|
| 842 |
# Mock utilities.set_caret_offset |
| 843 |
mock_script.utilities.set_caret_offset = test_context.Mock() |
| 844 |
|
| 845 |
# Consume the generator to trigger next_context call |
| 846 |
generator = presenter._say_all_iter(mock_obj, 0) |
| 847 |
for _ in generator: |
| 848 |
pass |
| 849 |
|
| 850 |
# Verify next_context was called with end_offset - 1 (or 0 if that would be negative) |
| 851 |
mock_script.utilities.next_context.assert_called_once() |
| 852 |
call_args = mock_script.utilities.next_context.call_args |
| 853 |
# next_context is called with positional args: (last_obj, offset, restrict_to=...) |
| 854 |
actual_offset = call_args[0][1] |
| 855 |
assert actual_offset == expected_next_context_offset, ( |
| 856 |
f"Expected next_context to be called with offset {expected_next_context_offset}, " |
| 857 |
f"but was called with {actual_offset}" |
| 858 |
) |
| 859 |
|
| 860 |
def test_stop_clears_all_state(self, test_context: OrcaTestContext) -> None: |
| 861 |
"""Test SayAllPresenter.stop clears contexts, contents, current_context and running flag.""" |
| 862 |
|
| 863 |
essential_modules = self._setup_dependencies(test_context) |
| 864 |
from orca import speechserver |
| 865 |
from orca.say_all_presenter import SayAllPresenter |
| 866 |
|
| 867 |
presenter = SayAllPresenter() |
| 868 |
presenter._contexts = [test_context.Mock(spec=speechserver.SayAllContext)] |
| 869 |
presenter._contents = [("obj", 0, 5, "text")] |
| 870 |
presenter._current_context = test_context.Mock(spec=speechserver.SayAllContext) |
| 871 |
presenter._say_all_is_running = True |
| 872 |
|
| 873 |
focus_manager_mock = essential_modules["orca.focus_manager"] |
| 874 |
manager_instance = test_context.Mock() |
| 875 |
focus_manager_mock.get_manager.return_value = manager_instance |
| 876 |
|
| 877 |
presenter.stop() |
| 878 |
|
| 879 |
assert not presenter._contexts |
| 880 |
assert not presenter._contents |
| 881 |
assert presenter._current_context is None |
| 882 |
assert presenter._say_all_is_running is False |
| 883 |
manager_instance.reset_active_mode.assert_called_once_with( |
| 884 |
"SAY ALL PRESENTER: Stopped Say All.", |
| 885 |
) |
| 886 |
|
| 887 |
def test_stop_from_empty_state(self, test_context: OrcaTestContext) -> None: |
| 888 |
"""Test SayAllPresenter.stop works correctly when already in empty state.""" |
| 889 |
|
| 890 |
essential_modules = self._setup_dependencies(test_context) |
| 891 |
from orca.say_all_presenter import SayAllPresenter |
| 892 |
|
| 893 |
presenter = SayAllPresenter() |
| 894 |
assert not presenter._contexts |
| 895 |
assert not presenter._contents |
| 896 |
assert presenter._current_context is None |
| 897 |
assert presenter._say_all_is_running is False |
| 898 |
|
| 899 |
focus_manager_mock = essential_modules["orca.focus_manager"] |
| 900 |
manager_instance = test_context.Mock() |
| 901 |
focus_manager_mock.get_manager.return_value = manager_instance |
| 902 |
|
| 903 |
presenter.stop() |
| 904 |
|
| 905 |
assert not presenter._contexts |
| 906 |
assert not presenter._contents |
| 907 |
assert presenter._current_context is None |
| 908 |
assert presenter._say_all_is_running is False |
| 909 |
manager_instance.reset_active_mode.assert_called_once() |