| 1 |
# Unit tests for ax_utilities_object.py methods. |
| 2 |
# |
| 3 |
# Copyright 2026 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=import-outside-toplevel |
| 22 |
# pylint: disable=protected-access |
| 23 |
# pylint: disable=too-many-public-methods |
| 24 |
|
| 25 |
"""Unit tests for ax_utilities_object.py methods.""" |
| 26 |
|
| 27 |
from __future__ import annotations |
| 28 |
|
| 29 |
from typing import TYPE_CHECKING |
| 30 |
|
| 31 |
import gi |
| 32 |
import pytest |
| 33 |
|
| 34 |
gi.require_version("Atspi", "2.0") |
| 35 |
from gi.repository import Atspi |
| 36 |
|
| 37 |
if TYPE_CHECKING: |
| 38 |
from unittest.mock import MagicMock |
| 39 |
|
| 40 |
from .orca_test_context import OrcaTestContext |
| 41 |
|
| 42 |
|
| 43 |
@pytest.mark.unit |
| 44 |
class TestAXUtilitiesObject: |
| 45 |
"""Test AXUtilitiesObject class methods.""" |
| 46 |
|
| 47 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 48 |
"""Set up mocks for ax_utilities_object dependencies.""" |
| 49 |
|
| 50 |
core_modules = [ |
| 51 |
"orca.debug", |
| 52 |
"orca.messages", |
| 53 |
"orca.input_event", |
| 54 |
"orca.keybindings", |
| 55 |
"orca.cmdnames", |
| 56 |
"orca.dbus_service", |
| 57 |
"orca.script_manager", |
| 58 |
"orca.orca_i18n", |
| 59 |
"orca.guilabels", |
| 60 |
"orca.text_attribute_names", |
| 61 |
"orca.focus_manager", |
| 62 |
"orca.braille", |
| 63 |
"orca.keynames", |
| 64 |
] |
| 65 |
|
| 66 |
essential_modules = {} |
| 67 |
for module_name in core_modules: |
| 68 |
mock_module = test_context.Mock() |
| 69 |
test_context.patch_module(module_name, mock_module) |
| 70 |
essential_modules[module_name] = mock_module |
| 71 |
|
| 72 |
test_context.configure_shared_module_behaviors(essential_modules) |
| 73 |
keynames_mock = essential_modules["orca.keynames"] |
| 74 |
keynames_mock.KEY_BACKSPACE = "BackSpace" |
| 75 |
keynames_mock.KEY_DELETE = "Delete" |
| 76 |
keynames_mock.KEY_TAB = "Tab" |
| 77 |
keynames_mock.KEY_RETURN = "Return" |
| 78 |
keynames_mock.KEY_ESCAPE = "Escape" |
| 79 |
|
| 80 |
return essential_modules |
| 81 |
|
| 82 |
def test_is_ancestor_invalid_obj(self, test_context: OrcaTestContext) -> None: |
| 83 |
"""Test AXUtilitiesObject.is_ancestor with invalid obj.""" |
| 84 |
|
| 85 |
self._setup_dependencies(test_context) |
| 86 |
from orca.ax_object import AXObject |
| 87 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 88 |
|
| 89 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 90 |
test_context.patch_object( |
| 91 |
AXObject, |
| 92 |
"is_valid", |
| 93 |
side_effect=lambda obj: obj == mock_accessible, |
| 94 |
) |
| 95 |
result = AXUtilitiesObject.is_ancestor(None, mock_accessible) |
| 96 |
assert result is False |
| 97 |
|
| 98 |
def test_is_ancestor_invalid_ancestor(self, test_context: OrcaTestContext) -> None: |
| 99 |
"""Test AXUtilitiesObject.is_ancestor with invalid ancestor.""" |
| 100 |
|
| 101 |
self._setup_dependencies(test_context) |
| 102 |
from orca.ax_object import AXObject |
| 103 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 104 |
|
| 105 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 106 |
test_context.patch_object( |
| 107 |
AXObject, |
| 108 |
"is_valid", |
| 109 |
side_effect=lambda obj: obj == mock_accessible, |
| 110 |
) |
| 111 |
result = AXUtilitiesObject.is_ancestor(mock_accessible, None) |
| 112 |
assert result is False |
| 113 |
|
| 114 |
def test_is_ancestor_same_object_inclusive(self, test_context: OrcaTestContext) -> None: |
| 115 |
"""Test AXUtilitiesObject.is_ancestor with same object and inclusive=True.""" |
| 116 |
|
| 117 |
self._setup_dependencies(test_context) |
| 118 |
from orca.ax_object import AXObject |
| 119 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 120 |
|
| 121 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 122 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 123 |
result = AXUtilitiesObject.is_ancestor(mock_accessible, mock_accessible, inclusive=True) |
| 124 |
assert result is True |
| 125 |
|
| 126 |
def test_is_ancestor_same_object_not_inclusive(self, test_context: OrcaTestContext) -> None: |
| 127 |
"""Test AXUtilitiesObject.is_ancestor with same object and inclusive=False.""" |
| 128 |
|
| 129 |
self._setup_dependencies(test_context) |
| 130 |
from orca.ax_object import AXObject |
| 131 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 132 |
|
| 133 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 134 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 135 |
test_context.patch_object(AXObject, "get_parent_checked", return_value=None) |
| 136 |
result = AXUtilitiesObject.is_ancestor(mock_accessible, mock_accessible, inclusive=False) |
| 137 |
assert result is False |
| 138 |
|
| 139 |
def test_is_ancestor_found(self, test_context: OrcaTestContext) -> None: |
| 140 |
"""Test AXUtilitiesObject.is_ancestor when ancestor is found.""" |
| 141 |
|
| 142 |
self._setup_dependencies(test_context) |
| 143 |
from orca.ax_object import AXObject |
| 144 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 145 |
|
| 146 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 147 |
mock_ancestor = test_context.Mock(spec=Atspi.Accessible) |
| 148 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 149 |
test_context.patch_object(AXObject, "get_parent_checked", return_value=mock_ancestor) |
| 150 |
result = AXUtilitiesObject.is_ancestor(mock_accessible, mock_ancestor) |
| 151 |
assert result is True |
| 152 |
|
| 153 |
def test_is_ancestor_not_found(self, test_context: OrcaTestContext) -> None: |
| 154 |
"""Test AXUtilitiesObject.is_ancestor when ancestor is not found.""" |
| 155 |
|
| 156 |
self._setup_dependencies(test_context) |
| 157 |
from orca.ax_object import AXObject |
| 158 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 159 |
|
| 160 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 161 |
mock_ancestor = test_context.Mock(spec=Atspi.Accessible) |
| 162 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 163 |
test_context.patch_object(AXObject, "get_parent_checked", return_value=None) |
| 164 |
result = AXUtilitiesObject.is_ancestor(mock_accessible, mock_ancestor) |
| 165 |
assert result is False |
| 166 |
|
| 167 |
def test_find_descendant_invalid_obj(self, test_context: OrcaTestContext) -> None: |
| 168 |
"""Test AXUtilitiesObject._find_descendant with invalid obj.""" |
| 169 |
|
| 170 |
self._setup_dependencies(test_context) |
| 171 |
from orca.ax_object import AXObject |
| 172 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 173 |
|
| 174 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 175 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 176 |
|
| 177 |
def always_true(_obj) -> bool: |
| 178 |
return True |
| 179 |
|
| 180 |
result = AXUtilitiesObject._find_descendant(mock_accessible, always_true) |
| 181 |
assert result is None |
| 182 |
|
| 183 |
def test_find_descendant_no_children(self, test_context: OrcaTestContext) -> None: |
| 184 |
"""Test AXUtilitiesObject._find_descendant with no children.""" |
| 185 |
|
| 186 |
self._setup_dependencies(test_context) |
| 187 |
from orca.ax_object import AXObject |
| 188 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 189 |
|
| 190 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 191 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 192 |
test_context.patch_object(AXObject, "get_child_count", return_value=0) |
| 193 |
|
| 194 |
def always_true(_obj) -> bool: |
| 195 |
return True |
| 196 |
|
| 197 |
result = AXUtilitiesObject._find_descendant(mock_accessible, always_true) |
| 198 |
assert result is None |
| 199 |
|
| 200 |
def test_find_descendant_found_direct_child(self, test_context: OrcaTestContext) -> None: |
| 201 |
"""Test AXUtilitiesObject._find_descendant with direct child match.""" |
| 202 |
|
| 203 |
self._setup_dependencies(test_context) |
| 204 |
from orca.ax_object import AXObject |
| 205 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 206 |
|
| 207 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 208 |
mock_child = test_context.Mock(spec=Atspi.Accessible) |
| 209 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 210 |
test_context.patch_object(AXObject, "get_child_count", return_value=1) |
| 211 |
test_context.patch_object(AXObject, "get_child_checked", return_value=mock_child) |
| 212 |
|
| 213 |
def match_child(obj) -> bool: |
| 214 |
return obj == mock_child |
| 215 |
|
| 216 |
result = AXUtilitiesObject._find_descendant(mock_accessible, match_child) |
| 217 |
assert result == mock_child |
| 218 |
|
| 219 |
def test_find_descendant_found_in_grandchild(self, test_context: OrcaTestContext) -> None: |
| 220 |
"""Test AXUtilitiesObject._find_descendant with grandchild match.""" |
| 221 |
|
| 222 |
self._setup_dependencies(test_context) |
| 223 |
from orca.ax_object import AXObject |
| 224 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 225 |
|
| 226 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 227 |
mock_child = test_context.Mock(spec=Atspi.Accessible) |
| 228 |
mock_grandchild = test_context.Mock(spec=Atspi.Accessible) |
| 229 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 230 |
|
| 231 |
def mock_get_child_count(obj) -> int: |
| 232 |
if obj == mock_accessible: |
| 233 |
return 1 |
| 234 |
if obj == mock_child: |
| 235 |
return 1 |
| 236 |
return 0 |
| 237 |
|
| 238 |
def mock_get_child_checked(obj, idx) -> object: |
| 239 |
if obj == mock_accessible and idx == 0: |
| 240 |
return mock_child |
| 241 |
if obj == mock_child and idx == 0: |
| 242 |
return mock_grandchild |
| 243 |
return None |
| 244 |
|
| 245 |
test_context.patch_object(AXObject, "get_child_count", new=mock_get_child_count) |
| 246 |
test_context.patch_object(AXObject, "get_child_checked", new=mock_get_child_checked) |
| 247 |
|
| 248 |
def match_grandchild(obj) -> bool: |
| 249 |
return obj == mock_grandchild |
| 250 |
|
| 251 |
result = AXUtilitiesObject._find_descendant(mock_accessible, match_grandchild) |
| 252 |
assert result == mock_grandchild |
| 253 |
|
| 254 |
def test_find_deepest_descendant_invalid_obj(self, test_context: OrcaTestContext) -> None: |
| 255 |
"""Test AXUtilitiesObject.find_deepest_descendant with invalid obj.""" |
| 256 |
|
| 257 |
self._setup_dependencies(test_context) |
| 258 |
from orca.ax_object import AXObject |
| 259 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 260 |
|
| 261 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 262 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 263 |
result = AXUtilitiesObject.find_deepest_descendant(mock_accessible) |
| 264 |
assert result is None |
| 265 |
|
| 266 |
def test_find_deepest_descendant_no_children(self, test_context: OrcaTestContext) -> None: |
| 267 |
"""Test AXUtilitiesObject.find_deepest_descendant with no children.""" |
| 268 |
|
| 269 |
self._setup_dependencies(test_context) |
| 270 |
from orca.ax_object import AXObject |
| 271 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 272 |
|
| 273 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 274 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 275 |
test_context.patch_object(AXObject, "get_child_count", return_value=0) |
| 276 |
test_context.patch_object(AXObject, "get_child", return_value=None) |
| 277 |
result = AXUtilitiesObject.find_deepest_descendant(mock_accessible) |
| 278 |
assert result == mock_accessible |
| 279 |
|
| 280 |
def test_find_deepest_descendant_with_children(self, test_context: OrcaTestContext) -> None: |
| 281 |
"""Test AXUtilitiesObject.find_deepest_descendant with nested children.""" |
| 282 |
|
| 283 |
self._setup_dependencies(test_context) |
| 284 |
from orca.ax_object import AXObject |
| 285 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 286 |
|
| 287 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 288 |
mock_child = test_context.Mock(spec=Atspi.Accessible) |
| 289 |
mock_grandchild = test_context.Mock(spec=Atspi.Accessible) |
| 290 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 291 |
|
| 292 |
def mock_get_child_count(obj) -> int: |
| 293 |
if obj == mock_accessible: |
| 294 |
return 2 |
| 295 |
if obj == mock_child: |
| 296 |
return 1 |
| 297 |
return 0 |
| 298 |
|
| 299 |
def mock_get_child(obj, index) -> object: |
| 300 |
if obj == mock_accessible and index == 1: |
| 301 |
return mock_child |
| 302 |
if obj == mock_child and index == 0: |
| 303 |
return mock_grandchild |
| 304 |
return None |
| 305 |
|
| 306 |
test_context.patch_object(AXObject, "get_child_count", new=mock_get_child_count) |
| 307 |
test_context.patch_object(AXObject, "get_child", new=mock_get_child) |
| 308 |
result = AXUtilitiesObject.find_deepest_descendant(mock_accessible) |
| 309 |
assert result == mock_grandchild |
| 310 |
|
| 311 |
def test_find_all_descendants_invalid_obj(self, test_context: OrcaTestContext) -> None: |
| 312 |
"""Test AXUtilitiesObject._find_all_descendants with invalid obj.""" |
| 313 |
|
| 314 |
self._setup_dependencies(test_context) |
| 315 |
from orca.ax_object import AXObject |
| 316 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 317 |
|
| 318 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 319 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 320 |
matches: list[Atspi.Accessible] = [] |
| 321 |
AXUtilitiesObject._find_all_descendants(mock_accessible, None, None, matches) |
| 322 |
assert not matches |
| 323 |
|
| 324 |
def test_find_all_descendants_with_include_filter( |
| 325 |
self, |
| 326 |
test_context: OrcaTestContext, |
| 327 |
) -> None: |
| 328 |
"""Test AXUtilitiesObject._find_all_descendants with include filter.""" |
| 329 |
|
| 330 |
self._setup_dependencies(test_context) |
| 331 |
from orca.ax_object import AXObject |
| 332 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 333 |
|
| 334 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 335 |
mock_child1 = test_context.Mock(spec=Atspi.Accessible) |
| 336 |
mock_child2 = test_context.Mock(spec=Atspi.Accessible) |
| 337 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 338 |
|
| 339 |
def mock_get_child_count(obj) -> int: |
| 340 |
if obj == mock_accessible: |
| 341 |
return 2 |
| 342 |
return 0 |
| 343 |
|
| 344 |
def mock_get_child(obj, idx) -> object: |
| 345 |
if obj == mock_accessible: |
| 346 |
if idx == 0: |
| 347 |
return mock_child1 |
| 348 |
if idx == 1: |
| 349 |
return mock_child2 |
| 350 |
return None |
| 351 |
|
| 352 |
test_context.patch_object(AXObject, "get_child_count", new=mock_get_child_count) |
| 353 |
test_context.patch_object(AXObject, "get_child", new=mock_get_child) |
| 354 |
|
| 355 |
def include_child1(obj) -> bool: |
| 356 |
return obj == mock_child1 |
| 357 |
|
| 358 |
matches: list[Atspi.Accessible] = [] |
| 359 |
AXUtilitiesObject._find_all_descendants(mock_accessible, include_child1, None, matches) |
| 360 |
assert matches == [mock_child1] |
| 361 |
|
| 362 |
def test_find_all_descendants_with_exclude_filter( |
| 363 |
self, |
| 364 |
test_context: OrcaTestContext, |
| 365 |
) -> None: |
| 366 |
"""Test AXUtilitiesObject._find_all_descendants with exclude filter.""" |
| 367 |
|
| 368 |
self._setup_dependencies(test_context) |
| 369 |
from orca.ax_object import AXObject |
| 370 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 371 |
|
| 372 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 373 |
mock_child1 = test_context.Mock(spec=Atspi.Accessible) |
| 374 |
mock_child2 = test_context.Mock(spec=Atspi.Accessible) |
| 375 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 376 |
|
| 377 |
def mock_get_child_count(obj) -> int: |
| 378 |
if obj == mock_accessible: |
| 379 |
return 2 |
| 380 |
return 0 |
| 381 |
|
| 382 |
def mock_get_child(obj, idx) -> object: |
| 383 |
if obj == mock_accessible: |
| 384 |
if idx == 0: |
| 385 |
return mock_child1 |
| 386 |
if idx == 1: |
| 387 |
return mock_child2 |
| 388 |
return None |
| 389 |
|
| 390 |
test_context.patch_object(AXObject, "get_child_count", new=mock_get_child_count) |
| 391 |
test_context.patch_object(AXObject, "get_child", new=mock_get_child) |
| 392 |
|
| 393 |
def include_all(_obj) -> bool: |
| 394 |
return True |
| 395 |
|
| 396 |
def exclude_child1(obj) -> bool: |
| 397 |
return obj == mock_child1 |
| 398 |
|
| 399 |
matches: list[Atspi.Accessible] = [] |
| 400 |
AXUtilitiesObject._find_all_descendants( |
| 401 |
mock_accessible, |
| 402 |
include_all, |
| 403 |
exclude_child1, |
| 404 |
matches, |
| 405 |
) |
| 406 |
assert matches == [mock_child2] |
| 407 |
|
| 408 |
def test_get_common_ancestor_with_none_objects(self, test_context: OrcaTestContext) -> None: |
| 409 |
"""Test AXUtilitiesObject.get_common_ancestor with None objects.""" |
| 410 |
|
| 411 |
self._setup_dependencies(test_context) |
| 412 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 413 |
|
| 414 |
result = AXUtilitiesObject.get_common_ancestor(None, None) |
| 415 |
assert result is None |
| 416 |
|
| 417 |
def test_get_common_ancestor_same_objects(self, test_context: OrcaTestContext) -> None: |
| 418 |
"""Test AXUtilitiesObject.get_common_ancestor with same objects.""" |
| 419 |
|
| 420 |
self._setup_dependencies(test_context) |
| 421 |
from orca.ax_object import AXObject |
| 422 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 423 |
|
| 424 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 425 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 426 |
result = AXUtilitiesObject.get_common_ancestor(mock_accessible, mock_accessible) |
| 427 |
assert result == mock_accessible |
| 428 |
|
| 429 |
def test_find_ancestor_found_in_parent(self, test_context: OrcaTestContext) -> None: |
| 430 |
"""Test AXUtilitiesObject.find_ancestor found in parent.""" |
| 431 |
|
| 432 |
self._setup_dependencies(test_context) |
| 433 |
from orca.ax_object import AXObject |
| 434 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 435 |
|
| 436 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 437 |
mock_parent = test_context.Mock(spec=Atspi.Accessible) |
| 438 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 439 |
|
| 440 |
def mock_get_parent_checked(obj) -> object: |
| 441 |
if obj == mock_accessible: |
| 442 |
return mock_parent |
| 443 |
return None |
| 444 |
|
| 445 |
test_context.patch_object(AXObject, "get_parent_checked", new=mock_get_parent_checked) |
| 446 |
|
| 447 |
def match_parent(obj) -> bool: |
| 448 |
return obj == mock_parent |
| 449 |
|
| 450 |
result = AXUtilitiesObject.find_ancestor(mock_accessible, match_parent) |
| 451 |
assert result == mock_parent |
| 452 |
|
| 453 |
def test_find_ancestor_found_in_grandparent(self, test_context: OrcaTestContext) -> None: |
| 454 |
"""Test AXUtilitiesObject.find_ancestor found in grandparent.""" |
| 455 |
|
| 456 |
self._setup_dependencies(test_context) |
| 457 |
from orca.ax_object import AXObject |
| 458 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 459 |
|
| 460 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 461 |
mock_parent = test_context.Mock(spec=Atspi.Accessible) |
| 462 |
mock_grandparent = test_context.Mock(spec=Atspi.Accessible) |
| 463 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 464 |
|
| 465 |
def mock_get_parent_checked(obj) -> object: |
| 466 |
if obj == mock_accessible: |
| 467 |
return mock_parent |
| 468 |
if obj == mock_parent: |
| 469 |
return mock_grandparent |
| 470 |
return None |
| 471 |
|
| 472 |
test_context.patch_object(AXObject, "get_parent_checked", new=mock_get_parent_checked) |
| 473 |
|
| 474 |
def match_grandparent(obj) -> bool: |
| 475 |
return obj == mock_grandparent |
| 476 |
|
| 477 |
result = AXUtilitiesObject.find_ancestor(mock_accessible, match_grandparent) |
| 478 |
assert result == mock_grandparent |
| 479 |
|
| 480 |
def test_find_ancestor_not_found(self, test_context: OrcaTestContext) -> None: |
| 481 |
"""Test AXUtilitiesObject.find_ancestor not found.""" |
| 482 |
|
| 483 |
self._setup_dependencies(test_context) |
| 484 |
from orca.ax_object import AXObject |
| 485 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 486 |
|
| 487 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 488 |
mock_parent = test_context.Mock(spec=Atspi.Accessible) |
| 489 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 490 |
|
| 491 |
def mock_get_parent_checked(obj) -> object: |
| 492 |
if obj == mock_accessible: |
| 493 |
return mock_parent |
| 494 |
return None |
| 495 |
|
| 496 |
test_context.patch_object(AXObject, "get_parent_checked", new=mock_get_parent_checked) |
| 497 |
|
| 498 |
def never_match(_obj) -> bool: |
| 499 |
return False |
| 500 |
|
| 501 |
result = AXUtilitiesObject.find_ancestor(mock_accessible, never_match) |
| 502 |
assert result is None |
| 503 |
|
| 504 |
def test_find_ancestor_no_parent(self, test_context: OrcaTestContext) -> None: |
| 505 |
"""Test AXUtilitiesObject.find_ancestor with no parent.""" |
| 506 |
|
| 507 |
self._setup_dependencies(test_context) |
| 508 |
from orca.ax_object import AXObject |
| 509 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 510 |
|
| 511 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 512 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 513 |
test_context.patch_object(AXObject, "get_parent_checked", return_value=None) |
| 514 |
|
| 515 |
def always_true(_obj) -> bool: |
| 516 |
return True |
| 517 |
|
| 518 |
result = AXUtilitiesObject.find_ancestor(mock_accessible, always_true) |
| 519 |
assert result is None |
| 520 |
|
| 521 |
def test_active_descendant_dead_obj(self, test_context: OrcaTestContext) -> None: |
| 522 |
"""Test AXUtilities.active_descendant with dead obj.""" |
| 523 |
|
| 524 |
self._setup_dependencies(test_context) |
| 525 |
from orca.ax_object import AXObject |
| 526 |
from orca.ax_utilities import AXUtilities |
| 527 |
|
| 528 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 529 |
test_context.patch_object(AXObject, "is_dead", return_value=True) |
| 530 |
result = AXUtilities.active_descendant(mock_accessible) |
| 531 |
assert result is None |
| 532 |
|
| 533 |
def test_active_descendant_non_table_cell(self, test_context: OrcaTestContext) -> None: |
| 534 |
"""Test AXUtilities.active_descendant with non-table-cell.""" |
| 535 |
|
| 536 |
self._setup_dependencies(test_context) |
| 537 |
from orca.ax_object import AXObject |
| 538 |
from orca.ax_utilities import AXUtilities |
| 539 |
from orca.ax_utilities_role import AXUtilitiesRole |
| 540 |
|
| 541 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 542 |
test_context.patch_object(AXObject, "is_dead", return_value=False) |
| 543 |
test_context.patch_object(AXUtilitiesRole, "is_table_cell", return_value=False) |
| 544 |
result = AXUtilities.active_descendant(mock_accessible) |
| 545 |
assert result == mock_accessible |
| 546 |
|
| 547 |
def test_active_descendant_table_cell_with_name(self, test_context: OrcaTestContext) -> None: |
| 548 |
"""Test AXUtilities.active_descendant with named table cell.""" |
| 549 |
|
| 550 |
self._setup_dependencies(test_context) |
| 551 |
from orca.ax_object import AXObject |
| 552 |
from orca.ax_utilities import AXUtilities |
| 553 |
from orca.ax_utilities_role import AXUtilitiesRole |
| 554 |
|
| 555 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 556 |
test_context.patch_object(AXObject, "is_dead", return_value=False) |
| 557 |
test_context.patch_object(AXUtilitiesRole, "is_table_cell", return_value=True) |
| 558 |
test_context.patch_object(AXObject, "get_name", return_value="Cell A1") |
| 559 |
result = AXUtilities.active_descendant(mock_accessible) |
| 560 |
assert result == mock_accessible |
| 561 |
|
| 562 |
def test_active_descendant_table_cell_with_descendant( |
| 563 |
self, |
| 564 |
test_context: OrcaTestContext, |
| 565 |
) -> None: |
| 566 |
"""Test AXUtilities.active_descendant with table cell having a named descendant.""" |
| 567 |
|
| 568 |
self._setup_dependencies(test_context) |
| 569 |
from orca.ax_object import AXObject |
| 570 |
from orca.ax_utilities import AXUtilities |
| 571 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 572 |
from orca.ax_utilities_role import AXUtilitiesRole |
| 573 |
|
| 574 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 575 |
mock_child = test_context.Mock(spec=Atspi.Accessible) |
| 576 |
test_context.patch_object(AXObject, "is_dead", return_value=False) |
| 577 |
test_context.patch_object(AXUtilitiesRole, "is_table_cell", return_value=True) |
| 578 |
test_context.patch_object( |
| 579 |
AXObject, |
| 580 |
"get_name", |
| 581 |
side_effect=lambda obj: "child_name" if obj == mock_child else "", |
| 582 |
) |
| 583 |
test_context.patch_object(AXUtilitiesObject, "find_descendant", return_value=mock_child) |
| 584 |
result = AXUtilities.active_descendant(mock_accessible) |
| 585 |
assert result == mock_child |
| 586 |
|
| 587 |
def test_find_previous_object_no_restriction(self, test_context: OrcaTestContext) -> None: |
| 588 |
"""Test AXUtilities.find_previous_object without restriction.""" |
| 589 |
|
| 590 |
self._setup_dependencies(test_context) |
| 591 |
from orca.ax_utilities import AXUtilities |
| 592 |
|
| 593 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 594 |
mock_previous = test_context.Mock(spec=Atspi.Accessible) |
| 595 |
test_context.patch_object( |
| 596 |
AXUtilities, |
| 597 |
"_get_previous_object", |
| 598 |
return_value=mock_previous, |
| 599 |
) |
| 600 |
result = AXUtilities.find_previous_object(mock_accessible) |
| 601 |
assert result == mock_previous |
| 602 |
|
| 603 |
def test_find_previous_object_with_restriction(self, test_context: OrcaTestContext) -> None: |
| 604 |
"""Test AXUtilities.find_previous_object with restriction.""" |
| 605 |
|
| 606 |
self._setup_dependencies(test_context) |
| 607 |
from orca.ax_utilities import AXUtilities |
| 608 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 609 |
|
| 610 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 611 |
mock_previous = test_context.Mock(spec=Atspi.Accessible) |
| 612 |
mock_restrict = test_context.Mock(spec=Atspi.Accessible) |
| 613 |
test_context.patch_object( |
| 614 |
AXUtilities, |
| 615 |
"_get_previous_object", |
| 616 |
return_value=mock_previous, |
| 617 |
) |
| 618 |
test_context.patch_object(AXUtilitiesObject, "is_ancestor", return_value=False) |
| 619 |
result = AXUtilities.find_previous_object(mock_accessible, mock_restrict) |
| 620 |
assert result is None |
| 621 |
|
| 622 |
def test_find_next_object_no_restriction(self, test_context: OrcaTestContext) -> None: |
| 623 |
"""Test AXUtilities.find_next_object without restriction.""" |
| 624 |
|
| 625 |
self._setup_dependencies(test_context) |
| 626 |
from orca.ax_utilities import AXUtilities |
| 627 |
|
| 628 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 629 |
mock_next = test_context.Mock(spec=Atspi.Accessible) |
| 630 |
test_context.patch_object(AXUtilities, "_get_next_object", return_value=mock_next) |
| 631 |
result = AXUtilities.find_next_object(mock_accessible) |
| 632 |
assert result == mock_next |
| 633 |
|
| 634 |
def test_find_next_object_with_restriction(self, test_context: OrcaTestContext) -> None: |
| 635 |
"""Test AXUtilities.find_next_object with restriction.""" |
| 636 |
|
| 637 |
self._setup_dependencies(test_context) |
| 638 |
from orca.ax_utilities import AXUtilities |
| 639 |
from orca.ax_utilities_object import AXUtilitiesObject |
| 640 |
|
| 641 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 642 |
mock_next = test_context.Mock(spec=Atspi.Accessible) |
| 643 |
mock_restrict = test_context.Mock(spec=Atspi.Accessible) |
| 644 |
test_context.patch_object(AXUtilities, "_get_next_object", return_value=mock_next) |
| 645 |
test_context.patch_object(AXUtilitiesObject, "is_ancestor", return_value=False) |
| 646 |
result = AXUtilities.find_next_object(mock_accessible, mock_restrict) |
| 647 |
assert result is None |
| 648 |
|
| 649 |
def test_get_next_object_invalid_obj(self, test_context: OrcaTestContext) -> None: |
| 650 |
"""Test AXUtilities._get_next_object with invalid obj.""" |
| 651 |
|
| 652 |
self._setup_dependencies(test_context) |
| 653 |
from orca.ax_object import AXObject |
| 654 |
from orca.ax_utilities import AXUtilities |
| 655 |
|
| 656 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 657 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 658 |
result = AXUtilities._get_next_object(mock_accessible) |
| 659 |
assert result is None |
| 660 |
|
| 661 |
def test_get_next_object_with_flows_to_relation(self, test_context: OrcaTestContext) -> None: |
| 662 |
"""Test AXUtilities._get_next_object with flows-to relation.""" |
| 663 |
|
| 664 |
self._setup_dependencies(test_context) |
| 665 |
from orca.ax_object import AXObject |
| 666 |
from orca.ax_utilities import AXUtilities |
| 667 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 668 |
|
| 669 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 670 |
mock_target = test_context.Mock(spec=Atspi.Accessible) |
| 671 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 672 |
test_context.patch_object(AXObject, "is_dead", return_value=False) |
| 673 |
test_context.patch_object( |
| 674 |
AXUtilitiesRelation, |
| 675 |
"get_flows_to", |
| 676 |
return_value=[mock_target], |
| 677 |
) |
| 678 |
result = AXUtilities._get_next_object(mock_obj) |
| 679 |
assert result == mock_target |
| 680 |
|
| 681 |
def test_get_next_object_with_dead_flows_to_targets( |
| 682 |
self, |
| 683 |
test_context: OrcaTestContext, |
| 684 |
) -> None: |
| 685 |
"""Test AXUtilities._get_next_object filters out dead flows-to targets.""" |
| 686 |
|
| 687 |
self._setup_dependencies(test_context) |
| 688 |
from orca.ax_object import AXObject |
| 689 |
from orca.ax_utilities import AXUtilities |
| 690 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 691 |
|
| 692 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 693 |
mock_dead_target = test_context.Mock(spec=Atspi.Accessible) |
| 694 |
mock_live_target = test_context.Mock(spec=Atspi.Accessible) |
| 695 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 696 |
test_context.patch_object( |
| 697 |
AXObject, |
| 698 |
"is_dead", |
| 699 |
side_effect=lambda obj: obj == mock_dead_target, |
| 700 |
) |
| 701 |
test_context.patch_object( |
| 702 |
AXUtilitiesRelation, |
| 703 |
"get_flows_to", |
| 704 |
return_value=[mock_dead_target, mock_live_target], |
| 705 |
) |
| 706 |
result = AXUtilities._get_next_object(mock_obj) |
| 707 |
assert result == mock_live_target |
| 708 |
|
| 709 |
def test_get_next_object_with_all_dead_flows_to_targets( |
| 710 |
self, |
| 711 |
test_context: OrcaTestContext, |
| 712 |
) -> None: |
| 713 |
"""Test AXUtilities._get_next_object falls back when all flows-to are dead.""" |
| 714 |
|
| 715 |
self._setup_dependencies(test_context) |
| 716 |
from orca.ax_object import AXObject |
| 717 |
from orca.ax_utilities import AXUtilities |
| 718 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 719 |
|
| 720 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 721 |
mock_dead_target1 = test_context.Mock(spec=Atspi.Accessible) |
| 722 |
mock_dead_target2 = test_context.Mock(spec=Atspi.Accessible) |
| 723 |
mock_parent = test_context.Mock(spec=Atspi.Accessible) |
| 724 |
mock_next = test_context.Mock(spec=Atspi.Accessible) |
| 725 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 726 |
test_context.patch_object(AXObject, "is_dead", return_value=True) |
| 727 |
test_context.patch_object(AXObject, "get_index_in_parent", return_value=0) |
| 728 |
test_context.patch_object(AXObject, "get_parent", return_value=mock_parent) |
| 729 |
test_context.patch_object(AXObject, "get_child_count", return_value=2) |
| 730 |
test_context.patch_object(AXObject, "get_child", return_value=mock_next) |
| 731 |
test_context.patch_object( |
| 732 |
AXUtilitiesRelation, |
| 733 |
"get_flows_to", |
| 734 |
return_value=[mock_dead_target1, mock_dead_target2], |
| 735 |
) |
| 736 |
result = AXUtilities._get_next_object(mock_obj) |
| 737 |
assert result == mock_next |
| 738 |
|
| 739 |
def test_get_next_object_normal_traversal(self, test_context: OrcaTestContext) -> None: |
| 740 |
"""Test AXUtilities._get_next_object with normal parent-child traversal.""" |
| 741 |
|
| 742 |
self._setup_dependencies(test_context) |
| 743 |
from orca.ax_object import AXObject |
| 744 |
from orca.ax_utilities import AXUtilities |
| 745 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 746 |
|
| 747 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 748 |
mock_parent = test_context.Mock(spec=Atspi.Accessible) |
| 749 |
mock_next = test_context.Mock(spec=Atspi.Accessible) |
| 750 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 751 |
test_context.patch_object(AXUtilitiesRelation, "get_flows_to", return_value=[]) |
| 752 |
test_context.patch_object(AXObject, "get_index_in_parent", return_value=0) |
| 753 |
test_context.patch_object(AXObject, "get_parent", return_value=mock_parent) |
| 754 |
test_context.patch_object(AXObject, "get_child_count", return_value=3) |
| 755 |
test_context.patch_object(AXObject, "get_child", return_value=mock_next) |
| 756 |
result = AXUtilities._get_next_object(mock_obj) |
| 757 |
assert result == mock_next |
| 758 |
|
| 759 |
def test_get_next_object_no_parent(self, test_context: OrcaTestContext) -> None: |
| 760 |
"""Test AXUtilities._get_next_object when no parent is found.""" |
| 761 |
|
| 762 |
self._setup_dependencies(test_context) |
| 763 |
from orca.ax_object import AXObject |
| 764 |
from orca.ax_utilities import AXUtilities |
| 765 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 766 |
|
| 767 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 768 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 769 |
test_context.patch_object(AXUtilitiesRelation, "get_flows_to", return_value=[]) |
| 770 |
test_context.patch_object(AXObject, "get_index_in_parent", return_value=2) |
| 771 |
test_context.patch_object(AXObject, "get_parent", return_value=None) |
| 772 |
result = AXUtilities._get_next_object(mock_obj) |
| 773 |
assert result is None |
| 774 |
|
| 775 |
def test_get_previous_object_invalid_obj(self, test_context: OrcaTestContext) -> None: |
| 776 |
"""Test AXUtilities._get_previous_object with invalid obj.""" |
| 777 |
|
| 778 |
self._setup_dependencies(test_context) |
| 779 |
from orca.ax_object import AXObject |
| 780 |
from orca.ax_utilities import AXUtilities |
| 781 |
|
| 782 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 783 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 784 |
result = AXUtilities._get_previous_object(mock_accessible) |
| 785 |
assert result is None |
| 786 |
|
| 787 |
def test_get_previous_object_with_flows_from_relation( |
| 788 |
self, |
| 789 |
test_context: OrcaTestContext, |
| 790 |
) -> None: |
| 791 |
"""Test AXUtilities._get_previous_object with flows-from relation.""" |
| 792 |
|
| 793 |
self._setup_dependencies(test_context) |
| 794 |
from orca.ax_object import AXObject |
| 795 |
from orca.ax_utilities import AXUtilities |
| 796 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 797 |
|
| 798 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 799 |
mock_source = test_context.Mock(spec=Atspi.Accessible) |
| 800 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 801 |
test_context.patch_object(AXObject, "is_dead", return_value=False) |
| 802 |
test_context.patch_object( |
| 803 |
AXUtilitiesRelation, |
| 804 |
"get_flows_from", |
| 805 |
return_value=[mock_source], |
| 806 |
) |
| 807 |
result = AXUtilities._get_previous_object(mock_obj) |
| 808 |
assert result == mock_source |
| 809 |
|
| 810 |
def test_get_previous_object_with_dead_flows_from_targets( |
| 811 |
self, |
| 812 |
test_context: OrcaTestContext, |
| 813 |
) -> None: |
| 814 |
"""Test AXUtilities._get_previous_object filters out dead flows-from targets.""" |
| 815 |
|
| 816 |
self._setup_dependencies(test_context) |
| 817 |
from orca.ax_object import AXObject |
| 818 |
from orca.ax_utilities import AXUtilities |
| 819 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 820 |
|
| 821 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 822 |
mock_dead_source = test_context.Mock(spec=Atspi.Accessible) |
| 823 |
mock_live_source = test_context.Mock(spec=Atspi.Accessible) |
| 824 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 825 |
test_context.patch_object( |
| 826 |
AXObject, |
| 827 |
"is_dead", |
| 828 |
side_effect=lambda obj: obj == mock_dead_source, |
| 829 |
) |
| 830 |
test_context.patch_object( |
| 831 |
AXUtilitiesRelation, |
| 832 |
"get_flows_from", |
| 833 |
return_value=[mock_dead_source, mock_live_source], |
| 834 |
) |
| 835 |
result = AXUtilities._get_previous_object(mock_obj) |
| 836 |
assert result == mock_live_source |
| 837 |
|
| 838 |
def test_get_previous_object_with_all_dead_flows_from_targets( |
| 839 |
self, |
| 840 |
test_context: OrcaTestContext, |
| 841 |
) -> None: |
| 842 |
"""Test AXUtilities._get_previous_object falls back when all flows-from are dead.""" |
| 843 |
|
| 844 |
self._setup_dependencies(test_context) |
| 845 |
from orca.ax_object import AXObject |
| 846 |
from orca.ax_utilities import AXUtilities |
| 847 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 848 |
|
| 849 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 850 |
mock_dead_source1 = test_context.Mock(spec=Atspi.Accessible) |
| 851 |
mock_dead_source2 = test_context.Mock(spec=Atspi.Accessible) |
| 852 |
mock_parent = test_context.Mock(spec=Atspi.Accessible) |
| 853 |
mock_previous = test_context.Mock(spec=Atspi.Accessible) |
| 854 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 855 |
test_context.patch_object(AXObject, "is_dead", return_value=True) |
| 856 |
test_context.patch_object(AXObject, "get_index_in_parent", return_value=1) |
| 857 |
test_context.patch_object(AXObject, "get_parent", return_value=mock_parent) |
| 858 |
test_context.patch_object(AXObject, "get_child_count", return_value=2) |
| 859 |
test_context.patch_object(AXObject, "get_child", return_value=mock_previous) |
| 860 |
test_context.patch_object( |
| 861 |
AXUtilitiesRelation, |
| 862 |
"get_flows_from", |
| 863 |
return_value=[mock_dead_source1, mock_dead_source2], |
| 864 |
) |
| 865 |
result = AXUtilities._get_previous_object(mock_obj) |
| 866 |
assert result == mock_previous |
| 867 |
|
| 868 |
def test_get_previous_object_no_parent(self, test_context: OrcaTestContext) -> None: |
| 869 |
"""Test AXUtilities._get_previous_object when no parent is found.""" |
| 870 |
|
| 871 |
self._setup_dependencies(test_context) |
| 872 |
from orca.ax_object import AXObject |
| 873 |
from orca.ax_utilities import AXUtilities |
| 874 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 875 |
|
| 876 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 877 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 878 |
test_context.patch_object(AXUtilitiesRelation, "get_flows_from", return_value=[]) |
| 879 |
test_context.patch_object(AXObject, "get_index_in_parent", return_value=0) |
| 880 |
test_context.patch_object(AXObject, "get_parent", return_value=None) |
| 881 |
result = AXUtilities._get_previous_object(mock_obj) |
| 882 |
assert result is None |