| 1 |
# Unit tests for ax_object.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=too-many-public-methods |
| 22 |
# pylint: disable=wrong-import-position |
| 23 |
# pylint: disable=protected-access |
| 24 |
# pylint: disable=too-many-arguments |
| 25 |
# pylint: disable=too-many-positional-arguments |
| 26 |
# pylint: disable=import-outside-toplevel |
| 27 |
# pylint: disable=too-many-lines |
| 28 |
# pylint: disable=wrong-import-order |
| 29 |
|
| 30 |
"""Unit tests for ax_object.py methods.""" |
| 31 |
|
| 32 |
from __future__ import annotations |
| 33 |
|
| 34 |
import sys |
| 35 |
from typing import TYPE_CHECKING |
| 36 |
|
| 37 |
import gi |
| 38 |
import pytest |
| 39 |
|
| 40 |
gi.require_version("Atspi", "2.0") |
| 41 |
from gi.repository import Atspi, GLib |
| 42 |
|
| 43 |
if TYPE_CHECKING: |
| 44 |
from unittest.mock import MagicMock |
| 45 |
|
| 46 |
from .orca_test_context import OrcaTestContext |
| 47 |
|
| 48 |
|
| 49 |
@pytest.mark.unit |
| 50 |
class TestAXObject: |
| 51 |
"""Test AXObject class methods.""" |
| 52 |
|
| 53 |
def _setup_soffice_collection_mocks( |
| 54 |
self, |
| 55 |
test_context, |
| 56 |
has_document_text=False, |
| 57 |
has_spreadsheet=False, |
| 58 |
): |
| 59 |
"""Set up common mocks for soffice collection testing scenarios.""" |
| 60 |
|
| 61 |
from orca.ax_object import AXObject |
| 62 |
|
| 63 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 64 |
mock_app = test_context.Mock(spec=Atspi.Accessible) |
| 65 |
|
| 66 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 67 |
test_context.patch_object(AXObject, "get_name", return_value="soffice") |
| 68 |
test_context.patch_object(Atspi.Accessible, "get_application", return_value=mock_app) |
| 69 |
test_context.patch_object( |
| 70 |
Atspi.Accessible, |
| 71 |
"get_collection_iface", |
| 72 |
side_effect=lambda obj: test_context.Mock(), |
| 73 |
) |
| 74 |
|
| 75 |
if has_document_text: |
| 76 |
mock_ancestor = test_context.Mock(spec=Atspi.Accessible) |
| 77 |
test_context.patch_object( |
| 78 |
AXObject, |
| 79 |
"_find_ancestor_with_role", |
| 80 |
side_effect=lambda obj, role: mock_ancestor |
| 81 |
if role == Atspi.Role.DOCUMENT_TEXT |
| 82 |
else None, |
| 83 |
) |
| 84 |
else: |
| 85 |
test_context.patch_object(AXObject, "_find_ancestor_with_role", return_value=None) |
| 86 |
if has_spreadsheet: |
| 87 |
test_context.patch_object(AXObject, "_has_document_spreadsheet", return_value=True) |
| 88 |
else: |
| 89 |
test_context.patch_object(AXObject, "_has_document_spreadsheet", return_value=False) |
| 90 |
|
| 91 |
return mock_accessible |
| 92 |
|
| 93 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 94 |
"""Set up mocks for ax_object dependencies.""" |
| 95 |
|
| 96 |
core_modules = [ |
| 97 |
"orca.debug", |
| 98 |
"orca.messages", |
| 99 |
"orca.input_event", |
| 100 |
"orca.keybindings", |
| 101 |
"orca.cmdnames", |
| 102 |
"orca.dbus_service", |
| 103 |
"orca.script_manager", |
| 104 |
"orca.orca_i18n", |
| 105 |
"orca.guilabels", |
| 106 |
"orca.text_attribute_names", |
| 107 |
"orca.focus_manager", |
| 108 |
"orca.braille", |
| 109 |
"orca.keynames", |
| 110 |
] |
| 111 |
|
| 112 |
essential_modules = {} |
| 113 |
for module_name in core_modules: |
| 114 |
mock_module = test_context.Mock() |
| 115 |
test_context.patch_module(module_name, mock_module) |
| 116 |
essential_modules[module_name] = mock_module |
| 117 |
|
| 118 |
test_context.configure_shared_module_behaviors(essential_modules) |
| 119 |
keynames_mock = essential_modules["orca.keynames"] |
| 120 |
keynames_mock.KEY_BACKSPACE = "BackSpace" |
| 121 |
keynames_mock.KEY_DELETE = "Delete" |
| 122 |
keynames_mock.KEY_TAB = "Tab" |
| 123 |
keynames_mock.KEY_RETURN = "Return" |
| 124 |
keynames_mock.KEY_ESCAPE = "Escape" |
| 125 |
|
| 126 |
return essential_modules |
| 127 |
|
| 128 |
@pytest.mark.parametrize( |
| 129 |
"case", |
| 130 |
[ |
| 131 |
{ |
| 132 |
"id": "gecko_section_hack", |
| 133 |
"test_type": "gecko_section_hack", |
| 134 |
"obj_role": Atspi.Role.SECTION, |
| 135 |
"parent_role": Atspi.Role.FRAME, |
| 136 |
"toolkit": "gecko", |
| 137 |
"expected_result": True, |
| 138 |
}, |
| 139 |
{ |
| 140 |
"id": "normal_object", |
| 141 |
"test_type": "normal_object", |
| 142 |
"obj_role": Atspi.Role.BUTTON, |
| 143 |
"parent_role": None, |
| 144 |
"toolkit": "gtk", |
| 145 |
"expected_result": False, |
| 146 |
}, |
| 147 |
], |
| 148 |
ids=lambda case: case["id"], |
| 149 |
) |
| 150 |
def test_is_bogus( |
| 151 |
self, |
| 152 |
test_context: OrcaTestContext, |
| 153 |
case: dict, |
| 154 |
) -> None: |
| 155 |
"""Test AXObject.is_bogus with various object types.""" |
| 156 |
|
| 157 |
self._setup_dependencies(test_context) |
| 158 |
from orca.ax_object import AXObject |
| 159 |
|
| 160 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 161 |
mock_parent = test_context.Mock(spec=Atspi.Accessible) if case["parent_role"] else None |
| 162 |
|
| 163 |
if case["test_type"] == "gecko_section_hack": |
| 164 |
mock_get_role = test_context.Mock( |
| 165 |
side_effect=lambda obj: case["obj_role"] |
| 166 |
if obj == mock_accessible |
| 167 |
else case["parent_role"], |
| 168 |
) |
| 169 |
else: |
| 170 |
mock_get_role = test_context.Mock(return_value=case["obj_role"]) |
| 171 |
|
| 172 |
mock_get_parent = test_context.Mock(return_value=mock_parent) |
| 173 |
mock_get_toolkit = test_context.Mock(return_value=case["toolkit"]) |
| 174 |
test_context.patch_object(AXObject, "get_role", new=mock_get_role) |
| 175 |
test_context.patch_object(AXObject, "get_parent", new=mock_get_parent) |
| 176 |
test_context.patch_object(AXObject, "get_toolkit_name", new=mock_get_toolkit) |
| 177 |
|
| 178 |
result = AXObject.is_bogus(mock_accessible) |
| 179 |
assert result is case["expected_result"] |
| 180 |
|
| 181 |
@pytest.mark.parametrize( |
| 182 |
"case", |
| 183 |
[ |
| 184 |
{ |
| 185 |
"id": "qt_bug", |
| 186 |
"toolkit": "qt", |
| 187 |
"obj_type": "mock", |
| 188 |
"parent_role": "window", |
| 189 |
"has_grandparent": False, |
| 190 |
"expected_result": True, |
| 191 |
"expects_debug": True, |
| 192 |
}, |
| 193 |
{ |
| 194 |
"id": "good_qt_ancestry", |
| 195 |
"toolkit": "qt", |
| 196 |
"obj_type": "mock", |
| 197 |
"parent_role": "button", |
| 198 |
"has_grandparent": True, |
| 199 |
"expected_result": False, |
| 200 |
"expects_debug": False, |
| 201 |
}, |
| 202 |
{ |
| 203 |
"id": "non_qt", |
| 204 |
"toolkit": "gtk", |
| 205 |
"obj_type": "mock", |
| 206 |
"parent_role": "button", |
| 207 |
"has_grandparent": False, |
| 208 |
"expected_result": False, |
| 209 |
"expects_debug": False, |
| 210 |
}, |
| 211 |
{ |
| 212 |
"id": "none_object", |
| 213 |
"toolkit": "qt", |
| 214 |
"obj_type": "none", |
| 215 |
"parent_role": "button", |
| 216 |
"has_grandparent": False, |
| 217 |
"expected_result": False, |
| 218 |
"expects_debug": False, |
| 219 |
}, |
| 220 |
], |
| 221 |
ids=lambda case: case["id"], |
| 222 |
) |
| 223 |
def test_has_broken_ancestry( |
| 224 |
self, |
| 225 |
test_context: OrcaTestContext, |
| 226 |
case: dict, |
| 227 |
) -> None: |
| 228 |
"""Test AXObject.has_broken_ancestry with various scenarios.""" |
| 229 |
|
| 230 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 231 |
from orca.ax_object import AXObject |
| 232 |
|
| 233 |
if case["obj_type"] == "none": |
| 234 |
result = AXObject.has_broken_ancestry(None) |
| 235 |
assert result is case["expected_result"] |
| 236 |
return |
| 237 |
|
| 238 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 239 |
test_context.patch_object( |
| 240 |
AXObject, |
| 241 |
"get_toolkit_name", |
| 242 |
side_effect=lambda obj: case["toolkit"], |
| 243 |
) |
| 244 |
|
| 245 |
if case["toolkit"] == "qt": |
| 246 |
mock_parent = test_context.Mock(spec=Atspi.Accessible) |
| 247 |
|
| 248 |
def mock_get_parent(obj) -> object: |
| 249 |
if obj == mock_accessible: |
| 250 |
return mock_parent |
| 251 |
if case["has_grandparent"] and obj == mock_parent: |
| 252 |
mock_app = test_context.Mock(spec=Atspi.Accessible) |
| 253 |
return mock_app |
| 254 |
return None |
| 255 |
|
| 256 |
def mock_get_role(obj) -> object: |
| 257 |
if obj == mock_parent and case["parent_role"] == "window": |
| 258 |
return Atspi.Role.WINDOW |
| 259 |
if case["has_grandparent"] and obj not in (mock_accessible, mock_parent): |
| 260 |
return Atspi.Role.APPLICATION |
| 261 |
return Atspi.Role.BUTTON |
| 262 |
|
| 263 |
test_context.patch_object(AXObject, "get_parent", new=mock_get_parent) |
| 264 |
test_context.patch_object(AXObject, "get_role", new=mock_get_role) |
| 265 |
|
| 266 |
result = AXObject.has_broken_ancestry(mock_accessible) |
| 267 |
assert result is case["expected_result"] |
| 268 |
|
| 269 |
if case["expects_debug"]: |
| 270 |
essential_modules["orca.debug"].print_tokens.assert_called() |
| 271 |
|
| 272 |
@pytest.mark.parametrize( |
| 273 |
"case", |
| 274 |
[ |
| 275 |
{ |
| 276 |
"id": "successful", |
| 277 |
"toolkit_result": "GTK", |
| 278 |
"expects_error": False, |
| 279 |
"expected_result": "gtk", |
| 280 |
"expects_debug": False, |
| 281 |
}, |
| 282 |
{ |
| 283 |
"id": "glib_error", |
| 284 |
"toolkit_result": GLib.GError("Test error"), |
| 285 |
"expects_error": True, |
| 286 |
"expected_result": "", |
| 287 |
"expects_debug": True, |
| 288 |
}, |
| 289 |
{ |
| 290 |
"id": "none_result", |
| 291 |
"toolkit_result": None, |
| 292 |
"expects_error": False, |
| 293 |
"expected_result": "", |
| 294 |
"expects_debug": False, |
| 295 |
}, |
| 296 |
], |
| 297 |
ids=lambda case: case["id"], |
| 298 |
) |
| 299 |
def test_get_toolkit_name( |
| 300 |
self, |
| 301 |
test_context: OrcaTestContext, |
| 302 |
case: dict, |
| 303 |
) -> None: |
| 304 |
"""Test AXObject.get_toolkit_name with various scenarios.""" |
| 305 |
|
| 306 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 307 |
from orca.ax_object import AXObject |
| 308 |
|
| 309 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 310 |
mock_app = test_context.Mock(spec=Atspi.Accessible) |
| 311 |
test_context.patch_object(Atspi.Accessible, "get_application", return_value=mock_app) |
| 312 |
|
| 313 |
if case["expects_error"]: |
| 314 |
|
| 315 |
def raise_glib_error(app): |
| 316 |
raise case["toolkit_result"] |
| 317 |
|
| 318 |
test_context.patch_object( |
| 319 |
Atspi.Accessible, |
| 320 |
"get_toolkit_name", |
| 321 |
side_effect=raise_glib_error, |
| 322 |
) |
| 323 |
from orca import debug |
| 324 |
|
| 325 |
test_context.patch_object( |
| 326 |
debug, |
| 327 |
"print_tokens", |
| 328 |
new=essential_modules["orca.debug"].print_tokens, |
| 329 |
) |
| 330 |
else: |
| 331 |
test_context.patch_object( |
| 332 |
Atspi.Accessible, |
| 333 |
"get_toolkit_name", |
| 334 |
side_effect=lambda app: case["toolkit_result"], |
| 335 |
) |
| 336 |
|
| 337 |
result = AXObject.get_toolkit_name(mock_accessible) |
| 338 |
assert result == case["expected_result"] |
| 339 |
|
| 340 |
if case["expects_debug"]: |
| 341 |
essential_modules["orca.debug"].print_tokens.assert_called() |
| 342 |
|
| 343 |
@pytest.mark.parametrize( |
| 344 |
"case", |
| 345 |
[ |
| 346 |
{ |
| 347 |
"id": "supports_action", |
| 348 |
"interface_type": "action", |
| 349 |
"interface_result": "mock_interface", |
| 350 |
"raises_error": False, |
| 351 |
"expected_result": True, |
| 352 |
}, |
| 353 |
{ |
| 354 |
"id": "no_action_interface", |
| 355 |
"interface_type": "action", |
| 356 |
"interface_result": None, |
| 357 |
"raises_error": False, |
| 358 |
"expected_result": False, |
| 359 |
}, |
| 360 |
{ |
| 361 |
"id": "action_glib_error", |
| 362 |
"interface_type": "action", |
| 363 |
"interface_result": None, |
| 364 |
"raises_error": True, |
| 365 |
"expected_result": False, |
| 366 |
}, |
| 367 |
{ |
| 368 |
"id": "supports_component", |
| 369 |
"interface_type": "component", |
| 370 |
"interface_result": "mock_interface", |
| 371 |
"raises_error": False, |
| 372 |
"expected_result": True, |
| 373 |
}, |
| 374 |
{ |
| 375 |
"id": "no_component_interface", |
| 376 |
"interface_type": "component", |
| 377 |
"interface_result": None, |
| 378 |
"raises_error": False, |
| 379 |
"expected_result": False, |
| 380 |
}, |
| 381 |
{ |
| 382 |
"id": "component_glib_error", |
| 383 |
"interface_type": "component", |
| 384 |
"interface_result": None, |
| 385 |
"raises_error": True, |
| 386 |
"expected_result": False, |
| 387 |
}, |
| 388 |
{ |
| 389 |
"id": "supports_document", |
| 390 |
"interface_type": "document", |
| 391 |
"interface_result": "mock_interface", |
| 392 |
"raises_error": False, |
| 393 |
"expected_result": True, |
| 394 |
}, |
| 395 |
{ |
| 396 |
"id": "no_document_interface", |
| 397 |
"interface_type": "document", |
| 398 |
"interface_result": None, |
| 399 |
"raises_error": False, |
| 400 |
"expected_result": False, |
| 401 |
}, |
| 402 |
{ |
| 403 |
"id": "document_glib_error", |
| 404 |
"interface_type": "document", |
| 405 |
"interface_result": None, |
| 406 |
"raises_error": True, |
| 407 |
"expected_result": False, |
| 408 |
}, |
| 409 |
{ |
| 410 |
"id": "supports_editable_text", |
| 411 |
"interface_type": "editable_text", |
| 412 |
"interface_result": "mock_interface", |
| 413 |
"raises_error": False, |
| 414 |
"expected_result": True, |
| 415 |
}, |
| 416 |
{ |
| 417 |
"id": "no_editable_text_interface", |
| 418 |
"interface_type": "editable_text", |
| 419 |
"interface_result": None, |
| 420 |
"raises_error": False, |
| 421 |
"expected_result": False, |
| 422 |
}, |
| 423 |
{ |
| 424 |
"id": "editable_text_glib_error", |
| 425 |
"interface_type": "editable_text", |
| 426 |
"interface_result": None, |
| 427 |
"raises_error": True, |
| 428 |
"expected_result": False, |
| 429 |
}, |
| 430 |
{ |
| 431 |
"id": "supports_hyperlink", |
| 432 |
"interface_type": "hyperlink", |
| 433 |
"interface_result": "mock_interface", |
| 434 |
"raises_error": False, |
| 435 |
"expected_result": True, |
| 436 |
}, |
| 437 |
{ |
| 438 |
"id": "no_hyperlink_interface", |
| 439 |
"interface_type": "hyperlink", |
| 440 |
"interface_result": None, |
| 441 |
"raises_error": False, |
| 442 |
"expected_result": False, |
| 443 |
}, |
| 444 |
{ |
| 445 |
"id": "hyperlink_glib_error", |
| 446 |
"interface_type": "hyperlink", |
| 447 |
"interface_result": None, |
| 448 |
"raises_error": True, |
| 449 |
"expected_result": False, |
| 450 |
}, |
| 451 |
{ |
| 452 |
"id": "supports_hypertext", |
| 453 |
"interface_type": "hypertext", |
| 454 |
"interface_result": "mock_interface", |
| 455 |
"raises_error": False, |
| 456 |
"expected_result": True, |
| 457 |
}, |
| 458 |
{ |
| 459 |
"id": "no_hypertext_interface", |
| 460 |
"interface_type": "hypertext", |
| 461 |
"interface_result": None, |
| 462 |
"raises_error": False, |
| 463 |
"expected_result": False, |
| 464 |
}, |
| 465 |
{ |
| 466 |
"id": "hypertext_glib_error", |
| 467 |
"interface_type": "hypertext", |
| 468 |
"interface_result": None, |
| 469 |
"raises_error": True, |
| 470 |
"expected_result": False, |
| 471 |
}, |
| 472 |
{ |
| 473 |
"id": "supports_image", |
| 474 |
"interface_type": "image", |
| 475 |
"interface_result": "mock_interface", |
| 476 |
"raises_error": False, |
| 477 |
"expected_result": True, |
| 478 |
}, |
| 479 |
{ |
| 480 |
"id": "no_image_interface", |
| 481 |
"interface_type": "image", |
| 482 |
"interface_result": None, |
| 483 |
"raises_error": False, |
| 484 |
"expected_result": False, |
| 485 |
}, |
| 486 |
{ |
| 487 |
"id": "image_glib_error", |
| 488 |
"interface_type": "image", |
| 489 |
"interface_result": None, |
| 490 |
"raises_error": True, |
| 491 |
"expected_result": False, |
| 492 |
}, |
| 493 |
{ |
| 494 |
"id": "supports_selection", |
| 495 |
"interface_type": "selection", |
| 496 |
"interface_result": "mock_interface", |
| 497 |
"raises_error": False, |
| 498 |
"expected_result": True, |
| 499 |
}, |
| 500 |
{ |
| 501 |
"id": "no_selection_interface", |
| 502 |
"interface_type": "selection", |
| 503 |
"interface_result": None, |
| 504 |
"raises_error": False, |
| 505 |
"expected_result": False, |
| 506 |
}, |
| 507 |
{ |
| 508 |
"id": "selection_glib_error", |
| 509 |
"interface_type": "selection", |
| 510 |
"interface_result": None, |
| 511 |
"raises_error": True, |
| 512 |
"expected_result": False, |
| 513 |
}, |
| 514 |
{ |
| 515 |
"id": "supports_table", |
| 516 |
"interface_type": "table", |
| 517 |
"interface_result": "mock_interface", |
| 518 |
"raises_error": False, |
| 519 |
"expected_result": True, |
| 520 |
}, |
| 521 |
{ |
| 522 |
"id": "no_table_interface", |
| 523 |
"interface_type": "table", |
| 524 |
"interface_result": None, |
| 525 |
"raises_error": False, |
| 526 |
"expected_result": False, |
| 527 |
}, |
| 528 |
{ |
| 529 |
"id": "table_glib_error", |
| 530 |
"interface_type": "table", |
| 531 |
"interface_result": None, |
| 532 |
"raises_error": True, |
| 533 |
"expected_result": False, |
| 534 |
}, |
| 535 |
{ |
| 536 |
"id": "supports_table_cell", |
| 537 |
"interface_type": "table_cell", |
| 538 |
"interface_result": "mock_interface", |
| 539 |
"raises_error": False, |
| 540 |
"expected_result": True, |
| 541 |
}, |
| 542 |
{ |
| 543 |
"id": "no_table_cell_interface", |
| 544 |
"interface_type": "table_cell", |
| 545 |
"interface_result": None, |
| 546 |
"raises_error": False, |
| 547 |
"expected_result": False, |
| 548 |
}, |
| 549 |
{ |
| 550 |
"id": "table_cell_glib_error", |
| 551 |
"interface_type": "table_cell", |
| 552 |
"interface_result": None, |
| 553 |
"raises_error": True, |
| 554 |
"expected_result": False, |
| 555 |
}, |
| 556 |
{ |
| 557 |
"id": "supports_text", |
| 558 |
"interface_type": "text", |
| 559 |
"interface_result": "mock_interface", |
| 560 |
"raises_error": False, |
| 561 |
"expected_result": True, |
| 562 |
}, |
| 563 |
{ |
| 564 |
"id": "no_text_interface", |
| 565 |
"interface_type": "text", |
| 566 |
"interface_result": None, |
| 567 |
"raises_error": False, |
| 568 |
"expected_result": False, |
| 569 |
}, |
| 570 |
{ |
| 571 |
"id": "text_glib_error", |
| 572 |
"interface_type": "text", |
| 573 |
"interface_result": None, |
| 574 |
"raises_error": True, |
| 575 |
"expected_result": False, |
| 576 |
}, |
| 577 |
{ |
| 578 |
"id": "supports_value", |
| 579 |
"interface_type": "value", |
| 580 |
"interface_result": "mock_interface", |
| 581 |
"raises_error": False, |
| 582 |
"expected_result": True, |
| 583 |
}, |
| 584 |
{ |
| 585 |
"id": "no_value_interface", |
| 586 |
"interface_type": "value", |
| 587 |
"interface_result": None, |
| 588 |
"raises_error": False, |
| 589 |
"expected_result": False, |
| 590 |
}, |
| 591 |
{ |
| 592 |
"id": "value_glib_error", |
| 593 |
"interface_type": "value", |
| 594 |
"interface_result": None, |
| 595 |
"raises_error": True, |
| 596 |
"expected_result": False, |
| 597 |
}, |
| 598 |
], |
| 599 |
ids=lambda case: case["id"], |
| 600 |
) |
| 601 |
def test_supports_interface_parametrized( |
| 602 |
self, |
| 603 |
test_context: OrcaTestContext, |
| 604 |
case: dict, |
| 605 |
) -> None: |
| 606 |
"""Test AXObject.supports_* interface methods with various scenarios.""" |
| 607 |
|
| 608 |
self._setup_dependencies(test_context) |
| 609 |
from orca.ax_object import AXObject |
| 610 |
|
| 611 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 612 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 613 |
|
| 614 |
interface_map = { |
| 615 |
"action": ("get_action_iface", AXObject.supports_action), |
| 616 |
"component": ("get_component_iface", AXObject.supports_component), |
| 617 |
"document": ("get_document_iface", AXObject.supports_document), |
| 618 |
"editable_text": ("get_editable_text_iface", AXObject.supports_editable_text), |
| 619 |
"hyperlink": ("get_hyperlink", AXObject.supports_hyperlink), |
| 620 |
"hypertext": ("get_hypertext_iface", AXObject.supports_hypertext), |
| 621 |
"image": ("get_image_iface", AXObject.supports_image), |
| 622 |
"selection": ("get_selection_iface", AXObject.supports_selection), |
| 623 |
"table": ("get_table_iface", AXObject.supports_table), |
| 624 |
"table_cell": ("get_table_cell", AXObject.supports_table_cell), |
| 625 |
"text": ("get_text_iface", AXObject.supports_text), |
| 626 |
"value": ("get_value_iface", AXObject.supports_value), |
| 627 |
} |
| 628 |
|
| 629 |
getter_name, support_method = interface_map[case["interface_type"]] |
| 630 |
|
| 631 |
if case["raises_error"]: |
| 632 |
|
| 633 |
def raise_glib_error(_obj) -> None: |
| 634 |
raise GLib.GError("Test error") |
| 635 |
|
| 636 |
test_context.patch_object(Atspi.Accessible, getter_name, side_effect=raise_glib_error) |
| 637 |
handle_error_mock = test_context.Mock() |
| 638 |
test_context.patch_object(AXObject, "handle_error", new=handle_error_mock) |
| 639 |
else: |
| 640 |
actual_interface_result = ( |
| 641 |
test_context.Mock() |
| 642 |
if case["interface_result"] == "mock_interface" |
| 643 |
else case["interface_result"] |
| 644 |
) |
| 645 |
test_context.patch_object( |
| 646 |
Atspi.Accessible, |
| 647 |
getter_name, |
| 648 |
return_value=actual_interface_result, |
| 649 |
) |
| 650 |
|
| 651 |
result = support_method(mock_accessible) |
| 652 |
assert result is case["expected_result"] |
| 653 |
|
| 654 |
if case["raises_error"]: |
| 655 |
handle_error_mock.assert_called_once() |
| 656 |
|
| 657 |
@pytest.mark.parametrize( |
| 658 |
"case", |
| 659 |
[ |
| 660 |
{ |
| 661 |
"id": "with_reason", |
| 662 |
"reason": "test reason", |
| 663 |
"expected_message": "AXObject: Clearing local cache. Reason: test reason", |
| 664 |
}, |
| 665 |
{ |
| 666 |
"id": "without_reason", |
| 667 |
"reason": None, |
| 668 |
"expected_message": "AXObject: Clearing local cache.", |
| 669 |
}, |
| 670 |
], |
| 671 |
ids=lambda case: case["id"], |
| 672 |
) |
| 673 |
def test_clear_all_dictionaries(self, test_context: OrcaTestContext, case: dict) -> None: |
| 674 |
"""Test AXObject._clear_all_dictionaries with and without reason.""" |
| 675 |
|
| 676 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 677 |
from orca.ax_object import AXObject |
| 678 |
|
| 679 |
AXObject.KNOWN_DEAD[123] = True |
| 680 |
AXObject.OBJECT_ATTRIBUTES[456] = {"role": "button"} |
| 681 |
|
| 682 |
if case["reason"]: |
| 683 |
AXObject._clear_all_dictionaries(case["reason"]) |
| 684 |
else: |
| 685 |
AXObject._clear_all_dictionaries() |
| 686 |
|
| 687 |
assert len(AXObject.KNOWN_DEAD) == 0 |
| 688 |
assert len(AXObject.OBJECT_ATTRIBUTES) == 0 |
| 689 |
essential_modules["orca.debug"].print_message.assert_called_with( |
| 690 |
essential_modules["orca.debug"].LEVEL_INFO, |
| 691 |
case["expected_message"], |
| 692 |
True, |
| 693 |
) |
| 694 |
|
| 695 |
def test_clear_cache_now_calls_clear_all_dictionaries( |
| 696 |
self, |
| 697 |
test_context: OrcaTestContext, |
| 698 |
) -> None: |
| 699 |
"""Test AXObject.clear_cache_now calls _clear_all_dictionaries.""" |
| 700 |
|
| 701 |
self._setup_dependencies(test_context) |
| 702 |
from orca.ax_object import AXObject |
| 703 |
|
| 704 |
clear_mock = test_context.Mock() |
| 705 |
test_context.patch_object(AXObject, "_clear_all_dictionaries", new=clear_mock) |
| 706 |
AXObject.clear_cache_now("test reason") |
| 707 |
clear_mock.assert_called_once_with("test reason") |
| 708 |
|
| 709 |
def test_start_cache_clearing_thread(self, test_context: OrcaTestContext) -> None: |
| 710 |
"""Test AXObject.start_cache_clearing_thread creates and starts thread.""" |
| 711 |
|
| 712 |
self._setup_dependencies(test_context) |
| 713 |
from orca.ax_object import AXObject |
| 714 |
|
| 715 |
mock_thread = test_context.Mock() |
| 716 |
thread_constructor_mock = test_context.Mock(return_value=mock_thread) |
| 717 |
test_context.patch("threading.Thread", new=thread_constructor_mock) |
| 718 |
AXObject.start_cache_clearing_thread() |
| 719 |
thread_constructor_mock.assert_called_once_with(target=AXObject._clear_stored_data) |
| 720 |
assert mock_thread.daemon is True |
| 721 |
mock_thread.start.assert_called_once() |
| 722 |
|
| 723 |
@pytest.mark.parametrize( |
| 724 |
"case", |
| 725 |
[ |
| 726 |
{ |
| 727 |
"id": "invalid_object", |
| 728 |
"scenario": "invalid_object", |
| 729 |
"app_name": "", |
| 730 |
"has_interface": False, |
| 731 |
"has_document_text": False, |
| 732 |
"has_spreadsheet": False, |
| 733 |
"raises_app_error": False, |
| 734 |
"raises_iface_error": False, |
| 735 |
"expected_result": False, |
| 736 |
"expects_debug": False, |
| 737 |
}, |
| 738 |
{ |
| 739 |
"id": "get_application_error", |
| 740 |
"scenario": "get_application_error", |
| 741 |
"app_name": "", |
| 742 |
"has_interface": False, |
| 743 |
"has_document_text": False, |
| 744 |
"has_spreadsheet": False, |
| 745 |
"raises_app_error": True, |
| 746 |
"raises_iface_error": False, |
| 747 |
"expected_result": False, |
| 748 |
"expects_debug": True, |
| 749 |
}, |
| 750 |
{ |
| 751 |
"id": "get_collection_iface_error", |
| 752 |
"scenario": "get_collection_iface_error", |
| 753 |
"app_name": "firefox", |
| 754 |
"has_interface": False, |
| 755 |
"has_document_text": False, |
| 756 |
"has_spreadsheet": False, |
| 757 |
"raises_app_error": False, |
| 758 |
"raises_iface_error": True, |
| 759 |
"expected_result": False, |
| 760 |
"expects_debug": False, |
| 761 |
}, |
| 762 |
{ |
| 763 |
"id": "non_soffice_with_interface", |
| 764 |
"scenario": "non_soffice_with_interface", |
| 765 |
"app_name": "firefox", |
| 766 |
"has_interface": True, |
| 767 |
"has_document_text": False, |
| 768 |
"has_spreadsheet": False, |
| 769 |
"raises_app_error": False, |
| 770 |
"raises_iface_error": False, |
| 771 |
"expected_result": True, |
| 772 |
"expects_debug": False, |
| 773 |
}, |
| 774 |
{ |
| 775 |
"id": "non_soffice_without_interface", |
| 776 |
"scenario": "non_soffice_without_interface", |
| 777 |
"app_name": "gedit", |
| 778 |
"has_interface": False, |
| 779 |
"has_document_text": False, |
| 780 |
"has_spreadsheet": False, |
| 781 |
"raises_app_error": False, |
| 782 |
"raises_iface_error": False, |
| 783 |
"expected_result": False, |
| 784 |
"expects_debug": False, |
| 785 |
}, |
| 786 |
{ |
| 787 |
"id": "soffice_with_document_text", |
| 788 |
"scenario": "soffice_with_document_text", |
| 789 |
"app_name": "soffice", |
| 790 |
"has_interface": True, |
| 791 |
"has_document_text": True, |
| 792 |
"has_spreadsheet": False, |
| 793 |
"raises_app_error": False, |
| 794 |
"raises_iface_error": False, |
| 795 |
"expected_result": True, |
| 796 |
"expects_debug": False, |
| 797 |
}, |
| 798 |
{ |
| 799 |
"id": "soffice_with_spreadsheet", |
| 800 |
"scenario": "soffice_with_spreadsheet", |
| 801 |
"app_name": "soffice", |
| 802 |
"has_interface": True, |
| 803 |
"has_document_text": False, |
| 804 |
"has_spreadsheet": True, |
| 805 |
"raises_app_error": False, |
| 806 |
"raises_iface_error": False, |
| 807 |
"expected_result": False, |
| 808 |
"expects_debug": True, |
| 809 |
}, |
| 810 |
{ |
| 811 |
"id": "soffice_without_text_or_spreadsheet", |
| 812 |
"scenario": "soffice_without_text_or_spreadsheet", |
| 813 |
"app_name": "soffice", |
| 814 |
"has_interface": True, |
| 815 |
"has_document_text": False, |
| 816 |
"has_spreadsheet": False, |
| 817 |
"raises_app_error": False, |
| 818 |
"raises_iface_error": False, |
| 819 |
"expected_result": True, |
| 820 |
"expects_debug": False, |
| 821 |
}, |
| 822 |
], |
| 823 |
ids=lambda case: case["id"], |
| 824 |
) |
| 825 |
def test_supports_collection( |
| 826 |
self, |
| 827 |
test_context: OrcaTestContext, |
| 828 |
case: dict, |
| 829 |
) -> None: |
| 830 |
"""Test AXObject.supports_collection with various scenarios.""" |
| 831 |
|
| 832 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 833 |
from orca.ax_object import AXObject |
| 834 |
|
| 835 |
if case["scenario"] == "invalid_object": |
| 836 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 837 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 838 |
elif case["app_name"] == "soffice": |
| 839 |
mock_accessible = self._setup_soffice_collection_mocks( |
| 840 |
test_context, |
| 841 |
has_document_text=case["has_document_text"], |
| 842 |
has_spreadsheet=case["has_spreadsheet"], |
| 843 |
) |
| 844 |
else: |
| 845 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 846 |
mock_app = test_context.Mock(spec=Atspi.Accessible) |
| 847 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 848 |
test_context.patch_object( |
| 849 |
AXObject, |
| 850 |
"get_name", |
| 851 |
side_effect=lambda obj: case["app_name"], |
| 852 |
) |
| 853 |
|
| 854 |
if case["raises_app_error"]: |
| 855 |
|
| 856 |
def raise_app_error(_obj) -> None: |
| 857 |
raise GLib.GError("Test application error") |
| 858 |
|
| 859 |
test_context.patch_object( |
| 860 |
Atspi.Accessible, |
| 861 |
"get_application", |
| 862 |
side_effect=raise_app_error, |
| 863 |
) |
| 864 |
elif case["raises_iface_error"]: |
| 865 |
|
| 866 |
def raise_iface_error(_obj) -> None: |
| 867 |
raise GLib.GError("Test collection iface error") |
| 868 |
|
| 869 |
handle_error_mock = test_context.Mock() |
| 870 |
test_context.patch_object(AXObject, "handle_error", new=handle_error_mock) |
| 871 |
test_context.patch_object( |
| 872 |
Atspi.Accessible, |
| 873 |
"get_application", |
| 874 |
return_value=mock_app, |
| 875 |
) |
| 876 |
test_context.patch_object( |
| 877 |
Atspi.Accessible, |
| 878 |
"get_collection_iface", |
| 879 |
side_effect=raise_iface_error, |
| 880 |
) |
| 881 |
else: |
| 882 |
test_context.patch_object( |
| 883 |
Atspi.Accessible, |
| 884 |
"get_application", |
| 885 |
return_value=mock_app, |
| 886 |
) |
| 887 |
mock_iface = test_context.Mock() if case["has_interface"] else None |
| 888 |
test_context.patch_object( |
| 889 |
Atspi.Accessible, |
| 890 |
"get_collection_iface", |
| 891 |
return_value=mock_iface, |
| 892 |
) |
| 893 |
|
| 894 |
result = AXObject.supports_collection(mock_accessible) |
| 895 |
assert result is case["expected_result"] |
| 896 |
|
| 897 |
if case["expects_debug"]: |
| 898 |
essential_modules["orca.debug"].print_message.assert_called() |
| 899 |
if case["scenario"] == "get_application_error": |
| 900 |
calls = essential_modules["orca.debug"].print_message.call_args_list |
| 901 |
assert len(calls) == 1 |
| 902 |
call_args = calls[0][0] |
| 903 |
assert call_args[0] == essential_modules["orca.debug"].LEVEL_INFO |
| 904 |
assert "AXObject: Exception in supports_collection:" in call_args[1] |
| 905 |
assert "Test application error" in call_args[1] |
| 906 |
elif case["scenario"] == "soffice_with_spreadsheet": |
| 907 |
essential_modules["orca.debug"].print_message.assert_called_with( |
| 908 |
essential_modules["orca.debug"].LEVEL_INFO, |
| 909 |
"AXObject: Treating soffice as not supporting collection due to spreadsheet.", |
| 910 |
True, |
| 911 |
) |
| 912 |
|
| 913 |
if case["raises_iface_error"]: |
| 914 |
handle_error_mock.assert_called_once() |
| 915 |
|
| 916 |
@pytest.mark.parametrize( |
| 917 |
"case", |
| 918 |
[ |
| 919 |
{"id": "none_object", "obj_type": "none", "is_dead": False, "expected_result": False}, |
| 920 |
{ |
| 921 |
"id": "known_dead_object", |
| 922 |
"obj_type": "mock", |
| 923 |
"is_dead": True, |
| 924 |
"expected_result": False, |
| 925 |
}, |
| 926 |
{"id": "valid_object", "obj_type": "mock", "is_dead": False, "expected_result": True}, |
| 927 |
], |
| 928 |
ids=lambda case: case["id"], |
| 929 |
) |
| 930 |
def test_is_valid(self, test_context, case: dict) -> None: |
| 931 |
"""Test AXObject.is_valid with different object scenarios.""" |
| 932 |
|
| 933 |
self._setup_dependencies(test_context) |
| 934 |
from orca.ax_object import AXObject |
| 935 |
|
| 936 |
if case["obj_type"] == "none": |
| 937 |
test_obj = None |
| 938 |
else: |
| 939 |
test_obj = test_context.Mock(spec=Atspi.Accessible) |
| 940 |
test_context.patch_object( |
| 941 |
AXObject, |
| 942 |
"object_is_known_dead", |
| 943 |
side_effect=lambda obj: case["is_dead"], |
| 944 |
) |
| 945 |
|
| 946 |
result = AXObject.is_valid(test_obj) |
| 947 |
assert result == case["expected_result"] |
| 948 |
|
| 949 |
@pytest.mark.parametrize( |
| 950 |
"case", |
| 951 |
[ |
| 952 |
{"id": "none_object", "use_none_object": True, "cache_status": None, "expected": False}, |
| 953 |
{ |
| 954 |
"id": "not_in_cache", |
| 955 |
"use_none_object": False, |
| 956 |
"cache_status": "clear", |
| 957 |
"expected": False, |
| 958 |
}, |
| 959 |
{ |
| 960 |
"id": "in_cache_as_dead", |
| 961 |
"use_none_object": False, |
| 962 |
"cache_status": True, |
| 963 |
"expected": True, |
| 964 |
}, |
| 965 |
{ |
| 966 |
"id": "in_cache_as_alive", |
| 967 |
"use_none_object": False, |
| 968 |
"cache_status": False, |
| 969 |
"expected": False, |
| 970 |
}, |
| 971 |
], |
| 972 |
ids=lambda case: case["id"], |
| 973 |
) |
| 974 |
def test_object_is_known_dead_scenarios( |
| 975 |
self, |
| 976 |
test_context: OrcaTestContext, |
| 977 |
case: dict, |
| 978 |
) -> None: |
| 979 |
"""Test AXObject.object_is_known_dead with various scenarios.""" |
| 980 |
|
| 981 |
self._setup_dependencies(test_context) |
| 982 |
from orca.ax_object import AXObject |
| 983 |
|
| 984 |
if case["use_none_object"]: |
| 985 |
test_obj = None |
| 986 |
else: |
| 987 |
test_obj = test_context.Mock(spec=Atspi.Accessible) |
| 988 |
if case["cache_status"] == "clear": |
| 989 |
AXObject.KNOWN_DEAD.clear() |
| 990 |
elif isinstance(case["cache_status"], bool): |
| 991 |
AXObject.KNOWN_DEAD[hash(test_obj)] = case["cache_status"] |
| 992 |
|
| 993 |
result = AXObject.object_is_known_dead(test_obj) |
| 994 |
assert result is case["expected"] |
| 995 |
|
| 996 |
@pytest.mark.parametrize( |
| 997 |
"case", |
| 998 |
[ |
| 999 |
{ |
| 1000 |
"id": "none_object", |
| 1001 |
"use_none_object": True, |
| 1002 |
"initial_status": None, |
| 1003 |
"new_status": True, |
| 1004 |
"expected_status": None, |
| 1005 |
"expects_debug_call": False, |
| 1006 |
}, |
| 1007 |
{ |
| 1008 |
"id": "same_status", |
| 1009 |
"use_none_object": False, |
| 1010 |
"initial_status": True, |
| 1011 |
"new_status": True, |
| 1012 |
"expected_status": True, |
| 1013 |
"expects_debug_call": False, |
| 1014 |
}, |
| 1015 |
{ |
| 1016 |
"id": "mark_as_dead", |
| 1017 |
"use_none_object": False, |
| 1018 |
"initial_status": "clear", |
| 1019 |
"new_status": True, |
| 1020 |
"expected_status": True, |
| 1021 |
"expects_debug_call": "print_message", |
| 1022 |
}, |
| 1023 |
{ |
| 1024 |
"id": "resurrect_object", |
| 1025 |
"use_none_object": False, |
| 1026 |
"initial_status": True, |
| 1027 |
"new_status": False, |
| 1028 |
"expected_status": False, |
| 1029 |
"expects_debug_call": "print_tokens", |
| 1030 |
}, |
| 1031 |
], |
| 1032 |
ids=lambda case: case["id"], |
| 1033 |
) |
| 1034 |
def test_set_known_dead_status_scenarios( # pylint: disable=too-many-arguments |
| 1035 |
self, |
| 1036 |
test_context: OrcaTestContext, |
| 1037 |
case: dict, |
| 1038 |
) -> None: |
| 1039 |
"""Test AXObject._set_known_dead_status with various scenarios.""" |
| 1040 |
|
| 1041 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 1042 |
from orca.ax_object import AXObject |
| 1043 |
|
| 1044 |
if case["use_none_object"]: |
| 1045 |
test_obj = None |
| 1046 |
AXObject._set_known_dead_status(test_obj, case["new_status"]) |
| 1047 |
else: |
| 1048 |
test_obj = test_context.Mock(spec=Atspi.Accessible) |
| 1049 |
if case["initial_status"] == "clear": |
| 1050 |
AXObject.KNOWN_DEAD.clear() |
| 1051 |
elif isinstance(case["initial_status"], bool): |
| 1052 |
AXObject.KNOWN_DEAD[hash(test_obj)] = case["initial_status"] |
| 1053 |
|
| 1054 |
AXObject._set_known_dead_status(test_obj, case["new_status"]) |
| 1055 |
|
| 1056 |
if case["expected_status"] is not None: |
| 1057 |
assert AXObject.KNOWN_DEAD[hash(test_obj)] is case["expected_status"] |
| 1058 |
|
| 1059 |
if case["expects_debug_call"] == "print_message": |
| 1060 |
essential_modules["orca.debug"].print_message.assert_called() |
| 1061 |
elif case["expects_debug_call"] == "print_tokens": |
| 1062 |
essential_modules["orca.debug"].print_tokens.assert_called() |
| 1063 |
|
| 1064 |
@pytest.mark.parametrize( |
| 1065 |
"case", |
| 1066 |
[ |
| 1067 |
{ |
| 1068 |
"id": "object_not_exist", |
| 1069 |
"error_string": "accessible/123 does not exist", |
| 1070 |
"expected_msg_part": "object no longer exists", |
| 1071 |
}, |
| 1072 |
{ |
| 1073 |
"id": "app_not_exist", |
| 1074 |
"error_string": "The application no longer exists", |
| 1075 |
"expected_msg_part": "app no longer exists", |
| 1076 |
}, |
| 1077 |
{ |
| 1078 |
"id": "other_error", |
| 1079 |
"error_string": "Some other error", |
| 1080 |
"expected_msg_part": "Some other error", |
| 1081 |
}, |
| 1082 |
], |
| 1083 |
ids=lambda case: case["id"], |
| 1084 |
) |
| 1085 |
def test_handle_error(self, test_context, case: dict) -> None: |
| 1086 |
"""Test AXObject.handle_error with different error types.""" |
| 1087 |
|
| 1088 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 1089 |
from orca.ax_object import AXObject |
| 1090 |
|
| 1091 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1092 |
error = Exception(case["error_string"]) |
| 1093 |
msg = f"AXObject: Error calling method: {case['error_string']}" |
| 1094 |
AXObject.handle_error(mock_accessible, error, msg) |
| 1095 |
essential_modules["orca.debug"].print_message.assert_called_once() |
| 1096 |
call_args = essential_modules["orca.debug"].print_message.call_args[0] |
| 1097 |
assert case["expected_msg_part"] in call_args[1] |
| 1098 |
|
| 1099 |
@pytest.mark.parametrize( |
| 1100 |
"case", |
| 1101 |
[ |
| 1102 |
{ |
| 1103 |
"id": "valid_object", |
| 1104 |
"scenario": "valid_object", |
| 1105 |
"expected_result": [1, 2], |
| 1106 |
"should_call_handle_error": False, |
| 1107 |
}, |
| 1108 |
{ |
| 1109 |
"id": "glib_error", |
| 1110 |
"scenario": "glib_error", |
| 1111 |
"expected_result": [], |
| 1112 |
"should_call_handle_error": True, |
| 1113 |
}, |
| 1114 |
], |
| 1115 |
ids=lambda case: case["id"], |
| 1116 |
) |
| 1117 |
def test_get_path(self, test_context, case: dict) -> None: |
| 1118 |
"""Test AXObject.get_path with different scenarios.""" |
| 1119 |
|
| 1120 |
self._setup_dependencies(test_context) |
| 1121 |
from orca.ax_object import AXObject |
| 1122 |
|
| 1123 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1124 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1125 |
handle_error_mock = test_context.Mock() |
| 1126 |
test_context.patch_object(AXObject, "handle_error", new=handle_error_mock) |
| 1127 |
|
| 1128 |
if case["scenario"] == "valid_object": |
| 1129 |
mock_parent = test_context.Mock(spec=Atspi.Accessible) |
| 1130 |
|
| 1131 |
def mock_get_index(obj) -> int: |
| 1132 |
if obj == mock_accessible: |
| 1133 |
return 2 |
| 1134 |
if obj == mock_parent: |
| 1135 |
return 1 |
| 1136 |
return 0 |
| 1137 |
|
| 1138 |
test_context.patch_object(Atspi.Accessible, "get_index_in_parent", new=mock_get_index) |
| 1139 |
|
| 1140 |
def mock_get_parent_checked(obj) -> object: |
| 1141 |
if obj == mock_accessible: |
| 1142 |
return mock_parent |
| 1143 |
return None |
| 1144 |
|
| 1145 |
test_context.patch_object(AXObject, "get_parent_checked", new=mock_get_parent_checked) |
| 1146 |
|
| 1147 |
elif case["scenario"] == "glib_error": |
| 1148 |
|
| 1149 |
def raise_glib_error(_obj) -> None: |
| 1150 |
raise GLib.GError("Test path error") |
| 1151 |
|
| 1152 |
test_context.patch_object( |
| 1153 |
Atspi.Accessible, |
| 1154 |
"get_index_in_parent", |
| 1155 |
side_effect=raise_glib_error, |
| 1156 |
) |
| 1157 |
|
| 1158 |
result = AXObject.get_path(mock_accessible) |
| 1159 |
assert result == case["expected_result"] |
| 1160 |
|
| 1161 |
if case["should_call_handle_error"]: |
| 1162 |
handle_error_mock.assert_called_once() |
| 1163 |
else: |
| 1164 |
handle_error_mock.assert_not_called() |
| 1165 |
|
| 1166 |
@pytest.mark.parametrize( |
| 1167 |
"case", |
| 1168 |
[ |
| 1169 |
{ |
| 1170 |
"id": "valid_index", |
| 1171 |
"scenario": "valid_index", |
| 1172 |
"expected_result": 3, |
| 1173 |
"should_call_handle_error": False, |
| 1174 |
}, |
| 1175 |
{ |
| 1176 |
"id": "glib_error", |
| 1177 |
"scenario": "glib_error", |
| 1178 |
"expected_result": -1, |
| 1179 |
"should_call_handle_error": True, |
| 1180 |
}, |
| 1181 |
], |
| 1182 |
ids=lambda case: case["id"], |
| 1183 |
) |
| 1184 |
def test_get_index_in_parent(self, test_context, case: dict) -> None: |
| 1185 |
"""Test AXObject.get_index_in_parent with different scenarios.""" |
| 1186 |
|
| 1187 |
self._setup_dependencies(test_context) |
| 1188 |
from orca.ax_object import AXObject |
| 1189 |
|
| 1190 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1191 |
handle_error_mock = test_context.Mock() |
| 1192 |
test_context.patch_object(AXObject, "handle_error", new=handle_error_mock) |
| 1193 |
|
| 1194 |
if case["scenario"] == "valid_index": |
| 1195 |
test_context.patch_object(Atspi.Accessible, "get_index_in_parent", return_value=3) |
| 1196 |
elif case["scenario"] == "glib_error": |
| 1197 |
|
| 1198 |
def raise_glib_error(_obj) -> None: |
| 1199 |
raise GLib.GError("Test index error") |
| 1200 |
|
| 1201 |
test_context.patch_object( |
| 1202 |
Atspi.Accessible, |
| 1203 |
"get_index_in_parent", |
| 1204 |
side_effect=raise_glib_error, |
| 1205 |
) |
| 1206 |
|
| 1207 |
result = AXObject.get_index_in_parent(mock_accessible) |
| 1208 |
assert result == case["expected_result"] |
| 1209 |
|
| 1210 |
if case["should_call_handle_error"]: |
| 1211 |
handle_error_mock.assert_called_once() |
| 1212 |
else: |
| 1213 |
handle_error_mock.assert_not_called() |
| 1214 |
|
| 1215 |
@pytest.mark.parametrize( |
| 1216 |
"case", |
| 1217 |
[ |
| 1218 |
{"id": "valid_parent", "scenario": "valid_parent", "should_call_handle_error": False}, |
| 1219 |
{"id": "glib_error", "scenario": "glib_error", "should_call_handle_error": True}, |
| 1220 |
], |
| 1221 |
ids=lambda case: case["id"], |
| 1222 |
) |
| 1223 |
def test_get_parent(self, test_context, case: dict) -> None: |
| 1224 |
"""Test AXObject.get_parent with different scenarios.""" |
| 1225 |
|
| 1226 |
self._setup_dependencies(test_context) |
| 1227 |
from orca.ax_object import AXObject |
| 1228 |
|
| 1229 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1230 |
handle_error_mock = test_context.Mock() |
| 1231 |
test_context.patch_object(AXObject, "handle_error", new=handle_error_mock) |
| 1232 |
|
| 1233 |
if case["scenario"] == "valid_parent": |
| 1234 |
mock_parent = test_context.Mock(spec=Atspi.Accessible) |
| 1235 |
test_context.patch_object(Atspi.Accessible, "get_parent", return_value=mock_parent) |
| 1236 |
result = AXObject.get_parent(mock_accessible) |
| 1237 |
assert result == mock_parent |
| 1238 |
elif case["scenario"] == "glib_error": |
| 1239 |
|
| 1240 |
def raise_glib_error(_obj) -> None: |
| 1241 |
raise GLib.GError("Test parent error") |
| 1242 |
|
| 1243 |
test_context.patch_object(Atspi.Accessible, "get_parent", side_effect=raise_glib_error) |
| 1244 |
result = AXObject.get_parent(mock_accessible) |
| 1245 |
assert result is None |
| 1246 |
|
| 1247 |
if case["should_call_handle_error"]: |
| 1248 |
handle_error_mock.assert_called_once() |
| 1249 |
else: |
| 1250 |
handle_error_mock.assert_not_called() |
| 1251 |
|
| 1252 |
@pytest.mark.parametrize( |
| 1253 |
"case", |
| 1254 |
[ |
| 1255 |
{ |
| 1256 |
"id": "valid_object", |
| 1257 |
"scenario": "valid_object", |
| 1258 |
"expected_result": Atspi.Role.BUTTON, |
| 1259 |
"should_call_handle_error": False, |
| 1260 |
"should_call_set_known_dead": True, |
| 1261 |
}, |
| 1262 |
{ |
| 1263 |
"id": "invalid_object", |
| 1264 |
"scenario": "invalid_object", |
| 1265 |
"expected_result": Atspi.Role.INVALID, |
| 1266 |
"should_call_handle_error": False, |
| 1267 |
"should_call_set_known_dead": False, |
| 1268 |
}, |
| 1269 |
{ |
| 1270 |
"id": "glib_error", |
| 1271 |
"scenario": "glib_error", |
| 1272 |
"expected_result": Atspi.Role.INVALID, |
| 1273 |
"should_call_handle_error": True, |
| 1274 |
"should_call_set_known_dead": False, |
| 1275 |
}, |
| 1276 |
], |
| 1277 |
ids=lambda case: case["id"], |
| 1278 |
) |
| 1279 |
def test_get_role( |
| 1280 |
self, |
| 1281 |
test_context, |
| 1282 |
case: dict, |
| 1283 |
) -> None: |
| 1284 |
"""Test AXObject.get_role with different scenarios.""" |
| 1285 |
|
| 1286 |
self._setup_dependencies(test_context) |
| 1287 |
from orca.ax_object import AXObject |
| 1288 |
|
| 1289 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1290 |
handle_error_mock = test_context.Mock() |
| 1291 |
test_context.patch_object(AXObject, "handle_error", new=handle_error_mock) |
| 1292 |
set_known_dead_status_mock = test_context.Mock() |
| 1293 |
test_context.patch_object( |
| 1294 |
AXObject, |
| 1295 |
"_set_known_dead_status", |
| 1296 |
new=set_known_dead_status_mock, |
| 1297 |
) |
| 1298 |
|
| 1299 |
if case["scenario"] == "valid_object": |
| 1300 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1301 |
test_context.patch_object(Atspi.Accessible, "get_role", return_value=Atspi.Role.BUTTON) |
| 1302 |
elif case["scenario"] == "invalid_object": |
| 1303 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 1304 |
elif case["scenario"] == "glib_error": |
| 1305 |
|
| 1306 |
def raise_glib_error(_obj) -> None: |
| 1307 |
raise GLib.GError("Test role error") |
| 1308 |
|
| 1309 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1310 |
test_context.patch_object(Atspi.Accessible, "get_role", side_effect=raise_glib_error) |
| 1311 |
|
| 1312 |
result = AXObject.get_role(mock_accessible) |
| 1313 |
assert result == case["expected_result"] |
| 1314 |
|
| 1315 |
if case["should_call_handle_error"]: |
| 1316 |
handle_error_mock.assert_called_once() |
| 1317 |
else: |
| 1318 |
handle_error_mock.assert_not_called() |
| 1319 |
|
| 1320 |
if case["should_call_set_known_dead"]: |
| 1321 |
set_known_dead_status_mock.assert_called_once_with(mock_accessible, False) |
| 1322 |
else: |
| 1323 |
set_known_dead_status_mock.assert_not_called() |
| 1324 |
|
| 1325 |
@pytest.mark.parametrize( |
| 1326 |
"case", |
| 1327 |
[ |
| 1328 |
{ |
| 1329 |
"id": "valid_not_localized", |
| 1330 |
"scenario": "valid_not_localized", |
| 1331 |
"localized": False, |
| 1332 |
"expected_result": "button", |
| 1333 |
"should_call_handle_error": False, |
| 1334 |
}, |
| 1335 |
{ |
| 1336 |
"id": "valid_localized", |
| 1337 |
"scenario": "valid_localized", |
| 1338 |
"localized": True, |
| 1339 |
"expected_result": "botón", |
| 1340 |
"should_call_handle_error": False, |
| 1341 |
}, |
| 1342 |
{ |
| 1343 |
"id": "invalid_object", |
| 1344 |
"scenario": "invalid_object", |
| 1345 |
"localized": None, |
| 1346 |
"expected_result": "", |
| 1347 |
"should_call_handle_error": False, |
| 1348 |
}, |
| 1349 |
{ |
| 1350 |
"id": "glib_error", |
| 1351 |
"scenario": "glib_error", |
| 1352 |
"localized": None, |
| 1353 |
"expected_result": "", |
| 1354 |
"should_call_handle_error": True, |
| 1355 |
}, |
| 1356 |
], |
| 1357 |
ids=lambda case: case["id"], |
| 1358 |
) |
| 1359 |
def test_get_role_name( |
| 1360 |
self, |
| 1361 |
test_context, |
| 1362 |
case: dict, |
| 1363 |
) -> None: |
| 1364 |
"""Test AXObject.get_role_name with different scenarios.""" |
| 1365 |
|
| 1366 |
self._setup_dependencies(test_context) |
| 1367 |
from orca.ax_object import AXObject |
| 1368 |
|
| 1369 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1370 |
handle_error_mock = test_context.Mock() |
| 1371 |
test_context.patch_object(AXObject, "handle_error", new=handle_error_mock) |
| 1372 |
result = "" |
| 1373 |
|
| 1374 |
if case["scenario"] == "valid_not_localized": |
| 1375 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1376 |
test_context.patch_object(Atspi.Accessible, "get_role_name", return_value="button") |
| 1377 |
result = AXObject.get_role_name(mock_accessible, localized=False) |
| 1378 |
elif case["scenario"] == "valid_localized": |
| 1379 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1380 |
test_context.patch_object( |
| 1381 |
Atspi.Accessible, |
| 1382 |
"get_localized_role_name", |
| 1383 |
return_value="botón", |
| 1384 |
) |
| 1385 |
result = AXObject.get_role_name(mock_accessible, localized=True) |
| 1386 |
elif case["scenario"] == "invalid_object": |
| 1387 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 1388 |
result = AXObject.get_role_name(mock_accessible, localized=case["localized"]) |
| 1389 |
elif case["scenario"] == "glib_error": |
| 1390 |
|
| 1391 |
def raise_glib_error(_obj) -> None: |
| 1392 |
raise GLib.GError("Test role name error") |
| 1393 |
|
| 1394 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1395 |
test_context.patch_object( |
| 1396 |
Atspi.Accessible, |
| 1397 |
"get_role_name", |
| 1398 |
side_effect=raise_glib_error, |
| 1399 |
) |
| 1400 |
result = AXObject.get_role_name(mock_accessible, localized=case["localized"]) |
| 1401 |
|
| 1402 |
assert result == case["expected_result"] |
| 1403 |
|
| 1404 |
if case["should_call_handle_error"]: |
| 1405 |
handle_error_mock.assert_called_once() |
| 1406 |
else: |
| 1407 |
handle_error_mock.assert_not_called() |
| 1408 |
|
| 1409 |
def test_get_role_description_with_invalid_object(self, test_context: OrcaTestContext) -> None: |
| 1410 |
"""Test AXObject.get_role_description with invalid object.""" |
| 1411 |
|
| 1412 |
self._setup_dependencies(test_context) |
| 1413 |
from orca.ax_object import AXObject |
| 1414 |
|
| 1415 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1416 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 1417 |
result = AXObject.get_role_description(mock_accessible) |
| 1418 |
assert result == "" |
| 1419 |
|
| 1420 |
def test_get_role_description_with_role_description( |
| 1421 |
self, |
| 1422 |
test_context: OrcaTestContext, |
| 1423 |
) -> None: |
| 1424 |
"""Test AXObject.get_role_description with role description attribute.""" |
| 1425 |
|
| 1426 |
self._setup_dependencies(test_context) |
| 1427 |
from orca.ax_object import AXObject |
| 1428 |
|
| 1429 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1430 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1431 |
test_context.patch_object( |
| 1432 |
AXObject, |
| 1433 |
"get_attributes_dict", |
| 1434 |
return_value={"roledescription": "custom button"}, |
| 1435 |
) |
| 1436 |
result = AXObject.get_role_description(mock_accessible) |
| 1437 |
assert result == "custom button" |
| 1438 |
|
| 1439 |
def test_get_role_description_with_braille_description( |
| 1440 |
self, |
| 1441 |
test_context: OrcaTestContext, |
| 1442 |
) -> None: |
| 1443 |
"""Test AXObject.get_role_description with braille role description.""" |
| 1444 |
|
| 1445 |
self._setup_dependencies(test_context) |
| 1446 |
from orca.ax_object import AXObject |
| 1447 |
|
| 1448 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1449 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1450 |
test_context.patch_object( |
| 1451 |
AXObject, |
| 1452 |
"get_attributes_dict", |
| 1453 |
return_value={"roledescription": "custom button", "brailleroledescription": "btn"}, |
| 1454 |
) |
| 1455 |
result = AXObject.get_role_description(mock_accessible, is_braille=True) |
| 1456 |
assert result == "btn" |
| 1457 |
|
| 1458 |
def test_get_role_description_braille_fallback(self, test_context: OrcaTestContext) -> None: |
| 1459 |
"""Test AXObject.get_role_description braille falls back to regular description.""" |
| 1460 |
|
| 1461 |
self._setup_dependencies(test_context) |
| 1462 |
from orca.ax_object import AXObject |
| 1463 |
|
| 1464 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1465 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1466 |
test_context.patch_object( |
| 1467 |
AXObject, |
| 1468 |
"get_attributes_dict", |
| 1469 |
return_value={"roledescription": "custom button"}, |
| 1470 |
) |
| 1471 |
result = AXObject.get_role_description(mock_accessible, is_braille=True) |
| 1472 |
assert result == "custom button" |
| 1473 |
|
| 1474 |
@pytest.mark.parametrize( |
| 1475 |
"case", |
| 1476 |
[ |
| 1477 |
{ |
| 1478 |
"id": "invalid_object", |
| 1479 |
"scenario": "invalid_object", |
| 1480 |
"expected_result": "", |
| 1481 |
"should_call_handle_error": False, |
| 1482 |
}, |
| 1483 |
{ |
| 1484 |
"id": "valid_object", |
| 1485 |
"scenario": "valid_object", |
| 1486 |
"expected_result": "button-123", |
| 1487 |
"should_call_handle_error": False, |
| 1488 |
}, |
| 1489 |
{ |
| 1490 |
"id": "glib_error", |
| 1491 |
"scenario": "glib_error", |
| 1492 |
"expected_result": "", |
| 1493 |
"should_call_handle_error": True, |
| 1494 |
}, |
| 1495 |
], |
| 1496 |
ids=lambda case: case["id"], |
| 1497 |
) |
| 1498 |
def test_get_accessible_id(self, test_context, case: dict) -> None: |
| 1499 |
"""Test AXObject.get_accessible_id with different scenarios.""" |
| 1500 |
|
| 1501 |
self._setup_dependencies(test_context) |
| 1502 |
from orca.ax_object import AXObject |
| 1503 |
|
| 1504 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1505 |
handle_error_mock = test_context.Mock() |
| 1506 |
test_context.patch_object(AXObject, "handle_error", new=handle_error_mock) |
| 1507 |
|
| 1508 |
if case["scenario"] == "invalid_object": |
| 1509 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 1510 |
elif case["scenario"] == "valid_object": |
| 1511 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1512 |
test_context.patch_object( |
| 1513 |
Atspi.Accessible, |
| 1514 |
"get_accessible_id", |
| 1515 |
return_value="button-123", |
| 1516 |
) |
| 1517 |
elif case["scenario"] == "glib_error": |
| 1518 |
|
| 1519 |
def raise_glib_error(_obj) -> None: |
| 1520 |
raise GLib.GError("Test accessible id error") |
| 1521 |
|
| 1522 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1523 |
test_context.patch_object( |
| 1524 |
Atspi.Accessible, |
| 1525 |
"get_accessible_id", |
| 1526 |
side_effect=raise_glib_error, |
| 1527 |
) |
| 1528 |
|
| 1529 |
result = AXObject.get_accessible_id(mock_accessible) |
| 1530 |
assert result == case["expected_result"] |
| 1531 |
|
| 1532 |
if case["should_call_handle_error"]: |
| 1533 |
handle_error_mock.assert_called_once() |
| 1534 |
else: |
| 1535 |
handle_error_mock.assert_not_called() |
| 1536 |
|
| 1537 |
@pytest.mark.parametrize( |
| 1538 |
"case", |
| 1539 |
[ |
| 1540 |
{ |
| 1541 |
"id": "invalid_object", |
| 1542 |
"scenario": "invalid_object", |
| 1543 |
"expected_result": "", |
| 1544 |
"should_call_handle_error": False, |
| 1545 |
}, |
| 1546 |
{ |
| 1547 |
"id": "valid_object", |
| 1548 |
"scenario": "valid_object", |
| 1549 |
"expected_result": "Submit", |
| 1550 |
"should_call_handle_error": False, |
| 1551 |
}, |
| 1552 |
{ |
| 1553 |
"id": "glib_error", |
| 1554 |
"scenario": "glib_error", |
| 1555 |
"expected_result": "", |
| 1556 |
"should_call_handle_error": True, |
| 1557 |
}, |
| 1558 |
], |
| 1559 |
ids=lambda case: case["id"], |
| 1560 |
) |
| 1561 |
def test_get_name(self, test_context, case: dict) -> None: |
| 1562 |
"""Test AXObject.get_name with different scenarios.""" |
| 1563 |
|
| 1564 |
self._setup_dependencies(test_context) |
| 1565 |
from orca.ax_object import AXObject |
| 1566 |
|
| 1567 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1568 |
handle_error_mock = test_context.Mock() |
| 1569 |
test_context.patch_object(AXObject, "handle_error", new=handle_error_mock) |
| 1570 |
|
| 1571 |
if case["scenario"] == "invalid_object": |
| 1572 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 1573 |
elif case["scenario"] == "valid_object": |
| 1574 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1575 |
test_context.patch_object(Atspi.Accessible, "get_name", return_value="Submit") |
| 1576 |
elif case["scenario"] == "glib_error": |
| 1577 |
|
| 1578 |
def raise_glib_error(_obj) -> None: |
| 1579 |
raise GLib.GError("Test name error") |
| 1580 |
|
| 1581 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1582 |
test_context.patch_object(Atspi.Accessible, "get_name", side_effect=raise_glib_error) |
| 1583 |
|
| 1584 |
result = AXObject.get_name(mock_accessible) |
| 1585 |
assert result == case["expected_result"] |
| 1586 |
|
| 1587 |
if case["should_call_handle_error"]: |
| 1588 |
handle_error_mock.assert_called_once() |
| 1589 |
else: |
| 1590 |
handle_error_mock.assert_not_called() |
| 1591 |
|
| 1592 |
def test_has_same_non_empty_name_both_empty(self, test_context: OrcaTestContext) -> None: |
| 1593 |
"""Test AXObject.has_same_non_empty_name with both objects having empty names.""" |
| 1594 |
|
| 1595 |
self._setup_dependencies(test_context) |
| 1596 |
from orca.ax_object import AXObject |
| 1597 |
|
| 1598 |
mock_obj1 = test_context.Mock(spec=Atspi.Accessible) |
| 1599 |
mock_obj2 = test_context.Mock(spec=Atspi.Accessible) |
| 1600 |
test_context.patch_object(AXObject, "get_name", return_value="") |
| 1601 |
result = AXObject.has_same_non_empty_name(mock_obj1, mock_obj2) |
| 1602 |
assert result is False |
| 1603 |
|
| 1604 |
def test_has_same_non_empty_name_same_non_empty(self, test_context: OrcaTestContext) -> None: |
| 1605 |
"""Test AXObject.has_same_non_empty_name with same non-empty names.""" |
| 1606 |
|
| 1607 |
self._setup_dependencies(test_context) |
| 1608 |
from orca.ax_object import AXObject |
| 1609 |
|
| 1610 |
mock_obj1 = test_context.Mock(spec=Atspi.Accessible) |
| 1611 |
mock_obj2 = test_context.Mock(spec=Atspi.Accessible) |
| 1612 |
test_context.patch_object(AXObject, "get_name", return_value="Submit") |
| 1613 |
result = AXObject.has_same_non_empty_name(mock_obj1, mock_obj2) |
| 1614 |
assert result is True |
| 1615 |
|
| 1616 |
def test_has_same_non_empty_name_different_names(self, test_context: OrcaTestContext) -> None: |
| 1617 |
"""Test AXObject.has_same_non_empty_name with different names.""" |
| 1618 |
|
| 1619 |
self._setup_dependencies(test_context) |
| 1620 |
from orca.ax_object import AXObject |
| 1621 |
|
| 1622 |
mock_obj1 = test_context.Mock(spec=Atspi.Accessible) |
| 1623 |
mock_obj2 = test_context.Mock(spec=Atspi.Accessible) |
| 1624 |
|
| 1625 |
def mock_get_name(obj) -> str: |
| 1626 |
if obj == mock_obj1: |
| 1627 |
return "Submit" |
| 1628 |
return "Cancel" |
| 1629 |
|
| 1630 |
test_context.patch_object(AXObject, "get_name", new=mock_get_name) |
| 1631 |
result = AXObject.has_same_non_empty_name(mock_obj1, mock_obj2) |
| 1632 |
assert result is False |
| 1633 |
|
| 1634 |
@pytest.mark.parametrize( |
| 1635 |
"case", |
| 1636 |
[ |
| 1637 |
{"id": "invalid_object", "is_valid": False, "raises_glib_error": False}, |
| 1638 |
{"id": "valid_object", "is_valid": True, "raises_glib_error": False}, |
| 1639 |
{"id": "glib_error", "is_valid": True, "raises_glib_error": True}, |
| 1640 |
], |
| 1641 |
ids=lambda case: case["id"], |
| 1642 |
) |
| 1643 |
def test_get_state_set( |
| 1644 |
self, |
| 1645 |
test_context: OrcaTestContext, |
| 1646 |
case: dict, |
| 1647 |
) -> None: |
| 1648 |
"""Test AXObject.get_state_set with various scenarios.""" |
| 1649 |
|
| 1650 |
self._setup_dependencies(test_context) |
| 1651 |
from orca.ax_object import AXObject |
| 1652 |
|
| 1653 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1654 |
test_context.patch_object(AXObject, "is_valid", side_effect=lambda obj: case["is_valid"]) |
| 1655 |
|
| 1656 |
if case["is_valid"] and not case["raises_glib_error"]: |
| 1657 |
mock_state_set = test_context.Mock() |
| 1658 |
test_context.patch_object( |
| 1659 |
Atspi.Accessible, |
| 1660 |
"get_state_set", |
| 1661 |
return_value=mock_state_set, |
| 1662 |
) |
| 1663 |
set_known_dead_status_mock = test_context.Mock() |
| 1664 |
test_context.patch_object( |
| 1665 |
AXObject, |
| 1666 |
"_set_known_dead_status", |
| 1667 |
new=set_known_dead_status_mock, |
| 1668 |
) |
| 1669 |
|
| 1670 |
result = AXObject.get_state_set(mock_accessible) |
| 1671 |
assert result == mock_state_set |
| 1672 |
set_known_dead_status_mock.assert_called_once_with(mock_accessible, False) |
| 1673 |
elif case["raises_glib_error"]: |
| 1674 |
|
| 1675 |
def raise_glib_error(_obj) -> None: |
| 1676 |
raise GLib.GError("Test state set error") |
| 1677 |
|
| 1678 |
test_context.patch_object( |
| 1679 |
Atspi.Accessible, |
| 1680 |
"get_state_set", |
| 1681 |
side_effect=raise_glib_error, |
| 1682 |
) |
| 1683 |
handle_error_mock = test_context.Mock() |
| 1684 |
test_context.patch_object(AXObject, "handle_error", new=handle_error_mock) |
| 1685 |
|
| 1686 |
result = AXObject.get_state_set(mock_accessible) |
| 1687 |
assert isinstance(result, type(Atspi.StateSet())) |
| 1688 |
handle_error_mock.assert_called_once() |
| 1689 |
else: |
| 1690 |
result = AXObject.get_state_set(mock_accessible) |
| 1691 |
assert isinstance(result, type(Atspi.StateSet())) |
| 1692 |
|
| 1693 |
def test_has_state_with_invalid_object(self, test_context: OrcaTestContext) -> None: |
| 1694 |
"""Test AXObject.has_state with invalid object.""" |
| 1695 |
|
| 1696 |
self._setup_dependencies(test_context) |
| 1697 |
from orca.ax_object import AXObject |
| 1698 |
|
| 1699 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1700 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 1701 |
result = AXObject.has_state(mock_accessible, Atspi.StateType.FOCUSED) |
| 1702 |
assert result is False |
| 1703 |
|
| 1704 |
def test_has_state_with_valid_object(self, test_context: OrcaTestContext) -> None: |
| 1705 |
"""Test AXObject.has_state with valid object.""" |
| 1706 |
|
| 1707 |
self._setup_dependencies(test_context) |
| 1708 |
from orca.ax_object import AXObject |
| 1709 |
|
| 1710 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1711 |
mock_state_set = test_context.Mock() |
| 1712 |
mock_state_set.contains.return_value = True |
| 1713 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1714 |
test_context.patch_object(AXObject, "get_state_set", return_value=mock_state_set) |
| 1715 |
result = AXObject.has_state(mock_accessible, Atspi.StateType.FOCUSED) |
| 1716 |
assert result is True |
| 1717 |
mock_state_set.contains.assert_called_once_with(Atspi.StateType.FOCUSED) |
| 1718 |
|
| 1719 |
def test_clear_cache_with_none_object(self, test_context: OrcaTestContext) -> None: |
| 1720 |
"""Test AXObject.clear_cache with None object.""" |
| 1721 |
|
| 1722 |
self._setup_dependencies(test_context) |
| 1723 |
from orca.ax_object import AXObject |
| 1724 |
|
| 1725 |
AXObject.clear_cache(None) |
| 1726 |
|
| 1727 |
def test_clear_cache_non_recursive(self, test_context: OrcaTestContext) -> None: |
| 1728 |
"""Test AXObject.clear_cache non-recursive.""" |
| 1729 |
|
| 1730 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 1731 |
from orca.ax_object import AXObject |
| 1732 |
|
| 1733 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1734 |
test_context.patch_object(Atspi.Accessible, "clear_cache_single", new=test_context.Mock()) |
| 1735 |
AXObject.clear_cache(mock_accessible, recursive=False, reason="test") |
| 1736 |
sys.modules["gi.repository"].Atspi.Accessible.clear_cache_single.assert_called_once_with( |
| 1737 |
mock_accessible, |
| 1738 |
) |
| 1739 |
essential_modules["orca.debug"].print_tokens.assert_called() |
| 1740 |
|
| 1741 |
def test_clear_cache_non_recursive_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 1742 |
"""Test AXObject.clear_cache non-recursive handles GLib.GError.""" |
| 1743 |
|
| 1744 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 1745 |
from orca.ax_object import AXObject |
| 1746 |
|
| 1747 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1748 |
|
| 1749 |
def raise_glib_error(_obj) -> None: |
| 1750 |
raise GLib.GError("Test clear cache error") |
| 1751 |
|
| 1752 |
test_context.patch_object( |
| 1753 |
Atspi.Accessible, |
| 1754 |
"clear_cache_single", |
| 1755 |
side_effect=raise_glib_error, |
| 1756 |
) |
| 1757 |
AXObject.clear_cache(mock_accessible, recursive=False) |
| 1758 |
essential_modules["orca.debug"].print_message.assert_called() |
| 1759 |
|
| 1760 |
def test_clear_cache_recursive(self, test_context: OrcaTestContext) -> None: |
| 1761 |
"""Test AXObject.clear_cache recursive.""" |
| 1762 |
|
| 1763 |
self._setup_dependencies(test_context) |
| 1764 |
from orca.ax_object import AXObject |
| 1765 |
|
| 1766 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1767 |
mock_clear_cache = test_context.patch("gi.repository.Atspi.Accessible.clear_cache") |
| 1768 |
AXObject.clear_cache(mock_accessible, recursive=True) |
| 1769 |
mock_clear_cache.assert_called_once_with(mock_accessible) |
| 1770 |
|
| 1771 |
def test_clear_cache_recursive_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 1772 |
"""Test AXObject.clear_cache recursive handles GLib.GError.""" |
| 1773 |
|
| 1774 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 1775 |
from orca.ax_object import AXObject |
| 1776 |
|
| 1777 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1778 |
|
| 1779 |
def raise_glib_error(_obj) -> None: |
| 1780 |
raise GLib.GError("Test clear cache recursive error") |
| 1781 |
|
| 1782 |
test_context.patch_object(Atspi.Accessible, "clear_cache", side_effect=raise_glib_error) |
| 1783 |
AXObject.clear_cache(mock_accessible, recursive=True) |
| 1784 |
essential_modules["orca.debug"].print_message.assert_called() |
| 1785 |
|
| 1786 |
@pytest.mark.parametrize( |
| 1787 |
"case", |
| 1788 |
[ |
| 1789 |
{ |
| 1790 |
"id": "invalid_object", |
| 1791 |
"is_valid": False, |
| 1792 |
"use_cache": True, |
| 1793 |
"cached_attrs": None, |
| 1794 |
"get_attributes_result": None, |
| 1795 |
"raises_error": False, |
| 1796 |
"expected_result": {}, |
| 1797 |
"expects_cache_update": False, |
| 1798 |
"expects_handle_error": False, |
| 1799 |
}, |
| 1800 |
{ |
| 1801 |
"id": "cache_hit", |
| 1802 |
"is_valid": True, |
| 1803 |
"use_cache": True, |
| 1804 |
"cached_attrs": {"id": "button1", "class": "primary"}, |
| 1805 |
"get_attributes_result": None, |
| 1806 |
"raises_error": False, |
| 1807 |
"expected_result": {"id": "button1", "class": "primary"}, |
| 1808 |
"expects_cache_update": False, |
| 1809 |
"expects_handle_error": False, |
| 1810 |
}, |
| 1811 |
{ |
| 1812 |
"id": "no_cache_valid_attributes", |
| 1813 |
"is_valid": True, |
| 1814 |
"use_cache": False, |
| 1815 |
"cached_attrs": None, |
| 1816 |
"get_attributes_result": {"id": "button2", "class": "secondary"}, |
| 1817 |
"raises_error": False, |
| 1818 |
"expected_result": {"id": "button2", "class": "secondary"}, |
| 1819 |
"expects_cache_update": True, |
| 1820 |
"expects_handle_error": False, |
| 1821 |
}, |
| 1822 |
{ |
| 1823 |
"id": "glib_error", |
| 1824 |
"is_valid": True, |
| 1825 |
"use_cache": False, |
| 1826 |
"cached_attrs": None, |
| 1827 |
"get_attributes_result": None, |
| 1828 |
"raises_error": True, |
| 1829 |
"expected_result": {}, |
| 1830 |
"expects_cache_update": False, |
| 1831 |
"expects_handle_error": True, |
| 1832 |
}, |
| 1833 |
{ |
| 1834 |
"id": "none_attributes", |
| 1835 |
"is_valid": True, |
| 1836 |
"use_cache": False, |
| 1837 |
"cached_attrs": None, |
| 1838 |
"get_attributes_result": None, |
| 1839 |
"raises_error": False, |
| 1840 |
"expected_result": {}, |
| 1841 |
"expects_cache_update": False, |
| 1842 |
"expects_handle_error": False, |
| 1843 |
}, |
| 1844 |
], |
| 1845 |
ids=lambda case: case["id"], |
| 1846 |
) |
| 1847 |
def test_get_attributes_dict_scenarios( |
| 1848 |
self, |
| 1849 |
test_context: OrcaTestContext, |
| 1850 |
case: dict, |
| 1851 |
) -> None: |
| 1852 |
"""Test AXObject.get_attributes_dict with various scenarios.""" |
| 1853 |
|
| 1854 |
self._setup_dependencies(test_context) |
| 1855 |
from orca.ax_object import AXObject |
| 1856 |
|
| 1857 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1858 |
test_context.patch_object(AXObject, "is_valid", side_effect=lambda obj: case["is_valid"]) |
| 1859 |
|
| 1860 |
if case["cached_attrs"]: |
| 1861 |
AXObject.OBJECT_ATTRIBUTES[hash(mock_accessible)] = case["cached_attrs"] |
| 1862 |
else: |
| 1863 |
AXObject.OBJECT_ATTRIBUTES.clear() |
| 1864 |
|
| 1865 |
if case["raises_error"]: |
| 1866 |
|
| 1867 |
def raise_glib_error(_obj) -> None: |
| 1868 |
raise GLib.GError("Test attributes error") |
| 1869 |
|
| 1870 |
test_context.patch_object( |
| 1871 |
Atspi.Accessible, |
| 1872 |
"get_attributes", |
| 1873 |
side_effect=raise_glib_error, |
| 1874 |
) |
| 1875 |
handle_error_mock = test_context.Mock() |
| 1876 |
test_context.patch_object(AXObject, "handle_error", new=handle_error_mock) |
| 1877 |
elif case["get_attributes_result"] is not None: |
| 1878 |
test_context.patch_object( |
| 1879 |
Atspi.Accessible, |
| 1880 |
"get_attributes", |
| 1881 |
return_value=case["get_attributes_result"], |
| 1882 |
) |
| 1883 |
else: |
| 1884 |
test_context.patch_object(Atspi.Accessible, "get_attributes", return_value=None) |
| 1885 |
|
| 1886 |
result = AXObject.get_attributes_dict(mock_accessible, use_cache=case["use_cache"]) |
| 1887 |
assert result == case["expected_result"] |
| 1888 |
|
| 1889 |
if case["expects_cache_update"] and case["get_attributes_result"]: |
| 1890 |
assert ( |
| 1891 |
AXObject.OBJECT_ATTRIBUTES[hash(mock_accessible)] == case["get_attributes_result"] |
| 1892 |
) |
| 1893 |
|
| 1894 |
if case["expects_handle_error"]: |
| 1895 |
handle_error_mock.assert_called_once() |
| 1896 |
|
| 1897 |
@pytest.mark.parametrize( |
| 1898 |
"case", |
| 1899 |
[ |
| 1900 |
{ |
| 1901 |
"id": "invalid_object", |
| 1902 |
"is_valid": False, |
| 1903 |
"attributes_dict": {}, |
| 1904 |
"attribute_name": "id", |
| 1905 |
"expected_result": "", |
| 1906 |
}, |
| 1907 |
{ |
| 1908 |
"id": "existing_attribute", |
| 1909 |
"is_valid": True, |
| 1910 |
"attributes_dict": {"id": "button1", "class": "primary"}, |
| 1911 |
"attribute_name": "id", |
| 1912 |
"expected_result": "button1", |
| 1913 |
}, |
| 1914 |
{ |
| 1915 |
"id": "missing_attribute", |
| 1916 |
"is_valid": True, |
| 1917 |
"attributes_dict": {"id": "button1"}, |
| 1918 |
"attribute_name": "class", |
| 1919 |
"expected_result": "", |
| 1920 |
}, |
| 1921 |
], |
| 1922 |
ids=lambda case: case["id"], |
| 1923 |
) |
| 1924 |
def test_get_attribute_scenarios( |
| 1925 |
self, |
| 1926 |
test_context: OrcaTestContext, |
| 1927 |
case: dict, |
| 1928 |
) -> None: |
| 1929 |
"""Test AXObject.get_attribute with various scenarios.""" |
| 1930 |
|
| 1931 |
self._setup_dependencies(test_context) |
| 1932 |
from orca.ax_object import AXObject |
| 1933 |
|
| 1934 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1935 |
test_context.patch_object(AXObject, "is_valid", side_effect=lambda obj: case["is_valid"]) |
| 1936 |
test_context.patch_object( |
| 1937 |
AXObject, |
| 1938 |
"get_attributes_dict", |
| 1939 |
side_effect=lambda obj, use_cache: case["attributes_dict"], |
| 1940 |
) |
| 1941 |
result = AXObject.get_attribute(mock_accessible, case["attribute_name"]) |
| 1942 |
assert result == case["expected_result"] |
| 1943 |
|
| 1944 |
def test_grab_focus_no_component_support(self, test_context: OrcaTestContext) -> None: |
| 1945 |
"""Test AXObject.grab_focus without component support.""" |
| 1946 |
|
| 1947 |
self._setup_dependencies(test_context) |
| 1948 |
from orca.ax_object import AXObject |
| 1949 |
|
| 1950 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1951 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1952 |
test_context.patch_object(AXObject, "supports_component", return_value=False) |
| 1953 |
result = AXObject.grab_focus(mock_accessible) |
| 1954 |
assert result is False |
| 1955 |
|
| 1956 |
def test_grab_focus_basic_success(self, test_context: OrcaTestContext) -> None: |
| 1957 |
"""Test AXObject.grab_focus basic success path.""" |
| 1958 |
|
| 1959 |
self._setup_dependencies(test_context) |
| 1960 |
from orca.ax_object import AXObject |
| 1961 |
|
| 1962 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1963 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1964 |
test_context.patch_object(AXObject, "supports_component", return_value=True) |
| 1965 |
test_context.patch_object(AXObject, "has_state", return_value=False) |
| 1966 |
|
| 1967 |
def mock_grab_focus(_obj) -> bool: |
| 1968 |
from orca import debug |
| 1969 |
|
| 1970 |
debug.LEVEL_INFO = 800 |
| 1971 |
debug.debugLevel = 2 |
| 1972 |
return True |
| 1973 |
|
| 1974 |
test_context.patch("orca.ax_object.Atspi.Component.grab_focus", new=mock_grab_focus) |
| 1975 |
result = AXObject.grab_focus(mock_accessible) |
| 1976 |
assert result is True |
| 1977 |
|
| 1978 |
def test_get_child_invalid_obj(self, test_context: OrcaTestContext) -> None: |
| 1979 |
"""Test AXObject.get_child with invalid obj.""" |
| 1980 |
|
| 1981 |
self._setup_dependencies(test_context) |
| 1982 |
from orca.ax_object import AXObject |
| 1983 |
|
| 1984 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1985 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 1986 |
result = AXObject.get_child(mock_accessible, 0) |
| 1987 |
assert result is None |
| 1988 |
|
| 1989 |
def test_get_child_no_children(self, test_context: OrcaTestContext) -> None: |
| 1990 |
"""Test AXObject.get_child with no children.""" |
| 1991 |
|
| 1992 |
self._setup_dependencies(test_context) |
| 1993 |
from orca.ax_object import AXObject |
| 1994 |
|
| 1995 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1996 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 1997 |
test_context.patch_object(AXObject, "get_child_count", return_value=0) |
| 1998 |
result = AXObject.get_child(mock_accessible, 0) |
| 1999 |
assert result is None |
| 2000 |
|
| 2001 |
def test_get_child_negative_index(self, test_context: OrcaTestContext) -> None: |
| 2002 |
"""Test AXObject.get_child with -1 index (last child).""" |
| 2003 |
|
| 2004 |
self._setup_dependencies(test_context) |
| 2005 |
from orca.ax_object import AXObject |
| 2006 |
|
| 2007 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2008 |
mock_child = test_context.Mock(spec=Atspi.Accessible) |
| 2009 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 2010 |
test_context.patch_object(AXObject, "get_child_count", return_value=3) |
| 2011 |
|
| 2012 |
def mock_get_child_at_index(_obj, index) -> object: |
| 2013 |
if index == 2: |
| 2014 |
return mock_child |
| 2015 |
return None |
| 2016 |
|
| 2017 |
sys.modules["gi.repository"].Atspi.Accessible.get_child_at_index = mock_get_child_at_index |
| 2018 |
result = AXObject.get_child(mock_accessible, -1) |
| 2019 |
assert result == mock_child |
| 2020 |
|
| 2021 |
def test_get_child_valid_index(self, test_context: OrcaTestContext) -> None: |
| 2022 |
"""Test AXObject.get_child with valid index.""" |
| 2023 |
|
| 2024 |
self._setup_dependencies(test_context) |
| 2025 |
from orca.ax_object import AXObject |
| 2026 |
|
| 2027 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2028 |
mock_child = test_context.Mock(spec=Atspi.Accessible) |
| 2029 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 2030 |
test_context.patch_object(AXObject, "get_child_count", return_value=3) |
| 2031 |
sys.modules["gi.repository"].Atspi.Accessible.get_child_at_index = ( |
| 2032 |
lambda obj, idx: mock_child if idx == 1 else None |
| 2033 |
) |
| 2034 |
result = AXObject.get_child(mock_accessible, 1) |
| 2035 |
assert result == mock_child |
| 2036 |
|
| 2037 |
def test_get_role_name_invalid_obj(self, test_context: OrcaTestContext) -> None: |
| 2038 |
"""Test AXObject.get_role_name with invalid obj.""" |
| 2039 |
|
| 2040 |
self._setup_dependencies(test_context) |
| 2041 |
from orca.ax_object import AXObject |
| 2042 |
|
| 2043 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2044 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 2045 |
result = AXObject.get_role_name(mock_accessible) |
| 2046 |
assert result == "" |
| 2047 |
|
| 2048 |
def test_get_role_name_non_localized(self, test_context: OrcaTestContext) -> None: |
| 2049 |
"""Test AXObject.get_role_name non-localized.""" |
| 2050 |
|
| 2051 |
self._setup_dependencies(test_context) |
| 2052 |
from orca.ax_object import AXObject |
| 2053 |
|
| 2054 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2055 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 2056 |
sys.modules["gi.repository"].Atspi.Accessible.get_role_name = test_context.Mock( |
| 2057 |
return_value="button", |
| 2058 |
) |
| 2059 |
result = AXObject.get_role_name(mock_accessible, localized=False) |
| 2060 |
assert result == "button" |
| 2061 |
sys.modules["gi.repository"].Atspi.Accessible.get_localized_role_name = test_context.Mock( |
| 2062 |
return_value="botón", |
| 2063 |
) |
| 2064 |
result = AXObject.get_role_name(mock_accessible, localized=True) |
| 2065 |
assert result == "botón" |
| 2066 |
|
| 2067 |
def test_get_role_name_glib_error(self, test_context: OrcaTestContext) -> None: |
| 2068 |
"""Test AXObject.get_role_name handles GLib.GError.""" |
| 2069 |
|
| 2070 |
self._setup_dependencies(test_context) |
| 2071 |
from orca.ax_object import AXObject |
| 2072 |
|
| 2073 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2074 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 2075 |
handle_error_mock = test_context.Mock() |
| 2076 |
test_context.patch_object(AXObject, "handle_error", new=handle_error_mock) |
| 2077 |
sys.modules["gi.repository"].Atspi.Accessible.get_role_name = test_context.Mock( |
| 2078 |
side_effect=GLib.GError("Test error"), |
| 2079 |
) |
| 2080 |
result = AXObject.get_role_name(mock_accessible) |
| 2081 |
assert result == "" |
| 2082 |
|
| 2083 |
def test_has_same_non_empty_name_empty_name1(self, test_context: OrcaTestContext) -> None: |
| 2084 |
"""Test AXObject.has_same_non_empty_name with empty first name.""" |
| 2085 |
|
| 2086 |
self._setup_dependencies(test_context) |
| 2087 |
from orca.ax_object import AXObject |
| 2088 |
|
| 2089 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2090 |
mock_obj2 = test_context.Mock(spec=Atspi.Accessible) |
| 2091 |
|
| 2092 |
def mock_get_name(obj) -> str: |
| 2093 |
if obj == mock_accessible: |
| 2094 |
return "" |
| 2095 |
return "test" |
| 2096 |
|
| 2097 |
test_context.patch_object(AXObject, "get_name", new=mock_get_name) |
| 2098 |
result = AXObject.has_same_non_empty_name(mock_accessible, mock_obj2) |
| 2099 |
assert result is False |
| 2100 |
|
| 2101 |
def test_has_same_non_empty_name_same_names(self, test_context: OrcaTestContext) -> None: |
| 2102 |
"""Test AXObject.has_same_non_empty_name with same names.""" |
| 2103 |
|
| 2104 |
self._setup_dependencies(test_context) |
| 2105 |
from orca.ax_object import AXObject |
| 2106 |
|
| 2107 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2108 |
mock_obj2 = test_context.Mock(spec=Atspi.Accessible) |
| 2109 |
test_context.patch_object(AXObject, "get_name", return_value="test") |
| 2110 |
result = AXObject.has_same_non_empty_name(mock_accessible, mock_obj2) |
| 2111 |
assert result is True |
| 2112 |
|
| 2113 |
@pytest.mark.parametrize( |
| 2114 |
"case", |
| 2115 |
[ |
| 2116 |
{ |
| 2117 |
"id": "desc_invalid_obj", |
| 2118 |
"method_name": "get_description", |
| 2119 |
"scenario": "invalid_obj", |
| 2120 |
"is_valid": False, |
| 2121 |
"atspi_method": "get_description", |
| 2122 |
"expected_result": "", |
| 2123 |
"expects_handle_error": False, |
| 2124 |
}, |
| 2125 |
{ |
| 2126 |
"id": "desc_success", |
| 2127 |
"method_name": "get_description", |
| 2128 |
"scenario": "success", |
| 2129 |
"is_valid": True, |
| 2130 |
"atspi_method": "get_description", |
| 2131 |
"expected_result": "Test description", |
| 2132 |
"expects_handle_error": False, |
| 2133 |
}, |
| 2134 |
{ |
| 2135 |
"id": "desc_glib_error", |
| 2136 |
"method_name": "get_description", |
| 2137 |
"scenario": "glib_error", |
| 2138 |
"is_valid": True, |
| 2139 |
"atspi_method": "get_description", |
| 2140 |
"expected_result": "", |
| 2141 |
"expects_handle_error": True, |
| 2142 |
}, |
| 2143 |
{ |
| 2144 |
"id": "child_count_invalid_obj", |
| 2145 |
"method_name": "get_child_count", |
| 2146 |
"scenario": "invalid_obj", |
| 2147 |
"is_valid": False, |
| 2148 |
"atspi_method": "get_child_count", |
| 2149 |
"expected_result": 0, |
| 2150 |
"expects_handle_error": False, |
| 2151 |
}, |
| 2152 |
{ |
| 2153 |
"id": "child_count_success", |
| 2154 |
"method_name": "get_child_count", |
| 2155 |
"scenario": "success", |
| 2156 |
"is_valid": True, |
| 2157 |
"atspi_method": "get_child_count", |
| 2158 |
"expected_result": 3, |
| 2159 |
"expects_handle_error": False, |
| 2160 |
}, |
| 2161 |
{ |
| 2162 |
"id": "child_count_glib_error", |
| 2163 |
"method_name": "get_child_count", |
| 2164 |
"scenario": "glib_error", |
| 2165 |
"is_valid": True, |
| 2166 |
"atspi_method": "get_child_count", |
| 2167 |
"expected_result": 0, |
| 2168 |
"expects_handle_error": True, |
| 2169 |
}, |
| 2170 |
{ |
| 2171 |
"id": "img_desc_invalid_obj", |
| 2172 |
"method_name": "get_image_description", |
| 2173 |
"scenario": "invalid_obj", |
| 2174 |
"is_valid": False, |
| 2175 |
"atspi_method": "get_image_description", |
| 2176 |
"expected_result": "", |
| 2177 |
"expects_handle_error": False, |
| 2178 |
}, |
| 2179 |
{ |
| 2180 |
"id": "img_desc_success", |
| 2181 |
"method_name": "get_image_description", |
| 2182 |
"scenario": "success", |
| 2183 |
"is_valid": True, |
| 2184 |
"atspi_method": "get_image_description", |
| 2185 |
"expected_result": "Image description", |
| 2186 |
"expects_handle_error": False, |
| 2187 |
}, |
| 2188 |
{ |
| 2189 |
"id": "img_desc_glib_error", |
| 2190 |
"method_name": "get_image_description", |
| 2191 |
"scenario": "glib_error", |
| 2192 |
"is_valid": True, |
| 2193 |
"atspi_method": "get_image_description", |
| 2194 |
"expected_result": "", |
| 2195 |
"expects_handle_error": True, |
| 2196 |
}, |
| 2197 |
], |
| 2198 |
ids=lambda case: case["id"], |
| 2199 |
) |
| 2200 |
def test_getter_methods_with_error_handling_scenarios( |
| 2201 |
self, |
| 2202 |
test_context, |
| 2203 |
case: dict, |
| 2204 |
) -> None: |
| 2205 |
"""Test AXObject getter methods with invalid_obj/success/glib_error scenarios.""" |
| 2206 |
|
| 2207 |
self._setup_dependencies(test_context) |
| 2208 |
from orca.ax_object import AXObject |
| 2209 |
|
| 2210 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2211 |
test_context.patch_object(AXObject, "is_valid", side_effect=lambda obj: case["is_valid"]) |
| 2212 |
|
| 2213 |
if case["method_name"] == "get_image_description": |
| 2214 |
if case["scenario"] == "invalid_obj": |
| 2215 |
test_context.patch_object(AXObject, "supports_image", return_value=False) |
| 2216 |
else: |
| 2217 |
test_context.patch_object(AXObject, "supports_image", return_value=True) |
| 2218 |
|
| 2219 |
if case["scenario"] == "glib_error": |
| 2220 |
handle_error_mock = test_context.Mock() |
| 2221 |
test_context.patch_object(AXObject, "handle_error", new=handle_error_mock) |
| 2222 |
glib_error = GLib.GError("Test error") |
| 2223 |
if case["method_name"] == "get_image_description": |
| 2224 |
sys.modules["gi.repository"].Atspi.Image.get_image_description = test_context.Mock( |
| 2225 |
side_effect=glib_error, |
| 2226 |
) |
| 2227 |
else: |
| 2228 |
setattr( |
| 2229 |
sys.modules["gi.repository"].Atspi.Accessible, |
| 2230 |
case["atspi_method"], |
| 2231 |
test_context.Mock(side_effect=glib_error), |
| 2232 |
) |
| 2233 |
elif case["scenario"] == "success": |
| 2234 |
if case["method_name"] == "get_image_description": |
| 2235 |
sys.modules["gi.repository"].Atspi.Image.get_image_description = test_context.Mock( |
| 2236 |
return_value=case["expected_result"], |
| 2237 |
) |
| 2238 |
else: |
| 2239 |
setattr( |
| 2240 |
sys.modules["gi.repository"].Atspi.Accessible, |
| 2241 |
case["atspi_method"], |
| 2242 |
test_context.Mock(return_value=case["expected_result"]), |
| 2243 |
) |
| 2244 |
|
| 2245 |
method_to_test = getattr(AXObject, case["method_name"]) |
| 2246 |
result = method_to_test(mock_accessible) |
| 2247 |
assert result == case["expected_result"] |
| 2248 |
|
| 2249 |
if case["scenario"] == "glib_error" and case["expects_handle_error"]: |
| 2250 |
handle_error_mock.assert_called_once() |
| 2251 |
|
| 2252 |
@pytest.mark.parametrize( |
| 2253 |
"case", |
| 2254 |
[ |
| 2255 |
{ |
| 2256 |
"id": "action_invalid_obj", |
| 2257 |
"interface_name": "action", |
| 2258 |
"getter_method": "get_action_iface", |
| 2259 |
"has_error": False, |
| 2260 |
"expected_result": False, |
| 2261 |
"expects_handle_error": False, |
| 2262 |
}, |
| 2263 |
{ |
| 2264 |
"id": "component_invalid_obj", |
| 2265 |
"interface_name": "component", |
| 2266 |
"getter_method": "get_component_iface", |
| 2267 |
"has_error": False, |
| 2268 |
"expected_result": False, |
| 2269 |
"expects_handle_error": False, |
| 2270 |
}, |
| 2271 |
{ |
| 2272 |
"id": "component_glib_error", |
| 2273 |
"interface_name": "component", |
| 2274 |
"getter_method": "get_component_iface", |
| 2275 |
"has_error": True, |
| 2276 |
"expected_result": False, |
| 2277 |
"expects_handle_error": True, |
| 2278 |
}, |
| 2279 |
], |
| 2280 |
ids=lambda case: case["id"], |
| 2281 |
) |
| 2282 |
def test_supports_interface_error_paths( |
| 2283 |
self, |
| 2284 |
test_context: OrcaTestContext, |
| 2285 |
case: dict, |
| 2286 |
) -> None: |
| 2287 |
"""Test AXObject.supports_* interface methods error paths.""" |
| 2288 |
|
| 2289 |
self._setup_dependencies(test_context) |
| 2290 |
from orca.ax_object import AXObject |
| 2291 |
|
| 2292 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2293 |
handle_error_mock = test_context.Mock() |
| 2294 |
test_context.patch_object(AXObject, "handle_error", new=handle_error_mock) |
| 2295 |
|
| 2296 |
method = getattr(AXObject, f"supports_{case['interface_name']}") |
| 2297 |
|
| 2298 |
if case["has_error"]: |
| 2299 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 2300 |
setattr( |
| 2301 |
sys.modules["gi.repository"].Atspi.Accessible, |
| 2302 |
case["getter_method"], |
| 2303 |
test_context.Mock(side_effect=GLib.GError("Test error")), |
| 2304 |
) |
| 2305 |
else: |
| 2306 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 2307 |
|
| 2308 |
result = method(mock_accessible) |
| 2309 |
assert result is case["expected_result"] |
| 2310 |
if case["expects_handle_error"]: |
| 2311 |
handle_error_mock.assert_called_once() |
| 2312 |
|
| 2313 |
def test_has_document_spreadsheet_import_path(self, test_context: OrcaTestContext) -> None: |
| 2314 |
"""Test AXObject._has_document_spreadsheet import path.""" |
| 2315 |
|
| 2316 |
self._setup_dependencies(test_context) |
| 2317 |
from orca.ax_object import AXObject |
| 2318 |
|
| 2319 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2320 |
mock_ax_collection = test_context.Mock() |
| 2321 |
mock_ax_collection.AXCollection = test_context.Mock() |
| 2322 |
mock_ax_collection.AXCollection.create_match_rule = test_context.Mock(return_value=None) |
| 2323 |
sys.modules["orca.ax_collection"] = mock_ax_collection |
| 2324 |
result = AXObject._has_document_spreadsheet(mock_accessible) |
| 2325 |
assert result is False |
| 2326 |
|
| 2327 |
def test_has_document_spreadsheet_no_frame_path(self, test_context: OrcaTestContext) -> None: |
| 2328 |
"""Test AXObject._has_document_spreadsheet no frame path.""" |
| 2329 |
|
| 2330 |
self._setup_dependencies(test_context) |
| 2331 |
from orca.ax_object import AXObject |
| 2332 |
|
| 2333 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2334 |
mock_ax_collection = test_context.Mock() |
| 2335 |
mock_ax_collection.AXCollection = test_context.Mock() |
| 2336 |
mock_ax_collection.AXCollection.create_match_rule = test_context.Mock( |
| 2337 |
return_value="mock_rule", |
| 2338 |
) |
| 2339 |
sys.modules["orca.ax_collection"] = mock_ax_collection |
| 2340 |
test_context.patch_object(AXObject, "_find_ancestor_with_role", return_value=None) |
| 2341 |
result = AXObject._has_document_spreadsheet(mock_accessible) |
| 2342 |
assert result is False |
| 2343 |
|
| 2344 |
def test_has_document_spreadsheet_with_matches_path( |
| 2345 |
self, |
| 2346 |
test_context: OrcaTestContext, |
| 2347 |
) -> None: |
| 2348 |
"""Test AXObject._has_document_spreadsheet with matches path.""" |
| 2349 |
|
| 2350 |
self._setup_dependencies(test_context) |
| 2351 |
from orca.ax_object import AXObject |
| 2352 |
|
| 2353 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2354 |
mock_frame = test_context.Mock(spec=Atspi.Accessible) |
| 2355 |
mock_ax_collection = test_context.Mock() |
| 2356 |
mock_ax_collection.AXCollection = test_context.Mock() |
| 2357 |
mock_ax_collection.AXCollection.create_match_rule = test_context.Mock( |
| 2358 |
return_value="mock_rule", |
| 2359 |
) |
| 2360 |
sys.modules["orca.ax_collection"] = mock_ax_collection |
| 2361 |
test_context.patch_object(AXObject, "_find_ancestor_with_role", return_value=mock_frame) |
| 2362 |
sys.modules["gi.repository"].Atspi.Collection.get_matches = test_context.Mock( |
| 2363 |
return_value=["match1"], |
| 2364 |
) |
| 2365 |
result = AXObject._has_document_spreadsheet(mock_accessible) |
| 2366 |
assert result is True |
| 2367 |
|
| 2368 |
def test_get_parent_checked_invalid_obj(self, test_context: OrcaTestContext) -> None: |
| 2369 |
"""Test AXObject.get_parent_checked with invalid obj.""" |
| 2370 |
|
| 2371 |
self._setup_dependencies(test_context) |
| 2372 |
from orca.ax_object import AXObject |
| 2373 |
|
| 2374 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2375 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 2376 |
result = AXObject.get_parent_checked(mock_accessible) |
| 2377 |
assert result is None |
| 2378 |
|
| 2379 |
def test_get_parent_checked_invalid_role(self, test_context: OrcaTestContext) -> None: |
| 2380 |
"""Test AXObject.get_parent_checked with invalid role.""" |
| 2381 |
|
| 2382 |
self._setup_dependencies(test_context) |
| 2383 |
from orca.ax_object import AXObject |
| 2384 |
|
| 2385 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2386 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 2387 |
test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.INVALID) |
| 2388 |
result = AXObject.get_parent_checked(mock_accessible) |
| 2389 |
assert result is None |
| 2390 |
|
| 2391 |
def test_has_document_spreadsheet_with_invalid_obj(self, test_context: OrcaTestContext) -> None: |
| 2392 |
"""Test AXObject._has_document_spreadsheet with invalid object.""" |
| 2393 |
|
| 2394 |
self._setup_dependencies(test_context) |
| 2395 |
from orca.ax_object import AXObject |
| 2396 |
|
| 2397 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2398 |
test_context.patch_object(AXObject, "is_valid", return_value=False) |
| 2399 |
result = AXObject._has_document_spreadsheet(mock_accessible) |
| 2400 |
assert result is False |
| 2401 |
|
| 2402 |
def test_has_document_spreadsheet_with_valid_obj(self, test_context: OrcaTestContext) -> None: |
| 2403 |
"""Test AXObject._has_document_spreadsheet with valid object.""" |
| 2404 |
|
| 2405 |
self._setup_dependencies(test_context) |
| 2406 |
from orca.ax_object import AXObject |
| 2407 |
|
| 2408 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2409 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 2410 |
mock_spreadsheet = test_context.Mock(spec=Atspi.Accessible) |
| 2411 |
test_context.patch_object( |
| 2412 |
AXObject, |
| 2413 |
"_find_ancestor_with_role", |
| 2414 |
return_value=mock_spreadsheet, |
| 2415 |
) |
| 2416 |
result = AXObject._has_document_spreadsheet(mock_accessible) |
| 2417 |
assert result is True |
| 2418 |
|
| 2419 |
def test_has_document_spreadsheet_no_spreadsheet(self, test_context: OrcaTestContext) -> None: |
| 2420 |
"""Test AXObject._has_document_spreadsheet with no spreadsheet.""" |
| 2421 |
|
| 2422 |
self._setup_dependencies(test_context) |
| 2423 |
from orca.ax_object import AXObject |
| 2424 |
|
| 2425 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2426 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 2427 |
test_context.patch_object(AXObject, "_find_ancestor_with_role", return_value=None) |
| 2428 |
result = AXObject._has_document_spreadsheet(mock_accessible) |
| 2429 |
assert result is False |
| 2430 |
|
| 2431 |
@pytest.mark.parametrize( |
| 2432 |
"interface_type,has_interface,expected", |
| 2433 |
[ |
| 2434 |
("document", True, True), |
| 2435 |
("document", False, False), |
| 2436 |
("editable_text", True, True), |
| 2437 |
("editable_text", False, False), |
| 2438 |
("hyperlink", True, True), |
| 2439 |
("hyperlink", False, False), |
| 2440 |
("image", True, True), |
| 2441 |
("image", False, False), |
| 2442 |
("selection", True, True), |
| 2443 |
("selection", False, False), |
| 2444 |
("table", True, True), |
| 2445 |
("table", False, False), |
| 2446 |
], |
| 2447 |
) |
| 2448 |
def test_supports_interface( |
| 2449 |
self, |
| 2450 |
test_context: OrcaTestContext, |
| 2451 |
interface_type: str, |
| 2452 |
has_interface: bool, |
| 2453 |
expected: bool, |
| 2454 |
) -> None: |
| 2455 |
"""Test AXObject.supports_* interface methods with various interface types.""" |
| 2456 |
|
| 2457 |
self._setup_dependencies(test_context) |
| 2458 |
from orca.ax_object import AXObject |
| 2459 |
|
| 2460 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2461 |
test_context.patch_object(AXObject, "is_valid", return_value=True) |
| 2462 |
|
| 2463 |
interface_map = { |
| 2464 |
"document": ( |
| 2465 |
"get_document_iface", |
| 2466 |
AXObject.supports_document, |
| 2467 |
), |
| 2468 |
"editable_text": ( |
| 2469 |
"get_editable_text_iface", |
| 2470 |
AXObject.supports_editable_text, |
| 2471 |
), |
| 2472 |
"hyperlink": ( |
| 2473 |
"get_hyperlink", |
| 2474 |
AXObject.supports_hyperlink, |
| 2475 |
), |
| 2476 |
"image": ( |
| 2477 |
"get_image_iface", |
| 2478 |
AXObject.supports_image, |
| 2479 |
), |
| 2480 |
"selection": ( |
| 2481 |
"get_selection_iface", |
| 2482 |
AXObject.supports_selection, |
| 2483 |
), |
| 2484 |
"table": ( |
| 2485 |
"get_table_iface", |
| 2486 |
AXObject.supports_table, |
| 2487 |
), |
| 2488 |
} |
| 2489 |
|
| 2490 |
getter_method, test_method = interface_map[interface_type] |
| 2491 |
mock_iface = test_context.Mock() if has_interface else None |
| 2492 |
setattr( |
| 2493 |
sys.modules["gi.repository"].Atspi.Accessible, |
| 2494 |
getter_method, |
| 2495 |
test_context.Mock(return_value=mock_iface), |
| 2496 |
) |
| 2497 |
|
| 2498 |
result = test_method(mock_accessible) |
| 2499 |
assert result is expected |
| 2500 |
|
| 2501 |
@pytest.mark.parametrize( |
| 2502 |
"case", |
| 2503 |
[ |
| 2504 |
{"id": "valid_image_size", "image_support": True, "expected_result": (100, 200)}, |
| 2505 |
{"id": "no_image_support", "image_support": False, "expected_result": (0, 0)}, |
| 2506 |
], |
| 2507 |
ids=lambda case: case["id"], |
| 2508 |
) |
| 2509 |
def test_get_image_size_various_conditions( |
| 2510 |
self, |
| 2511 |
test_context: OrcaTestContext, |
| 2512 |
case: dict, |
| 2513 |
) -> None: |
| 2514 |
"""Test AXObject.get_image_size with various conditions.""" |
| 2515 |
|
| 2516 |
self._setup_dependencies(test_context) |
| 2517 |
from orca.ax_object import AXObject |
| 2518 |
|
| 2519 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2520 |
|
| 2521 |
test_context.patch_object(AXObject, "supports_image", return_value=case["image_support"]) |
| 2522 |
|
| 2523 |
if case["image_support"]: |
| 2524 |
mock_point = test_context.Mock() |
| 2525 |
mock_point.x = case["expected_result"][0] |
| 2526 |
mock_point.y = case["expected_result"][1] |
| 2527 |
|
| 2528 |
atspi_image_mock = test_context.Mock() |
| 2529 |
atspi_image_mock.get_image_size.return_value = mock_point |
| 2530 |
test_context.patch("orca.ax_object.Atspi.Image", new=atspi_image_mock) |
| 2531 |
|
| 2532 |
result = AXObject.get_image_size(mock_accessible) |
| 2533 |
assert result == case["expected_result"] |
| 2534 |
|
| 2535 |
@pytest.mark.parametrize( |
| 2536 |
"case", |
| 2537 |
[ |
| 2538 |
{"id": "atspi_error_fallback", "atspi_error": True, "expected_result": (0, 0)}, |
| 2539 |
{"id": "successful_retrieval", "atspi_error": False, "expected_result": (150, 300)}, |
| 2540 |
], |
| 2541 |
ids=lambda case: case["id"], |
| 2542 |
) |
| 2543 |
def test_get_image_size_error_handling(self, test_context: OrcaTestContext, case: dict) -> None: |
| 2544 |
"""Test AXObject.get_image_size error handling.""" |
| 2545 |
|
| 2546 |
self._setup_dependencies(test_context) |
| 2547 |
from orca.ax_object import AXObject |
| 2548 |
|
| 2549 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2550 |
|
| 2551 |
test_context.patch_object(AXObject, "supports_image", return_value=True) |
| 2552 |
|
| 2553 |
test_context.patch_object(AXObject, "handle_error", return_value=None) |
| 2554 |
|
| 2555 |
atspi_image_mock = test_context.Mock() |
| 2556 |
if case["atspi_error"]: |
| 2557 |
atspi_image_mock.get_image_size.side_effect = GLib.GError("Image error") |
| 2558 |
else: |
| 2559 |
mock_point = test_context.Mock() |
| 2560 |
mock_point.x = case["expected_result"][0] |
| 2561 |
mock_point.y = case["expected_result"][1] |
| 2562 |
atspi_image_mock.get_image_size.return_value = mock_point |
| 2563 |
|
| 2564 |
test_context.patch("orca.ax_object.Atspi.Image", new=atspi_image_mock) |
| 2565 |
|
| 2566 |
result = AXObject.get_image_size(mock_accessible) |
| 2567 |
assert result == case["expected_result"] |
| 2568 |
|
| 2569 |
@pytest.mark.parametrize( |
| 2570 |
"case", |
| 2571 |
[ |
| 2572 |
{ |
| 2573 |
"id": "atspi_help_text", |
| 2574 |
"valid_obj": True, |
| 2575 |
"atspi_error": False, |
| 2576 |
"fallback_attr": None, |
| 2577 |
"expected_result": "Official help text", |
| 2578 |
}, |
| 2579 |
{ |
| 2580 |
"id": "fallback_to_attribute", |
| 2581 |
"valid_obj": True, |
| 2582 |
"atspi_error": True, |
| 2583 |
"fallback_attr": "Fallback help", |
| 2584 |
"expected_result": "Fallback help", |
| 2585 |
}, |
| 2586 |
{ |
| 2587 |
"id": "no_help_available", |
| 2588 |
"valid_obj": True, |
| 2589 |
"atspi_error": True, |
| 2590 |
"fallback_attr": None, |
| 2591 |
"expected_result": "", |
| 2592 |
}, |
| 2593 |
{ |
| 2594 |
"id": "invalid_object", |
| 2595 |
"valid_obj": False, |
| 2596 |
"atspi_error": False, |
| 2597 |
"fallback_attr": None, |
| 2598 |
"expected_result": "", |
| 2599 |
}, |
| 2600 |
], |
| 2601 |
ids=lambda case: case["id"], |
| 2602 |
) |
| 2603 |
def test_get_help_text_various_conditions( |
| 2604 |
self, |
| 2605 |
test_context: OrcaTestContext, |
| 2606 |
case: dict, |
| 2607 |
) -> None: |
| 2608 |
"""Test AXObject.get_help_text with various conditions.""" |
| 2609 |
|
| 2610 |
self._setup_dependencies(test_context) |
| 2611 |
from orca.ax_object import AXObject |
| 2612 |
|
| 2613 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 2614 |
|
| 2615 |
test_context.patch_object(AXObject, "is_valid", side_effect=lambda obj: case["valid_obj"]) |
| 2616 |
|
| 2617 |
if case["valid_obj"]: |
| 2618 |
test_context.patch_object( |
| 2619 |
AXObject, |
| 2620 |
"get_attribute", |
| 2621 |
side_effect=lambda obj, attr: case["fallback_attr"] if attr == "helptext" else None, |
| 2622 |
) |
| 2623 |
|
| 2624 |
atspi_accessible_mock = test_context.Mock() |
| 2625 |
if case["atspi_error"]: |
| 2626 |
atspi_accessible_mock.get_help_text.side_effect = GLib.GError("Help text error") |
| 2627 |
else: |
| 2628 |
atspi_accessible_mock.get_help_text.return_value = case["expected_result"] |
| 2629 |
|
| 2630 |
test_context.patch("orca.ax_object.Atspi.Accessible", new=atspi_accessible_mock) |
| 2631 |
|
| 2632 |
result = AXObject.get_help_text(mock_accessible) |
| 2633 |
assert result == case["expected_result"] |
| 2634 |
|
| 2635 |
@pytest.mark.parametrize( |
| 2636 |
"case", |
| 2637 |
[ |
| 2638 |
{ |
| 2639 |
"id": "invalid_object", |
| 2640 |
"is_valid": False, |
| 2641 |
"has_parent": True, |
| 2642 |
"index": 1, |
| 2643 |
"has_sibling": True, |
| 2644 |
"sibling_is_self": False, |
| 2645 |
"expected_result": None, |
| 2646 |
}, |
| 2647 |
{ |
| 2648 |
"id": "no_parent", |
| 2649 |
"is_valid": True, |
| 2650 |
"has_parent": False, |
| 2651 |
"index": 1, |
| 2652 |
"has_sibling": True, |
| 2653 |
"sibling_is_self": False, |
| 2654 |
"expected_result": None, |
| 2655 |
}, |
| 2656 |
{ |
| 2657 |
"id": "first_child_index_zero", |
| 2658 |
"is_valid": True, |
| 2659 |
"has_parent": True, |
| 2660 |
"index": 0, |
| 2661 |
"has_sibling": True, |
| 2662 |
"sibling_is_self": False, |
| 2663 |
"expected_result": None, |
| 2664 |
}, |
| 2665 |
{ |
| 2666 |
"id": "invalid_negative_index", |
| 2667 |
"is_valid": True, |
| 2668 |
"has_parent": True, |
| 2669 |
"index": -1, |
| 2670 |
"has_sibling": True, |
| 2671 |
"sibling_is_self": False, |
| 2672 |
"expected_result": None, |
| 2673 |
}, |
| 2674 |
{ |
| 2675 |
"id": "valid_previous_sibling", |
| 2676 |
"is_valid": True, |
| 2677 |
"has_parent": True, |
| 2678 |
"index": 2, |
| 2679 |
"has_sibling": True, |
| 2680 |
"sibling_is_self": False, |
| 2681 |
"expected_result": "sibling", |
| 2682 |
}, |
| 2683 |
{ |
| 2684 |
"id": "self_referential_sibling", |
| 2685 |
"is_valid": True, |
| 2686 |
"has_parent": True, |
| 2687 |
"index": 1, |
| 2688 |
"has_sibling": True, |
| 2689 |
"sibling_is_self": True, |
| 2690 |
"expected_result": None, |
| 2691 |
}, |
| 2692 |
], |
| 2693 |
ids=lambda case: case["id"], |
| 2694 |
) |
| 2695 |
def test_get_previous_sibling( |
| 2696 |
self, |
| 2697 |
test_context: OrcaTestContext, |
| 2698 |
case: dict, |
| 2699 |
) -> None: |
| 2700 |
"""Test AXObject.get_previous_sibling with various conditions.""" |
| 2701 |
|
| 2702 |
self._setup_dependencies(test_context) |
| 2703 |
from orca.ax_object import AXObject |
| 2704 |
|
| 2705 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 2706 |
mock_parent = test_context.Mock(spec=Atspi.Accessible) if case["has_parent"] else None |
| 2707 |
mock_sibling = ( |
| 2708 |
mock_obj if case["sibling_is_self"] else test_context.Mock(spec=Atspi.Accessible) |
| 2709 |
) |
| 2710 |
|
| 2711 |
expected_result = case["expected_result"] |
| 2712 |
if expected_result == "sibling" and not case["sibling_is_self"]: |
| 2713 |
expected_result = mock_sibling |
| 2714 |
|
| 2715 |
test_context.patch_object(AXObject, "is_valid", side_effect=lambda obj: case["is_valid"]) |
| 2716 |
test_context.patch_object(AXObject, "get_parent", return_value=mock_parent) |
| 2717 |
test_context.patch_object( |
| 2718 |
AXObject, |
| 2719 |
"get_index_in_parent", |
| 2720 |
side_effect=lambda obj: case["index"], |
| 2721 |
) |
| 2722 |
test_context.patch_object( |
| 2723 |
AXObject, |
| 2724 |
"get_child", |
| 2725 |
side_effect=lambda parent, idx: mock_sibling if case["has_sibling"] else None, |
| 2726 |
) |
| 2727 |
|
| 2728 |
result = AXObject.get_previous_sibling(mock_obj) |
| 2729 |
assert result == expected_result |
| 2730 |
|
| 2731 |
@pytest.mark.parametrize( |
| 2732 |
"case", |
| 2733 |
[ |
| 2734 |
{ |
| 2735 |
"id": "invalid_object", |
| 2736 |
"is_valid": False, |
| 2737 |
"has_parent": True, |
| 2738 |
"index": 1, |
| 2739 |
"has_sibling": True, |
| 2740 |
"sibling_is_self": False, |
| 2741 |
"expected_result": None, |
| 2742 |
}, |
| 2743 |
{ |
| 2744 |
"id": "no_parent", |
| 2745 |
"is_valid": True, |
| 2746 |
"has_parent": False, |
| 2747 |
"index": 1, |
| 2748 |
"has_sibling": True, |
| 2749 |
"sibling_is_self": False, |
| 2750 |
"expected_result": None, |
| 2751 |
}, |
| 2752 |
{ |
| 2753 |
"id": "invalid_negative_index", |
| 2754 |
"is_valid": True, |
| 2755 |
"has_parent": True, |
| 2756 |
"index": -1, |
| 2757 |
"has_sibling": True, |
| 2758 |
"sibling_is_self": False, |
| 2759 |
"expected_result": None, |
| 2760 |
}, |
| 2761 |
{ |
| 2762 |
"id": "valid_next_sibling", |
| 2763 |
"is_valid": True, |
| 2764 |
"has_parent": True, |
| 2765 |
"index": 1, |
| 2766 |
"has_sibling": True, |
| 2767 |
"sibling_is_self": False, |
| 2768 |
"expected_result": "sibling", |
| 2769 |
}, |
| 2770 |
{ |
| 2771 |
"id": "self_referential_sibling", |
| 2772 |
"is_valid": True, |
| 2773 |
"has_parent": True, |
| 2774 |
"index": 1, |
| 2775 |
"has_sibling": True, |
| 2776 |
"sibling_is_self": True, |
| 2777 |
"expected_result": None, |
| 2778 |
}, |
| 2779 |
{ |
| 2780 |
"id": "no_next_sibling_available", |
| 2781 |
"is_valid": True, |
| 2782 |
"has_parent": True, |
| 2783 |
"index": 1, |
| 2784 |
"has_sibling": False, |
| 2785 |
"sibling_is_self": False, |
| 2786 |
"expected_result": None, |
| 2787 |
}, |
| 2788 |
], |
| 2789 |
ids=lambda case: case["id"], |
| 2790 |
) |
| 2791 |
def test_get_next_sibling( |
| 2792 |
self, |
| 2793 |
test_context: OrcaTestContext, |
| 2794 |
case: dict, |
| 2795 |
) -> None: |
| 2796 |
"""Test AXObject.get_next_sibling with various conditions.""" |
| 2797 |
|
| 2798 |
self._setup_dependencies(test_context) |
| 2799 |
from orca.ax_object import AXObject |
| 2800 |
|
| 2801 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 2802 |
mock_parent = test_context.Mock(spec=Atspi.Accessible) if case["has_parent"] else None |
| 2803 |
mock_sibling = ( |
| 2804 |
mock_obj if case["sibling_is_self"] else test_context.Mock(spec=Atspi.Accessible) |
| 2805 |
) |
| 2806 |
|
| 2807 |
expected_result = case["expected_result"] |
| 2808 |
if expected_result == "sibling" and not case["sibling_is_self"]: |
| 2809 |
expected_result = mock_sibling |
| 2810 |
|
| 2811 |
test_context.patch_object(AXObject, "is_valid", side_effect=lambda obj: case["is_valid"]) |
| 2812 |
test_context.patch_object(AXObject, "get_parent", return_value=mock_parent) |
| 2813 |
test_context.patch_object( |
| 2814 |
AXObject, |
| 2815 |
"get_index_in_parent", |
| 2816 |
side_effect=lambda obj: case["index"], |
| 2817 |
) |
| 2818 |
test_context.patch_object( |
| 2819 |
AXObject, |
| 2820 |
"get_child", |
| 2821 |
side_effect=lambda parent, idx: mock_sibling if case["has_sibling"] else None, |
| 2822 |
) |
| 2823 |
|
| 2824 |
result = AXObject.get_next_sibling(mock_obj) |
| 2825 |
assert result == expected_result |
| 2826 |
|
| 2827 |
@pytest.mark.parametrize( |
| 2828 |
"case", |
| 2829 |
[ |
| 2830 |
{ |
| 2831 |
"id": "invalid_object", |
| 2832 |
"is_valid": False, |
| 2833 |
"child_count": 3, |
| 2834 |
"children_valid": True, |
| 2835 |
"becomes_invalid": False, |
| 2836 |
"predicate_filter": None, |
| 2837 |
"expected_count": 0, |
| 2838 |
"warns_large": False, |
| 2839 |
}, |
| 2840 |
{ |
| 2841 |
"id": "no_children", |
| 2842 |
"is_valid": True, |
| 2843 |
"child_count": 0, |
| 2844 |
"children_valid": True, |
| 2845 |
"becomes_invalid": False, |
| 2846 |
"predicate_filter": None, |
| 2847 |
"expected_count": 0, |
| 2848 |
"warns_large": False, |
| 2849 |
}, |
| 2850 |
{ |
| 2851 |
"id": "all_valid_children", |
| 2852 |
"is_valid": True, |
| 2853 |
"child_count": 3, |
| 2854 |
"children_valid": True, |
| 2855 |
"becomes_invalid": False, |
| 2856 |
"predicate_filter": None, |
| 2857 |
"expected_count": 3, |
| 2858 |
"warns_large": False, |
| 2859 |
}, |
| 2860 |
{ |
| 2861 |
"id": "becomes_invalid_during_iteration", |
| 2862 |
"is_valid": True, |
| 2863 |
"child_count": 3, |
| 2864 |
"children_valid": True, |
| 2865 |
"becomes_invalid": True, |
| 2866 |
"predicate_filter": None, |
| 2867 |
"expected_count": 1, |
| 2868 |
"warns_large": False, |
| 2869 |
}, |
| 2870 |
{ |
| 2871 |
"id": "with_predicate_filter", |
| 2872 |
"is_valid": True, |
| 2873 |
"child_count": 3, |
| 2874 |
"children_valid": True, |
| 2875 |
"becomes_invalid": False, |
| 2876 |
"predicate_filter": "filter_odd", |
| 2877 |
"expected_count": 2, |
| 2878 |
"warns_large": False, |
| 2879 |
}, |
| 2880 |
{ |
| 2881 |
"id": "large_child_count_warning", |
| 2882 |
"is_valid": True, |
| 2883 |
"child_count": 600, |
| 2884 |
"children_valid": True, |
| 2885 |
"becomes_invalid": False, |
| 2886 |
"predicate_filter": None, |
| 2887 |
"expected_count": 600, |
| 2888 |
"warns_large": True, |
| 2889 |
}, |
| 2890 |
{ |
| 2891 |
"id": "all_children_none", |
| 2892 |
"is_valid": True, |
| 2893 |
"child_count": 2, |
| 2894 |
"children_valid": False, |
| 2895 |
"becomes_invalid": False, |
| 2896 |
"predicate_filter": None, |
| 2897 |
"expected_count": 0, |
| 2898 |
"warns_large": False, |
| 2899 |
}, |
| 2900 |
], |
| 2901 |
ids=lambda case: case["id"], |
| 2902 |
) |
| 2903 |
def test_iter_children( # pylint: disable=too-many-locals |
| 2904 |
self, |
| 2905 |
test_context: OrcaTestContext, |
| 2906 |
case: dict, |
| 2907 |
) -> None: |
| 2908 |
"""Test AXObject.iter_children with various conditions.""" |
| 2909 |
|
| 2910 |
self._setup_dependencies(test_context) |
| 2911 |
from orca.ax_object import AXObject |
| 2912 |
|
| 2913 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 2914 |
|
| 2915 |
mock_children = [] |
| 2916 |
for i in range(case["child_count"]): |
| 2917 |
child = test_context.Mock(spec=Atspi.Accessible) if case["children_valid"] else None |
| 2918 |
if child: |
| 2919 |
child.index = i |
| 2920 |
mock_children.append(child) |
| 2921 |
|
| 2922 |
valid_calls = [0] |
| 2923 |
|
| 2924 |
def mock_is_valid(obj): |
| 2925 |
if obj == mock_obj: |
| 2926 |
if case["becomes_invalid"] and valid_calls[0] > 0: |
| 2927 |
return False |
| 2928 |
valid_calls[0] += 1 |
| 2929 |
return case["is_valid"] |
| 2930 |
return True |
| 2931 |
|
| 2932 |
child_calls = [0] |
| 2933 |
|
| 2934 |
def mock_get_child(parent, index): # pylint: disable=unused-argument |
| 2935 |
if case["becomes_invalid"] and child_calls[0] >= 1: |
| 2936 |
return None |
| 2937 |
child_calls[0] += 1 |
| 2938 |
if index < len(mock_children): |
| 2939 |
return mock_children[index] |
| 2940 |
return None |
| 2941 |
|
| 2942 |
test_context.patch_object(AXObject, "is_valid", new=mock_is_valid) |
| 2943 |
test_context.patch_object(AXObject, "get_child_count", return_value=case["child_count"]) |
| 2944 |
test_context.patch_object(AXObject, "get_child", new=mock_get_child) |
| 2945 |
|
| 2946 |
def pred_func(child): |
| 2947 |
return child.index % 2 == 0 |
| 2948 |
|
| 2949 |
predicate = pred_func if case["predicate_filter"] == "filter_odd" else None |
| 2950 |
|
| 2951 |
debug_calls = [] |
| 2952 |
debug_mock = test_context.Mock() |
| 2953 |
debug_mock.LEVEL_INFO = 1 |
| 2954 |
debug_mock.print_tokens = test_context.Mock( |
| 2955 |
side_effect=lambda *args: debug_calls.append(args), |
| 2956 |
) |
| 2957 |
test_context.patch("orca.ax_object.debug", new=debug_mock) |
| 2958 |
|
| 2959 |
results = list(AXObject.iter_children(mock_obj, predicate)) |
| 2960 |
|
| 2961 |
assert len(results) == case["expected_count"] |
| 2962 |
|
| 2963 |
if case["warns_large"]: |
| 2964 |
assert len(debug_calls) > 0 |
| 2965 |
warning_found = any("more than 500 children" in str(call) for call in debug_calls) |
| 2966 |
assert warning_found |
| 2967 |
else: |
| 2968 |
large_warning_found = any("more than 500 children" in str(call) for call in debug_calls) |
| 2969 |
assert not large_warning_found |