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