| 1 |
# Unit tests for object_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-locals |
| 27 |
|
| 28 |
"""Unit tests for object_navigator.py methods.""" |
| 29 |
|
| 30 |
from __future__ import annotations |
| 31 |
|
| 32 |
from typing import TYPE_CHECKING |
| 33 |
|
| 34 |
import pytest |
| 35 |
|
| 36 |
if TYPE_CHECKING: |
| 37 |
from unittest.mock import MagicMock |
| 38 |
|
| 39 |
from .orca_test_context import OrcaTestContext |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.unit |
| 43 |
class TestObjectNavigator: |
| 44 |
"""Test ObjectNavigator class methods.""" |
| 45 |
|
| 46 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 47 |
"""Set up mocks for object_navigator dependencies.""" |
| 48 |
|
| 49 |
import sys |
| 50 |
|
| 51 |
essential_modules = test_context._setup_essential_modules( |
| 52 |
[ |
| 53 |
"gi", |
| 54 |
"gi.repository", |
| 55 |
"gi.repository.Atspi", |
| 56 |
"orca.orca_i18n", |
| 57 |
"orca.guilabels", |
| 58 |
"orca.cmdnames", |
| 59 |
"orca.command_manager", |
| 60 |
"orca.dbus_service", |
| 61 |
"orca.debug", |
| 62 |
"orca.focus_manager", |
| 63 |
"orca.input_event", |
| 64 |
"orca.keybindings", |
| 65 |
"orca.messages", |
| 66 |
"orca.ax_event_synthesizer", |
| 67 |
"orca.ax_object", |
| 68 |
"orca.ax_utilities", |
| 69 |
"orca.orca_platform", |
| 70 |
"orca.braille_presenter", |
| 71 |
"orca.presentation_manager", |
| 72 |
], |
| 73 |
) |
| 74 |
|
| 75 |
command_manager_mock = essential_modules["orca.command_manager"] |
| 76 |
manager_instance_mock = test_context.Mock() |
| 77 |
manager_instance_mock.add_command = test_context.Mock() |
| 78 |
mock_command = test_context.Mock() |
| 79 |
mock_command.get_keybinding.return_value = test_context.Mock() |
| 80 |
mock_command.is_active.return_value = True |
| 81 |
manager_instance_mock.get_command = test_context.Mock(return_value=mock_command) |
| 82 |
manager_instance_mock.get_commands_by_group_label = test_context.Mock( |
| 83 |
return_value=[mock_command], |
| 84 |
) |
| 85 |
command_manager_mock.get_manager = test_context.Mock(return_value=manager_instance_mock) |
| 86 |
command_manager_mock.Command = test_context.Mock() |
| 87 |
|
| 88 |
atspi_mock = essential_modules["gi.repository.Atspi"] |
| 89 |
atspi_accessible_mock = test_context.Mock() |
| 90 |
atspi_accessible_mock.get_parent = test_context.Mock(return_value=None) |
| 91 |
atspi_accessible_mock.get_child_count = test_context.Mock(return_value=0) |
| 92 |
atspi_accessible_mock.get_child_at_index = test_context.Mock(return_value=None) |
| 93 |
atspi_mock.Accessible = atspi_accessible_mock |
| 94 |
|
| 95 |
dbus_service_mock = essential_modules["orca.dbus_service"] |
| 96 |
controller_mock = test_context.Mock() |
| 97 |
controller_mock.register_decorated_module = test_context.Mock() |
| 98 |
controller_mock.reset_mock() |
| 99 |
dbus_service_mock.get_remote_controller = test_context.Mock(return_value=controller_mock) |
| 100 |
|
| 101 |
def mock_command_decorator(func): |
| 102 |
return func |
| 103 |
|
| 104 |
dbus_service_mock.command = mock_command_decorator |
| 105 |
|
| 106 |
if "orca.object_navigator" in sys.modules: |
| 107 |
object_navigator_module = sys.modules["orca.object_navigator"] |
| 108 |
if not hasattr(object_navigator_module, "_original_navigator"): |
| 109 |
object_navigator_module._original_navigator = getattr( |
| 110 |
object_navigator_module, |
| 111 |
"_navigator", |
| 112 |
None, |
| 113 |
) |
| 114 |
object_navigator_module._navigator = test_context.Mock() |
| 115 |
|
| 116 |
debug_mock = essential_modules["orca.debug"] |
| 117 |
debug_mock.LEVEL_INFO = 800 |
| 118 |
debug_mock.print_message = test_context.Mock() |
| 119 |
debug_mock.print_tokens = test_context.Mock() |
| 120 |
|
| 121 |
input_event_mock = essential_modules["orca.input_event"] |
| 122 |
input_event_handler_mock = test_context.Mock() |
| 123 |
input_event_mock.InputEventHandler = test_context.Mock( |
| 124 |
return_value=input_event_handler_mock, |
| 125 |
) |
| 126 |
|
| 127 |
keybindings_mock = essential_modules["orca.keybindings"] |
| 128 |
keybindings_instance_mock = test_context.Mock() |
| 129 |
keybindings_instance_mock.is_empty = test_context.Mock(return_value=True) |
| 130 |
keybindings_instance_mock.remove_key_grabs = test_context.Mock() |
| 131 |
keybindings_mock.KeyBindings = test_context.Mock(return_value=keybindings_instance_mock) |
| 132 |
|
| 133 |
ax_object_mock = essential_modules["orca.ax_object"] |
| 134 |
ax_object_mock.AXObject = test_context.Mock() |
| 135 |
ax_object_mock.AXObject.get_parent = test_context.Mock(return_value=None) |
| 136 |
ax_object_mock.AXObject.get_child_count = test_context.Mock(return_value=0) |
| 137 |
ax_object_mock.AXObject.iter_children = test_context.Mock(return_value=[]) |
| 138 |
|
| 139 |
ax_utilities_mock = essential_modules["orca.ax_utilities"] |
| 140 |
ax_utilities_mock.AXUtilities = test_context.Mock() |
| 141 |
ax_utilities_mock.AXUtilities.is_focused = test_context.Mock(return_value=False) |
| 142 |
ax_utilities_mock.AXUtilities.is_paragraph = test_context.Mock(return_value=True) |
| 143 |
ax_utilities_mock.AXUtilities.is_layout_only = test_context.Mock(return_value=False) |
| 144 |
|
| 145 |
focus_manager_mock = essential_modules["orca.focus_manager"] |
| 146 |
focus_manager_mock.get_manager = test_context.Mock() |
| 147 |
focus_manager_instance = test_context.Mock() |
| 148 |
focus_manager_instance.get_locus_of_focus = test_context.Mock(return_value=None) |
| 149 |
focus_manager_instance.get_active_mode_and_object_of_interest = test_context.Mock( |
| 150 |
return_value=("default", None), |
| 151 |
) |
| 152 |
focus_manager_mock.get_manager.return_value = focus_manager_instance |
| 153 |
|
| 154 |
ax_event_synthesizer_mock = essential_modules["orca.ax_event_synthesizer"] |
| 155 |
ax_event_synthesizer_mock.AXEventSynthesizer = test_context.Mock() |
| 156 |
ax_event_synthesizer_mock.AXEventSynthesizer.try_all_clickable_actions = test_context.Mock( |
| 157 |
return_value=False, |
| 158 |
) |
| 159 |
ax_event_synthesizer_mock.AXEventSynthesizer.click_object = test_context.Mock() |
| 160 |
|
| 161 |
cmdnames_mock = essential_modules["orca.cmdnames"] |
| 162 |
cmdnames_mock.OBJECT_NAVIGATOR_PARENT = "objectNavigatorParent" |
| 163 |
cmdnames_mock.OBJECT_NAVIGATOR_FIRST_CHILD = "objectNavigatorFirstChild" |
| 164 |
cmdnames_mock.OBJECT_NAVIGATOR_NEXT_SIBLING = "objectNavigatorNextSibling" |
| 165 |
cmdnames_mock.OBJECT_NAVIGATOR_PREVIOUS_SIBLING = "objectNavigatorPreviousSibling" |
| 166 |
cmdnames_mock.OBJECT_NAVIGATOR_TOGGLE_SIMPLIFY = "objectNavigatorToggleSimplify" |
| 167 |
cmdnames_mock.OBJECT_NAVIGATOR_PERFORM_ACTION = "objectNavigatorPerformAction" |
| 168 |
|
| 169 |
messages_mock = essential_modules["orca.messages"] |
| 170 |
messages_mock.OBJECT_NAVIGATOR_NO_PARENT = "No parent" |
| 171 |
messages_mock.OBJECT_NAVIGATOR_NO_CHILD = "No child" |
| 172 |
messages_mock.OBJECT_NAVIGATOR_NO_SIBLING = "No sibling" |
| 173 |
messages_mock.OBJECT_NAVIGATOR_SIMPLIFY_ON = "Simplify navigation on" |
| 174 |
messages_mock.OBJECT_NAVIGATOR_SIMPLIFY_OFF = "Simplify navigation off" |
| 175 |
|
| 176 |
atspi_mock.ModifierType = test_context.Mock() |
| 177 |
atspi_mock.ModifierType.ALT = 3 |
| 178 |
atspi_mock.ModifierType.CONTROL = 2 |
| 179 |
atspi_mock.ModifierType.SHIFT = 0 |
| 180 |
|
| 181 |
return essential_modules |
| 182 |
|
| 183 |
def test_init_creates_navigator_with_defaults(self, test_context: OrcaTestContext) -> None: |
| 184 |
"""Test ObjectNavigator initialization with default settings.""" |
| 185 |
|
| 186 |
self._setup_dependencies(test_context) |
| 187 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 188 |
|
| 189 |
navigator: ObjectNavigator = ObjectNavigator() |
| 190 |
assert navigator._navigator_focus is None |
| 191 |
assert navigator._last_navigator_focus is None |
| 192 |
assert navigator._last_locus_of_focus is None |
| 193 |
assert navigator._simplify is True |
| 194 |
|
| 195 |
def test_setup_registers_dbus_commands(self, test_context: OrcaTestContext) -> None: |
| 196 |
"""Test ObjectNavigator registers D-Bus commands during setup.""" |
| 197 |
|
| 198 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 199 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 200 |
|
| 201 |
dbus_service = essential_modules["orca.dbus_service"] |
| 202 |
controller = dbus_service.get_remote_controller.return_value |
| 203 |
controller.register_decorated_module.reset_mock() |
| 204 |
navigator = ObjectNavigator() |
| 205 |
navigator.set_up_commands() |
| 206 |
controller.register_decorated_module.assert_called_once_with("ObjectNavigator", navigator) |
| 207 |
|
| 208 |
def test_include_in_simple_navigation_basic_object(self, test_context: OrcaTestContext) -> None: |
| 209 |
"""Test _include_in_simple_navigation with basic accessible object.""" |
| 210 |
|
| 211 |
self._setup_dependencies(test_context) |
| 212 |
mock_obj = test_context.Mock() |
| 213 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 214 |
|
| 215 |
navigator = ObjectNavigator() |
| 216 |
result = navigator._include_in_simple_navigation(mock_obj) |
| 217 |
assert result is True |
| 218 |
|
| 219 |
def test_exclude_from_simple_navigation_with_script( |
| 220 |
self, |
| 221 |
test_context: OrcaTestContext, |
| 222 |
) -> None: |
| 223 |
"""Test _exclude_from_simple_navigation with script parameter.""" |
| 224 |
|
| 225 |
self._setup_dependencies(test_context) |
| 226 |
mock_script = test_context.Mock() |
| 227 |
mock_obj = test_context.Mock() |
| 228 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 229 |
|
| 230 |
navigator = ObjectNavigator() |
| 231 |
result = navigator._exclude_from_simple_navigation(mock_script, mock_obj) |
| 232 |
assert result is False |
| 233 |
|
| 234 |
def test_children_returns_list_for_object_with_children( |
| 235 |
self, |
| 236 |
test_context: OrcaTestContext, |
| 237 |
) -> None: |
| 238 |
"""Test _children returns list of child objects.""" |
| 239 |
|
| 240 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 241 |
mock_script = test_context.Mock() |
| 242 |
mock_obj = test_context.Mock() |
| 243 |
mock_child = test_context.Mock() |
| 244 |
ax_object = essential_modules["orca.ax_object"].AXObject |
| 245 |
ax_object.get_child_count.return_value = 1 |
| 246 |
ax_object.iter_children.return_value = [mock_child] |
| 247 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 248 |
|
| 249 |
navigator = ObjectNavigator() |
| 250 |
children = navigator._children(mock_script, mock_obj) |
| 251 |
assert isinstance(children, list) |
| 252 |
assert len(children) == 1 |
| 253 |
assert children[0] == mock_child |
| 254 |
|
| 255 |
def test_children_returns_empty_list_for_object_without_children( |
| 256 |
self, |
| 257 |
test_context: OrcaTestContext, |
| 258 |
) -> None: |
| 259 |
"""Test _children returns empty list for object with no children.""" |
| 260 |
|
| 261 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 262 |
mock_script = test_context.Mock() |
| 263 |
mock_obj = test_context.Mock() |
| 264 |
ax_object = essential_modules["orca.ax_object"].AXObject |
| 265 |
ax_object.get_child_count.return_value = 0 |
| 266 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 267 |
|
| 268 |
navigator = ObjectNavigator() |
| 269 |
children = navigator._children(mock_script, mock_obj) |
| 270 |
assert isinstance(children, list) |
| 271 |
assert len(children) == 0 |
| 272 |
|
| 273 |
def test_parent_returns_parent_object(self, test_context: OrcaTestContext) -> None: |
| 274 |
"""Test _parent returns parent of accessible object.""" |
| 275 |
|
| 276 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 277 |
mock_script = test_context.Mock() |
| 278 |
mock_obj = test_context.Mock() |
| 279 |
mock_parent = test_context.Mock() |
| 280 |
ax_object = essential_modules["orca.ax_object"].AXObject |
| 281 |
ax_object.get_parent.return_value = mock_parent |
| 282 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 283 |
|
| 284 |
navigator = ObjectNavigator() |
| 285 |
parent = navigator._parent(mock_script, mock_obj) |
| 286 |
assert parent == mock_parent |
| 287 |
ax_object.get_parent.assert_called_once_with(mock_obj) |
| 288 |
|
| 289 |
def test_parent_returns_none_when_no_parent(self, test_context: OrcaTestContext) -> None: |
| 290 |
"""Test _parent returns None when object has no parent.""" |
| 291 |
|
| 292 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 293 |
mock_script = test_context.Mock() |
| 294 |
mock_obj = test_context.Mock() |
| 295 |
ax_object = essential_modules["orca.ax_object"].AXObject |
| 296 |
ax_object.get_parent.return_value = None |
| 297 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 298 |
|
| 299 |
navigator = ObjectNavigator() |
| 300 |
parent = navigator._parent(mock_script, mock_obj) |
| 301 |
assert parent is None |
| 302 |
|
| 303 |
def test_set_navigator_focus_updates_focus_tracking( |
| 304 |
self, |
| 305 |
test_context: OrcaTestContext, |
| 306 |
) -> None: |
| 307 |
"""Test _set_navigator_focus properly tracks focus changes.""" |
| 308 |
|
| 309 |
self._setup_dependencies(test_context) |
| 310 |
mock_obj = test_context.Mock() |
| 311 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 312 |
|
| 313 |
navigator = ObjectNavigator() |
| 314 |
navigator._set_navigator_focus(mock_obj) |
| 315 |
assert navigator._navigator_focus == mock_obj |
| 316 |
|
| 317 |
def test_set_navigator_focus_tracks_previous_focus(self, test_context: OrcaTestContext) -> None: |
| 318 |
"""Test _set_navigator_focus tracks previous focus object.""" |
| 319 |
|
| 320 |
self._setup_dependencies(test_context) |
| 321 |
mock_obj1 = test_context.Mock() |
| 322 |
mock_obj2 = test_context.Mock() |
| 323 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 324 |
|
| 325 |
navigator = ObjectNavigator() |
| 326 |
navigator._set_navigator_focus(mock_obj1) |
| 327 |
navigator._set_navigator_focus(mock_obj2) |
| 328 |
assert navigator._navigator_focus == mock_obj2 |
| 329 |
assert navigator._last_navigator_focus == mock_obj1 |
| 330 |
|
| 331 |
def test_update_tracks_locus_of_focus(self, test_context: OrcaTestContext) -> None: |
| 332 |
"""Test _update tracks the current locus of focus.""" |
| 333 |
|
| 334 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 335 |
mock_focus_obj = test_context.Mock() |
| 336 |
focus_manager = essential_modules["orca.focus_manager"].get_manager.return_value |
| 337 |
focus_manager.get_locus_of_focus.return_value = mock_focus_obj |
| 338 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 339 |
|
| 340 |
navigator = ObjectNavigator() |
| 341 |
navigator._update() |
| 342 |
assert navigator._last_locus_of_focus == mock_focus_obj |
| 343 |
|
| 344 |
def test_present_calls_script_presentation_method(self, test_context: OrcaTestContext) -> None: |
| 345 |
"""Test _present calls appropriate script method for object presentation.""" |
| 346 |
|
| 347 |
self._setup_dependencies(test_context) |
| 348 |
mock_script = test_context.Mock() |
| 349 |
mock_script.present_object = test_context.Mock() |
| 350 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 351 |
|
| 352 |
navigator = ObjectNavigator() |
| 353 |
navigator._navigator_focus = test_context.Mock() |
| 354 |
navigator._present(mock_script) |
| 355 |
mock_script.present_object.assert_called_once() |
| 356 |
|
| 357 |
@pytest.mark.parametrize( |
| 358 |
"notify_user", |
| 359 |
[ |
| 360 |
pytest.param(True, id="notify_user_true"), |
| 361 |
pytest.param(False, id="notify_user_false"), |
| 362 |
], |
| 363 |
) |
| 364 |
def test_present_respects_notify_user_parameter( |
| 365 |
self, |
| 366 |
test_context: OrcaTestContext, |
| 367 |
notify_user, |
| 368 |
) -> None: |
| 369 |
"""Test _present respects the notify_user parameter.""" |
| 370 |
|
| 371 |
self._setup_dependencies(test_context) |
| 372 |
mock_script = test_context.Mock() |
| 373 |
mock_script.present_object = test_context.Mock() |
| 374 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 375 |
|
| 376 |
navigator = ObjectNavigator() |
| 377 |
navigator._navigator_focus = test_context.Mock() |
| 378 |
navigator._present(mock_script, notify_user=notify_user) |
| 379 |
if notify_user: |
| 380 |
mock_script.present_object.assert_called_once() |
| 381 |
else: |
| 382 |
mock_script.present_object.assert_not_called() |
| 383 |
|
| 384 |
@pytest.mark.parametrize( |
| 385 |
"has_parent", |
| 386 |
[ |
| 387 |
pytest.param(True, id="success"), |
| 388 |
pytest.param(False, id="no_parent"), |
| 389 |
], |
| 390 |
) |
| 391 |
def test_move_to_parent_scenarios( |
| 392 |
self, |
| 393 |
test_context: OrcaTestContext, |
| 394 |
has_parent: bool, |
| 395 |
) -> None: |
| 396 |
"""Test move_to_parent with various parent availability scenarios.""" |
| 397 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 398 |
mock_script = test_context.Mock() |
| 399 |
mock_script.present_object = test_context.Mock() |
| 400 |
mock_current = test_context.Mock() |
| 401 |
|
| 402 |
if has_parent: |
| 403 |
mock_parent = test_context.Mock() |
| 404 |
ax_object = essential_modules["orca.ax_object"].AXObject |
| 405 |
ax_object.get_parent.return_value = mock_parent |
| 406 |
expected_focus = mock_parent |
| 407 |
else: |
| 408 |
ax_object = essential_modules["orca.ax_object"].AXObject |
| 409 |
ax_object.get_parent.return_value = None |
| 410 |
expected_focus = mock_current |
| 411 |
|
| 412 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 413 |
|
| 414 |
navigator = ObjectNavigator() |
| 415 |
navigator._navigator_focus = mock_current |
| 416 |
result = navigator.move_to_parent(mock_script) |
| 417 |
assert result is True |
| 418 |
assert navigator._navigator_focus == expected_focus |
| 419 |
|
| 420 |
@pytest.mark.parametrize( |
| 421 |
"has_children, child_count", |
| 422 |
[ |
| 423 |
pytest.param(True, 1, id="success"), |
| 424 |
pytest.param(False, 0, id="no_children"), |
| 425 |
], |
| 426 |
) |
| 427 |
def test_move_to_first_child_scenarios( |
| 428 |
self, |
| 429 |
test_context: OrcaTestContext, |
| 430 |
has_children: bool, |
| 431 |
child_count: int, |
| 432 |
) -> None: |
| 433 |
"""Test move_to_first_child with various child availability scenarios.""" |
| 434 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 435 |
mock_script = test_context.Mock() |
| 436 |
mock_script.present_object = test_context.Mock() |
| 437 |
mock_current = test_context.Mock() |
| 438 |
ax_object = essential_modules["orca.ax_object"].AXObject |
| 439 |
ax_object.get_child_count.return_value = child_count |
| 440 |
|
| 441 |
if has_children: |
| 442 |
mock_child = test_context.Mock() |
| 443 |
ax_object.iter_children.return_value = [mock_child] |
| 444 |
expected_focus = mock_child |
| 445 |
else: |
| 446 |
expected_focus = mock_current |
| 447 |
|
| 448 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 449 |
|
| 450 |
navigator = ObjectNavigator() |
| 451 |
navigator._navigator_focus = mock_current |
| 452 |
result = navigator.move_to_first_child(mock_script) |
| 453 |
assert result is True |
| 454 |
assert navigator._navigator_focus == expected_focus |
| 455 |
|
| 456 |
def test_move_to_next_sibling_success(self, test_context: OrcaTestContext) -> None: |
| 457 |
"""Test move_to_next_sibling successfully navigates to next sibling.""" |
| 458 |
|
| 459 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 460 |
mock_script = test_context.Mock() |
| 461 |
mock_script.present_object = test_context.Mock() |
| 462 |
mock_current = test_context.Mock() |
| 463 |
mock_parent = test_context.Mock() |
| 464 |
mock_sibling = test_context.Mock() |
| 465 |
ax_object = essential_modules["orca.ax_object"].AXObject |
| 466 |
ax_object.get_parent.return_value = mock_parent |
| 467 |
ax_object.get_child_count.return_value = 2 |
| 468 |
ax_object.iter_children.return_value = [mock_current, mock_sibling] |
| 469 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 470 |
|
| 471 |
navigator = ObjectNavigator() |
| 472 |
navigator._navigator_focus = mock_current |
| 473 |
result = navigator.move_to_next_sibling(mock_script) |
| 474 |
assert result is True |
| 475 |
assert navigator._navigator_focus == mock_sibling |
| 476 |
|
| 477 |
def test_move_to_previous_sibling_success(self, test_context: OrcaTestContext) -> None: |
| 478 |
"""Test move_to_previous_sibling successfully navigates to previous sibling.""" |
| 479 |
|
| 480 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 481 |
mock_script = test_context.Mock() |
| 482 |
mock_script.present_object = test_context.Mock() |
| 483 |
mock_current = test_context.Mock() |
| 484 |
mock_parent = test_context.Mock() |
| 485 |
mock_sibling = test_context.Mock() |
| 486 |
ax_object = essential_modules["orca.ax_object"].AXObject |
| 487 |
ax_object.get_parent.return_value = mock_parent |
| 488 |
ax_object.get_child_count.return_value = 2 |
| 489 |
ax_object.iter_children.return_value = [mock_sibling, mock_current] |
| 490 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 491 |
|
| 492 |
navigator = ObjectNavigator() |
| 493 |
navigator._navigator_focus = mock_current |
| 494 |
result = navigator.move_to_previous_sibling(mock_script) |
| 495 |
assert result is True |
| 496 |
assert navigator._navigator_focus == mock_sibling |
| 497 |
|
| 498 |
def test_toggle_simplify_changes_simplify_state(self, test_context: OrcaTestContext) -> None: |
| 499 |
"""Test toggle_simplify changes the simplification state.""" |
| 500 |
|
| 501 |
self._setup_dependencies(test_context) |
| 502 |
mock_script = test_context.Mock() |
| 503 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 504 |
|
| 505 |
navigator = ObjectNavigator() |
| 506 |
original_state = navigator._simplify |
| 507 |
result = navigator.toggle_simplify(mock_script) |
| 508 |
assert result is True |
| 509 |
assert navigator._simplify != original_state |
| 510 |
|
| 511 |
def test_toggle_simplify_announces_state_change(self, test_context: OrcaTestContext) -> None: |
| 512 |
"""Test toggle_simplify announces the state change to user.""" |
| 513 |
|
| 514 |
essential_modules = self._setup_dependencies(test_context) |
| 515 |
mock_script = test_context.Mock() |
| 516 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 517 |
|
| 518 |
pres_manager = essential_modules["orca.presentation_manager"].get_manager() |
| 519 |
pres_manager.present_message.reset_mock() |
| 520 |
navigator = ObjectNavigator() |
| 521 |
navigator.toggle_simplify(mock_script) |
| 522 |
pres_manager.present_message.assert_called_once() |
| 523 |
|
| 524 |
def test_perform_action_calls_synthesizer(self, test_context: OrcaTestContext) -> None: |
| 525 |
"""Test perform_action uses AXEventSynthesizer for action execution.""" |
| 526 |
|
| 527 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 528 |
mock_script = test_context.Mock() |
| 529 |
mock_obj = test_context.Mock() |
| 530 |
ax_event_synthesizer = essential_modules["orca.ax_event_synthesizer"].AXEventSynthesizer |
| 531 |
ax_event_synthesizer.try_all_clickable_actions = test_context.Mock(return_value=False) |
| 532 |
ax_event_synthesizer.click_object = test_context.Mock(return_value=True) |
| 533 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 534 |
|
| 535 |
navigator = ObjectNavigator() |
| 536 |
navigator._navigator_focus = mock_obj |
| 537 |
result = navigator.perform_action(mock_script) |
| 538 |
assert result is True |
| 539 |
ax_event_synthesizer.click_object.assert_called_once_with(mock_obj, 1) |
| 540 |
|
| 541 |
def test_children_with_null_object(self, test_context: OrcaTestContext) -> None: |
| 542 |
"""Test _children handles None object gracefully.""" |
| 543 |
|
| 544 |
self._setup_dependencies(test_context) |
| 545 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 546 |
|
| 547 |
navigator = ObjectNavigator() |
| 548 |
mock_script = test_context.Mock() |
| 549 |
children = navigator._children(mock_script, None) |
| 550 |
assert isinstance(children, list) |
| 551 |
assert len(children) == 0 |
| 552 |
|
| 553 |
def test_parent_with_null_object_returns_none(self, test_context: OrcaTestContext) -> None: |
| 554 |
"""Test _parent returns None when given None object.""" |
| 555 |
|
| 556 |
self._setup_dependencies(test_context) |
| 557 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 558 |
|
| 559 |
navigator = ObjectNavigator() |
| 560 |
mock_script = test_context.Mock() |
| 561 |
parent = navigator._parent(mock_script, None) |
| 562 |
assert parent is None |
| 563 |
|
| 564 |
def test_move_to_parent_with_null_navigator_focus(self, test_context: OrcaTestContext) -> None: |
| 565 |
"""Test move_to_parent handles None navigator focus gracefully.""" |
| 566 |
|
| 567 |
self._setup_dependencies(test_context) |
| 568 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 569 |
|
| 570 |
navigator = ObjectNavigator() |
| 571 |
navigator._navigator_focus = None |
| 572 |
mock_script = test_context.Mock() |
| 573 |
result = navigator.move_to_parent(mock_script) |
| 574 |
assert result is True |
| 575 |
|
| 576 |
def test_move_to_first_child_with_dead_object(self, test_context: OrcaTestContext) -> None: |
| 577 |
"""Test move_to_first_child handles dead AT-SPI objects.""" |
| 578 |
|
| 579 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 580 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 581 |
|
| 582 |
navigator = ObjectNavigator() |
| 583 |
mock_script = test_context.Mock() |
| 584 |
mock_obj = test_context.Mock() |
| 585 |
navigator._navigator_focus = mock_obj |
| 586 |
|
| 587 |
ax_object = essential_modules["orca.ax_object"].AXObject |
| 588 |
ax_object.is_dead.return_value = True |
| 589 |
|
| 590 |
result = navigator.move_to_first_child(mock_script) |
| 591 |
assert result is True |
| 592 |
|
| 593 |
def test_perform_action_with_null_focus_logs_debug_info( |
| 594 |
self, |
| 595 |
test_context: OrcaTestContext, |
| 596 |
) -> None: |
| 597 |
"""Test perform_action logs debug information with None navigator focus.""" |
| 598 |
|
| 599 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 600 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 601 |
|
| 602 |
navigator = ObjectNavigator() |
| 603 |
navigator._navigator_focus = None |
| 604 |
mock_script = test_context.Mock() |
| 605 |
|
| 606 |
result = navigator.perform_action(mock_script) |
| 607 |
assert result is True |
| 608 |
essential_modules["orca.debug"].print_tokens.assert_called() |
| 609 |
|
| 610 |
def test_toggle_simplify_announces_on_state(self, test_context: OrcaTestContext) -> None: |
| 611 |
"""Test toggle_simplify announces state change correctly.""" |
| 612 |
|
| 613 |
essential_modules = self._setup_dependencies(test_context) |
| 614 |
from orca.object_navigator import ObjectNavigator # pylint: disable=import-outside-toplevel |
| 615 |
|
| 616 |
navigator = ObjectNavigator() |
| 617 |
navigator._simplify = False |
| 618 |
mock_script = test_context.Mock() |
| 619 |
|
| 620 |
pres_manager = essential_modules["orca.presentation_manager"].get_manager() |
| 621 |
pres_manager.present_message.reset_mock() |
| 622 |
navigator.toggle_simplify(mock_script) |
| 623 |
|
| 624 |
pres_manager.present_message.assert_called_once() |
| 625 |
assert navigator._simplify is True |
| 626 |
|
| 627 |
def test_get_navigator_returns_singleton_instance(self, test_context: OrcaTestContext) -> None: |
| 628 |
"""Test get_navigator function returns singleton ObjectNavigator instance.""" |
| 629 |
|
| 630 |
self._setup_dependencies(test_context) |
| 631 |
from orca.object_navigator import get_navigator # pylint: disable=import-outside-toplevel |
| 632 |
|
| 633 |
navigator1 = get_navigator() |
| 634 |
navigator2 = get_navigator() |
| 635 |
assert navigator1 is navigator2 |
| 636 |
assert navigator1.__class__.__name__ == "ObjectNavigator" |