Debian Codegraph @

tests/unit_tests/test_input_event_manager.py

raw
1 # Unit tests for input_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 # pylint: disable=too-many-instance-attributes
31
32 """Unit tests for input_event_manager.py methods."""
33
34 from __future__ import annotations
35
36 from typing import TYPE_CHECKING
37 from unittest.mock import call
38
39 import pytest
40
41 if TYPE_CHECKING:
42 from unittest.mock import MagicMock
43
44 from .orca_test_context import OrcaTestContext
45
46
47 @pytest.mark.unit
48 class TestInputEventManager:
49 """Test InputEventManager class methods."""
50
51 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]:
52 """Returns dependencies for input_event_manager module testing."""
53
54 additional_modules = ["orca.ax_utilities", "gi.repository"]
55 essential_modules = test_context.setup_shared_dependencies(additional_modules)
56
57 debug_mock = essential_modules["orca.debug"]
58 debug_mock.LEVEL_INFO = 800
59 debug_mock.LEVEL_WARNING = 2
60 debug_mock.LEVEL_SEVERE = 3
61 debug_mock.debugLevel = 0
62
63 focus_manager_mock = essential_modules["orca.focus_manager"]
64 focus_mgr_instance = test_context.Mock()
65 focus_mgr_instance.get_locus_of_focus = test_context.Mock()
66 focus_mgr_instance.get_active_window = test_context.Mock()
67 focus_mgr_instance.focus_and_window_are_unknown = test_context.Mock(return_value=False)
68 focus_mgr_instance.clear_state = test_context.Mock()
69 focus_manager_mock.get_manager = test_context.Mock(return_value=focus_mgr_instance)
70
71 script_manager_mock = essential_modules["orca.script_manager"]
72 script_mgr_instance = test_context.Mock()
73 script_instance = test_context.Mock()
74 script_instance.app = test_context.Mock()
75 script_instance.event_cache = {}
76 script_instance.listeners = {}
77 script_instance.is_activatable_event = test_context.Mock(return_value=True)
78 script_instance.force_script_activation = test_context.Mock(return_value=False)
79 script_instance.present_if_inactive = False
80 script_mgr_instance.get_active_script = test_context.Mock(return_value=script_instance)
81 script_mgr_instance.get_script = test_context.Mock(return_value=script_instance)
82 script_mgr_instance.set_active_script = test_context.Mock()
83 script_mgr_instance.get_default_script = test_context.Mock(return_value=script_instance)
84 script_mgr_instance.reclaim_scripts = test_context.Mock()
85 script_manager_mock.get_manager = test_context.Mock(return_value=script_mgr_instance)
86
87 input_event_mock = essential_modules["orca.input_event"]
88
89 class MockKeyboardEvent:
90 """Mock KeyboardEvent class for testing."""
91
92 def __init__(self, pressed=True, keycode=65, keysym=97, modifiers=0, text="a"):
93 self.process = test_context.Mock()
94 self.set_window = test_context.Mock()
95 self.set_object = test_context.Mock()
96 self.set_script = test_context.Mock()
97 self.set_click_count = test_context.Mock()
98 self.is_modifier_key = test_context.Mock(return_value=False)
99 self.is_pressed_key = test_context.Mock(return_value=True)
100 self.get_window = test_context.Mock()
101 self.get_object = test_context.Mock()
102 self.get_script = test_context.Mock()
103 self.get_click_count = test_context.Mock(return_value=1)
104 self.is_printable_key = test_context.Mock(return_value=True)
105 self.as_single_line_string = test_context.Mock(return_value="KeyboardEvent")
106 self.pressed = pressed
107 self.keycode = keycode
108 self.keysym = keysym
109 self.modifiers = modifiers
110 self.text = text
111 self.id = 1
112 self.hw_code = keycode
113 self.keyval_name = text
114 self.time = 1000
115 self.button = None
116
117 def __eq__(self, other):
118 """Enable equality comparison for duplicate detection."""
119
120 if not isinstance(other, MockKeyboardEvent):
121 return False
122 return (
123 self.pressed == other.pressed
124 and self.keycode == other.keycode
125 and self.keysym == other.keysym
126 and self.modifiers == other.modifiers
127 and self.text == other.text
128 )
129
130 def __hash__(self):
131 return hash((self.pressed, self.keycode, self.keysym, self.modifiers, self.text))
132
133 class MockBrailleEvent:
134 """Mock BrailleEvent class for testing."""
135
136 def __init__(self, *_args, **_kwargs):
137 self.process = test_context.Mock(return_value=True)
138
139 class MockMouseButtonEvent:
140 """Mock MouseButtonEvent class for testing."""
141
142 def __init__(self, *_args, **_kwargs):
143 self.set_click_count = test_context.Mock()
144 self.get_click_count = test_context.Mock(return_value=1)
145 self.button = "1"
146 self.pressed = True
147 self.time = 1000
148
149 input_event_mock.KeyboardEvent = MockKeyboardEvent
150 input_event_mock.BrailleEvent = MockBrailleEvent
151 input_event_mock.MouseButtonEvent = MockMouseButtonEvent
152
153 ax_utilities_mock = essential_modules["orca.ax_utilities"]
154 ax_utilities_class = test_context.Mock()
155 ax_utilities_class.can_be_active_window = test_context.Mock(return_value=True)
156 ax_utilities_class.find_active_window = test_context.Mock(return_value=None)
157 ax_utilities_class.is_single_line = test_context.Mock(return_value=False)
158 ax_utilities_class.is_widget_controlled_by_line_navigation = test_context.Mock(
159 return_value=False,
160 )
161 ax_utilities_class.has_matching_shortcut = test_context.Mock(return_value=False)
162 ax_utilities_class.is_table_header = test_context.Mock(return_value=False)
163 ax_utilities_class.is_terminal = test_context.Mock(return_value=False)
164 ax_utilities_mock.AXUtilities = ax_utilities_class
165
166 gi_repo_mock = essential_modules["gi.repository"]
167
168 atspi_mock = test_context.Mock()
169 atspi_mock.get_version = test_context.Mock(return_value=(2, 56, 0))
170 atspi_mock.Device = test_context.Mock()
171 atspi_mock.Device.new_full = test_context.Mock()
172 atspi_mock.Device.new = test_context.Mock()
173 atspi_mock.Device.grab_keyboard = test_context.Mock()
174 atspi_mock.Device.ungrab_keyboard = test_context.Mock()
175 atspi_mock.KeyDefinition = test_context.Mock()
176 atspi_mock.ModifierType = test_context.Mock()
177 atspi_mock.ModifierType.CONTROL = 2
178 atspi_mock.ModifierType.SHIFT = 0
179 atspi_mock.ModifierType.ALT = 3
180 gi_repo_mock.Atspi = atspi_mock
181
182 essential_modules["focus_manager_instance"] = focus_mgr_instance
183 essential_modules["script_manager_instance"] = script_mgr_instance
184 essential_modules["script_instance"] = script_instance
185
186 essential_modules["ax_utilities_class"] = ax_utilities_class
187 essential_modules["atspi"] = atspi_mock
188 return essential_modules
189
190 def _setup_input_event_manager(self, test_context) -> tuple:
191 """Helper method to set up InputEventManager with dependencies."""
192
193 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
194
195 test_context.patch("orca.input_event_manager.debug", new=essential_modules["orca.debug"])
196 test_context.patch(
197 "orca.input_event_manager.focus_manager",
198 new=essential_modules["orca.focus_manager"],
199 )
200 test_context.patch(
201 "orca.input_event_manager.script_manager",
202 new=essential_modules["orca.script_manager"],
203 )
204 test_context.patch(
205 "orca.input_event_manager.input_event",
206 new=essential_modules["orca.input_event"],
207 )
208 test_context.patch(
209 "orca.input_event_manager.AXUtilities",
210 new=essential_modules["ax_utilities_class"],
211 )
212 test_context.patch("orca.input_event_manager.Atspi", new=essential_modules["atspi"])
213
214 ax_device_mgr_mock = test_context.Mock()
215 essential_modules["ax_device_manager"] = ax_device_mgr_mock
216 test_context.patch("orca.input_event_manager.ax_device_manager", new=ax_device_mgr_mock)
217
218 from orca.input_event_manager import InputEventManager
219
220 return InputEventManager(), essential_modules
221
222 def test_init(self, test_context: OrcaTestContext) -> None:
223 """Test InputEventManager.__init__."""
224
225 input_event_manager, _essential_modules = self._setup_input_event_manager(test_context)
226 assert input_event_manager._last_input_event is None
227 assert input_event_manager._last_non_modifier_key_event is None
228 assert input_event_manager._device is None
229 assert not input_event_manager._mapped_keycodes
230 assert not input_event_manager._mapped_keysyms
231 assert not input_event_manager._grabbed_bindings
232
233 def test_start_key_watcher(self, test_context) -> None:
234 """Test InputEventManager.start_key_watcher."""
235
236 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
237 mock_device = test_context.Mock()
238 ax_device_mgr = essential_modules["ax_device_manager"]
239 ax_device_mgr.get_manager.return_value.get_device.return_value = mock_device
240
241 input_event_manager.start_key_watcher()
242
243 ax_device_mgr.get_manager.return_value.get_device.assert_called_once()
244 mock_device.add_key_watcher.assert_called_once_with(
245 input_event_manager.process_keyboard_event,
246 )
247 assert input_event_manager._device is mock_device
248
249 def test_stop_key_watcher(self, test_context: OrcaTestContext) -> None:
250 """Test InputEventManager.stop_key_watcher."""
251
252 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
253 input_event_manager._device = test_context.Mock()
254 input_event_manager.stop_key_watcher()
255 assert input_event_manager._device is None
256 essential_modules["orca.debug"].print_message.assert_called()
257
258 @pytest.mark.parametrize(
259 "case",
260 [
261 {
262 "id": "disabled",
263 "scenario": "disabled",
264 "is_enabled": False,
265 "is_bound": True,
266 "has_grabs": False,
267 "has_device": False,
268 "expected_result": [],
269 "expects_debug_call": False,
270 },
271 {
272 "id": "unbound",
273 "scenario": "unbound",
274 "is_enabled": True,
275 "is_bound": False,
276 "has_grabs": False,
277 "has_device": False,
278 "expected_result": [],
279 "expects_debug_call": False,
280 },
281 {
282 "id": "has_grabs",
283 "scenario": "has_grabs",
284 "is_enabled": True,
285 "is_bound": True,
286 "has_grabs": True,
287 "has_device": False,
288 "existing_grab_ids": [333, 444],
289 "expected_result": [333, 444],
290 "expects_debug_call": True,
291 },
292 {
293 "id": "no_device",
294 "scenario": "no_device",
295 "is_enabled": True,
296 "is_bound": True,
297 "has_grabs": False,
298 "has_device": False,
299 "expected_result": [],
300 "expects_debug_call": True,
301 },
302 {
303 "id": "success",
304 "scenario": "success",
305 "is_enabled": True,
306 "is_bound": True,
307 "has_grabs": False,
308 "has_device": True,
309 "expected_result": [111, 222],
310 "expects_debug_call": False,
311 },
312 ],
313 ids=lambda case: case["id"],
314 )
315 def test_add_grabs_for_keybinding_scenarios(
316 self,
317 test_context: OrcaTestContext,
318 case: dict,
319 ) -> None:
320 """Test InputEventManager.add_grabs_for_keybinding with various scenarios."""
321
322 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
323
324 mock_binding = test_context.Mock()
325 mock_binding.is_enabled.return_value = case["is_enabled"]
326 mock_binding.is_bound.return_value = case["is_bound"]
327 mock_binding.has_grabs.return_value = case["has_grabs"]
328 mock_binding.get_grab_ids.return_value = case.get("existing_grab_ids", [])
329
330 if case["has_device"]:
331 mock_device = test_context.Mock()
332 mock_device.add_key_grab.side_effect = [111, 222]
333 input_event_manager._device = mock_device
334 mock_kd1 = test_context.Mock()
335 mock_kd2 = test_context.Mock()
336 mock_binding.key_definitions.return_value = [mock_kd1, mock_kd2]
337
338 result = input_event_manager.add_grabs_for_keybinding(mock_binding, ["Insert", "KP_Insert"])
339
340 if case["scenario"] == "success":
341 assert result == case["expected_result"]
342 assert input_event_manager._grabbed_bindings[111] == mock_binding
343 assert input_event_manager._grabbed_bindings[222] == mock_binding
344 mock_device.add_key_grab.assert_has_calls([call(mock_kd1, None), call(mock_kd2, None)])
345 else:
346 assert result == case["expected_result"]
347
348 if case["expects_debug_call"]:
349 essential_modules["orca.debug"].print_tokens.assert_called()
350
351 @pytest.mark.parametrize(
352 "case",
353 [
354 {
355 "id": "no_device",
356 "scenario": "no_device",
357 "has_device": False,
358 "grab_ids": [],
359 "has_grabbed_bindings": False,
360 "expects_debug_tokens": True,
361 "expects_debug_message": False,
362 "expects_device_calls": False,
363 },
364 {
365 "id": "no_grabs",
366 "scenario": "no_grabs",
367 "has_device": True,
368 "grab_ids": [],
369 "has_grabbed_bindings": False,
370 "expects_debug_tokens": False,
371 "expects_debug_message": False,
372 "expects_device_calls": False,
373 },
374 {
375 "id": "success",
376 "scenario": "success",
377 "has_device": True,
378 "grab_ids": [111, 222],
379 "has_grabbed_bindings": True,
380 "expects_debug_tokens": False,
381 "expects_debug_message": False,
382 "expects_device_calls": True,
383 },
384 {
385 "id": "missing_grab",
386 "scenario": "missing_grab",
387 "has_device": True,
388 "grab_ids": [999],
389 "has_grabbed_bindings": False,
390 "expects_debug_tokens": False,
391 "expects_debug_message": True,
392 "expects_device_calls": True,
393 },
394 ],
395 ids=lambda case: case["id"],
396 )
397 def test_remove_grabs_for_keybinding_scenarios(
398 self,
399 test_context: OrcaTestContext,
400 case: dict,
401 ) -> None:
402 """Test InputEventManager.remove_grabs_for_keybinding with various scenarios."""
403
404 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
405
406 if case["has_device"]:
407 mock_device = test_context.Mock()
408 input_event_manager._device = mock_device
409
410 if case["has_grabbed_bindings"]:
411 input_event_manager._grabbed_bindings = {
412 111: test_context.Mock(),
413 222: test_context.Mock(),
414 }
415
416 mock_binding = test_context.Mock()
417 mock_binding.get_grab_ids.return_value = case["grab_ids"]
418
419 input_event_manager.remove_grabs_for_keybinding(mock_binding)
420
421 if case["expects_debug_tokens"]:
422 essential_modules["orca.debug"].print_tokens.assert_called()
423
424 if case["expects_debug_message"]:
425 essential_modules["orca.debug"].print_message.assert_called()
426
427 if case["expects_device_calls"]:
428 if case["scenario"] == "success":
429 mock_device.remove_key_grab.assert_has_calls([call(111), call(222)])
430 assert 111 not in input_event_manager._grabbed_bindings
431 assert 222 not in input_event_manager._grabbed_bindings
432 elif case["scenario"] == "missing_grab":
433 mock_device.remove_key_grab.assert_called_once_with(999)
434
435 @pytest.mark.parametrize(
436 "case",
437 [
438 {
439 "id": "keysym_no_device",
440 "has_device": False,
441 "input_value": 0x61,
442 "expected_result": 0,
443 "device_method": None,
444 "device_return_value": None,
445 "mapped_collection": None,
446 },
447 {
448 "id": "keysym_success",
449 "has_device": True,
450 "input_value": 0x61,
451 "expected_result": 16,
452 "device_method": "map_keysym_modifier",
453 "device_return_value": 16,
454 "mapped_collection": "_mapped_keysyms",
455 },
456 ],
457 ids=lambda case: case["id"],
458 )
459 def test_map_keysym_to_modifier_scenarios(
460 self,
461 test_context: OrcaTestContext,
462 case: dict,
463 ) -> None:
464 """Test InputEventManager.map_keysym_to_modifier with various scenarios."""
465
466 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
467 if case["has_device"]:
468 mock_device = test_context.Mock()
469 if case["device_method"]:
470 getattr(mock_device, case["device_method"]).return_value = case[
471 "device_return_value"
472 ]
473 input_event_manager._device = mock_device
474
475 result = input_event_manager.map_keysym_to_modifier(case["input_value"])
476
477 assert result == case["expected_result"]
478
479 if case["has_device"] and case["device_method"] and case["mapped_collection"]:
480 getattr(mock_device, case["device_method"]).assert_called_once_with(case["input_value"])
481 collection = getattr(input_event_manager, case["mapped_collection"])
482 assert case["input_value"] in collection
483 elif not case["has_device"]:
484 essential_modules["orca.debug"].print_message.assert_called()
485
486 @pytest.mark.parametrize(
487 "case",
488 [
489 {
490 "id": "no_device",
491 "has_device": False,
492 "expects_debug_call": True,
493 "expects_unmap_calls": False,
494 },
495 {
496 "id": "success",
497 "has_device": True,
498 "expects_debug_call": False,
499 "expects_unmap_calls": True,
500 },
501 ],
502 ids=lambda case: case["id"],
503 )
504 def test_unmap_all_modifiers_scenarios(self, test_context, case: dict) -> None:
505 """Test InputEventManager.unmap_all_modifiers scenarios."""
506
507 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
508 input_event_manager._mapped_keycodes = [42, 43]
509 input_event_manager._mapped_keysyms = [0x61, 0x62]
510
511 if case["has_device"]:
512 mock_device = test_context.Mock()
513 input_event_manager._device = mock_device
514
515 input_event_manager.unmap_all_modifiers()
516
517 if case["expects_debug_call"]:
518 essential_modules["orca.debug"].print_message.assert_called()
519
520 if case["expects_unmap_calls"]:
521 mock_device.unmap_modifier.assert_has_calls([call(42), call(43)])
522 mock_device.unmap_keysym_modifier.assert_has_calls([call(0x61), call(0x62)])
523 assert not input_event_manager._mapped_keycodes
524 assert not input_event_manager._mapped_keysyms
525
526 @pytest.mark.parametrize(
527 "case",
528 [
529 {
530 "id": "add_no_device",
531 "operation": "add",
532 "scenario": "no_device",
533 "has_device": False,
534 "grab_id": None,
535 "expected_result": -1,
536 "expects_debug_call": True,
537 },
538 {
539 "id": "add_success",
540 "operation": "add",
541 "scenario": "success",
542 "has_device": True,
543 "grab_id": 789,
544 "expected_result": 789,
545 "expects_debug_call": False,
546 },
547 {
548 "id": "remove_no_device",
549 "operation": "remove",
550 "scenario": "no_device",
551 "has_device": False,
552 "grab_id": 789,
553 "expected_result": None,
554 "expects_debug_call": True,
555 },
556 {
557 "id": "remove_success",
558 "operation": "remove",
559 "scenario": "success",
560 "has_device": True,
561 "grab_id": 789,
562 "expected_result": None,
563 "expects_debug_call": True,
564 },
565 ],
566 ids=lambda case: case["id"],
567 )
568 def test_grab_for_modifier_scenarios(self, test_context, case: dict) -> None:
569 """Test InputEventManager add/remove_grab_for_modifier scenarios."""
570
571 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
572
573 if case["has_device"]:
574 mock_device = test_context.Mock()
575 if case["operation"] == "add":
576 mock_device.add_key_grab.return_value = case["grab_id"]
577 input_event_manager._device = mock_device
578
579 if case["operation"] == "add":
580 result = input_event_manager.add_grab_for_modifier("Shift", 0xFFE1, 50)
581 if case["expected_result"] is not None:
582 assert result == case["expected_result"]
583 if case["has_device"] and case["scenario"] == "success":
584 mock_device.add_key_grab.assert_called_once()
585 else:
586 input_event_manager.remove_grab_for_modifier("Shift", case["grab_id"])
587 if case["has_device"] and case["scenario"] == "success":
588 mock_device.remove_key_grab.assert_called_once_with(case["grab_id"])
589
590 if case["expects_debug_call"]:
591 essential_modules["orca.debug"].print_message.assert_called()
592
593 @pytest.mark.parametrize(
594 "case",
595 [
596 {
597 "id": "grab_without_reason",
598 "operation": "grab",
599 "reason": None,
600 "expected_reason_text": None,
601 },
602 {
603 "id": "grab_with_reason",
604 "operation": "grab",
605 "reason": "learn mode",
606 "expected_reason_text": "learn mode",
607 },
608 {
609 "id": "ungrab_without_reason",
610 "operation": "ungrab",
611 "reason": None,
612 "expected_reason_text": None,
613 },
614 {
615 "id": "ungrab_with_reason",
616 "operation": "ungrab",
617 "reason": "exiting learn mode",
618 "expected_reason_text": "exiting learn mode",
619 },
620 ],
621 ids=lambda case: case["id"],
622 )
623 def test_keyboard_grab_scenarios(self, test_context: OrcaTestContext, case: dict) -> None:
624 """Test InputEventManager keyboard grab/ungrab operations with and without reasons."""
625 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
626 mock_device = test_context.Mock()
627 input_event_manager._device = mock_device
628
629 if case["operation"] == "grab":
630 if case["reason"]:
631 input_event_manager.grab_keyboard(case["reason"])
632 else:
633 input_event_manager.grab_keyboard()
634 essential_modules["atspi"].Device.grab_keyboard.assert_called_once_with(mock_device)
635 else:
636 if case["reason"]:
637 input_event_manager.ungrab_keyboard(case["reason"])
638 else:
639 input_event_manager.ungrab_keyboard()
640 essential_modules["atspi"].Device.ungrab_keyboard.assert_called_once_with(mock_device)
641
642 if case["expected_reason_text"]:
643 debug_calls = essential_modules["orca.debug"].print_message.call_args_list
644 assert any(case["expected_reason_text"] in str(call) for call in debug_calls)
645
646 def test_process_braille_event(self, test_context: OrcaTestContext) -> None:
647 """Test InputEventManager.process_braille_event."""
648
649 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
650 mock_event = test_context.Mock()
651 result = input_event_manager.process_braille_event(mock_event)
652 assert result is True
653 assert isinstance(
654 input_event_manager._last_input_event,
655 essential_modules["orca.input_event"].BrailleEvent,
656 )
657 assert input_event_manager._last_non_modifier_key_event is None
658
659 def test_process_mouse_button_event(self, test_context: OrcaTestContext) -> None:
660 """Test InputEventManager.process_mouse_button_event."""
661
662 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
663 mock_event = test_context.Mock()
664 input_event_manager._determine_mouse_event_click_count = test_context.Mock(return_value=2)
665 input_event_manager.process_mouse_button_event(mock_event)
666 assert isinstance(
667 input_event_manager._last_input_event,
668 essential_modules["orca.input_event"].MouseButtonEvent,
669 )
670 input_event_manager._last_input_event.set_click_count.assert_called_once_with(2)
671
672 @pytest.mark.parametrize(
673 "case",
674 [
675 {
676 "id": "key_press_active_window",
677 "pressed": True,
678 "keycode": 65,
679 "keysym": 97,
680 "modifiers": 0,
681 "text": "a",
682 "window_can_be_active": True,
683 "new_window_found": None,
684 },
685 {
686 "id": "key_press_inactive_window_with_alternative",
687 "pressed": True,
688 "keycode": 65,
689 "keysym": 97,
690 "modifiers": 0,
691 "text": "a",
692 "window_can_be_active": False,
693 "new_window_found": "mock_window",
694 },
695 {
696 "id": "key_press_inactive_window_no_alternative",
697 "pressed": True,
698 "keycode": 65,
699 "keysym": 97,
700 "modifiers": 0,
701 "text": "a",
702 "window_can_be_active": False,
703 "new_window_found": None,
704 },
705 {
706 "id": "key_release",
707 "pressed": False,
708 "keycode": 65,
709 "keysym": 97,
710 "modifiers": 0,
711 "text": "a",
712 "window_can_be_active": True,
713 "new_window_found": None,
714 },
715 ],
716 ids=lambda case: case["id"],
717 )
718 def test_process_keyboard_event(self, test_context, case: dict) -> None:
719 """Test InputEventManager.process_keyboard_event."""
720
721 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
722 new_window_found = case["new_window_found"]
723 if new_window_found == "mock_window":
724 new_window_found = test_context.Mock()
725 mock_device = test_context.Mock()
726 mock_window = test_context.Mock()
727 mock_focus = test_context.Mock()
728 mock_script = test_context.Mock()
729 mock_focus_manager = essential_modules["orca.focus_manager"]
730 mock_focus_manager.get_active_window.return_value = mock_window
731 mock_focus_manager.get_locus_of_focus.return_value = mock_focus
732 mock_script_manager = essential_modules["orca.script_manager"]
733 mock_script_manager.get_active_script.return_value = mock_script
734 essential_modules["ax_utilities_class"].can_be_active_window.return_value = case[
735 "window_can_be_active"
736 ]
737 essential_modules["ax_utilities_class"].find_active_window.return_value = new_window_found
738 input_event_manager._determine_keyboard_event_click_count = test_context.Mock(
739 return_value=1,
740 )
741 result = input_event_manager.process_keyboard_event(
742 mock_device,
743 case["pressed"],
744 case["keycode"],
745 case["keysym"],
746 case["modifiers"],
747 case["text"],
748 )
749 assert result is True
750 assert isinstance(
751 input_event_manager._last_input_event,
752 essential_modules["orca.input_event"].KeyboardEvent,
753 )
754 input_event_manager._last_input_event.process.assert_called_once()
755
756 def test_process_keyboard_event_duplicate(self, test_context: OrcaTestContext) -> None:
757 """Test InputEventManager.process_keyboard_event with duplicate event."""
758
759 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
760 mock_device = test_context.Mock()
761 keyboard_event_instance = essential_modules["orca.input_event"].KeyboardEvent(
762 True,
763 65,
764 97,
765 0,
766 "a",
767 )
768 input_event_manager._last_input_event = keyboard_event_instance
769 result = input_event_manager.process_keyboard_event(mock_device, True, 65, 97, 0, "a")
770 assert result is False
771 essential_modules["orca.debug"].print_message.assert_called()
772
773 @pytest.mark.parametrize(
774 "case",
775 [
776 {
777 "id": "keyboard_true",
778 "method_name": "last_event_was_keyboard",
779 "event_class_name": "KeyboardEvent",
780 "expected_result": True,
781 },
782 {
783 "id": "keyboard_false",
784 "method_name": "last_event_was_keyboard",
785 "event_class_name": "MouseButtonEvent",
786 "expected_result": False,
787 },
788 {
789 "id": "mouse_button_true",
790 "method_name": "last_event_was_mouse_button",
791 "event_class_name": "MouseButtonEvent",
792 "expected_result": True,
793 },
794 {
795 "id": "mouse_button_false",
796 "method_name": "last_event_was_mouse_button",
797 "event_class_name": "KeyboardEvent",
798 "expected_result": False,
799 },
800 ],
801 ids=lambda case: case["id"],
802 )
803 def test_last_event_was_basic_event_types(
804 self,
805 test_context: OrcaTestContext,
806 case: dict,
807 ) -> None:
808 """Test InputEventManager basic event type detection methods."""
809
810 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
811
812 if case["event_class_name"] == "KeyboardEvent":
813 event_class = essential_modules["orca.input_event"].KeyboardEvent
814 mock_event = event_class()
815 elif case["event_class_name"] == "MouseButtonEvent":
816 event_class = essential_modules["orca.input_event"].MouseButtonEvent
817 mock_event = event_class()
818 else:
819 mock_event = test_context.Mock()
820
821 input_event_manager._last_input_event = mock_event
822 method = getattr(input_event_manager, case["method_name"])
823 result = method()
824 assert result is case["expected_result"]
825
826 @pytest.mark.parametrize(
827 "case",
828 [
829 {
830 "id": "event1_none",
831 "event1": None,
832 "event2": "mock_event",
833 "event1_pressed": None,
834 "event2_pressed": None,
835 "same_id": None,
836 "same_hw_code": None,
837 "same_keyval": None,
838 "event1_modifier": None,
839 "same_modifiers": None,
840 "expected_result": False,
841 },
842 {
843 "id": "event2_none",
844 "event1": "mock_event",
845 "event2": None,
846 "event1_pressed": None,
847 "event2_pressed": None,
848 "same_id": None,
849 "same_hw_code": None,
850 "same_keyval": None,
851 "event1_modifier": None,
852 "same_modifiers": None,
853 "expected_result": False,
854 },
855 {
856 "id": "release_for_press",
857 "event1": "mock_event1",
858 "event2": "mock_event2",
859 "event1_pressed": False,
860 "event2_pressed": True,
861 "same_id": True,
862 "same_hw_code": True,
863 "same_keyval": True,
864 "event1_modifier": False,
865 "same_modifiers": True,
866 "expected_result": True,
867 },
868 {
869 "id": "both_pressed",
870 "event1": "mock_event1",
871 "event2": "mock_event2",
872 "event1_pressed": True,
873 "event2_pressed": True,
874 "same_id": True,
875 "same_hw_code": True,
876 "same_keyval": True,
877 "event1_modifier": False,
878 "same_modifiers": True,
879 "expected_result": False,
880 },
881 {
882 "id": "both_released",
883 "event1": "mock_event1",
884 "event2": "mock_event2",
885 "event1_pressed": False,
886 "event2_pressed": False,
887 "same_id": True,
888 "same_hw_code": True,
889 "same_keyval": True,
890 "event1_modifier": False,
891 "same_modifiers": True,
892 "expected_result": False,
893 },
894 {
895 "id": "different_id",
896 "event1": "mock_event1",
897 "event2": "mock_event2",
898 "event1_pressed": False,
899 "event2_pressed": True,
900 "same_id": False,
901 "same_hw_code": True,
902 "same_keyval": True,
903 "event1_modifier": False,
904 "same_modifiers": True,
905 "expected_result": False,
906 },
907 {
908 "id": "modifier_key_ignore_modifiers",
909 "event1": "mock_event1",
910 "event2": "mock_event2",
911 "event1_pressed": False,
912 "event2_pressed": True,
913 "same_id": True,
914 "same_hw_code": True,
915 "same_keyval": True,
916 "event1_modifier": True,
917 "same_modifiers": True,
918 "expected_result": True,
919 },
920 {
921 "id": "modifier_key_different_modifiers",
922 "event1": "mock_event1",
923 "event2": "mock_event2",
924 "event1_pressed": False,
925 "event2_pressed": True,
926 "same_id": True,
927 "same_hw_code": True,
928 "same_keyval": True,
929 "event1_modifier": True,
930 "same_modifiers": False,
931 "expected_result": True,
932 },
933 {
934 "id": "non_modifier_different_modifiers",
935 "event1": "mock_event1",
936 "event2": "mock_event2",
937 "event1_pressed": False,
938 "event2_pressed": True,
939 "same_id": True,
940 "same_hw_code": True,
941 "same_keyval": True,
942 "event1_modifier": False,
943 "same_modifiers": False,
944 "expected_result": False,
945 },
946 ],
947 ids=lambda case: case["id"],
948 )
949 def test_is_release_for(
950 self,
951 test_context,
952 case: dict,
953 ) -> None:
954 """Test InputEventManager.is_release_for."""
955
956 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
957 event1 = case["event1"]
958 event2 = case["event2"]
959 if event1 in ("mock_event", "mock_event1"):
960 event1 = test_context.Mock()
961 if event2 in ("mock_event", "mock_event2"):
962 event2 = test_context.Mock()
963 if event1 is not None and event2 is not None:
964 keyboard_event_class = essential_modules["orca.input_event"].KeyboardEvent
965 event1 = keyboard_event_class()
966 event2 = keyboard_event_class()
967 event1.is_pressed_key.return_value = case["event1_pressed"]
968 event2.is_pressed_key.return_value = case["event2_pressed"]
969 event1.id = "test_id" if case["same_id"] else "other_id"
970 event2.id = "test_id"
971 event1.hw_code = 42 if case["same_hw_code"] else 43
972 event2.hw_code = 42
973 event1.keyval_name = "a" if case["same_keyval"] else "b"
974 event2.keyval_name = "a"
975 event1.is_modifier_key.return_value = case["event1_modifier"]
976 event1.modifiers = 4 if case["same_modifiers"] else 8
977 event2.modifiers = 4
978 event1.as_single_line_string.return_value = "event1"
979 event2.as_single_line_string.return_value = "event2"
980 result = input_event_manager.is_release_for(event1, event2)
981 assert result == case["expected_result"]
982
983 def test_last_event_equals_or_is_release_for_event_no_last_event(
984 self,
985 test_context: OrcaTestContext,
986 ) -> None:
987 """Test InputEventManager.last_event_equals_or_is_release_for_event with no last event."""
988
989 input_event_manager, _essential_modules = self._setup_input_event_manager(test_context)
990 mock_event = test_context.Mock()
991 result = input_event_manager.last_event_equals_or_is_release_for_event(mock_event)
992 assert result is False
993
994 def test_last_event_equals_or_is_release_for_event_equal(
995 self,
996 test_context: OrcaTestContext,
997 ) -> None:
998 """Test InputEventManager.last_event_equals_or_is_release_for_event with equal events."""
999
1000 input_event_manager, _essential_modules = self._setup_input_event_manager(test_context)
1001 mock_event = test_context.Mock()
1002 input_event_manager._last_non_modifier_key_event = mock_event
1003 input_event_manager.last_event_was_keyboard = test_context.Mock(return_value=True)
1004 result = input_event_manager.last_event_equals_or_is_release_for_event(mock_event)
1005 assert result is True
1006
1007 def test_last_event_equals_or_is_release_for_event_is_release(
1008 self,
1009 test_context: OrcaTestContext,
1010 ) -> None:
1011 """Test InputEventManager.last_event_equals_or_is_release_for_event with release event."""
1012
1013 input_event_manager, _essential_modules = self._setup_input_event_manager(test_context)
1014 mock_event = test_context.Mock()
1015 mock_last_event = test_context.Mock()
1016 input_event_manager._last_non_modifier_key_event = mock_last_event
1017 input_event_manager.last_event_was_keyboard = test_context.Mock(return_value=True)
1018
1019 input_event_manager.is_release_for = test_context.Mock(return_value=True)
1020 result = input_event_manager.last_event_equals_or_is_release_for_event(mock_event)
1021 assert result is True
1022 input_event_manager.is_release_for.assert_called_once_with(mock_last_event, mock_event)
1023
1024 @pytest.mark.parametrize(
1025 "case",
1026 [
1027 {
1028 "id": "no_last_non_modifier",
1029 "has_last_non_modifier": False,
1030 "last_event_keyboard": True,
1031 "expected_key": "",
1032 "expected_modifiers": 0,
1033 },
1034 {
1035 "id": "last_not_keyboard",
1036 "has_last_non_modifier": True,
1037 "last_event_keyboard": False,
1038 "expected_key": "",
1039 "expected_modifiers": 0,
1040 },
1041 {
1042 "id": "has_both",
1043 "has_last_non_modifier": True,
1044 "last_event_keyboard": True,
1045 "expected_key": "a",
1046 "expected_modifiers": 4,
1047 },
1048 ],
1049 ids=lambda case: case["id"],
1050 )
1051 def test_last_key_and_modifiers(
1052 self,
1053 test_context,
1054 case: dict,
1055 ) -> None:
1056 """Test InputEventManager._last_key_and_modifiers."""
1057
1058 input_event_manager, _essential_modules = self._setup_input_event_manager(test_context)
1059 if case["has_last_non_modifier"]:
1060 mock_last_non_modifier = test_context.Mock()
1061 mock_last_non_modifier.keyval_name = "a"
1062 input_event_manager._last_non_modifier_key_event = mock_last_non_modifier
1063 else:
1064 input_event_manager._last_non_modifier_key_event = None
1065 if case["last_event_keyboard"]:
1066 mock_last_event = test_context.Mock()
1067 mock_last_event.modifiers = 4
1068 input_event_manager._last_input_event = mock_last_event
1069 input_event_manager.last_event_was_keyboard = test_context.Mock(
1070 return_value=case["last_event_keyboard"],
1071 )
1072 result = input_event_manager._last_key_and_modifiers()
1073 assert result == (case["expected_key"], case["expected_modifiers"])
1074
1075 @pytest.mark.parametrize(
1076 "case",
1077 [
1078 {
1079 "id": "command_true",
1080 "method_name": "last_event_was_command",
1081 "key_modifiers": ("a", 1 << 2),
1082 "expected_result": True,
1083 "expects_debug": True,
1084 },
1085 {
1086 "id": "command_false",
1087 "method_name": "last_event_was_command",
1088 "key_modifiers": ("a", 0),
1089 "expected_result": False,
1090 "expects_debug": False,
1091 },
1092 ],
1093 ids=lambda case: case["id"],
1094 )
1095 def test_last_event_was_command(
1096 self,
1097 test_context: OrcaTestContext,
1098 case: dict,
1099 ) -> None:
1100 """Test InputEventManager command detection methods."""
1101
1102 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1103 input_event_manager._last_key_and_modifiers = test_context.Mock(
1104 return_value=case["key_modifiers"],
1105 )
1106 method = getattr(input_event_manager, case["method_name"])
1107 result = method()
1108 assert result is case["expected_result"]
1109
1110 if case["expects_debug"]:
1111 essential_modules["orca.debug"].print_message.assert_called()
1112 else:
1113 essential_modules["orca.debug"].print_message.assert_not_called()
1114
1115 @pytest.mark.parametrize(
1116 "case",
1117 [
1118 {
1119 "id": "empty_key_string",
1120 "key_string": "",
1121 "has_shortcut": False,
1122 "expected_result": False,
1123 },
1124 {
1125 "id": "matching_shortcut",
1126 "key_string": "f",
1127 "has_shortcut": True,
1128 "expected_result": True,
1129 },
1130 {
1131 "id": "no_matching_shortcut",
1132 "key_string": "f",
1133 "has_shortcut": False,
1134 "expected_result": False,
1135 },
1136 ],
1137 ids=lambda case: case["id"],
1138 )
1139 def test_last_event_was_shortcut_for(
1140 self,
1141 test_context,
1142 case: dict,
1143 ) -> None:
1144 """Test InputEventManager.last_event_was_shortcut_for."""
1145
1146 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1147 mock_obj = test_context.Mock()
1148 input_event_manager._last_key_and_modifiers = test_context.Mock(
1149 return_value=(case["key_string"], 0),
1150 )
1151 ax_utilities_class = essential_modules["orca.ax_utilities"].AXUtilities
1152 ax_utilities_class.has_matching_shortcut.return_value = case["has_shortcut"]
1153 result = input_event_manager.last_event_was_shortcut_for(mock_obj)
1154 assert result == case["expected_result"]
1155 if case["expected_result"]:
1156 essential_modules["orca.debug"].print_tokens.assert_called()
1157
1158 @pytest.mark.parametrize(
1159 "case",
1160 [
1161 {
1162 "id": "printable_key_true",
1163 "is_keyboard": True,
1164 "is_printable": True,
1165 "expected_result": True,
1166 "expects_debug": True,
1167 },
1168 {
1169 "id": "not_keyboard",
1170 "is_keyboard": False,
1171 "is_printable": None,
1172 "expected_result": False,
1173 "expects_debug": False,
1174 },
1175 {
1176 "id": "not_printable",
1177 "is_keyboard": True,
1178 "is_printable": False,
1179 "expected_result": False,
1180 "expects_debug": False,
1181 },
1182 ],
1183 ids=lambda case: case["id"],
1184 )
1185 def test_last_event_was_printable_key(
1186 self,
1187 test_context: OrcaTestContext,
1188 case: dict,
1189 ) -> None:
1190 """Test InputEventManager.last_event_was_printable_key with various scenarios."""
1191
1192 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1193 input_event_manager.last_event_was_keyboard = test_context.Mock(
1194 return_value=case["is_keyboard"],
1195 )
1196
1197 if case["is_keyboard"] and case["is_printable"] is not None:
1198 mock_last_event = test_context.Mock()
1199 mock_last_event.is_printable_key.return_value = case["is_printable"]
1200 input_event_manager._last_input_event = mock_last_event
1201
1202 result = input_event_manager.last_event_was_printable_key()
1203 assert result is case["expected_result"]
1204
1205 if case["expects_debug"]:
1206 essential_modules["orca.debug"].print_message.assert_called()
1207 else:
1208 essential_modules["orca.debug"].print_message.assert_not_called()
1209
1210 def test_last_event_was_caret_navigation(self, test_context: OrcaTestContext) -> None:
1211 """Test InputEventManager.last_event_was_caret_navigation."""
1212
1213 input_event_manager, _essential_modules = self._setup_input_event_manager(test_context)
1214 input_event_manager.last_event_was_character_navigation = test_context.Mock(
1215 return_value=False,
1216 )
1217 input_event_manager.last_event_was_word_navigation = test_context.Mock(return_value=True)
1218 input_event_manager.last_event_was_line_navigation = test_context.Mock(return_value=False)
1219 input_event_manager.last_event_was_line_boundary_navigation = test_context.Mock(
1220 return_value=False,
1221 )
1222 input_event_manager.last_event_was_file_boundary_navigation = test_context.Mock(
1223 return_value=False,
1224 )
1225 input_event_manager.last_event_was_page_navigation = test_context.Mock(return_value=False)
1226 result = input_event_manager.last_event_was_caret_navigation()
1227 assert result is True
1228
1229 @pytest.mark.parametrize(
1230 "case",
1231 [
1232 {
1233 "id": "left_with_shift",
1234 "key_string": "Left",
1235 "modifiers": 1 << 0,
1236 "expected_result": True,
1237 },
1238 {
1239 "id": "right_with_shift",
1240 "key_string": "Right",
1241 "modifiers": 1 << 0,
1242 "expected_result": True,
1243 },
1244 {
1245 "id": "up_with_shift",
1246 "key_string": "Up",
1247 "modifiers": 1 << 0,
1248 "expected_result": True,
1249 },
1250 {
1251 "id": "down_with_shift",
1252 "key_string": "Down",
1253 "modifiers": 1 << 0,
1254 "expected_result": True,
1255 },
1256 {
1257 "id": "left_without_shift",
1258 "key_string": "Left",
1259 "modifiers": 0,
1260 "expected_result": False,
1261 },
1262 {
1263 "id": "non_arrow_with_shift",
1264 "key_string": "a",
1265 "modifiers": 1 << 0,
1266 "expected_result": False,
1267 },
1268 ],
1269 ids=lambda case: case["id"],
1270 )
1271 def test_last_event_was_caret_selection(
1272 self,
1273 test_context,
1274 case: dict,
1275 ) -> None:
1276 """Test InputEventManager.last_event_was_caret_selection."""
1277
1278 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1279 input_event_manager._last_key_and_modifiers = test_context.Mock(
1280 return_value=(case["key_string"], case["modifiers"]),
1281 )
1282 result = input_event_manager.last_event_was_caret_selection()
1283 assert result == case["expected_result"]
1284 if case["expected_result"]:
1285 essential_modules["orca.debug"].print_message.assert_called()
1286
1287 @pytest.mark.parametrize(
1288 "case",
1289 [
1290 {"id": "up_without_shift", "key_string": "Up", "modifiers": 0, "expected_result": True},
1291 {
1292 "id": "left_without_shift",
1293 "key_string": "Left",
1294 "modifiers": 0,
1295 "expected_result": True,
1296 },
1297 {
1298 "id": "down_without_shift",
1299 "key_string": "Down",
1300 "modifiers": 0,
1301 "expected_result": False,
1302 },
1303 {
1304 "id": "right_without_shift",
1305 "key_string": "Right",
1306 "modifiers": 0,
1307 "expected_result": False,
1308 },
1309 {
1310 "id": "up_with_shift",
1311 "key_string": "Up",
1312 "modifiers": 1 << 0,
1313 "expected_result": False,
1314 },
1315 {"id": "non_arrow", "key_string": "a", "modifiers": 0, "expected_result": False},
1316 ],
1317 ids=lambda case: case["id"],
1318 )
1319 def test_last_event_was_backward_caret_navigation(
1320 self,
1321 test_context,
1322 case: dict,
1323 ) -> None:
1324 """Test InputEventManager.last_event_was_backward_caret_navigation."""
1325
1326 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1327 input_event_manager._last_key_and_modifiers = test_context.Mock(
1328 return_value=(case["key_string"], case["modifiers"]),
1329 )
1330 result = input_event_manager.last_event_was_backward_caret_navigation()
1331 assert result == case["expected_result"]
1332 if case["expected_result"]:
1333 essential_modules["orca.debug"].print_message.assert_called()
1334
1335 @pytest.mark.parametrize(
1336 "case",
1337 [
1338 {
1339 "id": "down_without_shift",
1340 "key_string": "Down",
1341 "modifiers": 0,
1342 "expected_result": True,
1343 },
1344 {
1345 "id": "right_without_shift",
1346 "key_string": "Right",
1347 "modifiers": 0,
1348 "expected_result": True,
1349 },
1350 {
1351 "id": "up_without_shift",
1352 "key_string": "Up",
1353 "modifiers": 0,
1354 "expected_result": False,
1355 },
1356 {
1357 "id": "left_without_shift",
1358 "key_string": "Left",
1359 "modifiers": 0,
1360 "expected_result": False,
1361 },
1362 {
1363 "id": "down_with_shift",
1364 "key_string": "Down",
1365 "modifiers": 1 << 0,
1366 "expected_result": False,
1367 },
1368 {"id": "non_arrow", "key_string": "a", "modifiers": 0, "expected_result": False},
1369 ],
1370 ids=lambda case: case["id"],
1371 )
1372 def test_last_event_was_forward_caret_navigation(
1373 self,
1374 test_context,
1375 case: dict,
1376 ) -> None:
1377 """Test InputEventManager.last_event_was_forward_caret_navigation."""
1378
1379 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1380 input_event_manager._last_key_and_modifiers = test_context.Mock(
1381 return_value=(case["key_string"], case["modifiers"]),
1382 )
1383 result = input_event_manager.last_event_was_forward_caret_navigation()
1384 assert result == case["expected_result"]
1385 if case["expected_result"]:
1386 essential_modules["orca.debug"].print_message.assert_called()
1387
1388 @pytest.mark.parametrize(
1389 "case",
1390 [
1391 {
1392 "id": "down_with_shift",
1393 "key_string": "Down",
1394 "modifiers": 1 << 0,
1395 "expected_result": True,
1396 },
1397 {
1398 "id": "right_with_shift",
1399 "key_string": "Right",
1400 "modifiers": 1 << 0,
1401 "expected_result": True,
1402 },
1403 {
1404 "id": "up_with_shift",
1405 "key_string": "Up",
1406 "modifiers": 1 << 0,
1407 "expected_result": False,
1408 },
1409 {
1410 "id": "left_with_shift",
1411 "key_string": "Left",
1412 "modifiers": 1 << 0,
1413 "expected_result": False,
1414 },
1415 {
1416 "id": "down_without_shift",
1417 "key_string": "Down",
1418 "modifiers": 0,
1419 "expected_result": False,
1420 },
1421 {"id": "non_arrow", "key_string": "a", "modifiers": 1 << 0, "expected_result": False},
1422 ],
1423 ids=lambda case: case["id"],
1424 )
1425 def test_last_event_was_forward_caret_selection(
1426 self,
1427 test_context,
1428 case: dict,
1429 ) -> None:
1430 """Test InputEventManager.last_event_was_forward_caret_selection."""
1431
1432 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1433 input_event_manager._last_key_and_modifiers = test_context.Mock(
1434 return_value=(case["key_string"], case["modifiers"]),
1435 )
1436 result = input_event_manager.last_event_was_forward_caret_selection()
1437 assert result == case["expected_result"]
1438 if case["expected_result"]:
1439 essential_modules["orca.debug"].print_message.assert_called()
1440
1441 @pytest.mark.parametrize(
1442 "case",
1443 [
1444 {
1445 "id": "left_no_modifiers",
1446 "key_string": "Left",
1447 "modifiers": 0,
1448 "expected_result": True,
1449 },
1450 {
1451 "id": "right_no_modifiers",
1452 "key_string": "Right",
1453 "modifiers": 0,
1454 "expected_result": True,
1455 },
1456 {
1457 "id": "left_with_ctrl",
1458 "key_string": "Left",
1459 "modifiers": 1 << 2,
1460 "expected_result": False,
1461 },
1462 {
1463 "id": "right_with_alt",
1464 "key_string": "Right",
1465 "modifiers": 1 << 3,
1466 "expected_result": False,
1467 },
1468 {
1469 "id": "non_horizontal_arrow",
1470 "key_string": "Up",
1471 "modifiers": 0,
1472 "expected_result": False,
1473 },
1474 {"id": "non_arrow", "key_string": "a", "modifiers": 0, "expected_result": False},
1475 ],
1476 ids=lambda case: case["id"],
1477 )
1478 def test_last_event_was_character_navigation(
1479 self,
1480 test_context,
1481 case: dict,
1482 ) -> None:
1483 """Test InputEventManager.last_event_was_character_navigation."""
1484
1485 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1486 input_event_manager._last_key_and_modifiers = test_context.Mock(
1487 return_value=(case["key_string"], case["modifiers"]),
1488 )
1489 result = input_event_manager.last_event_was_character_navigation()
1490 assert result == case["expected_result"]
1491 if case["expected_result"]:
1492 essential_modules["orca.debug"].print_message.assert_called()
1493
1494 @pytest.mark.parametrize(
1495 "case",
1496 [
1497 {
1498 "id": "left_with_ctrl",
1499 "key_string": "Left",
1500 "modifiers": 1 << 2,
1501 "expected_result": True,
1502 },
1503 {
1504 "id": "right_with_ctrl",
1505 "key_string": "Right",
1506 "modifiers": 1 << 2,
1507 "expected_result": True,
1508 },
1509 {"id": "left_no_ctrl", "key_string": "Left", "modifiers": 0, "expected_result": False},
1510 {
1511 "id": "right_no_ctrl",
1512 "key_string": "Right",
1513 "modifiers": 0,
1514 "expected_result": False,
1515 },
1516 {
1517 "id": "non_horizontal_arrow",
1518 "key_string": "Up",
1519 "modifiers": 1 << 2,
1520 "expected_result": False,
1521 },
1522 {"id": "non_arrow", "key_string": "a", "modifiers": 1 << 2, "expected_result": False},
1523 ],
1524 ids=lambda case: case["id"],
1525 )
1526 def test_last_event_was_word_navigation(
1527 self,
1528 test_context,
1529 case: dict,
1530 ) -> None:
1531 """Test InputEventManager.last_event_was_word_navigation."""
1532
1533 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1534 input_event_manager._last_key_and_modifiers = test_context.Mock(
1535 return_value=(case["key_string"], case["modifiers"]),
1536 )
1537 result = input_event_manager.last_event_was_word_navigation()
1538 assert result == case["expected_result"]
1539 if case["expected_result"]:
1540 essential_modules["orca.debug"].print_message.assert_called()
1541
1542 @pytest.mark.parametrize(
1543 "case",
1544 [
1545 {
1546 "id": "left_with_ctrl",
1547 "key_string": "Left",
1548 "modifiers": 1 << 2,
1549 "expected_result": True,
1550 },
1551 {
1552 "id": "right_with_ctrl",
1553 "key_string": "Right",
1554 "modifiers": 1 << 2,
1555 "expected_result": False,
1556 },
1557 {"id": "left_no_ctrl", "key_string": "Left", "modifiers": 0, "expected_result": False},
1558 {"id": "non_arrow", "key_string": "a", "modifiers": 1 << 2, "expected_result": False},
1559 ],
1560 ids=lambda case: case["id"],
1561 )
1562 def test_last_event_was_previous_word_navigation(
1563 self,
1564 test_context,
1565 case: dict,
1566 ) -> None:
1567 """Test InputEventManager.last_event_was_previous_word_navigation."""
1568
1569 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1570 input_event_manager._last_key_and_modifiers = test_context.Mock(
1571 return_value=(case["key_string"], case["modifiers"]),
1572 )
1573 result = input_event_manager.last_event_was_previous_word_navigation()
1574 assert result == case["expected_result"]
1575 if case["expected_result"]:
1576 essential_modules["orca.debug"].print_message.assert_called()
1577
1578 @pytest.mark.parametrize(
1579 "case",
1580 [
1581 {
1582 "id": "right_with_ctrl",
1583 "key_string": "Right",
1584 "modifiers": 1 << 2,
1585 "expected_result": True,
1586 },
1587 {
1588 "id": "left_with_ctrl",
1589 "key_string": "Left",
1590 "modifiers": 1 << 2,
1591 "expected_result": False,
1592 },
1593 {
1594 "id": "right_no_ctrl",
1595 "key_string": "Right",
1596 "modifiers": 0,
1597 "expected_result": False,
1598 },
1599 {"id": "non_arrow", "key_string": "a", "modifiers": 1 << 2, "expected_result": False},
1600 ],
1601 ids=lambda case: case["id"],
1602 )
1603 def test_last_event_was_next_word_navigation(
1604 self,
1605 test_context,
1606 case: dict,
1607 ) -> None:
1608 """Test InputEventManager.last_event_was_next_word_navigation."""
1609
1610 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1611 input_event_manager._last_key_and_modifiers = test_context.Mock(
1612 return_value=(case["key_string"], case["modifiers"]),
1613 )
1614 result = input_event_manager.last_event_was_next_word_navigation()
1615 assert result == case["expected_result"]
1616 if case["expected_result"]:
1617 essential_modules["orca.debug"].print_message.assert_called()
1618
1619 @pytest.mark.parametrize(
1620 "case",
1621 [
1622 {
1623 "id": "up_multiline_not_controlled",
1624 "key_string": "Up",
1625 "modifiers": 0,
1626 "is_single_line": False,
1627 "is_widget_controlled": False,
1628 "expected_result": True,
1629 },
1630 {
1631 "id": "down_multiline_not_controlled",
1632 "key_string": "Down",
1633 "modifiers": 0,
1634 "is_single_line": False,
1635 "is_widget_controlled": False,
1636 "expected_result": True,
1637 },
1638 {
1639 "id": "up_with_ctrl",
1640 "key_string": "Up",
1641 "modifiers": 1 << 2,
1642 "is_single_line": False,
1643 "is_widget_controlled": False,
1644 "expected_result": False,
1645 },
1646 {
1647 "id": "up_single_line",
1648 "key_string": "Up",
1649 "modifiers": 0,
1650 "is_single_line": True,
1651 "is_widget_controlled": False,
1652 "expected_result": False,
1653 },
1654 {
1655 "id": "up_widget_controlled",
1656 "key_string": "Up",
1657 "modifiers": 0,
1658 "is_single_line": False,
1659 "is_widget_controlled": True,
1660 "expected_result": False,
1661 },
1662 {
1663 "id": "non_vertical_arrow",
1664 "key_string": "Left",
1665 "modifiers": 0,
1666 "is_single_line": False,
1667 "is_widget_controlled": False,
1668 "expected_result": False,
1669 },
1670 ],
1671 ids=lambda case: case["id"],
1672 )
1673 def test_last_event_was_line_navigation(
1674 self,
1675 test_context,
1676 case: dict,
1677 ) -> None:
1678 """Test InputEventManager.last_event_was_line_navigation."""
1679
1680 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1681 input_event_manager._last_key_and_modifiers = test_context.Mock(
1682 return_value=(case["key_string"], case["modifiers"]),
1683 )
1684 mock_focus = test_context.Mock()
1685 essential_modules["orca.focus_manager"].get_locus_of_focus.return_value = mock_focus
1686 essential_modules["ax_utilities_class"].is_single_line.return_value = case["is_single_line"]
1687 essential_modules[
1688 "ax_utilities_class"
1689 ].is_widget_controlled_by_line_navigation.return_value = case["is_widget_controlled"]
1690 result = input_event_manager.last_event_was_line_navigation()
1691 assert result == case["expected_result"]
1692 if case["expected_result"]:
1693 essential_modules["orca.debug"].print_message.assert_called()
1694
1695 @pytest.mark.parametrize(
1696 "case",
1697 [
1698 {
1699 "id": "up_with_ctrl_no_shift",
1700 "key_string": "Up",
1701 "modifiers": (1 << 2),
1702 "expected_result": True,
1703 },
1704 {
1705 "id": "down_with_ctrl_no_shift",
1706 "key_string": "Down",
1707 "modifiers": (1 << 2),
1708 "expected_result": True,
1709 },
1710 {
1711 "id": "up_with_ctrl_and_shift",
1712 "key_string": "Up",
1713 "modifiers": (1 << 2) | (1 << 0),
1714 "expected_result": False,
1715 },
1716 {"id": "up_no_ctrl", "key_string": "Up", "modifiers": 0, "expected_result": False},
1717 {
1718 "id": "non_vertical_arrow",
1719 "key_string": "Left",
1720 "modifiers": 1 << 2,
1721 "expected_result": False,
1722 },
1723 ],
1724 ids=lambda case: case["id"],
1725 )
1726 def test_last_event_was_paragraph_navigation(
1727 self,
1728 test_context,
1729 case: dict,
1730 ) -> None:
1731 """Test InputEventManager.last_event_was_paragraph_navigation."""
1732
1733 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1734 input_event_manager._last_key_and_modifiers = test_context.Mock(
1735 return_value=(case["key_string"], case["modifiers"]),
1736 )
1737 result = input_event_manager.last_event_was_paragraph_navigation()
1738 assert result == case["expected_result"]
1739 if case["expected_result"]:
1740 essential_modules["orca.debug"].print_message.assert_called()
1741
1742 @pytest.mark.parametrize(
1743 "case",
1744 [
1745 {"id": "home_no_ctrl", "key_string": "Home", "modifiers": 0, "expected_result": True},
1746 {"id": "end_no_ctrl", "key_string": "End", "modifiers": 0, "expected_result": True},
1747 {
1748 "id": "home_with_ctrl",
1749 "key_string": "Home",
1750 "modifiers": 1 << 2,
1751 "expected_result": False,
1752 },
1753 {
1754 "id": "end_with_ctrl",
1755 "key_string": "End",
1756 "modifiers": 1 << 2,
1757 "expected_result": False,
1758 },
1759 {
1760 "id": "non_boundary_key",
1761 "key_string": "Left",
1762 "modifiers": 0,
1763 "expected_result": False,
1764 },
1765 ],
1766 ids=lambda case: case["id"],
1767 )
1768 def test_last_event_was_line_boundary_navigation(
1769 self,
1770 test_context,
1771 case: dict,
1772 ) -> None:
1773 """Test InputEventManager.last_event_was_line_boundary_navigation."""
1774
1775 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1776 input_event_manager._last_key_and_modifiers = test_context.Mock(
1777 return_value=(case["key_string"], case["modifiers"]),
1778 )
1779 result = input_event_manager.last_event_was_line_boundary_navigation()
1780 assert result == case["expected_result"]
1781 if case["expected_result"]:
1782 essential_modules["orca.debug"].print_message.assert_called()
1783
1784 @pytest.mark.parametrize(
1785 "case",
1786 [
1787 {
1788 "id": "home_with_ctrl",
1789 "key_string": "Home",
1790 "modifiers": 1 << 2,
1791 "expected_result": True,
1792 },
1793 {
1794 "id": "end_with_ctrl",
1795 "key_string": "End",
1796 "modifiers": 1 << 2,
1797 "expected_result": True,
1798 },
1799 {"id": "home_no_ctrl", "key_string": "Home", "modifiers": 0, "expected_result": False},
1800 {"id": "end_no_ctrl", "key_string": "End", "modifiers": 0, "expected_result": False},
1801 {
1802 "id": "non_boundary_key",
1803 "key_string": "Left",
1804 "modifiers": 1 << 2,
1805 "expected_result": False,
1806 },
1807 ],
1808 ids=lambda case: case["id"],
1809 )
1810 def test_last_event_was_file_boundary_navigation(
1811 self,
1812 test_context,
1813 case: dict,
1814 ) -> None:
1815 """Test InputEventManager.last_event_was_file_boundary_navigation."""
1816
1817 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1818 input_event_manager._last_key_and_modifiers = test_context.Mock(
1819 return_value=(case["key_string"], case["modifiers"]),
1820 )
1821 result = input_event_manager.last_event_was_file_boundary_navigation()
1822 assert result == case["expected_result"]
1823 if case["expected_result"]:
1824 essential_modules["orca.debug"].print_message.assert_called()
1825
1826 @pytest.mark.parametrize(
1827 "case",
1828 [
1829 {
1830 "id": "page_up_multiline_not_controlled",
1831 "key_string": "Page_Up",
1832 "modifiers": 0,
1833 "is_single_line": False,
1834 "is_widget_controlled": False,
1835 "expected_result": True,
1836 },
1837 {
1838 "id": "page_down_multiline_not_controlled",
1839 "key_string": "Page_Down",
1840 "modifiers": 0,
1841 "is_single_line": False,
1842 "is_widget_controlled": False,
1843 "expected_result": True,
1844 },
1845 {
1846 "id": "page_up_with_ctrl",
1847 "key_string": "Page_Up",
1848 "modifiers": 1 << 2,
1849 "is_single_line": False,
1850 "is_widget_controlled": False,
1851 "expected_result": False,
1852 },
1853 {
1854 "id": "page_up_single_line",
1855 "key_string": "Page_Up",
1856 "modifiers": 0,
1857 "is_single_line": True,
1858 "is_widget_controlled": False,
1859 "expected_result": False,
1860 },
1861 {
1862 "id": "page_up_widget_controlled",
1863 "key_string": "Page_Up",
1864 "modifiers": 0,
1865 "is_single_line": False,
1866 "is_widget_controlled": True,
1867 "expected_result": False,
1868 },
1869 {
1870 "id": "non_page_key",
1871 "key_string": "Left",
1872 "modifiers": 0,
1873 "is_single_line": False,
1874 "is_widget_controlled": False,
1875 "expected_result": False,
1876 },
1877 ],
1878 ids=lambda case: case["id"],
1879 )
1880 def test_last_event_was_page_navigation(
1881 self,
1882 test_context,
1883 case: dict,
1884 ) -> None:
1885 """Test InputEventManager.last_event_was_page_navigation."""
1886
1887 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1888 input_event_manager._last_key_and_modifiers = test_context.Mock(
1889 return_value=(case["key_string"], case["modifiers"]),
1890 )
1891 mock_focus = test_context.Mock()
1892 essential_modules["orca.focus_manager"].get_locus_of_focus.return_value = mock_focus
1893 essential_modules["ax_utilities_class"].is_single_line.return_value = case["is_single_line"]
1894 essential_modules[
1895 "ax_utilities_class"
1896 ].is_widget_controlled_by_line_navigation.return_value = case["is_widget_controlled"]
1897 result = input_event_manager.last_event_was_page_navigation()
1898 assert result == case["expected_result"]
1899 if case["expected_result"]:
1900 essential_modules["orca.debug"].print_message.assert_called()
1901
1902 @pytest.mark.parametrize(
1903 "case",
1904 [
1905 {
1906 "id": "numeric_with_alt",
1907 "key_string": "1",
1908 "modifiers": 1 << 3,
1909 "expected_result": True,
1910 },
1911 {
1912 "id": "numeric_2_with_alt",
1913 "key_string": "2",
1914 "modifiers": 1 << 3,
1915 "expected_result": True,
1916 },
1917 {
1918 "id": "page_up_with_ctrl",
1919 "key_string": "Page_Up",
1920 "modifiers": 1 << 2,
1921 "expected_result": True,
1922 },
1923 {
1924 "id": "page_down_with_ctrl",
1925 "key_string": "Page_Down",
1926 "modifiers": 1 << 2,
1927 "expected_result": True,
1928 },
1929 {"id": "numeric_no_alt", "key_string": "1", "modifiers": 0, "expected_result": False},
1930 {
1931 "id": "page_up_no_ctrl",
1932 "key_string": "Page_Up",
1933 "modifiers": 0,
1934 "expected_result": False,
1935 },
1936 {
1937 "id": "non_numeric_non_page",
1938 "key_string": "a",
1939 "modifiers": 1 << 3,
1940 "expected_result": False,
1941 },
1942 ],
1943 ids=lambda case: case["id"],
1944 )
1945 def test_last_event_was_page_switch(
1946 self,
1947 test_context,
1948 case: dict,
1949 ) -> None:
1950 """Test InputEventManager.last_event_was_page_switch."""
1951
1952 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
1953 input_event_manager._last_key_and_modifiers = test_context.Mock(
1954 return_value=(case["key_string"], case["modifiers"]),
1955 )
1956 result = input_event_manager.last_event_was_page_switch()
1957 assert result == case["expected_result"]
1958 if case["expected_result"]:
1959 essential_modules["orca.debug"].print_message.assert_called()
1960
1961 @pytest.mark.parametrize(
1962 "case",
1963 [
1964 {
1965 "id": "tab_no_modifiers",
1966 "key_string": "Tab",
1967 "modifiers": 0,
1968 "expected_result": True,
1969 },
1970 {
1971 "id": "shift_tab_no_modifiers",
1972 "key_string": "ISO_Left_Tab",
1973 "modifiers": 0,
1974 "expected_result": True,
1975 },
1976 {
1977 "id": "tab_with_ctrl",
1978 "key_string": "Tab",
1979 "modifiers": 1 << 2,
1980 "expected_result": False,
1981 },
1982 {
1983 "id": "tab_with_alt",
1984 "key_string": "Tab",
1985 "modifiers": 1 << 3,
1986 "expected_result": False,
1987 },
1988 {"id": "non_tab_key", "key_string": "a", "modifiers": 0, "expected_result": False},
1989 ],
1990 ids=lambda case: case["id"],
1991 )
1992 def test_last_event_was_tab_navigation(
1993 self,
1994 test_context,
1995 case: dict,
1996 ) -> None:
1997 """Test InputEventManager.last_event_was_tab_navigation."""
1998
1999 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
2000 input_event_manager._last_key_and_modifiers = test_context.Mock(
2001 return_value=(case["key_string"], case["modifiers"]),
2002 )
2003 result = input_event_manager.last_event_was_tab_navigation()
2004 assert result == case["expected_result"]
2005 if case["expected_result"]:
2006 essential_modules["orca.debug"].print_message.assert_called()
2007
2008 @pytest.mark.parametrize(
2009 "case",
2010 [
2011 {
2012 "id": "not_table_header",
2013 "is_table_header": False,
2014 "is_mouse_button": True,
2015 "is_primary_click": True,
2016 "is_keyboard": False,
2017 "is_return_or_space": False,
2018 "expected_result": False,
2019 },
2020 {
2021 "id": "table_header_mouse_primary_click",
2022 "is_table_header": True,
2023 "is_mouse_button": True,
2024 "is_primary_click": True,
2025 "is_keyboard": False,
2026 "is_return_or_space": False,
2027 "expected_result": True,
2028 },
2029 {
2030 "id": "table_header_mouse_not_primary",
2031 "is_table_header": True,
2032 "is_mouse_button": True,
2033 "is_primary_click": False,
2034 "is_keyboard": False,
2035 "is_return_or_space": False,
2036 "expected_result": False,
2037 },
2038 {
2039 "id": "table_header_keyboard_return_space",
2040 "is_table_header": True,
2041 "is_mouse_button": False,
2042 "is_primary_click": False,
2043 "is_keyboard": True,
2044 "is_return_or_space": True,
2045 "expected_result": True,
2046 },
2047 {
2048 "id": "table_header_keyboard_not_return_space",
2049 "is_table_header": True,
2050 "is_mouse_button": False,
2051 "is_primary_click": False,
2052 "is_keyboard": True,
2053 "is_return_or_space": False,
2054 "expected_result": False,
2055 },
2056 {
2057 "id": "table_header_neither_mouse_nor_keyboard",
2058 "is_table_header": True,
2059 "is_mouse_button": False,
2060 "is_primary_click": False,
2061 "is_keyboard": False,
2062 "is_return_or_space": False,
2063 "expected_result": False,
2064 },
2065 ],
2066 ids=lambda case: case["id"],
2067 )
2068 def test_last_event_was_table_sort(
2069 self,
2070 test_context,
2071 case: dict,
2072 ) -> None:
2073 """Test InputEventManager.last_event_was_table_sort."""
2074
2075 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
2076 mock_focus = test_context.Mock()
2077 essential_modules["orca.focus_manager"].get_locus_of_focus.return_value = mock_focus
2078 essential_modules["ax_utilities_class"].is_table_header.return_value = case[
2079 "is_table_header"
2080 ]
2081 input_event_manager.last_event_was_mouse_button = test_context.Mock(
2082 return_value=case["is_mouse_button"],
2083 )
2084 input_event_manager.last_event_was_primary_click = test_context.Mock(
2085 return_value=case["is_primary_click"],
2086 )
2087 input_event_manager.last_event_was_keyboard = test_context.Mock(
2088 return_value=case["is_keyboard"],
2089 )
2090 input_event_manager.last_event_was_return_or_space = test_context.Mock(
2091 return_value=case["is_return_or_space"],
2092 )
2093 result = input_event_manager.last_event_was_table_sort()
2094 assert result == case["expected_result"]
2095 if case["expected_result"]:
2096 essential_modules["orca.debug"].print_message.assert_called()
2097
2098 @pytest.mark.parametrize(
2099 "case",
2100 [
2101 {
2102 "id": "left_no_modifiers",
2103 "key_string": "Left",
2104 "modifiers": 0,
2105 "expected_result": True,
2106 },
2107 {
2108 "id": "right_no_modifiers",
2109 "key_string": "Right",
2110 "modifiers": 0,
2111 "expected_result": True,
2112 },
2113 {"id": "up_no_modifiers", "key_string": "Up", "modifiers": 0, "expected_result": True},
2114 {
2115 "id": "down_no_modifiers",
2116 "key_string": "Down",
2117 "modifiers": 0,
2118 "expected_result": True,
2119 },
2120 {
2121 "id": "left_with_ctrl",
2122 "key_string": "Left",
2123 "modifiers": 1 << 2,
2124 "expected_result": False,
2125 },
2126 {
2127 "id": "left_with_shift",
2128 "key_string": "Left",
2129 "modifiers": 1 << 0,
2130 "expected_result": False,
2131 },
2132 {
2133 "id": "left_with_alt",
2134 "key_string": "Left",
2135 "modifiers": 1 << 3,
2136 "expected_result": False,
2137 },
2138 {
2139 "id": "left_with_orca_modifier",
2140 "key_string": "Left",
2141 "modifiers": 1 << 8,
2142 "expected_result": False,
2143 },
2144 {"id": "non_arrow_key", "key_string": "a", "modifiers": 0, "expected_result": False},
2145 ],
2146 ids=lambda case: case["id"],
2147 )
2148 def test_last_event_was_unmodified_arrow(
2149 self,
2150 test_context,
2151 case: dict,
2152 ) -> None:
2153 """Test InputEventManager.last_event_was_unmodified_arrow."""
2154
2155 input_event_manager, _essential_modules = self._setup_input_event_manager(test_context)
2156 input_event_manager._last_key_and_modifiers = test_context.Mock(
2157 return_value=(case["key_string"], case["modifiers"]),
2158 )
2159 result = input_event_manager.last_event_was_unmodified_arrow()
2160 assert result == case["expected_result"]
2161
2162 @pytest.mark.parametrize(
2163 "case",
2164 [
2165 {
2166 "id": "has_alt_modifier",
2167 "modifiers": 1 << 3,
2168 "expected_result": 8,
2169 }, # Returns bitwise result, not boolean
2170 {"id": "no_alt_modifier", "modifiers": 0, "expected_result": 0},
2171 {
2172 "id": "alt_and_ctrl_modifiers",
2173 "modifiers": (1 << 3) | (1 << 2),
2174 "expected_result": 8,
2175 }, # Alt bit is still 8
2176 ],
2177 ids=lambda case: case["id"],
2178 )
2179 def test_last_event_was_alt_modified(self, test_context, case: dict) -> None:
2180 """Test InputEventManager.last_event_was_alt_modified."""
2181
2182 input_event_manager, _essential_modules = self._setup_input_event_manager(test_context)
2183 input_event_manager._last_key_and_modifiers = test_context.Mock(
2184 return_value=("a", case["modifiers"]),
2185 )
2186 result = input_event_manager.last_event_was_alt_modified()
2187 assert result == case["expected_result"]
2188
2189 @pytest.mark.parametrize(
2190 "case",
2191 [
2192 {"id": "backspace_key", "key_string": "BackSpace", "expected_result": True},
2193 {"id": "delete_key", "key_string": "Delete", "expected_result": False},
2194 {"id": "regular_key", "key_string": "a", "expected_result": False},
2195 ],
2196 ids=lambda case: case["id"],
2197 )
2198 def test_last_event_was_backspace(self, test_context, case: dict) -> None:
2199 """Test InputEventManager.last_event_was_backspace."""
2200
2201 input_event_manager, _essential_modules = self._setup_input_event_manager(test_context)
2202 input_event_manager._last_key_and_modifiers = test_context.Mock(
2203 return_value=(case["key_string"], 0),
2204 )
2205 result = input_event_manager.last_event_was_backspace()
2206 assert result == case["expected_result"]
2207
2208 @pytest.mark.parametrize(
2209 "case",
2210 [
2211 {"id": "down_key", "key_string": "Down", "expected_result": True},
2212 {"id": "up_key", "key_string": "Up", "expected_result": False},
2213 {"id": "regular_key", "key_string": "a", "expected_result": False},
2214 ],
2215 ids=lambda case: case["id"],
2216 )
2217 def test_last_event_was_down(self, test_context, case: dict) -> None:
2218 """Test InputEventManager.last_event_was_down."""
2219
2220 input_event_manager, _essential_modules = self._setup_input_event_manager(test_context)
2221 input_event_manager._last_key_and_modifiers = test_context.Mock(
2222 return_value=(case["key_string"], 0),
2223 )
2224 result = input_event_manager.last_event_was_down()
2225 assert result == case["expected_result"]
2226
2227 @pytest.mark.parametrize(
2228 "case",
2229 [
2230 {"id": "f1_key", "key_string": "F1", "expected_result": True},
2231 {"id": "f2_key", "key_string": "F2", "expected_result": False},
2232 {"id": "regular_key", "key_string": "a", "expected_result": False},
2233 ],
2234 ids=lambda case: case["id"],
2235 )
2236 def test_last_event_was_f1(self, test_context, case: dict) -> None:
2237 """Test InputEventManager.last_event_was_f1."""
2238
2239 input_event_manager, _essential_modules = self._setup_input_event_manager(test_context)
2240 input_event_manager._last_key_and_modifiers = test_context.Mock(
2241 return_value=(case["key_string"], 0),
2242 )
2243 result = input_event_manager.last_event_was_f1()
2244 assert result == case["expected_result"]
2245
2246 @pytest.mark.parametrize(
2247 "case",
2248 [
2249 {"id": "left_key", "key_string": "Left", "expected_result": True},
2250 {"id": "right_key", "key_string": "Right", "expected_result": False},
2251 {"id": "regular_key", "key_string": "a", "expected_result": False},
2252 ],
2253 ids=lambda case: case["id"],
2254 )
2255 def test_last_event_was_left(self, test_context, case: dict) -> None:
2256 """Test InputEventManager.last_event_was_left."""
2257
2258 input_event_manager, _essential_modules = self._setup_input_event_manager(test_context)
2259 input_event_manager._last_key_and_modifiers = test_context.Mock(
2260 return_value=(case["key_string"], 0),
2261 )
2262 result = input_event_manager.last_event_was_left()
2263 assert result == case["expected_result"]
2264
2265 @pytest.mark.parametrize(
2266 "case",
2267 [
2268 {
2269 "id": "left_or_right_left_key",
2270 "method_name": "last_event_was_left_or_right",
2271 "key_string": "Left",
2272 "expected_result": True,
2273 },
2274 {
2275 "id": "left_or_right_right_key",
2276 "method_name": "last_event_was_left_or_right",
2277 "key_string": "Right",
2278 "expected_result": True,
2279 },
2280 {
2281 "id": "left_or_right_up_key",
2282 "method_name": "last_event_was_left_or_right",
2283 "key_string": "Up",
2284 "expected_result": False,
2285 },
2286 {
2287 "id": "page_up_or_down_page_up",
2288 "method_name": "last_event_was_page_up_or_page_down",
2289 "key_string": "Page_Up",
2290 "expected_result": True,
2291 },
2292 {
2293 "id": "page_up_or_down_page_down",
2294 "method_name": "last_event_was_page_up_or_page_down",
2295 "key_string": "Page_Down",
2296 "expected_result": True,
2297 },
2298 {
2299 "id": "page_up_or_down_up_key",
2300 "method_name": "last_event_was_page_up_or_page_down",
2301 "key_string": "Up",
2302 "expected_result": False,
2303 },
2304 {
2305 "id": "right_right_key",
2306 "method_name": "last_event_was_right",
2307 "key_string": "Right",
2308 "expected_result": True,
2309 },
2310 {
2311 "id": "right_left_key",
2312 "method_name": "last_event_was_right",
2313 "key_string": "Left",
2314 "expected_result": False,
2315 },
2316 {
2317 "id": "return_return_key",
2318 "method_name": "last_event_was_return",
2319 "key_string": "Return",
2320 "expected_result": True,
2321 },
2322 {
2323 "id": "return_enter_key",
2324 "method_name": "last_event_was_return",
2325 "key_string": "Enter",
2326 "expected_result": False,
2327 },
2328 ],
2329 ids=lambda case: case["id"],
2330 )
2331 def test_single_key_detection(self, test_context, case: dict) -> None:
2332 """Test InputEventManager single key detection methods."""
2333
2334 input_event_manager, _essential_modules = self._setup_input_event_manager(test_context)
2335 input_event_manager._last_key_and_modifiers = test_context.Mock(
2336 return_value=(case["key_string"], 0),
2337 )
2338 method = getattr(input_event_manager, case["method_name"])
2339 result = method()
2340 assert result == case["expected_result"]
2341
2342 @pytest.mark.parametrize(
2343 "case",
2344 [
2345 {
2346 "id": "return_or_space_return",
2347 "method_name": "last_event_was_return_or_space",
2348 "key_string": "Return",
2349 "expected_result": True,
2350 },
2351 {
2352 "id": "return_or_space_space_key",
2353 "method_name": "last_event_was_return_or_space",
2354 "key_string": "space",
2355 "expected_result": True,
2356 },
2357 {
2358 "id": "return_or_space_space_char",
2359 "method_name": "last_event_was_return_or_space",
2360 "key_string": " ",
2361 "expected_result": True,
2362 },
2363 {
2364 "id": "return_or_space_enter",
2365 "method_name": "last_event_was_return_or_space",
2366 "key_string": "Enter",
2367 "expected_result": False,
2368 },
2369 {
2370 "id": "return_tab_space_return",
2371 "method_name": "last_event_was_return_tab_or_space",
2372 "key_string": "Return",
2373 "expected_result": True,
2374 },
2375 {
2376 "id": "return_tab_space_tab",
2377 "method_name": "last_event_was_return_tab_or_space",
2378 "key_string": "Tab",
2379 "expected_result": True,
2380 },
2381 {
2382 "id": "return_tab_space_space_key",
2383 "method_name": "last_event_was_return_tab_or_space",
2384 "key_string": "space",
2385 "expected_result": True,
2386 },
2387 {
2388 "id": "return_tab_space_space_char",
2389 "method_name": "last_event_was_return_tab_or_space",
2390 "key_string": " ",
2391 "expected_result": True,
2392 },
2393 {
2394 "id": "return_tab_space_enter",
2395 "method_name": "last_event_was_return_tab_or_space",
2396 "key_string": "Enter",
2397 "expected_result": False,
2398 },
2399 {
2400 "id": "space_space_character",
2401 "method_name": "last_event_was_space",
2402 "key_string": " ",
2403 "expected_result": True,
2404 },
2405 {
2406 "id": "space_space_key",
2407 "method_name": "last_event_was_space",
2408 "key_string": "space",
2409 "expected_result": True,
2410 },
2411 {
2412 "id": "space_return_key",
2413 "method_name": "last_event_was_space",
2414 "key_string": "Return",
2415 "expected_result": False,
2416 },
2417 {
2418 "id": "tab_tab_key",
2419 "method_name": "last_event_was_tab",
2420 "key_string": "Tab",
2421 "expected_result": True,
2422 },
2423 {
2424 "id": "tab_shift_tab_key",
2425 "method_name": "last_event_was_tab",
2426 "key_string": "ISO_Left_Tab",
2427 "expected_result": False,
2428 },
2429 {
2430 "id": "up_up_key",
2431 "method_name": "last_event_was_up",
2432 "key_string": "Up",
2433 "expected_result": True,
2434 },
2435 {
2436 "id": "up_down_key",
2437 "method_name": "last_event_was_up",
2438 "key_string": "Down",
2439 "expected_result": False,
2440 },
2441 {
2442 "id": "up_or_down_up_key",
2443 "method_name": "last_event_was_up_or_down",
2444 "key_string": "Up",
2445 "expected_result": True,
2446 },
2447 {
2448 "id": "up_or_down_down_key",
2449 "method_name": "last_event_was_up_or_down",
2450 "key_string": "Down",
2451 "expected_result": True,
2452 },
2453 {
2454 "id": "up_or_down_left_key",
2455 "method_name": "last_event_was_up_or_down",
2456 "key_string": "Left",
2457 "expected_result": False,
2458 },
2459 ],
2460 ids=lambda case: case["id"],
2461 )
2462 def test_composite_key_detection(self, test_context, case: dict) -> None:
2463 """Test InputEventManager composite key detection methods."""
2464
2465 input_event_manager, _essential_modules = self._setup_input_event_manager(test_context)
2466 input_event_manager._last_key_and_modifiers = test_context.Mock(
2467 return_value=(case["key_string"], 0),
2468 )
2469 method = getattr(input_event_manager, case["method_name"])
2470 result = method()
2471 assert result == case["expected_result"]
2472
2473 @pytest.mark.parametrize(
2474 "case",
2475 [
2476 {
2477 "id": "delete_delete_key",
2478 "method_name": "last_event_was_delete",
2479 "keynames": ["Delete"],
2480 "modifiers": 0,
2481 "expected_result": True,
2482 },
2483 {
2484 "id": "delete_keypad_delete",
2485 "method_name": "last_event_was_delete",
2486 "keynames": ["KP_Delete"],
2487 "modifiers": 0,
2488 "expected_result": True,
2489 },
2490 {
2491 "id": "delete_ctrl_d",
2492 "method_name": "last_event_was_delete",
2493 "keynames": ["d"],
2494 "modifiers": 1 << 2,
2495 "expected_result": True,
2496 },
2497 {
2498 "id": "delete_d_without_ctrl",
2499 "method_name": "last_event_was_delete",
2500 "keynames": ["d"],
2501 "modifiers": 0,
2502 "expected_result": False,
2503 },
2504 {
2505 "id": "delete_unknown_key",
2506 "method_name": "last_event_was_delete",
2507 "keynames": ["x"],
2508 "modifiers": 0,
2509 "expected_result": False,
2510 },
2511 {
2512 "id": "cut_ctrl_x",
2513 "method_name": "last_event_was_cut",
2514 "keynames": ["x"],
2515 "modifiers": (1 << 2),
2516 "expected_result": True,
2517 },
2518 {
2519 "id": "cut_ctrl_shift_x",
2520 "method_name": "last_event_was_cut",
2521 "keynames": ["x"],
2522 "modifiers": (1 << 2) | (1 << 0),
2523 "expected_result": False,
2524 },
2525 {
2526 "id": "cut_x_without_ctrl",
2527 "method_name": "last_event_was_cut",
2528 "keynames": ["x"],
2529 "modifiers": 0,
2530 "expected_result": False,
2531 },
2532 {
2533 "id": "cut_ctrl_y",
2534 "method_name": "last_event_was_cut",
2535 "keynames": ["y"],
2536 "modifiers": 1 << 2,
2537 "expected_result": False,
2538 },
2539 ],
2540 ids=lambda case: case["id"],
2541 )
2542 def test_simple_editing_actions(
2543 self,
2544 test_context,
2545 case: dict,
2546 ) -> None:
2547 """Test InputEventManager simple editing action methods."""
2548
2549 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
2550 input_event_manager._last_key_and_modifiers = test_context.Mock(
2551 return_value=(case["keynames"][0] if case["keynames"] else "", case["modifiers"]),
2552 )
2553 method = getattr(input_event_manager, case["method_name"])
2554 result = method()
2555 assert result == case["expected_result"]
2556 if case["expected_result"]:
2557 essential_modules["orca.debug"].print_message.assert_called()
2558
2559 @pytest.mark.parametrize(
2560 "case",
2561 [
2562 {
2563 "id": "copy_ctrl_c_non_terminal",
2564 "method_name": "last_event_was_copy",
2565 "keynames": ["c"],
2566 "modifiers": (1 << 2),
2567 "is_terminal": False,
2568 "expected_result": True,
2569 },
2570 {
2571 "id": "copy_ctrl_shift_c_terminal",
2572 "method_name": "last_event_was_copy",
2573 "keynames": ["c"],
2574 "modifiers": (1 << 2) | (1 << 0),
2575 "is_terminal": True,
2576 "expected_result": True,
2577 },
2578 {
2579 "id": "copy_ctrl_shift_c_non_terminal",
2580 "method_name": "last_event_was_copy",
2581 "keynames": ["c"],
2582 "modifiers": (1 << 2) | (1 << 0),
2583 "is_terminal": False,
2584 "expected_result": False,
2585 },
2586 {
2587 "id": "copy_ctrl_c_terminal",
2588 "method_name": "last_event_was_copy",
2589 "keynames": ["c"],
2590 "modifiers": (1 << 2),
2591 "is_terminal": True,
2592 "expected_result": False,
2593 },
2594 {
2595 "id": "copy_c_without_ctrl",
2596 "method_name": "last_event_was_copy",
2597 "keynames": ["c"],
2598 "modifiers": 0,
2599 "is_terminal": False,
2600 "expected_result": False,
2601 },
2602 {
2603 "id": "paste_ctrl_v_non_terminal",
2604 "method_name": "last_event_was_paste",
2605 "keynames": ["v"],
2606 "modifiers": (1 << 2),
2607 "is_terminal": False,
2608 "expected_result": True,
2609 },
2610 {
2611 "id": "paste_ctrl_shift_v_terminal",
2612 "method_name": "last_event_was_paste",
2613 "keynames": ["v"],
2614 "modifiers": (1 << 2) | (1 << 0),
2615 "is_terminal": True,
2616 "expected_result": True,
2617 },
2618 {
2619 "id": "paste_ctrl_shift_v_non_terminal",
2620 "method_name": "last_event_was_paste",
2621 "keynames": ["v"],
2622 "modifiers": (1 << 2) | (1 << 0),
2623 "is_terminal": False,
2624 "expected_result": False,
2625 },
2626 {
2627 "id": "paste_ctrl_v_terminal",
2628 "method_name": "last_event_was_paste",
2629 "keynames": ["v"],
2630 "modifiers": (1 << 2),
2631 "is_terminal": True,
2632 "expected_result": False,
2633 },
2634 {
2635 "id": "paste_v_without_ctrl",
2636 "method_name": "last_event_was_paste",
2637 "keynames": ["v"],
2638 "modifiers": 0,
2639 "is_terminal": False,
2640 "expected_result": False,
2641 },
2642 ],
2643 ids=lambda case: case["id"],
2644 )
2645 def test_terminal_aware_editing_actions(
2646 self,
2647 test_context,
2648 case: dict,
2649 ) -> None:
2650 """Test InputEventManager terminal-aware editing action methods."""
2651
2652 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
2653 mock_object = test_context.Mock()
2654 mock_last_event = test_context.Mock()
2655 mock_last_event.get_object.return_value = mock_object
2656 input_event_manager._last_input_event = mock_last_event
2657 input_event_manager._last_key_and_modifiers = test_context.Mock(
2658 return_value=(case["keynames"][0] if case["keynames"] else "", case["modifiers"]),
2659 )
2660 essential_modules["ax_utilities_class"].is_terminal.return_value = case["is_terminal"]
2661 method = getattr(input_event_manager, case["method_name"])
2662 result = method()
2663 assert result == case["expected_result"]
2664 if case["expected_result"]:
2665 essential_modules["orca.debug"].print_message.assert_called()
2666
2667 @pytest.mark.parametrize(
2668 "case",
2669 [
2670 {
2671 "id": "undo_ctrl_z",
2672 "method_name": "last_event_was_undo",
2673 "keynames": ["z"],
2674 "modifiers": (1 << 2),
2675 "expected_result": True,
2676 },
2677 {
2678 "id": "undo_ctrl_shift_z",
2679 "method_name": "last_event_was_undo",
2680 "keynames": ["z"],
2681 "modifiers": (1 << 2) | (1 << 0),
2682 "expected_result": False,
2683 },
2684 {
2685 "id": "undo_z_without_ctrl",
2686 "method_name": "last_event_was_undo",
2687 "keynames": ["z"],
2688 "modifiers": 0,
2689 "expected_result": False,
2690 },
2691 {
2692 "id": "undo_ctrl_y",
2693 "method_name": "last_event_was_undo",
2694 "keynames": ["y"],
2695 "modifiers": 1 << 2,
2696 "expected_result": False,
2697 },
2698 {
2699 "id": "redo_ctrl_shift_z",
2700 "method_name": "last_event_was_redo",
2701 "keynames": ["z"],
2702 "modifiers": (1 << 2) | (1 << 0),
2703 "expected_result": True,
2704 },
2705 {
2706 "id": "redo_ctrl_y_libreoffice",
2707 "method_name": "last_event_was_redo",
2708 "keynames": ["y"],
2709 "modifiers": (1 << 2),
2710 "expected_result": True,
2711 },
2712 {
2713 "id": "redo_ctrl_z",
2714 "method_name": "last_event_was_redo",
2715 "keynames": ["z"],
2716 "modifiers": (1 << 2),
2717 "expected_result": False,
2718 },
2719 {
2720 "id": "redo_ctrl_shift_y",
2721 "method_name": "last_event_was_redo",
2722 "keynames": ["y"],
2723 "modifiers": (1 << 2) | (1 << 0),
2724 "expected_result": False,
2725 },
2726 {
2727 "id": "redo_z_without_ctrl",
2728 "method_name": "last_event_was_redo",
2729 "keynames": ["z"],
2730 "modifiers": 0,
2731 "expected_result": False,
2732 },
2733 {
2734 "id": "redo_unknown_key",
2735 "method_name": "last_event_was_redo",
2736 "keynames": ["x"],
2737 "modifiers": 1 << 2,
2738 "expected_result": False,
2739 },
2740 {
2741 "id": "select_all_ctrl_a",
2742 "method_name": "last_event_was_select_all",
2743 "keynames": ["a"],
2744 "modifiers": (1 << 2),
2745 "expected_result": True,
2746 },
2747 {
2748 "id": "select_all_ctrl_shift_a",
2749 "method_name": "last_event_was_select_all",
2750 "keynames": ["a"],
2751 "modifiers": (1 << 2) | (1 << 0),
2752 "expected_result": False,
2753 },
2754 {
2755 "id": "select_all_a_without_ctrl",
2756 "method_name": "last_event_was_select_all",
2757 "keynames": ["a"],
2758 "modifiers": 0,
2759 "expected_result": False,
2760 },
2761 {
2762 "id": "select_all_unknown_key",
2763 "method_name": "last_event_was_select_all",
2764 "keynames": ["x"],
2765 "modifiers": 1 << 2,
2766 "expected_result": False,
2767 },
2768 ],
2769 ids=lambda case: case["id"],
2770 )
2771 def test_undo_redo_select_editing_actions(
2772 self,
2773 test_context,
2774 case: dict,
2775 ) -> None:
2776 """Test InputEventManager undo/redo/select editing action methods."""
2777
2778 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
2779 input_event_manager._last_key_and_modifiers = test_context.Mock(
2780 return_value=(case["keynames"][0] if case["keynames"] else "", case["modifiers"]),
2781 )
2782 method = getattr(input_event_manager, case["method_name"])
2783 result = method()
2784 assert result == case["expected_result"]
2785 if case["expected_result"]:
2786 essential_modules["orca.debug"].print_message.assert_called()
2787
2788 @pytest.mark.parametrize(
2789 "case",
2790 [
2791 {
2792 "id": "primary_click_primary_click",
2793 "method_name": "last_event_was_primary_click",
2794 "is_mouse_button": True,
2795 "button": "1",
2796 "pressed": True,
2797 "expected_result": True,
2798 },
2799 {
2800 "id": "primary_click_primary_release",
2801 "method_name": "last_event_was_primary_click",
2802 "is_mouse_button": True,
2803 "button": "1",
2804 "pressed": False,
2805 "expected_result": False,
2806 },
2807 {
2808 "id": "primary_click_middle_click",
2809 "method_name": "last_event_was_primary_click",
2810 "is_mouse_button": True,
2811 "button": "2",
2812 "pressed": True,
2813 "expected_result": False,
2814 },
2815 {
2816 "id": "primary_click_not_mouse_button",
2817 "method_name": "last_event_was_primary_click",
2818 "is_mouse_button": False,
2819 "button": "1",
2820 "pressed": True,
2821 "expected_result": False,
2822 },
2823 {
2824 "id": "primary_release_primary_release",
2825 "method_name": "last_event_was_primary_release",
2826 "is_mouse_button": True,
2827 "button": "1",
2828 "pressed": False,
2829 "expected_result": True,
2830 },
2831 {
2832 "id": "primary_release_primary_click",
2833 "method_name": "last_event_was_primary_release",
2834 "is_mouse_button": True,
2835 "button": "1",
2836 "pressed": True,
2837 "expected_result": False,
2838 },
2839 {
2840 "id": "primary_release_middle_release",
2841 "method_name": "last_event_was_primary_release",
2842 "is_mouse_button": True,
2843 "button": "2",
2844 "pressed": False,
2845 "expected_result": False,
2846 },
2847 {
2848 "id": "primary_release_not_mouse_button",
2849 "method_name": "last_event_was_primary_release",
2850 "is_mouse_button": False,
2851 "button": "1",
2852 "pressed": False,
2853 "expected_result": False,
2854 },
2855 ],
2856 ids=lambda case: case["id"],
2857 )
2858 def test_primary_mouse_button_actions(
2859 self,
2860 test_context,
2861 case: dict,
2862 ) -> None:
2863 """Test InputEventManager primary mouse button action methods."""
2864
2865 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
2866 mock_last_event = test_context.Mock()
2867 mock_last_event.button = case["button"]
2868 mock_last_event.pressed = case["pressed"]
2869 input_event_manager._last_input_event = mock_last_event
2870 input_event_manager.last_event_was_mouse_button = test_context.Mock(
2871 return_value=case["is_mouse_button"],
2872 )
2873 method = getattr(input_event_manager, case["method_name"])
2874 result = method()
2875 assert result == case["expected_result"]
2876 if case["expected_result"]:
2877 essential_modules["orca.debug"].print_message.assert_called()
2878
2879 @pytest.mark.parametrize(
2880 "case",
2881 [
2882 {
2883 "id": "primary_button",
2884 "is_mouse_button": True,
2885 "button": "1",
2886 "expected_result": True,
2887 },
2888 {
2889 "id": "middle_button",
2890 "is_mouse_button": True,
2891 "button": "2",
2892 "expected_result": False,
2893 },
2894 {
2895 "id": "secondary_button",
2896 "is_mouse_button": True,
2897 "button": "3",
2898 "expected_result": False,
2899 },
2900 {
2901 "id": "not_mouse_button",
2902 "is_mouse_button": False,
2903 "button": "1",
2904 "expected_result": False,
2905 },
2906 ],
2907 ids=lambda case: case["id"],
2908 )
2909 def test_last_event_was_primary_click_or_release(
2910 self,
2911 test_context,
2912 case: dict,
2913 ) -> None:
2914 """Test InputEventManager.last_event_was_primary_click_or_release."""
2915
2916 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
2917 mock_last_event = test_context.Mock()
2918 mock_last_event.button = case["button"]
2919 input_event_manager._last_input_event = mock_last_event
2920 input_event_manager.last_event_was_mouse_button = test_context.Mock(
2921 return_value=case["is_mouse_button"],
2922 )
2923 result = input_event_manager.last_event_was_primary_click_or_release()
2924 assert result == case["expected_result"]
2925 if case["expected_result"]:
2926 essential_modules["orca.debug"].print_message.assert_called()
2927
2928 @pytest.mark.parametrize(
2929 "case",
2930 [
2931 {
2932 "id": "middle_click_middle_click",
2933 "method_name": "last_event_was_middle_click",
2934 "is_mouse_button": True,
2935 "button": "2",
2936 "pressed": True,
2937 "expected_result": True,
2938 },
2939 {
2940 "id": "middle_click_middle_release",
2941 "method_name": "last_event_was_middle_click",
2942 "is_mouse_button": True,
2943 "button": "2",
2944 "pressed": False,
2945 "expected_result": False,
2946 },
2947 {
2948 "id": "middle_click_primary_click",
2949 "method_name": "last_event_was_middle_click",
2950 "is_mouse_button": True,
2951 "button": "1",
2952 "pressed": True,
2953 "expected_result": False,
2954 },
2955 {
2956 "id": "middle_click_not_mouse_button",
2957 "method_name": "last_event_was_middle_click",
2958 "is_mouse_button": False,
2959 "button": "2",
2960 "pressed": True,
2961 "expected_result": False,
2962 },
2963 {
2964 "id": "middle_release_middle_release",
2965 "method_name": "last_event_was_middle_release",
2966 "is_mouse_button": True,
2967 "button": "2",
2968 "pressed": False,
2969 "expected_result": True,
2970 },
2971 {
2972 "id": "middle_release_middle_click",
2973 "method_name": "last_event_was_middle_release",
2974 "is_mouse_button": True,
2975 "button": "2",
2976 "pressed": True,
2977 "expected_result": False,
2978 },
2979 {
2980 "id": "middle_release_primary_release",
2981 "method_name": "last_event_was_middle_release",
2982 "is_mouse_button": True,
2983 "button": "1",
2984 "pressed": False,
2985 "expected_result": False,
2986 },
2987 {
2988 "id": "middle_release_not_mouse_button",
2989 "method_name": "last_event_was_middle_release",
2990 "is_mouse_button": False,
2991 "button": "2",
2992 "pressed": False,
2993 "expected_result": False,
2994 },
2995 {
2996 "id": "secondary_click_secondary_click",
2997 "method_name": "last_event_was_secondary_click",
2998 "is_mouse_button": True,
2999 "button": "3",
3000 "pressed": True,
3001 "expected_result": True,
3002 },
3003 {
3004 "id": "secondary_click_secondary_release",
3005 "method_name": "last_event_was_secondary_click",
3006 "is_mouse_button": True,
3007 "button": "3",
3008 "pressed": False,
3009 "expected_result": False,
3010 },
3011 {
3012 "id": "secondary_click_primary_click",
3013 "method_name": "last_event_was_secondary_click",
3014 "is_mouse_button": True,
3015 "button": "1",
3016 "pressed": True,
3017 "expected_result": False,
3018 },
3019 {
3020 "id": "secondary_click_not_mouse_button",
3021 "method_name": "last_event_was_secondary_click",
3022 "is_mouse_button": False,
3023 "button": "3",
3024 "pressed": True,
3025 "expected_result": False,
3026 },
3027 {
3028 "id": "secondary_release_secondary_release",
3029 "method_name": "last_event_was_secondary_release",
3030 "is_mouse_button": True,
3031 "button": "3",
3032 "pressed": False,
3033 "expected_result": True,
3034 },
3035 {
3036 "id": "secondary_release_secondary_click",
3037 "method_name": "last_event_was_secondary_release",
3038 "is_mouse_button": True,
3039 "button": "3",
3040 "pressed": True,
3041 "expected_result": False,
3042 },
3043 {
3044 "id": "secondary_release_primary_release",
3045 "method_name": "last_event_was_secondary_release",
3046 "is_mouse_button": True,
3047 "button": "1",
3048 "pressed": False,
3049 "expected_result": False,
3050 },
3051 {
3052 "id": "secondary_release_not_mouse_button",
3053 "method_name": "last_event_was_secondary_release",
3054 "is_mouse_button": False,
3055 "button": "3",
3056 "pressed": False,
3057 "expected_result": False,
3058 },
3059 ],
3060 ids=lambda case: case["id"],
3061 )
3062 def test_middle_secondary_mouse_button_actions(
3063 self,
3064 test_context,
3065 case: dict,
3066 ) -> None:
3067 """Test InputEventManager middle and secondary mouse button action methods."""
3068
3069 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
3070 mock_last_event = test_context.Mock()
3071 mock_last_event.button = case["button"]
3072 mock_last_event.pressed = case["pressed"]
3073 input_event_manager._last_input_event = mock_last_event
3074 input_event_manager.last_event_was_mouse_button = test_context.Mock(
3075 return_value=case["is_mouse_button"],
3076 )
3077 method = getattr(input_event_manager, case["method_name"])
3078 result = method()
3079 assert result == case["expected_result"]
3080 if case["expected_result"]:
3081 essential_modules["orca.debug"].print_message.assert_called()
3082
3083 def test_get_manager(self, test_context: OrcaTestContext) -> None:
3084 """Test get_manager function returns singleton."""
3085
3086 self._setup_input_event_manager(test_context)
3087 from orca.input_event_manager import get_manager
3088
3089 manager1 = get_manager()
3090 manager2 = get_manager()
3091 assert manager1 is manager2
3092
3093 def test_pause_key_watcher_with_debug_message(self, test_context: OrcaTestContext) -> None:
3094 """Test InputEventManager.pause_key_watcher logs debug message."""
3095
3096 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
3097
3098 input_event_manager.pause_key_watcher(True, "Testing pause functionality")
3099
3100 essential_modules["orca.debug"].print_message.assert_called()
3101 assert input_event_manager._paused is True
3102
3103 input_event_manager.pause_key_watcher(False, "Testing unpause functionality")
3104
3105 assert input_event_manager._paused is False
3106
3107 def test_determine_click_count_no_multi_click_bindings(
3108 self,
3109 test_context: OrcaTestContext,
3110 ) -> None:
3111 """Test click count stays at 1 when no multi-click bindings exist for the key."""
3112
3113 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
3114
3115 # Mock command_manager to return False for has_multi_click_bindings
3116 command_manager_instance = test_context.Mock()
3117 command_manager_instance.has_multi_click_bindings = test_context.Mock(return_value=False)
3118 test_context.patch(
3119 "orca.command_manager.get_manager",
3120 return_value=command_manager_instance,
3121 )
3122
3123 # Create first event (press)
3124 first_event = essential_modules["orca.input_event"].KeyboardEvent(
3125 pressed=True,
3126 keycode=79,
3127 keysym=65429,
3128 modifiers=0,
3129 text="KP_Home",
3130 )
3131 first_event.keyval_name = "KP_Home"
3132 first_event.time = 1000.0
3133 first_event.is_pressed_key = test_context.Mock(return_value=True)
3134 first_event.is_modifier_key = test_context.Mock(return_value=False)
3135 first_event.get_click_count = test_context.Mock(return_value=1)
3136 first_event.get_object = test_context.Mock(return_value=None)
3137
3138 # Set first event as last event
3139 input_event_manager._last_input_event = first_event
3140 input_event_manager._last_non_modifier_key_event = first_event
3141
3142 # Create release event
3143 release_event = essential_modules["orca.input_event"].KeyboardEvent(
3144 pressed=False,
3145 keycode=79,
3146 keysym=65429,
3147 modifiers=0,
3148 text="KP_Home",
3149 )
3150 release_event.keyval_name = "KP_Home"
3151 release_event.time = 1000.1
3152 release_event.is_pressed_key = test_context.Mock(return_value=False)
3153 release_event.is_modifier_key = test_context.Mock(return_value=False)
3154 release_event.get_click_count = test_context.Mock(return_value=1)
3155 release_event.get_object = test_context.Mock(return_value=None)
3156
3157 # Set release as last event
3158 input_event_manager._last_input_event = release_event
3159 input_event_manager._last_non_modifier_key_event = release_event
3160
3161 # Create second press event within doubleClickTimeout
3162 second_event = essential_modules["orca.input_event"].KeyboardEvent(
3163 pressed=True,
3164 keycode=79,
3165 keysym=65429,
3166 modifiers=0,
3167 text="KP_Home",
3168 )
3169 second_event.keyval_name = "KP_Home"
3170 second_event.id = 65429 # keyval for has_multi_click_bindings call
3171 second_event.hw_code = 79 # keycode
3172 second_event.modifiers = 0
3173 second_event.time = 1000.3 # Within 0.5s timeout
3174 second_event.is_pressed_key = test_context.Mock(return_value=True)
3175 second_event.is_modifier_key = test_context.Mock(return_value=False)
3176 second_event.is_orca_modifier = test_context.Mock(return_value=False)
3177 second_event.get_object = test_context.Mock(return_value=None)
3178
3179 # Determine click count - should stay at 1 since no multi-click bindings
3180 result = input_event_manager._determine_keyboard_event_click_count(second_event)
3181
3182 assert result == 1
3183 command_manager_instance.has_multi_click_bindings.assert_called_with(65429, 79, 0)
3184
3185 def test_determine_click_count_with_multi_click_bindings(
3186 self,
3187 test_context: OrcaTestContext,
3188 ) -> None:
3189 """Test click count increments when multi-click bindings exist for the key."""
3190
3191 input_event_manager, essential_modules = self._setup_input_event_manager(test_context)
3192
3193 # Mock command_manager to return True for has_multi_click_bindings
3194 command_manager_instance = test_context.Mock()
3195 command_manager_instance.has_multi_click_bindings = test_context.Mock(return_value=True)
3196 test_context.patch(
3197 "orca.command_manager.get_manager",
3198 return_value=command_manager_instance,
3199 )
3200
3201 # Create first event (press)
3202 first_event = essential_modules["orca.input_event"].KeyboardEvent(
3203 pressed=True,
3204 keycode=80,
3205 keysym=65431,
3206 modifiers=0,
3207 text="KP_Up",
3208 )
3209 first_event.keyval_name = "KP_Up"
3210 first_event.time = 1000.0
3211 first_event.is_pressed_key = test_context.Mock(return_value=True)
3212 first_event.is_modifier_key = test_context.Mock(return_value=False)
3213 first_event.get_click_count = test_context.Mock(return_value=1)
3214 first_event.get_object = test_context.Mock(return_value=None)
3215
3216 # Set first event as last event
3217 input_event_manager._last_input_event = first_event
3218 input_event_manager._last_non_modifier_key_event = first_event
3219
3220 # Create release event
3221 release_event = essential_modules["orca.input_event"].KeyboardEvent(
3222 pressed=False,
3223 keycode=80,
3224 keysym=65431,
3225 modifiers=0,
3226 text="KP_Up",
3227 )
3228 release_event.keyval_name = "KP_Up"
3229 release_event.time = 1000.1
3230 release_event.is_pressed_key = test_context.Mock(return_value=False)
3231 release_event.is_modifier_key = test_context.Mock(return_value=False)
3232 release_event.get_click_count = test_context.Mock(return_value=1)
3233 release_event.get_object = test_context.Mock(return_value=None)
3234
3235 # Set release as last event
3236 input_event_manager._last_input_event = release_event
3237 input_event_manager._last_non_modifier_key_event = release_event
3238
3239 # Create second press event within doubleClickTimeout
3240 second_event = essential_modules["orca.input_event"].KeyboardEvent(
3241 pressed=True,
3242 keycode=80,
3243 keysym=65431,
3244 modifiers=0,
3245 text="KP_Up",
3246 )
3247 second_event.keyval_name = "KP_Up"
3248 second_event.id = 65431 # keyval for has_multi_click_bindings call
3249 second_event.hw_code = 80 # keycode
3250 second_event.modifiers = 0
3251 second_event.time = 1000.3 # Within 0.5s timeout
3252 second_event.is_pressed_key = test_context.Mock(return_value=True)
3253 second_event.is_modifier_key = test_context.Mock(return_value=False)
3254 second_event.is_orca_modifier = test_context.Mock(return_value=False)
3255 second_event.get_object = test_context.Mock(return_value=None)
3256
3257 # Determine click count - should increment to 2 since multi-click bindings exist
3258 result = input_event_manager._determine_keyboard_event_click_count(second_event)
3259
3260 assert result == 2
3261 command_manager_instance.has_multi_click_bindings.assert_called_with(65431, 80, 0)