| 1 |
# Unit tests for ax_utilities_component.py methods. |
| 2 |
# |
| 3 |
# Copyright 2026 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=import-outside-toplevel |
| 22 |
# pylint: disable=too-many-arguments |
| 23 |
# pylint: disable=too-many-positional-arguments |
| 24 |
# pylint: disable=protected-access |
| 25 |
|
| 26 |
"""Unit tests for ax_utilities_component.py methods.""" |
| 27 |
|
| 28 |
from __future__ import annotations |
| 29 |
|
| 30 |
from typing import TYPE_CHECKING |
| 31 |
|
| 32 |
import gi |
| 33 |
import pytest |
| 34 |
|
| 35 |
gi.require_version("Atspi", "2.0") |
| 36 |
from gi.repository import Atspi |
| 37 |
|
| 38 |
if TYPE_CHECKING: |
| 39 |
from unittest.mock import MagicMock |
| 40 |
|
| 41 |
from .orca_test_context import OrcaTestContext |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.unit |
| 45 |
class TestAXUtilitiesComponent: |
| 46 |
"""Test AXUtilitiesComponent class methods.""" |
| 47 |
|
| 48 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 49 |
"""Set up mocks for ax_utilities_component dependencies.""" |
| 50 |
|
| 51 |
additional_modules = ["orca.ax_utilities_role", "orca.ax_component"] |
| 52 |
essential_modules = test_context.setup_shared_dependencies(additional_modules) |
| 53 |
|
| 54 |
debug_mock = essential_modules["orca.debug"] |
| 55 |
debug_mock.print_message = test_context.Mock() |
| 56 |
debug_mock.print_tokens = test_context.Mock() |
| 57 |
debug_mock.LEVEL_INFO = 800 |
| 58 |
|
| 59 |
ax_object_class_mock = test_context.Mock() |
| 60 |
ax_object_class_mock.supports_component = test_context.Mock(return_value=True) |
| 61 |
ax_object_class_mock.is_bogus = test_context.Mock(return_value=False) |
| 62 |
ax_object_class_mock.get_child_count = test_context.Mock(return_value=0) |
| 63 |
ax_object_class_mock.get_parent = test_context.Mock(return_value=None) |
| 64 |
ax_object_class_mock.get_index_in_parent = test_context.Mock(return_value=0) |
| 65 |
ax_object_class_mock.clear_cache = test_context.Mock() |
| 66 |
essential_modules["orca.ax_object"].AXObject = ax_object_class_mock |
| 67 |
|
| 68 |
role_class_mock = test_context.Mock() |
| 69 |
role_class_mock.is_menu = test_context.Mock(return_value=False) |
| 70 |
essential_modules["orca.ax_utilities_role"].AXUtilitiesRole = role_class_mock |
| 71 |
|
| 72 |
ax_component_class_mock = test_context.Mock() |
| 73 |
ax_component_class_mock.get_rect = test_context.Mock() |
| 74 |
ax_component_class_mock.object_contains_point = test_context.Mock(return_value=False) |
| 75 |
ax_component_class_mock.get_object_at_point = test_context.Mock(return_value=None) |
| 76 |
essential_modules["orca.ax_component"].AXComponent = ax_component_class_mock |
| 77 |
|
| 78 |
return essential_modules |
| 79 |
|
| 80 |
def test_get_center_point(self, test_context: OrcaTestContext) -> None: |
| 81 |
"""Test AXUtilitiesComponent.get_center_point.""" |
| 82 |
|
| 83 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 84 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 85 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 86 |
|
| 87 |
mock_rect = Atspi.Rect() |
| 88 |
mock_rect.x = 10 |
| 89 |
mock_rect.y = 20 |
| 90 |
mock_rect.width = 100 |
| 91 |
mock_rect.height = 60 |
| 92 |
essential_modules["orca.ax_component"].AXComponent.get_rect.return_value = mock_rect |
| 93 |
result = AXUtilitiesComponent.get_center_point(mock_accessible) |
| 94 |
assert result == (60.0, 50.0) |
| 95 |
|
| 96 |
@pytest.mark.parametrize( |
| 97 |
"width, height, expected_result", |
| 98 |
[ |
| 99 |
pytest.param(0, 0, True, id="zero_width_and_height"), |
| 100 |
pytest.param(100, 0, False, id="zero_height_only"), |
| 101 |
pytest.param(0, 50, False, id="zero_width_only"), |
| 102 |
pytest.param(100, 50, False, id="non_zero_dimensions"), |
| 103 |
], |
| 104 |
) |
| 105 |
def test_has_no_size( |
| 106 |
self, |
| 107 |
test_context: OrcaTestContext, |
| 108 |
width: int, |
| 109 |
height: int, |
| 110 |
expected_result: bool, |
| 111 |
) -> None: |
| 112 |
"""Test AXUtilitiesComponent.has_no_size.""" |
| 113 |
|
| 114 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 115 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 116 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 117 |
|
| 118 |
mock_rect = Atspi.Rect() |
| 119 |
mock_rect.width = width |
| 120 |
mock_rect.height = height |
| 121 |
essential_modules["orca.ax_component"].AXComponent.get_rect.return_value = mock_rect |
| 122 |
result = AXUtilitiesComponent.has_no_size(mock_accessible) |
| 123 |
assert result is expected_result |
| 124 |
|
| 125 |
@pytest.mark.parametrize( |
| 126 |
"x, y, width, height, expected_result", |
| 127 |
[ |
| 128 |
pytest.param(0, 0, 0, 0, True, id="all_zero_dimensions"), |
| 129 |
pytest.param(10, 20, 0, 0, True, id="zero_size_non_zero_position"), |
| 130 |
pytest.param(-1, -1, -1, -1, True, id="all_negative_one"), |
| 131 |
pytest.param(10, 20, 100, 50, False, id="valid_dimensions"), |
| 132 |
pytest.param(10, 20, -2, 50, True, id="invalid_negative_width"), |
| 133 |
pytest.param(10, 20, 100, -5, True, id="invalid_negative_height"), |
| 134 |
], |
| 135 |
) |
| 136 |
def test_has_no_size_or_invalid_rect( |
| 137 |
self, |
| 138 |
test_context: OrcaTestContext, |
| 139 |
x: int, |
| 140 |
y: int, |
| 141 |
width: int, |
| 142 |
height: int, |
| 143 |
expected_result: bool, |
| 144 |
) -> None: |
| 145 |
"""Test AXUtilitiesComponent.has_no_size_or_invalid_rect.""" |
| 146 |
|
| 147 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 148 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 149 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 150 |
|
| 151 |
mock_rect = Atspi.Rect() |
| 152 |
mock_rect.x = x |
| 153 |
mock_rect.y = y |
| 154 |
mock_rect.width = width |
| 155 |
mock_rect.height = height |
| 156 |
essential_modules["orca.ax_component"].AXComponent.get_rect.return_value = mock_rect |
| 157 |
result = AXUtilitiesComponent.has_no_size_or_invalid_rect(mock_accessible) |
| 158 |
assert result is expected_result |
| 159 |
|
| 160 |
@pytest.mark.parametrize( |
| 161 |
"x, y, width, height, expected_result", |
| 162 |
[ |
| 163 |
pytest.param(0, 0, 0, 0, True, id="all_zeros"), |
| 164 |
pytest.param(0, 0, 100, 50, False, id="zero_position_valid_size"), |
| 165 |
pytest.param(10, 20, 0, 0, False, id="valid_position_zero_size"), |
| 166 |
pytest.param(10, 20, 100, 50, False, id="all_non_zero"), |
| 167 |
], |
| 168 |
) |
| 169 |
def test_is_empty_rect( |
| 170 |
self, |
| 171 |
test_context: OrcaTestContext, |
| 172 |
x: int, |
| 173 |
y: int, |
| 174 |
width: int, |
| 175 |
height: int, |
| 176 |
expected_result: bool, |
| 177 |
) -> None: |
| 178 |
"""Test AXUtilitiesComponent.is_empty_rect.""" |
| 179 |
|
| 180 |
self._setup_dependencies(test_context) |
| 181 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 182 |
|
| 183 |
rect = Atspi.Rect() |
| 184 |
rect.x = x |
| 185 |
rect.y = y |
| 186 |
rect.width = width |
| 187 |
rect.height = height |
| 188 |
result = AXUtilitiesComponent.is_empty_rect(rect) |
| 189 |
assert result is expected_result |
| 190 |
|
| 191 |
def test_is_same_rect(self, test_context: OrcaTestContext) -> None: |
| 192 |
"""Test AXUtilitiesComponent.is_same_rect.""" |
| 193 |
|
| 194 |
self._setup_dependencies(test_context) |
| 195 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 196 |
|
| 197 |
rect1 = Atspi.Rect() |
| 198 |
rect1.x = 10 |
| 199 |
rect1.y = 20 |
| 200 |
rect1.width = 100 |
| 201 |
rect1.height = 50 |
| 202 |
|
| 203 |
rect2 = Atspi.Rect() |
| 204 |
rect2.x = 10 |
| 205 |
rect2.y = 20 |
| 206 |
rect2.width = 100 |
| 207 |
rect2.height = 50 |
| 208 |
assert AXUtilitiesComponent.is_same_rect(rect1, rect2) is True |
| 209 |
|
| 210 |
rect2.x = 15 |
| 211 |
assert AXUtilitiesComponent.is_same_rect(rect1, rect2) is False |
| 212 |
|
| 213 |
rect2.x = 10 |
| 214 |
rect2.width = 200 |
| 215 |
assert AXUtilitiesComponent.is_same_rect(rect1, rect2) is False |
| 216 |
|
| 217 |
@pytest.mark.parametrize( |
| 218 |
"rect1_coords, rect2_coords, pixel_delta, expected", |
| 219 |
[ |
| 220 |
pytest.param((10, 20, 100, 30), (10, 20, 100, 30), 5, True, id="same_rects"), |
| 221 |
pytest.param( |
| 222 |
(50, 35, 0, 0), |
| 223 |
(10, 20, 100, 30), |
| 224 |
5, |
| 225 |
True, |
| 226 |
id="point_in_rect_vertical_range", |
| 227 |
), |
| 228 |
pytest.param((50, 10, 0, 0), (10, 20, 100, 30), 5, False, id="point_above_rect"), |
| 229 |
pytest.param((10, 20, 100, 30), (50, 35, 0, 0), 5, True, id="rect_contains_point"), |
| 230 |
pytest.param((10, 20, 100, 30), (10, 100, 100, 30), 5, False, id="no_vertical_overlap"), |
| 231 |
pytest.param((10, 20, 100, 30), (10, 22, 100, 30), 5, True, id="overlapping_same_line"), |
| 232 |
pytest.param((10, 20, 100, 30), (10, 40, 100, 30), 5, False, id="midpoints_too_far"), |
| 233 |
pytest.param( |
| 234 |
(10, 20, 100, 30), |
| 235 |
(10, 40, 100, 30), |
| 236 |
20, |
| 237 |
True, |
| 238 |
id="midpoints_within_larger_delta", |
| 239 |
), |
| 240 |
], |
| 241 |
) |
| 242 |
def test_rects_are_on_same_line( |
| 243 |
self, |
| 244 |
test_context: OrcaTestContext, |
| 245 |
rect1_coords: tuple[int, int, int, int], |
| 246 |
rect2_coords: tuple[int, int, int, int], |
| 247 |
pixel_delta: int, |
| 248 |
expected: bool, |
| 249 |
) -> None: |
| 250 |
"""Test AXUtilitiesComponent.rects_are_on_same_line.""" |
| 251 |
|
| 252 |
self._setup_dependencies(test_context) |
| 253 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 254 |
|
| 255 |
rect1 = Atspi.Rect() |
| 256 |
rect1.x, rect1.y, rect1.width, rect1.height = rect1_coords |
| 257 |
rect2 = Atspi.Rect() |
| 258 |
rect2.x, rect2.y, rect2.width, rect2.height = rect2_coords |
| 259 |
|
| 260 |
result = AXUtilitiesComponent.rects_are_on_same_line(rect1, rect2, pixel_delta) |
| 261 |
assert result is expected |
| 262 |
|
| 263 |
def test_get_rect_intersection(self, test_context: OrcaTestContext) -> None: |
| 264 |
"""Test AXUtilitiesComponent.get_rect_intersection.""" |
| 265 |
|
| 266 |
self._setup_dependencies(test_context) |
| 267 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 268 |
|
| 269 |
rect1 = Atspi.Rect() |
| 270 |
rect1.x = 10 |
| 271 |
rect1.y = 20 |
| 272 |
rect1.width = 100 |
| 273 |
rect1.height = 50 |
| 274 |
rect2 = Atspi.Rect() |
| 275 |
rect2.x = 50 |
| 276 |
rect2.y = 30 |
| 277 |
rect2.width = 80 |
| 278 |
rect2.height = 60 |
| 279 |
result = AXUtilitiesComponent.get_rect_intersection(rect1, rect2) |
| 280 |
assert result.x == 50 |
| 281 |
assert result.y == 30 |
| 282 |
assert result.width == 60 |
| 283 |
assert result.height == 40 |
| 284 |
|
| 285 |
@pytest.mark.parametrize( |
| 286 |
"case", |
| 287 |
[ |
| 288 |
{ |
| 289 |
"id": "no_overlap", |
| 290 |
"rect1_coords": (10, 20, 50, 30), |
| 291 |
"rect2_coords": (100, 200, 50, 30), |
| 292 |
"expected_result": (0, 0, 0, 0), |
| 293 |
}, |
| 294 |
{ |
| 295 |
"id": "huge_rect", |
| 296 |
"rect1_coords": (100, 100, 200, 150), |
| 297 |
"rect2_coords": (50, 50, 2147483647, 2147483647), |
| 298 |
"expected_result": (100, 100, 200, 150), |
| 299 |
}, |
| 300 |
], |
| 301 |
ids=lambda case: case["id"], |
| 302 |
) |
| 303 |
def test_get_rect_intersection_scenarios( |
| 304 |
self, |
| 305 |
test_context: OrcaTestContext, |
| 306 |
case: dict, |
| 307 |
) -> None: |
| 308 |
"""Test AXUtilitiesComponent.get_rect_intersection with different rectangle scenarios.""" |
| 309 |
|
| 310 |
self._setup_dependencies(test_context) |
| 311 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 312 |
|
| 313 |
rect1 = Atspi.Rect() |
| 314 |
rect1.x, rect1.y, rect1.width, rect1.height = case["rect1_coords"] |
| 315 |
rect2 = Atspi.Rect() |
| 316 |
rect2.x, rect2.y, rect2.width, rect2.height = case["rect2_coords"] |
| 317 |
|
| 318 |
result = AXUtilitiesComponent.get_rect_intersection(rect1, rect2) |
| 319 |
expected_x, expected_y, expected_width, expected_height = case["expected_result"] |
| 320 |
|
| 321 |
assert result.x == expected_x |
| 322 |
assert result.y == expected_y |
| 323 |
assert result.width == expected_width |
| 324 |
assert result.height == expected_height |
| 325 |
|
| 326 |
@pytest.mark.parametrize( |
| 327 |
"obj_coords,test_coords,expected_result", |
| 328 |
[ |
| 329 |
pytest.param((10, 20, 100, 50), (50, 30, 80, 60), True, id="with_intersection"), |
| 330 |
pytest.param((10, 20, 50, 30), (100, 200, 50, 30), False, id="no_intersection"), |
| 331 |
], |
| 332 |
) |
| 333 |
def test_object_intersects_rect( |
| 334 |
self, |
| 335 |
test_context: OrcaTestContext, |
| 336 |
obj_coords, |
| 337 |
test_coords, |
| 338 |
expected_result, |
| 339 |
) -> None: |
| 340 |
"""Test AXUtilitiesComponent.object_intersects_rect intersection scenarios.""" |
| 341 |
|
| 342 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 343 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 344 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 345 |
|
| 346 |
obj_rect = Atspi.Rect() |
| 347 |
obj_rect.x, obj_rect.y, obj_rect.width, obj_rect.height = obj_coords |
| 348 |
test_rect = Atspi.Rect() |
| 349 |
test_rect.x, test_rect.y, test_rect.width, test_rect.height = test_coords |
| 350 |
|
| 351 |
essential_modules["orca.ax_component"].AXComponent.get_rect.return_value = obj_rect |
| 352 |
result = AXUtilitiesComponent.object_intersects_rect(mock_accessible, test_rect) |
| 353 |
assert result is expected_result |
| 354 |
|
| 355 |
@pytest.mark.parametrize( |
| 356 |
"x, y, width, height, child_count, is_menu, expected_result", |
| 357 |
[ |
| 358 |
pytest.param(15000, 20, 100, 50, 0, False, True, id="extreme_x_position"), |
| 359 |
pytest.param(20, 15000, 100, 50, 0, False, True, id="extreme_y_position"), |
| 360 |
pytest.param(20, 30, 0, 0, 0, False, True, id="zero_size_no_children"), |
| 361 |
pytest.param(20, 30, 0, 0, 2, False, False, id="zero_size_has_children"), |
| 362 |
pytest.param(20, 30, 0, 0, 0, True, True, id="zero_size_menu_role"), |
| 363 |
pytest.param(-50, -60, 40, 30, 0, False, True, id="completely_offscreen"), |
| 364 |
pytest.param(20, 30, 100, 50, 0, False, False, id="normal_onscreen_object"), |
| 365 |
], |
| 366 |
) |
| 367 |
def test_object_is_off_screen( |
| 368 |
self, |
| 369 |
test_context: OrcaTestContext, |
| 370 |
x: int, |
| 371 |
y: int, |
| 372 |
width: int, |
| 373 |
height: int, |
| 374 |
child_count: int, |
| 375 |
is_menu: bool, |
| 376 |
expected_result: bool, |
| 377 |
) -> None: |
| 378 |
"""Test AXUtilitiesComponent.object_is_off_screen.""" |
| 379 |
|
| 380 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 381 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 382 |
|
| 383 |
essential_modules["orca.ax_object"].AXObject.get_child_count = test_context.Mock( |
| 384 |
return_value=child_count, |
| 385 |
) |
| 386 |
essential_modules["orca.ax_utilities_role"].AXUtilitiesRole.is_menu = test_context.Mock( |
| 387 |
return_value=is_menu, |
| 388 |
) |
| 389 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 390 |
|
| 391 |
mock_rect = Atspi.Rect() |
| 392 |
mock_rect.x = x |
| 393 |
mock_rect.y = y |
| 394 |
mock_rect.width = width |
| 395 |
mock_rect.height = height |
| 396 |
essential_modules["orca.ax_component"].AXComponent.get_rect.return_value = mock_rect |
| 397 |
result = AXUtilitiesComponent.object_is_off_screen(mock_accessible) |
| 398 |
assert result is expected_result |
| 399 |
|
| 400 |
def test_objects_have_same_rect(self, test_context: OrcaTestContext) -> None: |
| 401 |
"""Test AXUtilitiesComponent.objects_have_same_rect.""" |
| 402 |
|
| 403 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 404 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 405 |
|
| 406 |
obj1 = test_context.Mock(spec=Atspi.Accessible) |
| 407 |
obj2 = test_context.Mock(spec=Atspi.Accessible) |
| 408 |
rect1 = Atspi.Rect() |
| 409 |
rect1.x = 10 |
| 410 |
rect1.y = 20 |
| 411 |
rect1.width = 100 |
| 412 |
rect1.height = 50 |
| 413 |
rect2 = Atspi.Rect() |
| 414 |
rect2.x = 10 |
| 415 |
rect2.y = 20 |
| 416 |
rect2.width = 100 |
| 417 |
rect2.height = 50 |
| 418 |
|
| 419 |
def get_rect_for_obj(obj): |
| 420 |
return rect1 if obj == obj1 else rect2 |
| 421 |
|
| 422 |
essential_modules["orca.ax_component"].AXComponent.get_rect.side_effect = get_rect_for_obj |
| 423 |
|
| 424 |
result = AXUtilitiesComponent.objects_have_same_rect(obj1, obj2) |
| 425 |
assert result is True |
| 426 |
|
| 427 |
rect2.width = 200 |
| 428 |
result = AXUtilitiesComponent.objects_have_same_rect(obj1, obj2) |
| 429 |
assert result is False |
| 430 |
|
| 431 |
def test_objects_overlap(self, test_context: OrcaTestContext) -> None: |
| 432 |
"""Test AXUtilitiesComponent.objects_overlap.""" |
| 433 |
|
| 434 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 435 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 436 |
|
| 437 |
obj1 = test_context.Mock(spec=Atspi.Accessible) |
| 438 |
obj2 = test_context.Mock(spec=Atspi.Accessible) |
| 439 |
rect1 = Atspi.Rect() |
| 440 |
rect1.x = 10 |
| 441 |
rect1.y = 20 |
| 442 |
rect1.width = 100 |
| 443 |
rect1.height = 50 |
| 444 |
rect2 = Atspi.Rect() |
| 445 |
rect2.x = 50 |
| 446 |
rect2.y = 30 |
| 447 |
rect2.width = 80 |
| 448 |
rect2.height = 60 |
| 449 |
|
| 450 |
def get_rect_for_obj(obj): |
| 451 |
return rect1 if obj == obj1 else rect2 |
| 452 |
|
| 453 |
essential_modules["orca.ax_component"].AXComponent.get_rect.side_effect = get_rect_for_obj |
| 454 |
result = AXUtilitiesComponent.objects_overlap(obj1, obj2) |
| 455 |
assert result is True |
| 456 |
|
| 457 |
def test_on_same_line(self, test_context: OrcaTestContext) -> None: |
| 458 |
"""Test AXUtilitiesComponent.on_same_line.""" |
| 459 |
|
| 460 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 461 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 462 |
|
| 463 |
obj1 = test_context.Mock(spec=Atspi.Accessible) |
| 464 |
obj2 = test_context.Mock(spec=Atspi.Accessible) |
| 465 |
|
| 466 |
rect1 = Atspi.Rect() |
| 467 |
rect1.x = 10 |
| 468 |
rect1.y = 20 |
| 469 |
rect1.width = 50 |
| 470 |
rect1.height = 30 |
| 471 |
rect2 = Atspi.Rect() |
| 472 |
rect2.x = 100 |
| 473 |
rect2.y = 22 |
| 474 |
rect2.width = 60 |
| 475 |
rect2.height = 26 |
| 476 |
|
| 477 |
def get_rect_for_obj(obj): |
| 478 |
return rect1 if obj == obj1 else rect2 |
| 479 |
|
| 480 |
essential_modules["orca.ax_component"].AXComponent.get_rect.side_effect = get_rect_for_obj |
| 481 |
result = AXUtilitiesComponent.on_same_line(obj1, obj2) |
| 482 |
assert result is True |
| 483 |
|
| 484 |
rect2.height = 200 |
| 485 |
result = AXUtilitiesComponent.on_same_line(obj1, obj2) |
| 486 |
assert result is False |
| 487 |
|
| 488 |
def test_on_same_line_with_delta(self, test_context: OrcaTestContext) -> None: |
| 489 |
"""Test AXUtilitiesComponent.on_same_line with delta parameter.""" |
| 490 |
|
| 491 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 492 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 493 |
|
| 494 |
obj1 = test_context.Mock(spec=Atspi.Accessible) |
| 495 |
obj2 = test_context.Mock(spec=Atspi.Accessible) |
| 496 |
rect1 = Atspi.Rect() |
| 497 |
rect1.x = 10 |
| 498 |
rect1.y = 20 |
| 499 |
rect1.width = 50 |
| 500 |
rect1.height = 20 |
| 501 |
rect2 = Atspi.Rect() |
| 502 |
rect2.x = 100 |
| 503 |
rect2.y = 30 |
| 504 |
rect2.width = 60 |
| 505 |
rect2.height = 20 |
| 506 |
|
| 507 |
def get_rect_for_obj(obj): |
| 508 |
return rect1 if obj == obj1 else rect2 |
| 509 |
|
| 510 |
essential_modules["orca.ax_component"].AXComponent.get_rect.side_effect = get_rect_for_obj |
| 511 |
|
| 512 |
result = AXUtilitiesComponent.on_same_line(obj1, obj2, delta=5) |
| 513 |
assert result is False |
| 514 |
|
| 515 |
result = AXUtilitiesComponent.on_same_line(obj1, obj2, delta=15) |
| 516 |
assert result is True |
| 517 |
|
| 518 |
def test_sort_objects_by_size(self, test_context: OrcaTestContext) -> None: |
| 519 |
"""Test AXUtilitiesComponent.sort_objects_by_size.""" |
| 520 |
|
| 521 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 522 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 523 |
|
| 524 |
obj_small = test_context.Mock(spec=Atspi.Accessible) |
| 525 |
obj_medium = test_context.Mock(spec=Atspi.Accessible) |
| 526 |
obj_large = test_context.Mock(spec=Atspi.Accessible) |
| 527 |
|
| 528 |
rect_small = Atspi.Rect() |
| 529 |
rect_small.width = 10 |
| 530 |
rect_small.height = 10 |
| 531 |
rect_medium = Atspi.Rect() |
| 532 |
rect_medium.width = 20 |
| 533 |
rect_medium.height = 15 |
| 534 |
rect_large = Atspi.Rect() |
| 535 |
rect_large.width = 30 |
| 536 |
rect_large.height = 20 |
| 537 |
|
| 538 |
def get_rect_for_obj(obj): |
| 539 |
if obj == obj_small: |
| 540 |
return rect_small |
| 541 |
if obj == obj_medium: |
| 542 |
return rect_medium |
| 543 |
return rect_large |
| 544 |
|
| 545 |
essential_modules["orca.ax_component"].AXComponent.get_rect.side_effect = get_rect_for_obj |
| 546 |
objects = [obj_large, obj_small, obj_medium] |
| 547 |
result = AXUtilitiesComponent.sort_objects_by_size(objects) |
| 548 |
assert result == [obj_small, obj_medium, obj_large] |
| 549 |
|
| 550 |
def test_sort_objects_by_position(self, test_context: OrcaTestContext) -> None: |
| 551 |
"""Test AXUtilitiesComponent.sort_objects_by_position.""" |
| 552 |
|
| 553 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 554 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 555 |
|
| 556 |
obj_top_left = test_context.Mock(spec=Atspi.Accessible) |
| 557 |
obj_top_right = test_context.Mock(spec=Atspi.Accessible) |
| 558 |
obj_bottom_left = test_context.Mock(spec=Atspi.Accessible) |
| 559 |
rect_top_left = Atspi.Rect() |
| 560 |
rect_top_left.x = 10 |
| 561 |
rect_top_left.y = 10 |
| 562 |
rect_top_right = Atspi.Rect() |
| 563 |
rect_top_right.x = 100 |
| 564 |
rect_top_right.y = 10 |
| 565 |
rect_bottom_left = Atspi.Rect() |
| 566 |
rect_bottom_left.x = 10 |
| 567 |
rect_bottom_left.y = 100 |
| 568 |
|
| 569 |
def get_rect_for_obj(obj): |
| 570 |
if obj == obj_top_left: |
| 571 |
return rect_top_left |
| 572 |
if obj == obj_top_right: |
| 573 |
return rect_top_right |
| 574 |
return rect_bottom_left |
| 575 |
|
| 576 |
essential_modules["orca.ax_component"].AXComponent.get_rect.side_effect = get_rect_for_obj |
| 577 |
objects = [obj_bottom_left, obj_top_right, obj_top_left] |
| 578 |
result = AXUtilitiesComponent.sort_objects_by_position(objects) |
| 579 |
assert result == [obj_top_left, obj_top_right, obj_bottom_left] |
| 580 |
|
| 581 |
def test_sort_objects_by_position_same_coordinates(self, test_context: OrcaTestContext) -> None: |
| 582 |
"""Test AXUtilitiesComponent.sort_objects_by_position when objects have same coordinates.""" |
| 583 |
|
| 584 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 585 |
|
| 586 |
obj1 = test_context.Mock(spec=Atspi.Accessible) |
| 587 |
obj2 = test_context.Mock(spec=Atspi.Accessible) |
| 588 |
parent = test_context.Mock(spec=Atspi.Accessible) |
| 589 |
|
| 590 |
essential_modules["orca.ax_object"].AXObject.get_parent = test_context.Mock( |
| 591 |
return_value=parent, |
| 592 |
) |
| 593 |
essential_modules["orca.ax_object"].AXObject.get_index_in_parent = test_context.Mock( |
| 594 |
side_effect=lambda obj: 0 if obj == obj1 else 1, |
| 595 |
) |
| 596 |
from orca.ax_utilities_component import AXUtilitiesComponent |
| 597 |
|
| 598 |
same_rect = Atspi.Rect() |
| 599 |
same_rect.x = 50 |
| 600 |
same_rect.y = 50 |
| 601 |
essential_modules["orca.ax_component"].AXComponent.get_rect.return_value = same_rect |
| 602 |
objects = [obj2, obj1] |
| 603 |
result = AXUtilitiesComponent.sort_objects_by_position(objects) |
| 604 |
assert result == [obj1, obj2] |