Debian Codegraph @

tests/unit_tests/test_event_manager.py

raw
1 # Unit tests for event_manager.py methods.
2 #
3 # Copyright 2025 Igalia, S.L.
4 # Author: Joanmarie Diggs <[email protected]>
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the
18 # Free Software Foundation, Inc., Franklin Street, Fifth Floor,
19 # Boston MA 02110-1301 USA.
20
21 # pylint: disable=wrong-import-position
22 # pylint: disable=import-outside-toplevel
23 # pylint: disable=too-many-public-methods
24 # pylint: disable=too-many-statements
25 # pylint: disable=protected-access
26 # pylint: disable=too-many-arguments
27 # pylint: disable=too-many-positional-arguments
28 # pylint: disable=too-many-locals
29 # pylint: disable=too-many-lines
30
31 """Unit tests for event_manager.py methods."""
32
33 from __future__ import annotations
34
35 import itertools
36 import queue
37 from typing import TYPE_CHECKING
38 from unittest.mock import call
39
40 import gi
41 import pytest
42
43 gi.require_version("Atspi", "2.0")
44 from gi.repository import Atspi
45
46 if TYPE_CHECKING:
47 from unittest.mock import MagicMock
48
49 from .orca_test_context import OrcaTestContext
50
51
52 @pytest.mark.unit
53 class TestEventManager:
54 """Test EventManager class methods."""
55
56 def _setup_ignore_event_ax_utilities_mocks(self, test_context, mock_time=5000.0, is_text=True):
57 """Set up common AXUtilities mocks for event ignore testing scenarios."""
58
59 test_context.patch("time.time", return_value=mock_time)
60 test_context.patch("orca.event_manager.AXUtilities.is_text", return_value=is_text)
61 test_context.patch("orca.event_manager.AXUtilities.is_window", return_value=False)
62 test_context.patch("orca.event_manager.AXUtilities.is_frame", return_value=False)
63 test_context.patch("orca.event_manager.AXUtilities.is_notification", return_value=False)
64 test_context.patch("orca.event_manager.AXUtilities.is_alert", return_value=False)
65 test_context.patch("orca.event_manager.AXUtilities.is_selected", return_value=False)
66 test_context.patch("orca.event_manager.AXUtilities.is_focused", return_value=False)
67 test_context.patch("orca.event_manager.AXUtilities.is_section", return_value=False)
68
69 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]:
70 """Returns dependencies for event_manager module testing."""
71
72 additional_modules = [
73 "orca.input_event_manager",
74 "orca.orca_modifier_manager",
75 "orca.ax_utilities_debugging",
76 "orca.ax_utilities",
77 "orca.braille_presenter",
78 ]
79 essential_modules = test_context.setup_shared_dependencies(additional_modules)
80
81 debug_mock = essential_modules["orca.debug"]
82 debug_mock.LEVEL_INFO = 800
83 debug_mock.LEVEL_WARNING = 2
84 debug_mock.LEVEL_SEVERE = 3
85 debug_mock.debugLevel = 0
86
87 braille_presenter_mock = essential_modules["orca.braille_presenter"]
88 braille_presenter_instance = test_context.Mock()
89 braille_presenter_instance.disable_braille = test_context.Mock()
90 braille_presenter_mock.get_presenter = test_context.Mock(
91 return_value=braille_presenter_instance,
92 )
93
94 focus_manager_mock = essential_modules["orca.focus_manager"]
95 focus_mgr_instance = test_context.Mock()
96 focus_mgr_instance.get_locus_of_focus = test_context.Mock()
97 focus_mgr_instance.get_active_window = test_context.Mock()
98 focus_mgr_instance.focus_and_window_are_unknown = test_context.Mock(return_value=False)
99 focus_mgr_instance.clear_state = test_context.Mock()
100 focus_manager_mock.get_manager = test_context.Mock(return_value=focus_mgr_instance)
101
102 input_event_mock = essential_modules["orca.input_event"]
103
104 class MockKeyboardEvent:
105 """Mock KeyboardEvent for testing."""
106
107 class MockBrailleEvent:
108 """Mock BrailleEvent for testing."""
109
110 class MockMouseButtonEvent:
111 """Mock MouseButtonEvent for testing."""
112
113 input_event_mock.KeyboardEvent = MockKeyboardEvent
114 input_event_mock.BrailleEvent = MockBrailleEvent
115 input_event_mock.MouseButtonEvent = MockMouseButtonEvent
116
117 input_event_manager_mock = essential_modules["orca.input_event_manager"]
118 input_mgr_instance = test_context.Mock()
119 input_mgr_instance.start_key_watcher = test_context.Mock()
120 input_mgr_instance.stop_key_watcher = test_context.Mock()
121 input_event_manager_mock.get_manager = test_context.Mock(return_value=input_mgr_instance)
122
123 script_manager_mock = essential_modules["orca.script_manager"]
124 script_mgr_instance = test_context.Mock()
125 script_instance = test_context.Mock()
126 script_instance.app = test_context.Mock()
127 script_instance.event_cache = {}
128 script_instance.listeners = {}
129 script_instance.is_activatable_event = test_context.Mock(return_value=True)
130 script_instance.force_script_activation = test_context.Mock(return_value=False)
131 script_instance.present_if_inactive = False
132 script_mgr_instance.get_active_script = test_context.Mock(return_value=script_instance)
133 script_mgr_instance.get_script = test_context.Mock(return_value=script_instance)
134 script_mgr_instance.set_active_script = test_context.Mock()
135 script_mgr_instance.get_default_script = test_context.Mock(return_value=script_instance)
136 script_mgr_instance.reclaim_scripts = test_context.Mock()
137 script_manager_mock.get_manager = test_context.Mock(return_value=script_mgr_instance)
138
139 ax_utils_debugging_mock = essential_modules["orca.ax_utilities_debugging"]
140 ax_utils_debugging_mock.object_event_details_as_string = test_context.Mock(
141 return_value="mock details",
142 )
143
144 ax_object_mock = essential_modules["orca.ax_object"]
145 ax_object_mock.get_name = test_context.Mock()
146 ax_object_mock.get_parent = test_context.Mock()
147 ax_object_mock.get_role = test_context.Mock()
148 ax_object_mock.get_attribute = test_context.Mock()
149 ax_object_mock.is_dead = test_context.Mock(return_value=False)
150 ax_object_mock.has_state = test_context.Mock(return_value=False)
151
152 ax_utilities_mock = essential_modules["orca.ax_utilities"]
153 ax_utilities_mock.is_frame = test_context.Mock(return_value=False)
154 ax_utilities_mock.is_dialog_or_alert = test_context.Mock(return_value=False)
155 ax_utilities_mock.is_window = test_context.Mock(return_value=False)
156 ax_utilities_mock.is_text = test_context.Mock(return_value=False)
157 ax_utilities_mock.is_notification = test_context.Mock(return_value=False)
158 ax_utilities_mock.is_alert = test_context.Mock(return_value=False)
159 ax_utilities_mock.is_selected = test_context.Mock(return_value=False)
160 ax_utilities_mock.is_focused = test_context.Mock(return_value=False)
161 ax_utilities_mock.manages_descendants = test_context.Mock(return_value=False)
162 ax_utilities_mock.is_section = test_context.Mock(return_value=False)
163 ax_utilities_mock.get_application = test_context.Mock()
164 ax_utilities_mock.get_desktop = test_context.Mock()
165 ax_utilities_mock.is_invalid_role = test_context.Mock(return_value=False)
166 ax_utilities_mock.is_menu_related = test_context.Mock(return_value=False)
167 ax_utilities_mock.is_image = test_context.Mock(return_value=False)
168 ax_utilities_mock.is_showing = test_context.Mock(return_value=True)
169 ax_utilities_mock.is_selectable = test_context.Mock(return_value=False)
170 ax_utilities_mock.is_menu = test_context.Mock(return_value=False)
171 ax_utilities_mock.is_focusable = test_context.Mock(return_value=False)
172 ax_utilities_mock.is_panel = test_context.Mock(return_value=False)
173 ax_utilities_mock.is_modal = test_context.Mock(return_value=False)
174 ax_utilities_mock.is_progress_bar = test_context.Mock(return_value=False)
175 ax_utilities_mock.is_defunct = test_context.Mock(return_value=False)
176 ax_utilities_mock.is_application_in_desktop = test_context.Mock(return_value=True)
177 ax_utilities_mock.is_iconified = test_context.Mock(return_value=False)
178
179 glib_mock = test_context.Mock()
180 glib_mock.idle_add = test_context.Mock(return_value=123)
181 glib_mock.timeout_add = test_context.Mock()
182 test_context.patch("gi.repository.GLib", new=glib_mock)
183
184 essential_modules["focus_manager_instance"] = focus_mgr_instance
185 essential_modules["input_event_manager_instance"] = input_mgr_instance
186 essential_modules["script_manager_instance"] = script_mgr_instance
187 essential_modules["script_instance"] = script_instance
188 essential_modules["glib"] = glib_mock
189
190 return essential_modules
191
192 def test_init(self, test_context: OrcaTestContext) -> None:
193 """Test EventManager.__init__."""
194
195 self._setup_dependencies(test_context)
196 from orca.event_manager import EventManager
197
198 manager = EventManager()
199 assert not manager._script_listener_counts
200 assert manager._active is False
201 assert manager._paused is False
202 assert isinstance(manager._counter, itertools.count)
203 assert isinstance(manager._event_queue, queue.PriorityQueue)
204 assert manager._gidle_id == 0
205 assert not manager._event_history
206
207 @pytest.mark.parametrize(
208 "case",
209 [
210 {
211 "id": "activate_manager",
212 "operation": "activate",
213 "initial_active": False,
214 "initial_counts": {},
215 "expected_active": True,
216 "watcher_method": "start_key_watcher",
217 },
218 {
219 "id": "deactivate_manager",
220 "operation": "deactivate",
221 "initial_active": True,
222 "initial_counts": {"test": 1},
223 "expected_active": False,
224 "watcher_method": "stop_key_watcher",
225 },
226 ],
227 ids=lambda case: case["id"],
228 )
229 def test_activation_operations(
230 self,
231 test_context: OrcaTestContext,
232 case: dict,
233 ) -> None:
234 """Test EventManager activate/deactivate operations."""
235 self._setup_dependencies(test_context)
236 from orca.event_manager import EventManager
237
238 mock_get_input_mgr = test_context.Mock()
239 test_context.patch(
240 "orca.event_manager.input_event_manager.get_manager",
241 new=mock_get_input_mgr,
242 )
243 mock_input_mgr = test_context.Mock()
244 mock_get_input_mgr.return_value = mock_input_mgr
245 manager = EventManager()
246 manager._active = case["initial_active"]
247 manager._script_listener_counts = case["initial_counts"].copy()
248
249 getattr(manager, case["operation"])()
250 assert manager._active is case["expected_active"]
251
252 if case["operation"] == "deactivate":
253 assert not manager._script_listener_counts
254
255 getattr(mock_input_mgr, case["watcher_method"]).assert_called_once()
256
257 getattr(mock_input_mgr, case["watcher_method"]).reset_mock()
258 getattr(manager, case["operation"])()
259 getattr(mock_input_mgr, case["watcher_method"]).assert_not_called()
260
261 @pytest.mark.parametrize(
262 "case",
263 [
264 {"id": "pause_only", "pause": True, "clear_queue": False, "reason": ""},
265 {
266 "id": "unpause_with_reason",
267 "pause": False,
268 "clear_queue": False,
269 "reason": "test reason",
270 },
271 {
272 "id": "pause_and_clear",
273 "pause": True,
274 "clear_queue": True,
275 "reason": "clear and pause",
276 },
277 ],
278 ids=lambda case: case["id"],
279 )
280 def test_pause_queuing(self, test_context, case: dict) -> None:
281 """Test EventManager.pause_queuing."""
282
283 self._setup_dependencies(test_context)
284 from orca.event_manager import EventManager
285
286 manager = EventManager()
287 original_queue = manager._event_queue
288 manager.pause_queuing(case["pause"], case["clear_queue"], case["reason"])
289 assert manager._paused == case["pause"]
290 if case["clear_queue"]:
291 assert manager._event_queue is not original_queue
292 else:
293 assert manager._event_queue is original_queue
294
295 @pytest.mark.parametrize(
296 "case",
297 [
298 {"id": "window_event", "event_type": "window:activate", "expected_priority": 2},
299 {
300 "id": "focus_changed",
301 "event_type": "object:state-changed:focused",
302 "expected_priority": 3,
303 },
304 {
305 "id": "active_descendant",
306 "event_type": "object:active-descendant-changed",
307 "expected_priority": 3,
308 },
309 {
310 "id": "announcement_normal",
311 "event_type": "object:announcement",
312 "expected_priority": 4,
313 },
314 {
315 "id": "invalid_entry",
316 "event_type": "object:state-changed:invalid-entry",
317 "expected_priority": 5,
318 },
319 {
320 "id": "children_changed",
321 "event_type": "object:children-changed:add",
322 "expected_priority": 6,
323 },
324 {
325 "id": "other_event",
326 "event_type": "object:text-changed:insert",
327 "expected_priority": 4,
328 },
329 ],
330 ids=lambda case: case["id"],
331 )
332 def test_get_priority(self, test_context, case: dict) -> None:
333 """Test EventManager._get_priority."""
334
335 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
336 from orca.event_manager import EventManager
337
338 manager = EventManager()
339 mock_event = test_context.Mock(spec=Atspi.Event)
340 mock_event.type = case["event_type"]
341 mock_event.source = test_context.Mock()
342 mock_event.detail1 = 0
343 ax_utilities = essential_modules["orca.ax_utilities"]
344 if case["event_type"] == "object:state-changed:active":
345 ax_utilities.is_frame.return_value = True
346 elif case["event_type"] == "object:announcement":
347 mock_event.detail1 = Atspi.Live.POLITE if case["expected_priority"] == 3 else 0
348 priority = manager._get_priority(mock_event)
349 assert priority == case["expected_priority"]
350
351 def test_get_priority_announcement_levels(self, test_context: OrcaTestContext) -> None:
352 """Test EventManager._get_priority for announcement event levels."""
353
354 self._setup_dependencies(test_context)
355 from orca.event_manager import EventManager
356
357 manager = EventManager()
358 mock_event = test_context.Mock(spec=Atspi.Event)
359 mock_event.type = "object:announcement"
360 mock_event.source = test_context.Mock()
361
362 mock_event.detail1 = Atspi.Live.ASSERTIVE
363 priority = manager._get_priority(mock_event)
364 assert priority == 2 # PRIORITY_IMPORTANT
365
366 mock_event.detail1 = Atspi.Live.POLITE
367 priority = manager._get_priority(mock_event)
368 assert priority == 3 # PRIORITY_HIGH
369
370 mock_event.detail1 = 999
371 priority = manager._get_priority(mock_event)
372 assert priority == 4 # PRIORITY_NORMAL
373
374 @pytest.mark.parametrize(
375 "case",
376 [
377 {
378 "id": "duplicate_events",
379 "new_type": "object:text-changed:insert",
380 "new_detail1": 5,
381 "new_detail2": 10,
382 "new_data": "test",
383 "existing_type": "object:text-changed:insert",
384 "existing_detail1": 5,
385 "existing_detail2": 10,
386 "existing_data": "test",
387 "priority": 4,
388 "should_obsolete": True,
389 },
390 {
391 "id": "window_events",
392 "new_type": "window:activate",
393 "new_detail1": 0,
394 "new_detail2": 0,
395 "new_data": None,
396 "existing_type": "window:deactivate",
397 "existing_detail1": 0,
398 "existing_detail2": 0,
399 "existing_data": None,
400 "priority": 2,
401 "should_obsolete": True,
402 },
403 {
404 "id": "no_obsolescence",
405 "new_type": "object:text-changed:insert",
406 "new_detail1": 0,
407 "new_detail2": 0,
408 "new_data": None,
409 "existing_type": None,
410 "existing_detail1": None,
411 "existing_detail2": None,
412 "existing_data": None,
413 "priority": None,
414 "should_obsolete": False,
415 },
416 ],
417 ids=lambda case: case["id"],
418 )
419 def test_is_obsoleted_by_scenarios(
420 self,
421 test_context: OrcaTestContext,
422 case: dict,
423 ) -> None:
424 """Test EventManager._is_obsoleted_by for various obsolescence scenarios."""
425 self._setup_dependencies(test_context)
426 from orca.event_manager import EventManager
427
428 manager = EventManager()
429 mock_event = test_context.Mock(spec=Atspi.Event)
430 mock_event.type = case["new_type"]
431 mock_event.source = test_context.Mock()
432 mock_event.detail1 = case["new_detail1"]
433 mock_event.detail2 = case["new_detail2"]
434 mock_event.any_data = case["new_data"]
435
436 if case["existing_type"] is not None and case["priority"] is not None:
437 existing_event = test_context.Mock(spec=Atspi.Event)
438 existing_event.type = case["existing_type"]
439 existing_event.source = mock_event.source
440 existing_event.detail1 = case["existing_detail1"]
441 existing_event.detail2 = case["existing_detail2"]
442 existing_event.any_data = case["existing_data"]
443 manager._event_queue.put((case["priority"], 1, existing_event))
444
445 result = manager._is_obsoleted_by(mock_event)
446 if case["should_obsolete"]:
447 assert result is not None
448 else:
449 assert result is None
450
451 @pytest.mark.parametrize(
452 "case",
453 [
454 {"id": "inactive_manager", "active": False, "paused": False, "expected": True},
455 {"id": "paused_manager", "active": True, "paused": True, "expected": True},
456 ],
457 ids=lambda case: case["id"],
458 )
459 def test_ignore_inactive_or_paused_manager(
460 self,
461 test_context: OrcaTestContext,
462 case: dict,
463 ) -> None:
464 """Test EventManager._ignore when manager is inactive or paused."""
465 self._setup_dependencies(test_context)
466 from orca.event_manager import EventManager
467
468 manager = EventManager()
469 mock_event = test_context.Mock(spec=Atspi.Event)
470 mock_event.type = "object:text-changed:insert"
471
472 manager._active = case["active"]
473 manager._paused = case["paused"]
474 assert manager._ignore(mock_event) is case["expected"]
475
476 @pytest.mark.parametrize(
477 "case",
478 [
479 {
480 "id": "window_activate_event",
481 "event_type": "window:activate",
482 "event_config": {},
483 "expected_result": False,
484 },
485 {
486 "id": "mouse_button_event",
487 "event_type": "mouse:button:1p",
488 "event_config": {},
489 "expected_result": False,
490 },
491 {
492 "id": "focused_window_event",
493 "event_type": "object:state-changed:focused",
494 "event_config": {"is_window": True},
495 "expected_result": True,
496 },
497 ],
498 ids=lambda case: case["id"],
499 )
500 def test_ignore_window_and_mouse_events(
501 self,
502 test_context: OrcaTestContext,
503 case: dict,
504 ) -> None:
505 """Test EventManager._ignore for window, mouse, and focused window events."""
506 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
507 from orca.event_manager import EventManager
508
509 manager = EventManager()
510 manager._active = True
511 manager._paused = False
512 mock_event = test_context.Mock(spec=Atspi.Event)
513 mock_event.type = case["event_type"]
514 mock_event.source = test_context.Mock()
515
516 if "is_window" in case["event_config"]:
517 ax_utilities = essential_modules["orca.ax_utilities"]
518 ax_utilities.is_window.return_value = case["event_config"]["is_window"]
519
520 assert manager._ignore(mock_event) is case["expected_result"]
521
522 def test_ignore_frame_events(self, test_context: OrcaTestContext) -> None:
523 """Test EventManager._ignore for frame events."""
524
525 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
526 from orca.event_manager import EventManager
527
528 manager = EventManager()
529 manager._active = True
530 manager._paused = False
531 mock_event = test_context.Mock(spec=Atspi.Event)
532 mock_event.type = "object:test-event"
533 mock_event.source = test_context.Mock()
534 mock_event.any_data = test_context.Mock()
535 mock_event.detail1 = 0
536 mock_event.detail2 = 0
537 ax_utilities = essential_modules["orca.ax_utilities"]
538 focus_mgr = essential_modules["focus_manager_instance"]
539
540 focus_mgr.get_locus_of_focus.return_value = None
541 ax_utilities.is_window.return_value = False
542 test_context.patch("time.time", return_value=1000.0)
543 manager._event_history = {}
544
545 mock_app = test_context.Mock()
546 test_context.patch("orca.event_manager.AXUtilities.is_window", return_value=False)
547 test_context.patch("orca.event_manager.AXUtilities.is_frame", return_value=True)
548 test_context.patch("orca.event_manager.AXUtilities.get_application", return_value=mock_app)
549 test_context.patch("orca.event_manager.AXObject.get_name", return_value="mutter-x11-frames")
550 assert manager._ignore(mock_event) is True
551
552 regular_app = test_context.Mock()
553 test_context.patch("orca.event_manager.AXUtilities.is_window", return_value=False)
554 test_context.patch("orca.event_manager.AXUtilities.is_frame", return_value=True)
555 test_context.patch(
556 "orca.event_manager.AXUtilities.get_application",
557 return_value=regular_app,
558 )
559 test_context.patch("orca.event_manager.AXObject.get_name", return_value="regular-app")
560 assert manager._ignore(mock_event) is False
561
562 def test_ignore_text_events(self, test_context: OrcaTestContext) -> None:
563 """Test EventManager._ignore for text-related events."""
564
565 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
566 from orca.event_manager import EventManager
567
568 manager = EventManager()
569 manager._active = True
570 manager._paused = False
571 mock_event = test_context.Mock(spec=Atspi.Event)
572 mock_event.source = test_context.Mock()
573 mock_event.detail2 = 5001 # Large text insertion
574 mock_event.any_data = test_context.Mock()
575 mock_event.detail1 = 0
576 ax_utilities = essential_modules["orca.ax_utilities"]
577 focus_mgr = essential_modules["focus_manager_instance"]
578 focus_mgr.get_locus_of_focus.return_value = None
579 ax_utilities.is_window.return_value = False
580 ax_utilities.is_frame.return_value = False
581 ax_utilities.is_text.return_value = True # This triggers text logic
582 ax_utilities.is_notification.return_value = False
583 ax_utilities.is_alert.return_value = False
584 ax_utilities.is_selected.return_value = False
585 ax_utilities.is_focused.return_value = False
586 ax_utilities.is_section.return_value = False
587
588 self._setup_ignore_event_ax_utilities_mocks(test_context, mock_time=5000.0, is_text=True)
589 manager._event_history = {}
590
591 mock_event.type = "object:text-changed:insert"
592 result = manager._ignore(mock_event)
593 assert result is True, f"Expected True but got {result}"
594
595 mock_event.detail2 = 100
596 assert manager._ignore(mock_event) is False
597
598 mock_event.type = "object:text-changed:delete"
599 assert manager._ignore(mock_event) is False
600
601 @pytest.mark.parametrize(
602 "case",
603 [
604 {
605 "id": "notification_event",
606 "utility_method": "is_notification",
607 "utility_value": True,
608 "expected_result": False,
609 },
610 {
611 "id": "alert_event",
612 "utility_method": "is_alert",
613 "utility_value": True,
614 "expected_result": False,
615 },
616 ],
617 ids=lambda case: case["id"],
618 )
619 def test_ignore_notification_and_alert_events(
620 self,
621 test_context: OrcaTestContext,
622 case: dict,
623 ) -> None:
624 """Test EventManager._ignore for notification and alert events."""
625 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
626 from orca.event_manager import EventManager
627
628 manager = EventManager()
629 manager._active = True
630 manager._paused = False
631 mock_event = test_context.Mock(spec=Atspi.Event)
632 mock_event.type = "object:test-event"
633 mock_event.source = test_context.Mock()
634 ax_utilities = essential_modules["orca.ax_utilities"]
635
636 ax_utilities.is_notification.return_value = False
637 ax_utilities.is_alert.return_value = False
638 getattr(ax_utilities, case["utility_method"]).return_value = case["utility_value"]
639
640 assert manager._ignore(mock_event) is case["expected_result"]
641
642 def test_ignore_focus_and_selection_events(self, test_context: OrcaTestContext) -> None:
643 """Test EventManager._ignore for focus and selection events."""
644
645 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
646 from orca.event_manager import EventManager
647
648 manager = EventManager()
649 manager._active = True
650 manager._paused = False
651 mock_event = test_context.Mock(spec=Atspi.Event)
652 mock_event.type = "object:test-event"
653 mock_event.source = test_context.Mock()
654 mock_event.any_data = test_context.Mock()
655 ax_utilities = essential_modules["orca.ax_utilities"]
656 focus_mgr = essential_modules["focus_manager_instance"]
657
658 focus_mgr.get_locus_of_focus.return_value = mock_event.source
659 assert manager._ignore(mock_event) is False
660
661 focus_mgr.get_locus_of_focus.return_value = mock_event.any_data
662 assert manager._ignore(mock_event) is False
663
664 focus_mgr.get_locus_of_focus.return_value = test_context.Mock()
665 ax_utilities.is_selected.return_value = True
666 assert manager._ignore(mock_event) is False
667
668 def test_ignore_focused_source_events(self, test_context: OrcaTestContext) -> None:
669 """Test EventManager._ignore for events from focused sources."""
670
671 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
672 from orca.event_manager import EventManager
673
674 manager = EventManager()
675 manager._active = True
676 manager._paused = False
677 mock_event = test_context.Mock(spec=Atspi.Event)
678 mock_event.source = test_context.Mock()
679 mock_event.detail1 = 1
680 mock_event.detail2 = 0
681 mock_event.any_data = test_context.Mock()
682 focus_mgr = essential_modules["focus_manager_instance"]
683
684 focus_mgr.get_locus_of_focus.return_value = None
685 test_context.patch("time.time", return_value=3000.0)
686 manager._event_history = {}
687
688 mock_event.type = "object:test-event"
689 test_context.patch("orca.event_manager.AXUtilities.is_window", return_value=False)
690 test_context.patch("orca.event_manager.AXUtilities.is_frame", return_value=False)
691 test_context.patch("orca.event_manager.AXUtilities.is_text", return_value=False)
692 test_context.patch("orca.event_manager.AXUtilities.is_notification", return_value=False)
693 test_context.patch("orca.event_manager.AXUtilities.is_alert", return_value=False)
694 test_context.patch("orca.event_manager.AXUtilities.is_selected", return_value=False)
695 test_context.patch("orca.event_manager.AXUtilities.is_focused", return_value=True)
696 test_context.patch("orca.event_manager.AXUtilities.manages_descendants", return_value=False)
697 assert manager._ignore(mock_event) is False
698
699 # - should not be ignored
700 mock_event.type = "object:state-changed:focused"
701 mock_event.detail1 = 1
702 test_context.patch("orca.event_manager.AXUtilities.is_window", return_value=False)
703 test_context.patch("orca.event_manager.AXUtilities.is_frame", return_value=False)
704 test_context.patch("orca.event_manager.AXUtilities.is_text", return_value=False)
705 test_context.patch("orca.event_manager.AXUtilities.is_notification", return_value=False)
706 test_context.patch("orca.event_manager.AXUtilities.is_alert", return_value=False)
707 test_context.patch("orca.event_manager.AXUtilities.is_selected", return_value=False)
708 test_context.patch("orca.event_manager.AXUtilities.is_focused", return_value=True)
709 test_context.patch("orca.event_manager.AXUtilities.manages_descendants", return_value=True)
710 assert manager._ignore(mock_event) is False
711
712 def test_ignore_live_region_events(self, test_context: OrcaTestContext) -> None:
713 """Test EventManager._ignore for live region events."""
714
715 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
716 from orca.event_manager import EventManager
717
718 manager = EventManager()
719 manager._active = True
720 manager._paused = False
721 mock_event = test_context.Mock(spec=Atspi.Event)
722 mock_event.type = "object:text-changed:insert"
723 mock_event.source = test_context.Mock()
724 mock_event.detail2 = 100
725 ax_utilities = essential_modules["orca.ax_utilities"]
726 ax_object = essential_modules["orca.ax_object"]
727 focus_mgr = essential_modules["focus_manager_instance"]
728
729 focus_mgr.get_locus_of_focus.return_value = test_context.Mock()
730 ax_utilities.is_selected.return_value = False
731 ax_utilities.is_focused.return_value = False
732 ax_utilities.is_section.return_value = True
733
734 ax_object.get_attribute.return_value = "polite"
735 assert manager._ignore(mock_event) is False
736
737 ax_object.get_attribute.return_value = "off"
738 mock_app = test_context.Mock()
739 ax_utilities.get_application.return_value = mock_app
740 manager._event_history = {}
741 assert manager._ignore(mock_event) is False
742
743 def test_ignore_spam_filtering(self, test_context: OrcaTestContext) -> None:
744 """Test EventManager._ignore spam filtering mechanism."""
745
746 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
747 from orca.event_manager import EventManager
748
749 manager = EventManager()
750 manager._active = True
751 manager._paused = False
752 mock_event = test_context.Mock(spec=Atspi.Event)
753 mock_event.type = "object:test-event"
754 mock_event.source = test_context.Mock()
755 mock_event.detail1 = 0
756 mock_event.detail2 = 0
757 mock_event.any_data = test_context.Mock()
758 mock_app = test_context.Mock()
759 original_hash = hash
760
761 def mock_hash(obj):
762 if obj is mock_app:
763 return 12345
764 return original_hash(obj)
765
766 test_context.patch("builtins.hash", new=mock_hash)
767 focus_mgr = essential_modules["focus_manager_instance"]
768 focus_mgr.get_locus_of_focus.return_value = None
769
770 manager._event_history = {}
771 test_context.patch("time.time", return_value=100.0)
772 test_context.patch("orca.event_manager.AXUtilities.is_window", return_value=False)
773 test_context.patch("orca.event_manager.AXUtilities.is_frame", return_value=False)
774 test_context.patch("orca.event_manager.AXUtilities.is_text", return_value=False)
775 test_context.patch("orca.event_manager.AXUtilities.is_notification", return_value=False)
776 test_context.patch("orca.event_manager.AXUtilities.is_alert", return_value=False)
777 test_context.patch("orca.event_manager.AXUtilities.is_selected", return_value=False)
778 test_context.patch("orca.event_manager.AXUtilities.is_focused", return_value=False)
779 test_context.patch("orca.event_manager.AXUtilities.is_section", return_value=False)
780 test_context.patch("orca.event_manager.AXUtilities.manages_descendants", return_value=False)
781 test_context.patch("orca.event_manager.AXUtilities.get_application", return_value=mock_app)
782 test_context.patch("orca.event_manager.AXObject.get_name", return_value="regular-app")
783 test_context.patch("orca.event_manager.AXObject.get_attribute", return_value=None)
784 test_context.patch("orca.event_manager.focus_manager.get_manager", return_value=focus_mgr)
785 assert manager._ignore(mock_event) is False
786
787 test_context.patch("time.time", return_value=100.05)
788 test_context.patch("orca.event_manager.AXUtilities.is_window", return_value=False)
789 test_context.patch("orca.event_manager.AXUtilities.is_frame", return_value=False)
790 test_context.patch("orca.event_manager.AXUtilities.is_text", return_value=False)
791 test_context.patch("orca.event_manager.AXUtilities.is_notification", return_value=False)
792 test_context.patch("orca.event_manager.AXUtilities.is_alert", return_value=False)
793 test_context.patch("orca.event_manager.AXUtilities.is_selected", return_value=False)
794 test_context.patch("orca.event_manager.AXUtilities.is_focused", return_value=False)
795 test_context.patch("orca.event_manager.AXUtilities.is_section", return_value=False)
796 test_context.patch("orca.event_manager.AXUtilities.manages_descendants", return_value=False)
797 test_context.patch("orca.event_manager.AXUtilities.get_application", return_value=mock_app)
798 test_context.patch("orca.event_manager.AXObject.get_name", return_value="regular-app")
799 test_context.patch("orca.event_manager.AXObject.get_attribute", return_value=None)
800 test_context.patch("orca.event_manager.focus_manager.get_manager", return_value=focus_mgr)
801 assert manager._ignore(mock_event) is True
802
803 test_context.patch("time.time", return_value=100.2)
804 test_context.patch("orca.event_manager.AXUtilities.is_window", return_value=False)
805 test_context.patch("orca.event_manager.AXUtilities.is_frame", return_value=False)
806 test_context.patch("orca.event_manager.AXUtilities.is_text", return_value=False)
807 test_context.patch("orca.event_manager.AXUtilities.is_notification", return_value=False)
808 test_context.patch("orca.event_manager.AXUtilities.is_alert", return_value=False)
809 test_context.patch("orca.event_manager.AXUtilities.is_selected", return_value=False)
810 test_context.patch("orca.event_manager.AXUtilities.is_focused", return_value=False)
811 test_context.patch("orca.event_manager.AXUtilities.is_section", return_value=False)
812 test_context.patch("orca.event_manager.AXUtilities.manages_descendants", return_value=False)
813 test_context.patch("orca.event_manager.AXUtilities.get_application", return_value=mock_app)
814 test_context.patch("orca.event_manager.AXObject.get_name", return_value="regular-app")
815 test_context.patch("orca.event_manager.AXObject.get_attribute", return_value=None)
816 test_context.patch("orca.event_manager.focus_manager.get_manager", return_value=focus_mgr)
817 assert manager._ignore(mock_event) is False
818
819 def test_ignore_mutter_events(self, test_context: OrcaTestContext) -> None:
820 """Test EventManager._ignore for mutter-x11-frames events."""
821
822 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
823 from orca.event_manager import EventManager
824
825 manager = EventManager()
826 manager._active = True
827 manager._paused = False
828 mock_event = test_context.Mock(spec=Atspi.Event)
829 mock_event.type = "object:test-event"
830 mock_event.source = test_context.Mock()
831 mock_event.detail1 = 0
832 mock_event.detail2 = 0
833 mock_event.any_data = test_context.Mock()
834 mock_app = test_context.Mock()
835 original_hash = hash
836
837 def mock_hash(obj):
838 if obj is mock_app:
839 return 12345
840 return original_hash(obj)
841
842 test_context.patch("builtins.hash", new=mock_hash)
843 focus_mgr = essential_modules["focus_manager_instance"]
844 focus_mgr.get_locus_of_focus.return_value = None
845
846 test_context.patch("time.time", return_value=100.0)
847 test_context.patch("orca.event_manager.AXUtilities.is_window", return_value=False)
848 test_context.patch("orca.event_manager.AXUtilities.is_frame", return_value=False)
849 test_context.patch("orca.event_manager.AXUtilities.is_text", return_value=False)
850 test_context.patch("orca.event_manager.AXUtilities.is_notification", return_value=False)
851 test_context.patch("orca.event_manager.AXUtilities.is_alert", return_value=False)
852 test_context.patch("orca.event_manager.AXUtilities.is_selected", return_value=False)
853 test_context.patch("orca.event_manager.AXUtilities.is_focused", return_value=False)
854 test_context.patch("orca.event_manager.AXUtilities.is_section", return_value=False)
855 test_context.patch("orca.event_manager.AXUtilities.manages_descendants", return_value=False)
856 test_context.patch("orca.event_manager.AXUtilities.get_application", return_value=mock_app)
857 test_context.patch("orca.event_manager.AXObject.get_name", return_value="mutter-x11-frames")
858 test_context.patch("orca.event_manager.AXObject.get_attribute", return_value=None)
859 test_context.patch("orca.event_manager.focus_manager.get_manager", return_value=focus_mgr)
860 manager._event_history = {}
861 assert manager._ignore(mock_event) is True
862
863 @pytest.mark.parametrize(
864 "case",
865 [
866 {
867 "id": "window_event",
868 "event_type": "window:activate",
869 "is_frame": False,
870 "is_dialog_or_alert": False,
871 "detail1": 0,
872 "expected_priority": 2,
873 },
874 {
875 "id": "active_frame",
876 "event_type": "object:state-changed:active",
877 "is_frame": True,
878 "is_dialog_or_alert": False,
879 "detail1": 0,
880 "expected_priority": 2,
881 },
882 {
883 "id": "active_dialog",
884 "event_type": "object:state-changed:active",
885 "is_frame": False,
886 "is_dialog_or_alert": True,
887 "detail1": 0,
888 "expected_priority": 2,
889 },
890 {
891 "id": "focused_event",
892 "event_type": "object:state-changed:focused",
893 "is_frame": False,
894 "is_dialog_or_alert": False,
895 "detail1": 0,
896 "expected_priority": 3,
897 },
898 {
899 "id": "active_descendant",
900 "event_type": "object:active-descendant-changed",
901 "is_frame": False,
902 "is_dialog_or_alert": False,
903 "detail1": 0,
904 "expected_priority": 3,
905 },
906 {
907 "id": "assertive_announcement",
908 "event_type": "object:announcement",
909 "is_frame": False,
910 "is_dialog_or_alert": False,
911 "detail1": 2,
912 "expected_priority": 2,
913 },
914 {
915 "id": "polite_announcement",
916 "event_type": "object:announcement",
917 "is_frame": False,
918 "is_dialog_or_alert": False,
919 "detail1": 1,
920 "expected_priority": 3,
921 },
922 {
923 "id": "other_announcement",
924 "event_type": "object:announcement",
925 "is_frame": False,
926 "is_dialog_or_alert": False,
927 "detail1": 3,
928 "expected_priority": 4,
929 },
930 {
931 "id": "invalid_entry",
932 "event_type": "object:state-changed:invalid-entry",
933 "is_frame": False,
934 "is_dialog_or_alert": False,
935 "detail1": 0,
936 "expected_priority": 5,
937 },
938 {
939 "id": "children_changed",
940 "event_type": "object:children-changed:add",
941 "is_frame": False,
942 "is_dialog_or_alert": False,
943 "detail1": 0,
944 "expected_priority": 6,
945 },
946 {
947 "id": "default_normal",
948 "event_type": "object:text-changed:insert",
949 "is_frame": False,
950 "is_dialog_or_alert": False,
951 "detail1": 0,
952 "expected_priority": 4,
953 },
954 ],
955 ids=lambda case: case["id"],
956 )
957 def test_get_priority_various_event_types(
958 self,
959 test_context: OrcaTestContext,
960 case: dict,
961 ) -> None:
962 """Test EventManager._get_priority with various event types and conditions."""
963
964 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
965 from orca.event_manager import EventManager
966
967 manager = EventManager()
968 mock_event = test_context.Mock(spec=Atspi.Event)
969 mock_event.type = case["event_type"]
970 mock_event.detail1 = case["detail1"]
971 mock_event.source = test_context.Mock()
972
973 test_context.patch("orca.event_manager.AXUtilities.is_frame", return_value=case["is_frame"])
974 test_context.patch(
975 "orca.event_manager.AXUtilities.is_dialog_or_alert",
976 return_value=case["is_dialog_or_alert"],
977 )
978
979 result = manager._get_priority(mock_event)
980 assert result == case["expected_priority"]
981 essential_modules["orca.debug"].print_tokens.assert_called()
982
983 @pytest.mark.parametrize(
984 "case",
985 [
986 {
987 "id": "notification_not_ignored",
988 "source_role": "notification",
989 "event_type": "object:text-changed:insert",
990 "source_is_focused": False,
991 "manages_descendants": False,
992 "detail1": 0,
993 "expected_ignore": False,
994 },
995 {
996 "id": "alert_not_ignored",
997 "source_role": "alert",
998 "event_type": "object:text-changed:insert",
999 "source_is_focused": False,
1000 "manages_descendants": False,
1001 "detail1": 0,
1002 "expected_ignore": False,
1003 },
1004 {
1005 "id": "large_text_insertion_ignored",
1006 "source_role": "text",
1007 "event_type": "object:text-changed:insert",
1008 "source_is_focused": False,
1009 "manages_descendants": False,
1010 "detail1": 5001,
1011 "expected_ignore": True,
1012 },
1013 {
1014 "id": "small_text_insertion_not_ignored",
1015 "source_role": "text",
1016 "event_type": "object:text-changed:insert",
1017 "source_is_focused": False,
1018 "manages_descendants": False,
1019 "detail1": 4999,
1020 "expected_ignore": False,
1021 },
1022 {
1023 "id": "focused_source_not_ignored",
1024 "source_role": "push_button",
1025 "event_type": "object:state-changed:focused",
1026 "source_is_focused": True,
1027 "manages_descendants": False,
1028 "detail1": 0,
1029 "expected_ignore": False,
1030 },
1031 {
1032 "id": "focused_managing_descendants_with_detail1",
1033 "source_role": "push_button",
1034 "event_type": "object:state-changed:focused",
1035 "source_is_focused": True,
1036 "manages_descendants": True,
1037 "detail1": 1,
1038 "expected_ignore": False,
1039 },
1040 {
1041 "id": "window_focused_event_ignored",
1042 "source_role": "window",
1043 "event_type": "object:state-changed:focused",
1044 "source_is_focused": False,
1045 "manages_descendants": False,
1046 "detail1": 0,
1047 "expected_ignore": True,
1048 },
1049 ],
1050 ids=lambda case: case["id"],
1051 )
1052 def test_ignore_event_conditions(
1053 self,
1054 test_context: OrcaTestContext,
1055 case: dict,
1056 ) -> None:
1057 """Test EventManager._ignore with various event conditions."""
1058
1059 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1060 from orca.event_manager import EventManager
1061
1062 manager = EventManager()
1063 manager._active = True
1064 manager._paused = False
1065 manager._event_history = {}
1066
1067 mock_event = test_context.Mock(spec=Atspi.Event)
1068 mock_event.type = case["event_type"]
1069 mock_event.detail1 = case["detail1"]
1070 mock_event.detail2 = (
1071 0 if case["event_type"] != "object:text-changed:insert" else case["detail1"]
1072 )
1073 mock_event.source = test_context.Mock()
1074 mock_event.any_data = test_context.Mock()
1075
1076 mock_app = test_context.Mock()
1077 focus_mgr = essential_modules["focus_manager_instance"]
1078 focus_mgr.get_locus_of_focus.return_value = None
1079
1080 test_context.patch("time.time", return_value=100.0)
1081 test_context.patch(
1082 "orca.event_manager.AXUtilities.is_window",
1083 side_effect=lambda obj: (
1084 case["source_role"] == "window" if obj == mock_event.source else False
1085 ),
1086 )
1087 test_context.patch("orca.event_manager.AXUtilities.is_frame", return_value=False)
1088 test_context.patch(
1089 "orca.event_manager.AXUtilities.is_text",
1090 side_effect=lambda obj: (
1091 case["source_role"] == "text" if obj == mock_event.source else False
1092 ),
1093 )
1094 test_context.patch(
1095 "orca.event_manager.AXUtilities.is_notification",
1096 side_effect=lambda obj: case["source_role"] == "notification"
1097 if obj == mock_event.source
1098 else False,
1099 )
1100 test_context.patch(
1101 "orca.event_manager.AXUtilities.is_alert",
1102 side_effect=lambda obj: (
1103 case["source_role"] == "alert" if obj == mock_event.source else False
1104 ),
1105 )
1106 test_context.patch("orca.event_manager.AXUtilities.is_selected", return_value=False)
1107 test_context.patch(
1108 "orca.event_manager.AXUtilities.is_focused",
1109 side_effect=lambda obj: (
1110 case["source_is_focused"] if obj == mock_event.source else False
1111 ),
1112 )
1113 test_context.patch("orca.event_manager.AXUtilities.is_section", return_value=False)
1114 test_context.patch(
1115 "orca.event_manager.AXUtilities.manages_descendants",
1116 side_effect=lambda obj: (
1117 case["manages_descendants"] if obj == mock_event.source else False
1118 ),
1119 )
1120 test_context.patch("orca.event_manager.AXUtilities.get_application", return_value=mock_app)
1121 test_context.patch("orca.event_manager.AXObject.get_name", return_value="regular-app")
1122
1123 result = manager._ignore(mock_event)
1124 assert result is case["expected_ignore"]
1125
1126 def test_ignore_live_region_text_insertion(self, test_context: OrcaTestContext) -> None:
1127 """Test EventManager._ignore for live region text insertions."""
1128
1129 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1130 from orca.event_manager import EventManager
1131
1132 manager = EventManager()
1133 manager._active = True
1134 manager._paused = False
1135 manager._event_history = {}
1136
1137 mock_event = test_context.Mock(spec=Atspi.Event)
1138 mock_event.type = "object:text-changed:insert"
1139 mock_event.detail1 = 0
1140 mock_event.detail2 = 100
1141 mock_event.source = test_context.Mock()
1142 mock_event.any_data = test_context.Mock()
1143
1144 mock_app = test_context.Mock()
1145 focus_mgr = essential_modules["focus_manager_instance"]
1146 focus_mgr.get_locus_of_focus.return_value = None
1147
1148 test_context.patch("time.time", return_value=100.0)
1149 test_context.patch("orca.event_manager.AXUtilities.is_window", return_value=False)
1150 test_context.patch("orca.event_manager.AXUtilities.is_frame", return_value=False)
1151 test_context.patch("orca.event_manager.AXUtilities.is_text", return_value=False)
1152 test_context.patch("orca.event_manager.AXUtilities.is_notification", return_value=False)
1153 test_context.patch("orca.event_manager.AXUtilities.is_alert", return_value=False)
1154 test_context.patch("orca.event_manager.AXUtilities.is_selected", return_value=False)
1155 test_context.patch("orca.event_manager.AXUtilities.is_focused", return_value=False)
1156 test_context.patch("orca.event_manager.AXUtilities.is_section", return_value=True)
1157 test_context.patch("orca.event_manager.AXUtilities.manages_descendants", return_value=False)
1158 test_context.patch("orca.event_manager.AXUtilities.get_application", return_value=mock_app)
1159 test_context.patch("orca.event_manager.AXObject.get_name", return_value="regular-app")
1160 test_context.patch(
1161 "orca.event_manager.AXObject.get_attribute",
1162 side_effect=lambda obj, attr: "polite" if attr == "live" else None,
1163 )
1164
1165 result = manager._ignore(mock_event)
1166 assert result is False
1167
1168 def test_ignore_event_history_filtering(self, test_context: OrcaTestContext) -> None:
1169 """Test EventManager._ignore with event history spam filtering."""
1170
1171 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1172 from orca.event_manager import EventManager
1173
1174 manager = EventManager()
1175 manager._active = True
1176 manager._paused = False
1177 manager._event_history = {}
1178
1179 mock_event = test_context.Mock(spec=Atspi.Event)
1180 mock_event.type = "object:test-event"
1181 mock_event.detail1 = 0
1182 mock_event.detail2 = 0
1183 mock_event.source = test_context.Mock()
1184 mock_event.any_data = test_context.Mock()
1185
1186 mock_app = test_context.Mock()
1187 focus_mgr = essential_modules["focus_manager_instance"]
1188 focus_mgr.get_locus_of_focus.return_value = None
1189
1190 original_hash = hash
1191
1192 def mock_hash(obj):
1193 if obj is mock_app:
1194 return 12345
1195 return original_hash(obj)
1196
1197 test_context.patch("builtins.hash", new=mock_hash)
1198 test_context.patch("time.time", return_value=100.0)
1199 test_context.patch("orca.event_manager.AXUtilities.is_window", return_value=False)
1200 test_context.patch("orca.event_manager.AXUtilities.is_frame", return_value=False)
1201 test_context.patch("orca.event_manager.AXUtilities.is_text", return_value=False)
1202 test_context.patch("orca.event_manager.AXUtilities.is_notification", return_value=False)
1203 test_context.patch("orca.event_manager.AXUtilities.is_alert", return_value=False)
1204 test_context.patch("orca.event_manager.AXUtilities.is_selected", return_value=False)
1205 test_context.patch("orca.event_manager.AXUtilities.is_focused", return_value=False)
1206 test_context.patch("orca.event_manager.AXUtilities.is_section", return_value=False)
1207 test_context.patch("orca.event_manager.AXUtilities.manages_descendants", return_value=False)
1208 test_context.patch("orca.event_manager.AXUtilities.get_application", return_value=mock_app)
1209 test_context.patch("orca.event_manager.AXObject.get_name", return_value="regular-app")
1210 test_context.patch("orca.event_manager.AXObject.get_attribute", return_value=None)
1211
1212 # First call should not be ignored
1213 result1 = manager._ignore(mock_event)
1214 assert result1 is False
1215 assert manager._event_history["object:test-event"] == (12345, 100.0)
1216
1217 # Second call within 0.1s should be ignored
1218 test_context.patch("time.time", return_value=100.05)
1219 result2 = manager._ignore(mock_event)
1220 assert result2 is True
1221
1222 def test_is_obsoleted_by_identical_events(self, test_context: OrcaTestContext) -> None:
1223 """Test EventManager._is_obsoleted_by with identical events in queue."""
1224
1225 self._setup_dependencies(test_context)
1226 from orca.event_manager import EventManager
1227
1228 manager = EventManager()
1229 mock_event = test_context.Mock(spec=Atspi.Event)
1230 mock_event.type = "object:test-event"
1231 mock_event.source = test_context.Mock()
1232 mock_event.detail1 = 1
1233 mock_event.detail2 = 2
1234 mock_event.any_data = "test_data"
1235
1236 identical_event = test_context.Mock(spec=Atspi.Event)
1237 identical_event.type = "object:test-event"
1238 identical_event.source = mock_event.source
1239 identical_event.detail1 = 1
1240 identical_event.detail2 = 2
1241 identical_event.any_data = "test_data"
1242
1243 queue_data = [(4, 1, identical_event)]
1244 with manager._event_queue.mutex:
1245 manager._event_queue.queue = queue_data
1246
1247 result = manager._is_obsoleted_by(mock_event)
1248 assert result == identical_event
1249
1250 @pytest.mark.parametrize(
1251 "case",
1252 [
1253 {
1254 "id": "active_descendant_obsoletes",
1255 "existing_type": "object:active-descendant-changed",
1256 "existing_source_same": True,
1257 "new_type": "object:active-descendant-changed",
1258 "should_obsolete": True,
1259 },
1260 {
1261 "id": "state_changed_obsoletes",
1262 "existing_type": "object:state-changed:focused",
1263 "existing_source_same": True,
1264 "new_type": "object:state-changed:focused",
1265 "should_obsolete": True,
1266 },
1267 {
1268 "id": "caret_moved_obsoletes",
1269 "existing_type": "object:text-caret-moved",
1270 "existing_source_same": True,
1271 "new_type": "object:text-caret-moved",
1272 "should_obsolete": True,
1273 },
1274 {
1275 "id": "window_activate_obsoletes",
1276 "existing_type": "window:activate",
1277 "existing_source_same": True,
1278 "new_type": "window:activate",
1279 "should_obsolete": True,
1280 },
1281 {
1282 "id": "non_skippable_does_not_obsolete",
1283 "existing_type": "object:other-event",
1284 "existing_source_same": True,
1285 "new_type": "object:other-event",
1286 "should_obsolete": False,
1287 },
1288 ],
1289 ids=lambda case: case["id"],
1290 )
1291 def test_is_obsoleted_by_same_type_and_object(
1292 self,
1293 test_context: OrcaTestContext,
1294 case: dict,
1295 ) -> None:
1296 """Test EventManager._is_obsoleted_by with same type and object conditions."""
1297
1298 self._setup_dependencies(test_context)
1299 from orca.event_manager import EventManager
1300
1301 manager = EventManager()
1302 mock_source = test_context.Mock()
1303 mock_event = test_context.Mock(spec=Atspi.Event)
1304 mock_event.type = case["new_type"]
1305 mock_event.source = mock_source
1306 mock_event.detail1 = 0
1307 mock_event.detail2 = 0
1308 mock_event.any_data = None
1309
1310 existing_event = test_context.Mock(spec=Atspi.Event)
1311 existing_event.type = case["existing_type"]
1312 existing_event.source = mock_source if case["existing_source_same"] else test_context.Mock()
1313 existing_event.detail1 = (
1314 0 if case["should_obsolete"] else 1
1315 ) # Make it different for non-obsoleting case
1316 existing_event.detail2 = 0
1317 existing_event.any_data = None
1318
1319 queue_data = [(4, 1, existing_event)]
1320 with manager._event_queue.mutex:
1321 manager._event_queue.queue = queue_data
1322
1323 result = manager._is_obsoleted_by(mock_event)
1324 if case["should_obsolete"]:
1325 assert result == existing_event
1326 else:
1327 assert result is None
1328
1329 def test_is_obsoleted_by_sibling_events(self, test_context: OrcaTestContext) -> None:
1330 """Test EventManager._is_obsoleted_by with sibling event conditions."""
1331
1332 self._setup_dependencies(test_context)
1333 from orca.event_manager import EventManager
1334
1335 manager = EventManager()
1336 parent_mock = test_context.Mock()
1337 mock_source = test_context.Mock()
1338 sibling_source = test_context.Mock()
1339
1340 mock_event = test_context.Mock(spec=Atspi.Event)
1341 mock_event.type = "object:state-changed:focused"
1342 mock_event.source = mock_source
1343 mock_event.detail1 = 1
1344 mock_event.detail2 = 2
1345 mock_event.any_data = "test_data"
1346
1347 sibling_event = test_context.Mock(spec=Atspi.Event)
1348 sibling_event.type = "object:state-changed:focused"
1349 sibling_event.source = sibling_source
1350 sibling_event.detail1 = 1
1351 sibling_event.detail2 = 2
1352 sibling_event.any_data = "test_data"
1353
1354 test_context.patch(
1355 "orca.event_manager.AXObject.get_parent",
1356 side_effect=lambda obj: parent_mock
1357 if obj in [mock_source, sibling_source]
1358 else test_context.Mock(),
1359 )
1360
1361 queue_data = [(4, 1, sibling_event)]
1362 with manager._event_queue.mutex:
1363 manager._event_queue.queue = queue_data
1364
1365 result = manager._is_obsoleted_by(mock_event)
1366 assert result == sibling_event
1367
1368 def test_is_obsoleted_by_window_events(self, test_context: OrcaTestContext) -> None:
1369 """Test EventManager._is_obsoleted_by with window event conditions."""
1370
1371 self._setup_dependencies(test_context)
1372 from orca.event_manager import EventManager
1373
1374 manager = EventManager()
1375 mock_source = test_context.Mock()
1376
1377 mock_event = test_context.Mock(spec=Atspi.Event)
1378 mock_event.type = "window:activate"
1379 mock_event.source = mock_source
1380 mock_event.detail1 = 0
1381 mock_event.detail2 = 0
1382 mock_event.any_data = None
1383
1384 existing_event = test_context.Mock(spec=Atspi.Event)
1385 existing_event.type = "window:activate"
1386 existing_event.source = mock_source
1387 existing_event.detail1 = 0
1388 existing_event.detail2 = 0
1389 existing_event.any_data = None
1390
1391 queue_data = [(4, 1, existing_event)]
1392 with manager._event_queue.mutex:
1393 manager._event_queue.queue = queue_data
1394
1395 result = manager._is_obsoleted_by(mock_event)
1396 assert result == existing_event
1397
1398 def test_focus_conditions_in_ignore(self, test_context: OrcaTestContext) -> None:
1399 """Test EventManager._ignore focus-related conditions."""
1400
1401 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1402 from orca.event_manager import EventManager
1403
1404 manager = EventManager()
1405 manager._active = True
1406 manager._paused = False
1407 manager._event_history = {}
1408
1409 mock_event = test_context.Mock(spec=Atspi.Event)
1410 mock_event.type = "object:test-event"
1411 mock_event.detail1 = 0
1412 mock_event.detail2 = 0
1413 mock_event.source = test_context.Mock()
1414 mock_event.any_data = test_context.Mock()
1415
1416 focus_mgr = essential_modules["focus_manager_instance"]
1417
1418 focus_mgr.get_locus_of_focus.return_value = mock_event.source
1419 test_context.patch("orca.event_manager.AXUtilities.is_window", return_value=False)
1420 test_context.patch("orca.event_manager.AXUtilities.is_frame", return_value=False)
1421
1422 result = manager._ignore(mock_event)
1423 assert result is False
1424
1425 focus_mgr.get_locus_of_focus.return_value = mock_event.any_data
1426 result = manager._ignore(mock_event)
1427 assert result is False
1428
1429 def test_ignore_selected_source_not_ignored(self, test_context: OrcaTestContext) -> None:
1430 """Test EventManager._ignore does not ignore events from selected sources."""
1431
1432 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1433 from orca.event_manager import EventManager
1434
1435 manager = EventManager()
1436 manager._active = True
1437 manager._paused = False
1438 manager._event_history = {}
1439
1440 mock_event = test_context.Mock(spec=Atspi.Event)
1441 mock_event.type = "object:test-event"
1442 mock_event.detail1 = 0
1443 mock_event.detail2 = 0
1444 mock_event.source = test_context.Mock()
1445 mock_event.any_data = test_context.Mock()
1446
1447 mock_app = test_context.Mock()
1448 focus_mgr = essential_modules["focus_manager_instance"]
1449 focus_mgr.get_locus_of_focus.return_value = None
1450
1451 test_context.patch("time.time", return_value=100.0)
1452 test_context.patch("orca.event_manager.AXUtilities.is_window", return_value=False)
1453 test_context.patch("orca.event_manager.AXUtilities.is_frame", return_value=False)
1454 test_context.patch("orca.event_manager.AXUtilities.is_text", return_value=False)
1455 test_context.patch("orca.event_manager.AXUtilities.is_notification", return_value=False)
1456 test_context.patch("orca.event_manager.AXUtilities.is_alert", return_value=False)
1457 test_context.patch(
1458 "orca.event_manager.AXUtilities.is_selected",
1459 return_value=True, # Selected source should not be ignored
1460 )
1461 test_context.patch("orca.event_manager.AXUtilities.is_focused", return_value=False)
1462 test_context.patch("orca.event_manager.AXUtilities.is_section", return_value=False)
1463 test_context.patch("orca.event_manager.AXUtilities.manages_descendants", return_value=False)
1464 test_context.patch("orca.event_manager.AXUtilities.get_application", return_value=mock_app)
1465 test_context.patch("orca.event_manager.AXObject.get_name", return_value="regular-app")
1466 test_context.patch("orca.event_manager.AXObject.get_attribute", return_value=None)
1467
1468 result = manager._ignore(mock_event)
1469 assert result is False
1470
1471 def test_queue_println(self, test_context: OrcaTestContext) -> None:
1472 """Test EventManager._queue_println."""
1473
1474 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1475 from orca.event_manager import EventManager
1476
1477 manager = EventManager()
1478 mock_event = test_context.Mock(spec=Atspi.Event)
1479 mock_event.type = "test:event"
1480 debug_mock = essential_modules["orca.debug"]
1481 debug_mock.debugLevel = 10
1482 manager._queue_println(mock_event)
1483 manager._queue_println(mock_event, is_enqueue=False)
1484
1485 def test_enqueue_object_event_ignored(self, test_context: OrcaTestContext) -> None:
1486 """Test EventManager._enqueue_object_event for ignored events."""
1487
1488 self._setup_dependencies(test_context)
1489 from orca.event_manager import EventManager
1490
1491 manager = EventManager()
1492 manager._active = False
1493 mock_event = test_context.Mock(spec=Atspi.Event)
1494 mock_event.type = "object:text-changed:insert"
1495 manager._enqueue_object_event(mock_event)
1496 assert manager._event_queue.empty()
1497
1498 def test_on_no_focus(self, test_context: OrcaTestContext) -> None:
1499 """Test EventManager._on_no_focus."""
1500
1501 self._setup_dependencies(test_context)
1502 from orca.event_manager import EventManager
1503
1504 manager = EventManager()
1505 mock_get_focus_mgr = test_context.Mock()
1506 test_context.patch("orca.event_manager.focus_manager.get_manager", new=mock_get_focus_mgr)
1507 mock_get_script_mgr = test_context.Mock()
1508 test_context.patch("orca.event_manager.script_manager.get_manager", new=mock_get_script_mgr)
1509 mock_braille_presenter = test_context.Mock()
1510 mock_presenter_instance = test_context.Mock()
1511 mock_braille_presenter.get_presenter.return_value = mock_presenter_instance
1512 test_context.patch("orca.event_manager.braille_presenter", new=mock_braille_presenter)
1513 mock_focus_mgr = test_context.Mock()
1514 mock_script_mgr = test_context.Mock()
1515 mock_get_focus_mgr.return_value = mock_focus_mgr
1516 mock_get_script_mgr.return_value = mock_script_mgr
1517
1518 mock_focus_mgr.focus_and_window_are_unknown.return_value = False
1519 result = manager._on_no_focus()
1520 assert result is False
1521
1522 mock_focus_mgr.focus_and_window_are_unknown.return_value = True
1523 mock_script_mgr.get_active_script.return_value = None
1524 result = manager._on_no_focus()
1525 assert result is False
1526 mock_script_mgr.set_active_script.assert_called_once()
1527 mock_presenter_instance.disable_braille.assert_called_once()
1528
1529 def test_dequeue_object_event_empty_queue(self, test_context: OrcaTestContext) -> None:
1530 """Test EventManager._dequeue_object_event with empty queue."""
1531
1532 self._setup_dependencies(test_context)
1533 from orca.event_manager import EventManager
1534
1535 manager = EventManager()
1536 result = manager._dequeue_object_event()
1537 assert result is False
1538 assert manager._gidle_id == 0
1539
1540 @pytest.mark.parametrize(
1541 "case",
1542 [
1543 {
1544 "id": "first_registration",
1545 "operation": "register",
1546 "initial_count": 0,
1547 "expected_count": 1,
1548 "should_call_listener": True,
1549 },
1550 {
1551 "id": "duplicate_registration",
1552 "operation": "register",
1553 "initial_count": 1,
1554 "expected_count": 2,
1555 "should_call_listener": False,
1556 },
1557 {
1558 "id": "partial_deregistration",
1559 "operation": "deregister",
1560 "initial_count": 2,
1561 "expected_count": 1,
1562 "should_call_listener": False,
1563 },
1564 {
1565 "id": "final_deregistration",
1566 "operation": "deregister",
1567 "initial_count": 1,
1568 "expected_count": 0,
1569 "should_call_listener": True,
1570 },
1571 ],
1572 ids=lambda case: case["id"],
1573 )
1574 def test_listener_registration(
1575 self,
1576 test_context: OrcaTestContext,
1577 case: dict,
1578 ) -> None:
1579 """Test EventManager register/deregister listener operations."""
1580 self._setup_dependencies(test_context)
1581 from orca.event_manager import EventManager
1582
1583 manager = EventManager()
1584 manager._listener = test_context.Mock()
1585 event_type = "object:text-changed:insert"
1586
1587 if case["initial_count"] > 0:
1588 manager._script_listener_counts[event_type] = case["initial_count"]
1589
1590 if case["operation"] == "register":
1591 manager.register_listener(event_type)
1592 if case["should_call_listener"]:
1593 manager._listener.register.assert_called_once_with(event_type)
1594 else:
1595 manager._listener.register.assert_not_called()
1596 else: # deregister
1597 manager.deregister_listener(event_type)
1598 if case["should_call_listener"]:
1599 manager._listener.deregister.assert_called_once_with(event_type)
1600 else:
1601 manager._listener.deregister.assert_not_called()
1602
1603 if case["expected_count"] == 0:
1604 assert event_type not in manager._script_listener_counts
1605 else:
1606 assert manager._script_listener_counts[event_type] == case["expected_count"]
1607
1608 @pytest.mark.parametrize(
1609 "case",
1610 [
1611 {"id": "register_script_listeners", "operation": "register"},
1612 {"id": "deregister_script_listeners", "operation": "deregister"},
1613 ],
1614 ids=lambda case: case["id"],
1615 )
1616 def test_script_listeners_operations(self, test_context: OrcaTestContext, case: dict) -> None:
1617 """Test EventManager register/deregister script listeners operations."""
1618 self._setup_dependencies(test_context)
1619 from orca.event_manager import EventManager
1620
1621 manager = EventManager()
1622 mock_listener_method = test_context.Mock()
1623 method_name = f"{case['operation']}_listener"
1624 test_context.patch_object(manager, method_name, new=mock_listener_method)
1625
1626 mock_script = test_context.Mock()
1627 mock_script.listeners = {
1628 "object:text-changed:insert": test_context.Mock(),
1629 "object:state-changed:focused": test_context.Mock(),
1630 }
1631
1632 getattr(manager, f"{case['operation']}_script_listeners")(mock_script)
1633
1634 expected_calls = [
1635 call("object:text-changed:insert"),
1636 call("object:state-changed:focused"),
1637 ]
1638 mock_listener_method.assert_has_calls(expected_calls, any_order=True)
1639
1640 def test_get_script_for_event_focus(self, test_context: OrcaTestContext) -> None:
1641 """Test EventManager._get_script_for_event for focused events."""
1642
1643 self._setup_dependencies(test_context)
1644 from orca.event_manager import EventManager
1645
1646 manager = EventManager()
1647 mock_event = test_context.Mock(spec=Atspi.Event)
1648 mock_event.source = test_context.Mock()
1649 mock_event.type = "object:state-changed:focused"
1650 active_script = test_context.Mock()
1651 mock_get_focus_mgr = test_context.Mock()
1652 test_context.patch("orca.event_manager.focus_manager.get_manager", new=mock_get_focus_mgr)
1653 mock_focus_mgr = test_context.Mock()
1654 mock_get_focus_mgr.return_value = mock_focus_mgr
1655 mock_focus_mgr.get_locus_of_focus.return_value = mock_event.source
1656 result = manager._get_script_for_event(mock_event, active_script)
1657 assert result == active_script
1658
1659 def test_get_script_for_event_mouse(self, test_context: OrcaTestContext) -> None:
1660 """Test EventManager._get_script_for_event for mouse events."""
1661
1662 self._setup_dependencies(test_context)
1663 from orca.event_manager import EventManager
1664
1665 manager = EventManager()
1666 mock_event = test_context.Mock(spec=Atspi.Event)
1667 mock_event.type = "mouse:button:1p"
1668 mock_event.source = test_context.Mock()
1669 expected_script = test_context.Mock()
1670 mock_mouse_event = test_context.Mock()
1671 mock_mouse_event.app = test_context.Mock()
1672 mock_mouse_event.window = test_context.Mock()
1673 mock_get_script_mgr = test_context.Mock()
1674 test_context.patch("orca.event_manager.script_manager.get_manager", new=mock_get_script_mgr)
1675 test_context.patch(
1676 "orca.event_manager.input_event.MouseButtonEvent",
1677 return_value=mock_mouse_event,
1678 )
1679 mock_script_mgr = test_context.Mock()
1680 mock_get_script_mgr.return_value = mock_script_mgr
1681 mock_script_mgr.get_script.return_value = expected_script
1682 result = manager._get_script_for_event(mock_event)
1683 assert result == expected_script
1684
1685 def test_get_script_for_event_defunct_app(self, test_context: OrcaTestContext) -> None:
1686 """Test EventManager._get_script_for_event with defunct application."""
1687
1688 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1689 from orca.event_manager import EventManager
1690
1691 manager = EventManager()
1692 mock_event = test_context.Mock(spec=Atspi.Event)
1693 mock_event.type = "object:text-changed:insert"
1694 mock_event.source = test_context.Mock()
1695 mock_app = test_context.Mock()
1696 ax_utilities = essential_modules["orca.ax_utilities"]
1697 ax_utilities.get_application.return_value = mock_app
1698 ax_utilities.is_defunct.return_value = True
1699 result = manager._get_script_for_event(mock_event)
1700 assert result is None
1701
1702 @pytest.mark.parametrize(
1703 "case",
1704 [
1705 {
1706 "id": "focus_claimed",
1707 "event_type": "object:state-changed:focused",
1708 "detail1": 1,
1709 "expected": True,
1710 "reason_contains": "claimed focus",
1711 },
1712 {
1713 "id": "menu_selection",
1714 "event_type": "object:state-changed:selected",
1715 "detail1": 1,
1716 "expected": True,
1717 "reason_contains": "Selection change",
1718 },
1719 {
1720 "id": "modal_panel",
1721 "event_type": "object:state-changed:showing",
1722 "detail1": 0,
1723 "expected": True,
1724 "reason_contains": "Modal panel",
1725 },
1726 {
1727 "id": "no_activation",
1728 "event_type": "object:text-changed:insert",
1729 "detail1": 0,
1730 "expected": False,
1731 "reason_contains": "No reason",
1732 },
1733 ],
1734 ids=lambda case: case["id"],
1735 )
1736 def test_is_activatable_event(self, test_context: OrcaTestContext, case: dict) -> None:
1737 """Test EventManager._is_activatable_event."""
1738
1739 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1740 from orca.event_manager import EventManager
1741
1742 manager = EventManager()
1743 mock_event = test_context.Mock(spec=Atspi.Event)
1744 mock_event.type = case["event_type"]
1745 mock_event.detail1 = case["detail1"]
1746 mock_event.source = test_context.Mock()
1747 mock_script = test_context.Mock()
1748 mock_script.is_activatable_event.return_value = True
1749 mock_script.force_script_activation.return_value = False
1750 ax_utilities = essential_modules["orca.ax_utilities"]
1751 if case["event_type"] == "object:state-changed:selected":
1752 ax_utilities.is_menu.return_value = True
1753 ax_utilities.is_focusable.return_value = True
1754 elif case["event_type"] == "object:state-changed:showing":
1755 ax_utilities.is_panel.return_value = True
1756 ax_utilities.is_modal.return_value = True
1757 else:
1758 ax_utilities.is_frame.return_value = case["event_type"] == "object:state-changed:active"
1759 focus_mgr = essential_modules["focus_manager_instance"]
1760 if case["event_type"] == "window:activate":
1761 focus_mgr.get_active_window.return_value = mock_event.source
1762 else:
1763 focus_mgr.get_active_window.return_value = test_context.Mock()
1764 result, reason = manager._is_activatable_event(mock_event, mock_script)
1765 assert result == case["expected"]
1766 assert case["reason_contains"].lower() in reason.lower()
1767
1768 def test_is_activatable_event_no_source(self, test_context: OrcaTestContext) -> None:
1769 """Test EventManager._is_activatable_event with no event source."""
1770
1771 self._setup_dependencies(test_context)
1772 from orca.event_manager import EventManager
1773
1774 manager = EventManager()
1775 mock_event = test_context.Mock(spec=Atspi.Event)
1776 mock_event.source = None
1777 result, reason = manager._is_activatable_event(mock_event)
1778 assert result is False
1779 assert "event.source" in reason
1780
1781 def test_is_activatable_event_script_forces(self, test_context: OrcaTestContext) -> None:
1782 """Test EventManager._is_activatable_event when script forces activation."""
1783
1784 self._setup_dependencies(test_context)
1785 from orca.event_manager import EventManager
1786
1787 manager = EventManager()
1788 mock_event = test_context.Mock(spec=Atspi.Event)
1789 mock_event.type = "object:text-changed:insert"
1790 mock_event.source = test_context.Mock()
1791 mock_script = test_context.Mock()
1792 mock_script.is_activatable_event.return_value = True
1793 mock_script.force_script_activation.return_value = True
1794 result, reason = manager._is_activatable_event(mock_event, mock_script)
1795 assert result is True
1796 assert "insists" in reason
1797
1798 @pytest.mark.parametrize(
1799 "case",
1800 [
1801 {
1802 "id": "active_script",
1803 "event_script_is_active": True,
1804 "present_if_inactive": False,
1805 "event_type": "test:event",
1806 "is_progress_bar": False,
1807 "expected": True,
1808 },
1809 {
1810 "id": "inactive_but_presents",
1811 "event_script_is_active": False,
1812 "present_if_inactive": True,
1813 "event_type": "test:event",
1814 "is_progress_bar": False,
1815 "expected": True,
1816 },
1817 {
1818 "id": "progress_bar_value_change",
1819 "event_script_is_active": False,
1820 "present_if_inactive": False,
1821 "event_type": "object:property-change:accessible-value",
1822 "is_progress_bar": True,
1823 "expected": True,
1824 },
1825 {
1826 "id": "progress_bar_non_value_event",
1827 "event_script_is_active": False,
1828 "present_if_inactive": False,
1829 "event_type": "object:state-changed:focused",
1830 "is_progress_bar": True,
1831 "expected": False,
1832 },
1833 {
1834 "id": "no_reason_to_process",
1835 "event_script_is_active": False,
1836 "present_if_inactive": False,
1837 "event_type": "test:event",
1838 "is_progress_bar": False,
1839 "expected": False,
1840 },
1841 ],
1842 ids=lambda case: case["id"],
1843 )
1844 def test_should_process_event(
1845 self,
1846 test_context: OrcaTestContext,
1847 case: dict,
1848 ) -> None:
1849 """Test EventManager._should_process_event."""
1850
1851 self._setup_dependencies(test_context)
1852 from orca.ax_utilities import AXUtilities
1853 from orca.event_manager import EventManager
1854
1855 manager = EventManager()
1856 mock_event = test_context.Mock(spec=Atspi.Event)
1857 mock_event.type = case["event_type"]
1858 mock_event.source = test_context.Mock()
1859 event_script = test_context.Mock()
1860 event_script.present_if_inactive = case["present_if_inactive"]
1861 test_context.patch_object(
1862 AXUtilities,
1863 "is_progress_bar",
1864 side_effect=lambda obj: case["is_progress_bar"],
1865 )
1866 if case["event_script_is_active"]:
1867 active_script = event_script
1868 else:
1869 active_script = test_context.Mock()
1870 result = manager._should_process_event(mock_event, event_script, active_script)
1871 assert result == case["expected"]
1872
1873 def test_process_object_event_obsoleted(self, test_context: OrcaTestContext) -> None:
1874 """Test EventManager._process_object_event with obsoleted event."""
1875
1876 self._setup_dependencies(test_context)
1877 from orca.event_manager import EventManager
1878
1879 manager = EventManager()
1880 mock_event = test_context.Mock(spec=Atspi.Event)
1881 mock_is_obsoleted = test_context.Mock(return_value=test_context.Mock())
1882 test_context.patch_object(manager, "_is_obsoleted_by", new=mock_is_obsoleted)
1883 manager._process_object_event(mock_event)
1884
1885 def test_process_object_event_dead_source(self, test_context: OrcaTestContext) -> None:
1886 """Test EventManager._process_object_event with dead event source."""
1887
1888 self._setup_dependencies(test_context)
1889 from orca.event_manager import EventManager
1890
1891 manager = EventManager()
1892 mock_event = test_context.Mock(spec=Atspi.Event)
1893 mock_event.type = "window:deactivate"
1894 mock_event.source = test_context.Mock()
1895 mock_is_obsoleted = test_context.Mock(return_value=None)
1896 test_context.patch_object(manager, "_is_obsoleted_by", new=mock_is_obsoleted)
1897 test_context.patch("orca.event_manager.AXObject.is_dead", return_value=True)
1898 test_context.patch("orca.event_manager.AXUtilities.is_defunct", return_value=False)
1899 mock_get_focus_mgr = test_context.Mock()
1900 test_context.patch("orca.event_manager.focus_manager.get_manager", new=mock_get_focus_mgr)
1901 mock_get_script_mgr = test_context.Mock()
1902 test_context.patch("orca.event_manager.script_manager.get_manager", new=mock_get_script_mgr)
1903 mock_focus_mgr = test_context.Mock()
1904 mock_script_mgr = test_context.Mock()
1905 mock_get_focus_mgr.return_value = mock_focus_mgr
1906 mock_get_script_mgr.return_value = mock_script_mgr
1907 mock_focus_mgr.get_active_window.return_value = mock_event.source
1908 manager._process_object_event(mock_event)
1909
1910 mock_focus_mgr.clear_state.assert_called_once()
1911 mock_script_mgr.set_active_script.assert_called_once_with(
1912 None,
1913 "Active window is dead or defunct",
1914 )
1915
1916 def test_process_object_event_no_listener(self, test_context: OrcaTestContext) -> None:
1917 """Test EventManager._process_object_event with no matching listener."""
1918
1919 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
1920 from orca.event_manager import EventManager
1921
1922 manager = EventManager()
1923 mock_event = test_context.Mock(spec=Atspi.Event)
1924 mock_event.type = "object:unknown-event"
1925 mock_event.source = test_context.Mock()
1926 mock_is_obsoleted = test_context.Mock(return_value=None)
1927 test_context.patch_object(manager, "_is_obsoleted_by", new=mock_is_obsoleted)
1928 ax_object = essential_modules["orca.ax_object"]
1929 ax_utilities = essential_modules["orca.ax_utilities"]
1930 ax_object.is_dead.return_value = False
1931 ax_utilities.is_defunct.return_value = False
1932 ax_utilities.is_iconified.return_value = False
1933 mock_script = test_context.Mock()
1934 mock_script.listeners = {}
1935 script_mgr = essential_modules["script_manager_instance"]
1936 script_mgr.get_active_script.return_value = mock_script
1937 mock_get_script = test_context.Mock(return_value=mock_script)
1938 mock_is_activatable = test_context.Mock(return_value=(False, "test"))
1939 mock_should_process = test_context.Mock(return_value=True)
1940 test_context.patch_object(manager, "_get_script_for_event", new=mock_get_script)
1941 test_context.patch_object(manager, "_is_activatable_event", new=mock_is_activatable)
1942 test_context.patch_object(manager, "_should_process_event", new=mock_should_process)
1943 manager._process_object_event(mock_event)
1944
1945 def test_get_manager(self, test_context: OrcaTestContext) -> None:
1946 """Test event_manager.get_manager."""
1947
1948 self._setup_dependencies(test_context)
1949 from orca import event_manager
1950
1951 manager1 = event_manager.get_manager()
1952 manager2 = event_manager.get_manager()
1953
1954 assert manager1 is manager2
1955 assert isinstance(manager1, event_manager.EventManager)
1956
1957 def test_enqueue_with_comprehensive_gidle_management(
1958 self,
1959 test_context: OrcaTestContext,
1960 ) -> None:
1961 """Test EventManager._enqueue_object_event with comprehensive GLib idle management."""
1962
1963 self._setup_dependencies(test_context)
1964 from orca.event_manager import EventManager
1965
1966 manager = EventManager()
1967 manager._active = True
1968 manager._paused = False
1969 manager._gidle_id = 0 # No existing idle handler
1970 mock_event = test_context.Mock(spec=Atspi.Event)
1971 mock_event.type = "object:text-changed:insert"
1972 mock_event.source = test_context.Mock()
1973 mock_app = test_context.Mock()
1974 mock_script = test_context.Mock()
1975 mock_script.event_cache = {}
1976
1977 mock_ignore = test_context.Mock(return_value=False)
1978 test_context.patch_object(manager, "_ignore", new=mock_ignore)
1979 test_context.patch("orca.event_manager.AXUtilities.get_application", return_value=mock_app)
1980 mock_get_script_mgr = test_context.Mock()
1981 test_context.patch("orca.event_manager.script_manager.get_manager", new=mock_get_script_mgr)
1982 mock_idle_add = test_context.Mock(return_value=456)
1983 test_context.patch("orca.event_manager.GLib.idle_add", new=mock_idle_add)
1984 test_context.patch("time.time", return_value=200.0)
1985 mock_script_mgr = test_context.Mock()
1986 mock_get_script_mgr.return_value = mock_script_mgr
1987 mock_script_mgr.get_script.return_value = mock_script
1988 manager._enqueue_object_event(mock_event)
1989
1990 assert not manager._event_queue.empty()
1991 assert mock_event.type in mock_script.event_cache
1992 mock_idle_add.assert_called_once_with(manager._dequeue_object_event)
1993 assert manager._gidle_id == 456
1994
1995 mock_idle_add.reset_mock()
1996 manager._gidle_id = 456
1997 test_context.patch("time.time", return_value=200.1)
1998 manager._enqueue_object_event(mock_event)
1999
2000 mock_idle_add.assert_not_called()
2001 assert manager._gidle_id == 456