Debian Codegraph @

tests/unit_tests/test_ax_action.py

raw
1 # Unit tests for ax_action.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
23 """Unit tests for ax_action.py methods."""
24
25 from __future__ import annotations
26
27 import sys
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 TestAXAction:
44 """Test AXAction class methods."""
45
46 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]:
47 """Set up mocks for ax_action dependencies."""
48
49 core_modules = [
50 "orca.debug",
51 "orca.messages",
52 "orca.input_event",
53 "orca.keybindings",
54 "orca.cmdnames",
55 "orca.dbus_service",
56 "orca.script_manager",
57 "orca.orca_i18n",
58 "orca.guilabels",
59 "orca.text_attribute_names",
60 "orca.focus_manager",
61 "orca.braille",
62 "orca.keynames",
63 ]
64
65 essential_modules = {}
66 for module_name in core_modules:
67 mock_module = test_context.Mock()
68 test_context.patch_module(module_name, mock_module)
69 essential_modules[module_name] = mock_module
70
71 test_context.configure_shared_module_behaviors(essential_modules)
72
73 return essential_modules
74
75 def test_get_n_actions_with_unsupported_object(self, test_context: OrcaTestContext) -> None:
76 """Test AXAction.get_n_actions with object that doesn't support actions."""
77
78 self._setup_dependencies(test_context)
79 from orca.ax_action import AXAction
80 from orca.ax_object import AXObject
81
82 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
83 test_context.patch_object(AXObject, "supports_action", return_value=False)
84 result = AXAction.get_n_actions(mock_accessible)
85 assert result == 0
86
87 def test_get_n_actions_with_supported_object(self, test_context: OrcaTestContext) -> None:
88 """Test AXAction.get_n_actions with object that supports actions."""
89
90 self._setup_dependencies(test_context)
91 from orca.ax_action import AXAction
92 from orca.ax_object import AXObject
93
94 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
95 test_context.patch_object(AXObject, "supports_action", return_value=True)
96 test_context.patch_object(Atspi.Action, "get_n_actions", return_value=3)
97 result = AXAction.get_n_actions(mock_accessible)
98 assert result == 3
99
100 def test_get_n_actions_with_glib_error(self, test_context: OrcaTestContext) -> None:
101 """Test AXAction.get_n_actions handles GLib.GError."""
102
103 self._setup_dependencies(test_context)
104 from orca.ax_action import AXAction
105 from orca.ax_object import AXObject
106
107 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
108
109 def raise_glib_error(_obj) -> None:
110 raise GLib.GError("Test n actions error")
111
112 test_context.patch_object(AXObject, "supports_action", return_value=True)
113 test_context.patch_object(Atspi.Action, "get_n_actions", side_effect=raise_glib_error)
114 handle_error_mock = test_context.Mock()
115 test_context.patch_object(AXObject, "handle_error", new=handle_error_mock)
116 result = AXAction.get_n_actions(mock_accessible)
117 assert result == 0
118 handle_error_mock.assert_called_once()
119
120 @pytest.mark.parametrize(
121 "case",
122 [
123 {"id": "empty_string", "action_name": "", "expected_result": ""},
124 {"id": "simple_name", "action_name": "click", "expected_result": "click"},
125 {"id": "camel_case", "action_name": "clickButton", "expected_result": "click-button"},
126 {"id": "mixed_case", "action_name": "onClick", "expected_result": "on-click"},
127 {
128 "id": "with_punctuation",
129 "action_name": "click!button",
130 "expected_result": "click-button",
131 },
132 {
133 "id": "with_underscore",
134 "action_name": "click_button",
135 "expected_result": "click-button",
136 },
137 {"id": "uppercase", "action_name": "CLICK", "expected_result": "click"},
138 ],
139 ids=lambda case: case["id"],
140 )
141 def test_normalize_action_name(self, case: dict, test_context) -> None:
142 """Test AXAction.normalize_action_name with various inputs."""
143
144 self._setup_dependencies(test_context)
145 from orca.ax_action import AXAction
146
147 result = AXAction.normalize_action_name(case["action_name"])
148 assert result == case["expected_result"]
149
150 @pytest.mark.parametrize(
151 "case",
152 [
153 {
154 "id": "invalid_negative",
155 "test_index": -1,
156 "n_actions": 2,
157 "atspi_result": None,
158 "normalize_result": None,
159 "scenario": "invalid_negative",
160 "expected_result": "",
161 "expects_handle_error": False,
162 },
163 {
164 "id": "invalid_too_high",
165 "test_index": 2,
166 "n_actions": 2,
167 "atspi_result": None,
168 "normalize_result": None,
169 "scenario": "invalid_too_high",
170 "expected_result": "",
171 "expects_handle_error": False,
172 },
173 {
174 "id": "valid_index",
175 "test_index": 1,
176 "n_actions": 2,
177 "atspi_result": "clickButton",
178 "normalize_result": "click-button",
179 "scenario": "valid",
180 "expected_result": "click-button",
181 "expects_handle_error": False,
182 },
183 {
184 "id": "glib_error",
185 "test_index": 0,
186 "n_actions": 2,
187 "atspi_result": None,
188 "normalize_result": None,
189 "scenario": "glib_error",
190 "expected_result": "",
191 "expects_handle_error": True,
192 },
193 ],
194 ids=lambda case: case["id"],
195 )
196 def test_get_action_name(
197 self,
198 test_context: OrcaTestContext,
199 case: dict,
200 ) -> None:
201 """Test AXAction.get_action_name with various scenarios."""
202
203 self._setup_dependencies(test_context)
204 from orca.ax_action import AXAction
205 from orca.ax_object import AXObject
206
207 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
208 test_context.patch_object(
209 AXAction,
210 "get_n_actions",
211 side_effect=lambda obj: case["n_actions"],
212 )
213 handle_error_mock = test_context.Mock()
214 test_context.patch_object(AXObject, "handle_error", new=handle_error_mock)
215
216 if case["scenario"] == "valid":
217 test_context.patch_object(
218 Atspi.Action,
219 "get_action_name",
220 return_value=case["atspi_result"],
221 )
222 test_context.patch_object(
223 AXAction,
224 "normalize_action_name",
225 return_value=case["normalize_result"],
226 )
227 elif case["scenario"] == "glib_error":
228 test_context.patch_object(
229 Atspi.Action,
230 "get_action_name",
231 side_effect=GLib.GError("Test error"),
232 )
233
234 result = AXAction.get_action_name(mock_accessible, case["test_index"])
235 assert result == case["expected_result"]
236 if case["expects_handle_error"]:
237 handle_error_mock.assert_called_once()
238
239 @pytest.mark.parametrize(
240 "case",
241 [
242 {
243 "id": "desc_invalid_negative",
244 "test_index": -1,
245 "n_actions": 2,
246 "atspi_result": None,
247 "scenario": "invalid_negative",
248 "expected_result": "",
249 "expects_handle_error": False,
250 },
251 {
252 "id": "desc_invalid_too_high",
253 "test_index": 3,
254 "n_actions": 2,
255 "atspi_result": None,
256 "scenario": "invalid_too_high",
257 "expected_result": "",
258 "expects_handle_error": False,
259 },
260 {
261 "id": "desc_valid_index",
262 "test_index": 0,
263 "n_actions": 2,
264 "atspi_result": "Clicks the button",
265 "scenario": "valid",
266 "expected_result": "Clicks the button",
267 "expects_handle_error": False,
268 },
269 {
270 "id": "desc_glib_error",
271 "test_index": 1,
272 "n_actions": 2,
273 "atspi_result": None,
274 "scenario": "glib_error",
275 "expected_result": "",
276 "expects_handle_error": True,
277 },
278 ],
279 ids=lambda case: case["id"],
280 )
281 def test_get_action_description(
282 self,
283 test_context: OrcaTestContext,
284 case: dict,
285 ) -> None:
286 """Test AXAction.get_action_description with various scenarios."""
287
288 self._setup_dependencies(test_context)
289 from orca.ax_action import AXAction
290 from orca.ax_object import AXObject
291
292 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
293 test_context.patch_object(
294 AXAction,
295 "get_n_actions",
296 side_effect=lambda obj: case["n_actions"],
297 )
298 handle_error_mock = test_context.Mock()
299 test_context.patch_object(AXObject, "handle_error", new=handle_error_mock)
300
301 if case["scenario"] == "valid":
302 test_context.patch_object(
303 Atspi.Action,
304 "get_action_description",
305 return_value=case["atspi_result"],
306 )
307 elif case["scenario"] == "glib_error":
308 test_context.patch_object(
309 Atspi.Action,
310 "get_action_description",
311 side_effect=GLib.GError("Test error"),
312 )
313
314 result = AXAction.get_action_description(mock_accessible, case["test_index"])
315 assert result == case["expected_result"]
316 if case["expects_handle_error"]:
317 handle_error_mock.assert_called_once()
318
319 @pytest.mark.parametrize(
320 "case",
321 [
322 {
323 "id": "key_binding_invalid_negative",
324 "test_index": -1,
325 "n_actions": 2,
326 "atspi_result": None,
327 "scenario": "invalid_negative",
328 "expected_result": "",
329 "expects_handle_error": False,
330 },
331 {
332 "id": "key_binding_invalid_too_high",
333 "test_index": 2,
334 "n_actions": 2,
335 "atspi_result": None,
336 "scenario": "invalid_too_high",
337 "expected_result": "",
338 "expects_handle_error": False,
339 },
340 {
341 "id": "key_binding_valid_index",
342 "test_index": 0,
343 "n_actions": 2,
344 "atspi_result": "Ctrl+Enter",
345 "scenario": "valid",
346 "expected_result": "Ctrl+Enter",
347 "expects_handle_error": False,
348 },
349 {
350 "id": "key_binding_void_symbol",
351 "test_index": 0,
352 "n_actions": 2,
353 "atspi_result": "<VoidSymbol>",
354 "scenario": "void_symbol",
355 "expected_result": "",
356 "expects_handle_error": False,
357 },
358 {
359 "id": "key_binding_glib_error",
360 "test_index": 1,
361 "n_actions": 2,
362 "atspi_result": None,
363 "scenario": "glib_error",
364 "expected_result": "",
365 "expects_handle_error": True,
366 },
367 ],
368 ids=lambda case: case["id"],
369 )
370 def test_get_action_key_binding(
371 self,
372 test_context: OrcaTestContext,
373 case: dict,
374 ) -> None:
375 """Test AXAction.get_action_key_binding with various scenarios."""
376
377 self._setup_dependencies(test_context)
378 from orca.ax_action import AXAction
379 from orca.ax_object import AXObject
380
381 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
382 test_context.patch_object(
383 AXAction,
384 "get_n_actions",
385 side_effect=lambda obj: case["n_actions"],
386 )
387 handle_error_mock = test_context.Mock()
388 test_context.patch_object(AXObject, "handle_error", new=handle_error_mock)
389
390 if case["scenario"] in ["valid", "void_symbol"]:
391 test_context.patch_object(
392 Atspi.Action,
393 "get_key_binding",
394 return_value=case["atspi_result"],
395 )
396 elif case["scenario"] == "glib_error":
397 test_context.patch_object(
398 Atspi.Action,
399 "get_key_binding",
400 side_effect=GLib.GError("Test error"),
401 )
402
403 result = AXAction.get_action_key_binding(mock_accessible, case["test_index"])
404 assert result == case["expected_result"]
405 if case["expects_handle_error"]:
406 handle_error_mock.assert_called_once()
407
408 def test_do_action_success(self, test_context: OrcaTestContext) -> None:
409 """Test AXAction.do_action success."""
410
411 self._setup_dependencies(test_context)
412 from orca.ax_action import AXAction
413 from orca.ax_object import AXObject
414
415 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
416 test_context.patch_object(AXObject, "is_valid", return_value=True)
417 test_context.patch_object(AXAction, "get_n_actions", return_value=2)
418 sys.modules["gi.repository"].Atspi.Action.do_action = test_context.Mock(return_value=True)
419 result = AXAction.do_action(mock_accessible, 0)
420 assert result is True
421
422 def test_do_action_invalid_index(self, test_context: OrcaTestContext) -> None:
423 """Test AXAction.do_action invalid index."""
424
425 self._setup_dependencies(test_context)
426 from orca.ax_action import AXAction
427 from orca.ax_object import AXObject
428
429 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
430 test_context.patch_object(AXObject, "is_valid", return_value=True)
431 test_context.patch_object(AXAction, "get_n_actions", return_value=2)
432 result = AXAction.do_action(mock_accessible, 5)
433 assert result is False
434
435 def test_do_action_glib_error(self, test_context: OrcaTestContext) -> None:
436 """Test AXAction.do_action handles GLib.GError."""
437
438 self._setup_dependencies(test_context)
439 from orca.ax_action import AXAction
440 from orca.ax_object import AXObject
441
442 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
443 test_context.patch_object(AXObject, "is_valid", return_value=True)
444 test_context.patch_object(AXAction, "get_n_actions", return_value=2)
445 handle_error_mock = test_context.Mock()
446 test_context.patch_object(AXObject, "handle_error", new=handle_error_mock)
447 sys.modules["gi.repository"].Atspi.Action.do_action = test_context.Mock(
448 side_effect=sys.modules["gi.repository"].GLib.GError("Test error"),
449 )
450 result = AXAction.do_action(mock_accessible, 0)
451 assert result is False