Debian Codegraph @

tests/unit_tests/test_ax_event_synthesizer.py

raw
1 # Unit tests for ax_event_synthesizer.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=protected-access
22 # pylint: disable=wrong-import-order
23 # pylint: disable=wrong-import-position
24 # pylint: disable=import-outside-toplevel
25 # pylint: disable=too-many-public-methods
26
27 """Unit tests for ax_event_synthesizer.py methods."""
28
29 from __future__ import annotations
30
31 from typing import TYPE_CHECKING
32
33 import gi
34 import pytest
35
36 gi.require_version("Atspi", "2.0")
37 from gi.repository import Atspi, GLib
38
39 if TYPE_CHECKING:
40 from unittest.mock import MagicMock
41
42 from .orca_test_context import OrcaTestContext
43
44
45 @pytest.mark.unit
46 class TestAXEventSynthesizer:
47 """Test AXEventSynthesizer class methods."""
48
49 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]:
50 """Set up mocks for ax_event_synthesizer dependencies."""
51
52 additional_modules = [
53 "orca.ax_component",
54 "orca.ax_device_manager",
55 "orca.ax_object",
56 "orca.ax_text",
57 "orca.ax_utilities_action",
58 "orca.ax_utilities_component",
59 "orca.ax_utilities_debugging",
60 "orca.ax_utilities_role",
61 ]
62 essential_modules = test_context.setup_shared_dependencies(additional_modules)
63
64 ax_component_mock = essential_modules["orca.ax_component"].AXComponent
65 ax_component_mock.get_rect = test_context.Mock()
66 ax_component_mock.get_position = test_context.Mock(return_value=(0, 0))
67 ax_component_mock.scroll_object_to_location = test_context.Mock()
68 ax_component_mock.scroll_object_to_point = test_context.Mock()
69
70 ax_utilities_component_mock = essential_modules[
71 "orca.ax_utilities_component"
72 ].AXUtilitiesComponent
73 ax_utilities_component_mock.get_rect_intersection = test_context.Mock()
74 ax_utilities_component_mock.is_empty_rect = test_context.Mock(return_value=False)
75
76 def mock_rect(x=0, y=0, width=100, height=50):
77 rect = test_context.Mock()
78 rect.x = x
79 rect.y = y
80 rect.width = width
81 rect.height = height
82 return rect
83
84 ax_component_mock.get_rect.return_value = mock_rect()
85
86 ax_text_mock = essential_modules["orca.ax_text"].AXText
87 ax_text_mock.get_caret_offset = test_context.Mock(return_value=0)
88 ax_text_mock.get_character_rect = test_context.Mock(return_value=mock_rect())
89 ax_text_mock.set_caret_offset = test_context.Mock()
90 ax_text_mock.scroll_substring_to_location = test_context.Mock()
91 ax_text_mock.scroll_substring_to_point = test_context.Mock()
92
93 ax_utilities_role_mock = essential_modules["orca.ax_utilities_role"].AXUtilitiesRole
94 ax_utilities_role_mock.is_application = test_context.Mock(return_value=False)
95
96 ax_utilities_debugging_mock = essential_modules["orca.ax_utilities_debugging"]
97 ax_utilities_debugging_mock.AXUtilitiesDebugging = test_context.Mock()
98 ax_utilities_debugging_mock.AXUtilitiesDebugging.actions_as_string = test_context.Mock(
99 return_value="mock actions",
100 )
101
102 if "orca.debug" in essential_modules:
103 debug_mock = essential_modules["orca.debug"]
104 debug_mock.LEVEL_INFO = 800
105 debug_mock.debugLevel = 2
106
107 return essential_modules
108
109 def _setup_successful_mouse_event_mocks(self, test_context, essential_modules) -> None:
110 """Helper method to set up mocks for successful mouse event completion."""
111
112 ax_text_mock = essential_modules["orca.ax_text"].AXText
113 char_rect = test_context.Mock()
114 char_rect.x = 50
115 char_rect.y = 60
116 char_rect.width = 10
117 char_rect.height = 15
118 ax_text_mock.get_character_rect.return_value = char_rect
119 ax_component_mock = essential_modules["orca.ax_component"].AXComponent
120 obj_rect = test_context.Mock()
121 obj_rect.x = 40
122 obj_rect.y = 50
123 ax_component_mock.get_rect.return_value = obj_rect
124 ax_uc_mock = essential_modules["orca.ax_utilities_component"].AXUtilitiesComponent
125 ax_uc_mock.get_rect_intersection.return_value = test_context.Mock()
126 ax_uc_mock.is_empty_rect.return_value = False
127
128 def test_highest_ancestor_true_when_parent_is_none(self, test_context: OrcaTestContext) -> None:
129 """Test _highest_ancestor returns True when parent is None."""
130
131 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
132 from orca.ax_event_synthesizer import AXEventSynthesizer
133
134 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
135 ax_object_mock = essential_modules["orca.ax_object"].AXObject
136 ax_object_mock.get_parent.return_value = None
137 result = AXEventSynthesizer._highest_ancestor(mock_accessible)
138 assert result is True
139 ax_object_mock.get_parent.assert_called_once_with(mock_accessible)
140
141 def test_highest_ancestor_true_when_parent_is_application(
142 self,
143 test_context: OrcaTestContext,
144 ) -> None:
145 """Test _highest_ancestor returns True when parent is application."""
146
147 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
148 from orca.ax_event_synthesizer import AXEventSynthesizer
149
150 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
151 mock_parent = test_context.Mock(spec=Atspi.Accessible)
152 ax_object_mock = essential_modules["orca.ax_object"].AXObject
153 ax_object_mock.get_parent.return_value = mock_parent
154 ax_utilities_role_mock = essential_modules["orca.ax_utilities_role"].AXUtilitiesRole
155 ax_utilities_role_mock.is_application.return_value = True
156 result = AXEventSynthesizer._highest_ancestor(mock_accessible)
157 assert result is True
158 ax_object_mock.get_parent.assert_called_once_with(mock_accessible)
159 ax_utilities_role_mock.is_application.assert_called_once_with(mock_parent)
160
161 def test_is_scrolled_off_screen_false_when_no_ancestor(
162 self,
163 test_context: OrcaTestContext,
164 ) -> None:
165 """Test _is_scrolled_off_screen returns False when no ancestor found."""
166
167 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
168 from orca.ax_event_synthesizer import AXEventSynthesizer
169
170 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
171 ax_object_mock = essential_modules["orca.ax_object"].AXObject
172 ax_object_mock.find_ancestor.return_value = None
173 result = AXEventSynthesizer._is_scrolled_off_screen(mock_accessible)
174 assert result is False
175
176 def test_is_scrolled_off_screen_true_when_outside_ancestor(
177 self,
178 test_context: OrcaTestContext,
179 ) -> None:
180 """Test _is_scrolled_off_screen returns True when object is outside ancestor."""
181
182 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
183 from orca.ax_event_synthesizer import AXEventSynthesizer
184
185 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
186 mock_ancestor = test_context.Mock(spec=Atspi.Accessible)
187 ax_object_mock = essential_modules["orca.ax_object"].AXObject
188 ax_object_mock.find_ancestor.return_value = mock_ancestor
189 ax_component_mock = essential_modules["orca.ax_component"].AXComponent
190 obj_rect = test_context.Mock()
191 obj_rect.x = 200
192 obj_rect.y = 200
193 obj_rect.width = 100
194 obj_rect.height = 50
195 ancestor_rect = test_context.Mock()
196 ancestor_rect.x = 0
197 ancestor_rect.y = 0
198 ancestor_rect.width = 100
199 ancestor_rect.height = 100
200 ax_component_mock.get_rect.side_effect = [obj_rect, ancestor_rect]
201 ax_uc_mock = essential_modules["orca.ax_utilities_component"].AXUtilitiesComponent
202 ax_uc_mock.get_rect_intersection.return_value = test_context.Mock()
203 ax_uc_mock.is_empty_rect.return_value = True
204 result = AXEventSynthesizer._is_scrolled_off_screen(mock_accessible)
205 assert result is True
206
207 def test_generate_mouse_event_success(self, test_context: OrcaTestContext) -> None:
208 """Test _generate_mouse_event returns True on successful event generation."""
209
210 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
211 from orca.ax_event_synthesizer import AXEventSynthesizer
212
213 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
214 mock_device = test_context.Mock()
215 ax_device_mgr = essential_modules["orca.ax_device_manager"]
216 ax_device_mgr.get_manager.return_value.get_device.return_value = mock_device
217 mock_generate = test_context.Mock()
218 test_context.patch("gi.repository.Atspi.Device.generate_mouse_event", new=mock_generate)
219 result = AXEventSynthesizer._generate_mouse_event(mock_accessible, 50, 25, "b1c")
220 assert result is True
221 ax_device_mgr.get_manager.return_value.get_device.assert_called_once()
222 mock_generate.assert_called_once_with(mock_device, mock_accessible, 50, 25, "b1c")
223
224 def test_generate_mouse_event_exception_returns_false(
225 self,
226 test_context: OrcaTestContext,
227 ) -> None:
228 """Test _generate_mouse_event returns False on GLib.GError exception."""
229
230 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
231 from orca.ax_event_synthesizer import AXEventSynthesizer
232
233 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
234 mock_device = test_context.Mock()
235 ax_device_mgr = essential_modules["orca.ax_device_manager"]
236 ax_device_mgr.get_manager.return_value.get_device.return_value = mock_device
237 mock_generate = test_context.Mock()
238 test_context.patch("gi.repository.Atspi.Device.generate_mouse_event", new=mock_generate)
239 mock_generate.side_effect = GLib.GError("Test error")
240 result = AXEventSynthesizer._generate_mouse_event(mock_accessible, 50, 25, "b1c")
241 assert result is False
242
243 def test_mouse_event_on_character_uses_caret_offset_when_none(
244 self,
245 test_context: OrcaTestContext,
246 ) -> None:
247 """Test _mouse_event_on_character uses caret offset when offset is None."""
248
249 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
250 from orca.ax_event_synthesizer import AXEventSynthesizer
251
252 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
253 ax_text_mock = essential_modules["orca.ax_text"].AXText
254 ax_text_mock.get_caret_offset.return_value = 5
255 self._setup_successful_mouse_event_mocks(test_context, essential_modules)
256 mock_generate = test_context.Mock()
257 test_context.patch_object(AXEventSynthesizer, "_generate_mouse_event", new=mock_generate)
258 mock_generate.return_value = True
259 result = AXEventSynthesizer._mouse_event_on_character(mock_accessible, None, "b1c")
260 ax_text_mock.get_caret_offset.assert_called_once_with(mock_accessible)
261 assert result is True
262
263 def test_route_to_character_calls_mouse_event_with_abs(
264 self,
265 test_context: OrcaTestContext,
266 ) -> None:
267 """Test route_to_character calls _mouse_event_on_character with 'abs' event."""
268
269 self._setup_dependencies(test_context)
270 from orca.ax_event_synthesizer import AXEventSynthesizer
271
272 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
273 mock_event = test_context.Mock()
274 test_context.patch_object(AXEventSynthesizer, "_mouse_event_on_character", new=mock_event)
275 mock_event.return_value = True
276 result = AXEventSynthesizer.route_to_character(mock_accessible, 15)
277 mock_event.assert_called_once_with(mock_accessible, 15, "abs")
278 assert result is True
279
280 def test_route_to_object_calls_mouse_event_with_abs(
281 self,
282 test_context: OrcaTestContext,
283 ) -> None:
284 """Test route_to_object calls _mouse_event_on_object with 'abs' event."""
285
286 self._setup_dependencies(test_context)
287 from orca.ax_event_synthesizer import AXEventSynthesizer
288
289 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
290 mock_event = test_context.Mock()
291 test_context.patch_object(AXEventSynthesizer, "_mouse_event_on_object", new=mock_event)
292 mock_event.return_value = True
293 result = AXEventSynthesizer.route_to_object(mock_accessible)
294 mock_event.assert_called_once_with(mock_accessible, "abs")
295 assert result is True
296
297 def test_click_character_calls_mouse_event_with_button_click(
298 self,
299 test_context: OrcaTestContext,
300 ) -> None:
301 """Test click_character calls _mouse_event_on_character with button click event."""
302
303 self._setup_dependencies(test_context)
304 from orca.ax_event_synthesizer import AXEventSynthesizer
305
306 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
307 mock_event = test_context.Mock()
308 test_context.patch_object(AXEventSynthesizer, "_mouse_event_on_character", new=mock_event)
309 mock_event.return_value = True
310 result = AXEventSynthesizer.click_character(mock_accessible, 20, 2)
311 mock_event.assert_called_once_with(mock_accessible, 20, "b2c")
312 assert result is True
313
314 def test_click_object_calls_mouse_event_with_button_click(
315 self,
316 test_context: OrcaTestContext,
317 ) -> None:
318 """Test click_object calls _mouse_event_on_object with button click event."""
319
320 self._setup_dependencies(test_context)
321 from orca.ax_event_synthesizer import AXEventSynthesizer
322
323 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
324 mock_event = test_context.Mock()
325 test_context.patch_object(AXEventSynthesizer, "_mouse_event_on_object", new=mock_event)
326 mock_event.return_value = True
327 result = AXEventSynthesizer.click_object(mock_accessible, 3)
328 mock_event.assert_called_once_with(mock_accessible, "b3c")
329 assert result is True
330
331 def test_scroll_into_view_calls_scroll_to_location_anywhere(
332 self,
333 test_context: OrcaTestContext,
334 ) -> None:
335 """Test scroll_into_view calls _scroll_to_location with ANYWHERE."""
336
337 self._setup_dependencies(test_context)
338 from orca.ax_event_synthesizer import AXEventSynthesizer
339
340 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
341 mock_scroll = test_context.Mock()
342 test_context.patch_object(AXEventSynthesizer, "_scroll_to_location", new=mock_scroll)
343 AXEventSynthesizer.scroll_into_view(mock_accessible, 5, 15)
344 mock_scroll.assert_called_once_with(mock_accessible, Atspi.ScrollType.ANYWHERE, 5, 15)
345
346 def test_scroll_to_center_calculates_center_coordinates(
347 self,
348 test_context: OrcaTestContext,
349 ) -> None:
350 """Test scroll_to_center calculates center coordinates of ancestor."""
351
352 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
353 from orca.ax_event_synthesizer import AXEventSynthesizer
354
355 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
356 mock_ancestor = test_context.Mock(spec=Atspi.Accessible)
357 ax_object_mock = essential_modules["orca.ax_object"].AXObject
358 ax_object_mock.find_ancestor.return_value = mock_ancestor
359 ax_uc_mock = essential_modules["orca.ax_utilities_component"].AXUtilitiesComponent
360 ax_uc_mock.get_center_point.return_value = (300.0, 350.0)
361 mock_scroll = test_context.Mock()
362 test_context.patch_object(AXEventSynthesizer, "_scroll_to_point", new=mock_scroll)
363 AXEventSynthesizer.scroll_to_center(mock_accessible, 5, 15)
364
365 mock_scroll.assert_called_once_with(mock_accessible, 300, 350, 5, 15)
366
367 @pytest.mark.parametrize(
368 "method_name,scroll_type",
369 [
370 pytest.param("scroll_to_top_edge", "Atspi.ScrollType.TOP_EDGE", id="top_edge"),
371 pytest.param("scroll_to_top_left", "Atspi.ScrollType.TOP_LEFT", id="top_left"),
372 pytest.param("scroll_to_left_edge", "Atspi.ScrollType.LEFT_EDGE", id="left_edge"),
373 pytest.param("scroll_to_bottom_edge", "Atspi.ScrollType.BOTTOM_EDGE", id="bottom_edge"),
374 pytest.param(
375 "scroll_to_bottom_right",
376 "Atspi.ScrollType.BOTTOM_RIGHT",
377 id="bottom_right",
378 ),
379 pytest.param("scroll_to_right_edge", "Atspi.ScrollType.RIGHT_EDGE", id="right_edge"),
380 ],
381 )
382 def test_scroll_direction_methods(self, test_context, method_name, scroll_type) -> None:
383 """Test all directional scroll methods call _scroll_to_location with correct type."""
384
385 self._setup_dependencies(test_context)
386 from orca.ax_event_synthesizer import AXEventSynthesizer
387
388 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
389 scroll_type_value = getattr(Atspi.ScrollType, scroll_type.split(".")[-1])
390 mock_scroll = test_context.Mock()
391 test_context.patch_object(AXEventSynthesizer, "_scroll_to_location", new=mock_scroll)
392 method = getattr(AXEventSynthesizer, method_name)
393 method(mock_accessible, 10, 20)
394 mock_scroll.assert_called_once_with(mock_accessible, scroll_type_value, 10, 20)
395
396 def test_try_all_clickable_actions_success_on_first_action(
397 self,
398 test_context: OrcaTestContext,
399 ) -> None:
400 """Test try_all_clickable_actions returns True on first successful action."""
401
402 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
403 from orca.ax_event_synthesizer import AXEventSynthesizer
404
405 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
406 ax_utilities_action_mock = essential_modules["orca.ax_utilities_action"].AXUtilitiesAction
407 ax_utilities_action_mock.do_named_action.return_value = True
408 result = AXEventSynthesizer.try_all_clickable_actions(mock_accessible)
409 assert result is True
410 ax_utilities_action_mock.do_named_action.assert_called_once_with(mock_accessible, "click")
411
412 def test_try_all_clickable_actions_false_when_all_fail(
413 self,
414 test_context: OrcaTestContext,
415 ) -> None:
416 """Test try_all_clickable_actions returns False when all actions fail."""
417
418 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
419 from orca.ax_event_synthesizer import AXEventSynthesizer
420
421 mock_accessible = test_context.Mock(spec=Atspi.Accessible)
422 ax_utilities_action_mock = essential_modules["orca.ax_utilities_action"].AXUtilitiesAction
423 ax_utilities_action_mock.do_named_action.return_value = False
424 result = AXEventSynthesizer.try_all_clickable_actions(mock_accessible)
425 assert result is False
426 assert ax_utilities_action_mock.do_named_action.call_count == 5
427
428 def test_get_synthesizer_returns_singleton(self, test_context: OrcaTestContext) -> None:
429 """Test get_synthesizer returns the singleton instance."""
430
431 self._setup_dependencies(test_context)
432 from orca.ax_event_synthesizer import get_synthesizer
433
434 synthesizer1 = get_synthesizer()
435 synthesizer2 = get_synthesizer()
436 assert synthesizer1 is synthesizer2
437
438 def test_mouse_event_on_character_returns_false_when_empty_rect(
439 self,
440 test_context: OrcaTestContext,
441 ) -> None:
442 """Test _mouse_event_on_character returns False when character rect is empty."""
443
444 essential_modules = self._setup_dependencies(test_context)
445 from orca.ax_event_synthesizer import AXEventSynthesizer
446
447 mock_obj = test_context.Mock(spec=Atspi.Accessible)
448
449 ax_text_mock = essential_modules["orca.ax_text"].AXText
450 ax_text_mock.get_caret_offset.return_value = 5
451 empty_rect = test_context.Mock()
452 empty_rect.x = empty_rect.y = empty_rect.width = empty_rect.height = 0
453 ax_text_mock.get_character_rect.return_value = empty_rect
454
455 ax_uc_mock = essential_modules["orca.ax_utilities_component"].AXUtilitiesComponent
456 ax_uc_mock.is_empty_rect.return_value = True
457
458 test_context.patch_object(
459 AXEventSynthesizer,
460 "_is_scrolled_off_screen",
461 side_effect=lambda obj, offset: False,
462 )
463
464 result = AXEventSynthesizer._mouse_event_on_character(mock_obj, 5, "abs")
465 assert result is False
466
467 def test_mouse_event_on_character_returns_false_when_caret_outside_object(
468 self,
469 test_context: OrcaTestContext,
470 ) -> None:
471 """Test _mouse_event_on_character returns False when caret is outside object bounds."""
472
473 essential_modules = self._setup_dependencies(test_context)
474 from orca.ax_event_synthesizer import AXEventSynthesizer
475
476 mock_obj = test_context.Mock(spec=Atspi.Accessible)
477
478 ax_text_mock = essential_modules["orca.ax_text"].AXText
479 ax_text_mock.get_caret_offset.return_value = 10
480 char_rect = test_context.Mock()
481 char_rect.x, char_rect.y, char_rect.width, char_rect.height = 100, 100, 10, 10
482 ax_text_mock.get_character_rect.return_value = char_rect
483
484 ax_component_mock = essential_modules["orca.ax_component"].AXComponent
485 obj_rect = test_context.Mock()
486 obj_rect.x, obj_rect.y, obj_rect.width, obj_rect.height = 200, 200, 50, 50
487 ax_component_mock.get_rect.return_value = obj_rect
488 empty_intersection = test_context.Mock()
489 empty_intersection.width = empty_intersection.height = 0
490 ax_uc_mock = essential_modules["orca.ax_utilities_component"].AXUtilitiesComponent
491 ax_uc_mock.get_rect_intersection.return_value = empty_intersection
492 ax_uc_mock.is_empty_rect.side_effect = lambda rect: rect.width == 0
493
494 test_context.patch_object(
495 AXEventSynthesizer,
496 "_is_scrolled_off_screen",
497 side_effect=lambda obj, offset: False,
498 )
499
500 result = AXEventSynthesizer._mouse_event_on_character(mock_obj, 10, "abs")
501 assert result is False
502 essential_modules["orca.debug"].print_tokens.assert_called()
503
504 @pytest.mark.parametrize(
505 "method_name,args,position_changes,expected_text_method,expected_object_method",
506 [
507 pytest.param(
508 "_scroll_to_location",
509 ("Atspi.ScrollType.ANYWHERE", 5, 10),
510 True,
511 "scroll_substring_to_location",
512 "scroll_object_to_location",
513 id="location_success",
514 ),
515 pytest.param(
516 "_scroll_to_location",
517 ("Atspi.ScrollType.BOTTOM_EDGE", 15, 20),
518 False,
519 "scroll_substring_to_location",
520 "scroll_object_to_location",
521 id="location_fallback",
522 ),
523 pytest.param(
524 "_scroll_to_point",
525 (300, 400, 25, 30),
526 False,
527 "scroll_substring_to_point",
528 "scroll_object_to_point",
529 id="point_fallback",
530 ),
531 ],
532 )
533 def test_scroll_methods_with_fallback( # pylint: disable=too-many-arguments,too-many-positional-arguments,too-many-locals
534 self,
535 test_context: OrcaTestContext,
536 method_name,
537 args,
538 position_changes,
539 expected_text_method,
540 expected_object_method,
541 ) -> None:
542 """Test scroll methods with success and fallback scenarios."""
543
544 essential_modules = self._setup_dependencies(test_context)
545 from orca.ax_event_synthesizer import AXEventSynthesizer
546
547 mock_obj = test_context.Mock(spec=Atspi.Accessible)
548 ax_component_mock = essential_modules["orca.ax_component"].AXComponent
549 ax_text_mock = essential_modules["orca.ax_text"].AXText
550 ax_object_mock = essential_modules["orca.ax_object"].AXObject
551
552 if position_changes:
553 initial_pos = test_context.Mock()
554 initial_pos.x, initial_pos.y = 100, 100
555 changed_pos = test_context.Mock()
556 changed_pos.x, changed_pos.y = 150, 120
557 position_call_count = 0
558
559 def mock_get_position(_obj):
560 nonlocal position_call_count
561 position_call_count += 1
562 return initial_pos if position_call_count == 1 else changed_pos
563
564 ax_component_mock.get_position.side_effect = mock_get_position
565 else:
566 same_pos = test_context.Mock()
567 if "location" in method_name:
568 same_pos.x, same_pos.y = 100, 100
569 else:
570 same_pos.x, same_pos.y = 75, 125
571 ax_component_mock.get_position.return_value = same_pos
572
573 final_args = []
574 for arg in args:
575 if isinstance(arg, str) and "Atspi.ScrollType" in arg:
576 scroll_type = getattr(Atspi.ScrollType, arg.split(".")[-1])
577 final_args.append(scroll_type)
578 else:
579 final_args.append(arg)
580
581 method = getattr(AXEventSynthesizer, method_name)
582 method(mock_obj, *final_args)
583
584 text_method = getattr(ax_text_mock, expected_text_method)
585 text_method.assert_called_once_with(mock_obj, *final_args)
586
587 object_method = getattr(ax_component_mock, expected_object_method)
588 if position_changes:
589 object_method.assert_not_called()
590 assert ax_object_mock.clear_cache.call_count == 1
591 assert essential_modules["orca.debug"].print_tokens.call_count == 1
592 else:
593 if "location" in method_name:
594 object_method.assert_called_once_with(mock_obj, final_args[0])
595 else:
596 object_method.assert_called_once_with(mock_obj, final_args[0], final_args[1])
597 assert ax_object_mock.clear_cache.call_count == 2
598 assert essential_modules["orca.debug"].print_tokens.call_count == 2
599
600 def test_is_scrolled_off_screen_returns_false_when_no_ancestor_found(
601 self,
602 test_context: OrcaTestContext,
603 ) -> None:
604 """Test _is_scrolled_off_screen returns False when no ancestor is found."""
605
606 essential_modules = self._setup_dependencies(test_context)
607 from orca.ax_event_synthesizer import AXEventSynthesizer
608
609 mock_obj = test_context.Mock(spec=Atspi.Accessible)
610
611 ax_component_mock = essential_modules["orca.ax_component"].AXComponent
612 rect = test_context.Mock()
613 rect.x, rect.y, rect.width, rect.height = 50, 50, 100, 100
614 ax_component_mock.get_rect.return_value = rect
615
616 ax_object_mock = essential_modules["orca.ax_object"].AXObject
617 ax_object_mock.find_ancestor.return_value = None
618
619 result = AXEventSynthesizer._is_scrolled_off_screen(mock_obj, 5)
620 assert result is False
621 essential_modules["orca.debug"].print_tokens.assert_called()
622
623 def test_is_scrolled_off_screen_returns_true_when_object_outside_ancestor(
624 self,
625 test_context: OrcaTestContext,
626 ) -> None:
627 """Test _is_scrolled_off_screen returns True when object is outside ancestor bounds."""
628
629 essential_modules = self._setup_dependencies(test_context)
630 from orca.ax_event_synthesizer import AXEventSynthesizer
631
632 mock_obj = test_context.Mock(spec=Atspi.Accessible)
633 mock_ancestor = test_context.Mock(spec=Atspi.Accessible)
634
635 ax_component_mock = essential_modules["orca.ax_component"].AXComponent
636 obj_rect = test_context.Mock()
637 obj_rect.x, obj_rect.y, obj_rect.width, obj_rect.height = 200, 200, 50, 50
638 ancestor_rect = test_context.Mock()
639 ancestor_rect.x, ancestor_rect.y, ancestor_rect.width, ancestor_rect.height = (
640 100,
641 100,
642 50,
643 50,
644 )
645 empty_intersection = test_context.Mock()
646 empty_intersection.width = empty_intersection.height = 0
647
648 ax_component_mock.get_rect.side_effect = [obj_rect, ancestor_rect]
649 ax_uc_mock = essential_modules["orca.ax_utilities_component"].AXUtilitiesComponent
650 ax_uc_mock.get_rect_intersection.return_value = empty_intersection
651 ax_uc_mock.is_empty_rect.return_value = True
652
653 ax_object_mock = essential_modules["orca.ax_object"].AXObject
654 ax_object_mock.find_ancestor.return_value = mock_ancestor
655
656 result = AXEventSynthesizer._is_scrolled_off_screen(mock_obj, 10)
657 assert result is True
658 essential_modules["orca.debug"].print_tokens.assert_called()
659
660 def test_is_scrolled_off_screen_returns_false_when_no_offset(
661 self,
662 test_context: OrcaTestContext,
663 ) -> None:
664 """Test _is_scrolled_off_screen returns False when offset is None and object in bounds."""
665
666 essential_modules = self._setup_dependencies(test_context)
667 from orca.ax_event_synthesizer import AXEventSynthesizer
668
669 mock_obj = test_context.Mock(spec=Atspi.Accessible)
670 mock_ancestor = test_context.Mock(spec=Atspi.Accessible)
671
672 ax_component_mock = essential_modules["orca.ax_component"].AXComponent
673 obj_rect = test_context.Mock()
674 ancestor_rect = test_context.Mock()
675 intersection = test_context.Mock()
676 intersection.width = intersection.height = 50
677
678 ax_component_mock.get_rect.side_effect = [obj_rect, ancestor_rect]
679 ax_uc_mock = essential_modules["orca.ax_utilities_component"].AXUtilitiesComponent
680 ax_uc_mock.get_rect_intersection.return_value = intersection
681 ax_uc_mock.is_empty_rect.return_value = False
682
683 ax_object_mock = essential_modules["orca.ax_object"].AXObject
684 ax_object_mock.find_ancestor.return_value = mock_ancestor
685
686 result = AXEventSynthesizer._is_scrolled_off_screen(mock_obj, None)
687 assert result is False
688 essential_modules["orca.debug"].print_tokens.assert_called()
689
690 def test_is_scrolled_off_screen_returns_false_when_empty_character_rect(
691 self,
692 test_context: OrcaTestContext,
693 ) -> None:
694 """Test _is_scrolled_off_screen returns False when character rect is empty."""
695
696 essential_modules = self._setup_dependencies(test_context)
697 from orca.ax_event_synthesizer import AXEventSynthesizer
698
699 mock_obj = test_context.Mock(spec=Atspi.Accessible)
700 mock_ancestor = test_context.Mock(spec=Atspi.Accessible)
701
702 ax_component_mock = essential_modules["orca.ax_component"].AXComponent
703 obj_rect = test_context.Mock()
704 ancestor_rect = test_context.Mock()
705 intersection = test_context.Mock()
706 intersection.width = intersection.height = 50
707 empty_char_rect = test_context.Mock()
708 empty_char_rect.width = empty_char_rect.height = 0
709
710 ax_component_mock.get_rect.side_effect = [obj_rect, ancestor_rect]
711 ax_uc_mock = essential_modules["orca.ax_utilities_component"].AXUtilitiesComponent
712 ax_uc_mock.get_rect_intersection.return_value = intersection
713 ax_uc_mock.is_empty_rect.side_effect = [False, True]
714
715 ax_text_mock = essential_modules["orca.ax_text"].AXText
716 ax_text_mock.get_character_rect.return_value = empty_char_rect
717
718 ax_object_mock = essential_modules["orca.ax_object"].AXObject
719 ax_object_mock.find_ancestor.return_value = mock_ancestor
720
721 result = AXEventSynthesizer._is_scrolled_off_screen(mock_obj, 8)
722 assert result is False
723 essential_modules["orca.debug"].print_tokens.assert_called()
724
725 def test_mouse_event_on_object_grabs_focus_when_still_offscreen(
726 self,
727 test_context: OrcaTestContext,
728 ) -> None:
729 """Test _mouse_event_on_object grabs focus when object remains offscreen after scroll."""
730
731 essential_modules = self._setup_dependencies(test_context)
732 from orca.ax_event_synthesizer import AXEventSynthesizer
733
734 mock_obj = test_context.Mock(spec=Atspi.Accessible)
735
736 scroll_call_count = 0
737
738 def mock_scroll_into_view(_obj):
739 nonlocal scroll_call_count
740 scroll_call_count += 1
741
742 test_context.patch_object(AXEventSynthesizer, "scroll_into_view", new=mock_scroll_into_view)
743 test_context.patch_object(
744 AXEventSynthesizer,
745 "_is_scrolled_off_screen",
746 side_effect=lambda obj, offset=None: True,
747 )
748 test_context.patch_object(
749 AXEventSynthesizer,
750 "_generate_mouse_event",
751 side_effect=lambda obj, x, y, event: True,
752 )
753
754 rect = test_context.Mock()
755 rect.width = rect.height = 100
756 ax_component_mock = essential_modules["orca.ax_component"].AXComponent
757 ax_component_mock.get_rect.return_value = rect
758
759 ax_object_mock = essential_modules["orca.ax_object"].AXObject
760
761 result = AXEventSynthesizer._mouse_event_on_object(mock_obj, "click")
762 assert result is True
763 assert scroll_call_count == 1
764 ax_object_mock.grab_focus.assert_called_once_with(mock_obj)
765 essential_modules["orca.debug"].print_tokens.assert_called()