| 1 |
# Unit tests for ax_selection.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 |
|
| 28 |
"""Unit tests for ax_selection.py methods.""" |
| 29 |
|
| 30 |
from __future__ import annotations |
| 31 |
|
| 32 |
from typing import TYPE_CHECKING |
| 33 |
|
| 34 |
import gi |
| 35 |
import pytest |
| 36 |
|
| 37 |
gi.require_version("Atspi", "2.0") |
| 38 |
from gi.repository import Atspi, GLib |
| 39 |
|
| 40 |
if TYPE_CHECKING: |
| 41 |
from unittest.mock import MagicMock |
| 42 |
|
| 43 |
from .orca_test_context import OrcaTestContext |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.unit |
| 47 |
class TestAXSelection: |
| 48 |
"""Test AXSelection class methods.""" |
| 49 |
|
| 50 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 51 |
"""Set up mocks for ax_selection dependencies.""" |
| 52 |
|
| 53 |
essential_modules = test_context.setup_shared_dependencies() |
| 54 |
|
| 55 |
debug_mock = essential_modules["orca.debug"] |
| 56 |
debug_mock.print_message = test_context.Mock() |
| 57 |
debug_mock.print_tokens = test_context.Mock() |
| 58 |
debug_mock.LEVEL_INFO = 800 |
| 59 |
|
| 60 |
ax_object_mock = essential_modules["orca.ax_object"] |
| 61 |
ax_object_mock.AXObject = test_context.Mock() |
| 62 |
ax_object_mock.AXObject.supports_selection = test_context.Mock(return_value=True) |
| 63 |
|
| 64 |
return essential_modules |
| 65 |
|
| 66 |
@pytest.mark.parametrize( |
| 67 |
"has_selection_support,side_effect,expected_result,expects_debug_call", |
| 68 |
[ |
| 69 |
pytest.param(True, lambda obj: 3, 3, True, id="with_support"), |
| 70 |
pytest.param(False, None, 0, False, id="without_support"), |
| 71 |
pytest.param( |
| 72 |
True, |
| 73 |
lambda obj: (_ for _ in ()).throw(GLib.GError("Test error")), |
| 74 |
0, |
| 75 |
True, |
| 76 |
id="glib_error", |
| 77 |
), |
| 78 |
], |
| 79 |
) |
| 80 |
def test_get_selected_child_count_scenarios( |
| 81 |
self, |
| 82 |
test_context: OrcaTestContext, |
| 83 |
has_selection_support: bool, |
| 84 |
side_effect, |
| 85 |
expected_result: int, |
| 86 |
expects_debug_call: bool, |
| 87 |
) -> None: |
| 88 |
"""Test AXSelection.get_selected_child_count with various scenarios.""" |
| 89 |
|
| 90 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 91 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 92 |
from orca import debug |
| 93 |
from orca.ax_object import AXObject |
| 94 |
from orca.ax_selection import AXSelection |
| 95 |
|
| 96 |
test_context.patch_object( |
| 97 |
AXObject, |
| 98 |
"supports_selection", |
| 99 |
return_value=has_selection_support, |
| 100 |
) |
| 101 |
if side_effect: |
| 102 |
test_context.patch_object( |
| 103 |
Atspi.Selection, |
| 104 |
"get_n_selected_children", |
| 105 |
side_effect=side_effect, |
| 106 |
) |
| 107 |
test_context.patch_object( |
| 108 |
debug, |
| 109 |
"print_tokens", |
| 110 |
new=essential_modules["orca.debug"].print_tokens, |
| 111 |
) |
| 112 |
result = AXSelection.get_selected_child_count(mock_accessible) |
| 113 |
assert result == expected_result |
| 114 |
if expects_debug_call: |
| 115 |
essential_modules["orca.debug"].print_tokens.assert_called() |
| 116 |
|
| 117 |
@pytest.mark.parametrize( |
| 118 |
"selected_count, index, expected_child", |
| 119 |
[ |
| 120 |
pytest.param(3, 0, "child_0", id="first_child"), |
| 121 |
pytest.param(3, 2, "child_2", id="last_child"), |
| 122 |
pytest.param(3, -1, "child_2", id="negative_index_last"), |
| 123 |
pytest.param(0, 0, None, id="no_selected_children"), |
| 124 |
pytest.param(3, 5, None, id="index_out_of_bounds"), |
| 125 |
pytest.param(3, -2, None, id="invalid_negative_index"), |
| 126 |
], |
| 127 |
) |
| 128 |
def test_get_selected_child(self, test_context, selected_count, index, expected_child) -> None: |
| 129 |
"""Test AXSelection.get_selected_child.""" |
| 130 |
|
| 131 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 132 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 133 |
from orca.ax_selection import AXSelection |
| 134 |
|
| 135 |
mock_children = {i: f"child_{i}" for i in range(selected_count)} |
| 136 |
|
| 137 |
def mock_get_selected_child(_obj, idx): |
| 138 |
return mock_children.get(idx) |
| 139 |
|
| 140 |
test_context.patch_object( |
| 141 |
AXSelection, |
| 142 |
"get_selected_child_count", |
| 143 |
return_value=selected_count, |
| 144 |
) |
| 145 |
test_context.patch_object( |
| 146 |
Atspi.Selection, |
| 147 |
"get_selected_child", |
| 148 |
side_effect=mock_get_selected_child, |
| 149 |
) |
| 150 |
result = AXSelection.get_selected_child(mock_accessible, index) |
| 151 |
assert result == expected_child |
| 152 |
if expected_child is not None: |
| 153 |
essential_modules["orca.debug"].print_tokens.assert_called() |
| 154 |
|
| 155 |
@pytest.mark.parametrize( |
| 156 |
"error_scenario,expected_result,expects_debug_call", |
| 157 |
[ |
| 158 |
pytest.param("returns_self", None, True, id="returns_self"), |
| 159 |
pytest.param("glib_error", None, True, id="glib_error"), |
| 160 |
], |
| 161 |
) |
| 162 |
def test_get_selected_child_error_scenarios( |
| 163 |
self, |
| 164 |
test_context: OrcaTestContext, |
| 165 |
error_scenario: str, |
| 166 |
expected_result, |
| 167 |
expects_debug_call: bool, |
| 168 |
) -> None: |
| 169 |
"""Test AXSelection.get_selected_child error scenarios.""" |
| 170 |
|
| 171 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 172 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 173 |
from orca import debug |
| 174 |
from orca.ax_selection import AXSelection |
| 175 |
|
| 176 |
test_context.patch_object(AXSelection, "get_selected_child_count", return_value=1) |
| 177 |
|
| 178 |
if error_scenario == "returns_self": |
| 179 |
test_context.patch_object( |
| 180 |
Atspi.Selection, |
| 181 |
"get_selected_child", |
| 182 |
return_value=mock_accessible, |
| 183 |
) |
| 184 |
elif error_scenario == "glib_error": |
| 185 |
|
| 186 |
def raise_glib_error(obj, idx): |
| 187 |
raise GLib.GError("Test error") |
| 188 |
|
| 189 |
test_context.patch_object( |
| 190 |
Atspi.Selection, |
| 191 |
"get_selected_child", |
| 192 |
side_effect=raise_glib_error, |
| 193 |
) |
| 194 |
|
| 195 |
test_context.patch_object( |
| 196 |
debug, |
| 197 |
"print_tokens", |
| 198 |
new=essential_modules["orca.debug"].print_tokens, |
| 199 |
) |
| 200 |
result = AXSelection.get_selected_child(mock_accessible, 0) |
| 201 |
assert result == expected_result |
| 202 |
if expects_debug_call: |
| 203 |
essential_modules["orca.debug"].print_tokens.assert_called() |