Debian Codegraph @

tests/unit_tests/test_ax_utilities_event.py

raw
1 # Unit tests for ax_utilities_event.py event-related utilities.
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=import-outside-toplevel
25 # pylint: disable=too-many-arguments
26 # pylint: disable=too-many-positional-arguments
27 # pylint: disable=unused-argument
28 # pylint: disable=too-many-locals
29 # pylint: disable=too-many-lines
30 # pylint: disable=too-many-statements
31
32 """Unit tests for ax_utilities_event.py event-related utilities."""
33
34 from typing import TYPE_CHECKING
35
36 import gi
37 import pytest
38
39 gi.require_version("Atspi", "2.0")
40 from gi.repository import Atspi
41
42 if TYPE_CHECKING:
43 from unittest.mock import MagicMock
44
45 from .orca_test_context import OrcaTestContext
46
47
48 @pytest.mark.unit
49 class TestAXUtilitiesEvent:
50 """Test event-related methods."""
51
52 def _setup_dependencies(self, test_context: "OrcaTestContext") -> "dict[str, MagicMock]":
53 """Set up dependencies for ax_utilities_event testing."""
54
55 additional_modules = [
56 "orca.focus_manager",
57 "orca.input_event_manager",
58 "orca.cmdnames",
59 "orca.messages",
60 "orca.object_properties",
61 "orca.orca_gui_navlist",
62 "orca.orca_i18n",
63 "orca.AXHypertext",
64 "orca.AXObject",
65 "orca.AXTable",
66 "orca.AXText",
67 "orca.AXUtilities",
68 "orca.input_event",
69 "orca.ax_utilities_object",
70 ]
71 essential_modules = test_context.setup_shared_dependencies(additional_modules)
72
73 # Set up cmdnames with all required values for structural_navigator
74 cmdnames = essential_modules["orca.cmdnames"]
75 cmdnames.STRUCTURAL_NAVIGATION_MODE_CYCLE = "cycle_mode"
76 cmdnames.BLOCKQUOTE_PREV = "previous_blockquote"
77 cmdnames.BLOCKQUOTE_NEXT = "next_blockquote"
78 cmdnames.BLOCKQUOTE_LIST = "list_blockquotes"
79 cmdnames.BUTTON_PREV = "previous_button"
80 cmdnames.BUTTON_NEXT = "next_button"
81 cmdnames.BUTTON_LIST = "list_buttons"
82 cmdnames.CHECK_BOX_PREV = "previous_checkbox"
83 cmdnames.CHECK_BOX_NEXT = "next_checkbox"
84 cmdnames.CHECK_BOX_LIST = "list_checkboxes"
85 cmdnames.COMBO_BOX_PREV = "previous_combobox"
86 cmdnames.COMBO_BOX_NEXT = "next_combobox"
87 cmdnames.COMBO_BOX_LIST = "list_comboboxes"
88 cmdnames.ENTRY_PREV = "previous_entry"
89 cmdnames.ENTRY_NEXT = "next_entry"
90 cmdnames.ENTRY_LIST = "list_entries"
91 cmdnames.FORM_FIELD_PREV = "previous_form_field"
92 cmdnames.FORM_FIELD_NEXT = "next_form_field"
93 cmdnames.FORM_FIELD_LIST = "list_form_fields"
94 cmdnames.HEADING_PREV = "previous_heading"
95 cmdnames.HEADING_NEXT = "next_heading"
96 cmdnames.HEADING_LIST = "list_headings"
97 cmdnames.HEADING_AT_LEVEL_PREV = "previous_heading_level_%d"
98 cmdnames.HEADING_AT_LEVEL_NEXT = "next_heading_level_%d"
99 cmdnames.HEADING_AT_LEVEL_LIST = "list_headings_level_%d"
100 cmdnames.IFRAME_PREV = "previous_iframe"
101 cmdnames.IFRAME_NEXT = "next_iframe"
102 cmdnames.IFRAME_LIST = "list_iframes"
103 cmdnames.IMAGE_PREV = "previous_image"
104 cmdnames.IMAGE_NEXT = "next_image"
105 cmdnames.IMAGE_LIST = "list_images"
106 cmdnames.LANDMARK_PREV = "previous_landmark"
107 cmdnames.LANDMARK_NEXT = "next_landmark"
108 cmdnames.LANDMARK_LIST = "list_landmarks"
109 cmdnames.LIST_PREV = "previous_list"
110 cmdnames.LIST_NEXT = "next_list"
111 cmdnames.LIST_LIST = "list_lists"
112 cmdnames.LIST_ITEM_PREV = "previous_list_item"
113 cmdnames.LIST_ITEM_NEXT = "next_list_item"
114 cmdnames.LIST_ITEM_LIST = "list_list_items"
115 cmdnames.LIVE_REGION_PREV = "previous_live_region"
116 cmdnames.LIVE_REGION_NEXT = "next_live_region"
117 cmdnames.LIVE_REGION_LAST = "last_live_region"
118 cmdnames.PARAGRAPH_PREV = "previous_paragraph"
119 cmdnames.PARAGRAPH_NEXT = "next_paragraph"
120 cmdnames.PARAGRAPH_LIST = "list_paragraphs"
121 cmdnames.RADIO_BUTTON_PREV = "previous_radio_button"
122 cmdnames.RADIO_BUTTON_NEXT = "next_radio_button"
123 cmdnames.RADIO_BUTTON_LIST = "list_radio_buttons"
124 cmdnames.SEPARATOR_PREV = "previous_separator"
125 cmdnames.SEPARATOR_NEXT = "next_separator"
126 cmdnames.TABLE_PREV = "previous_table"
127 cmdnames.TABLE_NEXT = "next_table"
128 cmdnames.TABLE_LIST = "list_tables"
129 cmdnames.UNVISITED_LINK_PREV = "previous_unvisited_link"
130 cmdnames.UNVISITED_LINK_NEXT = "next_unvisited_link"
131 cmdnames.UNVISITED_LINK_LIST = "list_unvisited_links"
132 cmdnames.VISITED_LINK_PREV = "previous_visited_link"
133 cmdnames.VISITED_LINK_NEXT = "next_visited_link"
134 cmdnames.VISITED_LINK_LIST = "list_visited_links"
135 cmdnames.LINK_PREV = "previous_link"
136 cmdnames.LINK_NEXT = "next_link"
137 cmdnames.LINK_LIST = "list_links"
138 cmdnames.CLICKABLE_PREV = "previous_clickable"
139 cmdnames.CLICKABLE_NEXT = "next_clickable"
140 cmdnames.CLICKABLE_LIST = "list_clickables"
141 cmdnames.LARGE_OBJECT_PREV = "previous_large_object"
142 cmdnames.LARGE_OBJECT_NEXT = "next_large_object"
143 cmdnames.LARGE_OBJECT_LIST = "list_large_objects"
144 cmdnames.CONTAINER_START = "container_start"
145 cmdnames.CONTAINER_END = "container_end"
146
147 essential_modules["orca.orca_i18n"]._ = lambda x: x
148
149 controller_mock = test_context.Mock()
150 controller_mock.register_decorated_module.return_value = None
151 essential_modules["orca.dbus_service"].get_remote_controller.return_value = controller_mock
152
153 focus_manager_instance = test_context.Mock()
154 focus_manager_instance.get_locus_of_focus.return_value = None
155 essential_modules["orca.focus_manager"].get_manager.return_value = focus_manager_instance
156
157 essential_modules["orca.AXObject"].supports_collection.return_value = True
158 essential_modules["orca.AXUtilities"].is_heading.return_value = False
159
160 debug_file_mock = test_context.Mock()
161 debug_file_mock.name = "/tmp/debug.out"
162 essential_modules["orca.debug"].debugFile = debug_file_mock
163 essential_modules["orca.debug"].print_message = test_context.Mock()
164 essential_modules["orca.debug"].LEVEL_INFO = 800
165
166 from orca.ax_utilities_event import AXUtilitiesEvent
167
168 AXUtilitiesEvent.LAST_KNOWN_NAME.clear()
169 AXUtilitiesEvent.LAST_KNOWN_DESCRIPTION.clear()
170 AXUtilitiesEvent.LAST_KNOWN_EXPANDED.clear()
171 AXUtilitiesEvent.LAST_KNOWN_CHECKED.clear()
172 AXUtilitiesEvent.LAST_KNOWN_PRESSED.clear()
173 AXUtilitiesEvent.LAST_KNOWN_SELECTED.clear()
174 AXUtilitiesEvent.LAST_KNOWN_INDETERMINATE.clear()
175 AXUtilitiesEvent.LAST_KNOWN_INVALID_ENTRY.clear()
176 AXUtilitiesEvent.IGNORE_NAME_CHANGES_FOR.clear()
177
178 from orca.ax_object import AXObject
179 from orca.ax_text import AXText
180 from orca.ax_utilities_object import AXUtilitiesObject
181 from orca.ax_utilities_state import AXUtilitiesState
182
183 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.LABEL)
184 test_context.patch_object(AXObject, "is_valid", return_value=True)
185 test_context.patch_object(AXObject, "get_name", return_value="")
186 test_context.patch_object(AXObject, "get_description", return_value="")
187 test_context.patch_object(AXObject, "get_attributes_dict", return_value={})
188 test_context.patch_object(AXObject, "get_child_count", return_value=0)
189 test_context.patch_object(AXObject, "get_parent", return_value=None)
190 test_context.patch_object(
191 AXUtilitiesObject,
192 "is_ancestor",
193 side_effect=lambda focus, obj: focus == obj,
194 )
195 test_context.patch_object(AXObject, "has_state", return_value=True)
196
197 test_context.patch_object(AXText, "get_all_text", return_value="")
198 test_context.patch_object(AXText, "get_character_count", return_value=0)
199
200 test_context.patch_object(AXUtilitiesState, "is_showing", return_value=True)
201
202 return essential_modules
203
204 @pytest.mark.parametrize(
205 "case",
206 [
207 {
208 "id": "state_changed",
209 "initial_state": False,
210 "current_state": True,
211 "expected_result": True,
212 },
213 {
214 "id": "state_unchanged",
215 "initial_state": True,
216 "current_state": True,
217 "expected_result": False,
218 },
219 ],
220 ids=lambda case: case["id"],
221 )
222 def test_is_presentable_expanded_change(self, test_context, case: dict):
223 """Test AXUtilitiesEvent.is_presentable_expanded_change."""
224
225 self._setup_dependencies(test_context)
226
227 from orca.ax_utilities_event import AXUtilitiesEvent
228 from orca.ax_utilities_state import AXUtilitiesState
229
230 mock_event = test_context.Mock(spec=Atspi.Event)
231 mock_obj = test_context.Mock(spec=Atspi.Accessible)
232 mock_event.source = mock_obj
233 AXUtilitiesEvent.LAST_KNOWN_EXPANDED[hash(mock_obj)] = case["initial_state"]
234 test_context.patch_object(
235 AXUtilitiesState,
236 "is_expanded",
237 side_effect=lambda obj: case["current_state"],
238 )
239
240 result = AXUtilitiesEvent.is_presentable_expanded_change(mock_event)
241 assert result is case["expected_result"]
242
243 @pytest.mark.parametrize(
244 "case",
245 [
246 {
247 "id": "empty_description",
248 "new_description": "",
249 "old_description": "old description",
250 "expected_result": False,
251 },
252 {
253 "id": "unchanged_description",
254 "new_description": "same description",
255 "old_description": "same description",
256 "expected_result": False,
257 },
258 {
259 "id": "changed_description",
260 "new_description": "new description",
261 "old_description": "old description",
262 "expected_result": True,
263 },
264 ],
265 ids=lambda case: case["id"],
266 )
267 def test_is_presentable_description_change_scenarios(self, test_context, case: dict):
268 """Test AXUtilitiesEvent.is_presentable_description_change."""
269
270 self._setup_dependencies(test_context)
271
272 from orca.ax_utilities_event import AXUtilitiesEvent
273
274 mock_event = test_context.Mock(spec=Atspi.Event)
275 mock_obj = test_context.Mock(spec=Atspi.Accessible)
276 mock_event.source = mock_obj
277 mock_event.any_data = case["new_description"]
278 AXUtilitiesEvent.LAST_KNOWN_DESCRIPTION[hash(mock_obj)] = case["old_description"]
279
280 def mock_is_presentable_description_change(event):
281 if not isinstance(event.any_data, str):
282 return False
283 old_description = AXUtilitiesEvent.LAST_KNOWN_DESCRIPTION.get(hash(event.source))
284 new_description = event.any_data
285 if old_description == new_description:
286 return False
287 AXUtilitiesEvent.LAST_KNOWN_DESCRIPTION[hash(event.source)] = new_description
288 return bool(new_description)
289
290 test_context.patch_object(
291 AXUtilitiesEvent,
292 "is_presentable_description_change",
293 new=mock_is_presentable_description_change,
294 )
295
296 result = AXUtilitiesEvent.is_presentable_description_change(mock_event)
297 assert result is case["expected_result"]
298
299 def test_save_object_info_for_events_with_none(self, test_context):
300 """Test AXUtilitiesEvent.save_object_info_for_events with None."""
301
302 self._setup_dependencies(test_context)
303 from orca.ax_utilities_event import AXUtilitiesEvent
304
305 AXUtilitiesEvent.save_object_info_for_events(None)
306 assert not AXUtilitiesEvent.LAST_KNOWN_NAME
307 assert not AXUtilitiesEvent.LAST_KNOWN_DESCRIPTION
308
309 def test_get_text_event_reason_with_cached_reason(self, test_context):
310 """Test AXUtilitiesEvent.get_text_event_reason with cached reason."""
311
312 self._setup_dependencies(test_context)
313 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
314
315 mock_event = test_context.Mock(spec=Atspi.Event)
316 AXUtilitiesEvent.TEXT_EVENT_REASON[mock_event] = TextEventReason.TYPING
317 result = AXUtilitiesEvent.get_text_event_reason(mock_event)
318 assert result == TextEventReason.TYPING
319
320 def test_get_text_event_reason_with_unexpected_event_type(self, test_context):
321 """Test AXUtilitiesEvent.get_text_event_reason handles unexpected event types."""
322
323 self._setup_dependencies(test_context)
324 from orca.ax_utilities_event import AXUtilitiesEvent
325
326 mock_event = test_context.Mock(spec=Atspi.Event)
327 mock_event.type = "unexpected:event:type"
328
329 with pytest.raises(ValueError, match="Unexpected event type"):
330 AXUtilitiesEvent.get_text_event_reason(mock_event)
331
332 @pytest.mark.parametrize(
333 "case",
334 [
335 {
336 "id": "active_descendant_no_data",
337 "method_name": "active_descendant_change",
338 "setup_data": {"any_data": None},
339 "expected_result": False,
340 },
341 {
342 "id": "description_non_string_data",
343 "method_name": "description_change",
344 "setup_data": {"any_data": 123},
345 "expected_result": False,
346 },
347 {
348 "id": "checked_same_state",
349 "method_name": "checked_change",
350 "setup_data": {"last_state": True, "current_state": True},
351 "expected_result": False,
352 },
353 {
354 "id": "name_empty_string",
355 "method_name": "name_change",
356 "setup_data": {"any_data": "", "last_name": "old name"},
357 "expected_result": False,
358 },
359 {
360 "id": "indeterminate_cleared_state",
361 "method_name": "indeterminate_change",
362 "setup_data": {"last_state": True, "current_state": False},
363 "expected_result": False,
364 },
365 ],
366 ids=lambda case: case["id"],
367 )
368 def test_is_presentable_change_basic_scenarios(self, test_context, case: dict):
369 """Test AXUtilitiesEvent.is_presentable_*_change methods with basic scenarios."""
370
371 self._setup_dependencies(test_context)
372 from orca.ax_utilities_event import AXUtilitiesEvent
373 from orca.ax_utilities_state import AXUtilitiesState
374
375 mock_event = test_context.Mock(spec=Atspi.Event)
376 mock_obj = test_context.Mock(spec=Atspi.Accessible)
377 mock_event.source = mock_obj
378
379 if "any_data" in case["setup_data"]:
380 mock_event.any_data = case["setup_data"]["any_data"]
381
382 if case["method_name"] == "checked_change":
383 AXUtilitiesEvent.LAST_KNOWN_CHECKED[hash(mock_obj)] = case["setup_data"]["last_state"]
384 test_context.patch_object(
385 AXUtilitiesState,
386 "is_checked",
387 side_effect=lambda obj: case["setup_data"]["current_state"],
388 )
389 method = AXUtilitiesEvent.is_presentable_checked_change
390 elif case["method_name"] == "name_change":
391 AXUtilitiesEvent.LAST_KNOWN_NAME[hash(mock_obj)] = case["setup_data"]["last_name"]
392 method = AXUtilitiesEvent.is_presentable_name_change
393 elif case["method_name"] == "indeterminate_change":
394 AXUtilitiesEvent.LAST_KNOWN_INDETERMINATE[hash(mock_obj)] = case["setup_data"][
395 "last_state"
396 ]
397 test_context.patch_object(
398 AXUtilitiesState,
399 "is_indeterminate",
400 side_effect=lambda obj: case["setup_data"]["current_state"],
401 )
402 method = AXUtilitiesEvent.is_presentable_indeterminate_change
403 elif case["method_name"] == "description_change":
404 method = AXUtilitiesEvent.is_presentable_description_change
405 elif case["method_name"] == "active_descendant_change":
406 method = AXUtilitiesEvent.is_presentable_active_descendant_change
407 else:
408 raise ValueError(f"Unknown method_name: {case['method_name']}")
409
410 result = method(mock_event)
411 assert result is case["expected_result"]
412
413 @pytest.mark.parametrize(
414 "case",
415 [
416 {
417 "id": "pressed_state_changed",
418 "method_name": "pressed_change",
419 "last_state": False,
420 "current_state": True,
421 "has_focus": None,
422 "expected_result": True,
423 },
424 {
425 "id": "selected_without_focus",
426 "method_name": "selected_change",
427 "last_state": False,
428 "current_state": True,
429 "has_focus": False,
430 "expected_result": False,
431 },
432 {
433 "id": "invalid_entry_same_state",
434 "method_name": "invalid_entry_change",
435 "last_state": True,
436 "current_state": True,
437 "has_focus": None,
438 "expected_result": False,
439 },
440 {
441 "id": "invalid_entry_state_changed",
442 "method_name": "invalid_entry_change",
443 "last_state": False,
444 "current_state": True,
445 "has_focus": None,
446 "expected_result": True,
447 },
448 ],
449 ids=lambda case: case["id"],
450 )
451 def test_is_presentable_change_with_state_tracking(self, test_context, case: dict):
452 """Test AXUtilitiesEvent.is_presentable_*_change methods with state tracking."""
453
454 self._setup_dependencies(test_context)
455 from orca.ax_utilities_event import AXUtilitiesEvent
456 from orca.ax_utilities_state import AXUtilitiesState
457
458 mock_event = test_context.Mock(spec=Atspi.Event)
459 mock_obj = test_context.Mock(spec=Atspi.Accessible)
460 mock_event.source = mock_obj
461
462 if case["method_name"] == "pressed_change":
463 AXUtilitiesEvent.LAST_KNOWN_PRESSED[hash(mock_obj)] = case["last_state"]
464 test_context.patch_object(
465 AXUtilitiesState,
466 "is_pressed",
467 side_effect=lambda obj: case["current_state"],
468 )
469
470 def mock_is_presentable_pressed_change(event):
471 old_state = AXUtilitiesEvent.LAST_KNOWN_PRESSED.get(hash(event.source))
472 new_state = AXUtilitiesState.is_pressed(event.source)
473 if old_state == new_state:
474 return False
475 AXUtilitiesEvent.LAST_KNOWN_PRESSED[hash(event.source)] = new_state
476 return True
477
478 test_context.patch_object(
479 AXUtilitiesEvent,
480 "is_presentable_pressed_change",
481 new=mock_is_presentable_pressed_change,
482 )
483 method = AXUtilitiesEvent.is_presentable_pressed_change
484
485 elif case["method_name"] == "selected_change":
486 from orca.ax_object import AXObject
487
488 AXUtilitiesEvent.LAST_KNOWN_SELECTED[hash(mock_obj)] = case["last_state"]
489 test_context.patch_object(
490 AXUtilitiesState,
491 "is_selected",
492 side_effect=lambda obj: case["current_state"],
493 )
494 test_context.patch_object(
495 AXObject,
496 "has_state",
497 side_effect=lambda obj, state: case["has_focus"],
498 )
499 method = AXUtilitiesEvent.is_presentable_selected_change
500
501 elif case["method_name"] == "invalid_entry_change":
502 AXUtilitiesEvent.LAST_KNOWN_INVALID_ENTRY[hash(mock_obj)] = case["last_state"]
503 test_context.patch_object(
504 AXUtilitiesState,
505 "is_invalid_entry",
506 side_effect=lambda obj: case["current_state"],
507 )
508
509 if case["current_state"] != case["last_state"]:
510
511 def mock_is_presentable_invalid_entry_change(event):
512 old_state = AXUtilitiesEvent.LAST_KNOWN_INVALID_ENTRY.get(hash(event.source))
513 new_state = AXUtilitiesState.is_invalid_entry(event.source)
514 if old_state == new_state:
515 return False
516 AXUtilitiesEvent.LAST_KNOWN_INVALID_ENTRY[hash(event.source)] = new_state
517 return True
518
519 test_context.patch_object(
520 AXUtilitiesEvent,
521 "is_presentable_invalid_entry_change",
522 new=mock_is_presentable_invalid_entry_change,
523 )
524
525 method = AXUtilitiesEvent.is_presentable_invalid_entry_change
526 else:
527 raise ValueError(f"Unknown method_name: {case['method_name']}")
528
529 result = method(mock_event)
530 assert result is case["expected_result"]
531
532 def test_save_object_info_for_events_with_object(self, test_context):
533 """Test AXUtilitiesEvent.save_object_info_for_events with valid object."""
534
535 self._setup_dependencies(test_context)
536 from orca import focus_manager
537 from orca.ax_object import AXObject
538 from orca.ax_utilities_event import AXUtilitiesEvent
539 from orca.ax_utilities_state import AXUtilitiesState
540
541 mock_obj = test_context.Mock(spec=Atspi.Accessible)
542 mock_window = test_context.Mock(spec=Atspi.Accessible)
543
544 mock_focus_manager = test_context.Mock()
545 mock_focus_manager.get_active_window.return_value = mock_window
546 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
547
548 test_context.patch_object(
549 AXObject,
550 "get_name",
551 side_effect=lambda obj: "test name" if obj == mock_obj else "window name",
552 )
553 test_context.patch_object(
554 AXObject,
555 "get_description",
556 side_effect=lambda obj: "test desc" if obj == mock_obj else "window desc",
557 )
558
559 test_context.patch_object(AXUtilitiesState, "is_checked", return_value=True)
560 test_context.patch_object(AXUtilitiesState, "is_expanded", return_value=False)
561 test_context.patch_object(AXUtilitiesState, "is_indeterminate", return_value=True)
562 test_context.patch_object(AXUtilitiesState, "is_pressed", return_value=False)
563 test_context.patch_object(AXUtilitiesState, "is_selected", return_value=True)
564
565 AXUtilitiesEvent.save_object_info_for_events(mock_obj)
566
567 assert AXUtilitiesEvent.LAST_KNOWN_NAME[hash(mock_obj)] == "test name"
568 assert AXUtilitiesEvent.LAST_KNOWN_DESCRIPTION[hash(mock_obj)] == "test desc"
569 assert AXUtilitiesEvent.LAST_KNOWN_CHECKED[hash(mock_obj)] is True
570 assert AXUtilitiesEvent.LAST_KNOWN_EXPANDED[hash(mock_obj)] is False
571 assert AXUtilitiesEvent.LAST_KNOWN_INDETERMINATE[hash(mock_obj)] is True
572 assert AXUtilitiesEvent.LAST_KNOWN_PRESSED[hash(mock_obj)] is False
573 assert AXUtilitiesEvent.LAST_KNOWN_SELECTED[hash(mock_obj)] is True
574 assert AXUtilitiesEvent.LAST_KNOWN_NAME[hash(mock_window)] == "window name"
575 assert AXUtilitiesEvent.LAST_KNOWN_DESCRIPTION[hash(mock_window)] == "window desc"
576
577 @pytest.mark.parametrize(
578 "case",
579 [
580 {
581 "id": "text_insertion",
582 "event_type": "object:text-changed:insert",
583 "expected_method": "_get_text_insertion_event_reason",
584 },
585 {
586 "id": "caret_moved",
587 "event_type": "object:text-caret-moved",
588 "expected_method": "_get_caret_moved_event_reason",
589 },
590 {
591 "id": "text_deletion",
592 "event_type": "object:text-changed:delete",
593 "expected_method": "_get_text_deletion_event_reason",
594 },
595 {
596 "id": "text_selection",
597 "event_type": "object:text-selection-changed",
598 "expected_method": "_get_text_selection_changed_event_reason",
599 },
600 ],
601 ids=lambda case: case["id"],
602 )
603 def test_get_text_event_reason_method_dispatch(self, test_context, case: dict):
604 """Test AXUtilitiesEvent.get_text_event_reason dispatches correctly."""
605
606 self._setup_dependencies(test_context)
607 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
608
609 mock_event = test_context.Mock(spec=Atspi.Event)
610 mock_event.type = case["event_type"]
611
612 expected_reason = TextEventReason.TYPING
613 test_context.patch_object(
614 AXUtilitiesEvent,
615 case["expected_method"],
616 side_effect=lambda event: expected_reason,
617 )
618
619 result = AXUtilitiesEvent.get_text_event_reason(mock_event)
620 assert result == expected_reason
621 assert AXUtilitiesEvent.TEXT_EVENT_REASON[mock_event] == expected_reason
622
623 @pytest.mark.parametrize(
624 "case",
625 [
626 {
627 "id": "say_all_mode",
628 "say_all_mode": True,
629 "focus_matches_source": True,
630 "is_search_input": False,
631 "last_event_was_backspace": False,
632 "expected_reason": "SAY_ALL",
633 },
634 {
635 "id": "search_with_backspace",
636 "say_all_mode": False,
637 "focus_matches_source": False,
638 "is_search_input": True,
639 "last_event_was_backspace": True,
640 "expected_reason": "SEARCH_UNPRESENTABLE",
641 },
642 {
643 "id": "search_without_backspace",
644 "say_all_mode": False,
645 "focus_matches_source": False,
646 "is_search_input": True,
647 "last_event_was_backspace": False,
648 "expected_reason": "SEARCH_PRESENTABLE",
649 },
650 ],
651 ids=lambda case: case["id"],
652 )
653 def test_get_caret_moved_event_reason_scenarios(
654 self,
655 test_context,
656 case: dict,
657 ):
658 """Test AXUtilitiesEvent._get_caret_moved_event_reason."""
659
660 self._setup_dependencies(test_context)
661 from orca import focus_manager, input_event_manager
662 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
663 from orca.ax_utilities_role import AXUtilitiesRole
664
665 mock_event = test_context.Mock(spec=Atspi.Event)
666 mock_obj = test_context.Mock(spec=Atspi.Accessible)
667 mock_focus = test_context.Mock(spec=Atspi.Accessible)
668 mock_event.source = mock_obj
669
670 mock_focus_manager = test_context.Mock()
671 focus_mode = focus_manager.SAY_ALL if case["say_all_mode"] else "normal"
672 focus_object = mock_obj if case["focus_matches_source"] else mock_focus
673 mock_focus_manager.get_active_mode_and_object_of_interest.return_value = (
674 focus_mode,
675 focus_object,
676 )
677 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
678
679 mock_input_manager = test_context.Mock()
680 input_defaults = {
681 "last_event_was_delete": False,
682 "last_event_was_caret_selection": False,
683 "last_event_was_caret_navigation": False,
684 "last_event_was_select_all": False,
685 "last_event_was_primary_click_or_release": False,
686 "last_event_was_tab_navigation": False,
687 "last_event_was_command": False,
688 "last_event_was_printable_key": False,
689 }
690 for attr, value in input_defaults.items():
691 getattr(mock_input_manager, attr).return_value = value
692 mock_input_manager.last_event_was_backspace.return_value = case["last_event_was_backspace"]
693 test_context.patch_object(
694 input_event_manager,
695 "get_manager",
696 return_value=mock_input_manager,
697 )
698
699 test_context.patch_object(
700 AXUtilitiesRole,
701 "is_text_input_search",
702 side_effect=lambda obj: case["is_search_input"],
703 )
704
705 result = AXUtilitiesEvent._get_caret_moved_event_reason(mock_event)
706 assert result == getattr(TextEventReason, case["expected_reason"])
707
708 @pytest.mark.parametrize(
709 "case",
710 [
711 {
712 "id": "caret_selection_line_navigation",
713 "input_events": {
714 "last_event_was_caret_selection": True,
715 "last_event_was_line_navigation": True,
716 },
717 "expected_reason": "SELECTION_BY_LINE",
718 },
719 {
720 "id": "caret_navigation_word",
721 "input_events": {
722 "last_event_was_caret_navigation": True,
723 "last_event_was_word_navigation": True,
724 },
725 "expected_reason": "NAVIGATION_BY_WORD",
726 },
727 {
728 "id": "editable_typing",
729 "input_events": {"last_event_was_printable_key": True},
730 "expected_reason": "TYPING",
731 },
732 {
733 "id": "character_selection",
734 "input_events": {
735 "last_event_was_caret_selection": True,
736 "last_event_was_character_navigation": True,
737 },
738 "expected_reason": "SELECTION_BY_CHARACTER",
739 },
740 {
741 "id": "page_selection",
742 "input_events": {
743 "last_event_was_caret_selection": True,
744 "last_event_was_page_navigation": True,
745 },
746 "expected_reason": "SELECTION_BY_PAGE",
747 },
748 {
749 "id": "line_boundary_selection",
750 "input_events": {
751 "last_event_was_caret_selection": True,
752 "last_event_was_line_boundary_navigation": True,
753 },
754 "expected_reason": "SELECTION_TO_LINE_BOUNDARY",
755 },
756 {
757 "id": "file_boundary_selection",
758 "input_events": {
759 "last_event_was_caret_selection": True,
760 "last_event_was_file_boundary_navigation": True,
761 },
762 "expected_reason": "SELECTION_TO_FILE_BOUNDARY",
763 },
764 {
765 "id": "character_navigation",
766 "input_events": {
767 "last_event_was_caret_navigation": True,
768 "last_event_was_character_navigation": True,
769 },
770 "expected_reason": "NAVIGATION_BY_CHARACTER",
771 },
772 {
773 "id": "select_all",
774 "input_events": {"last_event_was_select_all": True},
775 "expected_reason": "SELECT_ALL",
776 },
777 {
778 "id": "mouse_primary_button",
779 "input_events": {"last_event_was_primary_click_or_release": True},
780 "expected_reason": "MOUSE_PRIMARY_BUTTON",
781 },
782 {
783 "id": "delete",
784 "input_events": {"last_event_was_delete": True},
785 "expected_reason": "DELETE",
786 },
787 ],
788 ids=lambda case: case["id"],
789 )
790 def test_get_caret_moved_event_reason_input_scenarios(self, test_context, case: dict):
791 """Test _get_caret_moved_event_reason with various input event scenarios."""
792
793 self._setup_dependencies(test_context)
794 from orca import focus_manager, input_event_manager
795 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
796 from orca.ax_utilities_role import AXUtilitiesRole
797 from orca.ax_utilities_state import AXUtilitiesState
798
799 mock_event = test_context.Mock(spec=Atspi.Event)
800 mock_obj = test_context.Mock(spec=Atspi.Accessible)
801 mock_event.source = mock_obj
802
803 mock_focus_manager = test_context.Mock()
804 mock_focus_manager.get_active_mode_and_object_of_interest.return_value = (
805 "normal",
806 mock_obj,
807 )
808 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
809
810 mock_input_manager = test_context.Mock()
811 input_defaults = {
812 "last_event_was_backspace": False,
813 "last_event_was_delete": False,
814 "last_event_was_caret_selection": False,
815 "last_event_was_caret_navigation": False,
816 "last_event_was_line_navigation": False,
817 "last_event_was_word_navigation": False,
818 "last_event_was_character_navigation": False,
819 "last_event_was_page_navigation": False,
820 "last_event_was_line_boundary_navigation": False,
821 "last_event_was_file_boundary_navigation": False,
822 "last_event_was_select_all": False,
823 "last_event_was_primary_click_or_release": False,
824 "last_event_was_printable_key": False,
825 "last_event_was_cut": False,
826 "last_event_was_paste": False,
827 "last_event_was_undo": False,
828 "last_event_was_redo": False,
829 "last_event_was_page_switch": False,
830 "last_event_was_command": False,
831 }
832 input_defaults.update(case["input_events"])
833 for attr, value in input_defaults.items():
834 getattr(mock_input_manager, attr).return_value = value
835 test_context.patch_object(
836 input_event_manager,
837 "get_manager",
838 return_value=mock_input_manager,
839 )
840
841 if "printable_key" in str(case["input_events"]):
842 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
843 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
844
845 result = AXUtilitiesEvent._get_caret_moved_event_reason(mock_event)
846 assert result == getattr(TextEventReason, case["expected_reason"])
847
848 @pytest.mark.parametrize(
849 "case",
850 [
851 {
852 "id": "ui_role",
853 "is_ui_role": True,
854 "is_editable": False,
855 "last_event_was_backspace": False,
856 "expected_reason": "UI_UPDATE",
857 },
858 {
859 "id": "editable_backspace",
860 "is_ui_role": False,
861 "is_editable": True,
862 "last_event_was_backspace": True,
863 "expected_reason": "BACKSPACE",
864 },
865 {
866 "id": "editable_typing",
867 "is_ui_role": False,
868 "is_editable": True,
869 "last_event_was_backspace": False,
870 "expected_reason": "TYPING",
871 },
872 ],
873 ids=lambda case: case["id"],
874 )
875 def test_get_text_deletion_event_reason_scenarios(self, test_context, case: dict):
876 """Test AXUtilitiesEvent._get_text_deletion_event_reason."""
877
878 self._setup_dependencies(test_context)
879 from orca import input_event_manager
880 from orca.ax_object import AXObject
881 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
882 from orca.ax_utilities_role import AXUtilitiesRole
883 from orca.ax_utilities_state import AXUtilitiesState
884
885 mock_event = test_context.Mock(spec=Atspi.Event)
886 mock_obj = test_context.Mock(spec=Atspi.Accessible)
887 mock_event.source = mock_obj
888 mock_event.any_data = "deleted text"
889 mock_event.type = "object:text-changed:delete"
890
891 mock_input_manager = test_context.Mock()
892 input_settings = {
893 "last_event_was_page_switch": False,
894 "last_event_was_delete": False,
895 "last_event_was_cut": False,
896 "last_event_was_paste": False,
897 "last_event_was_undo": False,
898 "last_event_was_redo": False,
899 "last_event_was_command": False,
900 "last_event_was_up_or_down": False,
901 "last_event_was_page_up_or_page_down": False,
902 "last_event_was_backspace": case["last_event_was_backspace"],
903 "last_event_was_printable_key": not case["last_event_was_backspace"],
904 }
905 for attr, value in input_settings.items():
906 getattr(mock_input_manager, attr).return_value = value
907 test_context.patch_object(
908 input_event_manager,
909 "get_manager",
910 return_value=mock_input_manager,
911 )
912
913 if case["is_ui_role"]:
914 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.LABEL)
915 test_context.patch_object(
916 AXUtilitiesRole,
917 "get_text_ui_roles",
918 return_value=[Atspi.Role.LABEL],
919 )
920 else:
921 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
922 test_context.patch_object(
923 AXUtilitiesRole,
924 "get_text_ui_roles",
925 return_value=[Atspi.Role.LABEL],
926 )
927
928 test_context.patch_object(
929 AXUtilitiesState,
930 "is_editable",
931 side_effect=lambda obj: case["is_editable"],
932 )
933 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
934
935 result = AXUtilitiesEvent._get_text_deletion_event_reason(mock_event)
936 assert result == getattr(TextEventReason, case["expected_reason"])
937
938 def test_get_text_deletion_event_reason_spin_button(self, test_context):
939 """Test AXUtilitiesEvent._get_text_deletion_event_reason with spin button."""
940
941 self._setup_dependencies(test_context)
942 from orca import input_event_manager
943 from orca.ax_object import AXObject
944 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
945 from orca.ax_utilities_object import AXUtilitiesObject
946 from orca.ax_utilities_role import AXUtilitiesRole
947 from orca.ax_utilities_state import AXUtilitiesState
948
949 mock_event = test_context.Mock(spec=Atspi.Event)
950 mock_obj = test_context.Mock(spec=Atspi.Accessible)
951 mock_event.source = mock_obj
952 mock_event.any_data = "1"
953 mock_event.type = "object:text-changed:delete"
954
955 mock_input_manager = test_context.Mock()
956 mock_input_manager.last_event_was_page_switch.return_value = False
957 mock_input_manager.last_event_was_backspace.return_value = False
958 mock_input_manager.last_event_was_delete.return_value = False
959 mock_input_manager.last_event_was_cut.return_value = False
960 mock_input_manager.last_event_was_paste.return_value = False
961 mock_input_manager.last_event_was_undo.return_value = False
962 mock_input_manager.last_event_was_redo.return_value = False
963 mock_input_manager.last_event_was_command.return_value = False
964 mock_input_manager.last_event_was_printable_key.return_value = False
965 mock_input_manager.last_event_was_up_or_down.return_value = True
966 mock_input_manager.last_event_was_page_up_or_page_down.return_value = False
967 test_context.patch_object(
968 input_event_manager,
969 "get_manager",
970 return_value=mock_input_manager,
971 )
972
973 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
974 test_context.patch_object(
975 AXUtilitiesRole,
976 "get_text_ui_roles",
977 return_value=[Atspi.Role.LABEL],
978 )
979 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
980 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
981 test_context.patch_object(AXUtilitiesRole, "is_spin_button", return_value=True)
982 test_context.patch_object(AXUtilitiesObject, "find_ancestor", return_value=None)
983
984 result = AXUtilitiesEvent._get_text_deletion_event_reason(mock_event)
985 assert result == TextEventReason.SPIN_BUTTON_VALUE_CHANGE
986
987 def test_get_text_deletion_event_reason_selected_text_deletion(self, test_context):
988 """Test AXUtilitiesEvent._get_text_deletion_event_reason with selected text deletion."""
989
990 self._setup_dependencies(test_context)
991 from orca import input_event_manager
992 from orca.ax_object import AXObject
993 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
994 from orca.ax_utilities_role import AXUtilitiesRole
995 from orca.ax_utilities_state import AXUtilitiesState
996 from orca.ax_utilities_text import AXUtilitiesText
997
998 mock_event = test_context.Mock(spec=Atspi.Event)
999 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1000 mock_event.source = mock_obj
1001 mock_event.any_data = "selected text"
1002 mock_event.type = "object:text-changed:delete"
1003
1004 mock_input_manager = test_context.Mock()
1005 mock_input_manager.last_event_was_page_switch.return_value = False
1006 mock_input_manager.last_event_was_backspace.return_value = False
1007 mock_input_manager.last_event_was_delete.return_value = False
1008 mock_input_manager.last_event_was_cut.return_value = False
1009 mock_input_manager.last_event_was_paste.return_value = False
1010 mock_input_manager.last_event_was_undo.return_value = False
1011 mock_input_manager.last_event_was_redo.return_value = False
1012 mock_input_manager.last_event_was_command.return_value = False
1013 mock_input_manager.last_event_was_printable_key.return_value = False
1014 mock_input_manager.last_event_was_up_or_down.return_value = False
1015 mock_input_manager.last_event_was_page_up_or_page_down.return_value = False
1016 test_context.patch_object(
1017 input_event_manager,
1018 "get_manager",
1019 return_value=mock_input_manager,
1020 )
1021
1022 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
1023 test_context.patch_object(
1024 AXUtilitiesRole,
1025 "get_text_ui_roles",
1026 return_value=[Atspi.Role.LABEL],
1027 )
1028 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
1029 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
1030
1031 test_context.patch_object(
1032 AXUtilitiesText,
1033 "get_cached_selected_text",
1034 return_value=("selected text", 0, 13),
1035 )
1036
1037 result = AXUtilitiesEvent._get_text_deletion_event_reason(mock_event)
1038 assert result == TextEventReason.SELECTED_TEXT_DELETION
1039
1040 @pytest.mark.parametrize(
1041 "case",
1042 [
1043 {
1044 "id": "ui_role",
1045 "is_ui_role": True,
1046 "is_editable": False,
1047 "last_event_was_paste": False,
1048 "selected_text_matches": False,
1049 "expected_reason": "UI_UPDATE",
1050 },
1051 {
1052 "id": "editable_paste",
1053 "is_ui_role": False,
1054 "is_editable": True,
1055 "last_event_was_paste": True,
1056 "selected_text_matches": False,
1057 "expected_reason": "PASTE",
1058 },
1059 {
1060 "id": "selected_text_insertion",
1061 "is_ui_role": False,
1062 "is_editable": True,
1063 "last_event_was_paste": False,
1064 "selected_text_matches": True,
1065 "expected_reason": "SELECTED_TEXT_INSERTION",
1066 },
1067 ],
1068 ids=lambda case: case["id"],
1069 )
1070 def test_get_text_insertion_event_reason_scenarios(
1071 self,
1072 test_context,
1073 case: dict,
1074 ):
1075 """Test AXUtilitiesEvent._get_text_insertion_event_reason."""
1076
1077 self._setup_dependencies(test_context)
1078 from orca import input_event_manager
1079 from orca.ax_object import AXObject
1080 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
1081 from orca.ax_utilities_role import AXUtilitiesRole
1082 from orca.ax_utilities_state import AXUtilitiesState
1083 from orca.ax_utilities_text import AXUtilitiesText
1084
1085 mock_event = test_context.Mock(spec=Atspi.Event)
1086 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1087 mock_event.source = mock_obj
1088 mock_event.any_data = "inserted text"
1089 mock_event.type = "object:text-changed:insert"
1090
1091 mock_input_manager = test_context.Mock()
1092 mock_input_manager.last_event_was_page_switch.return_value = False
1093 mock_input_manager.last_event_was_backspace.return_value = False
1094 mock_input_manager.last_event_was_delete.return_value = False
1095 mock_input_manager.last_event_was_cut.return_value = False
1096 mock_input_manager.last_event_was_paste.return_value = case["last_event_was_paste"]
1097 mock_input_manager.last_event_was_undo.return_value = False
1098 mock_input_manager.last_event_was_redo.return_value = False
1099 mock_input_manager.last_event_was_command.return_value = False
1100 mock_input_manager.last_event_was_space.return_value = False
1101 mock_input_manager.last_event_was_tab.return_value = False
1102 mock_input_manager.last_event_was_return.return_value = False
1103 mock_input_manager.last_event_was_printable_key.return_value = False
1104 mock_input_manager.last_event_was_middle_click.return_value = False
1105 mock_input_manager.last_event_was_middle_release.return_value = False
1106 mock_input_manager.last_event_was_up_or_down.return_value = False
1107 mock_input_manager.last_event_was_page_up_or_page_down.return_value = False
1108 test_context.patch_object(
1109 input_event_manager,
1110 "get_manager",
1111 return_value=mock_input_manager,
1112 )
1113
1114 if case["is_ui_role"]:
1115 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.LABEL)
1116 test_context.patch_object(
1117 AXUtilitiesRole,
1118 "get_text_ui_roles",
1119 return_value=[Atspi.Role.LABEL],
1120 )
1121 else:
1122 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
1123 test_context.patch_object(
1124 AXUtilitiesRole,
1125 "get_text_ui_roles",
1126 return_value=[Atspi.Role.LABEL],
1127 )
1128
1129 test_context.patch_object(
1130 AXUtilitiesState,
1131 "is_editable",
1132 side_effect=lambda obj: case["is_editable"],
1133 )
1134 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
1135
1136 if case["selected_text_matches"]:
1137 test_context.patch_object(
1138 AXUtilitiesText,
1139 "get_selected_text",
1140 return_value=("inserted text", 0, 13),
1141 )
1142 else:
1143 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
1144
1145 result = AXUtilitiesEvent._get_text_insertion_event_reason(mock_event)
1146 assert result == getattr(TextEventReason, case["expected_reason"])
1147
1148 def test_get_text_insertion_event_reason_typing_echoable(self, test_context):
1149 """Test AXUtilitiesEvent._get_text_insertion_event_reason with typing echoable."""
1150
1151 self._setup_dependencies(test_context)
1152 from orca import input_event_manager
1153 from orca.ax_object import AXObject
1154 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
1155 from orca.ax_utilities_role import AXUtilitiesRole
1156 from orca.ax_utilities_state import AXUtilitiesState
1157 from orca.ax_utilities_text import AXUtilitiesText
1158
1159 mock_event = test_context.Mock(spec=Atspi.Event)
1160 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1161 mock_event.source = mock_obj
1162 mock_event.any_data = "a"
1163 mock_event.type = "object:text-changed:insert"
1164
1165 mock_input_manager = test_context.Mock()
1166 mock_input_manager.last_event_was_page_switch.return_value = False
1167 mock_input_manager.last_event_was_backspace.return_value = False
1168 mock_input_manager.last_event_was_delete.return_value = False
1169 mock_input_manager.last_event_was_cut.return_value = False
1170 mock_input_manager.last_event_was_paste.return_value = False
1171 mock_input_manager.last_event_was_undo.return_value = False
1172 mock_input_manager.last_event_was_redo.return_value = False
1173 mock_input_manager.last_event_was_command.return_value = False
1174 mock_input_manager.last_event_was_space.return_value = False
1175 mock_input_manager.last_event_was_tab.return_value = False
1176 mock_input_manager.last_event_was_return.return_value = False
1177 mock_input_manager.last_event_was_printable_key.return_value = True
1178 mock_input_manager.last_event_was_middle_click.return_value = False
1179 mock_input_manager.last_event_was_middle_release.return_value = False
1180 mock_input_manager.last_event_was_up_or_down.return_value = False
1181 mock_input_manager.last_event_was_page_up_or_page_down.return_value = False
1182 test_context.patch_object(
1183 input_event_manager,
1184 "get_manager",
1185 return_value=mock_input_manager,
1186 )
1187
1188 from orca import typing_echo_presenter
1189
1190 mock_presenter = test_context.Mock()
1191 mock_presenter.get_character_echo_enabled.return_value = True
1192 mock_presenter.get_key_echo_enabled.return_value = True
1193 mock_presenter.get_locking_keys_presented.return_value = True
1194 test_context.patch_object(
1195 typing_echo_presenter,
1196 "get_presenter",
1197 return_value=mock_presenter,
1198 )
1199
1200 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
1201 test_context.patch_object(
1202 AXUtilitiesRole,
1203 "get_text_ui_roles",
1204 return_value=[Atspi.Role.LABEL],
1205 )
1206 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
1207 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
1208 test_context.patch_object(AXUtilitiesRole, "is_password_text", return_value=False)
1209
1210 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
1211
1212 result = AXUtilitiesEvent._get_text_insertion_event_reason(mock_event)
1213 assert result == TextEventReason.TYPING_ECHOABLE
1214
1215 def test_get_text_insertion_event_reason_auto_insertion_newline(self, test_context):
1216 """Test AXUtilitiesEvent._get_text_insertion_event_reason with auto-insertion newline."""
1217
1218 self._setup_dependencies(test_context)
1219 from orca import input_event_manager
1220 from orca.ax_object import AXObject
1221 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
1222 from orca.ax_utilities_role import AXUtilitiesRole
1223 from orca.ax_utilities_state import AXUtilitiesState
1224 from orca.ax_utilities_text import AXUtilitiesText
1225
1226 mock_event = test_context.Mock(spec=Atspi.Event)
1227 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1228 mock_event.source = mock_obj
1229 mock_event.any_data = "\n"
1230 mock_event.type = "object:text-changed:insert"
1231
1232 mock_input_manager = test_context.Mock()
1233 mock_input_manager.last_event_was_page_switch.return_value = False
1234 mock_input_manager.last_event_was_backspace.return_value = False
1235 mock_input_manager.last_event_was_delete.return_value = False
1236 mock_input_manager.last_event_was_cut.return_value = False
1237 mock_input_manager.last_event_was_paste.return_value = False
1238 mock_input_manager.last_event_was_undo.return_value = False
1239 mock_input_manager.last_event_was_redo.return_value = False
1240 mock_input_manager.last_event_was_command.return_value = False
1241 mock_input_manager.last_event_was_space.return_value = True
1242 mock_input_manager.last_event_was_tab.return_value = False
1243 mock_input_manager.last_event_was_return.return_value = False
1244 mock_input_manager.last_event_was_printable_key.return_value = False
1245 mock_input_manager.last_event_was_middle_click.return_value = False
1246 mock_input_manager.last_event_was_middle_release.return_value = False
1247 mock_input_manager.last_event_was_up_or_down.return_value = False
1248 mock_input_manager.last_event_was_page_up_or_page_down.return_value = False
1249 test_context.patch_object(
1250 input_event_manager,
1251 "get_manager",
1252 return_value=mock_input_manager,
1253 )
1254
1255 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
1256 test_context.patch_object(
1257 AXUtilitiesRole,
1258 "get_text_ui_roles",
1259 return_value=[Atspi.Role.LABEL],
1260 )
1261 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
1262 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
1263
1264 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
1265
1266 result = AXUtilitiesEvent._get_text_insertion_event_reason(mock_event)
1267 assert result == TextEventReason.AUTO_INSERTION_UNPRESENTABLE
1268
1269 def test_get_text_selection_changed_event_reason_search_scenarios(self, test_context):
1270 """Test AXUtilitiesEvent._get_text_selection_changed_event_reason with search scenarios."""
1271
1272 self._setup_dependencies(test_context)
1273 from orca import focus_manager, input_event_manager
1274 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
1275 from orca.ax_utilities_role import AXUtilitiesRole
1276
1277 mock_event = test_context.Mock(spec=Atspi.Event)
1278 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1279 mock_focus = test_context.Mock(spec=Atspi.Accessible)
1280 mock_event.source = mock_obj
1281
1282 mock_focus_manager = test_context.Mock()
1283 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
1284 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
1285
1286 mock_input_manager = test_context.Mock()
1287 mock_input_manager.last_event_was_backspace.return_value = True
1288 mock_input_manager.last_event_was_delete.return_value = False
1289 mock_input_manager.last_event_was_caret_selection.return_value = False
1290 mock_input_manager.last_event_was_caret_navigation.return_value = False
1291 mock_input_manager.last_event_was_select_all.return_value = False
1292 mock_input_manager.last_event_was_primary_click_or_release.return_value = False
1293 test_context.patch_object(
1294 input_event_manager,
1295 "get_manager",
1296 return_value=mock_input_manager,
1297 )
1298
1299 test_context.patch_object(
1300 AXUtilitiesRole,
1301 "is_text_input_search",
1302 side_effect=lambda obj: obj == mock_focus,
1303 )
1304
1305 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
1306 assert result == TextEventReason.SEARCH_UNPRESENTABLE
1307
1308 def test_get_text_selection_changed_event_reason_caret_selection_word(self, test_context):
1309 """Test AXUtilitiesEvent._get_text_selection_changed_event_reason with word selection."""
1310
1311 self._setup_dependencies(test_context)
1312 from orca import focus_manager, input_event_manager
1313 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
1314 from orca.ax_utilities_role import AXUtilitiesRole
1315
1316 mock_event = test_context.Mock(spec=Atspi.Event)
1317 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1318 mock_event.source = mock_obj
1319
1320 mock_focus_manager = test_context.Mock()
1321 mock_focus_manager.get_locus_of_focus.return_value = mock_obj
1322 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
1323
1324 mock_input_manager = test_context.Mock()
1325 mock_input_manager.last_event_was_backspace.return_value = False
1326 mock_input_manager.last_event_was_delete.return_value = False
1327 mock_input_manager.last_event_was_caret_selection.return_value = True
1328 mock_input_manager.last_event_was_line_navigation.return_value = False
1329 mock_input_manager.last_event_was_word_navigation.return_value = True
1330 mock_input_manager.last_event_was_character_navigation.return_value = False
1331 mock_input_manager.last_event_was_page_navigation.return_value = False
1332 mock_input_manager.last_event_was_line_boundary_navigation.return_value = False
1333 mock_input_manager.last_event_was_file_boundary_navigation.return_value = False
1334 test_context.patch_object(
1335 input_event_manager,
1336 "get_manager",
1337 return_value=mock_input_manager,
1338 )
1339
1340 test_context.patch_object(AXUtilitiesRole, "is_text_input_search", return_value=False)
1341
1342 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
1343 assert result == TextEventReason.SELECTION_BY_WORD
1344
1345 def test_is_presentable_active_descendant_change_with_focused_source(self, test_context):
1346 """Test AXUtilitiesEvent.is_presentable_active_descendant_change with focused source."""
1347
1348 self._setup_dependencies(test_context)
1349 from orca import focus_manager
1350 from orca.ax_utilities_event import AXUtilitiesEvent
1351 from orca.ax_utilities_role import AXUtilitiesRole
1352 from orca.ax_utilities_state import AXUtilitiesState
1353
1354 mock_event = test_context.Mock(spec=Atspi.Event)
1355 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1356 mock_child = test_context.Mock(spec=Atspi.Accessible)
1357 mock_event.source = mock_obj
1358 mock_event.any_data = mock_child
1359
1360 mock_focus_manager = test_context.Mock()
1361 mock_focus_manager.get_locus_of_focus.return_value = mock_obj
1362 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
1363
1364 test_context.patch_object(
1365 AXUtilitiesState,
1366 "is_focused",
1367 side_effect=lambda obj: obj == mock_child,
1368 )
1369
1370 test_context.patch_object(AXUtilitiesRole, "is_table_cell", return_value=False)
1371
1372 result = AXUtilitiesEvent.is_presentable_active_descendant_change(mock_event)
1373 assert result is True
1374
1375 def test_is_presentable_active_descendant_change_tree_table_scenario(self, test_context):
1376 """Test AXUtilitiesEvent.is_presentable_active_descendant_change with tree table."""
1377
1378 self._setup_dependencies(test_context)
1379 from orca import focus_manager
1380 from orca.ax_utilities_event import AXUtilitiesEvent
1381 from orca.ax_utilities_object import AXUtilitiesObject
1382 from orca.ax_utilities_role import AXUtilitiesRole
1383 from orca.ax_utilities_state import AXUtilitiesState
1384
1385 mock_event = test_context.Mock(spec=Atspi.Event)
1386 mock_child = test_context.Mock(spec=Atspi.Accessible)
1387 mock_focus = test_context.Mock(spec=Atspi.Accessible)
1388 mock_table = test_context.Mock(spec=Atspi.Accessible)
1389 mock_different_table = test_context.Mock(spec=Atspi.Accessible)
1390 mock_event.source = mock_different_table
1391 mock_event.any_data = mock_child
1392
1393 mock_focus_manager = test_context.Mock()
1394 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
1395 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
1396
1397 test_context.patch_object(
1398 AXUtilitiesState,
1399 "is_focused",
1400 side_effect=lambda obj: obj == mock_child,
1401 )
1402
1403 test_context.patch_object(
1404 AXUtilitiesRole,
1405 "is_table_cell",
1406 side_effect=lambda obj: obj == mock_focus,
1407 )
1408 test_context.patch_object(
1409 AXUtilitiesRole,
1410 "is_tree_or_tree_table",
1411 side_effect=lambda obj: obj in [mock_table, mock_different_table],
1412 )
1413
1414 test_context.patch_object(
1415 AXUtilitiesObject,
1416 "find_ancestor",
1417 side_effect=lambda obj, predicate: mock_table if obj == mock_focus else None,
1418 )
1419
1420 result = AXUtilitiesEvent.is_presentable_active_descendant_change(mock_event)
1421 assert result is False
1422
1423 def test_is_presentable_checked_change_with_focus_checking(self, test_context):
1424 """Test AXUtilitiesEvent.is_presentable_checked_change with focus scenarios."""
1425
1426 self._setup_dependencies(test_context)
1427 from orca import focus_manager, input_event_manager
1428 from orca.ax_utilities_event import AXUtilitiesEvent
1429 from orca.ax_utilities_object import AXUtilitiesObject
1430 from orca.ax_utilities_role import AXUtilitiesRole
1431 from orca.ax_utilities_state import AXUtilitiesState
1432
1433 mock_event = test_context.Mock(spec=Atspi.Event)
1434 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1435 mock_focus = test_context.Mock(spec=Atspi.Accessible)
1436 mock_event.source = mock_obj
1437
1438 AXUtilitiesEvent.LAST_KNOWN_CHECKED[hash(mock_obj)] = False
1439
1440 mock_focus_manager = test_context.Mock()
1441 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
1442 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
1443
1444 mock_input_manager = test_context.Mock()
1445 mock_input_manager.last_event_was_space.return_value = True
1446 test_context.patch_object(
1447 input_event_manager,
1448 "get_manager",
1449 return_value=mock_input_manager,
1450 )
1451
1452 test_context.patch_object(AXUtilitiesState, "is_checked", return_value=True)
1453
1454 test_context.patch_object(AXUtilitiesRole, "is_radio_button", return_value=True)
1455 test_context.patch_object(AXUtilitiesRole, "is_list_item", return_value=True)
1456
1457 test_context.patch_object(
1458 AXUtilitiesObject,
1459 "is_ancestor",
1460 side_effect=lambda obj1, obj2: obj1 == mock_obj and obj2 == mock_focus,
1461 )
1462
1463 result = AXUtilitiesEvent.is_presentable_checked_change(mock_event)
1464 assert result is True
1465
1466 def test_is_presentable_description_change_with_focus_scenarios(self, test_context):
1467 """Test AXUtilitiesEvent.is_presentable_description_change with focus scenarios."""
1468
1469 self._setup_dependencies(test_context)
1470 from orca import focus_manager
1471 from orca.ax_utilities_event import AXUtilitiesEvent
1472 from orca.ax_utilities_object import AXUtilitiesObject
1473 from orca.ax_utilities_state import AXUtilitiesState
1474
1475 mock_event = test_context.Mock(spec=Atspi.Event)
1476 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1477 mock_focus = test_context.Mock(spec=Atspi.Accessible)
1478 mock_event.source = mock_obj
1479 mock_event.any_data = "new description"
1480
1481 AXUtilitiesEvent.LAST_KNOWN_DESCRIPTION[hash(mock_obj)] = "old description"
1482
1483 mock_focus_manager = test_context.Mock()
1484 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
1485 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
1486
1487 test_context.patch_object(AXUtilitiesState, "is_showing", return_value=True)
1488
1489 test_context.patch_object(
1490 AXUtilitiesObject,
1491 "is_ancestor",
1492 side_effect=lambda obj1, obj2: obj1 == mock_focus and obj2 == mock_obj,
1493 )
1494
1495 result = AXUtilitiesEvent.is_presentable_description_change(mock_event)
1496 assert result is True
1497
1498 def test_is_presentable_expanded_change_with_detail_and_roles(self, test_context):
1499 """Test AXUtilitiesEvent.is_presentable_expanded_change with detail and role scenarios."""
1500
1501 self._setup_dependencies(test_context)
1502 from orca import focus_manager
1503 from orca.ax_utilities_event import AXUtilitiesEvent
1504 from orca.ax_utilities_role import AXUtilitiesRole
1505 from orca.ax_utilities_state import AXUtilitiesState
1506
1507 mock_event = test_context.Mock(spec=Atspi.Event)
1508 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1509 mock_focus = test_context.Mock(spec=Atspi.Accessible)
1510 mock_event.source = mock_obj
1511 mock_event.detail1 = 1
1512 AXUtilitiesEvent.LAST_KNOWN_EXPANDED[hash(mock_obj)] = False
1513
1514 mock_focus_manager = test_context.Mock()
1515 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
1516 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
1517
1518 test_context.patch_object(AXUtilitiesState, "is_expanded", return_value=True)
1519 test_context.patch_object(AXUtilitiesState, "is_focused", return_value=True)
1520
1521 test_context.patch_object(AXUtilitiesRole, "is_table_row", return_value=True)
1522 test_context.patch_object(AXUtilitiesRole, "is_list_box", return_value=False)
1523 test_context.patch_object(AXUtilitiesRole, "is_combo_box", return_value=False)
1524 test_context.patch_object(AXUtilitiesRole, "is_button", return_value=False)
1525
1526 result = AXUtilitiesEvent.is_presentable_expanded_change(mock_event)
1527 assert result is True
1528
1529 def test_is_presentable_indeterminate_change_focus_check(self, test_context):
1530 """Test AXUtilitiesEvent.is_presentable_indeterminate_change with focus check."""
1531
1532 self._setup_dependencies(test_context)
1533 from orca import focus_manager
1534 from orca.ax_utilities_event import AXUtilitiesEvent
1535 from orca.ax_utilities_state import AXUtilitiesState
1536
1537 mock_event = test_context.Mock(spec=Atspi.Event)
1538 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1539 mock_event.source = mock_obj
1540
1541 AXUtilitiesEvent.LAST_KNOWN_INDETERMINATE[hash(mock_obj)] = False
1542
1543 mock_focus_manager = test_context.Mock()
1544 mock_focus_manager.get_locus_of_focus.return_value = mock_obj
1545 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
1546
1547 test_context.patch_object(AXUtilitiesState, "is_indeterminate", return_value=True)
1548
1549 result = AXUtilitiesEvent.is_presentable_indeterminate_change(mock_event)
1550 assert result is True
1551
1552 def test_is_presentable_name_change_with_frame(self, test_context):
1553 """Test AXUtilitiesEvent.is_presentable_name_change with frame scenarios."""
1554
1555 self._setup_dependencies(test_context)
1556 from orca import focus_manager
1557 from orca.ax_text import AXText
1558 from orca.ax_utilities_event import AXUtilitiesEvent
1559 from orca.ax_utilities_role import AXUtilitiesRole
1560 from orca.ax_utilities_state import AXUtilitiesState
1561
1562 mock_event = test_context.Mock(spec=Atspi.Event)
1563 mock_frame = test_context.Mock(spec=Atspi.Accessible)
1564 mock_focus = test_context.Mock(spec=Atspi.Accessible)
1565 mock_event.source = mock_frame
1566 mock_event.any_data = "New Window Title: user input text"
1567
1568 AXUtilitiesEvent.LAST_KNOWN_NAME[hash(mock_frame)] = "Old Window Title"
1569
1570 mock_focus_manager = test_context.Mock()
1571 mock_focus_manager.get_active_window.return_value = mock_frame
1572 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
1573 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
1574
1575 test_context.patch_object(AXUtilitiesState, "is_showing", return_value=True)
1576 test_context.patch_object(
1577 AXUtilitiesState,
1578 "is_editable",
1579 side_effect=lambda obj: obj == mock_focus,
1580 )
1581
1582 test_context.patch_object(
1583 AXUtilitiesRole,
1584 "is_frame",
1585 side_effect=lambda obj: obj == mock_frame,
1586 )
1587
1588 test_context.patch_object(AXText, "get_character_count", return_value=15)
1589 test_context.patch_object(AXText, "get_all_text", return_value="user input text")
1590
1591 result = AXUtilitiesEvent.is_presentable_name_change(mock_event)
1592 assert result is False
1593
1594 def test_is_presentable_text_event_editable_scenarios(self, test_context):
1595 """Test AXUtilitiesEvent._is_presentable_text_event with editable scenarios."""
1596
1597 self._setup_dependencies(test_context)
1598 from orca import focus_manager
1599 from orca.ax_utilities_event import AXUtilitiesEvent
1600 from orca.ax_utilities_object import AXUtilitiesObject
1601 from orca.ax_utilities_role import AXUtilitiesRole
1602 from orca.ax_utilities_state import AXUtilitiesState
1603
1604 mock_event = test_context.Mock(spec=Atspi.Event)
1605 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1606 mock_focus = test_context.Mock(spec=Atspi.Accessible)
1607 mock_event.source = mock_obj
1608
1609 mock_focus_manager = test_context.Mock()
1610 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
1611 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
1612
1613 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
1614 test_context.patch_object(AXUtilitiesState, "is_focused", return_value=False)
1615
1616 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
1617
1618 test_context.patch_object(
1619 AXUtilitiesObject,
1620 "is_ancestor",
1621 side_effect=lambda obj1, obj2: obj1 == mock_obj and obj2 == mock_focus,
1622 )
1623
1624 result = AXUtilitiesEvent._is_presentable_text_event(mock_event)
1625 assert result is True
1626
1627 @pytest.mark.parametrize(
1628 "case",
1629 [
1630 {"id": "text_attributes", "method_name": "is_presentable_text_attributes_change"},
1631 {"id": "text_deletion", "method_name": "is_presentable_text_deletion"},
1632 {"id": "text_insertion", "method_name": "is_presentable_text_insertion"},
1633 ],
1634 ids=lambda case: case["id"],
1635 )
1636 def test_text_event_methods_delegate(self, test_context, case: dict):
1637 """Test AXUtilitiesEvent text event methods delegate to _is_presentable_text_event."""
1638
1639 self._setup_dependencies(test_context)
1640 from orca.ax_utilities_event import AXUtilitiesEvent
1641
1642 mock_event = test_context.Mock(spec=Atspi.Event)
1643
1644 test_context.patch_object(AXUtilitiesEvent, "_is_presentable_text_event", return_value=True)
1645
1646 method = getattr(AXUtilitiesEvent, case["method_name"])
1647 result = method(mock_event)
1648 assert result is True
1649
1650 @pytest.mark.parametrize(
1651 "case",
1652 [
1653 {
1654 "id": "delete",
1655 "test_scenario": "delete",
1656 "input_manager_config": {"last_event_was_delete": True},
1657 "editable_state": True,
1658 "expected_result": "DELETE",
1659 },
1660 {
1661 "id": "selection_by_word",
1662 "test_scenario": "selection_by_word",
1663 "input_manager_config": {
1664 "last_event_was_caret_selection": True,
1665 "last_event_was_word_navigation": True,
1666 },
1667 "editable_state": False,
1668 "expected_result": "SELECTION_BY_WORD",
1669 },
1670 {
1671 "id": "navigation_by_page",
1672 "test_scenario": "navigation_by_page",
1673 "input_manager_config": {
1674 "last_event_was_caret_navigation": True,
1675 "last_event_was_page_navigation": True,
1676 },
1677 "editable_state": False,
1678 "expected_result": "NAVIGATION_BY_PAGE",
1679 },
1680 {
1681 "id": "navigation_to_line_boundary",
1682 "test_scenario": "navigation_to_line_boundary",
1683 "input_manager_config": {
1684 "last_event_was_caret_navigation": True,
1685 "last_event_was_line_boundary_navigation": True,
1686 },
1687 "editable_state": False,
1688 "expected_result": "NAVIGATION_TO_LINE_BOUNDARY",
1689 },
1690 {
1691 "id": "navigation_to_file_boundary",
1692 "test_scenario": "navigation_to_file_boundary",
1693 "input_manager_config": {
1694 "last_event_was_caret_navigation": True,
1695 "last_event_was_file_boundary_navigation": True,
1696 },
1697 "editable_state": False,
1698 "expected_result": "NAVIGATION_TO_FILE_BOUNDARY",
1699 },
1700 {
1701 "id": "cut",
1702 "test_scenario": "cut",
1703 "input_manager_config": {"last_event_was_cut": True},
1704 "editable_state": True,
1705 "expected_result": "CUT",
1706 },
1707 {
1708 "id": "paste",
1709 "test_scenario": "paste",
1710 "input_manager_config": {"last_event_was_paste": True},
1711 "editable_state": True,
1712 "expected_result": "PASTE",
1713 },
1714 {
1715 "id": "undo",
1716 "test_scenario": "undo",
1717 "input_manager_config": {"last_event_was_undo": True},
1718 "editable_state": True,
1719 "expected_result": "UNDO",
1720 },
1721 {
1722 "id": "redo",
1723 "test_scenario": "redo",
1724 "input_manager_config": {"last_event_was_redo": True},
1725 "editable_state": True,
1726 "expected_result": "REDO",
1727 },
1728 {
1729 "id": "page_switch",
1730 "test_scenario": "page_switch",
1731 "input_manager_config": {"last_event_was_page_switch": True},
1732 "editable_state": True,
1733 "expected_result": "PAGE_SWITCH",
1734 },
1735 {
1736 "id": "command",
1737 "test_scenario": "command",
1738 "input_manager_config": {"last_event_was_command": True},
1739 "editable_state": True,
1740 "expected_result": "UNSPECIFIED_COMMAND",
1741 },
1742 {
1743 "id": "tab_navigation",
1744 "test_scenario": "tab_navigation",
1745 "input_manager_config": {"last_event_was_tab_navigation": True},
1746 "editable_state": False,
1747 "expected_result": "FOCUS_CHANGE",
1748 },
1749 {
1750 "id": "ui_update",
1751 "test_scenario": "ui_update",
1752 "input_manager_config": {},
1753 "editable_state": False,
1754 "expected_result": "UI_UPDATE",
1755 },
1756 {
1757 "id": "navigation_by_line",
1758 "test_scenario": "navigation_by_line",
1759 "input_manager_config": {
1760 "last_event_was_caret_navigation": True,
1761 "last_event_was_line_navigation": True,
1762 },
1763 "editable_state": False,
1764 "expected_result": "NAVIGATION_BY_LINE",
1765 },
1766 {
1767 "id": "unspecified_navigation",
1768 "test_scenario": "unspecified_navigation",
1769 "input_manager_config": {
1770 "last_event_was_caret_navigation": True,
1771 },
1772 "editable_state": False,
1773 "expected_result": "UNSPECIFIED_NAVIGATION",
1774 },
1775 {
1776 "id": "editable_backspace",
1777 "test_scenario": "editable_backspace",
1778 "input_manager_config": {"last_event_was_backspace": True},
1779 "editable_state": True,
1780 "expected_result": "BACKSPACE",
1781 },
1782 ],
1783 ids=lambda case: case["id"],
1784 )
1785 def test_get_caret_moved_event_reason_consolidated(self, test_context, case: dict):
1786 """Test AXUtilitiesEvent._get_caret_moved_event_reason with various scenarios."""
1787
1788 self._setup_dependencies(test_context)
1789 from orca import focus_manager, input_event_manager
1790 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
1791 from orca.ax_utilities_role import AXUtilitiesRole
1792 from orca.ax_utilities_state import AXUtilitiesState
1793
1794 mock_event = test_context.Mock(spec=Atspi.Event)
1795 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1796 mock_event.source = mock_obj
1797
1798 mock_focus_manager = test_context.Mock()
1799 mock_focus_manager.get_active_mode_and_object_of_interest.return_value = (
1800 "normal",
1801 mock_obj,
1802 )
1803 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
1804
1805 mock_input_manager = test_context.Mock()
1806 default_config = {
1807 "last_event_was_backspace": False,
1808 "last_event_was_delete": False,
1809 "last_event_was_caret_selection": False,
1810 "last_event_was_caret_navigation": False,
1811 "last_event_was_line_navigation": False,
1812 "last_event_was_word_navigation": False,
1813 "last_event_was_character_navigation": False,
1814 "last_event_was_page_navigation": False,
1815 "last_event_was_line_boundary_navigation": False,
1816 "last_event_was_file_boundary_navigation": False,
1817 "last_event_was_select_all": False,
1818 "last_event_was_primary_click_or_release": False,
1819 "last_event_was_cut": False,
1820 "last_event_was_paste": False,
1821 "last_event_was_undo": False,
1822 "last_event_was_redo": False,
1823 "last_event_was_page_switch": False,
1824 "last_event_was_command": False,
1825 "last_event_was_printable_key": False,
1826 "last_event_was_tab_navigation": False,
1827 }
1828 default_config.update(case["input_manager_config"])
1829
1830 for attr, value in default_config.items():
1831 setattr(mock_input_manager, attr, test_context.Mock(return_value=value))
1832
1833 test_context.patch_object(
1834 input_event_manager,
1835 "get_manager",
1836 return_value=mock_input_manager,
1837 )
1838
1839 test_context.patch_object(
1840 AXUtilitiesState,
1841 "is_editable",
1842 side_effect=lambda obj: case["editable_state"],
1843 )
1844 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
1845 if case["test_scenario"] == "ui_update":
1846 from orca.ax_utilities_object import AXUtilitiesObject
1847
1848 test_context.patch_object(
1849 AXUtilitiesObject, "find_ancestor", return_value=test_context.Mock()
1850 )
1851
1852 result = AXUtilitiesEvent._get_caret_moved_event_reason(mock_event)
1853 assert result == getattr(TextEventReason, case["expected_result"])
1854
1855 def test_get_text_event_reason_ui_update(self, test_context):
1856 """Test get_text_event_reason with UI update scenario."""
1857
1858 self._setup_dependencies(test_context)
1859 from orca import input_event_manager
1860 from orca.ax_object import AXObject
1861 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
1862 from orca.ax_utilities_role import AXUtilitiesRole
1863
1864 mock_event = test_context.Mock(spec=Atspi.Event)
1865 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1866 mock_event.source = mock_obj
1867 mock_event.type = "object:text-changed:insert"
1868
1869 mock_input_manager = test_context.Mock()
1870 mock_input_manager.last_event_was_page_switch.return_value = False
1871 test_context.patch_object(
1872 input_event_manager,
1873 "get_manager",
1874 return_value=mock_input_manager,
1875 )
1876
1877 test_context.patch_object(AXUtilitiesRole, "get_text_ui_roles", return_value=["text"])
1878 test_context.patch_object(AXObject, "get_role", return_value="text")
1879
1880 result = AXUtilitiesEvent.get_text_event_reason(mock_event)
1881 assert result == TextEventReason.UI_UPDATE
1882
1883 def test_get_text_event_reason_page_switch_scenario(self, test_context):
1884 """Test get_text_event_reason with page switch scenario."""
1885
1886 self._setup_dependencies(test_context)
1887 from orca import input_event_manager
1888 from orca.ax_object import AXObject
1889 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
1890 from orca.ax_utilities_role import AXUtilitiesRole
1891
1892 mock_event = test_context.Mock(spec=Atspi.Event)
1893 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1894 mock_event.source = mock_obj
1895 mock_event.type = "object:text-changed:insert"
1896
1897 mock_input_manager = test_context.Mock()
1898 mock_input_manager.last_event_was_page_switch.return_value = True
1899 test_context.patch_object(
1900 input_event_manager,
1901 "get_manager",
1902 return_value=mock_input_manager,
1903 )
1904
1905 test_context.patch_object(AXUtilitiesRole, "get_text_ui_roles", return_value=[])
1906 test_context.patch_object(AXObject, "get_role", return_value="document")
1907
1908 result = AXUtilitiesEvent.get_text_event_reason(mock_event)
1909 assert result == TextEventReason.PAGE_SWITCH
1910
1911 def test_get_text_event_reason_command_scenario(self, test_context):
1912 """Test get_text_event_reason with command scenario."""
1913
1914 self._setup_dependencies(test_context)
1915 from orca import input_event_manager
1916 from orca.ax_object import AXObject
1917 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
1918 from orca.ax_utilities_role import AXUtilitiesRole
1919 from orca.ax_utilities_state import AXUtilitiesState
1920
1921 mock_event = test_context.Mock(spec=Atspi.Event)
1922 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1923 mock_event.source = mock_obj
1924 mock_event.type = "object:text-changed:insert"
1925 mock_event.any_data = "test"
1926
1927 mock_input_manager = test_context.Mock()
1928 mock_input_manager.last_event_was_page_switch.return_value = False
1929 mock_input_manager.last_event_was_command.return_value = True
1930 test_context.patch_object(
1931 input_event_manager,
1932 "get_manager",
1933 return_value=mock_input_manager,
1934 )
1935
1936 test_context.patch_object(AXUtilitiesRole, "get_text_ui_roles", return_value=[])
1937 test_context.patch_object(AXObject, "get_role", return_value="document")
1938 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=False)
1939 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
1940
1941 result = AXUtilitiesEvent.get_text_event_reason(mock_event)
1942 assert result == TextEventReason.UNSPECIFIED_COMMAND
1943
1944 def test_get_text_event_reason_delete_in_editable(self, test_context):
1945 """Test get_text_event_reason with delete in editable object."""
1946
1947 self._setup_dependencies(test_context)
1948 from orca import input_event_manager
1949 from orca.ax_object import AXObject
1950 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
1951 from orca.ax_utilities_role import AXUtilitiesRole
1952 from orca.ax_utilities_state import AXUtilitiesState
1953
1954 mock_event = test_context.Mock(spec=Atspi.Event)
1955 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1956 mock_event.source = mock_obj
1957 mock_event.type = "object:text-changed:insert"
1958 mock_event.any_data = "test"
1959
1960 mock_input_manager = test_context.Mock()
1961 mock_input_manager.last_event_was_page_switch.return_value = False
1962 mock_input_manager.last_event_was_backspace.return_value = False
1963 mock_input_manager.last_event_was_delete.return_value = True
1964 test_context.patch_object(
1965 input_event_manager,
1966 "get_manager",
1967 return_value=mock_input_manager,
1968 )
1969
1970 test_context.patch_object(AXUtilitiesRole, "get_text_ui_roles", return_value=[])
1971 test_context.patch_object(AXObject, "get_role", return_value="entry")
1972 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
1973 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
1974
1975 from orca.ax_utilities_text import AXUtilitiesText
1976
1977 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
1978
1979 result = AXUtilitiesEvent.get_text_event_reason(mock_event)
1980 assert result == TextEventReason.DELETE
1981
1982 def test_get_text_event_reason_cut_in_editable(self, test_context):
1983 """Test get_text_event_reason with cut in editable object."""
1984
1985 self._setup_dependencies(test_context)
1986 from orca import input_event_manager
1987 from orca.ax_object import AXObject
1988 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
1989 from orca.ax_utilities_role import AXUtilitiesRole
1990 from orca.ax_utilities_state import AXUtilitiesState
1991
1992 mock_event = test_context.Mock(spec=Atspi.Event)
1993 mock_obj = test_context.Mock(spec=Atspi.Accessible)
1994 mock_event.source = mock_obj
1995 mock_event.type = "object:text-changed:insert"
1996 mock_event.any_data = "test"
1997
1998 mock_input_manager = test_context.Mock()
1999 mock_input_manager.last_event_was_page_switch.return_value = False
2000 mock_input_manager.last_event_was_backspace.return_value = False
2001 mock_input_manager.last_event_was_delete.return_value = False
2002 mock_input_manager.last_event_was_cut.return_value = True
2003 test_context.patch_object(
2004 input_event_manager,
2005 "get_manager",
2006 return_value=mock_input_manager,
2007 )
2008
2009 test_context.patch_object(AXUtilitiesRole, "get_text_ui_roles", return_value=[])
2010 test_context.patch_object(AXObject, "get_role", return_value="entry")
2011 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
2012 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2013
2014 from orca.ax_utilities_text import AXUtilitiesText
2015
2016 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
2017
2018 result = AXUtilitiesEvent.get_text_event_reason(mock_event)
2019 assert result == TextEventReason.CUT
2020
2021 def test_get_text_event_reason_paste_in_editable(self, test_context):
2022 """Test get_text_event_reason with paste in editable object."""
2023
2024 self._setup_dependencies(test_context)
2025 from orca import input_event_manager
2026 from orca.ax_object import AXObject
2027 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2028 from orca.ax_utilities_role import AXUtilitiesRole
2029 from orca.ax_utilities_state import AXUtilitiesState
2030
2031 mock_event = test_context.Mock(spec=Atspi.Event)
2032 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2033 mock_event.source = mock_obj
2034 mock_event.type = "object:text-changed:insert"
2035 mock_event.any_data = "test"
2036
2037 mock_input_manager = test_context.Mock()
2038 mock_input_manager.last_event_was_page_switch.return_value = False
2039 mock_input_manager.last_event_was_backspace.return_value = False
2040 mock_input_manager.last_event_was_delete.return_value = False
2041 mock_input_manager.last_event_was_cut.return_value = False
2042 mock_input_manager.last_event_was_paste.return_value = True
2043 test_context.patch_object(
2044 input_event_manager,
2045 "get_manager",
2046 return_value=mock_input_manager,
2047 )
2048
2049 test_context.patch_object(AXUtilitiesRole, "get_text_ui_roles", return_value=[])
2050 test_context.patch_object(AXObject, "get_role", return_value="entry")
2051 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
2052 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2053
2054 from orca.ax_utilities_text import AXUtilitiesText
2055
2056 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
2057
2058 result = AXUtilitiesEvent.get_text_event_reason(mock_event)
2059 assert result == TextEventReason.PASTE
2060
2061 def test_get_text_event_reason_undo_in_editable(self, test_context):
2062 """Test get_text_event_reason with undo in editable object."""
2063
2064 self._setup_dependencies(test_context)
2065 from orca import input_event_manager
2066 from orca.ax_object import AXObject
2067 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2068 from orca.ax_utilities_role import AXUtilitiesRole
2069 from orca.ax_utilities_state import AXUtilitiesState
2070
2071 mock_event = test_context.Mock(spec=Atspi.Event)
2072 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2073 mock_event.source = mock_obj
2074 mock_event.type = "object:text-changed:insert"
2075 mock_event.any_data = "test"
2076
2077 mock_input_manager = test_context.Mock()
2078 mock_input_manager.last_event_was_page_switch.return_value = False
2079 mock_input_manager.last_event_was_backspace.return_value = False
2080 mock_input_manager.last_event_was_delete.return_value = False
2081 mock_input_manager.last_event_was_cut.return_value = False
2082 mock_input_manager.last_event_was_paste.return_value = False
2083 mock_input_manager.last_event_was_undo.return_value = True
2084 test_context.patch_object(
2085 input_event_manager,
2086 "get_manager",
2087 return_value=mock_input_manager,
2088 )
2089
2090 test_context.patch_object(AXUtilitiesRole, "get_text_ui_roles", return_value=[])
2091 test_context.patch_object(AXObject, "get_role", return_value="entry")
2092 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
2093 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2094
2095 from orca.ax_utilities_text import AXUtilitiesText
2096
2097 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
2098
2099 result = AXUtilitiesEvent.get_text_event_reason(mock_event)
2100 assert result == TextEventReason.UNDO
2101
2102 def test_get_text_event_reason_redo_in_editable(self, test_context):
2103 """Test get_text_event_reason with redo in editable object."""
2104
2105 self._setup_dependencies(test_context)
2106 from orca import input_event_manager
2107 from orca.ax_object import AXObject
2108 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2109 from orca.ax_utilities_role import AXUtilitiesRole
2110 from orca.ax_utilities_state import AXUtilitiesState
2111
2112 mock_event = test_context.Mock(spec=Atspi.Event)
2113 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2114 mock_event.source = mock_obj
2115 mock_event.type = "object:text-changed:insert"
2116 mock_event.any_data = "test"
2117
2118 mock_input_manager = test_context.Mock()
2119 mock_input_manager.last_event_was_page_switch.return_value = False
2120 mock_input_manager.last_event_was_backspace.return_value = False
2121 mock_input_manager.last_event_was_delete.return_value = False
2122 mock_input_manager.last_event_was_cut.return_value = False
2123 mock_input_manager.last_event_was_paste.return_value = False
2124 mock_input_manager.last_event_was_undo.return_value = False
2125 mock_input_manager.last_event_was_redo.return_value = True
2126 test_context.patch_object(
2127 input_event_manager,
2128 "get_manager",
2129 return_value=mock_input_manager,
2130 )
2131
2132 test_context.patch_object(AXUtilitiesRole, "get_text_ui_roles", return_value=[])
2133 test_context.patch_object(AXObject, "get_role", return_value="entry")
2134 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
2135 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2136
2137 from orca.ax_utilities_text import AXUtilitiesText
2138
2139 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
2140
2141 result = AXUtilitiesEvent.get_text_event_reason(mock_event)
2142 assert result == TextEventReason.REDO
2143
2144 def test_get_text_event_reason_command_in_editable(self, test_context):
2145 """Test get_text_event_reason with command in editable object."""
2146
2147 self._setup_dependencies(test_context)
2148 from orca import input_event_manager
2149 from orca.ax_object import AXObject
2150 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2151 from orca.ax_utilities_role import AXUtilitiesRole
2152 from orca.ax_utilities_state import AXUtilitiesState
2153
2154 mock_event = test_context.Mock(spec=Atspi.Event)
2155 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2156 mock_event.source = mock_obj
2157 mock_event.type = "object:text-changed:insert"
2158 mock_event.any_data = "test"
2159
2160 mock_input_manager = test_context.Mock()
2161 mock_input_manager.last_event_was_page_switch.return_value = False
2162 mock_input_manager.last_event_was_backspace.return_value = False
2163 mock_input_manager.last_event_was_delete.return_value = False
2164 mock_input_manager.last_event_was_cut.return_value = False
2165 mock_input_manager.last_event_was_paste.return_value = False
2166 mock_input_manager.last_event_was_undo.return_value = False
2167 mock_input_manager.last_event_was_redo.return_value = False
2168 mock_input_manager.last_event_was_command.return_value = True
2169 test_context.patch_object(
2170 input_event_manager,
2171 "get_manager",
2172 return_value=mock_input_manager,
2173 )
2174
2175 test_context.patch_object(AXUtilitiesRole, "get_text_ui_roles", return_value=[])
2176 test_context.patch_object(AXObject, "get_role", return_value="entry")
2177 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
2178 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2179
2180 from orca.ax_utilities_text import AXUtilitiesText
2181
2182 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
2183
2184 result = AXUtilitiesEvent.get_text_event_reason(mock_event)
2185 assert result == TextEventReason.UNSPECIFIED_COMMAND
2186
2187 def test_clear_all_dictionaries_direct_call(self, test_context):
2188 """Test _clear_all_dictionaries method directly."""
2189
2190 self._setup_dependencies(test_context)
2191 from orca import debug
2192 from orca.ax_utilities_event import AXUtilitiesEvent
2193
2194 mock_print_message = test_context.Mock()
2195 test_context.patch_object(debug, "print_message", new=mock_print_message)
2196
2197 AXUtilitiesEvent._clear_all_dictionaries("direct test")
2198
2199 mock_print_message.assert_called_once()
2200 args = mock_print_message.call_args[0]
2201 assert "AXUtilitiesEvent: Clearing local cache." in args[1]
2202 assert "Reason: direct test" in args[1]
2203
2204 @pytest.mark.parametrize(
2205 "case",
2206 [
2207 {
2208 "id": "delete_scenario",
2209 "test_event_type": "delete",
2210 "input_manager_config": {"last_event_was_delete": True},
2211 "expected_reason": "DELETE",
2212 },
2213 {
2214 "id": "cut_scenario",
2215 "test_event_type": "cut",
2216 "input_manager_config": {"last_event_was_cut": True},
2217 "expected_reason": "CUT",
2218 },
2219 {
2220 "id": "paste_scenario",
2221 "test_event_type": "paste",
2222 "input_manager_config": {"last_event_was_paste": True},
2223 "expected_reason": "PASTE",
2224 },
2225 {
2226 "id": "undo_scenario",
2227 "test_event_type": "undo",
2228 "input_manager_config": {"last_event_was_undo": True},
2229 "expected_reason": "UNDO",
2230 },
2231 {
2232 "id": "redo_scenario",
2233 "test_event_type": "redo",
2234 "input_manager_config": {"last_event_was_redo": True},
2235 "expected_reason": "REDO",
2236 },
2237 {
2238 "id": "command_scenario",
2239 "test_event_type": "command",
2240 "input_manager_config": {"last_event_was_command": True},
2241 "expected_reason": "UNSPECIFIED_COMMAND",
2242 },
2243 ],
2244 ids=lambda case: case["id"],
2245 )
2246 def test_get_text_insertion_event_reason_input_scenarios(
2247 self,
2248 test_context,
2249 case: dict,
2250 ) -> None:
2251 """Test _get_text_insertion_event_reason with various input scenarios."""
2252
2253 self._setup_dependencies(test_context)
2254 from orca import input_event_manager
2255 from orca.ax_object import AXObject
2256 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2257 from orca.ax_utilities_role import AXUtilitiesRole
2258 from orca.ax_utilities_state import AXUtilitiesState
2259 from orca.ax_utilities_text import AXUtilitiesText
2260
2261 mock_event = test_context.Mock(spec=Atspi.Event)
2262 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2263 mock_event.source = mock_obj
2264 mock_event.any_data = "test"
2265 mock_event.type = "object:text-changed:insert"
2266
2267 mock_input_manager = test_context.Mock()
2268 default_config = {
2269 "last_event_was_page_switch": False,
2270 "last_event_was_backspace": False,
2271 "last_event_was_delete": False,
2272 "last_event_was_cut": False,
2273 "last_event_was_paste": False,
2274 "last_event_was_undo": False,
2275 "last_event_was_redo": False,
2276 "last_event_was_command": False,
2277 }
2278 default_config.update(case["input_manager_config"])
2279 for method_name, return_value in default_config.items():
2280 setattr(mock_input_manager, method_name, test_context.Mock(return_value=return_value))
2281 test_context.patch_object(
2282 input_event_manager,
2283 "get_manager",
2284 return_value=mock_input_manager,
2285 )
2286
2287 test_context.patch_object(AXUtilitiesRole, "get_text_ui_roles", return_value=[])
2288 test_context.patch_object(AXObject, "get_role", return_value="entry")
2289 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
2290 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2291 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
2292
2293 result = AXUtilitiesEvent._get_text_insertion_event_reason(mock_event)
2294 assert result == getattr(TextEventReason, case["expected_reason"])
2295
2296 def test_get_text_deletion_event_reason_auto_deletion(self, test_context):
2297 """Test AXUtilitiesEvent._get_text_deletion_event_reason with auto deletion."""
2298
2299 self._setup_dependencies(test_context)
2300 from orca import input_event_manager
2301 from orca.ax_object import AXObject
2302 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2303 from orca.ax_utilities_object import AXUtilitiesObject
2304 from orca.ax_utilities_role import AXUtilitiesRole
2305 from orca.ax_utilities_state import AXUtilitiesState
2306
2307 mock_event = test_context.Mock(spec=Atspi.Event)
2308 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2309 mock_event.source = mock_obj
2310 mock_event.any_data = "auto deleted"
2311 mock_event.type = "object:text-changed:delete"
2312
2313 mock_input_manager = test_context.Mock()
2314 mock_input_manager.last_event_was_page_switch.return_value = False
2315 mock_input_manager.last_event_was_backspace.return_value = False
2316 mock_input_manager.last_event_was_delete.return_value = False
2317 mock_input_manager.last_event_was_cut.return_value = False
2318 mock_input_manager.last_event_was_paste.return_value = False
2319 mock_input_manager.last_event_was_undo.return_value = False
2320 mock_input_manager.last_event_was_redo.return_value = False
2321 mock_input_manager.last_event_was_command.return_value = False
2322 mock_input_manager.last_event_was_printable_key.return_value = False
2323 mock_input_manager.last_event_was_up_or_down.return_value = True
2324 mock_input_manager.last_event_was_page_up_or_page_down.return_value = False
2325 test_context.patch_object(
2326 input_event_manager,
2327 "get_manager",
2328 return_value=mock_input_manager,
2329 )
2330
2331 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
2332 test_context.patch_object(
2333 AXUtilitiesRole,
2334 "get_text_ui_roles",
2335 return_value=[Atspi.Role.LABEL],
2336 )
2337 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
2338 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2339 test_context.patch_object(AXUtilitiesRole, "is_spin_button", return_value=False)
2340 test_context.patch_object(AXUtilitiesObject, "find_ancestor", return_value=None)
2341
2342 result = AXUtilitiesEvent._get_text_deletion_event_reason(mock_event)
2343 assert result == TextEventReason.AUTO_DELETION
2344
2345 def test_get_text_deletion_event_reason_children_change(self, test_context):
2346 """Test AXUtilitiesEvent._get_text_deletion_event_reason with children change."""
2347
2348 self._setup_dependencies(test_context)
2349 from orca import input_event_manager
2350 from orca.ax_object import AXObject
2351 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2352 from orca.ax_utilities_role import AXUtilitiesRole
2353 from orca.ax_utilities_state import AXUtilitiesState
2354
2355 mock_event = test_context.Mock(spec=Atspi.Event)
2356 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2357 mock_event.source = mock_obj
2358 mock_event.any_data = "\ufffc"
2359 mock_event.type = "object:text-changed:delete"
2360
2361 mock_input_manager = test_context.Mock()
2362 mock_input_manager.last_event_was_page_switch.return_value = False
2363 mock_input_manager.last_event_was_command.return_value = False
2364 test_context.patch_object(
2365 input_event_manager,
2366 "get_manager",
2367 return_value=mock_input_manager,
2368 )
2369
2370 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
2371 test_context.patch_object(
2372 AXUtilitiesRole,
2373 "get_text_ui_roles",
2374 return_value=[Atspi.Role.LABEL],
2375 )
2376 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=False)
2377 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2378
2379 result = AXUtilitiesEvent._get_text_deletion_event_reason(mock_event)
2380 assert result == TextEventReason.CHILDREN_CHANGE
2381
2382 def test_get_text_insertion_event_reason_selected_text_restoration(self, test_context):
2383 """Test AXUtilitiesEvent._get_text_insertion_event_reason with selected text restoration."""
2384
2385 self._setup_dependencies(test_context)
2386 from orca import input_event_manager
2387 from orca.ax_object import AXObject
2388 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2389 from orca.ax_utilities_role import AXUtilitiesRole
2390 from orca.ax_utilities_state import AXUtilitiesState
2391 from orca.ax_utilities_text import AXUtilitiesText
2392
2393 mock_event = test_context.Mock(spec=Atspi.Event)
2394 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2395 mock_event.source = mock_obj
2396 mock_event.any_data = "restored text"
2397 mock_event.type = "object:text-changed:insert"
2398
2399 mock_input_manager = test_context.Mock()
2400 mock_input_manager.last_event_was_page_switch.return_value = False
2401 mock_input_manager.last_event_was_backspace.return_value = False
2402 mock_input_manager.last_event_was_delete.return_value = False
2403 mock_input_manager.last_event_was_cut.return_value = False
2404 mock_input_manager.last_event_was_paste.return_value = False
2405 mock_input_manager.last_event_was_undo.return_value = True
2406 mock_input_manager.last_event_was_redo.return_value = False
2407 mock_input_manager.last_event_was_command.return_value = False
2408 mock_input_manager.last_event_was_space.return_value = False
2409 mock_input_manager.last_event_was_tab.return_value = False
2410 mock_input_manager.last_event_was_return.return_value = False
2411 mock_input_manager.last_event_was_printable_key.return_value = False
2412 mock_input_manager.last_event_was_middle_click.return_value = False
2413 mock_input_manager.last_event_was_middle_release.return_value = False
2414 mock_input_manager.last_event_was_up_or_down.return_value = False
2415 mock_input_manager.last_event_was_page_up_or_page_down.return_value = False
2416 test_context.patch_object(
2417 input_event_manager,
2418 "get_manager",
2419 return_value=mock_input_manager,
2420 )
2421
2422 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
2423 test_context.patch_object(
2424 AXUtilitiesRole,
2425 "get_text_ui_roles",
2426 return_value=[Atspi.Role.LABEL],
2427 )
2428 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
2429 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2430
2431 test_context.patch_object(
2432 AXUtilitiesText,
2433 "get_selected_text",
2434 return_value=("restored text", 0, 13),
2435 )
2436
2437 result = AXUtilitiesEvent._get_text_insertion_event_reason(mock_event)
2438 assert result == TextEventReason.SELECTED_TEXT_RESTORATION
2439
2440 def test_get_text_insertion_event_reason_auto_insertion_presentable_tab(self, test_context):
2441 """Test AXUtilitiesEvent._get_text_insertion_event_reason with auto insertion from tab."""
2442
2443 self._setup_dependencies(test_context)
2444 from orca import input_event_manager
2445 from orca.ax_object import AXObject
2446 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2447 from orca.ax_utilities_role import AXUtilitiesRole
2448 from orca.ax_utilities_state import AXUtilitiesState
2449 from orca.ax_utilities_text import AXUtilitiesText
2450
2451 mock_event = test_context.Mock(spec=Atspi.Event)
2452 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2453 mock_event.source = mock_obj
2454 mock_event.any_data = "auto inserted text"
2455 mock_event.type = "object:text-changed:insert"
2456
2457 mock_input_manager = test_context.Mock()
2458 mock_input_manager.last_event_was_page_switch.return_value = False
2459 mock_input_manager.last_event_was_backspace.return_value = False
2460 mock_input_manager.last_event_was_delete.return_value = False
2461 mock_input_manager.last_event_was_cut.return_value = False
2462 mock_input_manager.last_event_was_paste.return_value = False
2463 mock_input_manager.last_event_was_undo.return_value = False
2464 mock_input_manager.last_event_was_redo.return_value = False
2465 mock_input_manager.last_event_was_command.return_value = False
2466 mock_input_manager.last_event_was_space.return_value = False
2467 mock_input_manager.last_event_was_tab.return_value = True
2468 mock_input_manager.last_event_was_return.return_value = False
2469 mock_input_manager.last_event_was_printable_key.return_value = False
2470 mock_input_manager.last_event_was_middle_click.return_value = False
2471 mock_input_manager.last_event_was_middle_release.return_value = False
2472 mock_input_manager.last_event_was_up_or_down.return_value = False
2473 mock_input_manager.last_event_was_page_up_or_page_down.return_value = False
2474 test_context.patch_object(
2475 input_event_manager,
2476 "get_manager",
2477 return_value=mock_input_manager,
2478 )
2479
2480 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
2481 test_context.patch_object(
2482 AXUtilitiesRole,
2483 "get_text_ui_roles",
2484 return_value=[Atspi.Role.LABEL],
2485 )
2486 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
2487 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2488
2489 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
2490
2491 result = AXUtilitiesEvent._get_text_insertion_event_reason(mock_event)
2492 assert result == TextEventReason.AUTO_INSERTION_PRESENTABLE
2493
2494 def test_get_text_insertion_event_reason_auto_insertion_unpresentable_return(
2495 self,
2496 test_context,
2497 ):
2498 """Test _get_text_insertion_event_reason with auto insertion from return in single line."""
2499
2500 self._setup_dependencies(test_context)
2501 from orca import input_event_manager
2502 from orca.ax_object import AXObject
2503 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2504 from orca.ax_utilities_role import AXUtilitiesRole
2505 from orca.ax_utilities_state import AXUtilitiesState
2506 from orca.ax_utilities_text import AXUtilitiesText
2507
2508 mock_event = test_context.Mock(spec=Atspi.Event)
2509 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2510 mock_event.source = mock_obj
2511 mock_event.any_data = "auto inserted text"
2512 mock_event.type = "object:text-changed:insert"
2513
2514 mock_input_manager = test_context.Mock()
2515 mock_input_manager.last_event_was_page_switch.return_value = False
2516 mock_input_manager.last_event_was_backspace.return_value = False
2517 mock_input_manager.last_event_was_delete.return_value = False
2518 mock_input_manager.last_event_was_cut.return_value = False
2519 mock_input_manager.last_event_was_paste.return_value = False
2520 mock_input_manager.last_event_was_undo.return_value = False
2521 mock_input_manager.last_event_was_redo.return_value = False
2522 mock_input_manager.last_event_was_command.return_value = False
2523 mock_input_manager.last_event_was_space.return_value = False
2524 mock_input_manager.last_event_was_tab.return_value = False
2525 mock_input_manager.last_event_was_return.return_value = True
2526 mock_input_manager.last_event_was_printable_key.return_value = False
2527 mock_input_manager.last_event_was_middle_click.return_value = False
2528 mock_input_manager.last_event_was_middle_release.return_value = False
2529 mock_input_manager.last_event_was_up_or_down.return_value = False
2530 mock_input_manager.last_event_was_page_up_or_page_down.return_value = False
2531 test_context.patch_object(
2532 input_event_manager,
2533 "get_manager",
2534 return_value=mock_input_manager,
2535 )
2536
2537 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
2538 test_context.patch_object(
2539 AXUtilitiesRole,
2540 "get_text_ui_roles",
2541 return_value=[Atspi.Role.LABEL],
2542 )
2543 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
2544 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2545 test_context.patch_object(AXUtilitiesState, "is_single_line", return_value=True)
2546
2547 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
2548
2549 result = AXUtilitiesEvent._get_text_insertion_event_reason(mock_event)
2550 assert result == TextEventReason.AUTO_INSERTION_UNPRESENTABLE
2551
2552 def test_get_text_insertion_event_reason_middle_click(self, test_context):
2553 """Test AXUtilitiesEvent._get_text_insertion_event_reason with middle click."""
2554
2555 self._setup_dependencies(test_context)
2556 from orca import input_event_manager
2557 from orca.ax_object import AXObject
2558 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2559 from orca.ax_utilities_role import AXUtilitiesRole
2560 from orca.ax_utilities_state import AXUtilitiesState
2561 from orca.ax_utilities_text import AXUtilitiesText
2562
2563 mock_event = test_context.Mock(spec=Atspi.Event)
2564 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2565 mock_event.source = mock_obj
2566 mock_event.any_data = "pasted text"
2567 mock_event.type = "object:text-changed:insert"
2568
2569 mock_input_manager = test_context.Mock()
2570 mock_input_manager.last_event_was_page_switch.return_value = False
2571 mock_input_manager.last_event_was_backspace.return_value = False
2572 mock_input_manager.last_event_was_delete.return_value = False
2573 mock_input_manager.last_event_was_cut.return_value = False
2574 mock_input_manager.last_event_was_paste.return_value = False
2575 mock_input_manager.last_event_was_undo.return_value = False
2576 mock_input_manager.last_event_was_redo.return_value = False
2577 mock_input_manager.last_event_was_command.return_value = False
2578 mock_input_manager.last_event_was_space.return_value = False
2579 mock_input_manager.last_event_was_tab.return_value = False
2580 mock_input_manager.last_event_was_return.return_value = False
2581 mock_input_manager.last_event_was_printable_key.return_value = False
2582 mock_input_manager.last_event_was_middle_click.return_value = True
2583 mock_input_manager.last_event_was_middle_release.return_value = False
2584 mock_input_manager.last_event_was_up_or_down.return_value = False
2585 mock_input_manager.last_event_was_page_up_or_page_down.return_value = False
2586 test_context.patch_object(
2587 input_event_manager,
2588 "get_manager",
2589 return_value=mock_input_manager,
2590 )
2591
2592 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
2593 test_context.patch_object(
2594 AXUtilitiesRole,
2595 "get_text_ui_roles",
2596 return_value=[Atspi.Role.LABEL],
2597 )
2598 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
2599 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2600
2601 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
2602
2603 result = AXUtilitiesEvent._get_text_insertion_event_reason(mock_event)
2604 assert result == TextEventReason.MOUSE_MIDDLE_BUTTON
2605
2606 def test_clear_cache_now_with_reason(self, test_context):
2607 """Test AXUtilitiesEvent.clear_cache_now with reason parameter."""
2608
2609 self._setup_dependencies(test_context)
2610 from orca.ax_utilities_event import AXUtilitiesEvent
2611
2612 AXUtilitiesEvent.clear_cache_now("test reason")
2613 assert len(AXUtilitiesEvent.LAST_KNOWN_NAME) == 0
2614 assert len(AXUtilitiesEvent.LAST_KNOWN_DESCRIPTION) == 0
2615 assert len(AXUtilitiesEvent.TEXT_EVENT_REASON) == 0
2616
2617 def test_get_text_deletion_event_reason_page_switch(self, test_context):
2618 """Test _get_text_deletion_event_reason with page switch scenario."""
2619
2620 self._setup_dependencies(test_context)
2621 from orca import input_event_manager
2622 from orca.ax_object import AXObject
2623 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2624 from orca.ax_utilities_role import AXUtilitiesRole
2625 from orca.ax_utilities_state import AXUtilitiesState
2626
2627 mock_event = test_context.Mock(spec=Atspi.Event)
2628 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2629 mock_event.source = mock_obj
2630 mock_event.type = "object:text-changed:delete"
2631
2632 mock_input_manager = test_context.Mock()
2633 mock_input_manager.last_event_was_page_switch.return_value = True
2634 test_context.patch_object(
2635 input_event_manager,
2636 "get_manager",
2637 return_value=mock_input_manager,
2638 )
2639
2640 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
2641 test_context.patch_object(
2642 AXUtilitiesRole,
2643 "get_text_ui_roles",
2644 return_value=[Atspi.Role.LABEL],
2645 )
2646 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=False)
2647 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2648
2649 result = AXUtilitiesEvent._get_text_deletion_event_reason(mock_event)
2650 assert result == TextEventReason.PAGE_SWITCH
2651
2652 def test_get_text_deletion_event_reason_editable_scenarios(self, test_context):
2653 """Test _get_text_deletion_event_reason with various editable scenarios."""
2654
2655 self._setup_dependencies(test_context)
2656 from orca import input_event_manager
2657 from orca.ax_object import AXObject
2658 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2659 from orca.ax_utilities_role import AXUtilitiesRole
2660 from orca.ax_utilities_state import AXUtilitiesState
2661 from orca.ax_utilities_text import AXUtilitiesText
2662
2663 mock_event = test_context.Mock(spec=Atspi.Event)
2664 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2665 mock_event.source = mock_obj
2666 mock_event.any_data = "deleted text"
2667 mock_event.type = "object:text-changed:delete"
2668
2669 mock_input_manager = test_context.Mock()
2670 mock_input_manager.last_event_was_page_switch.return_value = False
2671 mock_input_manager.last_event_was_backspace.return_value = False
2672 mock_input_manager.last_event_was_delete.return_value = True
2673 mock_input_manager.last_event_was_cut.return_value = False
2674 mock_input_manager.last_event_was_paste.return_value = False
2675 mock_input_manager.last_event_was_undo.return_value = False
2676 mock_input_manager.last_event_was_redo.return_value = False
2677 mock_input_manager.last_event_was_command.return_value = False
2678 mock_input_manager.last_event_was_printable_key.return_value = False
2679 mock_input_manager.last_event_was_up_or_down.return_value = False
2680 mock_input_manager.last_event_was_page_up_or_page_down.return_value = False
2681 test_context.patch_object(
2682 input_event_manager,
2683 "get_manager",
2684 return_value=mock_input_manager,
2685 )
2686
2687 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
2688 test_context.patch_object(
2689 AXUtilitiesRole,
2690 "get_text_ui_roles",
2691 return_value=[Atspi.Role.LABEL],
2692 )
2693 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
2694 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2695 test_context.patch_object(
2696 AXUtilitiesText, "get_cached_selected_text", return_value=("", 0, 0)
2697 )
2698
2699 result = AXUtilitiesEvent._get_text_deletion_event_reason(mock_event)
2700 assert result == TextEventReason.DELETE
2701
2702 mock_input_manager.last_event_was_delete.return_value = False
2703 mock_input_manager.last_event_was_cut.return_value = True
2704 result = AXUtilitiesEvent._get_text_deletion_event_reason(mock_event)
2705 assert result == TextEventReason.CUT
2706
2707 mock_input_manager.last_event_was_cut.return_value = False
2708 mock_input_manager.last_event_was_paste.return_value = True
2709 result = AXUtilitiesEvent._get_text_deletion_event_reason(mock_event)
2710 assert result == TextEventReason.PASTE
2711
2712 mock_input_manager.last_event_was_paste.return_value = False
2713 mock_input_manager.last_event_was_undo.return_value = True
2714 result = AXUtilitiesEvent._get_text_deletion_event_reason(mock_event)
2715 assert result == TextEventReason.UNDO
2716
2717 mock_input_manager.last_event_was_undo.return_value = False
2718 mock_input_manager.last_event_was_redo.return_value = True
2719 result = AXUtilitiesEvent._get_text_deletion_event_reason(mock_event)
2720 assert result == TextEventReason.REDO
2721
2722 mock_input_manager.last_event_was_redo.return_value = False
2723 mock_input_manager.last_event_was_command.return_value = True
2724 result = AXUtilitiesEvent._get_text_deletion_event_reason(mock_event)
2725 assert result == TextEventReason.UNSPECIFIED_COMMAND
2726
2727 def test_get_text_deletion_event_reason_command_non_editable(self, test_context):
2728 """Test _get_text_deletion_event_reason command scenario for non-editable."""
2729
2730 self._setup_dependencies(test_context)
2731 from orca import input_event_manager
2732 from orca.ax_object import AXObject
2733 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2734 from orca.ax_utilities_role import AXUtilitiesRole
2735 from orca.ax_utilities_state import AXUtilitiesState
2736
2737 mock_event = test_context.Mock(spec=Atspi.Event)
2738 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2739 mock_event.source = mock_obj
2740 mock_event.any_data = "deleted text"
2741 mock_event.type = "object:text-changed:delete"
2742
2743 mock_input_manager = test_context.Mock()
2744 mock_input_manager.last_event_was_page_switch.return_value = False
2745 mock_input_manager.last_event_was_command.return_value = True
2746 test_context.patch_object(
2747 input_event_manager,
2748 "get_manager",
2749 return_value=mock_input_manager,
2750 )
2751
2752 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
2753 test_context.patch_object(
2754 AXUtilitiesRole,
2755 "get_text_ui_roles",
2756 return_value=[Atspi.Role.LABEL],
2757 )
2758 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=False)
2759 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2760
2761 result = AXUtilitiesEvent._get_text_deletion_event_reason(mock_event)
2762 assert result == TextEventReason.UNSPECIFIED_COMMAND
2763
2764 def test_get_text_insertion_event_reason_editable_backspace(self, test_context):
2765 """Test _get_text_insertion_event_reason with editable backspace scenario."""
2766
2767 self._setup_dependencies(test_context)
2768 from orca import input_event_manager
2769 from orca.ax_object import AXObject
2770 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2771 from orca.ax_utilities_role import AXUtilitiesRole
2772 from orca.ax_utilities_state import AXUtilitiesState
2773 from orca.ax_utilities_text import AXUtilitiesText
2774
2775 mock_event = test_context.Mock(spec=Atspi.Event)
2776 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2777 mock_event.source = mock_obj
2778 mock_event.any_data = "inserted text"
2779 mock_event.type = "object:text-changed:insert"
2780
2781 mock_input_manager = test_context.Mock()
2782 mock_input_manager.last_event_was_page_switch.return_value = False
2783 mock_input_manager.last_event_was_backspace.return_value = True
2784 mock_input_manager.last_event_was_delete.return_value = False
2785 mock_input_manager.last_event_was_cut.return_value = False
2786 mock_input_manager.last_event_was_paste.return_value = False
2787 mock_input_manager.last_event_was_undo.return_value = False
2788 mock_input_manager.last_event_was_redo.return_value = False
2789 mock_input_manager.last_event_was_command.return_value = False
2790 mock_input_manager.last_event_was_space.return_value = False
2791 mock_input_manager.last_event_was_tab.return_value = False
2792 mock_input_manager.last_event_was_return.return_value = False
2793 mock_input_manager.last_event_was_printable_key.return_value = False
2794 mock_input_manager.last_event_was_middle_click.return_value = False
2795 mock_input_manager.last_event_was_middle_release.return_value = False
2796 mock_input_manager.last_event_was_up_or_down.return_value = False
2797 mock_input_manager.last_event_was_page_up_or_page_down.return_value = False
2798 test_context.patch_object(
2799 input_event_manager,
2800 "get_manager",
2801 return_value=mock_input_manager,
2802 )
2803
2804 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
2805 test_context.patch_object(
2806 AXUtilitiesRole,
2807 "get_text_ui_roles",
2808 return_value=[Atspi.Role.LABEL],
2809 )
2810 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
2811 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2812 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
2813 result = AXUtilitiesEvent._get_text_insertion_event_reason(mock_event)
2814 assert result == TextEventReason.BACKSPACE
2815
2816 def test_get_text_insertion_event_reason_redo_selected_text_restoration(self, test_context):
2817 """Test _get_text_insertion_event_reason with redo and selected text restoration."""
2818
2819 self._setup_dependencies(test_context)
2820 from orca import input_event_manager
2821 from orca.ax_object import AXObject
2822 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2823 from orca.ax_utilities_role import AXUtilitiesRole
2824 from orca.ax_utilities_state import AXUtilitiesState
2825 from orca.ax_utilities_text import AXUtilitiesText
2826
2827 mock_event = test_context.Mock(spec=Atspi.Event)
2828 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2829 mock_event.source = mock_obj
2830 mock_event.any_data = "selected text"
2831 mock_event.type = "object:text-changed:insert"
2832
2833 mock_input_manager = test_context.Mock()
2834 mock_input_manager.last_event_was_page_switch.return_value = False
2835 mock_input_manager.last_event_was_backspace.return_value = False
2836 mock_input_manager.last_event_was_delete.return_value = False
2837 mock_input_manager.last_event_was_cut.return_value = False
2838 mock_input_manager.last_event_was_paste.return_value = False
2839 mock_input_manager.last_event_was_undo.return_value = False
2840 mock_input_manager.last_event_was_redo.return_value = True
2841 mock_input_manager.last_event_was_command.return_value = False
2842 mock_input_manager.last_event_was_space.return_value = False
2843 mock_input_manager.last_event_was_tab.return_value = False
2844 mock_input_manager.last_event_was_return.return_value = False
2845 mock_input_manager.last_event_was_printable_key.return_value = False
2846 mock_input_manager.last_event_was_middle_click.return_value = False
2847 mock_input_manager.last_event_was_middle_release.return_value = False
2848 mock_input_manager.last_event_was_up_or_down.return_value = False
2849 mock_input_manager.last_event_was_page_up_or_page_down.return_value = False
2850 test_context.patch_object(
2851 input_event_manager,
2852 "get_manager",
2853 return_value=mock_input_manager,
2854 )
2855
2856 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
2857 test_context.patch_object(
2858 AXUtilitiesRole,
2859 "get_text_ui_roles",
2860 return_value=[Atspi.Role.LABEL],
2861 )
2862 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
2863 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2864 test_context.patch_object(
2865 AXUtilitiesText,
2866 "get_selected_text",
2867 return_value=("selected text", 0, 13),
2868 )
2869
2870 result = AXUtilitiesEvent._get_text_insertion_event_reason(mock_event)
2871 assert result == TextEventReason.SELECTED_TEXT_RESTORATION
2872
2873 def test_get_text_selection_changed_event_reason_comprehensive(self, test_context):
2874 """Test _get_text_selection_changed_event_reason with various scenarios."""
2875
2876 self._setup_dependencies(test_context)
2877 from orca import focus_manager, input_event_manager
2878 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2879 from orca.ax_utilities_role import AXUtilitiesRole
2880 from orca.ax_utilities_state import AXUtilitiesState
2881
2882 mock_event = test_context.Mock(spec=Atspi.Event)
2883 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2884 mock_event.source = mock_obj
2885
2886 mock_focus_manager = test_context.Mock()
2887 mock_focus_manager.get_locus_of_focus.return_value = mock_obj
2888 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
2889
2890 mock_search_obj = test_context.Mock(spec=Atspi.Accessible)
2891 mock_focus_manager.get_locus_of_focus.return_value = mock_search_obj
2892
2893 mock_input_manager = test_context.Mock()
2894 mock_input_manager.last_event_was_backspace.return_value = False
2895 mock_input_manager.last_event_was_delete.return_value = False
2896 mock_input_manager.last_event_was_caret_selection.return_value = False
2897 mock_input_manager.last_event_was_caret_navigation.return_value = False
2898 mock_input_manager.last_event_was_select_all.return_value = False
2899 mock_input_manager.last_event_was_primary_click_or_release.return_value = False
2900 test_context.patch_object(
2901 input_event_manager,
2902 "get_manager",
2903 return_value=mock_input_manager,
2904 )
2905
2906 test_context.patch_object(AXUtilitiesRole, "is_text_input_search", return_value=True)
2907 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=False)
2908 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2909
2910 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
2911 assert result == TextEventReason.SEARCH_PRESENTABLE
2912
2913 mock_input_manager.last_event_was_backspace.return_value = True
2914 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
2915 assert result == TextEventReason.SEARCH_UNPRESENTABLE
2916
2917 mock_input_manager.last_event_was_backspace.return_value = False
2918 mock_input_manager.last_event_was_delete.return_value = True
2919 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
2920 assert result == TextEventReason.SEARCH_UNPRESENTABLE
2921
2922 mock_focus_manager.get_locus_of_focus.return_value = mock_obj
2923 mock_input_manager.last_event_was_delete.return_value = False
2924 mock_input_manager.last_event_was_caret_selection.return_value = True
2925
2926 mock_input_manager.last_event_was_line_navigation.return_value = True
2927 mock_input_manager.last_event_was_word_navigation.return_value = False
2928 mock_input_manager.last_event_was_character_navigation.return_value = False
2929 mock_input_manager.last_event_was_page_navigation.return_value = False
2930 mock_input_manager.last_event_was_line_boundary_navigation.return_value = False
2931 mock_input_manager.last_event_was_file_boundary_navigation.return_value = False
2932 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
2933 assert result == TextEventReason.SELECTION_BY_LINE
2934
2935 mock_input_manager.last_event_was_line_navigation.return_value = False
2936 mock_input_manager.last_event_was_word_navigation.return_value = True
2937 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
2938 assert result == TextEventReason.SELECTION_BY_WORD
2939
2940 mock_input_manager.last_event_was_word_navigation.return_value = False
2941 mock_input_manager.last_event_was_character_navigation.return_value = True
2942 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
2943 assert result == TextEventReason.SELECTION_BY_CHARACTER
2944
2945 mock_input_manager.last_event_was_character_navigation.return_value = False
2946 mock_input_manager.last_event_was_page_navigation.return_value = True
2947 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
2948 assert result == TextEventReason.SELECTION_BY_PAGE
2949
2950 mock_input_manager.last_event_was_page_navigation.return_value = False
2951 mock_input_manager.last_event_was_line_boundary_navigation.return_value = True
2952 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
2953 assert result == TextEventReason.SELECTION_TO_LINE_BOUNDARY
2954
2955 mock_input_manager.last_event_was_line_boundary_navigation.return_value = False
2956 mock_input_manager.last_event_was_file_boundary_navigation.return_value = True
2957 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
2958 assert result == TextEventReason.SELECTION_TO_FILE_BOUNDARY
2959
2960 mock_input_manager.last_event_was_file_boundary_navigation.return_value = False
2961 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
2962 assert result == TextEventReason.UNSPECIFIED_SELECTION
2963
2964 def test_get_text_selection_changed_event_reason_caret_navigation(self, test_context):
2965 """Test _get_text_selection_changed_event_reason caret navigation scenarios."""
2966
2967 self._setup_dependencies(test_context)
2968 from orca import focus_manager, input_event_manager
2969 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
2970 from orca.ax_utilities_role import AXUtilitiesRole
2971 from orca.ax_utilities_state import AXUtilitiesState
2972
2973 mock_event = test_context.Mock(spec=Atspi.Event)
2974 mock_obj = test_context.Mock(spec=Atspi.Accessible)
2975 mock_event.source = mock_obj
2976
2977 mock_focus_manager = test_context.Mock()
2978 mock_focus_manager.get_locus_of_focus.return_value = mock_obj
2979 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
2980
2981 mock_input_manager = test_context.Mock()
2982 mock_input_manager.last_event_was_caret_selection.return_value = False
2983 mock_input_manager.last_event_was_caret_navigation.return_value = True
2984 mock_input_manager.last_event_was_select_all.return_value = False
2985 mock_input_manager.last_event_was_primary_click_or_release.return_value = False
2986 test_context.patch_object(
2987 input_event_manager,
2988 "get_manager",
2989 return_value=mock_input_manager,
2990 )
2991
2992 test_context.patch_object(AXUtilitiesRole, "is_text_input_search", return_value=False)
2993 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=False)
2994 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
2995
2996 mock_input_manager.last_event_was_line_navigation.return_value = True
2997 mock_input_manager.last_event_was_word_navigation.return_value = False
2998 mock_input_manager.last_event_was_character_navigation.return_value = False
2999 mock_input_manager.last_event_was_page_navigation.return_value = False
3000 mock_input_manager.last_event_was_line_boundary_navigation.return_value = False
3001 mock_input_manager.last_event_was_file_boundary_navigation.return_value = False
3002 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3003 assert result == TextEventReason.NAVIGATION_BY_LINE
3004
3005 mock_input_manager.last_event_was_line_navigation.return_value = False
3006 mock_input_manager.last_event_was_word_navigation.return_value = True
3007 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3008 assert result == TextEventReason.NAVIGATION_BY_WORD
3009
3010 mock_input_manager.last_event_was_word_navigation.return_value = False
3011 mock_input_manager.last_event_was_character_navigation.return_value = True
3012 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3013 assert result == TextEventReason.NAVIGATION_BY_CHARACTER
3014
3015 mock_input_manager.last_event_was_character_navigation.return_value = False
3016 mock_input_manager.last_event_was_page_navigation.return_value = True
3017 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3018 assert result == TextEventReason.NAVIGATION_BY_PAGE
3019
3020 mock_input_manager.last_event_was_page_navigation.return_value = False
3021 mock_input_manager.last_event_was_line_boundary_navigation.return_value = True
3022 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3023 assert result == TextEventReason.NAVIGATION_TO_LINE_BOUNDARY
3024
3025 mock_input_manager.last_event_was_line_boundary_navigation.return_value = False
3026 mock_input_manager.last_event_was_file_boundary_navigation.return_value = True
3027 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3028 assert result == TextEventReason.NAVIGATION_TO_FILE_BOUNDARY
3029
3030 mock_input_manager.last_event_was_file_boundary_navigation.return_value = False
3031 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3032 assert result == TextEventReason.UNSPECIFIED_NAVIGATION
3033
3034 def test_get_text_selection_changed_event_reason_editable_scenarios(self, test_context):
3035 """Test _get_text_selection_changed_event_reason with editable scenarios."""
3036
3037 self._setup_dependencies(test_context)
3038 from orca import focus_manager, input_event_manager
3039 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
3040 from orca.ax_utilities_object import AXUtilitiesObject
3041 from orca.ax_utilities_role import AXUtilitiesRole
3042 from orca.ax_utilities_state import AXUtilitiesState
3043
3044 mock_event = test_context.Mock(spec=Atspi.Event)
3045 mock_obj = test_context.Mock(spec=Atspi.Accessible)
3046 mock_event.source = mock_obj
3047
3048 mock_focus_manager = test_context.Mock()
3049 mock_focus_manager.get_locus_of_focus.return_value = mock_obj
3050 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
3051
3052 mock_input_manager = test_context.Mock()
3053 mock_input_manager.last_event_was_caret_selection.return_value = False
3054 mock_input_manager.last_event_was_caret_navigation.return_value = False
3055 mock_input_manager.last_event_was_select_all.return_value = False
3056 mock_input_manager.last_event_was_primary_click_or_release.return_value = False
3057 mock_input_manager.last_event_was_page_switch.return_value = False
3058 mock_input_manager.last_event_was_command.return_value = False
3059 mock_input_manager.last_event_was_printable_key.return_value = False
3060 mock_input_manager.last_event_was_up_or_down.return_value = False
3061 mock_input_manager.last_event_was_page_up_or_page_down.return_value = False
3062 test_context.patch_object(
3063 input_event_manager,
3064 "get_manager",
3065 return_value=mock_input_manager,
3066 )
3067
3068 test_context.patch_object(AXUtilitiesRole, "is_text_input_search", return_value=False)
3069 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
3070 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
3071 test_context.patch_object(AXUtilitiesRole, "is_spin_button", return_value=False)
3072 test_context.patch_object(AXUtilitiesObject, "find_ancestor", return_value=None)
3073
3074 mock_input_manager.last_event_was_backspace.return_value = True
3075 mock_input_manager.last_event_was_delete.return_value = False
3076 mock_input_manager.last_event_was_cut.return_value = False
3077 mock_input_manager.last_event_was_paste.return_value = False
3078 mock_input_manager.last_event_was_undo.return_value = False
3079 mock_input_manager.last_event_was_redo.return_value = False
3080 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3081 assert result == TextEventReason.BACKSPACE
3082
3083 mock_input_manager.last_event_was_backspace.return_value = False
3084 mock_input_manager.last_event_was_delete.return_value = True
3085 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3086 assert result == TextEventReason.DELETE
3087
3088 mock_input_manager.last_event_was_delete.return_value = False
3089 mock_input_manager.last_event_was_cut.return_value = True
3090 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3091 assert result == TextEventReason.CUT
3092
3093 mock_input_manager.last_event_was_cut.return_value = False
3094 mock_input_manager.last_event_was_paste.return_value = True
3095 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3096 assert result == TextEventReason.PASTE
3097
3098 mock_input_manager.last_event_was_paste.return_value = False
3099 mock_input_manager.last_event_was_undo.return_value = True
3100 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3101 assert result == TextEventReason.UNDO
3102
3103 mock_input_manager.last_event_was_undo.return_value = False
3104 mock_input_manager.last_event_was_redo.return_value = True
3105 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3106 assert result == TextEventReason.REDO
3107
3108 mock_input_manager.last_event_was_redo.return_value = False
3109 mock_input_manager.last_event_was_page_switch.return_value = True
3110 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3111 assert result == TextEventReason.PAGE_SWITCH
3112
3113 mock_input_manager.last_event_was_page_switch.return_value = False
3114 mock_input_manager.last_event_was_command.return_value = True
3115 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3116 assert result == TextEventReason.UNSPECIFIED_COMMAND
3117
3118 mock_input_manager.last_event_was_command.return_value = False
3119 mock_input_manager.last_event_was_printable_key.return_value = True
3120
3121 from orca.ax_utilities_text import AXUtilitiesText
3122
3123 test_context.patch_object(
3124 AXUtilitiesText, "get_cached_selected_text", return_value=("", 0, 0)
3125 )
3126 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
3127 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3128 assert result == TextEventReason.TYPING
3129
3130 test_context.patch_object(
3131 AXUtilitiesText, "get_selected_text", return_value=("n.wikipedia.org/", 1, 17)
3132 )
3133 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3134 assert result == TextEventReason.AUTO_SELECTION
3135
3136 mock_input_manager.last_event_was_printable_key.return_value = False
3137 mock_input_manager.last_event_was_up_or_down.return_value = True
3138 test_context.patch_object(AXUtilitiesRole, "is_spin_button", return_value=True)
3139 result = AXUtilitiesEvent._get_text_selection_changed_event_reason(mock_event)
3140 assert result == TextEventReason.SPIN_BUTTON_VALUE_CHANGE
3141
3142 @pytest.mark.parametrize(
3143 "case",
3144 [
3145 {
3146 "id": "active_descendant_no_focused_state",
3147 "test_scenario": "no_focused_state",
3148 "setup_config": {"is_focused": lambda obj: False},
3149 "expected_result": False,
3150 },
3151 {
3152 "id": "active_descendant_tree_table_different_tree",
3153 "test_scenario": "tree_table_different_tree",
3154 "setup_config": {
3155 "is_focused": "child_focused",
3156 "is_table_cell": True,
3157 "setup_tree_table": True,
3158 },
3159 "expected_result": False,
3160 },
3161 ],
3162 ids=lambda case: case["id"],
3163 )
3164 def test_is_presentable_active_descendant_change_edge_cases(self, test_context, case: dict):
3165 """Test is_presentable_active_descendant_change edge cases."""
3166
3167 self._setup_dependencies(test_context)
3168 from orca import focus_manager
3169 from orca.ax_utilities_event import AXUtilitiesEvent
3170 from orca.ax_utilities_object import AXUtilitiesObject
3171 from orca.ax_utilities_role import AXUtilitiesRole
3172 from orca.ax_utilities_state import AXUtilitiesState
3173
3174 mock_event = test_context.Mock(spec=Atspi.Event)
3175 mock_source = test_context.Mock(spec=Atspi.Accessible)
3176 mock_child = test_context.Mock(spec=Atspi.Accessible)
3177 mock_event.source = mock_source
3178 mock_event.any_data = mock_child
3179
3180 if case["test_scenario"] == "no_focused_state":
3181 test_context.patch_object(
3182 AXUtilitiesState,
3183 "is_focused",
3184 new=case["setup_config"]["is_focused"],
3185 )
3186 elif case["test_scenario"] == "tree_table_different_tree":
3187 test_context.patch_object(
3188 AXUtilitiesState,
3189 "is_focused",
3190 side_effect=lambda obj: obj == mock_child,
3191 )
3192 mock_focus = test_context.Mock(spec=Atspi.Accessible)
3193 mock_table = test_context.Mock(spec=Atspi.Accessible)
3194 mock_focus_manager = test_context.Mock()
3195 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
3196 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
3197 test_context.patch_object(AXUtilitiesRole, "is_table_cell", return_value=True)
3198 test_context.patch_object(AXUtilitiesObject, "find_ancestor", return_value=mock_table)
3199
3200 result = AXUtilitiesEvent.is_presentable_active_descendant_change(mock_event)
3201 assert result is case["expected_result"]
3202
3203 @pytest.mark.parametrize(
3204 "case",
3205 [
3206 {
3207 "id": "checked_radio_button_no_space",
3208 "test_scenario": "ancestor_not_list_tree_item",
3209 "expected_result": False,
3210 },
3211 ],
3212 ids=lambda case: case["id"],
3213 )
3214 def test_is_presentable_checked_change_edge_cases(self, test_context, case: dict):
3215 """Test is_presentable_checked_change edge cases."""
3216
3217 self._setup_dependencies(test_context)
3218 from orca import focus_manager, input_event_manager
3219 from orca.ax_utilities_event import AXUtilitiesEvent
3220 from orca.ax_utilities_object import AXUtilitiesObject
3221 from orca.ax_utilities_role import AXUtilitiesRole
3222 from orca.ax_utilities_state import AXUtilitiesState
3223
3224 mock_event = test_context.Mock(spec=Atspi.Event)
3225 mock_source = test_context.Mock(spec=Atspi.Accessible)
3226 mock_event.source = mock_source
3227 mock_focus = test_context.Mock(spec=Atspi.Accessible)
3228 mock_focus_manager = test_context.Mock()
3229 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
3230 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
3231 test_context.patch_object(AXUtilitiesState, "is_checked", return_value=True)
3232 AXUtilitiesEvent.LAST_KNOWN_CHECKED.clear()
3233
3234 if case["test_scenario"] == "ancestor_not_list_tree_item":
3235 test_context.patch_object(AXUtilitiesObject, "is_ancestor", return_value=True)
3236 test_context.patch_object(AXUtilitiesRole, "is_list_item", return_value=False)
3237 test_context.patch_object(AXUtilitiesRole, "is_tree_item", return_value=False)
3238 elif case["test_scenario"] == "radio_button_no_space":
3239 mock_focus_manager.get_locus_of_focus.return_value = mock_source
3240 test_context.patch_object(AXUtilitiesRole, "is_radio_button", return_value=True)
3241 mock_input_manager = test_context.Mock()
3242 mock_input_manager.last_event_was_space.return_value = False
3243 test_context.patch_object(
3244 input_event_manager,
3245 "get_manager",
3246 return_value=mock_input_manager,
3247 )
3248
3249 result = AXUtilitiesEvent.is_presentable_checked_change(mock_event)
3250 assert result is case["expected_result"]
3251
3252 @pytest.mark.parametrize(
3253 "case",
3254 [
3255 {
3256 "id": "description_empty",
3257 "test_scenario": "empty_description",
3258 "description_data": "",
3259 "is_showing": True,
3260 "is_ancestor": True,
3261 "expected_result": False,
3262 },
3263 ],
3264 ids=lambda case: case["id"],
3265 )
3266 def test_is_presentable_description_change_edge_cases(self, test_context, case: dict):
3267 """Test is_presentable_description_change edge cases."""
3268
3269 self._setup_dependencies(test_context)
3270 from orca import focus_manager
3271 from orca.ax_utilities_event import AXUtilitiesEvent
3272 from orca.ax_utilities_object import AXUtilitiesObject
3273 from orca.ax_utilities_state import AXUtilitiesState
3274
3275 mock_event = test_context.Mock(spec=Atspi.Event)
3276 mock_source = test_context.Mock(spec=Atspi.Accessible)
3277 mock_event.source = mock_source
3278 mock_event.any_data = case["description_data"]
3279 AXUtilitiesEvent.LAST_KNOWN_DESCRIPTION.clear()
3280
3281 test_context.patch_object(
3282 AXUtilitiesState,
3283 "is_showing",
3284 side_effect=lambda obj: case["is_showing"],
3285 )
3286
3287 if case["test_scenario"] in ["not_focus_ancestor", "not_showing"]:
3288 mock_focus = test_context.Mock(spec=Atspi.Accessible)
3289 mock_focus_manager = test_context.Mock()
3290 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
3291 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
3292 test_context.patch_object(
3293 AXUtilitiesObject,
3294 "is_ancestor",
3295 side_effect=lambda focus, source: case["is_ancestor"],
3296 )
3297
3298 result = AXUtilitiesEvent.is_presentable_description_change(mock_event)
3299 assert result is case["expected_result"]
3300
3301 @pytest.mark.parametrize(
3302 "case",
3303 [
3304 {
3305 "id": "expanded_not_focus_ancestor",
3306 "test_scenario": "not_focus_ancestor",
3307 "detail1": 0,
3308 "role_configs": {},
3309 "is_ancestor": False,
3310 "is_focused": True,
3311 "expected_result": False,
3312 },
3313 {
3314 "id": "expanded_table_row",
3315 "test_scenario": "table_row",
3316 "detail1": 0,
3317 "role_configs": {"is_table_row": True, "is_list_box": False},
3318 "is_ancestor": True,
3319 "is_focused": True,
3320 "expected_result": True,
3321 },
3322 {
3323 "id": "expanded_list_box",
3324 "test_scenario": "list_box",
3325 "detail1": 1,
3326 "role_configs": {"is_table_row": False, "is_list_box": True},
3327 "is_ancestor": True,
3328 "is_focused": True,
3329 "expected_result": True,
3330 },
3331 {
3332 "id": "expanded_combo_box_no_focus",
3333 "test_scenario": "combo_box_no_focus",
3334 "detail1": 1,
3335 "role_configs": {"is_combo_box": True, "is_button": False},
3336 "is_ancestor": True,
3337 "is_focused": False,
3338 "expected_result": False,
3339 },
3340 {
3341 "id": "expanded_button_no_focus",
3342 "test_scenario": "button_no_focus",
3343 "detail1": 1,
3344 "role_configs": {"is_combo_box": False, "is_button": True},
3345 "is_ancestor": True,
3346 "is_focused": False,
3347 "expected_result": False,
3348 },
3349 ],
3350 ids=lambda case: case["id"],
3351 )
3352 def test_is_presentable_expanded_change_edge_cases(self, test_context, case: dict):
3353 """Test is_presentable_expanded_change edge cases."""
3354
3355 self._setup_dependencies(test_context)
3356 from orca import focus_manager
3357 from orca.ax_utilities_event import AXUtilitiesEvent
3358 from orca.ax_utilities_object import AXUtilitiesObject
3359 from orca.ax_utilities_role import AXUtilitiesRole
3360 from orca.ax_utilities_state import AXUtilitiesState
3361
3362 mock_event = test_context.Mock(spec=Atspi.Event)
3363 mock_source = test_context.Mock(spec=Atspi.Accessible)
3364 mock_event.source = mock_source
3365 mock_event.detail1 = case["detail1"]
3366 AXUtilitiesEvent.LAST_KNOWN_EXPANDED.clear()
3367
3368 test_context.patch_object(AXUtilitiesState, "is_expanded", return_value=True)
3369 mock_focus = test_context.Mock(spec=Atspi.Accessible)
3370 mock_focus_manager = test_context.Mock()
3371 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
3372 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
3373 test_context.patch_object(
3374 AXUtilitiesObject,
3375 "is_ancestor",
3376 side_effect=lambda focus, source: case["is_ancestor"],
3377 )
3378
3379 for role_method, value in case["role_configs"].items():
3380 test_context.patch_object(
3381 AXUtilitiesRole,
3382 role_method,
3383 side_effect=lambda obj, v=value: v,
3384 )
3385
3386 if "no_focus" in case["test_scenario"]:
3387 test_context.patch_object(
3388 AXUtilitiesState,
3389 "is_focused",
3390 side_effect=lambda obj: case["is_focused"],
3391 )
3392
3393 result = AXUtilitiesEvent.is_presentable_expanded_change(mock_event)
3394 assert result is case["expected_result"]
3395
3396 @pytest.mark.parametrize(
3397 "case",
3398 [
3399 {
3400 "id": "indeterminate_not_from_focus",
3401 "change_type": "indeterminate",
3402 "state_method": "is_indeterminate",
3403 "expected_result": False,
3404 },
3405 {
3406 "id": "invalid_entry_not_from_focus",
3407 "change_type": "invalid_entry",
3408 "state_method": "is_invalid_entry",
3409 "expected_result": False,
3410 },
3411 ],
3412 ids=lambda case: case["id"],
3413 )
3414 def test_is_presentable_simple_change_edge_cases(self, test_context, case: dict) -> None:
3415 """Test simple presentable change edge cases."""
3416
3417 self._setup_dependencies(test_context)
3418 from orca import focus_manager
3419 from orca.ax_utilities_event import AXUtilitiesEvent
3420 from orca.ax_utilities_state import AXUtilitiesState
3421
3422 mock_event = test_context.Mock(spec=Atspi.Event)
3423 mock_source = test_context.Mock(spec=Atspi.Accessible)
3424 mock_event.source = mock_source
3425
3426 cache_name = f"LAST_KNOWN_{case['change_type'].upper()}"
3427 getattr(AXUtilitiesEvent, cache_name).clear()
3428
3429 test_context.patch_object(AXUtilitiesState, case["state_method"], return_value=True)
3430
3431 mock_focus = test_context.Mock(spec=Atspi.Accessible)
3432 mock_focus_manager = test_context.Mock()
3433 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
3434 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
3435
3436 method_name = f"is_presentable_{case['change_type']}_change"
3437 result = getattr(AXUtilitiesEvent, method_name)(mock_event)
3438 assert result is case["expected_result"]
3439
3440 def test_is_presentable_name_change_edge_cases(self, test_context):
3441 """Test is_presentable_name_change edge cases."""
3442
3443 self._setup_dependencies(test_context)
3444 from orca import focus_manager
3445 from orca.ax_text import AXText
3446 from orca.ax_utilities_event import AXUtilitiesEvent
3447 from orca.ax_utilities_role import AXUtilitiesRole
3448 from orca.ax_utilities_state import AXUtilitiesState
3449
3450 mock_event = test_context.Mock(spec=Atspi.Event)
3451 mock_source = test_context.Mock(spec=Atspi.Accessible)
3452 mock_event.source = mock_source
3453 mock_event.any_data = "new name"
3454 AXUtilitiesEvent.LAST_KNOWN_NAME.clear()
3455
3456 mock_event.any_data = ""
3457 result = AXUtilitiesEvent.is_presentable_name_change(mock_event)
3458 assert result is False
3459
3460 mock_event.any_data = "new name"
3461 test_context.patch_object(AXUtilitiesState, "is_showing", return_value=False)
3462 result = AXUtilitiesEvent.is_presentable_name_change(mock_event)
3463 assert result is False
3464
3465 test_context.patch_object(AXUtilitiesState, "is_showing", return_value=True)
3466 test_context.patch_object(AXUtilitiesRole, "is_frame", return_value=True)
3467
3468 mock_active_window = test_context.Mock(spec=Atspi.Accessible)
3469 mock_focus_manager = test_context.Mock()
3470 mock_focus_manager.get_active_window.return_value = mock_active_window
3471 mock_focus_manager.get_locus_of_focus.return_value = mock_source
3472 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
3473
3474 result = AXUtilitiesEvent.is_presentable_name_change(mock_event)
3475 assert result is False
3476
3477 mock_focus_manager.get_active_window.return_value = mock_source
3478
3479 mock_focus = test_context.Mock(spec=Atspi.Accessible)
3480 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
3481
3482 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
3483 test_context.patch_object(AXText, "get_character_count", return_value=10)
3484 test_context.patch_object(AXText, "get_all_text", return_value="new")
3485
3486 result = AXUtilitiesEvent.is_presentable_name_change(mock_event)
3487 assert result is False
3488
3489 @pytest.mark.parametrize(
3490 "case",
3491 [
3492 {
3493 "id": "pressed_not_from_focus",
3494 "change_type": "pressed",
3495 "state_method": "is_pressed",
3496 "expected_result": False,
3497 },
3498 {
3499 "id": "selected_not_from_focus",
3500 "change_type": "selected",
3501 "state_method": "is_selected",
3502 "expected_result": False,
3503 },
3504 ],
3505 ids=lambda case: case["id"],
3506 )
3507 def test_is_presentable_state_change_edge_cases(self, test_context, case: dict) -> None:
3508 """Test presentable state change edge cases not from focus."""
3509
3510 self._setup_dependencies(test_context)
3511 from orca import focus_manager
3512 from orca.ax_utilities_event import AXUtilitiesEvent
3513 from orca.ax_utilities_state import AXUtilitiesState
3514
3515 mock_event = test_context.Mock(spec=Atspi.Event)
3516 mock_source = test_context.Mock(spec=Atspi.Accessible)
3517 mock_event.source = mock_source
3518
3519 cache_name = f"LAST_KNOWN_{case['change_type'].upper()}"
3520 getattr(AXUtilitiesEvent, cache_name).clear()
3521
3522 test_context.patch_object(AXUtilitiesState, case["state_method"], return_value=True)
3523
3524 mock_focus = test_context.Mock(spec=Atspi.Accessible)
3525 mock_focus_manager = test_context.Mock()
3526 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
3527 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
3528
3529 method_name = f"is_presentable_{case['change_type']}_change"
3530 result = getattr(AXUtilitiesEvent, method_name)(mock_event)
3531 assert result is case["expected_result"]
3532
3533 def test_is_presentable_text_event_edge_cases(self, test_context):
3534 """Test _is_presentable_text_event edge cases."""
3535
3536 self._setup_dependencies(test_context)
3537 from orca import focus_manager
3538 from orca.ax_utilities_event import AXUtilitiesEvent
3539 from orca.ax_utilities_object import AXUtilitiesObject
3540 from orca.ax_utilities_role import AXUtilitiesRole
3541 from orca.ax_utilities_state import AXUtilitiesState
3542
3543 mock_event = test_context.Mock(spec=Atspi.Event)
3544 mock_source = test_context.Mock(spec=Atspi.Accessible)
3545 mock_event.source = mock_source
3546
3547 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=False)
3548 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
3549
3550 result = AXUtilitiesEvent._is_presentable_text_event(mock_event)
3551 assert result is False
3552
3553 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
3554
3555 mock_focus = test_context.Mock(spec=Atspi.Accessible)
3556 mock_focus_manager = test_context.Mock()
3557 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
3558 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
3559
3560 test_context.patch_object(AXUtilitiesState, "is_focused", return_value=False)
3561 test_context.patch_object(AXUtilitiesObject, "is_ancestor", return_value=True)
3562
3563 result = AXUtilitiesEvent._is_presentable_text_event(mock_event)
3564 assert result is True
3565
3566 def test_get_text_insertion_event_reason_additional_scenarios(self, test_context):
3567 """Test additional _get_text_insertion_event_reason scenarios for missing coverage."""
3568
3569 self._setup_dependencies(test_context)
3570 from orca import input_event_manager
3571 from orca.ax_object import AXObject
3572 from orca.ax_utilities_event import AXUtilitiesEvent, TextEventReason
3573 from orca.ax_utilities_role import AXUtilitiesRole
3574 from orca.ax_utilities_state import AXUtilitiesState
3575 from orca.ax_utilities_text import AXUtilitiesText
3576
3577 mock_event = test_context.Mock(spec=Atspi.Event)
3578 mock_obj = test_context.Mock(spec=Atspi.Accessible)
3579 mock_event.source = mock_obj
3580 mock_event.any_data = "x"
3581 mock_event.type = "object:text-changed:insert"
3582
3583 mock_input_manager = test_context.Mock()
3584 mock_input_manager.last_event_was_page_switch.return_value = False
3585 mock_input_manager.last_event_was_backspace.return_value = False
3586 mock_input_manager.last_event_was_delete.return_value = False
3587 mock_input_manager.last_event_was_cut.return_value = False
3588 mock_input_manager.last_event_was_paste.return_value = False
3589 mock_input_manager.last_event_was_undo.return_value = False
3590 mock_input_manager.last_event_was_redo.return_value = False
3591 mock_input_manager.last_event_was_command.return_value = False
3592 mock_input_manager.last_event_was_space.return_value = False
3593 mock_input_manager.last_event_was_tab.return_value = False
3594 mock_input_manager.last_event_was_return.return_value = False
3595 mock_input_manager.last_event_was_printable_key.return_value = True
3596 mock_input_manager.last_event_was_middle_click.return_value = False
3597 mock_input_manager.last_event_was_middle_release.return_value = False
3598 mock_input_manager.last_event_was_up_or_down.return_value = False
3599 mock_input_manager.last_event_was_page_up_or_page_down.return_value = False
3600 test_context.patch_object(
3601 input_event_manager,
3602 "get_manager",
3603 return_value=mock_input_manager,
3604 )
3605
3606 test_context.patch_object(AXObject, "get_role", return_value=Atspi.Role.TEXT)
3607 test_context.patch_object(
3608 AXUtilitiesRole,
3609 "get_text_ui_roles",
3610 return_value=[Atspi.Role.LABEL],
3611 )
3612 test_context.patch_object(AXUtilitiesState, "is_editable", return_value=True)
3613 test_context.patch_object(AXUtilitiesRole, "is_terminal", return_value=False)
3614 test_context.patch_object(AXUtilitiesRole, "is_password_text", return_value=True)
3615 test_context.patch_object(AXUtilitiesText, "get_selected_text", return_value=("", 0, 0))
3616
3617 from orca import typing_echo_presenter
3618
3619 mock_presenter = test_context.Mock()
3620 mock_presenter.get_key_echo_enabled.return_value = True
3621 mock_presenter.get_character_echo_enabled.return_value = False
3622 mock_presenter.get_locking_keys_presented.return_value = True
3623 test_context.patch_object(
3624 typing_echo_presenter,
3625 "get_presenter",
3626 return_value=mock_presenter,
3627 )
3628
3629 result = AXUtilitiesEvent._get_text_insertion_event_reason(mock_event)
3630 assert result == TextEventReason.TYPING_ECHOABLE
3631
3632 mock_event.any_data = "multi-char"
3633 test_context.patch_object(AXUtilitiesRole, "is_password_text", return_value=False)
3634 mock_presenter.get_locking_keys_presented.return_value = False
3635
3636 result = AXUtilitiesEvent._get_text_insertion_event_reason(mock_event)
3637 assert result == TextEventReason.TYPING
3638
3639 def test_ignore_name_changes_for_cleared_by_clear_all(self, test_context):
3640 """Test that IGNORE_NAME_CHANGES_FOR is cleared by _clear_all_dictionaries."""
3641
3642 self._setup_dependencies(test_context)
3643 from orca import debug
3644 from orca.ax_utilities_event import AXUtilitiesEvent
3645
3646 mock_obj = test_context.Mock(spec=Atspi.Accessible)
3647 AXUtilitiesEvent.IGNORE_NAME_CHANGES_FOR.append(hash(mock_obj))
3648 assert len(AXUtilitiesEvent.IGNORE_NAME_CHANGES_FOR) == 1
3649
3650 test_context.patch_object(debug, "print_message", new=test_context.Mock())
3651
3652 AXUtilitiesEvent._clear_all_dictionaries("test clear")
3653
3654 assert len(AXUtilitiesEvent.IGNORE_NAME_CHANGES_FOR) == 0
3655
3656 def test_is_presentable_name_change_returns_false_when_source_in_ignore_list(
3657 self,
3658 test_context,
3659 ):
3660 """Test is_presentable_name_change returns False when source is in ignore list."""
3661
3662 self._setup_dependencies(test_context)
3663 from orca.ax_utilities_event import AXUtilitiesEvent
3664
3665 mock_event = test_context.Mock(spec=Atspi.Event)
3666 mock_source = test_context.Mock(spec=Atspi.Accessible)
3667 mock_event.source = mock_source
3668 mock_event.any_data = "new name"
3669
3670 AXUtilitiesEvent.IGNORE_NAME_CHANGES_FOR.clear()
3671 AXUtilitiesEvent.IGNORE_NAME_CHANGES_FOR.append(hash(mock_source))
3672
3673 result = AXUtilitiesEvent.is_presentable_name_change(mock_event)
3674 assert result is False
3675
3676 def test_is_presentable_name_change_list_item_with_progress_bar(self, test_context):
3677 """Test is_presentable_name_change returns False for list item with progress bar."""
3678
3679 self._setup_dependencies(test_context)
3680 from orca import focus_manager
3681 from orca.ax_object import AXObject
3682 from orca.ax_utilities_event import AXUtilitiesEvent
3683 from orca.ax_utilities_object import AXUtilitiesObject
3684 from orca.ax_utilities_role import AXUtilitiesRole
3685 from orca.ax_utilities_state import AXUtilitiesState
3686
3687 mock_event = test_context.Mock(spec=Atspi.Event)
3688 mock_source = test_context.Mock(spec=Atspi.Accessible)
3689 mock_progress_bar = test_context.Mock(spec=Atspi.Accessible)
3690 mock_event.source = mock_source
3691 mock_event.any_data = "Downloading... 50%"
3692
3693 AXUtilitiesEvent.LAST_KNOWN_NAME.clear()
3694 AXUtilitiesEvent.IGNORE_NAME_CHANGES_FOR.clear()
3695
3696 mock_focus_manager = test_context.Mock()
3697 mock_focus_manager.get_locus_of_focus.return_value = mock_source
3698 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
3699
3700 test_context.patch_object(AXUtilitiesState, "is_showing", return_value=True)
3701 test_context.patch_object(AXUtilitiesRole, "is_frame", return_value=False)
3702 test_context.patch_object(AXUtilitiesRole, "is_list_item", return_value=True)
3703 test_context.patch_object(AXObject, "supports_collection", return_value=False)
3704 test_context.patch_object(
3705 AXUtilitiesObject, "find_descendant", return_value=mock_progress_bar
3706 )
3707
3708 result = AXUtilitiesEvent.is_presentable_name_change(mock_event)
3709 assert result is False
3710 assert hash(mock_source) in AXUtilitiesEvent.IGNORE_NAME_CHANGES_FOR
3711
3712 def test_is_presentable_name_change_list_item_without_progress_bar(self, test_context):
3713 """Test is_presentable_name_change returns True for list item without progress bar."""
3714
3715 self._setup_dependencies(test_context)
3716 from orca import focus_manager
3717 from orca.ax_object import AXObject
3718 from orca.ax_utilities_event import AXUtilitiesEvent
3719 from orca.ax_utilities_object import AXUtilitiesObject
3720 from orca.ax_utilities_role import AXUtilitiesRole
3721 from orca.ax_utilities_state import AXUtilitiesState
3722
3723 mock_event = test_context.Mock(spec=Atspi.Event)
3724 mock_source = test_context.Mock(spec=Atspi.Accessible)
3725 mock_event.source = mock_source
3726 mock_event.any_data = "List item name"
3727
3728 AXUtilitiesEvent.LAST_KNOWN_NAME.clear()
3729 AXUtilitiesEvent.IGNORE_NAME_CHANGES_FOR.clear()
3730
3731 mock_focus_manager = test_context.Mock()
3732 mock_focus_manager.get_locus_of_focus.return_value = mock_source
3733 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
3734
3735 test_context.patch_object(AXUtilitiesState, "is_showing", return_value=True)
3736 test_context.patch_object(AXUtilitiesRole, "is_frame", return_value=False)
3737 test_context.patch_object(AXUtilitiesRole, "is_list_item", return_value=True)
3738 test_context.patch_object(AXObject, "supports_collection", return_value=False)
3739 test_context.patch_object(AXUtilitiesObject, "find_descendant", return_value=None)
3740
3741 result = AXUtilitiesEvent.is_presentable_name_change(mock_event)
3742 assert result is True
3743 assert hash(mock_source) not in AXUtilitiesEvent.IGNORE_NAME_CHANGES_FOR
3744
3745 def test_is_presentable_name_change_subsequent_calls_ignored(self, test_context):
3746 """Test that subsequent name changes are ignored after source added to ignore list."""
3747
3748 self._setup_dependencies(test_context)
3749 from orca import focus_manager
3750 from orca.ax_object import AXObject
3751 from orca.ax_utilities_event import AXUtilitiesEvent
3752 from orca.ax_utilities_object import AXUtilitiesObject
3753 from orca.ax_utilities_role import AXUtilitiesRole
3754 from orca.ax_utilities_state import AXUtilitiesState
3755
3756 mock_event = test_context.Mock(spec=Atspi.Event)
3757 mock_source = test_context.Mock(spec=Atspi.Accessible)
3758 mock_progress_bar = test_context.Mock(spec=Atspi.Accessible)
3759 mock_event.source = mock_source
3760 mock_event.any_data = "Downloading... 50%"
3761
3762 AXUtilitiesEvent.LAST_KNOWN_NAME.clear()
3763 AXUtilitiesEvent.IGNORE_NAME_CHANGES_FOR.clear()
3764
3765 mock_focus_manager = test_context.Mock()
3766 mock_focus_manager.get_locus_of_focus.return_value = mock_source
3767 test_context.patch_object(focus_manager, "get_manager", return_value=mock_focus_manager)
3768
3769 test_context.patch_object(AXUtilitiesState, "is_showing", return_value=True)
3770 test_context.patch_object(AXUtilitiesRole, "is_frame", return_value=False)
3771 test_context.patch_object(AXUtilitiesRole, "is_list_item", return_value=True)
3772 test_context.patch_object(AXObject, "supports_collection", return_value=False)
3773 test_context.patch_object(
3774 AXUtilitiesObject, "find_descendant", return_value=mock_progress_bar
3775 )
3776
3777 result1 = AXUtilitiesEvent.is_presentable_name_change(mock_event)
3778 assert result1 is False
3779 assert hash(mock_source) in AXUtilitiesEvent.IGNORE_NAME_CHANGES_FOR
3780
3781 mock_event.any_data = "Downloading... 75%"
3782 result2 = AXUtilitiesEvent.is_presentable_name_change(mock_event)
3783 assert result2 is False
3784
3785 mock_event.any_data = "Download complete"
3786 result3 = AXUtilitiesEvent.is_presentable_name_change(mock_event)
3787 assert result3 is False
3788
3789 @pytest.mark.parametrize(
3790 "case",
3791 [
3792 {
3793 "id": "identical_strings",
3794 "str1": "hello world",
3795 "str2": "hello world",
3796 "expected": True,
3797 },
3798 {
3799 "id": "very_similar_strings",
3800 "str1": "Loading...",
3801 "str2": "Loading.. ",
3802 "expected": True,
3803 },
3804 {
3805 "id": "similar_with_minor_difference",
3806 "str1": "File: document.txt",
3807 "str2": "File: document.txt ",
3808 "expected": True,
3809 },
3810 {
3811 "id": "different_strings",
3812 "str1": "hello world",
3813 "str2": "goodbye universe",
3814 "expected": False,
3815 },
3816 {
3817 "id": "empty_first_string",
3818 "str1": "",
3819 "str2": "hello",
3820 "expected": False,
3821 },
3822 {
3823 "id": "empty_second_string",
3824 "str1": "hello",
3825 "str2": "",
3826 "expected": False,
3827 },
3828 {
3829 "id": "both_empty",
3830 "str1": "",
3831 "str2": "",
3832 "expected": False,
3833 },
3834 {
3835 "id": "none_first_string",
3836 "str1": None,
3837 "str2": "hello",
3838 "expected": False,
3839 },
3840 {
3841 "id": "none_second_string",
3842 "str1": "hello",
3843 "str2": None,
3844 "expected": False,
3845 },
3846 {
3847 "id": "case_insensitive_identical",
3848 "str1": "Hello World",
3849 "str2": "hello world",
3850 "expected": True,
3851 },
3852 {
3853 "id": "moderately_different",
3854 "str1": "Button clicked",
3855 "str2": "Button pressed",
3856 "expected": False,
3857 },
3858 ],
3859 ids=lambda case: case["id"],
3860 )
3861 def test_strings_are_redundant(self, test_context, case: dict):
3862 """Test AXUtilitiesEvent._strings_are_redundant."""
3863
3864 self._setup_dependencies(test_context)
3865 from orca.ax_utilities_event import AXUtilitiesEvent
3866
3867 result = AXUtilitiesEvent._strings_are_redundant(case["str1"], case["str2"])
3868 assert result is case["expected"]
3869
3870 def test_strings_are_redundant_custom_threshold(self, test_context):
3871 """Test AXUtilitiesEvent._strings_are_redundant with custom threshold."""
3872
3873 self._setup_dependencies(test_context)
3874 from orca.ax_utilities_event import AXUtilitiesEvent
3875
3876 # These strings are ~73% similar
3877 str1 = "processing file"
3878 str2 = "processing data"
3879
3880 # Should be False with default 0.85 threshold
3881 result_default = AXUtilitiesEvent._strings_are_redundant(str1, str2)
3882 assert result_default is False
3883
3884 # Should be True with lower 0.7 threshold
3885 result_lower = AXUtilitiesEvent._strings_are_redundant(str1, str2, threshold=0.7)
3886 assert result_lower is True