1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
|
# Unit tests for ax_hypertext.py methods.
#
# Copyright 2025 Igalia, S.L.
# Author: Joanmarie Diggs <jdiggs@igalia.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
# Boston MA 02110-1301 USA.
# pylint: disable=too-many-public-methods
# pylint: disable=protected-access
# pylint: disable=wrong-import-position
# pylint: disable=import-outside-toplevel
"""Unit tests for ax_hypertext.py methods."""
from __future__ import annotations
from typing import TYPE_CHECKING
import gi
import pytest
gi.require_version("Atspi", "2.0")
from gi.repository import Atspi
from gi.repository import GLib
if TYPE_CHECKING:
from .orca_test_context import OrcaTestContext
from unittest.mock import MagicMock
@pytest.mark.unit
class TestAXHypertext:
"""Test AXHypertext class methods."""
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]:
"""Set up mocks for ax_hypertext dependencies."""
essential_modules = test_context.setup_shared_dependencies([])
debug_mock = essential_modules["orca.debug"]
debug_mock.print_message = test_context.Mock()
debug_mock.print_tokens = test_context.Mock()
debug_mock.LEVEL_INFO = 800
ax_object_class_mock = test_context.Mock()
ax_object_class_mock.supports_hypertext = test_context.Mock(return_value=True)
ax_object_class_mock.supports_text = test_context.Mock(return_value=True)
ax_object_class_mock.get_parent = test_context.Mock(return_value=None)
ax_object_class_mock.get_name = test_context.Mock(return_value="test-object")
ax_object_class_mock.is_dead = test_context.Mock(return_value=False)
essential_modules["orca.ax_object"].AXObject = ax_object_class_mock
return essential_modules
def test_get_link_count_with_hypertext_support(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext._get_link_count with hypertext support."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
test_context.patch(
"orca.ax_hypertext.AXObject.supports_hypertext", return_value=True
)
test_context.patch(
"orca.ax_hypertext.Atspi.Hypertext.get_n_links", return_value=3
)
result = AXHypertext._get_link_count(mock_accessible)
assert result == 3
def test_get_link_count_without_hypertext_support(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext._get_link_count without hypertext support."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
test_context.patch(
"orca.ax_hypertext.AXObject.supports_hypertext", return_value=False
)
result = AXHypertext._get_link_count(mock_accessible)
assert result == 0
def test_get_link_count_with_glib_error(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext._get_link_count handles GLib.GError."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
def raise_glib_error(self) -> None:
raise GLib.GError("Test error")
test_context.patch(
"orca.ax_hypertext.AXObject.supports_hypertext", return_value=True
)
test_context.patch(
"orca.ax_hypertext.Atspi.Hypertext.get_n_links", side_effect=raise_glib_error
)
test_context.patch(
"orca.ax_hypertext.debug.print_message",
new=essential_modules["orca.debug"].print_message,
)
result = AXHypertext._get_link_count(mock_accessible)
assert result == 0
essential_modules["orca.debug"].print_message.assert_called()
@pytest.mark.parametrize(
"has_hypertext_support,raises_glib_error,should_return_link",
[
(True, False, True), # Valid index
(False, False, False), # Without hypertext support
(True, True, False), # With GLib error
],
)
def test_get_link_at_index_scenarios(
self,
test_context: OrcaTestContext,
has_hypertext_support: bool,
raises_glib_error: bool,
should_return_link: bool,
) -> None:
"""Test AXHypertext._get_link_at_index under different scenarios."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
mock_hyperlink = test_context.Mock(spec=Atspi.Hyperlink)
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
test_context.patch(
"orca.ax_hypertext.AXObject.supports_hypertext", return_value=has_hypertext_support
)
if raises_glib_error:
def raise_glib_error(self, index) -> None:
raise GLib.GError("Test error")
test_context.patch(
"orca.ax_hypertext.Atspi.Hypertext.get_link", side_effect=raise_glib_error
)
test_context.patch(
"orca.ax_hypertext.debug.print_message",
new=essential_modules["orca.debug"].print_message,
)
else:
test_context.patch(
"orca.ax_hypertext.Atspi.Hypertext.get_link",
return_value=mock_hyperlink,
)
result = AXHypertext._get_link_at_index(mock_accessible, 0)
if should_return_link:
assert result == mock_hyperlink
else:
assert result is None
if raises_glib_error:
essential_modules["orca.debug"].print_message.assert_called()
@pytest.mark.parametrize(
"start_offset, end_offset, link_ranges, expected_count",
[
pytest.param(0, 10, [], 0, id="no_links"),
pytest.param(0, 10, [(5, 8)], 1, id="link_within_range"),
pytest.param(0, 10, [(12, 15)], 0, id="link_outside_range"),
pytest.param(0, 10, [(0, 5), (8, 10)], 2, id="multiple_links_in_range"),
pytest.param(5, 15, [(0, 6), (10, 20)], 2, id="links_partially_overlapping"),
pytest.param(10, 15, [(10, 12)], 1, id="link_start_matches_range_start"),
],
)
def test_get_all_links_in_range( # pylint: disable=too-many-arguments, too-many-positional-arguments
self,
start_offset,
end_offset,
link_ranges,
expected_count,
test_context,
) -> None:
"""Test AXHypertext.get_all_links_in_range."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
mock_links = [test_context.Mock(spec=Atspi.Hyperlink) for _ in link_ranges]
def get_offset(link, is_start=True) -> int:
for i, mock_link in enumerate(mock_links):
if link == mock_link:
return link_ranges[i][0 if is_start else 1]
return -1
test_context.patch(
"orca.ax_hypertext.AXHypertext._get_link_count", return_value=len(mock_links)
)
def mock_get_link_at_index(_obj, index):
return mock_links[index] if index < len(mock_links) else None
test_context.patch(
"orca.ax_hypertext.AXHypertext._get_link_at_index",
side_effect=mock_get_link_at_index,
)
test_context.patch(
"orca.ax_hypertext.AXHypertext.get_link_start_offset",
side_effect=lambda link: get_offset(link, True),
)
test_context.patch(
"orca.ax_hypertext.AXHypertext.get_link_end_offset",
side_effect=lambda link: get_offset(link, False),
)
result = AXHypertext.get_all_links_in_range(mock_accessible, start_offset, end_offset)
assert len(result) == expected_count
def test_get_all_links_empty_object(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext.get_all_links with object containing no links."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
test_context.patch(
"orca.ax_hypertext.AXHypertext._get_link_count", return_value=0
)
result = AXHypertext.get_all_links(mock_accessible)
assert not result
def test_get_all_links_with_valid_links(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext.get_all_links with multiple valid links."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
mock_link1 = test_context.Mock(spec=Atspi.Hyperlink)
mock_link2 = test_context.Mock(spec=Atspi.Hyperlink)
test_context.patch(
"orca.ax_hypertext.AXHypertext._get_link_count", return_value=2
)
def mock_get_link_by_index(_obj, index):
return mock_link1 if index == 0 else mock_link2
test_context.patch(
"orca.ax_hypertext.AXHypertext._get_link_at_index",
side_effect=mock_get_link_by_index,
)
result = AXHypertext.get_all_links(mock_accessible)
assert len(result) == 2
assert result[0] == mock_link1
assert result[1] == mock_link2
def test_get_all_links_filters_none_values(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext.get_all_links filters out None values."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
mock_link = test_context.Mock(spec=Atspi.Hyperlink)
test_context.patch(
"orca.ax_hypertext.AXHypertext._get_link_count", return_value=3
)
def mock_get_specific_link(_obj, index):
return mock_link if index == 1 else None
test_context.patch(
"orca.ax_hypertext.AXHypertext._get_link_at_index",
side_effect=mock_get_specific_link,
)
result = AXHypertext.get_all_links(mock_accessible)
assert len(result) == 1
assert result[0] == mock_link
def test_get_link_uri_with_valid_hyperlink(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext.get_link_uri with valid hyperlink."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
mock_hyperlink = test_context.Mock(spec=Atspi.Hyperlink)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
test_context.patch(
"orca.ax_hypertext.Atspi.Accessible.get_hyperlink",
return_value=mock_hyperlink,
)
test_context.patch(
"orca.ax_hypertext.Atspi.Hyperlink.get_uri",
return_value="https://example.com",
)
result = AXHypertext.get_link_uri(mock_accessible, 0)
assert result == "https://example.com"
def test_get_link_uri_with_no_hyperlink(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext.get_link_uri with no hyperlink."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
def raise_glib_error(obj) -> None:
raise GLib.GError("No hyperlink")
test_context.patch(
"orca.ax_hypertext.Atspi.Accessible.get_hyperlink", side_effect=raise_glib_error
)
test_context.patch(
"orca.ax_hypertext.debug.print_message",
new=essential_modules["orca.debug"].print_message,
)
result = AXHypertext.get_link_uri(mock_accessible, 0)
assert result == ""
essential_modules["orca.debug"].print_message.assert_called()
def test_get_link_uri_with_glib_error(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext.get_link_uri handles GLib.GError."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
mock_hyperlink = test_context.Mock(spec=Atspi.Hyperlink)
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
def raise_glib_error(link, index) -> None:
raise GLib.GError("Test error")
test_context.patch(
"orca.ax_hypertext.Atspi.Accessible.get_hyperlink",
return_value=mock_hyperlink,
)
test_context.patch(
"orca.ax_hypertext.Atspi.Hyperlink.get_uri", side_effect=raise_glib_error
)
test_context.patch(
"orca.ax_hypertext.debug.print_message",
new=essential_modules["orca.debug"].print_message,
)
result = AXHypertext.get_link_uri(mock_accessible, 0)
assert result == ""
essential_modules["orca.debug"].print_message.assert_called()
@pytest.mark.parametrize(
"input_type, expected_offset",
[
pytest.param("accessible", 5, id="accessible_input"),
pytest.param("hyperlink", 10, id="hyperlink_input"),
],
)
def test_get_link_start_offset(
self,
input_type,
expected_offset,
test_context,
) -> None:
"""Test AXHypertext.get_link_start_offset."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
mock_hyperlink = test_context.Mock(spec=Atspi.Hyperlink)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
if input_type == "accessible":
test_context.patch(
"orca.ax_hypertext.Atspi.Accessible.get_hyperlink",
return_value=mock_hyperlink,
)
input_obj = mock_accessible
else:
test_context.patch(
"orca.ax_hypertext.Atspi.Hyperlink.get_object",
return_value=mock_accessible,
)
input_obj = mock_hyperlink
test_context.patch(
"orca.ax_hypertext.Atspi.Hyperlink.get_start_index",
return_value=expected_offset,
)
result = AXHypertext.get_link_start_offset(input_obj)
assert result == expected_offset
def test_get_link_start_offset_no_hyperlink(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext.get_link_start_offset with no hyperlink."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
test_context.patch(
"orca.ax_hypertext.Atspi.Accessible.get_hyperlink", return_value=None
)
result = AXHypertext.get_link_start_offset(mock_accessible)
assert result == -1
def test_get_link_start_offset_with_glib_error(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext.get_link_start_offset handles GLib.GError."""
mock_hyperlink = test_context.Mock(spec=Atspi.Hyperlink)
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
def raise_glib_error(link) -> None:
raise GLib.GError("Test error")
test_context.patch(
"orca.ax_hypertext.Atspi.Hyperlink.get_object",
return_value=mock_hyperlink,
)
test_context.patch(
"orca.ax_hypertext.Atspi.Hyperlink.get_start_index", side_effect=raise_glib_error
)
test_context.patch(
"orca.ax_hypertext.debug.print_message",
new=essential_modules["orca.debug"].print_message,
)
result = AXHypertext.get_link_start_offset(mock_hyperlink)
assert result == -1
essential_modules["orca.debug"].print_message.assert_called()
@pytest.mark.parametrize(
"input_type, expected_offset",
[
pytest.param("accessible", 15, id="accessible_input"),
pytest.param("hyperlink", 20, id="hyperlink_input"),
],
)
def test_get_link_end_offset(
self,
input_type,
expected_offset,
test_context,
) -> None:
"""Test AXHypertext.get_link_end_offset."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
mock_hyperlink = test_context.Mock(spec=Atspi.Hyperlink)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
if input_type == "accessible":
test_context.patch(
"orca.ax_hypertext.Atspi.Accessible.get_hyperlink",
return_value=mock_hyperlink,
)
input_obj = mock_accessible
else:
test_context.patch(
"orca.ax_hypertext.Atspi.Hyperlink.get_object",
return_value=mock_accessible,
)
input_obj = mock_hyperlink
test_context.patch(
"orca.ax_hypertext.Atspi.Hyperlink.get_end_index",
return_value=expected_offset,
)
result = AXHypertext.get_link_end_offset(input_obj)
assert result == expected_offset
def test_get_link_end_offset_no_hyperlink(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext.get_link_end_offset with no hyperlink."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
test_context.patch(
"orca.ax_hypertext.Atspi.Accessible.get_hyperlink", return_value=None
)
result = AXHypertext.get_link_end_offset(mock_accessible)
assert result == -1
def test_get_link_end_offset_with_glib_error(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext.get_link_end_offset handles GLib.GError."""
mock_hyperlink = test_context.Mock(spec=Atspi.Hyperlink)
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
def raise_glib_error(link) -> None:
raise GLib.GError("Test error")
test_context.patch(
"orca.ax_hypertext.Atspi.Hyperlink.get_object",
return_value=mock_hyperlink,
)
test_context.patch(
"orca.ax_hypertext.Atspi.Hyperlink.get_end_index", side_effect=raise_glib_error
)
test_context.patch(
"orca.ax_hypertext.debug.print_message",
new=essential_modules["orca.debug"].print_message,
)
result = AXHypertext.get_link_end_offset(mock_hyperlink)
assert result == -1
essential_modules["orca.debug"].print_message.assert_called()
@pytest.mark.parametrize(
"uri, remove_extension, expected_result",
[
pytest.param(
"https://example.com/path/file.html", False, "file.html", id="path_with_extension"
),
pytest.param(
"https://example.com/path/file.html", True, "file", id="path_with_extension_removed"
),
pytest.param("https://example.com/", False, "", id="no_path_component"),
pytest.param("", False, "", id="empty_uri"),
pytest.param("https://example.com/simple", False, "simple", id="path_no_extension"),
pytest.param(
"https://example.com/simple", True, "simple", id="path_no_extension_capitalized"
),
pytest.param("file:///home/user/document.pdf", False, "document.pdf", id="file_uri"),
pytest.param(
"file:///home/user/document.pdf", True, "document", id="file_uri_extension_removed"
),
],
)
def test_get_link_basename(
self,
uri,
remove_extension,
expected_result,
test_context,
) -> None:
"""Test AXHypertext.get_link_basename."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
test_context.patch(
"orca.ax_hypertext.AXHypertext.get_link_uri", return_value=uri
)
result = AXHypertext.get_link_basename(mock_accessible, 0, remove_extension)
assert result == expected_result
def test_get_child_at_offset_with_valid_link(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext.get_child_at_offset with valid link."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
mock_hyperlink = test_context.Mock(spec=Atspi.Hyperlink)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
mock_child = test_context.Mock(spec=Atspi.Accessible)
test_context.patch(
"orca.ax_hypertext.AXObject.supports_hypertext", return_value=True
)
test_context.patch(
"orca.ax_hypertext.Atspi.Hypertext.get_link_index", return_value=0
)
test_context.patch(
"orca.ax_hypertext.AXHypertext._get_link_at_index",
return_value=mock_hyperlink,
)
test_context.patch(
"orca.ax_hypertext.Atspi.Hyperlink.get_object",
return_value=mock_child,
)
result = AXHypertext.get_child_at_offset(mock_accessible, 5)
assert result == mock_child
def test_get_child_at_offset_without_hypertext_support(
self, test_context: OrcaTestContext
) -> None:
"""Test AXHypertext.get_child_at_offset without hypertext support."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
test_context.patch(
"orca.ax_hypertext.AXObject.supports_hypertext", return_value=False
)
result = AXHypertext.get_child_at_offset(mock_accessible, 5)
assert result is None
def test_get_child_at_offset_no_link_at_index(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext.get_child_at_offset with no link at index."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
test_context.patch(
"orca.ax_hypertext.AXObject.supports_hypertext", return_value=True
)
test_context.patch(
"orca.ax_hypertext.Atspi.Hypertext.get_link_index", return_value=-1
)
result = AXHypertext.get_child_at_offset(mock_accessible, 5)
assert result is None
def test_get_child_at_offset_with_glib_error_get_link_index(
self, test_context: OrcaTestContext
) -> None:
"""Test AXHypertext.get_child_at_offset handles GLib.GError in get_link_index."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
def raise_glib_error(obj, offset) -> None:
raise GLib.GError("Test error")
test_context.patch(
"orca.ax_hypertext.AXObject.supports_hypertext", return_value=True
)
test_context.patch(
"orca.ax_hypertext.Atspi.Hypertext.get_link_index", side_effect=raise_glib_error
)
test_context.patch(
"orca.ax_hypertext.debug.print_message",
new=essential_modules["orca.debug"].print_message,
)
result = AXHypertext.get_child_at_offset(mock_accessible, 5)
assert result is None
essential_modules["orca.debug"].print_message.assert_called()
def test_get_child_at_offset_with_glib_error_get_object(
self, test_context: OrcaTestContext
) -> None:
"""Test AXHypertext.get_child_at_offset handles GLib.GError in get_object."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
mock_hyperlink = test_context.Mock(spec=Atspi.Hyperlink)
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
def raise_glib_error(link, index) -> None:
raise GLib.GError("Test error")
test_context.patch(
"orca.ax_hypertext.AXObject.supports_hypertext", return_value=True
)
test_context.patch(
"orca.ax_hypertext.Atspi.Hypertext.get_link_index", return_value=0
)
test_context.patch(
"orca.ax_hypertext.AXHypertext._get_link_at_index",
return_value=mock_hyperlink,
)
test_context.patch(
"orca.ax_hypertext.Atspi.Hyperlink.get_object", side_effect=raise_glib_error
)
test_context.patch(
"orca.ax_hypertext.debug.print_message",
new=essential_modules["orca.debug"].print_message,
)
result = AXHypertext.get_child_at_offset(mock_accessible, 5)
assert result is None
essential_modules["orca.debug"].print_message.assert_called()
def test_get_character_offset_in_parent_with_text_parent(
self, test_context: OrcaTestContext
) -> None:
"""Test AXHypertext.get_character_offset_in_parent with text-supporting parent."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
mock_parent = test_context.Mock(spec=Atspi.Accessible)
test_context.patch(
"orca.ax_hypertext.AXObject.get_parent", return_value=mock_parent
)
test_context.patch(
"orca.ax_hypertext.AXObject.supports_text", return_value=True
)
test_context.patch(
"orca.ax_hypertext.AXHypertext.get_link_start_offset", return_value=10
)
result = AXHypertext.get_character_offset_in_parent(mock_accessible)
assert result == 10
def test_get_character_offset_in_parent_without_text_parent(
self, test_context: OrcaTestContext
) -> None:
"""Test AXHypertext.get_character_offset_in_parent without text-supporting parent."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
mock_parent = test_context.Mock(spec=Atspi.Accessible)
test_context.patch(
"orca.ax_hypertext.AXObject.get_parent", return_value=mock_parent
)
test_context.patch(
"orca.ax_hypertext.AXObject.supports_text", return_value=False
)
result = AXHypertext.get_character_offset_in_parent(mock_accessible)
assert result == -1
def test_get_character_offset_in_parent_no_parent(self, test_context: OrcaTestContext) -> None:
"""Test AXHypertext.get_character_offset_in_parent with no parent."""
mock_accessible = test_context.Mock(spec=Atspi.Accessible)
self._setup_dependencies(test_context)
from orca.ax_hypertext import AXHypertext
test_context.patch("orca.ax_hypertext.AXObject.get_parent", return_value=None)
test_context.patch(
"orca.ax_hypertext.Atspi.Accessible.get_hyperlink", return_value=None
)
result = AXHypertext.get_character_offset_in_parent(mock_accessible)
assert result == -1
|