| 1 |
# Unit tests for ax_text.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=too-many-locals |
| 29 |
|
| 30 |
"""Unit tests for ax_text.py methods.""" |
| 31 |
|
| 32 |
from __future__ import annotations |
| 33 |
|
| 34 |
from typing import TYPE_CHECKING |
| 35 |
|
| 36 |
import gi |
| 37 |
import pytest |
| 38 |
|
| 39 |
gi.require_version("Atspi", "2.0") |
| 40 |
from gi.repository import Atspi, GLib |
| 41 |
|
| 42 |
if TYPE_CHECKING: |
| 43 |
from collections.abc import Generator |
| 44 |
from unittest.mock import MagicMock |
| 45 |
|
| 46 |
from .orca_test_context import OrcaTestContext |
| 47 |
|
| 48 |
|
| 49 |
class MockRect: |
| 50 |
"""Mock rectangle class for testing.""" |
| 51 |
|
| 52 |
def __init__(self, x: int = 0, y: int = 0, width: int = 0, height: int = 0) -> None: |
| 53 |
self.x = x |
| 54 |
self.y = y |
| 55 |
self.width = width |
| 56 |
self.height = height |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.unit |
| 60 |
class TestAXTextAttribute: |
| 61 |
"""Test AXTextAttribute enum methods.""" |
| 62 |
|
| 63 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 64 |
"""Set up mocks for ax_text dependencies.""" |
| 65 |
|
| 66 |
additional_modules = [ |
| 67 |
"locale", |
| 68 |
"orca.colornames", |
| 69 |
"orca.ax_utilities_role", |
| 70 |
"orca.ax_utilities_state", |
| 71 |
] |
| 72 |
essential_modules = test_context.setup_shared_dependencies(additional_modules) |
| 73 |
|
| 74 |
locale_mock = essential_modules["locale"] |
| 75 |
locale_mock.localeconv = test_context.Mock(return_value={"decimal_point": "."}) |
| 76 |
|
| 77 |
colornames_mock = essential_modules["orca.colornames"] |
| 78 |
colornames_mock.COLOR_NAMES = {"#ff0000": "red", "#00ff00": "green"} |
| 79 |
colornames_mock.rgb_string_to_color_name = test_context.Mock( |
| 80 |
side_effect=lambda color: colornames_mock.COLOR_NAMES.get(color, color), |
| 81 |
) |
| 82 |
colornames_mock.normalize_rgb_string = test_context.Mock(side_effect=lambda color: color) |
| 83 |
colornames_mock.get_presentable_color_name = test_context.Mock( |
| 84 |
side_effect=lambda color: colornames_mock.COLOR_NAMES.get(color, color), |
| 85 |
) |
| 86 |
|
| 87 |
debug_mock = essential_modules["orca.debug"] |
| 88 |
debug_mock.LEVEL_INFO = 800 |
| 89 |
debug_mock.debugLevel = 0 |
| 90 |
debug_mock.debug = test_context.Mock() |
| 91 |
debug_mock.print_message = test_context.Mock() |
| 92 |
|
| 93 |
messages_mock = essential_modules["orca.messages"] |
| 94 |
messages_mock.TEXT_ATTRIBUTE_NAMES = { |
| 95 |
"bg-color": "Background Color", |
| 96 |
"size": "size", |
| 97 |
"language": "Language", |
| 98 |
"weight": "Weight", |
| 99 |
"family-name": "Family Name", |
| 100 |
} |
| 101 |
messages_mock.pixel_count = test_context.Mock( |
| 102 |
side_effect=lambda count: f"{count} pixel{'s' if count != 1.0 else ''}", |
| 103 |
) |
| 104 |
|
| 105 |
text_attribute_names_mock = essential_modules["orca.text_attribute_names"] |
| 106 |
text_attribute_names_mock.attribute_names = { |
| 107 |
"bg-color": "Background Color", |
| 108 |
"size": "size", |
| 109 |
"language": "Language", |
| 110 |
"weight": "Weight", |
| 111 |
"family-name": "Family Name", |
| 112 |
} |
| 113 |
text_attribute_names_mock.attribute_values = test_context.Mock() |
| 114 |
text_attribute_names_mock.attribute_values.get = test_context.Mock( |
| 115 |
side_effect=lambda value, default: default, |
| 116 |
) |
| 117 |
|
| 118 |
ax_object_mock = essential_modules["orca.ax_object"] |
| 119 |
ax_object_class_mock = test_context.Mock() |
| 120 |
ax_object_class_mock.supports_text = test_context.Mock(return_value=True) |
| 121 |
ax_object_mock.AXObject = ax_object_class_mock |
| 122 |
|
| 123 |
ax_utilities_role_mock = essential_modules["orca.ax_utilities_role"] |
| 124 |
role_class_mock = test_context.Mock() |
| 125 |
ax_utilities_role_mock.AXUtilitiesRole = role_class_mock |
| 126 |
|
| 127 |
ax_utilities_state_mock = essential_modules["orca.ax_utilities_state"] |
| 128 |
state_class_mock = test_context.Mock() |
| 129 |
ax_utilities_state_mock.AXUtilitiesState = state_class_mock |
| 130 |
|
| 131 |
return essential_modules |
| 132 |
|
| 133 |
def test_from_string_with_valid_attribute(self, test_context: OrcaTestContext) -> None: |
| 134 |
"""Test AXTextAttribute.from_string with valid attribute name.""" |
| 135 |
|
| 136 |
self._setup_dependencies(test_context) |
| 137 |
from orca.ax_text import AXTextAttribute |
| 138 |
|
| 139 |
result = AXTextAttribute.from_string("bg-color") |
| 140 |
assert result == AXTextAttribute.BG_COLOR |
| 141 |
|
| 142 |
def test_from_string_with_invalid_attribute(self, test_context: OrcaTestContext) -> None: |
| 143 |
"""Test AXTextAttribute.from_string with invalid attribute name.""" |
| 144 |
|
| 145 |
self._setup_dependencies(test_context) |
| 146 |
from orca.ax_text import AXTextAttribute |
| 147 |
|
| 148 |
result = AXTextAttribute.from_string("invalid-attribute") |
| 149 |
assert result is None |
| 150 |
|
| 151 |
def test_from_localized_string_with_valid_attribute( |
| 152 |
self, |
| 153 |
test_context: OrcaTestContext, |
| 154 |
) -> None: |
| 155 |
"""Test AXTextAttribute.from_localized_string with valid localized name.""" |
| 156 |
|
| 157 |
self._setup_dependencies(test_context) |
| 158 |
from orca.ax_text import AXTextAttribute |
| 159 |
|
| 160 |
result = AXTextAttribute.from_localized_string("Background Color") |
| 161 |
assert result == AXTextAttribute.BG_COLOR |
| 162 |
|
| 163 |
def test_from_localized_string_with_invalid_attribute( |
| 164 |
self, |
| 165 |
test_context: OrcaTestContext, |
| 166 |
) -> None: |
| 167 |
"""Test AXTextAttribute.from_localized_string with invalid localized name.""" |
| 168 |
|
| 169 |
self._setup_dependencies(test_context) |
| 170 |
from orca.ax_text import AXTextAttribute |
| 171 |
|
| 172 |
result = AXTextAttribute.from_localized_string("Invalid Localized") |
| 173 |
assert result is None |
| 174 |
|
| 175 |
def test_get_attribute_name(self, test_context: OrcaTestContext) -> None: |
| 176 |
"""Test AXTextAttribute.get_attribute_name.""" |
| 177 |
|
| 178 |
self._setup_dependencies(test_context) |
| 179 |
from orca.ax_text import AXTextAttribute |
| 180 |
|
| 181 |
result = AXTextAttribute.BG_COLOR.get_attribute_name() |
| 182 |
assert result == "bg-color" |
| 183 |
|
| 184 |
def test_get_localized_name_with_translation(self, test_context: OrcaTestContext) -> None: |
| 185 |
"""Test AXTextAttribute.get_localized_name with available translation.""" |
| 186 |
|
| 187 |
self._setup_dependencies(test_context) |
| 188 |
from orca.ax_text import AXTextAttribute |
| 189 |
|
| 190 |
result = AXTextAttribute.BG_COLOR.get_localized_name() |
| 191 |
assert result == "Background Color" |
| 192 |
|
| 193 |
def test_get_localized_name_without_translation(self, test_context: OrcaTestContext) -> None: |
| 194 |
"""Test AXTextAttribute.get_localized_name without available translation.""" |
| 195 |
|
| 196 |
self._setup_dependencies(test_context) |
| 197 |
from orca.ax_text import AXTextAttribute |
| 198 |
|
| 199 |
result = AXTextAttribute.INDENT.get_localized_name() |
| 200 |
assert result == "indent" |
| 201 |
|
| 202 |
@pytest.mark.parametrize( |
| 203 |
"case", |
| 204 |
[ |
| 205 |
{ |
| 206 |
"id": "pixel_int_multiple", |
| 207 |
"attribute_name": "INDENT", |
| 208 |
"input_value": "12px", |
| 209 |
"expected_result": "12 pixels", |
| 210 |
}, |
| 211 |
{ |
| 212 |
"id": "pixel_int_singular", |
| 213 |
"attribute_name": "INDENT", |
| 214 |
"input_value": "1px", |
| 215 |
"expected_result": "1 pixel", |
| 216 |
}, |
| 217 |
{ |
| 218 |
"id": "pixel_int_zero", |
| 219 |
"attribute_name": "INDENT", |
| 220 |
"input_value": "0px", |
| 221 |
"expected_result": "0 pixels", |
| 222 |
}, |
| 223 |
{ |
| 224 |
"id": "pixel_float_multiple", |
| 225 |
"attribute_name": "INDENT", |
| 226 |
"input_value": "12.5px", |
| 227 |
"expected_result": "12.5 pixels", |
| 228 |
}, |
| 229 |
{ |
| 230 |
"id": "pixel_float_singular", |
| 231 |
"attribute_name": "INDENT", |
| 232 |
"input_value": "1.0px", |
| 233 |
"expected_result": "1.0 pixel", |
| 234 |
}, |
| 235 |
{ |
| 236 |
"id": "pixel_float_zero", |
| 237 |
"attribute_name": "INDENT", |
| 238 |
"input_value": "0.0px", |
| 239 |
"expected_result": "0.0 pixels", |
| 240 |
}, |
| 241 |
{ |
| 242 |
"id": "color_red", |
| 243 |
"attribute_name": "BG_COLOR", |
| 244 |
"input_value": "#ff0000", |
| 245 |
"expected_result": "red", |
| 246 |
}, |
| 247 |
{ |
| 248 |
"id": "moz_prefix_removal", |
| 249 |
"attribute_name": "JUSTIFICATION", |
| 250 |
"input_value": "left-moz", |
| 251 |
"expected_result": "left", |
| 252 |
}, |
| 253 |
{ |
| 254 |
"id": "justify_to_fill", |
| 255 |
"attribute_name": "JUSTIFICATION", |
| 256 |
"input_value": "justify", |
| 257 |
"expected_result": "fill", |
| 258 |
}, |
| 259 |
{ |
| 260 |
"id": "family_name_cleanup", |
| 261 |
"attribute_name": "FAMILY_NAME", |
| 262 |
"input_value": "Arial, sans-serif", |
| 263 |
"expected_result": "Arial", |
| 264 |
}, |
| 265 |
{ |
| 266 |
"id": "regular_value", |
| 267 |
"attribute_name": "LANGUAGE", |
| 268 |
"input_value": "en-US", |
| 269 |
"expected_result": "en-US", |
| 270 |
}, |
| 271 |
{ |
| 272 |
"id": "none_value", |
| 273 |
"attribute_name": "LANGUAGE", |
| 274 |
"input_value": None, |
| 275 |
"expected_result": "", |
| 276 |
}, |
| 277 |
], |
| 278 |
ids=lambda case: case["id"], |
| 279 |
) |
| 280 |
def test_get_localized_value_scenarios( |
| 281 |
self, |
| 282 |
test_context: OrcaTestContext, |
| 283 |
case: dict, |
| 284 |
) -> None: |
| 285 |
"""Test AXTextAttribute.get_localized_value with various scenarios.""" |
| 286 |
|
| 287 |
self._setup_dependencies(test_context) |
| 288 |
from orca.ax_text import AXTextAttribute |
| 289 |
|
| 290 |
attribute = getattr(AXTextAttribute, case["attribute_name"]) |
| 291 |
result = attribute.get_localized_value(case["input_value"]) |
| 292 |
assert result == case["expected_result"] |
| 293 |
|
| 294 |
@pytest.mark.parametrize( |
| 295 |
"case", |
| 296 |
[ |
| 297 |
{"id": "bg_color_true", "attribute": "BG_COLOR", "expected": True}, |
| 298 |
{"id": "bg_full_height_false", "attribute": "BG_FULL_HEIGHT", "expected": False}, |
| 299 |
], |
| 300 |
ids=lambda case: case["id"], |
| 301 |
) |
| 302 |
def test_should_present_by_default(self, test_context: OrcaTestContext, case: dict) -> None: |
| 303 |
"""Test AXTextAttribute.should_present_by_default.""" |
| 304 |
|
| 305 |
self._setup_dependencies(test_context) |
| 306 |
from orca.ax_text import AXTextAttribute |
| 307 |
|
| 308 |
attr = getattr(AXTextAttribute, case["attribute"]) |
| 309 |
result = attr.should_present_by_default() |
| 310 |
assert result == case["expected"] |
| 311 |
|
| 312 |
@pytest.mark.parametrize( |
| 313 |
"case", |
| 314 |
[ |
| 315 |
{"id": "bg_color_zero", "attribute": "BG_COLOR", "value": "0", "expected": True}, |
| 316 |
{"id": "bg_color_zero_px", "attribute": "BG_COLOR", "value": "0px", "expected": True}, |
| 317 |
{"id": "bg_color_none", "attribute": "BG_COLOR", "value": "none", "expected": True}, |
| 318 |
{"id": "bg_color_empty", "attribute": "BG_COLOR", "value": "", "expected": True}, |
| 319 |
{"id": "bg_color_null", "attribute": "BG_COLOR", "value": None, "expected": True}, |
| 320 |
{"id": "scale_one", "attribute": "SCALE", "value": "1.0", "expected": True}, |
| 321 |
{ |
| 322 |
"id": "text_position_baseline", |
| 323 |
"attribute": "TEXT_POSITION", |
| 324 |
"value": "baseline", |
| 325 |
"expected": True, |
| 326 |
}, |
| 327 |
{"id": "weight_400", "attribute": "WEIGHT", "value": "400", "expected": True}, |
| 328 |
{ |
| 329 |
"id": "bg_color_bold_false", |
| 330 |
"attribute": "BG_COLOR", |
| 331 |
"value": "bold", |
| 332 |
"expected": False, |
| 333 |
}, |
| 334 |
], |
| 335 |
ids=lambda case: case["id"], |
| 336 |
) |
| 337 |
def test_value_is_default(self, test_context: OrcaTestContext, case: dict) -> None: |
| 338 |
"""Test AXTextAttribute.value_is_default.""" |
| 339 |
|
| 340 |
self._setup_dependencies(test_context) |
| 341 |
from orca.ax_text import AXTextAttribute |
| 342 |
|
| 343 |
attr = getattr(AXTextAttribute, case["attribute"]) |
| 344 |
result = attr.value_is_default(case["value"]) |
| 345 |
assert result == case["expected"] |
| 346 |
|
| 347 |
|
| 348 |
@pytest.mark.unit |
| 349 |
class TestAXText: |
| 350 |
"""Test AXText class methods.""" |
| 351 |
|
| 352 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 353 |
"""Set up mocks for ax_text dependencies.""" |
| 354 |
|
| 355 |
additional_modules = [ |
| 356 |
"locale", |
| 357 |
"orca.colornames", |
| 358 |
"orca.ax_utilities_role", |
| 359 |
"orca.ax_utilities_state", |
| 360 |
] |
| 361 |
essential_modules = test_context.setup_shared_dependencies(additional_modules) |
| 362 |
|
| 363 |
locale_mock = essential_modules["locale"] |
| 364 |
locale_mock.localeconv = test_context.Mock(return_value={"decimal_point": "."}) |
| 365 |
|
| 366 |
colornames_mock = essential_modules["orca.colornames"] |
| 367 |
colornames_mock.COLOR_NAMES = {"#ff0000": "red", "#00ff00": "green"} |
| 368 |
colornames_mock.rgb_string_to_color_name = test_context.Mock( |
| 369 |
side_effect=lambda color: colornames_mock.COLOR_NAMES.get(color, color), |
| 370 |
) |
| 371 |
colornames_mock.normalize_rgb_string = test_context.Mock(side_effect=lambda color: color) |
| 372 |
colornames_mock.get_presentable_color_name = test_context.Mock( |
| 373 |
side_effect=lambda color: colornames_mock.COLOR_NAMES.get(color, color), |
| 374 |
) |
| 375 |
|
| 376 |
debug_mock = essential_modules["orca.debug"] |
| 377 |
debug_mock.LEVEL_INFO = 800 |
| 378 |
debug_mock.debugLevel = 0 |
| 379 |
debug_mock.debug = test_context.Mock() |
| 380 |
debug_mock.print_message = test_context.Mock() |
| 381 |
|
| 382 |
messages_mock = essential_modules["orca.messages"] |
| 383 |
messages_mock.TEXT_ATTRIBUTE_NAMES = { |
| 384 |
"bg-color": "Background Color", |
| 385 |
"size": "size", |
| 386 |
"language": "Language", |
| 387 |
"weight": "Weight", |
| 388 |
"family-name": "Family Name", |
| 389 |
} |
| 390 |
messages_mock.pixel_count = test_context.Mock( |
| 391 |
side_effect=lambda count: f"{count} pixel{'s' if count != 1.0 else ''}", |
| 392 |
) |
| 393 |
|
| 394 |
text_attribute_names_mock = essential_modules["orca.text_attribute_names"] |
| 395 |
text_attribute_names_mock.attribute_names = { |
| 396 |
"bg-color": "Background Color", |
| 397 |
"size": "size", |
| 398 |
"language": "Language", |
| 399 |
"weight": "Weight", |
| 400 |
"family-name": "Family Name", |
| 401 |
} |
| 402 |
text_attribute_names_mock.attribute_values = test_context.Mock() |
| 403 |
text_attribute_names_mock.attribute_values.get = test_context.Mock( |
| 404 |
side_effect=lambda value, default: default, |
| 405 |
) |
| 406 |
|
| 407 |
ax_object_mock = essential_modules["orca.ax_object"] |
| 408 |
ax_object_class_mock = test_context.Mock() |
| 409 |
ax_object_class_mock.supports_text = test_context.Mock(return_value=True) |
| 410 |
ax_object_mock.AXObject = ax_object_class_mock |
| 411 |
|
| 412 |
ax_utilities_role_mock = essential_modules["orca.ax_utilities_role"] |
| 413 |
role_class_mock = test_context.Mock() |
| 414 |
ax_utilities_role_mock.AXUtilitiesRole = role_class_mock |
| 415 |
|
| 416 |
ax_utilities_state_mock = essential_modules["orca.ax_utilities_state"] |
| 417 |
state_class_mock = test_context.Mock() |
| 418 |
ax_utilities_state_mock.AXUtilitiesState = state_class_mock |
| 419 |
|
| 420 |
return essential_modules |
| 421 |
|
| 422 |
def test_is_eoc_with_embedded_object_character(self, test_context: OrcaTestContext) -> None: |
| 423 |
"""Test AXText.is_eoc with embedded object character.""" |
| 424 |
|
| 425 |
self._setup_dependencies(test_context) |
| 426 |
from orca.ax_utilities_text import AXUtilitiesText |
| 427 |
|
| 428 |
result = AXUtilitiesText.is_eoc("\ufffc") |
| 429 |
assert result is True |
| 430 |
|
| 431 |
def test_is_eoc_with_regular_character(self, test_context: OrcaTestContext) -> None: |
| 432 |
"""Test AXText.is_eoc with regular character.""" |
| 433 |
|
| 434 |
self._setup_dependencies(test_context) |
| 435 |
from orca.ax_utilities_text import AXUtilitiesText |
| 436 |
|
| 437 |
result = AXUtilitiesText.is_eoc("a") |
| 438 |
assert result is False |
| 439 |
|
| 440 |
def test_character_at_offset_is_eoc_with_eoc(self, test_context: OrcaTestContext) -> None: |
| 441 |
"""Test AXText.character_at_offset_is_eoc with embedded object character.""" |
| 442 |
|
| 443 |
self._setup_dependencies(test_context) |
| 444 |
from orca.ax_text import AXText |
| 445 |
from orca.ax_utilities_text import AXUtilitiesText |
| 446 |
|
| 447 |
test_context.patch_object(AXText, "get_character_at_offset", return_value=("\ufffc", 5, 6)) |
| 448 |
result = AXUtilitiesText.character_at_offset_is_eoc( |
| 449 |
test_context.Mock(spec=Atspi.Accessible), 5 |
| 450 |
) |
| 451 |
assert result is True |
| 452 |
|
| 453 |
def test_character_at_offset_is_eoc_without_eoc(self, test_context: OrcaTestContext) -> None: |
| 454 |
"""Test AXText.character_at_offset_is_eoc without embedded object character.""" |
| 455 |
|
| 456 |
self._setup_dependencies(test_context) |
| 457 |
from orca.ax_text import AXText |
| 458 |
from orca.ax_utilities_text import AXUtilitiesText |
| 459 |
|
| 460 |
test_context.patch_object(AXText, "get_character_at_offset", return_value=("a", 5, 6)) |
| 461 |
result = AXUtilitiesText.character_at_offset_is_eoc( |
| 462 |
test_context.Mock(spec=Atspi.Accessible), 5 |
| 463 |
) |
| 464 |
assert result is False |
| 465 |
|
| 466 |
def test_is_whitespace_or_empty_with_whitespace(self, test_context: OrcaTestContext) -> None: |
| 467 |
"""Test AXText.is_whitespace_or_empty with whitespace text.""" |
| 468 |
|
| 469 |
self._setup_dependencies(test_context) |
| 470 |
from orca.ax_text import AXText |
| 471 |
from orca.ax_utilities_text import AXUtilitiesText |
| 472 |
|
| 473 |
test_context.patch_object(AXText, "get_all_text", return_value=" \t\n ") |
| 474 |
result = AXUtilitiesText.is_whitespace_or_empty(test_context.Mock(spec=Atspi.Accessible)) |
| 475 |
assert result is True |
| 476 |
|
| 477 |
def test_is_whitespace_or_empty_with_content(self, test_context: OrcaTestContext) -> None: |
| 478 |
"""Test AXText.is_whitespace_or_empty with actual content.""" |
| 479 |
|
| 480 |
self._setup_dependencies(test_context) |
| 481 |
from orca.ax_text import AXText |
| 482 |
from orca.ax_utilities_text import AXUtilitiesText |
| 483 |
|
| 484 |
test_context.patch_object(AXText, "get_all_text", return_value="Hello world") |
| 485 |
result = AXUtilitiesText.is_whitespace_or_empty(test_context.Mock(spec=Atspi.Accessible)) |
| 486 |
assert result is False |
| 487 |
|
| 488 |
def test_has_presentable_text_with_word_characters(self, test_context: OrcaTestContext) -> None: |
| 489 |
"""Test AXText.has_presentable_text with word characters.""" |
| 490 |
|
| 491 |
self._setup_dependencies(test_context) |
| 492 |
from orca.ax_text import AXText |
| 493 |
from orca.ax_utilities_text import AXUtilitiesText |
| 494 |
|
| 495 |
test_context.patch_object(AXText, "get_all_text", return_value="Hello123") |
| 496 |
result = AXUtilitiesText.has_presentable_text(test_context.Mock(spec=Atspi.Accessible)) |
| 497 |
assert result is True |
| 498 |
|
| 499 |
def test_has_presentable_text_without_word_characters( |
| 500 |
self, |
| 501 |
test_context: OrcaTestContext, |
| 502 |
) -> None: |
| 503 |
"""Test AXText.has_presentable_text without word characters.""" |
| 504 |
|
| 505 |
self._setup_dependencies(test_context) |
| 506 |
from orca.ax_text import AXText |
| 507 |
from orca.ax_utilities_text import AXUtilitiesText |
| 508 |
|
| 509 |
test_context.patch_object(AXText, "get_all_text", return_value="!@#$%^&*()") |
| 510 |
result = AXUtilitiesText.has_presentable_text(test_context.Mock(spec=Atspi.Accessible)) |
| 511 |
assert result is False |
| 512 |
|
| 513 |
@pytest.mark.parametrize( |
| 514 |
"case", |
| 515 |
[ |
| 516 |
{ |
| 517 |
"id": "successful", |
| 518 |
"should_raise_error": False, |
| 519 |
"expected_result": 50, |
| 520 |
"expected_debug_method": "print_tokens", |
| 521 |
}, |
| 522 |
{ |
| 523 |
"id": "glib_error", |
| 524 |
"should_raise_error": True, |
| 525 |
"expected_result": -1, |
| 526 |
"expected_debug_method": "print_message", |
| 527 |
}, |
| 528 |
], |
| 529 |
ids=lambda case: case["id"], |
| 530 |
) |
| 531 |
def test_get_caret_offset( |
| 532 |
self, |
| 533 |
test_context: OrcaTestContext, |
| 534 |
case: dict, |
| 535 |
) -> None: |
| 536 |
"""Test AXText.get_caret_offset successful case and GLib.GError handling.""" |
| 537 |
|
| 538 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 539 |
from orca.ax_text import AXText |
| 540 |
|
| 541 |
if case["should_raise_error"]: |
| 542 |
|
| 543 |
def raise_glib_error(_obj) -> None: |
| 544 |
raise GLib.GError("Test error") |
| 545 |
|
| 546 |
test_context.patch_object(Atspi.Text, "get_caret_offset", new=raise_glib_error) |
| 547 |
else: |
| 548 |
test_context.patch_object(Atspi.Text, "get_caret_offset", return_value=50) |
| 549 |
|
| 550 |
essential_modules["orca.debug"].print_tokens = test_context.Mock() |
| 551 |
essential_modules["orca.debug"].print_message = test_context.Mock() |
| 552 |
result = AXText.get_caret_offset(test_context.Mock(spec=Atspi.Accessible)) |
| 553 |
assert result == case["expected_result"] |
| 554 |
getattr(essential_modules["orca.debug"], case["expected_debug_method"]).assert_called() |
| 555 |
|
| 556 |
def test_set_caret_offset_successful(self, test_context: OrcaTestContext) -> None: |
| 557 |
"""Test AXText.set_caret_offset successful case.""" |
| 558 |
|
| 559 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 560 |
from orca.ax_text import AXText |
| 561 |
|
| 562 |
test_context.patch_object(Atspi.Text, "set_caret_offset", return_value=True) |
| 563 |
essential_modules["orca.debug"].print_tokens = test_context.Mock() |
| 564 |
result = AXText.set_caret_offset(test_context.Mock(spec=Atspi.Accessible), 25) |
| 565 |
assert result is True |
| 566 |
essential_modules["orca.debug"].print_tokens.assert_called() |
| 567 |
|
| 568 |
def test_set_caret_offset_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 569 |
"""Test AXText.set_caret_offset handles GLib.GError.""" |
| 570 |
|
| 571 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 572 |
from orca.ax_text import AXText |
| 573 |
|
| 574 |
def raise_glib_error(_obj, _offset) -> None: |
| 575 |
raise GLib.GError("Test error") |
| 576 |
|
| 577 |
test_context.patch_object(Atspi.Text, "set_caret_offset", new=raise_glib_error) |
| 578 |
essential_modules["orca.debug"].print_message = test_context.Mock() |
| 579 |
result = AXText.set_caret_offset(test_context.Mock(spec=Atspi.Accessible), 25) |
| 580 |
assert result is False |
| 581 |
essential_modules["orca.debug"].print_message.assert_called() |
| 582 |
|
| 583 |
def test_set_caret_offset_to_start(self, test_context: OrcaTestContext) -> None: |
| 584 |
"""Test AXText.set_caret_offset_to_start.""" |
| 585 |
|
| 586 |
self._setup_dependencies(test_context) |
| 587 |
from orca.ax_text import AXText |
| 588 |
from orca.ax_utilities_text import AXUtilitiesText |
| 589 |
|
| 590 |
test_context.patch_object(AXText, "set_caret_offset", return_value=True) |
| 591 |
result = AXUtilitiesText.set_caret_offset_to_start(test_context.Mock(spec=Atspi.Accessible)) |
| 592 |
assert result is True |
| 593 |
|
| 594 |
def test_set_caret_offset_to_end(self, test_context: OrcaTestContext) -> None: |
| 595 |
"""Test AXText.set_caret_offset_to_end.""" |
| 596 |
|
| 597 |
self._setup_dependencies(test_context) |
| 598 |
from orca.ax_text import AXText |
| 599 |
from orca.ax_utilities_text import AXUtilitiesText |
| 600 |
|
| 601 |
test_context.patch_object(AXText, "get_character_count", return_value=100) |
| 602 |
test_context.patch_object(AXText, "set_caret_offset", return_value=True) |
| 603 |
result = AXUtilitiesText.set_caret_offset_to_end(test_context.Mock(spec=Atspi.Accessible)) |
| 604 |
assert result is True |
| 605 |
|
| 606 |
def test_has_selected_text_without_selection(self, test_context: OrcaTestContext) -> None: |
| 607 |
"""Test AXText.has_selected_text without text selection.""" |
| 608 |
|
| 609 |
self._setup_dependencies(test_context) |
| 610 |
from orca.ax_text import AXText |
| 611 |
from orca.ax_utilities_text import AXUtilitiesText |
| 612 |
|
| 613 |
test_context.patch_object(AXText, "get_n_selections", return_value=0) |
| 614 |
result = AXUtilitiesText.has_selected_text(test_context.Mock(spec=Atspi.Accessible)) |
| 615 |
assert result is False |
| 616 |
|
| 617 |
def test_get_n_selections_successful(self, test_context: OrcaTestContext) -> None: |
| 618 |
"""Test AXText.get_n_selections successful case.""" |
| 619 |
|
| 620 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 621 |
from orca.ax_text import AXText |
| 622 |
|
| 623 |
test_context.patch_object(Atspi.Text, "get_n_selections", return_value=2) |
| 624 |
essential_modules["orca.debug"].print_tokens = test_context.Mock() |
| 625 |
result = AXText.get_n_selections(test_context.Mock(spec=Atspi.Accessible)) |
| 626 |
assert result == 2 |
| 627 |
essential_modules["orca.debug"].print_tokens.assert_called() |
| 628 |
|
| 629 |
def test_get_n_selections_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 630 |
"""Test AXText.get_n_selections handles GLib.GError.""" |
| 631 |
|
| 632 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 633 |
from orca.ax_text import AXText |
| 634 |
|
| 635 |
def raise_glib_error(_obj) -> None: |
| 636 |
raise GLib.GError("Test error") |
| 637 |
|
| 638 |
test_context.patch_object(Atspi.Text, "get_n_selections", new=raise_glib_error) |
| 639 |
essential_modules["orca.debug"].print_message = test_context.Mock() |
| 640 |
result = AXText.get_n_selections(test_context.Mock(spec=Atspi.Accessible)) |
| 641 |
assert result == 0 |
| 642 |
essential_modules["orca.debug"].print_message.assert_called() |
| 643 |
|
| 644 |
def test_get_cached_selected_text_without_cache(self, test_context: OrcaTestContext) -> None: |
| 645 |
"""Test AXText.get_cached_selected_text without cached data.""" |
| 646 |
|
| 647 |
self._setup_dependencies(test_context) |
| 648 |
from orca.ax_utilities_text import AXUtilitiesText |
| 649 |
|
| 650 |
test_context.Mock(spec=Atspi.Accessible).hash = test_context.Mock(return_value=54321) |
| 651 |
AXUtilitiesText.CACHED_TEXT_SELECTION.clear() |
| 652 |
result = AXUtilitiesText.get_cached_selected_text(test_context.Mock(spec=Atspi.Accessible)) |
| 653 |
assert result == ("", 0, 0) |
| 654 |
|
| 655 |
@pytest.mark.parametrize( |
| 656 |
"case", |
| 657 |
[ |
| 658 |
{ |
| 659 |
"id": "fully_contained", |
| 660 |
"rect1": MockRect(x=10, y=10, width=20, height=20), |
| 661 |
"rect2": MockRect(x=5, y=5, width=30, height=30), |
| 662 |
"expected": True, |
| 663 |
}, |
| 664 |
{ |
| 665 |
"id": "not_contained", |
| 666 |
"rect1": MockRect(x=0, y=0, width=20, height=20), |
| 667 |
"rect2": MockRect(x=5, y=5, width=10, height=10), |
| 668 |
"expected": False, |
| 669 |
}, |
| 670 |
{ |
| 671 |
"id": "exactly_same", |
| 672 |
"rect1": MockRect(x=10, y=10, width=20, height=20), |
| 673 |
"rect2": MockRect(x=10, y=10, width=20, height=20), |
| 674 |
"expected": True, |
| 675 |
}, |
| 676 |
], |
| 677 |
ids=lambda case: case["id"], |
| 678 |
) |
| 679 |
def test_rect_is_fully_contained_in(self, test_context: OrcaTestContext, case: dict) -> None: |
| 680 |
"""Test AXText._rect_is_fully_contained_in.""" |
| 681 |
|
| 682 |
self._setup_dependencies(test_context) |
| 683 |
from orca.ax_utilities_text import AXUtilitiesText |
| 684 |
|
| 685 |
result = AXUtilitiesText._rect_is_fully_contained_in(case["rect1"], case["rect2"]) |
| 686 |
assert result is case["expected"] |
| 687 |
|
| 688 |
@pytest.mark.parametrize( |
| 689 |
"case", |
| 690 |
[ |
| 691 |
{ |
| 692 |
"id": "line_inside_clip", |
| 693 |
"line_rect": MockRect(y=10, height=10), |
| 694 |
"clip_rect": MockRect(y=5, height=30), |
| 695 |
"expected": 0, |
| 696 |
}, |
| 697 |
{ |
| 698 |
"id": "line_above_clip", |
| 699 |
"line_rect": MockRect(y=0, height=10), |
| 700 |
"clip_rect": MockRect(y=15, height=20), |
| 701 |
"expected": -1, |
| 702 |
}, |
| 703 |
{ |
| 704 |
"id": "line_below_clip", |
| 705 |
"line_rect": MockRect(y=40, height=10), |
| 706 |
"clip_rect": MockRect(y=5, height=20), |
| 707 |
"expected": 1, |
| 708 |
}, |
| 709 |
], |
| 710 |
ids=lambda case: case["id"], |
| 711 |
) |
| 712 |
def test_line_comparison(self, test_context: OrcaTestContext, case: dict) -> None: |
| 713 |
"""Test AXText._line_comparison.""" |
| 714 |
|
| 715 |
self._setup_dependencies(test_context) |
| 716 |
from orca.ax_utilities_text import AXUtilitiesText |
| 717 |
|
| 718 |
result = AXUtilitiesText._line_comparison(case["line_rect"], case["clip_rect"]) |
| 719 |
assert result == case["expected"] |
| 720 |
|
| 721 |
@pytest.mark.parametrize( |
| 722 |
"case", |
| 723 |
[ |
| 724 |
{"id": "successful", "should_raise_error": False, "expected_result": ("hello", 5, 10)}, |
| 725 |
{"id": "glib_error", "should_raise_error": True, "expected_result": ("", 0, 0)}, |
| 726 |
], |
| 727 |
ids=lambda case: case["id"], |
| 728 |
) |
| 729 |
def test_get_word_at_offset( |
| 730 |
self, |
| 731 |
test_context: OrcaTestContext, |
| 732 |
case: dict, |
| 733 |
) -> None: |
| 734 |
"""Test AXText.get_word_at_offset successful case and GLib.GError handling.""" |
| 735 |
|
| 736 |
self._setup_dependencies(test_context) |
| 737 |
from orca.ax_text import AXText |
| 738 |
|
| 739 |
test_context.patch_object(AXText, "get_character_count", return_value=20) |
| 740 |
test_context.patch_object(AXText, "get_caret_offset", return_value=7) |
| 741 |
|
| 742 |
if case["should_raise_error"]: |
| 743 |
|
| 744 |
def mock_get_string_at_offset(_obj, _offset, _granularity) -> None: |
| 745 |
raise GLib.GError("Test error") |
| 746 |
|
| 747 |
test_context.patch( |
| 748 |
"gi.repository.Atspi.Text.get_string_at_offset", |
| 749 |
new=mock_get_string_at_offset, |
| 750 |
) |
| 751 |
else: |
| 752 |
mock_result = test_context.Mock() |
| 753 |
mock_result.content = "hello" |
| 754 |
mock_result.start_offset = 5 |
| 755 |
mock_result.end_offset = 10 |
| 756 |
test_context.patch( |
| 757 |
"gi.repository.Atspi.Text.get_string_at_offset", |
| 758 |
side_effect=lambda obj, offset, granularity: mock_result, |
| 759 |
) |
| 760 |
|
| 761 |
result = AXText.get_word_at_offset(test_context.Mock(spec=Atspi.Accessible)) |
| 762 |
assert result == case["expected_result"] |
| 763 |
|
| 764 |
def test_get_all_text_successful(self, test_context: OrcaTestContext) -> None: |
| 765 |
"""Test AXText.get_all_text.""" |
| 766 |
|
| 767 |
self._setup_dependencies(test_context) |
| 768 |
from orca.ax_text import AXText |
| 769 |
|
| 770 |
test_context.patch_object(AXText, "get_character_count", return_value=25) |
| 771 |
test_context.patch( |
| 772 |
"gi.repository.Atspi.Text.get_text", |
| 773 |
return_value="This is some test text", |
| 774 |
) |
| 775 |
result = AXText.get_all_text(test_context.Mock(spec=Atspi.Accessible)) |
| 776 |
assert result == "This is some test text" |
| 777 |
|
| 778 |
def test_get_all_text_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 779 |
"""Test AXText.get_all_text handles GLib.GError exceptions.""" |
| 780 |
|
| 781 |
self._setup_dependencies(test_context) |
| 782 |
from orca.ax_text import AXText |
| 783 |
|
| 784 |
test_context.patch_object(AXText, "get_character_count", return_value=25) |
| 785 |
|
| 786 |
def mock_get_text(_obj, _start, _end) -> None: |
| 787 |
raise GLib.GError("Test error") |
| 788 |
|
| 789 |
test_context.patch("gi.repository.Atspi.Text.get_text", new=mock_get_text) |
| 790 |
result = AXText.get_all_text(test_context.Mock(spec=Atspi.Accessible)) |
| 791 |
assert result == "" |
| 792 |
|
| 793 |
def test_get_selected_text_with_selection(self, test_context: OrcaTestContext) -> None: |
| 794 |
"""Test AXText.get_selected_text with text selected.""" |
| 795 |
|
| 796 |
self._setup_dependencies(test_context) |
| 797 |
from orca.ax_text import AXText |
| 798 |
from orca.ax_utilities_text import AXUtilitiesText |
| 799 |
|
| 800 |
test_context.patch_object(AXText, "get_selected_ranges", return_value=[(5, 10), (15, 20)]) |
| 801 |
test_context.patch_object( |
| 802 |
AXText, |
| 803 |
"get_substring", |
| 804 |
side_effect=lambda obj, start, end: f"text{start}-{end}", |
| 805 |
) |
| 806 |
result = AXUtilitiesText.get_selected_text(test_context.Mock(spec=Atspi.Accessible)) |
| 807 |
assert result == ("text5-10 text15-20", 5, 20) |
| 808 |
|
| 809 |
def test_get_selected_text_without_selection(self, test_context: OrcaTestContext) -> None: |
| 810 |
"""Test AXText.get_selected_text without text selected.""" |
| 811 |
|
| 812 |
self._setup_dependencies(test_context) |
| 813 |
from orca.ax_text import AXText |
| 814 |
from orca.ax_utilities_text import AXUtilitiesText |
| 815 |
|
| 816 |
test_context.patch_object(AXText, "get_selected_ranges", return_value=[]) |
| 817 |
result = AXUtilitiesText.get_selected_text(test_context.Mock(spec=Atspi.Accessible)) |
| 818 |
assert result == ("", 0, 0) |
| 819 |
|
| 820 |
def test_get_text_attributes_at_offset_successful(self, test_context: OrcaTestContext) -> None: |
| 821 |
"""Test AXText.get_text_attributes_at_offset.""" |
| 822 |
|
| 823 |
self._setup_dependencies(test_context) |
| 824 |
from orca.ax_object import AXObject |
| 825 |
from orca.ax_text import AXText |
| 826 |
|
| 827 |
test_context.patch_object(AXObject, "supports_text", return_value=True) |
| 828 |
test_context.patch_object(AXText, "get_caret_offset", return_value=5) |
| 829 |
test_context.patch_object( |
| 830 |
AXText, |
| 831 |
"get_text_attributes_at_offset", |
| 832 |
return_value=({"font-family": "Arial", "font-size": "12pt"}, 0, 10), |
| 833 |
) |
| 834 |
result = AXText.get_text_attributes_at_offset(test_context.Mock(spec=Atspi.Accessible)) |
| 835 |
assert result == ({"font-family": "Arial", "font-size": "12pt"}, 0, 10) |
| 836 |
|
| 837 |
def test_get_text_attributes_at_offset_with_glib_error( |
| 838 |
self, |
| 839 |
test_context: OrcaTestContext, |
| 840 |
) -> None: |
| 841 |
"""Test AXText.get_text_attributes_at_offset handles GLib.GError exceptions.""" |
| 842 |
|
| 843 |
self._setup_dependencies(test_context) |
| 844 |
from orca.ax_object import AXObject |
| 845 |
from orca.ax_text import AXText |
| 846 |
|
| 847 |
test_context.patch_object(AXObject, "supports_text", return_value=True) |
| 848 |
test_context.patch_object(AXText, "get_caret_offset", return_value=5) |
| 849 |
test_context.patch_object(AXText, "get_character_count", return_value=20) |
| 850 |
|
| 851 |
def mock_get_attribute_run(_obj, _offset, include_defaults=True) -> None: |
| 852 |
raise GLib.GError("Test error") |
| 853 |
|
| 854 |
test_context.patch("gi.repository.Atspi.Text.get_attribute_run", new=mock_get_attribute_run) |
| 855 |
result = AXText.get_text_attributes_at_offset(test_context.Mock(spec=Atspi.Accessible)) |
| 856 |
assert result == ({}, 0, 20) |
| 857 |
|
| 858 |
def test_get_text_attributes_at_offset_with_null_attributes( |
| 859 |
self, |
| 860 |
test_context: OrcaTestContext, |
| 861 |
) -> None: |
| 862 |
"""Test AXText.get_text_attributes_at_offset when the attribute dict is None.""" |
| 863 |
|
| 864 |
self._setup_dependencies(test_context) |
| 865 |
from orca.ax_object import AXObject |
| 866 |
from orca.ax_text import AXText |
| 867 |
|
| 868 |
test_context.patch_object(AXObject, "supports_text", return_value=True) |
| 869 |
test_context.patch_object(AXText, "get_caret_offset", return_value=5) |
| 870 |
test_context.patch_object(AXText, "get_character_count", return_value=20) |
| 871 |
|
| 872 |
# An app that is crashing can return a null hash table for the attributes while still |
| 873 |
# providing the start and end offsets, which PyGObject packs as (None, start, end). |
| 874 |
def mock_get_attribute_run(_obj, _offset, include_defaults=True): |
| 875 |
return None, 0, 10 |
| 876 |
|
| 877 |
test_context.patch("gi.repository.Atspi.Text.get_attribute_run", new=mock_get_attribute_run) |
| 878 |
result = AXText.get_text_attributes_at_offset(test_context.Mock(spec=Atspi.Accessible)) |
| 879 |
assert result == ({}, 0, 20) |
| 880 |
|
| 881 |
def test_get_all_text_attributes_successful(self, test_context: OrcaTestContext) -> None: |
| 882 |
"""Test AXText.get_all_text_attributes.""" |
| 883 |
|
| 884 |
self._setup_dependencies(test_context) |
| 885 |
from orca.ax_object import AXObject |
| 886 |
from orca.ax_text import AXText |
| 887 |
from orca.ax_utilities_text import AXUtilitiesText |
| 888 |
|
| 889 |
test_context.patch_object(AXObject, "supports_text", return_value=True) |
| 890 |
test_context.patch_object(AXText, "get_character_count", return_value=20) |
| 891 |
|
| 892 |
def mock_get_text_attributes_at_offset(_obj, offset) -> tuple[dict[str, str], int, int]: |
| 893 |
if offset < 10: |
| 894 |
return ({"font-weight": "bold"}, 0, 10) |
| 895 |
return ({"font-style": "italic"}, 10, 20) |
| 896 |
|
| 897 |
test_context.patch_object( |
| 898 |
AXText, |
| 899 |
"get_text_attributes_at_offset", |
| 900 |
new=mock_get_text_attributes_at_offset, |
| 901 |
) |
| 902 |
result = AXUtilitiesText.get_all_text_attributes(test_context.Mock(spec=Atspi.Accessible)) |
| 903 |
assert len(result) == 2 |
| 904 |
assert result[0] == (0, 10, {"font-weight": "bold"}) |
| 905 |
assert result[1] == (10, 20, {"font-style": "italic"}) |
| 906 |
|
| 907 |
def test_supports_paragraph_iteration_true(self, test_context: OrcaTestContext) -> None: |
| 908 |
"""Test AXText.supports_paragraph_iteration returns True.""" |
| 909 |
|
| 910 |
self._setup_dependencies(test_context) |
| 911 |
from orca.ax_text import AXText |
| 912 |
from orca.ax_utilities_text import AXUtilitiesText |
| 913 |
|
| 914 |
test_context.patch_object( |
| 915 |
AXText, |
| 916 |
"get_paragraph_at_offset", |
| 917 |
return_value=("This is a paragraph.", 0, 20), |
| 918 |
) |
| 919 |
result = AXUtilitiesText.supports_paragraph_iteration( |
| 920 |
test_context.Mock(spec=Atspi.Accessible) |
| 921 |
) |
| 922 |
assert result is True |
| 923 |
|
| 924 |
def test_iter_character_with_valid_text(self, test_context: OrcaTestContext) -> None: |
| 925 |
"""Test AXText.iter_character with valid text.""" |
| 926 |
|
| 927 |
self._setup_dependencies(test_context) |
| 928 |
from orca.ax_text import AXText |
| 929 |
from orca.ax_utilities_text import AXUtilitiesText |
| 930 |
|
| 931 |
test_context.patch_object(AXText, "get_caret_offset", return_value=0) |
| 932 |
test_context.patch_object(AXText, "get_character_count", return_value=3) |
| 933 |
|
| 934 |
def mock_get_character_at_offset(_obj, offset) -> tuple[str, int, int]: |
| 935 |
chars = [("a", 0, 1), ("b", 1, 2), ("c", 2, 3)] |
| 936 |
if 0 <= offset < 3: |
| 937 |
return chars[offset] |
| 938 |
return ("", 0, 0) |
| 939 |
|
| 940 |
test_context.patch_object( |
| 941 |
AXText, |
| 942 |
"get_character_at_offset", |
| 943 |
new=mock_get_character_at_offset, |
| 944 |
) |
| 945 |
result = list(AXUtilitiesText.iter_character(test_context.Mock(spec=Atspi.Accessible))) |
| 946 |
assert result == [("a", 0, 1), ("b", 1, 2), ("c", 2, 3)] |
| 947 |
|
| 948 |
def test_iter_character_with_empty_text(self, test_context: OrcaTestContext) -> None: |
| 949 |
"""Test AXText.iter_character with empty text.""" |
| 950 |
|
| 951 |
self._setup_dependencies(test_context) |
| 952 |
from orca.ax_text import AXText |
| 953 |
from orca.ax_utilities_text import AXUtilitiesText |
| 954 |
|
| 955 |
test_context.patch_object(AXText, "get_caret_offset", return_value=0) |
| 956 |
test_context.patch_object(AXText, "get_character_count", return_value=0) |
| 957 |
result = list(AXUtilitiesText.iter_character(test_context.Mock(spec=Atspi.Accessible))) |
| 958 |
assert not result |
| 959 |
|
| 960 |
def test_iter_word_with_valid_text(self, test_context: OrcaTestContext) -> None: |
| 961 |
"""Test AXText.iter_word with valid text.""" |
| 962 |
|
| 963 |
self._setup_dependencies(test_context) |
| 964 |
from orca.ax_text import AXText |
| 965 |
from orca.ax_utilities_text import AXUtilitiesText |
| 966 |
|
| 967 |
test_context.patch_object(AXText, "get_caret_offset", return_value=0) |
| 968 |
test_context.patch_object(AXText, "get_character_count", return_value=11) |
| 969 |
|
| 970 |
def mock_get_word_at_offset(_obj, offset) -> tuple[str, int, int]: |
| 971 |
if offset is None: |
| 972 |
offset = 0 |
| 973 |
if 0 <= offset < 5: |
| 974 |
return ("hello", 0, 5) |
| 975 |
if 5 <= offset < 11: |
| 976 |
return (" world", 5, 11) |
| 977 |
return ("", 0, 0) |
| 978 |
|
| 979 |
test_context.patch_object(AXText, "get_word_at_offset", new=mock_get_word_at_offset) |
| 980 |
result = list(AXUtilitiesText.iter_word(test_context.Mock(spec=Atspi.Accessible))) |
| 981 |
assert result == [("hello", 0, 5), (" world", 5, 11)] |
| 982 |
|
| 983 |
def test_iter_line_with_valid_text(self, test_context: OrcaTestContext) -> None: |
| 984 |
"""Test AXText.iter_line with valid text.""" |
| 985 |
|
| 986 |
self._setup_dependencies(test_context) |
| 987 |
from orca.ax_text import AXText |
| 988 |
from orca.ax_utilities_text import AXUtilitiesText |
| 989 |
|
| 990 |
test_context.patch_object(AXText, "get_caret_offset", return_value=0) |
| 991 |
test_context.patch_object(AXText, "get_character_count", return_value=20) |
| 992 |
|
| 993 |
def mock_get_line_at_offset(_obj, offset) -> tuple[str, int, int]: |
| 994 |
if offset is None: |
| 995 |
offset = 0 |
| 996 |
if 0 <= offset < 10: |
| 997 |
return ("First line", 0, 10) |
| 998 |
if 10 <= offset < 20: |
| 999 |
return ("Second line", 10, 20) |
| 1000 |
return ("", 0, 0) |
| 1001 |
|
| 1002 |
test_context.patch_object(AXText, "get_line_at_offset", new=mock_get_line_at_offset) |
| 1003 |
result = list(AXUtilitiesText.iter_line(test_context.Mock(spec=Atspi.Accessible))) |
| 1004 |
assert result == [("First line", 0, 10), ("Second line", 10, 20)] |
| 1005 |
|
| 1006 |
def test_iter_line_prevents_infinite_loop_when_get_next_line_returns_same_position( |
| 1007 |
self, |
| 1008 |
test_context: OrcaTestContext, |
| 1009 |
) -> None: |
| 1010 |
"""Test that iter_line prevents infinite loops when get_next_line doesn't advance.""" |
| 1011 |
|
| 1012 |
self._setup_dependencies(test_context) |
| 1013 |
from orca.ax_text import AXText |
| 1014 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1015 |
|
| 1016 |
test_context.patch_object(AXText, "get_caret_offset", return_value=0) |
| 1017 |
|
| 1018 |
def mock_get_line_at_offset(_obj, offset) -> tuple[str, int, int]: |
| 1019 |
if offset is None: |
| 1020 |
offset = 0 |
| 1021 |
return ("Test line", 0, 10) |
| 1022 |
|
| 1023 |
def mock_get_next_line(_obj, _offset) -> tuple[str, int, int]: |
| 1024 |
return ("Next line", 0, 10) |
| 1025 |
|
| 1026 |
test_context.patch_object(AXText, "get_line_at_offset", new=mock_get_line_at_offset) |
| 1027 |
test_context.patch_object(AXUtilitiesText, "get_next_line", new=mock_get_next_line) |
| 1028 |
|
| 1029 |
result = list(AXUtilitiesText.iter_line(test_context.Mock(spec=Atspi.Accessible))) |
| 1030 |
assert result == [("Test line", 0, 10)] |
| 1031 |
|
| 1032 |
def test_iter_line_skips_duplicate_when_offset_at_end( |
| 1033 |
self, |
| 1034 |
test_context: OrcaTestContext, |
| 1035 |
) -> None: |
| 1036 |
"""Ensure iter_line doesn't yield the same line when starting at its end offset.""" |
| 1037 |
|
| 1038 |
self._setup_dependencies(test_context) |
| 1039 |
from orca.ax_text import AXText |
| 1040 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1041 |
|
| 1042 |
# get_line_at_offset returns the same first line even when called at end |
| 1043 |
test_context.patch_object( |
| 1044 |
AXText, |
| 1045 |
"get_line_at_offset", |
| 1046 |
new=lambda _obj, _offset: ("First line", 0, 10), |
| 1047 |
) |
| 1048 |
|
| 1049 |
# get_next_line advances to a distinct second line only once |
| 1050 |
def mock_get_next_line(_obj, offset) -> tuple[str, int, int]: |
| 1051 |
if offset == 0: |
| 1052 |
return ("Second line", 10, 20) |
| 1053 |
return ("", 0, 0) |
| 1054 |
|
| 1055 |
test_context.patch_object(AXUtilitiesText, "get_next_line", new=mock_get_next_line) |
| 1056 |
|
| 1057 |
result = list(AXUtilitiesText.iter_line(test_context.Mock(spec=Atspi.Accessible), 10)) |
| 1058 |
assert result == [("Second line", 10, 20)] |
| 1059 |
|
| 1060 |
def test_iter_sentence_prevents_infinite_loop_when_get_next_sentence_returns_same_position( |
| 1061 |
self, |
| 1062 |
test_context: OrcaTestContext, |
| 1063 |
) -> None: |
| 1064 |
"""Test that iter_sentence prevents infinite loops if get_next_sentence doesn't advance.""" |
| 1065 |
|
| 1066 |
self._setup_dependencies(test_context) |
| 1067 |
from orca.ax_text import AXText |
| 1068 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1069 |
|
| 1070 |
test_context.patch_object(AXText, "get_caret_offset", return_value=0) |
| 1071 |
|
| 1072 |
def mock_get_sentence_at_offset(_obj, offset) -> tuple[str, int, int]: |
| 1073 |
if offset is None: |
| 1074 |
offset = 0 |
| 1075 |
return ("Test sentence", 0, 15) |
| 1076 |
|
| 1077 |
def mock_get_next_sentence(_obj, _offset) -> tuple[str, int, int]: |
| 1078 |
return ("Next sentence", 0, 15) |
| 1079 |
|
| 1080 |
test_context.patch_object(AXText, "get_sentence_at_offset", new=mock_get_sentence_at_offset) |
| 1081 |
test_context.patch_object(AXUtilitiesText, "get_next_sentence", new=mock_get_next_sentence) |
| 1082 |
|
| 1083 |
result = list(AXUtilitiesText.iter_sentence(test_context.Mock(spec=Atspi.Accessible))) |
| 1084 |
assert result == [("Test sentence", 0, 15)] |
| 1085 |
|
| 1086 |
def test_iter_sentence_skips_duplicate_when_offset_at_end( |
| 1087 |
self, |
| 1088 |
test_context: OrcaTestContext, |
| 1089 |
) -> None: |
| 1090 |
"""Ensure iter_sentence doesn't yield the same sentence when starting at its end.""" |
| 1091 |
|
| 1092 |
self._setup_dependencies(test_context) |
| 1093 |
from orca.ax_text import AXText |
| 1094 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1095 |
|
| 1096 |
# get_sentence_at_offset returns the same first sentence even at its end |
| 1097 |
test_context.patch_object( |
| 1098 |
AXText, |
| 1099 |
"get_sentence_at_offset", |
| 1100 |
new=lambda _obj, _offset: ("First sentence.", 0, 12), |
| 1101 |
) |
| 1102 |
|
| 1103 |
# get_next_sentence advances to a distinct second sentence only once |
| 1104 |
def mock_get_next_sentence(_obj, offset) -> tuple[str, int, int]: |
| 1105 |
if offset == 0: |
| 1106 |
return ("Second sentence.", 12, 25) |
| 1107 |
return ("", 0, 0) |
| 1108 |
|
| 1109 |
test_context.patch_object(AXUtilitiesText, "get_next_sentence", new=mock_get_next_sentence) |
| 1110 |
|
| 1111 |
result = list(AXUtilitiesText.iter_sentence(test_context.Mock(spec=Atspi.Accessible), 12)) |
| 1112 |
assert result == [("Second sentence.", 12, 25)] |
| 1113 |
|
| 1114 |
def test_iter_sentence_with_valid_text(self, test_context: OrcaTestContext) -> None: |
| 1115 |
"""Test AXText.iter_sentence with valid text.""" |
| 1116 |
|
| 1117 |
self._setup_dependencies(test_context) |
| 1118 |
from orca.ax_text import AXText |
| 1119 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1120 |
|
| 1121 |
test_context.patch_object(AXText, "get_caret_offset", return_value=0) |
| 1122 |
test_context.patch_object(AXText, "get_character_count", return_value=25) |
| 1123 |
|
| 1124 |
def mock_get_sentence_at_offset(_obj, offset) -> tuple[str, int, int]: |
| 1125 |
if offset is None: |
| 1126 |
offset = 0 |
| 1127 |
if 0 <= offset < 12: |
| 1128 |
return ("First sentence.", 0, 12) |
| 1129 |
if 12 <= offset < 25: |
| 1130 |
return ("Second sentence.", 12, 25) |
| 1131 |
return ("", 0, 0) |
| 1132 |
|
| 1133 |
test_context.patch_object(AXText, "get_sentence_at_offset", new=mock_get_sentence_at_offset) |
| 1134 |
result = list(AXUtilitiesText.iter_sentence(test_context.Mock(spec=Atspi.Accessible))) |
| 1135 |
assert result == [("First sentence.", 0, 12), ("Second sentence.", 12, 25)] |
| 1136 |
|
| 1137 |
def test_iter_paragraph_with_valid_text(self, test_context: OrcaTestContext) -> None: |
| 1138 |
"""Test AXText.iter_paragraph with valid text.""" |
| 1139 |
|
| 1140 |
self._setup_dependencies(test_context) |
| 1141 |
from orca.ax_text import AXText |
| 1142 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1143 |
|
| 1144 |
test_context.patch_object(AXText, "get_caret_offset", return_value=0) |
| 1145 |
test_context.patch_object(AXText, "get_character_count", return_value=30) |
| 1146 |
|
| 1147 |
def mock_get_paragraph_at_offset(_obj, offset) -> tuple[str, int, int]: |
| 1148 |
if offset is None: |
| 1149 |
offset = 0 |
| 1150 |
if 0 <= offset < 15: |
| 1151 |
return ("First paragraph.", 0, 15) |
| 1152 |
if 15 <= offset < 30: |
| 1153 |
return ("Second paragraph.", 15, 30) |
| 1154 |
return ("", 0, 0) |
| 1155 |
|
| 1156 |
test_context.patch_object( |
| 1157 |
AXText, |
| 1158 |
"get_paragraph_at_offset", |
| 1159 |
new=mock_get_paragraph_at_offset, |
| 1160 |
) |
| 1161 |
result = list(AXUtilitiesText.iter_paragraph(test_context.Mock(spec=Atspi.Accessible))) |
| 1162 |
assert result == [("First paragraph.", 0, 15), ("Second paragraph.", 15, 30)] |
| 1163 |
|
| 1164 |
@pytest.mark.parametrize( |
| 1165 |
"case", |
| 1166 |
[ |
| 1167 |
{ |
| 1168 |
"id": "offset_successful", |
| 1169 |
"method_type": "offset", |
| 1170 |
"offset_result": 25, |
| 1171 |
"char_count": 30, |
| 1172 |
"expected_result": 25, |
| 1173 |
"should_raise_error": False, |
| 1174 |
"debug_method": "print_tokens", |
| 1175 |
"x_coord": 100, |
| 1176 |
"y_coord": 200, |
| 1177 |
}, |
| 1178 |
{ |
| 1179 |
"id": "offset_glib_error", |
| 1180 |
"method_type": "offset", |
| 1181 |
"offset_result": -1, |
| 1182 |
"char_count": 30, |
| 1183 |
"expected_result": -1, |
| 1184 |
"should_raise_error": True, |
| 1185 |
"debug_method": "print_message", |
| 1186 |
"x_coord": 100, |
| 1187 |
"y_coord": 200, |
| 1188 |
}, |
| 1189 |
{ |
| 1190 |
"id": "character_valid", |
| 1191 |
"method_type": "character", |
| 1192 |
"offset_result": 5, |
| 1193 |
"char_count": 10, |
| 1194 |
"expected_result": ("a", 5, 6), |
| 1195 |
"should_raise_error": False, |
| 1196 |
"debug_method": None, |
| 1197 |
"x_coord": 100, |
| 1198 |
"y_coord": 200, |
| 1199 |
}, |
| 1200 |
{ |
| 1201 |
"id": "character_invalid_offset", |
| 1202 |
"method_type": "character", |
| 1203 |
"offset_result": -1, |
| 1204 |
"char_count": 10, |
| 1205 |
"expected_result": ("", 0, 0), |
| 1206 |
"should_raise_error": False, |
| 1207 |
"debug_method": None, |
| 1208 |
"x_coord": 100, |
| 1209 |
"y_coord": 200, |
| 1210 |
}, |
| 1211 |
{ |
| 1212 |
"id": "word_valid", |
| 1213 |
"method_type": "word", |
| 1214 |
"offset_result": 5, |
| 1215 |
"char_count": 10, |
| 1216 |
"expected_result": ("hello", 3, 8), |
| 1217 |
"should_raise_error": False, |
| 1218 |
"debug_method": None, |
| 1219 |
"x_coord": 100, |
| 1220 |
"y_coord": 200, |
| 1221 |
}, |
| 1222 |
{ |
| 1223 |
"id": "line_valid", |
| 1224 |
"method_type": "line", |
| 1225 |
"offset_result": 5, |
| 1226 |
"char_count": 20, |
| 1227 |
"expected_result": ("This is a line", 0, 14), |
| 1228 |
"should_raise_error": False, |
| 1229 |
"debug_method": None, |
| 1230 |
"x_coord": 100, |
| 1231 |
"y_coord": 200, |
| 1232 |
}, |
| 1233 |
{ |
| 1234 |
"id": "sentence_valid", |
| 1235 |
"method_type": "sentence", |
| 1236 |
"offset_result": 5, |
| 1237 |
"char_count": 20, |
| 1238 |
"expected_result": ("This is a sentence.", 0, 19), |
| 1239 |
"should_raise_error": False, |
| 1240 |
"debug_method": None, |
| 1241 |
"x_coord": 100, |
| 1242 |
"y_coord": 200, |
| 1243 |
}, |
| 1244 |
{ |
| 1245 |
"id": "paragraph_valid", |
| 1246 |
"method_type": "paragraph", |
| 1247 |
"offset_result": 5, |
| 1248 |
"char_count": 25, |
| 1249 |
"expected_result": ("This is a paragraph.", 0, 20), |
| 1250 |
"should_raise_error": False, |
| 1251 |
"debug_method": None, |
| 1252 |
"x_coord": 100, |
| 1253 |
"y_coord": 200, |
| 1254 |
}, |
| 1255 |
], |
| 1256 |
ids=lambda case: case["id"], |
| 1257 |
) |
| 1258 |
def test_get_at_point_methods_scenarios( # pylint: disable=too-many-locals |
| 1259 |
self, |
| 1260 |
test_context: OrcaTestContext, |
| 1261 |
case: dict, |
| 1262 |
) -> None: |
| 1263 |
"""Test AXText get_*_at_point methods with various scenarios.""" |
| 1264 |
|
| 1265 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 1266 |
from orca.ax_text import AXText |
| 1267 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1268 |
|
| 1269 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 1270 |
result: int | tuple[str, int, int] | None = None |
| 1271 |
|
| 1272 |
if case["method_type"] == "offset": |
| 1273 |
if case["should_raise_error"]: |
| 1274 |
|
| 1275 |
def raise_glib_error(_obj, _x, _y, _coord_type) -> None: |
| 1276 |
raise GLib.GError("Test error") |
| 1277 |
|
| 1278 |
test_context.patch_object(Atspi.Text, "get_offset_at_point", new=raise_glib_error) |
| 1279 |
else: |
| 1280 |
test_context.patch_object( |
| 1281 |
Atspi.Text, |
| 1282 |
"get_offset_at_point", |
| 1283 |
side_effect=lambda obj, x, y, coord_type: case["offset_result"], |
| 1284 |
) |
| 1285 |
essential_modules["orca.debug"].print_tokens = test_context.Mock() |
| 1286 |
essential_modules["orca.debug"].print_message = test_context.Mock() |
| 1287 |
result = AXText.get_offset_at_point(mock_obj, case["x_coord"], case["y_coord"]) |
| 1288 |
if case["debug_method"]: |
| 1289 |
getattr(essential_modules["orca.debug"], case["debug_method"]).assert_called() |
| 1290 |
else: |
| 1291 |
test_context.patch_object( |
| 1292 |
AXText, |
| 1293 |
"get_offset_at_point", |
| 1294 |
side_effect=lambda obj, x, y: case["offset_result"], |
| 1295 |
) |
| 1296 |
test_context.patch_object( |
| 1297 |
AXText, |
| 1298 |
"get_character_count", |
| 1299 |
side_effect=lambda obj: case["char_count"], |
| 1300 |
) |
| 1301 |
|
| 1302 |
if case["method_type"] == "character": |
| 1303 |
if case["offset_result"] == -1: |
| 1304 |
result = AXUtilitiesText.get_character_at_point( |
| 1305 |
mock_obj, |
| 1306 |
case["x_coord"], |
| 1307 |
case["y_coord"], |
| 1308 |
) |
| 1309 |
else: |
| 1310 |
test_context.patch_object( |
| 1311 |
AXText, |
| 1312 |
"get_character_at_offset", |
| 1313 |
side_effect=lambda obj, offset: case["expected_result"], |
| 1314 |
) |
| 1315 |
result = AXUtilitiesText.get_character_at_point( |
| 1316 |
mock_obj, |
| 1317 |
case["x_coord"], |
| 1318 |
case["y_coord"], |
| 1319 |
) |
| 1320 |
elif case["method_type"] == "word": |
| 1321 |
test_context.patch_object( |
| 1322 |
AXText, |
| 1323 |
"get_word_at_offset", |
| 1324 |
side_effect=lambda obj, offset: case["expected_result"], |
| 1325 |
) |
| 1326 |
result = AXUtilitiesText.get_word_at_point( |
| 1327 |
mock_obj, case["x_coord"], case["y_coord"] |
| 1328 |
) |
| 1329 |
elif case["method_type"] == "line": |
| 1330 |
test_context.patch_object( |
| 1331 |
AXText, |
| 1332 |
"get_line_at_offset", |
| 1333 |
side_effect=lambda obj, offset: case["expected_result"], |
| 1334 |
) |
| 1335 |
result = AXUtilitiesText.get_line_at_point( |
| 1336 |
mock_obj, case["x_coord"], case["y_coord"] |
| 1337 |
) |
| 1338 |
elif case["method_type"] == "sentence": |
| 1339 |
test_context.patch_object( |
| 1340 |
AXText, |
| 1341 |
"get_sentence_at_offset", |
| 1342 |
side_effect=lambda obj, offset: case["expected_result"], |
| 1343 |
) |
| 1344 |
result = AXUtilitiesText.get_sentence_at_point( |
| 1345 |
mock_obj, case["x_coord"], case["y_coord"] |
| 1346 |
) |
| 1347 |
elif case["method_type"] == "paragraph": |
| 1348 |
test_context.patch_object( |
| 1349 |
AXText, |
| 1350 |
"get_paragraph_at_offset", |
| 1351 |
side_effect=lambda obj, offset: case["expected_result"], |
| 1352 |
) |
| 1353 |
result = AXUtilitiesText.get_paragraph_at_point( |
| 1354 |
mock_obj, case["x_coord"], case["y_coord"] |
| 1355 |
) |
| 1356 |
|
| 1357 |
assert result == case["expected_result"] |
| 1358 |
|
| 1359 |
def test_string_has_spelling_error_with_invalid_spelling( |
| 1360 |
self, |
| 1361 |
test_context: OrcaTestContext, |
| 1362 |
) -> None: |
| 1363 |
"""Test AXText.string_has_spelling_error with spelling error.""" |
| 1364 |
|
| 1365 |
self._setup_dependencies(test_context) |
| 1366 |
from orca.ax_text import AXText |
| 1367 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1368 |
|
| 1369 |
test_context.patch_object( |
| 1370 |
AXText, |
| 1371 |
"get_text_attributes_at_offset", |
| 1372 |
return_value=({"invalid": "spelling"}, 0, 10), |
| 1373 |
) |
| 1374 |
result = AXUtilitiesText.string_has_spelling_error( |
| 1375 |
test_context.Mock(spec=Atspi.Accessible), 5 |
| 1376 |
) |
| 1377 |
assert result is True |
| 1378 |
|
| 1379 |
def test_string_has_spelling_error_with_text_spelling( |
| 1380 |
self, |
| 1381 |
test_context: OrcaTestContext, |
| 1382 |
) -> None: |
| 1383 |
"""Test AXText.string_has_spelling_error with text-spelling misspelled.""" |
| 1384 |
|
| 1385 |
self._setup_dependencies(test_context) |
| 1386 |
from orca.ax_text import AXText |
| 1387 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1388 |
|
| 1389 |
test_context.patch_object( |
| 1390 |
AXText, |
| 1391 |
"get_text_attributes_at_offset", |
| 1392 |
return_value=({"text-spelling": "misspelled"}, 0, 10), |
| 1393 |
) |
| 1394 |
result = AXUtilitiesText.string_has_spelling_error( |
| 1395 |
test_context.Mock(spec=Atspi.Accessible), 5 |
| 1396 |
) |
| 1397 |
assert result is True |
| 1398 |
|
| 1399 |
def test_string_has_spelling_error_with_underline_error( |
| 1400 |
self, |
| 1401 |
test_context: OrcaTestContext, |
| 1402 |
) -> None: |
| 1403 |
"""Test AXText.string_has_spelling_error with underline error.""" |
| 1404 |
|
| 1405 |
self._setup_dependencies(test_context) |
| 1406 |
from orca.ax_text import AXText |
| 1407 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1408 |
|
| 1409 |
test_context.patch_object( |
| 1410 |
AXText, |
| 1411 |
"get_text_attributes_at_offset", |
| 1412 |
return_value=({"underline": "spelling"}, 0, 10), |
| 1413 |
) |
| 1414 |
result = AXUtilitiesText.string_has_spelling_error( |
| 1415 |
test_context.Mock(spec=Atspi.Accessible), 5 |
| 1416 |
) |
| 1417 |
assert result is True |
| 1418 |
|
| 1419 |
def test_string_has_spelling_error_without_error(self, test_context: OrcaTestContext) -> None: |
| 1420 |
"""Test AXText.string_has_spelling_error without spelling error.""" |
| 1421 |
|
| 1422 |
self._setup_dependencies(test_context) |
| 1423 |
from orca.ax_text import AXText |
| 1424 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1425 |
|
| 1426 |
test_context.patch_object( |
| 1427 |
AXText, |
| 1428 |
"get_text_attributes_at_offset", |
| 1429 |
return_value=({"font-size": "12pt"}, 0, 10), |
| 1430 |
) |
| 1431 |
result = AXUtilitiesText.string_has_spelling_error( |
| 1432 |
test_context.Mock(spec=Atspi.Accessible), 5 |
| 1433 |
) |
| 1434 |
assert result is False |
| 1435 |
|
| 1436 |
def test_string_has_grammar_error_with_invalid_grammar( |
| 1437 |
self, |
| 1438 |
test_context: OrcaTestContext, |
| 1439 |
) -> None: |
| 1440 |
"""Test AXText.string_has_grammar_error with grammar error.""" |
| 1441 |
|
| 1442 |
self._setup_dependencies(test_context) |
| 1443 |
from orca.ax_text import AXText |
| 1444 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1445 |
|
| 1446 |
test_context.patch_object( |
| 1447 |
AXText, |
| 1448 |
"get_text_attributes_at_offset", |
| 1449 |
return_value=({"invalid": "grammar"}, 0, 10), |
| 1450 |
) |
| 1451 |
result = AXUtilitiesText.string_has_grammar_error( |
| 1452 |
test_context.Mock(spec=Atspi.Accessible), 5 |
| 1453 |
) |
| 1454 |
assert result is True |
| 1455 |
|
| 1456 |
def test_string_has_grammar_error_with_underline_grammar( |
| 1457 |
self, |
| 1458 |
test_context: OrcaTestContext, |
| 1459 |
) -> None: |
| 1460 |
"""Test AXText.string_has_grammar_error with underline grammar.""" |
| 1461 |
|
| 1462 |
self._setup_dependencies(test_context) |
| 1463 |
from orca.ax_text import AXText |
| 1464 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1465 |
|
| 1466 |
test_context.patch_object( |
| 1467 |
AXText, |
| 1468 |
"get_text_attributes_at_offset", |
| 1469 |
return_value=({"underline": "grammar"}, 0, 10), |
| 1470 |
) |
| 1471 |
result = AXUtilitiesText.string_has_grammar_error( |
| 1472 |
test_context.Mock(spec=Atspi.Accessible), 5 |
| 1473 |
) |
| 1474 |
assert result is True |
| 1475 |
|
| 1476 |
def test_string_has_grammar_error_without_error(self, test_context: OrcaTestContext) -> None: |
| 1477 |
"""Test AXText.string_has_grammar_error without grammar error.""" |
| 1478 |
|
| 1479 |
self._setup_dependencies(test_context) |
| 1480 |
from orca.ax_text import AXText |
| 1481 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1482 |
|
| 1483 |
test_context.patch_object( |
| 1484 |
AXText, |
| 1485 |
"get_text_attributes_at_offset", |
| 1486 |
return_value=({"font-size": "12pt"}, 0, 10), |
| 1487 |
) |
| 1488 |
result = AXUtilitiesText.string_has_grammar_error( |
| 1489 |
test_context.Mock(spec=Atspi.Accessible), 5 |
| 1490 |
) |
| 1491 |
assert result is False |
| 1492 |
|
| 1493 |
@pytest.mark.parametrize( |
| 1494 |
"case", |
| 1495 |
[ |
| 1496 |
{ |
| 1497 |
"id": "start_with_selection", |
| 1498 |
"method_name": "get_selection_start_offset", |
| 1499 |
"has_selection": True, |
| 1500 |
"expected_result": 5, |
| 1501 |
}, |
| 1502 |
{ |
| 1503 |
"id": "start_without_selection", |
| 1504 |
"method_name": "get_selection_start_offset", |
| 1505 |
"has_selection": False, |
| 1506 |
"expected_result": -1, |
| 1507 |
}, |
| 1508 |
{ |
| 1509 |
"id": "end_with_selection", |
| 1510 |
"method_name": "get_selection_end_offset", |
| 1511 |
"has_selection": True, |
| 1512 |
"expected_result": 20, |
| 1513 |
}, |
| 1514 |
{ |
| 1515 |
"id": "end_without_selection", |
| 1516 |
"method_name": "get_selection_end_offset", |
| 1517 |
"has_selection": False, |
| 1518 |
"expected_result": -1, |
| 1519 |
}, |
| 1520 |
], |
| 1521 |
ids=lambda case: case["id"], |
| 1522 |
) |
| 1523 |
def test_selection_offset_methods( |
| 1524 |
self, |
| 1525 |
test_context: OrcaTestContext, |
| 1526 |
case: dict, |
| 1527 |
) -> None: |
| 1528 |
"""Test AXText selection offset methods with and without selection.""" |
| 1529 |
|
| 1530 |
self._setup_dependencies(test_context) |
| 1531 |
from orca.ax_text import AXText |
| 1532 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1533 |
|
| 1534 |
selected_ranges = [(5, 10), (15, 20)] if case["has_selection"] else [] |
| 1535 |
test_context.patch_object(AXText, "get_selected_ranges", return_value=selected_ranges) |
| 1536 |
|
| 1537 |
method = getattr(AXUtilitiesText, case["method_name"]) |
| 1538 |
result = method(test_context.Mock(spec=Atspi.Accessible)) |
| 1539 |
assert result == case["expected_result"] |
| 1540 |
|
| 1541 |
def test_is_all_text_selected_with_full_selection(self, test_context: OrcaTestContext) -> None: |
| 1542 |
"""Test AXText.is_all_text_selected with full text selected.""" |
| 1543 |
|
| 1544 |
self._setup_dependencies(test_context) |
| 1545 |
from orca.ax_text import AXText |
| 1546 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1547 |
|
| 1548 |
test_context.patch_object(AXText, "get_character_count", return_value=20) |
| 1549 |
test_context.patch_object(AXText, "get_selected_ranges", return_value=[(0, 20)]) |
| 1550 |
result = AXUtilitiesText.is_all_text_selected(test_context.Mock(spec=Atspi.Accessible)) |
| 1551 |
assert result is True |
| 1552 |
|
| 1553 |
def test_is_all_text_selected_with_partial_selection( |
| 1554 |
self, |
| 1555 |
test_context: OrcaTestContext, |
| 1556 |
) -> None: |
| 1557 |
"""Test AXText.is_all_text_selected with partial selection.""" |
| 1558 |
|
| 1559 |
self._setup_dependencies(test_context) |
| 1560 |
from orca.ax_text import AXText |
| 1561 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1562 |
|
| 1563 |
test_context.patch_object(AXText, "get_character_count", return_value=20) |
| 1564 |
test_context.patch_object(AXText, "get_selected_ranges", return_value=[(5, 15)]) |
| 1565 |
result = AXUtilitiesText.is_all_text_selected(test_context.Mock(spec=Atspi.Accessible)) |
| 1566 |
assert result is False |
| 1567 |
|
| 1568 |
def test_is_all_text_selected_without_selection(self, test_context: OrcaTestContext) -> None: |
| 1569 |
"""Test AXText.is_all_text_selected without selection.""" |
| 1570 |
|
| 1571 |
self._setup_dependencies(test_context) |
| 1572 |
from orca.ax_text import AXText |
| 1573 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1574 |
|
| 1575 |
test_context.patch_object(AXText, "get_character_count", return_value=20) |
| 1576 |
test_context.patch_object(AXText, "get_selected_ranges", return_value=[]) |
| 1577 |
result = AXUtilitiesText.is_all_text_selected(test_context.Mock(spec=Atspi.Accessible)) |
| 1578 |
assert result is False |
| 1579 |
|
| 1580 |
def test_clear_all_selected_text(self, test_context: OrcaTestContext) -> None: |
| 1581 |
"""Test AXText.clear_all_selected_text.""" |
| 1582 |
|
| 1583 |
self._setup_dependencies(test_context) |
| 1584 |
from orca.ax_text import AXText |
| 1585 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1586 |
|
| 1587 |
removed_selections = [] |
| 1588 |
|
| 1589 |
def mock_remove_selection(_obj, selection_number) -> None: |
| 1590 |
removed_selections.append(selection_number) |
| 1591 |
|
| 1592 |
test_context.patch_object(AXText, "get_n_selections", return_value=3) |
| 1593 |
test_context.patch_object(AXText, "remove_selection", new=mock_remove_selection) |
| 1594 |
AXUtilitiesText.clear_all_selected_text(test_context.Mock(spec=Atspi.Accessible)) |
| 1595 |
assert removed_selections == [0, 1, 2] |
| 1596 |
|
| 1597 |
def test_remove_selection(self, test_context: OrcaTestContext) -> None: |
| 1598 |
"""Test AXText.remove_selection.""" |
| 1599 |
|
| 1600 |
self._setup_dependencies(test_context) |
| 1601 |
from orca.ax_object import AXObject |
| 1602 |
from orca.ax_text import AXText |
| 1603 |
|
| 1604 |
test_context.patch_object(AXObject, "supports_text", return_value=True) |
| 1605 |
mock_remove_selection = test_context.Mock() |
| 1606 |
test_context.patch("gi.repository.Atspi.Text.remove_selection", new=mock_remove_selection) |
| 1607 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1608 |
AXText.remove_selection(mock_accessible, 0) |
| 1609 |
mock_remove_selection.assert_called_once_with(mock_accessible, 0) |
| 1610 |
|
| 1611 |
def test_remove_selection_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 1612 |
"""Test AXText.remove_selection handles GLib.GError.""" |
| 1613 |
|
| 1614 |
self._setup_dependencies(test_context) |
| 1615 |
from orca.ax_object import AXObject |
| 1616 |
from orca.ax_text import AXText |
| 1617 |
|
| 1618 |
test_context.patch_object(AXObject, "supports_text", return_value=True) |
| 1619 |
|
| 1620 |
def mock_remove_selection(obj, selection_number) -> None: |
| 1621 |
raise GLib.GError("Test error") |
| 1622 |
|
| 1623 |
test_context.patch("gi.repository.Atspi.Text.remove_selection", new=mock_remove_selection) |
| 1624 |
|
| 1625 |
AXText.remove_selection(test_context.Mock(spec=Atspi.Accessible), 0) |
| 1626 |
|
| 1627 |
def test_get_selected_ranges_with_valid_selections(self, test_context: OrcaTestContext) -> None: |
| 1628 |
"""Test AXText.get_selected_ranges with valid selections.""" |
| 1629 |
|
| 1630 |
self._setup_dependencies(test_context) |
| 1631 |
from orca.ax_text import AXText |
| 1632 |
|
| 1633 |
test_context.patch_object(AXText, "get_n_selections", return_value=2) |
| 1634 |
|
| 1635 |
def mock_get_selection(_obj, selection_num) -> None: |
| 1636 |
results = [ |
| 1637 |
test_context.Mock(start_offset=5, end_offset=10), |
| 1638 |
test_context.Mock(start_offset=15, end_offset=20), |
| 1639 |
] |
| 1640 |
return results[selection_num] |
| 1641 |
|
| 1642 |
test_context.patch("gi.repository.Atspi.Text.get_selection", new=mock_get_selection) |
| 1643 |
result = AXText.get_selected_ranges(test_context.Mock(spec=Atspi.Accessible)) |
| 1644 |
assert result == [(5, 10), (15, 20)] |
| 1645 |
|
| 1646 |
def test_get_selected_ranges_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 1647 |
"""Test AXText.get_selected_ranges handles GLib.GError.""" |
| 1648 |
|
| 1649 |
self._setup_dependencies(test_context) |
| 1650 |
from orca.ax_text import AXText |
| 1651 |
|
| 1652 |
test_context.patch_object(AXText, "get_n_selections", return_value=1) |
| 1653 |
|
| 1654 |
def mock_get_selection(obj, selection_num) -> None: |
| 1655 |
raise GLib.GError("Test error") |
| 1656 |
|
| 1657 |
test_context.patch("gi.repository.Atspi.Text.get_selection", new=mock_get_selection) |
| 1658 |
result = AXText.get_selected_ranges(test_context.Mock(spec=Atspi.Accessible)) |
| 1659 |
assert not result |
| 1660 |
|
| 1661 |
def test_add_new_selection_successful(self, test_context: OrcaTestContext) -> None: |
| 1662 |
"""Test AXText.add_new_selection successful case.""" |
| 1663 |
|
| 1664 |
self._setup_dependencies(test_context) |
| 1665 |
from orca.ax_object import AXObject |
| 1666 |
from orca.ax_text import AXText |
| 1667 |
|
| 1668 |
test_context.patch_object(AXObject, "supports_text", return_value=True) |
| 1669 |
test_context.patch("gi.repository.Atspi.Text.add_selection", return_value=True) |
| 1670 |
result = AXText.add_new_selection(test_context.Mock(spec=Atspi.Accessible), 5, 10) |
| 1671 |
assert result is True |
| 1672 |
|
| 1673 |
def test_add_new_selection_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 1674 |
"""Test AXText.add_new_selection handles GLib.GError.""" |
| 1675 |
|
| 1676 |
self._setup_dependencies(test_context) |
| 1677 |
from orca.ax_object import AXObject |
| 1678 |
from orca.ax_text import AXText |
| 1679 |
|
| 1680 |
test_context.patch_object(AXObject, "supports_text", return_value=True) |
| 1681 |
|
| 1682 |
def mock_add_selection(obj, start, end) -> None: |
| 1683 |
raise GLib.GError("Test error") |
| 1684 |
|
| 1685 |
test_context.patch("gi.repository.Atspi.Text.add_selection", new=mock_add_selection) |
| 1686 |
result = AXText.add_new_selection(test_context.Mock(spec=Atspi.Accessible), 5, 10) |
| 1687 |
assert result is False |
| 1688 |
|
| 1689 |
def test_update_existing_selection_successful(self, test_context: OrcaTestContext) -> None: |
| 1690 |
"""Test AXText.update_existing_selection successful case.""" |
| 1691 |
|
| 1692 |
self._setup_dependencies(test_context) |
| 1693 |
from orca.ax_object import AXObject |
| 1694 |
from orca.ax_text import AXText |
| 1695 |
|
| 1696 |
test_context.patch_object(AXObject, "supports_text", return_value=True) |
| 1697 |
test_context.patch("gi.repository.Atspi.Text.set_selection", return_value=True) |
| 1698 |
result = AXText.update_existing_selection( |
| 1699 |
test_context.Mock(spec=Atspi.Accessible), |
| 1700 |
5, |
| 1701 |
10, |
| 1702 |
0, |
| 1703 |
) |
| 1704 |
assert result is True |
| 1705 |
|
| 1706 |
def test_update_existing_selection_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 1707 |
"""Test AXText.update_existing_selection handles GLib.GError.""" |
| 1708 |
|
| 1709 |
self._setup_dependencies(test_context) |
| 1710 |
from orca.ax_object import AXObject |
| 1711 |
from orca.ax_text import AXText |
| 1712 |
|
| 1713 |
test_context.patch_object(AXObject, "supports_text", return_value=True) |
| 1714 |
|
| 1715 |
def mock_set_selection(obj, num, start, end) -> None: |
| 1716 |
raise GLib.GError("Test error") |
| 1717 |
|
| 1718 |
test_context.patch("gi.repository.Atspi.Text.set_selection", new=mock_set_selection) |
| 1719 |
result = AXText.update_existing_selection( |
| 1720 |
test_context.Mock(spec=Atspi.Accessible), |
| 1721 |
5, |
| 1722 |
10, |
| 1723 |
0, |
| 1724 |
) |
| 1725 |
assert result is False |
| 1726 |
|
| 1727 |
def test_set_selected_text_with_existing_selection(self, test_context: OrcaTestContext) -> None: |
| 1728 |
"""Test AXText.set_selected_text with existing selection.""" |
| 1729 |
|
| 1730 |
self._setup_dependencies(test_context) |
| 1731 |
from orca.ax_text import AXText |
| 1732 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1733 |
|
| 1734 |
test_context.patch_object(AXText, "get_n_selections", return_value=1) |
| 1735 |
test_context.patch_object(AXText, "update_existing_selection", return_value=True) |
| 1736 |
test_context.patch_object(AXText, "get_substring", return_value="test") |
| 1737 |
test_context.patch_object( |
| 1738 |
AXUtilitiesText, "get_selected_text", return_value=("test", 5, 10) |
| 1739 |
) |
| 1740 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 1741 |
essential_modules["orca.debug"].debugLevel = 0 |
| 1742 |
result = AXUtilitiesText.set_selected_text(test_context.Mock(spec=Atspi.Accessible), 5, 10) |
| 1743 |
assert result is True |
| 1744 |
|
| 1745 |
def test_set_selected_text_with_new_selection(self, test_context: OrcaTestContext) -> None: |
| 1746 |
"""Test AXText.set_selected_text with new selection.""" |
| 1747 |
|
| 1748 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 1749 |
from orca.ax_text import AXText |
| 1750 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1751 |
|
| 1752 |
test_context.patch_object(AXText, "get_n_selections", return_value=0) |
| 1753 |
test_context.patch_object(AXText, "add_new_selection", return_value=True) |
| 1754 |
test_context.patch_object(AXText, "get_substring", return_value="test") |
| 1755 |
test_context.patch_object( |
| 1756 |
AXUtilitiesText, "get_selected_text", return_value=("test", 5, 10) |
| 1757 |
) |
| 1758 |
essential_modules["orca.debug"].debugLevel = 0 |
| 1759 |
result = AXUtilitiesText.set_selected_text(test_context.Mock(spec=Atspi.Accessible), 5, 10) |
| 1760 |
assert result is True |
| 1761 |
|
| 1762 |
def test_update_cached_selected_text(self, test_context: OrcaTestContext) -> None: |
| 1763 |
"""Test AXText.update_cached_selected_text.""" |
| 1764 |
|
| 1765 |
self._setup_dependencies(test_context) |
| 1766 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1767 |
|
| 1768 |
test_context.patch_object( |
| 1769 |
AXUtilitiesText, "get_selected_text", return_value=("cached text", 5, 15) |
| 1770 |
) |
| 1771 |
AXUtilitiesText.CACHED_TEXT_SELECTION.clear() |
| 1772 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1773 |
AXUtilitiesText.update_cached_selected_text(mock_accessible) |
| 1774 |
mock_hash = hash(mock_accessible) |
| 1775 |
assert mock_hash in AXUtilitiesText.CACHED_TEXT_SELECTION |
| 1776 |
assert AXUtilitiesText.CACHED_TEXT_SELECTION[mock_hash] == ("cached text", 5, 15) |
| 1777 |
|
| 1778 |
def test_get_character_rect_successful(self, test_context: OrcaTestContext) -> None: |
| 1779 |
"""Test AXText.get_character_rect successful case.""" |
| 1780 |
|
| 1781 |
self._setup_dependencies(test_context) |
| 1782 |
from orca.ax_object import AXObject |
| 1783 |
from orca.ax_text import AXText |
| 1784 |
|
| 1785 |
test_context.patch_object(AXObject, "supports_text", return_value=True) |
| 1786 |
test_context.patch_object(AXText, "get_caret_offset", return_value=5) |
| 1787 |
mock_rect = test_context.Mock(x=10, y=20, width=5, height=12) |
| 1788 |
test_context.patch( |
| 1789 |
"gi.repository.Atspi.Text.get_character_extents", |
| 1790 |
side_effect=lambda obj, offset, coord_type: mock_rect, |
| 1791 |
) |
| 1792 |
result = AXText.get_character_rect(test_context.Mock(spec=Atspi.Accessible), 5) |
| 1793 |
assert result == mock_rect |
| 1794 |
|
| 1795 |
def test_get_character_rect_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 1796 |
"""Test AXText.get_character_rect handles GLib.GError.""" |
| 1797 |
|
| 1798 |
self._setup_dependencies(test_context) |
| 1799 |
from orca.ax_object import AXObject |
| 1800 |
from orca.ax_text import AXText |
| 1801 |
|
| 1802 |
test_context.patch_object(AXObject, "supports_text", return_value=True) |
| 1803 |
test_context.patch_object(AXText, "get_caret_offset", return_value=5) |
| 1804 |
|
| 1805 |
def mock_get_character_extents(obj, offset, coord_type) -> None: |
| 1806 |
raise GLib.GError("Test error") |
| 1807 |
|
| 1808 |
test_context.patch( |
| 1809 |
"gi.repository.Atspi.Text.get_character_extents", |
| 1810 |
new=mock_get_character_extents, |
| 1811 |
) |
| 1812 |
result = AXText.get_character_rect(test_context.Mock(spec=Atspi.Accessible), 5) |
| 1813 |
assert result.x == 0 and result.y == 0 |
| 1814 |
|
| 1815 |
def test_get_range_rect_successful(self, test_context: OrcaTestContext) -> None: |
| 1816 |
"""Test AXText.get_range_rect successful case.""" |
| 1817 |
|
| 1818 |
self._setup_dependencies(test_context) |
| 1819 |
from orca.ax_object import AXObject |
| 1820 |
from orca.ax_text import AXText |
| 1821 |
|
| 1822 |
test_context.patch_object(AXObject, "supports_text", return_value=True) |
| 1823 |
mock_rect = test_context.Mock(x=10, y=20, width=50, height=12) |
| 1824 |
test_context.patch( |
| 1825 |
"gi.repository.Atspi.Text.get_range_extents", |
| 1826 |
side_effect=lambda obj, start, end, coord_type: mock_rect, |
| 1827 |
) |
| 1828 |
result = AXText.get_range_rect(test_context.Mock(spec=Atspi.Accessible), 5, 15) |
| 1829 |
assert result == mock_rect |
| 1830 |
|
| 1831 |
def test_get_range_rect_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 1832 |
"""Test AXText.get_range_rect handles GLib.GError.""" |
| 1833 |
|
| 1834 |
self._setup_dependencies(test_context) |
| 1835 |
from orca.ax_object import AXObject |
| 1836 |
from orca.ax_text import AXText |
| 1837 |
|
| 1838 |
test_context.patch_object(AXObject, "supports_text", return_value=True) |
| 1839 |
|
| 1840 |
def mock_get_range_extents(obj, start, end, coord_type) -> None: |
| 1841 |
raise GLib.GError("Test error") |
| 1842 |
|
| 1843 |
test_context.patch("gi.repository.Atspi.Text.get_range_extents", new=mock_get_range_extents) |
| 1844 |
result = AXText.get_range_rect(test_context.Mock(spec=Atspi.Accessible), 5, 15) |
| 1845 |
assert result.x == 0 and result.y == 0 |
| 1846 |
|
| 1847 |
def test_scroll_substring_to_point_successful(self, test_context: OrcaTestContext) -> None: |
| 1848 |
"""Test AXText.scroll_substring_to_point successful case.""" |
| 1849 |
|
| 1850 |
self._setup_dependencies(test_context) |
| 1851 |
from orca.ax_text import AXText |
| 1852 |
|
| 1853 |
test_context.patch_object(AXText, "get_character_count", return_value=20) |
| 1854 |
test_context.patch( |
| 1855 |
"gi.repository.Atspi.Text.scroll_substring_to_point", |
| 1856 |
return_value=True, |
| 1857 |
) |
| 1858 |
result = AXText.scroll_substring_to_point( |
| 1859 |
test_context.Mock(spec=Atspi.Accessible), |
| 1860 |
100, |
| 1861 |
200, |
| 1862 |
5, |
| 1863 |
15, |
| 1864 |
) |
| 1865 |
assert result is True |
| 1866 |
|
| 1867 |
def test_scroll_substring_to_point_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 1868 |
"""Test AXText.scroll_substring_to_point handles GLib.GError.""" |
| 1869 |
|
| 1870 |
self._setup_dependencies(test_context) |
| 1871 |
from orca.ax_text import AXText |
| 1872 |
|
| 1873 |
test_context.patch_object(AXText, "get_character_count", return_value=20) |
| 1874 |
|
| 1875 |
def mock_scroll_substring_to_point(obj, start, end, coord_type, x, y) -> None: |
| 1876 |
raise GLib.GError("Test error") |
| 1877 |
|
| 1878 |
test_context.patch( |
| 1879 |
"gi.repository.Atspi.Text.scroll_substring_to_point", |
| 1880 |
new=mock_scroll_substring_to_point, |
| 1881 |
) |
| 1882 |
result = AXText.scroll_substring_to_point( |
| 1883 |
test_context.Mock(spec=Atspi.Accessible), |
| 1884 |
100, |
| 1885 |
200, |
| 1886 |
5, |
| 1887 |
15, |
| 1888 |
) |
| 1889 |
assert result is False |
| 1890 |
|
| 1891 |
def test_scroll_substring_to_location_successful(self, test_context: OrcaTestContext) -> None: |
| 1892 |
"""Test AXText.scroll_substring_to_location successful case.""" |
| 1893 |
|
| 1894 |
self._setup_dependencies(test_context) |
| 1895 |
from orca.ax_text import AXText |
| 1896 |
|
| 1897 |
test_context.patch_object(AXText, "get_character_count", return_value=20) |
| 1898 |
mock_scroll_type = test_context.Mock() |
| 1899 |
|
| 1900 |
def mock_scroll_success(_obj, _start, _end, _location) -> bool: |
| 1901 |
return True |
| 1902 |
|
| 1903 |
test_context.patch("gi.repository.Atspi.Text.scroll_substring_to", new=mock_scroll_success) |
| 1904 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1905 |
result = AXText.scroll_substring_to_location(mock_accessible, mock_scroll_type, 5, 15) |
| 1906 |
assert result is True |
| 1907 |
|
| 1908 |
def test_scroll_substring_to_location_with_glib_error( |
| 1909 |
self, |
| 1910 |
test_context: OrcaTestContext, |
| 1911 |
) -> None: |
| 1912 |
"""Test AXText.scroll_substring_to_location handles GLib.GError.""" |
| 1913 |
|
| 1914 |
self._setup_dependencies(test_context) |
| 1915 |
from orca.ax_text import AXText |
| 1916 |
|
| 1917 |
test_context.patch_object(AXText, "get_character_count", return_value=20) |
| 1918 |
|
| 1919 |
def mock_scroll_substring_to(obj, start, end, location) -> None: |
| 1920 |
raise GLib.GError("Test error") |
| 1921 |
|
| 1922 |
mock_scroll_type = test_context.Mock() |
| 1923 |
scroll_attr = "gi.repository.Atspi.Text.scroll_substring_to" |
| 1924 |
test_context.patch(scroll_attr, new=mock_scroll_substring_to) |
| 1925 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 1926 |
result = AXText.scroll_substring_to_location(mock_accessible, mock_scroll_type, 5, 15) |
| 1927 |
assert result is False |
| 1928 |
|
| 1929 |
def test_get_visible_lines(self, test_context: OrcaTestContext) -> None: |
| 1930 |
"""Test AXText.get_visible_lines.""" |
| 1931 |
|
| 1932 |
self._setup_dependencies(test_context) |
| 1933 |
from orca.ax_text import AXText |
| 1934 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1935 |
|
| 1936 |
clip_rect = test_context.Mock(x=0, y=0, width=100, height=50) |
| 1937 |
test_context.patch_object( |
| 1938 |
AXUtilitiesText, |
| 1939 |
"find_first_visible_line", |
| 1940 |
return_value=("First line", 0, 10), |
| 1941 |
) |
| 1942 |
|
| 1943 |
def mock_iter_line(_obj, _offset) -> Generator[tuple[str, int, int], None, None]: |
| 1944 |
lines = [("Second line", 10, 20), ("Third line", 20, 30)] |
| 1945 |
yield from lines |
| 1946 |
|
| 1947 |
test_context.patch_object(AXUtilitiesText, "iter_line", new=mock_iter_line) |
| 1948 |
test_context.patch_object( |
| 1949 |
AXText, |
| 1950 |
"get_range_rect", |
| 1951 |
side_effect=lambda obj, start, end: test_context.Mock(x=0, y=10, width=50, height=10), |
| 1952 |
) |
| 1953 |
test_context.patch_object(AXUtilitiesText, "_line_comparison", return_value=0) |
| 1954 |
result = AXUtilitiesText.get_visible_lines( |
| 1955 |
test_context.Mock(spec=Atspi.Accessible), clip_rect |
| 1956 |
) |
| 1957 |
assert len(result) >= 1 |
| 1958 |
assert result[0] == ("First line", 0, 10) |
| 1959 |
|
| 1960 |
def test_find_first_visible_line_at_start(self, test_context: OrcaTestContext) -> None: |
| 1961 |
"""Test AXText.find_first_visible_line when first line is at start.""" |
| 1962 |
|
| 1963 |
self._setup_dependencies(test_context) |
| 1964 |
from orca.ax_text import AXText |
| 1965 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1966 |
|
| 1967 |
clip_rect = test_context.Mock(x=0, y=0, width=100, height=50) |
| 1968 |
test_context.patch_object(AXText, "get_character_count", return_value=100) |
| 1969 |
test_context.patch_object(AXText, "get_line_at_offset", return_value=("First line", 0, 10)) |
| 1970 |
result = AXUtilitiesText.find_first_visible_line( |
| 1971 |
test_context.Mock(spec=Atspi.Accessible), clip_rect |
| 1972 |
) |
| 1973 |
assert result == ("First line", 0, 10) |
| 1974 |
|
| 1975 |
def test_find_last_visible_line_at_end(self, test_context: OrcaTestContext) -> None: |
| 1976 |
"""Test AXText.find_last_visible_line when last line is at end.""" |
| 1977 |
|
| 1978 |
self._setup_dependencies(test_context) |
| 1979 |
from orca.ax_text import AXText |
| 1980 |
from orca.ax_utilities_text import AXUtilitiesText |
| 1981 |
|
| 1982 |
clip_rect = test_context.Mock(x=0, y=0, width=100, height=50) |
| 1983 |
test_context.patch_object(AXText, "get_character_count", return_value=100) |
| 1984 |
test_context.patch_object(AXText, "get_line_at_offset", return_value=("Last line", 90, 100)) |
| 1985 |
result = AXUtilitiesText.find_last_visible_line( |
| 1986 |
test_context.Mock(spec=Atspi.Accessible), clip_rect |
| 1987 |
) |
| 1988 |
assert result == ("Last line", 90, 100) |
| 1989 |
|
| 1990 |
@pytest.mark.parametrize( |
| 1991 |
"case", |
| 1992 |
[ |
| 1993 |
{ |
| 1994 |
"id": "successful", |
| 1995 |
"should_raise_error": False, |
| 1996 |
"expected_result": ("This is a line", 0, 14), |
| 1997 |
}, |
| 1998 |
{"id": "glib_error", "should_raise_error": True, "expected_result": ("", 0, 0)}, |
| 1999 |
], |
| 2000 |
ids=lambda case: case["id"], |
| 2001 |
) |
| 2002 |
def test_get_line_at_offset( |
| 2003 |
self, |
| 2004 |
test_context: OrcaTestContext, |
| 2005 |
case: dict, |
| 2006 |
) -> None: |
| 2007 |
"""Test AXText.get_line_at_offset successful case and GLib.GError handling.""" |
| 2008 |
|
| 2009 |
self._setup_dependencies(test_context) |
| 2010 |
from orca.ax_text import AXText |
| 2011 |
|
| 2012 |
test_context.patch_object(AXText, "get_character_count", return_value=20) |
| 2013 |
test_context.patch_object(AXText, "get_caret_offset", return_value=7) |
| 2014 |
|
| 2015 |
if case["should_raise_error"]: |
| 2016 |
|
| 2017 |
def mock_get_string_at_offset(_obj, _offset, _granularity) -> None: |
| 2018 |
raise GLib.GError("Test error") |
| 2019 |
|
| 2020 |
test_context.patch( |
| 2021 |
"gi.repository.Atspi.Text.get_string_at_offset", |
| 2022 |
new=mock_get_string_at_offset, |
| 2023 |
) |
| 2024 |
else: |
| 2025 |
mock_result = test_context.Mock() |
| 2026 |
mock_result.content = "This is a line" |
| 2027 |
mock_result.start_offset = 0 |
| 2028 |
mock_result.end_offset = 14 |
| 2029 |
test_context.patch( |
| 2030 |
"gi.repository.Atspi.Text.get_string_at_offset", |
| 2031 |
side_effect=lambda obj, offset, granularity: mock_result, |
| 2032 |
) |
| 2033 |
|
| 2034 |
result = AXText.get_line_at_offset(test_context.Mock(spec=Atspi.Accessible)) |
| 2035 |
assert result == case["expected_result"] |
| 2036 |
|
| 2037 |
def test_get_line_at_offset_chromium_fallback(self, test_context: OrcaTestContext) -> None: |
| 2038 |
"""Test AXText.get_line_at_offset with Chromium fallback for invalid result.""" |
| 2039 |
|
| 2040 |
self._setup_dependencies(test_context) |
| 2041 |
from orca.ax_text import AXText |
| 2042 |
|
| 2043 |
test_context.patch_object(AXText, "get_character_count", return_value=20) |
| 2044 |
test_context.patch_object(AXText, "get_caret_offset", return_value=20) |
| 2045 |
call_count = 0 |
| 2046 |
|
| 2047 |
def mock_get_string_at_offset(_obj, _offset, _granularity): |
| 2048 |
nonlocal call_count |
| 2049 |
call_count += 1 |
| 2050 |
mock_result = test_context.Mock() |
| 2051 |
if call_count == 1: |
| 2052 |
mock_result.content = "" |
| 2053 |
mock_result.start_offset = -1 |
| 2054 |
mock_result.end_offset = -1 |
| 2055 |
else: |
| 2056 |
mock_result.content = "Valid line" |
| 2057 |
mock_result.start_offset = 10 |
| 2058 |
mock_result.end_offset = 20 |
| 2059 |
return mock_result |
| 2060 |
|
| 2061 |
test_context.patch( |
| 2062 |
"gi.repository.Atspi.Text.get_string_at_offset", |
| 2063 |
new=mock_get_string_at_offset, |
| 2064 |
) |
| 2065 |
result = AXText.get_line_at_offset(test_context.Mock(spec=Atspi.Accessible), 20) |
| 2066 |
assert result == ("Valid line", 10, 20) |
| 2067 |
|
| 2068 |
@pytest.mark.parametrize( |
| 2069 |
"case", |
| 2070 |
[ |
| 2071 |
{ |
| 2072 |
"id": "successful", |
| 2073 |
"should_raise_error": False, |
| 2074 |
"expected_result": ("This is a sentence.", 0, 19), |
| 2075 |
}, |
| 2076 |
{"id": "glib_error", "should_raise_error": True, "expected_result": ("", 0, 0)}, |
| 2077 |
], |
| 2078 |
ids=lambda case: case["id"], |
| 2079 |
) |
| 2080 |
def test_get_sentence_at_offset( |
| 2081 |
self, |
| 2082 |
test_context: OrcaTestContext, |
| 2083 |
case: dict, |
| 2084 |
) -> None: |
| 2085 |
"""Test AXText.get_sentence_at_offset successful case and GLib.GError handling.""" |
| 2086 |
|
| 2087 |
self._setup_dependencies(test_context) |
| 2088 |
from orca.ax_text import AXText |
| 2089 |
|
| 2090 |
test_context.patch_object(AXText, "get_character_count", return_value=25) |
| 2091 |
test_context.patch_object(AXText, "get_caret_offset", return_value=7) |
| 2092 |
|
| 2093 |
if case["should_raise_error"]: |
| 2094 |
|
| 2095 |
def mock_get_string_at_offset(_obj, _offset, _granularity) -> None: |
| 2096 |
raise GLib.GError("Test error") |
| 2097 |
|
| 2098 |
test_context.patch( |
| 2099 |
"gi.repository.Atspi.Text.get_string_at_offset", |
| 2100 |
new=mock_get_string_at_offset, |
| 2101 |
) |
| 2102 |
test_context.patch("gi.repository.Atspi.Text.get_text", return_value="") |
| 2103 |
else: |
| 2104 |
mock_result = test_context.Mock() |
| 2105 |
mock_result.content = "This is a sentence." |
| 2106 |
mock_result.start_offset = 0 |
| 2107 |
mock_result.end_offset = 19 |
| 2108 |
test_context.patch( |
| 2109 |
"gi.repository.Atspi.Text.get_string_at_offset", |
| 2110 |
side_effect=lambda obj, offset, granularity: mock_result, |
| 2111 |
) |
| 2112 |
|
| 2113 |
result = AXText.get_sentence_at_offset(test_context.Mock(spec=Atspi.Accessible)) |
| 2114 |
assert result == case["expected_result"] |
| 2115 |
|
| 2116 |
@pytest.mark.parametrize( |
| 2117 |
"case", |
| 2118 |
[ |
| 2119 |
{ |
| 2120 |
"id": "successful", |
| 2121 |
"should_raise_error": False, |
| 2122 |
"expected_result": ("This is a paragraph.", 0, 20), |
| 2123 |
}, |
| 2124 |
{"id": "glib_error", "should_raise_error": True, "expected_result": ("", 0, 0)}, |
| 2125 |
], |
| 2126 |
ids=lambda case: case["id"], |
| 2127 |
) |
| 2128 |
def test_get_paragraph_at_offset( |
| 2129 |
self, |
| 2130 |
test_context: OrcaTestContext, |
| 2131 |
case: dict, |
| 2132 |
) -> None: |
| 2133 |
"""Test AXText.get_paragraph_at_offset successful case and GLib.GError handling.""" |
| 2134 |
|
| 2135 |
self._setup_dependencies(test_context) |
| 2136 |
from orca.ax_text import AXText |
| 2137 |
|
| 2138 |
test_context.patch_object(AXText, "get_character_count", return_value=25) |
| 2139 |
test_context.patch_object(AXText, "get_caret_offset", return_value=7) |
| 2140 |
|
| 2141 |
if case["should_raise_error"]: |
| 2142 |
|
| 2143 |
def mock_get_string_at_offset(_obj, _offset, _granularity) -> None: |
| 2144 |
raise GLib.GError("Test error") |
| 2145 |
|
| 2146 |
test_context.patch( |
| 2147 |
"gi.repository.Atspi.Text.get_string_at_offset", |
| 2148 |
new=mock_get_string_at_offset, |
| 2149 |
) |
| 2150 |
else: |
| 2151 |
mock_result = test_context.Mock() |
| 2152 |
mock_result.content = "This is a paragraph." |
| 2153 |
mock_result.start_offset = 0 |
| 2154 |
mock_result.end_offset = 20 |
| 2155 |
test_context.patch( |
| 2156 |
"gi.repository.Atspi.Text.get_string_at_offset", |
| 2157 |
side_effect=lambda obj, offset, granularity: mock_result, |
| 2158 |
) |
| 2159 |
|
| 2160 |
result = AXText.get_paragraph_at_offset(test_context.Mock(spec=Atspi.Accessible)) |
| 2161 |
assert result == case["expected_result"] |
| 2162 |
|
| 2163 |
def test_get_character_at_offset_invalid_offset(self, test_context: OrcaTestContext) -> None: |
| 2164 |
"""Test AXText.get_character_at_offset with invalid offset.""" |
| 2165 |
|
| 2166 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 2167 |
from orca.ax_text import AXText |
| 2168 |
|
| 2169 |
essential_modules["orca.debug"].print_message = test_context.Mock() |
| 2170 |
test_context.patch("gi.repository.Atspi.Text.get_character_count", return_value=10) |
| 2171 |
result = AXText.get_character_at_offset(test_context.Mock(spec=Atspi.Accessible), 15) |
| 2172 |
assert result == ("", 0, 0) |
| 2173 |
essential_modules["orca.debug"].print_message.assert_called() |
| 2174 |
|
| 2175 |
def test_get_substring_with_end_offset_minus_one(self, test_context: OrcaTestContext) -> None: |
| 2176 |
"""Test AXText.get_substring with end_offset=-1.""" |
| 2177 |
|
| 2178 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 2179 |
from orca.ax_text import AXText |
| 2180 |
|
| 2181 |
test_context.patch_object(AXText, "get_character_count", return_value=20) |
| 2182 |
test_context.patch_object(Atspi.Text, "get_text", return_value="substring") |
| 2183 |
essential_modules["orca.debug"].print_tokens = test_context.Mock() |
| 2184 |
result = AXText.get_substring(test_context.Mock(spec=Atspi.Accessible), 5, -1) |
| 2185 |
assert result == "substring" |
| 2186 |
|
| 2187 |
def test_supports_paragraph_iteration_false(self, test_context: OrcaTestContext) -> None: |
| 2188 |
"""Test AXText.supports_paragraph_iteration returns False.""" |
| 2189 |
|
| 2190 |
self._setup_dependencies(test_context) |
| 2191 |
from orca.ax_object import AXObject |
| 2192 |
from orca.ax_utilities_text import AXUtilitiesText |
| 2193 |
|
| 2194 |
test_context.patch_object(AXObject, "supports_text", return_value=False) |
| 2195 |
result = AXUtilitiesText.supports_paragraph_iteration( |
| 2196 |
test_context.Mock(spec=Atspi.Accessible) |
| 2197 |
) |
| 2198 |
assert result is False |
| 2199 |
|
| 2200 |
def test_is_whitespace_or_empty_no_text_support(self, test_context: OrcaTestContext) -> None: |
| 2201 |
"""Test AXText.is_whitespace_or_empty with no text support.""" |
| 2202 |
|
| 2203 |
self._setup_dependencies(test_context) |
| 2204 |
from orca.ax_object import AXObject |
| 2205 |
from orca.ax_utilities_text import AXUtilitiesText |
| 2206 |
|
| 2207 |
test_context.patch_object(AXObject, "supports_text", return_value=False) |
| 2208 |
result = AXUtilitiesText.is_whitespace_or_empty(test_context.Mock(spec=Atspi.Accessible)) |
| 2209 |
assert result is True |
| 2210 |
|
| 2211 |
@pytest.mark.parametrize( |
| 2212 |
"case", |
| 2213 |
[ |
| 2214 |
{"id": "matching_locale", "attribute_value": "en", "expected": True}, |
| 2215 |
{"id": "non_matching_locale", "attribute_value": "fr", "expected": False}, |
| 2216 |
], |
| 2217 |
ids=lambda case: case["id"], |
| 2218 |
) |
| 2219 |
def test_value_is_default_language_attribute( |
| 2220 |
self, |
| 2221 |
test_context: OrcaTestContext, |
| 2222 |
case: dict, |
| 2223 |
) -> None: |
| 2224 |
"""Test AXTextAttribute.value_is_default for LANGUAGE attribute.""" |
| 2225 |
|
| 2226 |
self._setup_dependencies(test_context) |
| 2227 |
from orca.ax_text import AXTextAttribute |
| 2228 |
|
| 2229 |
test_context.patch("locale.getlocale", return_value=("en_US", "UTF-8")) |
| 2230 |
result = AXTextAttribute.LANGUAGE.value_is_default(case["attribute_value"]) |
| 2231 |
assert result is case["expected"] |
| 2232 |
|
| 2233 |
@pytest.mark.parametrize( |
| 2234 |
"case", |
| 2235 |
[ |
| 2236 |
{ |
| 2237 |
"id": "next_character_successful", |
| 2238 |
"direction": "next", |
| 2239 |
"unit_type": "character", |
| 2240 |
"setup_params": { |
| 2241 |
"caret_offset": 0, |
| 2242 |
"count": 5, |
| 2243 |
"data": {"0": ("a", 0, 1), "1": ("b", 1, 2), "2": ("c", 2, 3)}, |
| 2244 |
}, |
| 2245 |
"method_params": {}, |
| 2246 |
"expected": ("b", 1, 2), |
| 2247 |
}, |
| 2248 |
{ |
| 2249 |
"id": "next_character_with_offset", |
| 2250 |
"direction": "next", |
| 2251 |
"unit_type": "character", |
| 2252 |
"setup_params": {"count": 5, "data": {"1": ("b", 1, 2), "2": ("c", 2, 3)}}, |
| 2253 |
"method_params": {"offset": 1}, |
| 2254 |
"expected": ("c", 2, 3), |
| 2255 |
}, |
| 2256 |
{ |
| 2257 |
"id": "next_character_no_current", |
| 2258 |
"direction": "next", |
| 2259 |
"unit_type": "character", |
| 2260 |
"setup_params": {"caret_offset": 5, "data": {}}, |
| 2261 |
"method_params": {}, |
| 2262 |
"expected": ("", 0, 0), |
| 2263 |
}, |
| 2264 |
{ |
| 2265 |
"id": "next_character_at_end", |
| 2266 |
"direction": "next", |
| 2267 |
"unit_type": "character", |
| 2268 |
"setup_params": {"caret_offset": 4, "count": 5, "data": {"4": ("z", 4, 5)}}, |
| 2269 |
"method_params": {}, |
| 2270 |
"expected": ("", 0, 0), |
| 2271 |
}, |
| 2272 |
{ |
| 2273 |
"id": "next_word_successful", |
| 2274 |
"direction": "next", |
| 2275 |
"unit_type": "word", |
| 2276 |
"setup_params": { |
| 2277 |
"caret_offset": 0, |
| 2278 |
"count": 15, |
| 2279 |
"word_data": {"0-5": ("first", 0, 5), "6-11": ("second", 6, 11)}, |
| 2280 |
}, |
| 2281 |
"method_params": {}, |
| 2282 |
"expected": ("second", 6, 11), |
| 2283 |
}, |
| 2284 |
{ |
| 2285 |
"id": "next_word_with_offset", |
| 2286 |
"direction": "next", |
| 2287 |
"unit_type": "word", |
| 2288 |
"setup_params": { |
| 2289 |
"count": 15, |
| 2290 |
"word_data": {"0-5": ("first", 0, 5), "6-11": ("second", 6, 11)}, |
| 2291 |
}, |
| 2292 |
"method_params": {"offset": 2}, |
| 2293 |
"expected": ("second", 6, 11), |
| 2294 |
}, |
| 2295 |
{ |
| 2296 |
"id": "next_word_no_current", |
| 2297 |
"direction": "next", |
| 2298 |
"unit_type": "word", |
| 2299 |
"setup_params": {"caret_offset": 5, "word_data": {}}, |
| 2300 |
"method_params": {}, |
| 2301 |
"expected": ("", 0, 0), |
| 2302 |
}, |
| 2303 |
{ |
| 2304 |
"id": "next_word_at_end", |
| 2305 |
"direction": "next", |
| 2306 |
"unit_type": "word", |
| 2307 |
"setup_params": { |
| 2308 |
"caret_offset": 10, |
| 2309 |
"count": 15, |
| 2310 |
"word_data": {"10-15": ("last", 10, 15)}, |
| 2311 |
}, |
| 2312 |
"method_params": {}, |
| 2313 |
"expected": ("", 0, 0), |
| 2314 |
}, |
| 2315 |
{ |
| 2316 |
"id": "next_line_successful", |
| 2317 |
"direction": "next", |
| 2318 |
"unit_type": "line", |
| 2319 |
"setup_params": { |
| 2320 |
"caret_offset": 0, |
| 2321 |
"count": 25, |
| 2322 |
"line_data": {"0-10": ("First line", 0, 10), "11-20": ("Second line", 11, 20)}, |
| 2323 |
}, |
| 2324 |
"method_params": {}, |
| 2325 |
"expected": ("Second line", 11, 20), |
| 2326 |
}, |
| 2327 |
{ |
| 2328 |
"id": "next_line_with_offset", |
| 2329 |
"direction": "next", |
| 2330 |
"unit_type": "line", |
| 2331 |
"setup_params": { |
| 2332 |
"count": 25, |
| 2333 |
"line_data": {"0-10": ("First line", 0, 10), "11-20": ("Second line", 11, 20)}, |
| 2334 |
}, |
| 2335 |
"method_params": {"offset": 5}, |
| 2336 |
"expected": ("Second line", 11, 20), |
| 2337 |
}, |
| 2338 |
{ |
| 2339 |
"id": "next_line_no_current", |
| 2340 |
"direction": "next", |
| 2341 |
"unit_type": "line", |
| 2342 |
"setup_params": {"caret_offset": 5, "line_data": {}}, |
| 2343 |
"method_params": {}, |
| 2344 |
"expected": ("", 0, 0), |
| 2345 |
}, |
| 2346 |
{ |
| 2347 |
"id": "next_line_at_end", |
| 2348 |
"direction": "next", |
| 2349 |
"unit_type": "line", |
| 2350 |
"setup_params": { |
| 2351 |
"caret_offset": 15, |
| 2352 |
"count": 25, |
| 2353 |
"line_data": {"15-25": ("Last line", 15, 25)}, |
| 2354 |
}, |
| 2355 |
"method_params": {}, |
| 2356 |
"expected": ("", 0, 0), |
| 2357 |
}, |
| 2358 |
{ |
| 2359 |
"id": "next_sentence_successful", |
| 2360 |
"direction": "next", |
| 2361 |
"unit_type": "sentence", |
| 2362 |
"setup_params": { |
| 2363 |
"caret_offset": 0, |
| 2364 |
"count": 30, |
| 2365 |
"sentence_data": { |
| 2366 |
"0-10": ("First sentence.", 0, 10), |
| 2367 |
"11-25": ("Second sentence.", 11, 25), |
| 2368 |
}, |
| 2369 |
}, |
| 2370 |
"method_params": {}, |
| 2371 |
"expected": ("Second sentence.", 11, 25), |
| 2372 |
}, |
| 2373 |
{ |
| 2374 |
"id": "next_sentence_with_offset", |
| 2375 |
"direction": "next", |
| 2376 |
"unit_type": "sentence", |
| 2377 |
"setup_params": { |
| 2378 |
"count": 30, |
| 2379 |
"sentence_data": { |
| 2380 |
"0-10": ("First sentence.", 0, 10), |
| 2381 |
"11-25": ("Second sentence.", 11, 25), |
| 2382 |
}, |
| 2383 |
}, |
| 2384 |
"method_params": {"offset": 5}, |
| 2385 |
"expected": ("Second sentence.", 11, 25), |
| 2386 |
}, |
| 2387 |
{ |
| 2388 |
"id": "next_sentence_no_current", |
| 2389 |
"direction": "next", |
| 2390 |
"unit_type": "sentence", |
| 2391 |
"setup_params": {"caret_offset": 5, "sentence_data": {}}, |
| 2392 |
"method_params": {}, |
| 2393 |
"expected": ("", 0, 0), |
| 2394 |
}, |
| 2395 |
{ |
| 2396 |
"id": "next_sentence_at_end", |
| 2397 |
"direction": "next", |
| 2398 |
"unit_type": "sentence", |
| 2399 |
"setup_params": { |
| 2400 |
"caret_offset": 20, |
| 2401 |
"count": 30, |
| 2402 |
"sentence_data": {"20-30": ("Last sentence.", 20, 30)}, |
| 2403 |
}, |
| 2404 |
"method_params": {}, |
| 2405 |
"expected": ("", 0, 0), |
| 2406 |
}, |
| 2407 |
{ |
| 2408 |
"id": "next_paragraph_successful", |
| 2409 |
"direction": "next", |
| 2410 |
"unit_type": "paragraph", |
| 2411 |
"setup_params": { |
| 2412 |
"caret_offset": 0, |
| 2413 |
"count": 40, |
| 2414 |
"paragraph_data": { |
| 2415 |
"0-15": ("First paragraph.", 0, 15), |
| 2416 |
"16-30": ("Second paragraph.", 16, 30), |
| 2417 |
}, |
| 2418 |
}, |
| 2419 |
"method_params": {}, |
| 2420 |
"expected": ("Second paragraph.", 16, 30), |
| 2421 |
}, |
| 2422 |
{ |
| 2423 |
"id": "next_paragraph_with_offset", |
| 2424 |
"direction": "next", |
| 2425 |
"unit_type": "paragraph", |
| 2426 |
"setup_params": { |
| 2427 |
"count": 40, |
| 2428 |
"paragraph_data": { |
| 2429 |
"0-15": ("First paragraph.", 0, 15), |
| 2430 |
"16-30": ("Second paragraph.", 16, 30), |
| 2431 |
}, |
| 2432 |
}, |
| 2433 |
"method_params": {"offset": 8}, |
| 2434 |
"expected": ("Second paragraph.", 16, 30), |
| 2435 |
}, |
| 2436 |
{ |
| 2437 |
"id": "next_paragraph_no_current", |
| 2438 |
"direction": "next", |
| 2439 |
"unit_type": "paragraph", |
| 2440 |
"setup_params": {"caret_offset": 5, "paragraph_data": {}}, |
| 2441 |
"method_params": {}, |
| 2442 |
"expected": ("", 0, 0), |
| 2443 |
}, |
| 2444 |
{ |
| 2445 |
"id": "next_paragraph_at_end", |
| 2446 |
"direction": "next", |
| 2447 |
"unit_type": "paragraph", |
| 2448 |
"setup_params": { |
| 2449 |
"caret_offset": 25, |
| 2450 |
"count": 40, |
| 2451 |
"paragraph_data": {"25-40": ("Last paragraph.", 25, 40)}, |
| 2452 |
}, |
| 2453 |
"method_params": {}, |
| 2454 |
"expected": ("", 0, 0), |
| 2455 |
}, |
| 2456 |
{ |
| 2457 |
"id": "previous_character_successful", |
| 2458 |
"direction": "previous", |
| 2459 |
"unit_type": "character", |
| 2460 |
"setup_params": {"caret_offset": 2, "data": {"1": ("a", 1, 2), "2": ("b", 2, 3)}}, |
| 2461 |
"method_params": {}, |
| 2462 |
"expected": ("a", 1, 2), |
| 2463 |
}, |
| 2464 |
{ |
| 2465 |
"id": "previous_character_with_offset", |
| 2466 |
"direction": "previous", |
| 2467 |
"unit_type": "character", |
| 2468 |
"setup_params": {"data": {"0": ("a", 0, 1), "1": ("b", 1, 2)}}, |
| 2469 |
"method_params": {"offset": 1}, |
| 2470 |
"expected": ("a", 0, 1), |
| 2471 |
}, |
| 2472 |
{ |
| 2473 |
"id": "previous_character_no_current", |
| 2474 |
"direction": "previous", |
| 2475 |
"unit_type": "character", |
| 2476 |
"setup_params": {"caret_offset": 5, "data": {}}, |
| 2477 |
"method_params": {}, |
| 2478 |
"expected": ("", 0, 0), |
| 2479 |
}, |
| 2480 |
{ |
| 2481 |
"id": "previous_character_at_start", |
| 2482 |
"direction": "previous", |
| 2483 |
"unit_type": "character", |
| 2484 |
"setup_params": {"caret_offset": 0, "data": {"0": ("a", 0, 1)}}, |
| 2485 |
"method_params": {}, |
| 2486 |
"expected": ("", 0, 0), |
| 2487 |
}, |
| 2488 |
{ |
| 2489 |
"id": "previous_word_successful", |
| 2490 |
"direction": "previous", |
| 2491 |
"unit_type": "word", |
| 2492 |
"setup_params": { |
| 2493 |
"caret_offset": 8, |
| 2494 |
"word_data": {"0-4": ("first", 0, 4), "5-11": ("second", 5, 11)}, |
| 2495 |
}, |
| 2496 |
"method_params": {}, |
| 2497 |
"expected": ("first", 0, 4), |
| 2498 |
}, |
| 2499 |
{ |
| 2500 |
"id": "previous_word_with_offset", |
| 2501 |
"direction": "previous", |
| 2502 |
"unit_type": "word", |
| 2503 |
"setup_params": {"word_data": {"0-4": ("first", 0, 4), "5-11": ("second", 5, 11)}}, |
| 2504 |
"method_params": {"offset": 8}, |
| 2505 |
"expected": ("first", 0, 4), |
| 2506 |
}, |
| 2507 |
{ |
| 2508 |
"id": "previous_word_no_current", |
| 2509 |
"direction": "previous", |
| 2510 |
"unit_type": "word", |
| 2511 |
"setup_params": {"caret_offset": 5, "word_data": {}}, |
| 2512 |
"method_params": {}, |
| 2513 |
"expected": ("", 0, 0), |
| 2514 |
}, |
| 2515 |
{ |
| 2516 |
"id": "previous_word_at_start", |
| 2517 |
"direction": "previous", |
| 2518 |
"unit_type": "word", |
| 2519 |
"setup_params": {"caret_offset": 0, "word_data": {"0-4": ("first", 0, 4)}}, |
| 2520 |
"method_params": {}, |
| 2521 |
"expected": ("", 0, 0), |
| 2522 |
}, |
| 2523 |
{ |
| 2524 |
"id": "previous_line_successful", |
| 2525 |
"direction": "previous", |
| 2526 |
"unit_type": "line", |
| 2527 |
"setup_params": { |
| 2528 |
"caret_offset": 15, |
| 2529 |
"line_data": {"0-10": ("First line", 0, 10), "11-20": ("Second line", 11, 20)}, |
| 2530 |
}, |
| 2531 |
"method_params": {}, |
| 2532 |
"expected": ("First line", 0, 10), |
| 2533 |
}, |
| 2534 |
{ |
| 2535 |
"id": "previous_line_with_offset", |
| 2536 |
"direction": "previous", |
| 2537 |
"unit_type": "line", |
| 2538 |
"setup_params": { |
| 2539 |
"line_data": {"0-10": ("First line", 0, 10), "11-20": ("Second line", 11, 20)}, |
| 2540 |
}, |
| 2541 |
"method_params": {"offset": 15}, |
| 2542 |
"expected": ("First line", 0, 10), |
| 2543 |
}, |
| 2544 |
{ |
| 2545 |
"id": "previous_line_no_current", |
| 2546 |
"direction": "previous", |
| 2547 |
"unit_type": "line", |
| 2548 |
"setup_params": {"caret_offset": 5, "count": 10, "line_data": {}}, |
| 2549 |
"method_params": {}, |
| 2550 |
"expected": ("", 0, 0), |
| 2551 |
}, |
| 2552 |
{ |
| 2553 |
"id": "previous_line_at_start", |
| 2554 |
"direction": "previous", |
| 2555 |
"unit_type": "line", |
| 2556 |
"setup_params": {"caret_offset": 0, "line_data": {"0-10": ("First line", 0, 10)}}, |
| 2557 |
"method_params": {}, |
| 2558 |
"expected": ("", 0, 0), |
| 2559 |
}, |
| 2560 |
{ |
| 2561 |
"id": "previous_sentence_successful", |
| 2562 |
"direction": "previous", |
| 2563 |
"unit_type": "sentence", |
| 2564 |
"setup_params": { |
| 2565 |
"caret_offset": 20, |
| 2566 |
"sentence_data": { |
| 2567 |
"0-15": ("First sentence.", 0, 15), |
| 2568 |
"16-30": ("Second sentence.", 16, 30), |
| 2569 |
}, |
| 2570 |
}, |
| 2571 |
"method_params": {}, |
| 2572 |
"expected": ("First sentence.", 0, 15), |
| 2573 |
}, |
| 2574 |
{ |
| 2575 |
"id": "previous_sentence_with_offset", |
| 2576 |
"direction": "previous", |
| 2577 |
"unit_type": "sentence", |
| 2578 |
"setup_params": { |
| 2579 |
"sentence_data": { |
| 2580 |
"0-15": ("First sentence.", 0, 15), |
| 2581 |
"16-30": ("Second sentence.", 16, 30), |
| 2582 |
}, |
| 2583 |
}, |
| 2584 |
"method_params": {"offset": 20}, |
| 2585 |
"expected": ("First sentence.", 0, 15), |
| 2586 |
}, |
| 2587 |
{ |
| 2588 |
"id": "previous_sentence_no_current", |
| 2589 |
"direction": "previous", |
| 2590 |
"unit_type": "sentence", |
| 2591 |
"setup_params": {"caret_offset": 5, "sentence_data": {}}, |
| 2592 |
"method_params": {}, |
| 2593 |
"expected": ("", 0, 0), |
| 2594 |
}, |
| 2595 |
{ |
| 2596 |
"id": "previous_sentence_at_start", |
| 2597 |
"direction": "previous", |
| 2598 |
"unit_type": "sentence", |
| 2599 |
"setup_params": { |
| 2600 |
"caret_offset": 0, |
| 2601 |
"sentence_data": {"0-15": ("First sentence.", 0, 15)}, |
| 2602 |
}, |
| 2603 |
"method_params": {}, |
| 2604 |
"expected": ("", 0, 0), |
| 2605 |
}, |
| 2606 |
{ |
| 2607 |
"id": "previous_paragraph_successful", |
| 2608 |
"direction": "previous", |
| 2609 |
"unit_type": "paragraph", |
| 2610 |
"setup_params": { |
| 2611 |
"caret_offset": 20, |
| 2612 |
"paragraph_data": { |
| 2613 |
"0-15": ("First paragraph.", 0, 15), |
| 2614 |
"16-30": ("Second paragraph.", 16, 30), |
| 2615 |
}, |
| 2616 |
}, |
| 2617 |
"method_params": {}, |
| 2618 |
"expected": ("First paragraph.", 0, 15), |
| 2619 |
}, |
| 2620 |
{ |
| 2621 |
"id": "previous_paragraph_with_offset", |
| 2622 |
"direction": "previous", |
| 2623 |
"unit_type": "paragraph", |
| 2624 |
"setup_params": { |
| 2625 |
"paragraph_data": { |
| 2626 |
"0-15": ("First paragraph.", 0, 15), |
| 2627 |
"16-30": ("Second paragraph.", 16, 30), |
| 2628 |
}, |
| 2629 |
}, |
| 2630 |
"method_params": {"offset": 20}, |
| 2631 |
"expected": ("First paragraph.", 0, 15), |
| 2632 |
}, |
| 2633 |
{ |
| 2634 |
"id": "previous_paragraph_no_current", |
| 2635 |
"direction": "previous", |
| 2636 |
"unit_type": "paragraph", |
| 2637 |
"setup_params": {"caret_offset": 5, "paragraph_data": {}}, |
| 2638 |
"method_params": {}, |
| 2639 |
"expected": ("", 0, 0), |
| 2640 |
}, |
| 2641 |
{ |
| 2642 |
"id": "previous_paragraph_at_start", |
| 2643 |
"direction": "previous", |
| 2644 |
"unit_type": "paragraph", |
| 2645 |
"setup_params": { |
| 2646 |
"caret_offset": 0, |
| 2647 |
"paragraph_data": {"0-15": ("First paragraph.", 0, 15)}, |
| 2648 |
}, |
| 2649 |
"method_params": {}, |
| 2650 |
"expected": ("", 0, 0), |
| 2651 |
}, |
| 2652 |
], |
| 2653 |
ids=lambda case: case["id"], |
| 2654 |
) |
| 2655 |
def test_get_next_text_unit_scenarios( # pylint: disable=too-many-branches,too-many-statements |
| 2656 |
self, |
| 2657 |
test_context: OrcaTestContext, |
| 2658 |
case: dict, |
| 2659 |
) -> None: |
| 2660 |
"""Test AXText.get_next_* and get_previous_* methods with various scenarios.""" |
| 2661 |
|
| 2662 |
self._setup_dependencies(test_context) |
| 2663 |
from orca.ax_text import AXText |
| 2664 |
from orca.ax_utilities_text import AXUtilitiesText |
| 2665 |
|
| 2666 |
setup_params = case["setup_params"] |
| 2667 |
method_params = case["method_params"] |
| 2668 |
direction = case["direction"] |
| 2669 |
unit_type = case["unit_type"] |
| 2670 |
expected = case["expected"] |
| 2671 |
|
| 2672 |
if "caret_offset" in setup_params: |
| 2673 |
test_context.patch_object( |
| 2674 |
AXText, |
| 2675 |
"get_caret_offset", |
| 2676 |
side_effect=lambda obj: setup_params["caret_offset"], |
| 2677 |
) |
| 2678 |
if "count" in setup_params: |
| 2679 |
test_context.patch_object( |
| 2680 |
AXText, |
| 2681 |
"get_character_count", |
| 2682 |
side_effect=lambda obj: setup_params["count"], |
| 2683 |
) |
| 2684 |
|
| 2685 |
if unit_type == "character": |
| 2686 |
|
| 2687 |
def mock_get_character_at_offset(_obj, offset) -> tuple[str, int, int]: |
| 2688 |
return setup_params["data"].get(str(offset), ("", 0, 0)) |
| 2689 |
|
| 2690 |
test_context.patch_object( |
| 2691 |
AXText, |
| 2692 |
"get_character_at_offset", |
| 2693 |
new=mock_get_character_at_offset, |
| 2694 |
) |
| 2695 |
|
| 2696 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 2697 |
if direction == "next": |
| 2698 |
if method_params: |
| 2699 |
result = AXUtilitiesText.get_next_character(mock_obj, **method_params) |
| 2700 |
else: |
| 2701 |
result = AXUtilitiesText.get_next_character(mock_obj) |
| 2702 |
elif method_params: |
| 2703 |
result = AXUtilitiesText.get_previous_character(mock_obj, **method_params) |
| 2704 |
else: |
| 2705 |
result = AXUtilitiesText.get_previous_character(mock_obj) |
| 2706 |
elif unit_type == "word": |
| 2707 |
|
| 2708 |
def mock_get_word_at_offset(_obj, offset) -> tuple[str, int, int]: |
| 2709 |
for range_key, data in setup_params.get("word_data", {}).items(): |
| 2710 |
if "-" in range_key: |
| 2711 |
start, end = map(int, range_key.split("-")) |
| 2712 |
if start <= offset <= end: |
| 2713 |
return data |
| 2714 |
return ("", 0, 0) |
| 2715 |
|
| 2716 |
test_context.patch_object(AXText, "get_word_at_offset", new=mock_get_word_at_offset) |
| 2717 |
|
| 2718 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 2719 |
if direction == "next": |
| 2720 |
if method_params: |
| 2721 |
result = AXUtilitiesText.get_next_word(mock_obj, **method_params) |
| 2722 |
else: |
| 2723 |
result = AXUtilitiesText.get_next_word(mock_obj) |
| 2724 |
elif method_params: |
| 2725 |
result = AXUtilitiesText.get_previous_word(mock_obj, **method_params) |
| 2726 |
else: |
| 2727 |
result = AXUtilitiesText.get_previous_word(mock_obj) |
| 2728 |
elif unit_type == "line": |
| 2729 |
|
| 2730 |
def mock_get_line_at_offset(_obj, offset) -> tuple[str, int, int]: |
| 2731 |
for range_key, data in setup_params.get("line_data", {}).items(): |
| 2732 |
if "-" in range_key: |
| 2733 |
start, end = map(int, range_key.split("-")) |
| 2734 |
if start <= offset <= end: |
| 2735 |
return data |
| 2736 |
return ("", 0, 0) |
| 2737 |
|
| 2738 |
test_context.patch_object(AXText, "get_line_at_offset", new=mock_get_line_at_offset) |
| 2739 |
|
| 2740 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 2741 |
if direction == "next": |
| 2742 |
if method_params: |
| 2743 |
result = AXUtilitiesText.get_next_line(mock_obj, **method_params) |
| 2744 |
else: |
| 2745 |
result = AXUtilitiesText.get_next_line(mock_obj) |
| 2746 |
elif method_params: |
| 2747 |
result = AXUtilitiesText.get_previous_line(mock_obj, **method_params) |
| 2748 |
else: |
| 2749 |
result = AXUtilitiesText.get_previous_line(mock_obj) |
| 2750 |
elif unit_type == "sentence": |
| 2751 |
|
| 2752 |
def mock_get_sentence_at_offset(_obj, offset) -> tuple[str, int, int]: |
| 2753 |
for range_key, data in setup_params.get("sentence_data", {}).items(): |
| 2754 |
if "-" in range_key: |
| 2755 |
start, end = map(int, range_key.split("-")) |
| 2756 |
if start <= offset <= end: |
| 2757 |
return data |
| 2758 |
return ("", 0, 0) |
| 2759 |
|
| 2760 |
test_context.patch_object( |
| 2761 |
AXText, |
| 2762 |
"get_sentence_at_offset", |
| 2763 |
new=mock_get_sentence_at_offset, |
| 2764 |
) |
| 2765 |
|
| 2766 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 2767 |
if direction == "next": |
| 2768 |
if method_params: |
| 2769 |
result = AXUtilitiesText.get_next_sentence(mock_obj, **method_params) |
| 2770 |
else: |
| 2771 |
result = AXUtilitiesText.get_next_sentence(mock_obj) |
| 2772 |
elif method_params: |
| 2773 |
result = AXUtilitiesText.get_previous_sentence(mock_obj, **method_params) |
| 2774 |
else: |
| 2775 |
result = AXUtilitiesText.get_previous_sentence(mock_obj) |
| 2776 |
elif unit_type == "paragraph": |
| 2777 |
|
| 2778 |
def mock_get_paragraph_at_offset(_obj, offset) -> tuple[str, int, int]: |
| 2779 |
for range_key, data in setup_params.get("paragraph_data", {}).items(): |
| 2780 |
if "-" in range_key: |
| 2781 |
start, end = map(int, range_key.split("-")) |
| 2782 |
if start <= offset <= end: |
| 2783 |
return data |
| 2784 |
return ("", 0, 0) |
| 2785 |
|
| 2786 |
test_context.patch_object( |
| 2787 |
AXText, |
| 2788 |
"get_paragraph_at_offset", |
| 2789 |
new=mock_get_paragraph_at_offset, |
| 2790 |
) |
| 2791 |
|
| 2792 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 2793 |
if direction == "next": |
| 2794 |
if method_params: |
| 2795 |
result = AXUtilitiesText.get_next_paragraph(mock_obj, **method_params) |
| 2796 |
else: |
| 2797 |
result = AXUtilitiesText.get_next_paragraph(mock_obj) |
| 2798 |
elif method_params: |
| 2799 |
result = AXUtilitiesText.get_previous_paragraph(mock_obj, **method_params) |
| 2800 |
else: |
| 2801 |
result = AXUtilitiesText.get_previous_paragraph(mock_obj) |
| 2802 |
else: |
| 2803 |
result = ("", 0, 0) |
| 2804 |
|
| 2805 |
assert result == expected |
| 2806 |
|
| 2807 |
def test_get_character_at_offset_with_empty_text(self, test_context: OrcaTestContext) -> None: |
| 2808 |
"""Test AXText.get_character_at_offset when text is empty.""" |
| 2809 |
|
| 2810 |
self._setup_dependencies(test_context) |
| 2811 |
from orca.ax_text import AXText |
| 2812 |
|
| 2813 |
test_context.patch("gi.repository.Atspi.Text.get_character_count", return_value=0) |
| 2814 |
result = AXText.get_character_at_offset(test_context.Mock(spec=Atspi.Accessible), 0) |
| 2815 |
assert result == ("", 0, 0) |
| 2816 |
|
| 2817 |
def test_get_character_at_offset_with_none_offset(self, test_context: OrcaTestContext) -> None: |
| 2818 |
"""Test AXText.get_character_at_offset when offset is None.""" |
| 2819 |
|
| 2820 |
self._setup_dependencies(test_context) |
| 2821 |
from orca.ax_text import AXText |
| 2822 |
|
| 2823 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 2824 |
test_context.patch("gi.repository.Atspi.Text.get_character_count", return_value=10) |
| 2825 |
test_context.patch_object(AXText, "get_caret_offset", return_value=5) |
| 2826 |
|
| 2827 |
mock_result = test_context.Mock() |
| 2828 |
mock_result.content = "a" |
| 2829 |
mock_result.start_offset = 5 |
| 2830 |
mock_result.end_offset = 6 |
| 2831 |
test_context.patch( |
| 2832 |
"gi.repository.Atspi.Text.get_string_at_offset", |
| 2833 |
side_effect=lambda obj, offset, granularity: mock_result, |
| 2834 |
) |
| 2835 |
|
| 2836 |
result = AXText.get_character_at_offset(mock_obj, None) |
| 2837 |
assert result == ("a", 5, 6) |
| 2838 |
|
| 2839 |
def test_get_character_at_offset_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 2840 |
"""Test AXText.get_character_at_offset when GLib.GError occurs.""" |
| 2841 |
|
| 2842 |
self._setup_dependencies(test_context) |
| 2843 |
from orca.ax_text import AXText |
| 2844 |
|
| 2845 |
def raise_glib_error(_obj, _offset, _granularity): |
| 2846 |
raise GLib.GError("Test error") |
| 2847 |
|
| 2848 |
test_context.patch("gi.repository.Atspi.Text.get_character_count", return_value=10) |
| 2849 |
test_context.patch("gi.repository.Atspi.Text.get_string_at_offset", new=raise_glib_error) |
| 2850 |
|
| 2851 |
result = AXText.get_character_at_offset(test_context.Mock(spec=Atspi.Accessible), 5) |
| 2852 |
assert result == ("", 0, 0) |
| 2853 |
|
| 2854 |
def test_get_previous_line_at_end_with_newline_sets_correct_start( |
| 2855 |
self, |
| 2856 |
test_context: OrcaTestContext, |
| 2857 |
) -> None: |
| 2858 |
"""Test AXText.get_previous_line sets start when fallback ends with newline.""" |
| 2859 |
|
| 2860 |
self._setup_dependencies(test_context) |
| 2861 |
from orca.ax_text import AXText |
| 2862 |
from orca.ax_utilities_text import AXUtilitiesText |
| 2863 |
|
| 2864 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 2865 |
|
| 2866 |
# Mock get_character_count to return 12 (end of text) |
| 2867 |
test_context.patch_object(AXText, "get_character_count", return_value=12) |
| 2868 |
|
| 2869 |
# Mock get_line_at_offset for the sequence |
| 2870 |
def mock_get_line_at_offset(_obj, offset): |
| 2871 |
if offset == 12: # At end of text - empty line |
| 2872 |
return ("", 12, 12) |
| 2873 |
if offset == 11: # One character back - line ending with newline |
| 2874 |
return ("Hello World\n", 0, 12) |
| 2875 |
if offset == 10: # Previous line search from (offset-1)-1 = 10 |
| 2876 |
return ("Previous Line", 0, 11) |
| 2877 |
return ("", 0, 0) |
| 2878 |
|
| 2879 |
test_context.patch_object(AXText, "get_line_at_offset", new=mock_get_line_at_offset) |
| 2880 |
|
| 2881 |
# Test: when at end with empty line and fallback has newline |
| 2882 |
result = AXUtilitiesText.get_previous_line(mock_obj, 12) |
| 2883 |
|
| 2884 |
# The current line becomes "Hello World\n" with start=11 |
| 2885 |
assert result == ("Previous Line", 0, 11) |
| 2886 |
|
| 2887 |
def test_get_previous_line_at_end_of_text_fallback_to_normal_behavior( |
| 2888 |
self, |
| 2889 |
test_context: OrcaTestContext, |
| 2890 |
) -> None: |
| 2891 |
"""Test AXText.get_previous_line at end when fallback lacks newline.""" |
| 2892 |
|
| 2893 |
self._setup_dependencies(test_context) |
| 2894 |
from orca.ax_text import AXText |
| 2895 |
from orca.ax_utilities_text import AXUtilitiesText |
| 2896 |
|
| 2897 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 2898 |
|
| 2899 |
# Mock get_character_count to return 11 (end of text) |
| 2900 |
test_context.patch_object(AXText, "get_character_count", return_value=11) |
| 2901 |
|
| 2902 |
# Mock get_line_at_offset scenarios |
| 2903 |
def mock_get_line_at_offset(_obj, offset): |
| 2904 |
if offset == 11: # At end of text |
| 2905 |
return ("", 11, 11) |
| 2906 |
if offset == 10: # One character back - no newline |
| 2907 |
return ("Hello World", 5, 15) # start > 0 so it continues |
| 2908 |
if offset == 4: # Previous line lookup from start-1 = 4 |
| 2909 |
return ("Previous Line", 0, 5) |
| 2910 |
return ("", 0, 0) |
| 2911 |
|
| 2912 |
test_context.patch_object(AXText, "get_line_at_offset", new=mock_get_line_at_offset) |
| 2913 |
|
| 2914 |
# Test: should use normal behavior when fallback line doesn't end with newline |
| 2915 |
result = AXUtilitiesText.get_previous_line(mock_obj, 11) |
| 2916 |
assert result == ("Previous Line", 0, 5) |
| 2917 |
|
| 2918 |
def test_get_previous_line_empty_current_line_returns_empty( |
| 2919 |
self, |
| 2920 |
test_context: OrcaTestContext, |
| 2921 |
) -> None: |
| 2922 |
"""Test AXText.get_previous_line returns empty when current line is empty.""" |
| 2923 |
|
| 2924 |
self._setup_dependencies(test_context) |
| 2925 |
from orca.ax_text import AXText |
| 2926 |
from orca.ax_utilities_text import AXUtilitiesText |
| 2927 |
|
| 2928 |
mock_obj = test_context.Mock(spec=Atspi.Accessible) |
| 2929 |
|
| 2930 |
# Mock get_character_count to return 5 (not at end) |
| 2931 |
test_context.patch_object(AXText, "get_character_count", return_value=5) |
| 2932 |
|
| 2933 |
# Mock get_line_at_offset to return empty line not at end of text |
| 2934 |
def mock_get_line_at_offset(_obj, _offset): |
| 2935 |
return ("", 3, 3) |
| 2936 |
|
| 2937 |
test_context.patch_object(AXText, "get_line_at_offset", new=mock_get_line_at_offset) |
| 2938 |
|
| 2939 |
# Test: when current_line is empty (and not at end), should return empty |
| 2940 |
result = AXUtilitiesText.get_previous_line(mock_obj, 3) |
| 2941 |
assert result == ("", 0, 0) |
| 2942 |
|
| 2943 |
@pytest.mark.parametrize( |
| 2944 |
"text, expected_boundaries", |
| 2945 |
[ |
| 2946 |
pytest.param("", [], id="empty_string"), |
| 2947 |
pytest.param("Hello", [0, 5], id="no_sentence_ending"), |
| 2948 |
pytest.param("Hello.", [0, 6], id="single_sentence"), |
| 2949 |
pytest.param("Hello. World.", [0, 7, 13], id="two_sentences"), |
| 2950 |
pytest.param("Hello! World?", [0, 7, 13], id="exclamation_and_question"), |
| 2951 |
pytest.param("Hello... World.", [0, 9, 15], id="ellipsis"), |
| 2952 |
pytest.param("Hello. World.", [0, 8, 14], id="double_space_after_period"), |
| 2953 |
], |
| 2954 |
ids=lambda case: case if isinstance(case, str) else None, |
| 2955 |
) |
| 2956 |
def test_find_sentence_boundaries_basic( |
| 2957 |
self, |
| 2958 |
test_context: OrcaTestContext, |
| 2959 |
text: str, |
| 2960 |
expected_boundaries: list[int], |
| 2961 |
) -> None: |
| 2962 |
"""Test AXText._find_sentence_boundaries with basic text.""" |
| 2963 |
|
| 2964 |
self._setup_dependencies(test_context) |
| 2965 |
from orca.ax_text import AXText |
| 2966 |
|
| 2967 |
result = AXText._find_sentence_boundaries(text) |
| 2968 |
assert result == expected_boundaries |
| 2969 |
|
| 2970 |
@pytest.mark.parametrize( |
| 2971 |
"text, expected_boundaries", |
| 2972 |
[ |
| 2973 |
pytest.param("Hello.\ufffc", [0, 6, 7], id="sentence_ending_before_fffc"), |
| 2974 |
pytest.param("Hello. \ufffc", [0, 7, 8], id="sentence_ending_space_fffc"), |
| 2975 |
pytest.param( |
| 2976 |
"Test 1. Test 2. \ufffc Test 3.", |
| 2977 |
[0, 8, 16, 25], |
| 2978 |
id="fffc_between_sentences", |
| 2979 |
), |
| 2980 |
pytest.param( |
| 2981 |
"Test 1. Test 2. \ufffcTest 4.", |
| 2982 |
[0, 8, 16, 24], |
| 2983 |
id="nested_element_fffc_no_space_after", |
| 2984 |
), |
| 2985 |
], |
| 2986 |
ids=lambda case: case if isinstance(case, str) else None, |
| 2987 |
) |
| 2988 |
def test_find_sentence_boundaries_with_embedded_objects( |
| 2989 |
self, |
| 2990 |
test_context: OrcaTestContext, |
| 2991 |
text: str, |
| 2992 |
expected_boundaries: list[int], |
| 2993 |
) -> None: |
| 2994 |
"""Test AXText._find_sentence_boundaries with embedded object characters (FFFC). |
| 2995 |
|
| 2996 |
Embedded object characters represent child accessible objects in the text. |
| 2997 |
The boundary should be set AT the FFFC position so that the child object |
| 2998 |
can be properly traversed, rather than skipping over it. |
| 2999 |
""" |
| 3000 |
|
| 3001 |
self._setup_dependencies(test_context) |
| 3002 |
from orca.ax_text import AXText |
| 3003 |
|
| 3004 |
result = AXText._find_sentence_boundaries(text) |
| 3005 |
assert result == expected_boundaries |