| 1 |
# Unit tests for ax_utilities_relation.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=too-many-lines |
| 24 |
# pylint: disable=protected-access |
| 25 |
# pylint: disable=import-outside-toplevel |
| 26 |
|
| 27 |
"""Unit tests for ax_utilities_relation.py methods.""" |
| 28 |
|
| 29 |
from __future__ import annotations |
| 30 |
|
| 31 |
from typing import TYPE_CHECKING |
| 32 |
|
| 33 |
import gi |
| 34 |
import pytest |
| 35 |
|
| 36 |
gi.require_version("Atspi", "2.0") |
| 37 |
from gi.repository import Atspi |
| 38 |
|
| 39 |
if TYPE_CHECKING: |
| 40 |
from unittest.mock import MagicMock |
| 41 |
|
| 42 |
from .orca_test_context import OrcaTestContext |
| 43 |
|
| 44 |
|
| 45 |
@pytest.mark.unit |
| 46 |
class TestAXUtilitiesRelation: |
| 47 |
"""Test AXUtilitiesRelation class methods.""" |
| 48 |
|
| 49 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 50 |
"""Set up mocks for ax_utilities_relation dependencies.""" |
| 51 |
|
| 52 |
essential_modules = test_context.setup_shared_dependencies([]) |
| 53 |
|
| 54 |
ax_object_mock = essential_modules["orca.ax_object"] |
| 55 |
ax_object_mock.is_valid = test_context.Mock(return_value=True) |
| 56 |
ax_object_mock.is_ancestor = test_context.Mock(return_value=False) |
| 57 |
|
| 58 |
return essential_modules |
| 59 |
|
| 60 |
@pytest.mark.parametrize( |
| 61 |
"case", |
| 62 |
[ |
| 63 |
{ |
| 64 |
"id": "controlled_by", |
| 65 |
"method_name": "get_is_controlled_by", |
| 66 |
"relation_type": Atspi.RelationType.CONTROLLED_BY, |
| 67 |
}, |
| 68 |
{ |
| 69 |
"id": "controller_for", |
| 70 |
"method_name": "get_is_controller_for", |
| 71 |
"relation_type": Atspi.RelationType.CONTROLLER_FOR, |
| 72 |
}, |
| 73 |
{ |
| 74 |
"id": "described_by", |
| 75 |
"method_name": "get_is_described_by", |
| 76 |
"relation_type": Atspi.RelationType.DESCRIBED_BY, |
| 77 |
}, |
| 78 |
{ |
| 79 |
"id": "description_for", |
| 80 |
"method_name": "get_is_description_for", |
| 81 |
"relation_type": Atspi.RelationType.DESCRIPTION_FOR, |
| 82 |
}, |
| 83 |
{ |
| 84 |
"id": "details", |
| 85 |
"method_name": "get_details", |
| 86 |
"relation_type": Atspi.RelationType.DETAILS, |
| 87 |
}, |
| 88 |
{ |
| 89 |
"id": "details_for", |
| 90 |
"method_name": "get_is_details_for", |
| 91 |
"relation_type": Atspi.RelationType.DETAILS_FOR, |
| 92 |
}, |
| 93 |
{ |
| 94 |
"id": "embedded_by", |
| 95 |
"method_name": "get_is_embedded_by", |
| 96 |
"relation_type": Atspi.RelationType.EMBEDDED_BY, |
| 97 |
}, |
| 98 |
{ |
| 99 |
"id": "embeds", |
| 100 |
"method_name": "get_embeds", |
| 101 |
"relation_type": Atspi.RelationType.EMBEDS, |
| 102 |
}, |
| 103 |
{ |
| 104 |
"id": "error_for", |
| 105 |
"method_name": "get_is_error_for", |
| 106 |
"relation_type": Atspi.RelationType.ERROR_FOR, |
| 107 |
}, |
| 108 |
{ |
| 109 |
"id": "error_message", |
| 110 |
"method_name": "get_error_message", |
| 111 |
"relation_type": Atspi.RelationType.ERROR_MESSAGE, |
| 112 |
}, |
| 113 |
{ |
| 114 |
"id": "flows_from", |
| 115 |
"method_name": "get_flows_from", |
| 116 |
"relation_type": Atspi.RelationType.FLOWS_FROM, |
| 117 |
}, |
| 118 |
{ |
| 119 |
"id": "flows_to", |
| 120 |
"method_name": "get_flows_to", |
| 121 |
"relation_type": Atspi.RelationType.FLOWS_TO, |
| 122 |
}, |
| 123 |
{ |
| 124 |
"id": "label_for", |
| 125 |
"method_name": "get_is_label_for", |
| 126 |
"relation_type": Atspi.RelationType.LABEL_FOR, |
| 127 |
}, |
| 128 |
{ |
| 129 |
"id": "member_of", |
| 130 |
"method_name": "get_is_member_of", |
| 131 |
"relation_type": Atspi.RelationType.MEMBER_OF, |
| 132 |
}, |
| 133 |
{ |
| 134 |
"id": "node_child_of", |
| 135 |
"method_name": "get_is_node_child_of", |
| 136 |
"relation_type": Atspi.RelationType.NODE_CHILD_OF, |
| 137 |
}, |
| 138 |
{ |
| 139 |
"id": "node_parent_of", |
| 140 |
"method_name": "get_is_node_parent_of", |
| 141 |
"relation_type": Atspi.RelationType.NODE_PARENT_OF, |
| 142 |
}, |
| 143 |
{ |
| 144 |
"id": "parent_window_of", |
| 145 |
"method_name": "get_is_parent_window_of", |
| 146 |
"relation_type": Atspi.RelationType.PARENT_WINDOW_OF, |
| 147 |
}, |
| 148 |
{ |
| 149 |
"id": "popup_for", |
| 150 |
"method_name": "get_is_popup_for", |
| 151 |
"relation_type": Atspi.RelationType.POPUP_FOR, |
| 152 |
}, |
| 153 |
{ |
| 154 |
"id": "subwindow_of", |
| 155 |
"method_name": "get_is_subwindow_of", |
| 156 |
"relation_type": Atspi.RelationType.SUBWINDOW_OF, |
| 157 |
}, |
| 158 |
{ |
| 159 |
"id": "tooltip_for", |
| 160 |
"method_name": "get_is_tooltip_for", |
| 161 |
"relation_type": Atspi.RelationType.TOOLTIP_FOR, |
| 162 |
}, |
| 163 |
{ |
| 164 |
"id": "debugging_labelled_by", |
| 165 |
"method_name": "get_relation_targets_for_debugging", |
| 166 |
"relation_type": Atspi.RelationType.LABELLED_BY, |
| 167 |
}, |
| 168 |
], |
| 169 |
ids=lambda case: case["id"], |
| 170 |
) |
| 171 |
def test_get_relation_targets_wrappers(self, test_context: OrcaTestContext, case: dict) -> None: |
| 172 |
"""Test that various get_*_relation_targets methods call _get_relation_targets.""" |
| 173 |
|
| 174 |
self._setup_dependencies(test_context) |
| 175 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 176 |
|
| 177 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 178 |
mock_targets = [test_context.Mock(spec=Atspi.Accessible)] |
| 179 |
test_context.patch_object( |
| 180 |
AXUtilitiesRelation, |
| 181 |
"_get_relation_targets", |
| 182 |
return_value=mock_targets, |
| 183 |
) |
| 184 |
method = getattr(AXUtilitiesRelation, case["method_name"]) |
| 185 |
if case["method_name"] == "get_relation_targets_for_debugging": |
| 186 |
result = method(mock_obj, case["relation_type"]) |
| 187 |
else: |
| 188 |
result = method(mock_obj) |
| 189 |
assert result == mock_targets |
| 190 |
|
| 191 |
@pytest.mark.parametrize( |
| 192 |
"case", |
| 193 |
[ |
| 194 |
{ |
| 195 |
"id": "invalid_object", |
| 196 |
"is_valid": False, |
| 197 |
"has_cache": False, |
| 198 |
"should_raise_error": False, |
| 199 |
"expected_result": [], |
| 200 |
"should_cache": False, |
| 201 |
}, |
| 202 |
{ |
| 203 |
"id": "from_cache", |
| 204 |
"is_valid": True, |
| 205 |
"has_cache": True, |
| 206 |
"should_raise_error": False, |
| 207 |
"expected_result": "cached_relations", |
| 208 |
"should_cache": False, |
| 209 |
}, |
| 210 |
{ |
| 211 |
"id": "from_atspi", |
| 212 |
"is_valid": True, |
| 213 |
"has_cache": False, |
| 214 |
"should_raise_error": False, |
| 215 |
"expected_result": "atspi_relations", |
| 216 |
"should_cache": True, |
| 217 |
}, |
| 218 |
{ |
| 219 |
"id": "glib_error", |
| 220 |
"is_valid": True, |
| 221 |
"has_cache": False, |
| 222 |
"should_raise_error": True, |
| 223 |
"expected_result": [], |
| 224 |
"should_cache": False, |
| 225 |
}, |
| 226 |
], |
| 227 |
ids=lambda case: case["id"], |
| 228 |
) |
| 229 |
def test_get_relations(self, test_context: OrcaTestContext, case: dict) -> None: |
| 230 |
"""Test AXUtilitiesRelation.get_relations with various scenarios.""" |
| 231 |
|
| 232 |
self._setup_dependencies(test_context) |
| 233 |
from gi.repository import GLib |
| 234 |
|
| 235 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 236 |
|
| 237 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 238 |
mock_relations = [test_context.Mock(spec=Atspi.Relation)] |
| 239 |
|
| 240 |
test_context.patch( |
| 241 |
"orca.ax_utilities_relation.AXObject.is_valid", |
| 242 |
return_value=case["is_valid"], |
| 243 |
) |
| 244 |
|
| 245 |
AXUtilitiesRelation.RELATIONS.clear() |
| 246 |
if case["has_cache"]: |
| 247 |
AXUtilitiesRelation.RELATIONS[hash(mock_obj)] = mock_relations |
| 248 |
|
| 249 |
if case["should_raise_error"]: |
| 250 |
|
| 251 |
def raise_glib_error(self): |
| 252 |
raise GLib.GError("Test error") |
| 253 |
|
| 254 |
test_context.patch_object( |
| 255 |
Atspi.Accessible, |
| 256 |
"get_relation_set", |
| 257 |
side_effect=raise_glib_error, |
| 258 |
) |
| 259 |
else: |
| 260 |
test_context.patch_object( |
| 261 |
Atspi.Accessible, |
| 262 |
"get_relation_set", |
| 263 |
return_value=mock_relations, |
| 264 |
) |
| 265 |
|
| 266 |
result = AXUtilitiesRelation.get_relations(mock_obj) |
| 267 |
|
| 268 |
if case["expected_result"] in ("cached_relations", "atspi_relations"): |
| 269 |
assert result == mock_relations |
| 270 |
else: |
| 271 |
assert result == case["expected_result"] |
| 272 |
|
| 273 |
if case["should_cache"]: |
| 274 |
assert AXUtilitiesRelation.RELATIONS[hash(mock_obj)] == mock_relations |
| 275 |
|
| 276 |
@pytest.mark.parametrize( |
| 277 |
"case", |
| 278 |
[ |
| 279 |
{ |
| 280 |
"id": "matching_type", |
| 281 |
"relation_type_to_find": Atspi.RelationType.LABELLED_BY, |
| 282 |
"relation_type_available": Atspi.RelationType.LABELLED_BY, |
| 283 |
"expected_result": "found_relation", |
| 284 |
}, |
| 285 |
{ |
| 286 |
"id": "no_matching_type", |
| 287 |
"relation_type_to_find": Atspi.RelationType.LABEL_FOR, |
| 288 |
"relation_type_available": Atspi.RelationType.LABELLED_BY, |
| 289 |
"expected_result": None, |
| 290 |
}, |
| 291 |
], |
| 292 |
ids=lambda case: case["id"], |
| 293 |
) |
| 294 |
def test_get_relation(self, test_context: OrcaTestContext, case: dict) -> None: |
| 295 |
"""Test AXUtilitiesRelation._get_relation with various scenarios.""" |
| 296 |
|
| 297 |
self._setup_dependencies(test_context) |
| 298 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 299 |
|
| 300 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 301 |
mock_relation = test_context.Mock(spec=Atspi.Relation) |
| 302 |
mock_relation.get_relation_type.return_value = case["relation_type_available"] |
| 303 |
test_context.patch_object( |
| 304 |
AXUtilitiesRelation, |
| 305 |
"get_relations", |
| 306 |
return_value=[mock_relation], |
| 307 |
) |
| 308 |
|
| 309 |
result = AXUtilitiesRelation._get_relation(mock_obj, case["relation_type_to_find"]) |
| 310 |
|
| 311 |
if case["expected_result"] == "found_relation": |
| 312 |
assert result == mock_relation |
| 313 |
else: |
| 314 |
assert result is case["expected_result"] |
| 315 |
|
| 316 |
@pytest.mark.parametrize( |
| 317 |
"case", |
| 318 |
[ |
| 319 |
{ |
| 320 |
"id": "from_cache", |
| 321 |
"scenario": "cached", |
| 322 |
"relation_type": Atspi.RelationType.LABELLED_BY, |
| 323 |
"expected_result": "cached_targets", |
| 324 |
"has_self_target": False, |
| 325 |
}, |
| 326 |
{ |
| 327 |
"id": "no_relation", |
| 328 |
"scenario": "no_relation", |
| 329 |
"relation_type": Atspi.RelationType.LABELLED_BY, |
| 330 |
"expected_result": [], |
| 331 |
"has_self_target": False, |
| 332 |
}, |
| 333 |
{ |
| 334 |
"id": "valid_targets", |
| 335 |
"scenario": "valid_targets", |
| 336 |
"relation_type": Atspi.RelationType.LABELLED_BY, |
| 337 |
"expected_result": "all_targets", |
| 338 |
"has_self_target": False, |
| 339 |
}, |
| 340 |
{ |
| 341 |
"id": "removes_self", |
| 342 |
"scenario": "with_self", |
| 343 |
"relation_type": Atspi.RelationType.LABELLED_BY, |
| 344 |
"expected_result": "exclude_self", |
| 345 |
"has_self_target": True, |
| 346 |
}, |
| 347 |
{ |
| 348 |
"id": "keeps_member_of_self", |
| 349 |
"scenario": "member_of_self", |
| 350 |
"relation_type": Atspi.RelationType.MEMBER_OF, |
| 351 |
"expected_result": "include_self", |
| 352 |
"has_self_target": True, |
| 353 |
}, |
| 354 |
], |
| 355 |
ids=lambda case: case["id"], |
| 356 |
) |
| 357 |
def test_get_relation_targets(self, test_context: OrcaTestContext, case: dict) -> None: |
| 358 |
"""Test AXUtilitiesRelation._get_relation_targets with various scenarios.""" |
| 359 |
|
| 360 |
self._setup_dependencies(test_context) |
| 361 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 362 |
|
| 363 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 364 |
mock_target1 = test_context.Mock(spec=Atspi.Accessible) |
| 365 |
mock_target2 = test_context.Mock(spec=Atspi.Accessible) |
| 366 |
|
| 367 |
AXUtilitiesRelation.TARGETS.clear() |
| 368 |
|
| 369 |
if case["scenario"] == "cached": |
| 370 |
mock_targets = [mock_target1] |
| 371 |
AXUtilitiesRelation.TARGETS[hash(mock_obj)] = {case["relation_type"]: mock_targets} |
| 372 |
result = AXUtilitiesRelation._get_relation_targets(mock_obj, case["relation_type"]) |
| 373 |
assert result == mock_targets |
| 374 |
return |
| 375 |
|
| 376 |
if case["scenario"] == "no_relation": |
| 377 |
test_context.patch_object(AXUtilitiesRelation, "_get_relation", return_value=None) |
| 378 |
else: |
| 379 |
mock_relation = test_context.Mock(spec=Atspi.Relation) |
| 380 |
if case["has_self_target"]: |
| 381 |
mock_relation.get_n_targets.return_value = 2 |
| 382 |
mock_relation.get_target.side_effect = [mock_target1, mock_obj] |
| 383 |
else: |
| 384 |
mock_relation.get_n_targets.return_value = 2 |
| 385 |
mock_relation.get_target.side_effect = [mock_target1, mock_target2] |
| 386 |
test_context.patch_object( |
| 387 |
AXUtilitiesRelation, |
| 388 |
"_get_relation", |
| 389 |
return_value=mock_relation, |
| 390 |
) |
| 391 |
|
| 392 |
result = AXUtilitiesRelation._get_relation_targets(mock_obj, case["relation_type"]) |
| 393 |
|
| 394 |
if case["expected_result"] == "all_targets": |
| 395 |
assert set(result) == {mock_target1, mock_target2} |
| 396 |
elif case["expected_result"] == "exclude_self": |
| 397 |
assert result == [mock_target1] |
| 398 |
elif case["expected_result"] == "include_self": |
| 399 |
assert set(result) == {mock_target1, mock_obj} |
| 400 |
else: |
| 401 |
assert result == case["expected_result"] |
| 402 |
|
| 403 |
if case["scenario"] == "no_relation": |
| 404 |
assert AXUtilitiesRelation.TARGETS[hash(mock_obj)][case["relation_type"]] == [] |
| 405 |
|
| 406 |
@pytest.mark.parametrize( |
| 407 |
"case", |
| 408 |
[ |
| 409 |
{ |
| 410 |
"id": "without_filtering", |
| 411 |
"exclude_ancestors": False, |
| 412 |
"expected_result": "all_targets", |
| 413 |
}, |
| 414 |
{ |
| 415 |
"id": "with_filtering", |
| 416 |
"exclude_ancestors": True, |
| 417 |
"expected_result": "non_ancestors_only", |
| 418 |
}, |
| 419 |
], |
| 420 |
ids=lambda case: case["id"], |
| 421 |
) |
| 422 |
def test_get_is_labelled_by(self, test_context: OrcaTestContext, case: dict) -> None: |
| 423 |
"""Test AXUtilitiesRelation.get_is_labelled_by with and without ancestor filtering.""" |
| 424 |
|
| 425 |
self._setup_dependencies(test_context) |
| 426 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 427 |
|
| 428 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 429 |
mock_ancestor = test_context.Mock(spec=Atspi.Accessible) |
| 430 |
mock_non_ancestor = test_context.Mock(spec=Atspi.Accessible) |
| 431 |
mock_targets = [mock_ancestor, mock_non_ancestor] |
| 432 |
|
| 433 |
test_context.patch_object( |
| 434 |
AXUtilitiesRelation, |
| 435 |
"_get_relation_targets", |
| 436 |
return_value=mock_targets, |
| 437 |
) |
| 438 |
|
| 439 |
if case["exclude_ancestors"]: |
| 440 |
test_context.patch( |
| 441 |
"orca.ax_utilities_relation.AXUtilitiesObject.is_ancestor", |
| 442 |
side_effect=lambda child, acc: acc == mock_ancestor, |
| 443 |
) |
| 444 |
|
| 445 |
result = AXUtilitiesRelation.get_is_labelled_by( |
| 446 |
mock_obj, |
| 447 |
exclude_ancestors=case["exclude_ancestors"], |
| 448 |
) |
| 449 |
|
| 450 |
if case["expected_result"] == "all_targets": |
| 451 |
assert result == mock_targets |
| 452 |
else: |
| 453 |
assert result == [mock_non_ancestor] |
| 454 |
|
| 455 |
@pytest.mark.parametrize( |
| 456 |
"case", |
| 457 |
[ |
| 458 |
{"id": "controlled", "is_controlled": True, "expected": True}, |
| 459 |
{"id": "not_controlled", "is_controlled": False, "expected": False}, |
| 460 |
], |
| 461 |
ids=lambda case: case["id"], |
| 462 |
) |
| 463 |
def test_object_is_controlled_by(self, test_context: OrcaTestContext, case: dict) -> None: |
| 464 |
"""Test AXUtilitiesRelation.object_is_controlled_by with controlled/uncontrolled objects.""" |
| 465 |
|
| 466 |
self._setup_dependencies(test_context) |
| 467 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 468 |
|
| 469 |
mock_obj1 = test_context.Mock(spec=Atspi.Accessible) |
| 470 |
mock_obj2 = test_context.Mock(spec=Atspi.Accessible) |
| 471 |
mock_obj3 = test_context.Mock(spec=Atspi.Accessible) |
| 472 |
targets = [mock_obj2] if case["is_controlled"] else [mock_obj3] |
| 473 |
test_context.patch_object( |
| 474 |
AXUtilitiesRelation, |
| 475 |
"_get_relation_targets", |
| 476 |
return_value=targets, |
| 477 |
) |
| 478 |
result = AXUtilitiesRelation.object_is_controlled_by(mock_obj1, mock_obj2) |
| 479 |
assert result is case["expected"] |
| 480 |
|
| 481 |
@pytest.mark.parametrize( |
| 482 |
"case", |
| 483 |
[ |
| 484 |
{"id": "no_relations", "has_relations": False, "expected": True}, |
| 485 |
{"id": "has_relations", "has_relations": True, "expected": False}, |
| 486 |
], |
| 487 |
ids=lambda case: case["id"], |
| 488 |
) |
| 489 |
def test_object_is_unrelated(self, test_context: OrcaTestContext, case: dict) -> None: |
| 490 |
"""Test AXUtilitiesRelation.object_is_unrelated with objects with and without relations.""" |
| 491 |
|
| 492 |
self._setup_dependencies(test_context) |
| 493 |
from orca.ax_utilities_relation import AXUtilitiesRelation |
| 494 |
|
| 495 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 496 |
relations = [test_context.Mock(spec=Atspi.Relation)] if case["has_relations"] else [] |
| 497 |
test_context.patch_object(AXUtilitiesRelation, "get_relations", return_value=relations) |
| 498 |
result = AXUtilitiesRelation.object_is_unrelated(mock_obj) |
| 499 |
assert result is case["expected"] |