Debian Codegraph @

tests/unit_tests/test_ax_component.py

raw
1 # Unit tests for ax_component.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=import-outside-toplevel
22
23 """Unit tests for ax_component.py methods."""
24
25 from __future__ import annotations
26
27 from typing import TYPE_CHECKING
28
29 import gi
30 import pytest
31
32 gi.require_version("Atspi", "2.0")
33 from gi.repository import Atspi, GLib
34
35 if TYPE_CHECKING:
36 from unittest.mock import MagicMock
37
38 from .orca_test_context import OrcaTestContext
39
40
41 @pytest.mark.unit
42 class TestAXComponent:
43 """Test AXComponent class methods."""
44
45 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]:
46 """Set up mocks for ax_component dependencies."""
47
48 essential_modules = test_context.setup_shared_dependencies()
49
50 debug_mock = essential_modules["orca.debug"]
51 debug_mock.print_message = test_context.Mock()
52 debug_mock.print_tokens = test_context.Mock()
53 debug_mock.LEVEL_INFO = 800
54
55 ax_object_class_mock = test_context.Mock()
56 ax_object_class_mock.supports_component = test_context.Mock(return_value=True)
57 ax_object_class_mock.is_bogus = test_context.Mock(return_value=False)
58 ax_object_class_mock.get_child_count = test_context.Mock(return_value=0)
59 essential_modules["orca.ax_object"].AXObject = ax_object_class_mock
60
61 return essential_modules
62
63 @pytest.mark.parametrize(
64 "case",
65 [
66 {
67 "id": "no_component_support",
68 "supports_component": False,
69 "position_result": None,
70 "expected_x": -1,
71 "expected_y": -1,
72 },
73 {
74 "id": "valid_position",
75 "supports_component": True,
76 "position_result": "mock_position",
77 "expected_x": 25,
78 "expected_y": 35,
79 },
80 ],
81 ids=lambda case: case["id"],
82 )
83 def test_get_position(
84 self,
85 test_context: OrcaTestContext,
86 case: dict,
87 ) -> None:
88 """Test AXComponent.get_position."""
89
90 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
91 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
92 test_context.patch_object(
93 essential_modules["orca.ax_object"].AXObject,
94 "supports_component",
95 side_effect=lambda obj: case["supports_component"],
96 )
97 from orca.ax_component import AXComponent
98
99 if case["supports_component"] and case["position_result"] == "mock_position":
100 mock_position = test_context.Mock(x=25, y=35)
101 test_context.patch_object(Atspi.Component, "get_position", return_value=mock_position)
102 elif case["supports_component"] and case["position_result"]:
103 test_context.patch_object(
104 Atspi.Component,
105 "get_position",
106 return_value=case["position_result"],
107 )
108 else:
109 mock_negative_position = test_context.Mock(x=-1, y=-1)
110 test_context.patch_object(
111 Atspi.Component,
112 "get_position",
113 return_value=mock_negative_position,
114 )
115 result = AXComponent.get_position(mock_accessible)
116 assert result == (case["expected_x"], case["expected_y"])
117
118 @pytest.mark.parametrize(
119 "method_name, atspi_method_name, expected_result",
120 [
121 pytest.param("get_position", "get_position", (-1, -1), id="get_position"),
122 pytest.param("get_size", "get_size", (-1, -1), id="get_size"),
123 pytest.param("get_rect", "get_extents", "empty_rect", id="get_rect"),
124 pytest.param("object_contains_point", "contains", False, id="object_contains_point"),
125 pytest.param(
126 "scroll_object_to_point",
127 "scroll_to_point",
128 False,
129 id="scroll_object_to_point",
130 ),
131 pytest.param(
132 "scroll_object_to_location",
133 "scroll_to",
134 False,
135 id="scroll_object_to_location",
136 ),
137 ],
138 )
139 def test_glib_error_scenarios(
140 self,
141 test_context: OrcaTestContext,
142 method_name: str,
143 atspi_method_name: str,
144 expected_result,
145 ) -> None:
146 """Test AXComponent methods handle GLib.GError exceptions."""
147
148 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
149 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
150 test_context.patch_object(
151 essential_modules["orca.ax_object"].AXObject,
152 "supports_component",
153 return_value=True,
154 )
155 from orca.ax_component import AXComponent
156
157 def raise_glib_error(*args, **kwargs):
158 raise GLib.GError(f"{method_name} error")
159
160 test_context.patch_object(Atspi.Component, atspi_method_name, side_effect=raise_glib_error)
161
162 method = getattr(AXComponent, method_name)
163 if method_name in ["object_contains_point", "scroll_object_to_point"]:
164 result = method(mock_accessible, 10, 10)
165 elif method_name == "scroll_object_to_location":
166 result = method(mock_accessible, Atspi.ScrollType.TOP_LEFT)
167 else:
168 result = method(mock_accessible)
169
170 if expected_result == "empty_rect":
171 assert isinstance(result, Atspi.Rect)
172 assert result.x == result.y == result.width == result.height == 0
173 else:
174 assert result == expected_result
175
176 @pytest.mark.parametrize(
177 "case",
178 [
179 {
180 "id": "no_component_support",
181 "supports_component": False,
182 "size_result": None,
183 "expected_width": -1,
184 "expected_height": -1,
185 },
186 {
187 "id": "valid_size",
188 "supports_component": True,
189 "size_result": "mock_size",
190 "expected_width": 150,
191 "expected_height": 75,
192 },
193 ],
194 ids=lambda case: case["id"],
195 )
196 def test_get_size(
197 self,
198 test_context: OrcaTestContext,
199 case: dict,
200 ) -> None:
201 """Test AXComponent.get_size."""
202
203 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
204 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
205 test_context.patch_object(
206 essential_modules["orca.ax_object"].AXObject,
207 "supports_component",
208 side_effect=lambda obj: case["supports_component"],
209 )
210 from orca.ax_component import AXComponent
211
212 if case["supports_component"] and case["size_result"] == "mock_size":
213 mock_size = test_context.Mock(x=150, y=75)
214 test_context.patch_object(Atspi.Component, "get_size", return_value=mock_size)
215 elif case["supports_component"] and case["size_result"]:
216 test_context.patch_object(Atspi.Component, "get_size", return_value=case["size_result"])
217 else:
218 mock_negative_size = test_context.Mock(x=-1, y=-1)
219 test_context.patch_object(Atspi.Component, "get_size", return_value=mock_negative_size)
220 result = AXComponent.get_size(mock_accessible)
221 assert result == (case["expected_width"], case["expected_height"])
222
223 @pytest.mark.parametrize(
224 "case",
225 [
226 {"id": "no_component_support", "supports_component": False},
227 {"id": "valid_rect", "supports_component": True},
228 ],
229 ids=lambda case: case["id"],
230 )
231 def test_get_rect(self, test_context: OrcaTestContext, case: dict) -> None:
232 """Test AXComponent.get_rect."""
233
234 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
235 mock_rect = Atspi.Rect()
236 mock_rect.x = 10
237 mock_rect.y = 20
238 mock_rect.width = 100
239 mock_rect.height = 50
240 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
241
242 essential_modules["orca.ax_object"].AXObject.supports_component = test_context.Mock(
243 return_value=case["supports_component"],
244 )
245 from orca.ax_component import AXComponent
246
247 if case["supports_component"]:
248 test_context.patch_object(Atspi.Component, "get_extents", return_value=mock_rect)
249 else:
250
251 def mock_get_extents(obj, coord_type): # pylint: disable=unused-argument
252 return Atspi.Rect()
253
254 test_context.patch_object(Atspi.Component, "get_extents", side_effect=mock_get_extents)
255 result = AXComponent.get_rect(mock_accessible)
256 if case["supports_component"]:
257 assert result == mock_rect
258 else:
259 assert isinstance(result, Atspi.Rect)
260 assert result.x == result.y == result.width == result.height == 0
261
262 @pytest.mark.parametrize(
263 "case",
264 [
265 {
266 "id": "no_component_support",
267 "supports_component": False,
268 "is_bogus": False,
269 "contains_result": None,
270 "expected_result": False,
271 },
272 {
273 "id": "bogus_object",
274 "supports_component": True,
275 "is_bogus": True,
276 "contains_result": None,
277 "expected_result": False,
278 },
279 {
280 "id": "point_contained",
281 "supports_component": True,
282 "is_bogus": False,
283 "contains_result": True,
284 "expected_result": True,
285 },
286 {
287 "id": "point_not_contained",
288 "supports_component": True,
289 "is_bogus": False,
290 "contains_result": False,
291 "expected_result": False,
292 },
293 ],
294 ids=lambda case: case["id"],
295 )
296 def test_object_contains_point(
297 self,
298 test_context: OrcaTestContext,
299 case: dict,
300 ) -> None:
301 """Test AXComponent.object_contains_point."""
302
303 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
304 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
305
306 essential_modules["orca.ax_object"].AXObject.supports_component = test_context.Mock(
307 return_value=case["supports_component"],
308 )
309 essential_modules["orca.ax_object"].AXObject.is_bogus = test_context.Mock(
310 return_value=case["is_bogus"],
311 )
312 from orca.ax_component import AXComponent
313
314 def contains_func(obj, x, y, coord_type): # pylint: disable=unused-argument
315 if (
316 case["supports_component"]
317 and not case["is_bogus"]
318 and case["contains_result"] is not None
319 ):
320 return case["contains_result"]
321 return False
322
323 test_context.patch_object(Atspi.Component, "contains", side_effect=contains_func)
324 result = AXComponent.object_contains_point(mock_accessible, 50, 75)
325 assert result is case["expected_result"]
326
327 @pytest.mark.parametrize(
328 "case",
329 [
330 {
331 "id": "no_component_support",
332 "supports_component": False,
333 "scroll_result": None,
334 "expected_result": False,
335 },
336 {
337 "id": "scroll_successful",
338 "supports_component": True,
339 "scroll_result": True,
340 "expected_result": True,
341 },
342 {
343 "id": "scroll_failed",
344 "supports_component": True,
345 "scroll_result": False,
346 "expected_result": False,
347 },
348 ],
349 ids=lambda case: case["id"],
350 )
351 def test_scroll_object_to_point(
352 self,
353 test_context: OrcaTestContext,
354 case: dict,
355 ) -> None:
356 """Test AXComponent.scroll_object_to_point."""
357
358 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
359 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
360 from orca.ax_component import AXComponent
361
362 test_context.patch_object(
363 essential_modules["orca.ax_object"].AXObject,
364 "supports_component",
365 side_effect=lambda obj: case["supports_component"],
366 )
367 if case["supports_component"] and case["scroll_result"] is not None:
368 test_context.patch_object(
369 Atspi.Component,
370 "scroll_to_point",
371 return_value=case["scroll_result"],
372 )
373 else:
374 test_context.patch_object(Atspi.Component, "scroll_to_point", return_value=False)
375 result = AXComponent.scroll_object_to_point(mock_accessible, 100, 200)
376 assert result is case["expected_result"]
377
378 @pytest.mark.parametrize(
379 "case",
380 [
381 {
382 "id": "no_component_support",
383 "supports_component": False,
384 "scroll_result": None,
385 "expected_result": False,
386 },
387 {
388 "id": "scroll_successful",
389 "supports_component": True,
390 "scroll_result": True,
391 "expected_result": True,
392 },
393 {
394 "id": "scroll_failed",
395 "supports_component": True,
396 "scroll_result": False,
397 "expected_result": False,
398 },
399 ],
400 ids=lambda case: case["id"],
401 )
402 def test_scroll_object_to_location(
403 self,
404 test_context: OrcaTestContext,
405 case: dict,
406 ) -> None:
407 """Test AXComponent.scroll_object_to_location."""
408
409 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
410 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
411 from orca.ax_component import AXComponent
412
413 test_context.patch_object(
414 essential_modules["orca.ax_object"].AXObject,
415 "supports_component",
416 side_effect=lambda obj: case["supports_component"],
417 )
418 if case["supports_component"] and case["scroll_result"] is not None:
419 test_context.patch_object(
420 Atspi.Component,
421 "scroll_to",
422 return_value=case["scroll_result"],
423 )
424 else:
425 test_context.patch_object(Atspi.Component, "scroll_to", return_value=False)
426 result = AXComponent.scroll_object_to_location(mock_accessible, Atspi.ScrollType.TOP_LEFT)
427 assert result is case["expected_result"]