| 1 |
# Unit tests for ax_collection.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=wrong-import-position |
| 22 |
# pylint: disable=import-outside-toplevel |
| 23 |
|
| 24 |
"""Unit tests for ax_collection.py methods.""" |
| 25 |
|
| 26 |
from __future__ import annotations |
| 27 |
|
| 28 |
from typing import TYPE_CHECKING |
| 29 |
|
| 30 |
import gi |
| 31 |
import pytest |
| 32 |
|
| 33 |
gi.require_version("Atspi", "2.0") |
| 34 |
from gi.repository import Atspi, GLib |
| 35 |
|
| 36 |
if TYPE_CHECKING: |
| 37 |
from unittest.mock import MagicMock |
| 38 |
|
| 39 |
from .orca_test_context import OrcaTestContext |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.unit |
| 43 |
class TestAXCollection: |
| 44 |
"""Test AXCollection class methods.""" |
| 45 |
|
| 46 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 47 |
"""Set up mocks for ax_collection dependencies.""" |
| 48 |
|
| 49 |
essential_modules = test_context.setup_shared_dependencies([]) |
| 50 |
|
| 51 |
debug_mock = essential_modules["orca.debug"] |
| 52 |
debug_mock.print_message = test_context.Mock() |
| 53 |
debug_mock.print_tokens = test_context.Mock() |
| 54 |
debug_mock.LEVEL_INFO = 800 |
| 55 |
|
| 56 |
ax_object_class_mock = test_context.Mock() |
| 57 |
ax_object_class_mock.supports_collection = test_context.Mock(return_value=True) |
| 58 |
essential_modules["orca.ax_object"].AXObject = ax_object_class_mock |
| 59 |
return essential_modules |
| 60 |
|
| 61 |
def test_create_match_rule_with_colon_values(self, test_context: OrcaTestContext) -> None: |
| 62 |
"""Test create_match_rule with attributes that contain colons in values.""" |
| 63 |
|
| 64 |
self._setup_dependencies(test_context) |
| 65 |
from orca.ax_collection import AXCollection |
| 66 |
|
| 67 |
mock_rule = test_context.Mock(spec=Atspi.MatchRule) |
| 68 |
mock_match_rule_new = test_context.patch( |
| 69 |
"gi.repository.Atspi.MatchRule.new", |
| 70 |
return_value=mock_rule, |
| 71 |
) |
| 72 |
|
| 73 |
attributes = ["url:http://example.com:8080"] |
| 74 |
result = AXCollection.create_match_rule(attributes=attributes) |
| 75 |
|
| 76 |
expected_attrs = {"url": r"http\://example.com\:8080"} |
| 77 |
mock_match_rule_new.assert_called_once() |
| 78 |
args = mock_match_rule_new.call_args[0] |
| 79 |
assert args[2] == expected_attrs |
| 80 |
assert result == mock_rule |
| 81 |
|
| 82 |
def test_create_match_rule_with_duplicate_keys(self, test_context: OrcaTestContext) -> None: |
| 83 |
"""Test create_match_rule with duplicate attribute keys.""" |
| 84 |
|
| 85 |
self._setup_dependencies(test_context) |
| 86 |
from orca.ax_collection import AXCollection |
| 87 |
|
| 88 |
mock_rule = test_context.Mock(spec=Atspi.MatchRule) |
| 89 |
mock_match_rule_new = test_context.patch( |
| 90 |
"gi.repository.Atspi.MatchRule.new", |
| 91 |
return_value=mock_rule, |
| 92 |
) |
| 93 |
|
| 94 |
attributes = ["class:button", "class:primary"] |
| 95 |
result = AXCollection.create_match_rule( |
| 96 |
attributes=attributes, |
| 97 |
attribute_match_type=Atspi.CollectionMatchType.ANY, |
| 98 |
) |
| 99 |
|
| 100 |
expected_attrs = {"class": "button:primary"} |
| 101 |
mock_match_rule_new.assert_called_once() |
| 102 |
args = mock_match_rule_new.call_args[0] |
| 103 |
assert args[2] == expected_attrs |
| 104 |
assert result == mock_rule |
| 105 |
|
| 106 |
def test_create_match_rule_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 107 |
"""Test create_match_rule when Atspi.MatchRule.new raises GLib.GError.""" |
| 108 |
|
| 109 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 110 |
from orca.ax_collection import AXCollection |
| 111 |
|
| 112 |
test_context.patch( |
| 113 |
"gi.repository.Atspi.MatchRule.new", |
| 114 |
side_effect=GLib.GError("Test error"), |
| 115 |
) |
| 116 |
result = AXCollection.create_match_rule() |
| 117 |
|
| 118 |
essential_modules["orca.debug"].print_tokens.assert_called_once() |
| 119 |
args = essential_modules["orca.debug"].print_tokens.call_args[0] |
| 120 |
assert "AXCollection: Exception in create_match_rule:" in args[1][0] |
| 121 |
assert result is None |
| 122 |
|
| 123 |
def test_get_all_matches_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 124 |
"""Test get_all_matches when Atspi.Collection.get_matches raises GLib.GError.""" |
| 125 |
|
| 126 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 127 |
from orca.ax_collection import AXCollection |
| 128 |
|
| 129 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 130 |
essential_modules["orca.ax_object"].AXObject.supports_collection.return_value = True |
| 131 |
test_context.patch( |
| 132 |
"gi.repository.Atspi.Collection.get_matches", |
| 133 |
side_effect=GLib.GError("Collection error"), |
| 134 |
) |
| 135 |
mock_rule = test_context.Mock(spec=Atspi.MatchRule) |
| 136 |
result = AXCollection.get_all_matches(mock_accessible, mock_rule) |
| 137 |
|
| 138 |
essential_modules["orca.debug"].print_tokens.assert_called_once() |
| 139 |
args = essential_modules["orca.debug"].print_tokens.call_args[0] |
| 140 |
assert "AXCollection: Exception in get_all_matches:" in args[1][0] |
| 141 |
assert result == [] |
| 142 |
|
| 143 |
def test_get_first_match_with_glib_error(self, test_context: OrcaTestContext) -> None: |
| 144 |
"""Test get_first_match when Atspi.Collection.get_matches raises GLib.GError.""" |
| 145 |
|
| 146 |
essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context) |
| 147 |
from orca.ax_collection import AXCollection |
| 148 |
|
| 149 |
mock_accessible = test_context.Mock(spec=Atspi.Accessible) |
| 150 |
essential_modules["orca.ax_object"].AXObject.supports_collection.return_value = True |
| 151 |
test_context.patch( |
| 152 |
"gi.repository.Atspi.Collection.get_matches", |
| 153 |
side_effect=GLib.GError("First match error"), |
| 154 |
) |
| 155 |
mock_rule = test_context.Mock(spec=Atspi.MatchRule) |
| 156 |
result = AXCollection.get_first_match(mock_accessible, mock_rule) |
| 157 |
|
| 158 |
essential_modules["orca.debug"].print_tokens.assert_called_once() |
| 159 |
args = essential_modules["orca.debug"].print_tokens.call_args[0] |
| 160 |
assert "AXCollection: Exception in get_first_match:" in args[1][0] |
| 161 |
assert result is None |
| 162 |
|
| 163 |
def test_create_match_rule_with_states(self, test_context: OrcaTestContext) -> None: |
| 164 |
"""Test create_match_rule with states parameter uses real StateSet.""" |
| 165 |
|
| 166 |
self._setup_dependencies(test_context) |
| 167 |
from orca.ax_collection import AXCollection |
| 168 |
|
| 169 |
mock_rule = test_context.Mock(spec=Atspi.MatchRule) |
| 170 |
mock_match_rule_new = test_context.patch( |
| 171 |
"gi.repository.Atspi.MatchRule.new", |
| 172 |
return_value=mock_rule, |
| 173 |
) |
| 174 |
|
| 175 |
states = [Atspi.StateType.FOCUSED, Atspi.StateType.SELECTED] |
| 176 |
result = AXCollection.create_match_rule(states=states) |
| 177 |
|
| 178 |
mock_match_rule_new.assert_called_once() |
| 179 |
args = mock_match_rule_new.call_args[0] |
| 180 |
state_set = args[0] # First argument should be the StateSet |
| 181 |
assert isinstance(state_set, Atspi.StateSet) |
| 182 |
assert result == mock_rule |