329 lines
16 KiB
Python
329 lines
16 KiB
Python
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
|
|
|
|
from cthulhu import input_event_manager
|
|
|
|
|
|
class InputEventManagerX11FocusRegressionTests(unittest.TestCase):
|
|
def test_active_x11_window_differs_from_cached_atspi_window_by_pid(self):
|
|
manager = input_event_manager.InputEventManager()
|
|
cachedWindow = object()
|
|
|
|
with (
|
|
mock.patch.object(manager, "_get_active_x11_window_pid", return_value=1002),
|
|
mock.patch.object(input_event_manager.AXObject, "get_process_id", return_value=1001),
|
|
mock.patch.object(input_event_manager.debug, "print_tokens"),
|
|
):
|
|
self.assertTrue(manager._active_x11_window_differs_from(cachedWindow))
|
|
|
|
def test_keyboard_event_preserves_key_handling_when_unknown_x11_window_is_not_xterm(self):
|
|
manager = input_event_manager.InputEventManager()
|
|
staleWindow = object()
|
|
staleFocus = object()
|
|
focusManager = mock.Mock()
|
|
focusManager.get_active_window.return_value = staleWindow
|
|
focusManager.get_locus_of_focus.return_value = staleFocus
|
|
scriptManager = mock.Mock()
|
|
keyboardEvent = mock.Mock()
|
|
keyboardEvent.is_modifier_key.return_value = False
|
|
|
|
with (
|
|
mock.patch.object(input_event_manager.cthulhu_state, "capturingKeys", False),
|
|
mock.patch.object(input_event_manager.focus_manager, "get_manager", return_value=focusManager),
|
|
mock.patch.object(input_event_manager.script_manager, "get_manager", return_value=scriptManager),
|
|
mock.patch.object(input_event_manager.input_event, "KeyboardEvent", return_value=keyboardEvent),
|
|
mock.patch.object(input_event_manager.AXUtilities, "can_be_active_window", return_value=False),
|
|
mock.patch.object(input_event_manager.AXUtilities, "find_active_window", return_value=None),
|
|
mock.patch.object(manager, "_get_top_level_window", return_value=None),
|
|
mock.patch.object(manager, "_should_pass_through_for_active_xterm", return_value=False),
|
|
mock.patch.object(manager, "_active_x11_window_differs_from", return_value=True),
|
|
mock.patch.object(manager, "last_event_was_keyboard", return_value=False),
|
|
mock.patch.object(input_event_manager.debug, "print_message"),
|
|
):
|
|
result = manager.process_keyboard_event(
|
|
mock.Mock(),
|
|
True,
|
|
36,
|
|
65293,
|
|
0,
|
|
"Return",
|
|
)
|
|
|
|
self.assertTrue(result)
|
|
scriptManager.set_active_script.assert_not_called()
|
|
focusManager.clear_state.assert_not_called()
|
|
keyboardEvent.process.assert_called_once_with()
|
|
|
|
def test_keyboard_event_uses_active_script_app_when_cached_window_is_missing(self):
|
|
manager = input_event_manager.InputEventManager()
|
|
staleApp = object()
|
|
staleScript = mock.Mock(app=staleApp)
|
|
focusManager = mock.Mock()
|
|
focusManager.get_active_window.return_value = None
|
|
focusManager.get_locus_of_focus.return_value = None
|
|
scriptManager = mock.Mock()
|
|
scriptManager.get_active_script.return_value = staleScript
|
|
keyboardEvent = mock.Mock()
|
|
keyboardEvent.is_modifier_key.return_value = False
|
|
|
|
with (
|
|
mock.patch.object(input_event_manager.cthulhu_state, "capturingKeys", False),
|
|
mock.patch.object(input_event_manager.focus_manager, "get_manager", return_value=focusManager),
|
|
mock.patch.object(input_event_manager.script_manager, "get_manager", return_value=scriptManager),
|
|
mock.patch.object(input_event_manager.input_event, "KeyboardEvent", return_value=keyboardEvent),
|
|
mock.patch.object(input_event_manager.AXUtilities, "can_be_active_window", return_value=False),
|
|
mock.patch.object(input_event_manager.AXUtilities, "find_active_window", return_value=None),
|
|
mock.patch.object(manager, "_get_top_level_window", return_value=None),
|
|
mock.patch.object(manager, "_should_pass_through_for_active_xterm", return_value=False),
|
|
mock.patch.object(manager, "_active_x11_window_differs_from", return_value=True) as differs,
|
|
mock.patch.object(manager, "last_event_was_keyboard", return_value=False),
|
|
mock.patch.object(input_event_manager.debug, "print_message"),
|
|
):
|
|
result = manager.process_keyboard_event(
|
|
mock.Mock(),
|
|
True,
|
|
81,
|
|
65434,
|
|
0,
|
|
"KP_Page_Up",
|
|
)
|
|
|
|
self.assertTrue(result)
|
|
differs.assert_called_once_with(staleApp)
|
|
scriptManager.set_active_script.assert_not_called()
|
|
focusManager.clear_state.assert_not_called()
|
|
keyboardEvent.process.assert_called_once_with()
|
|
|
|
def test_keyboard_press_keeps_cthulhu_commands_when_unknown_window_is_not_xterm(self):
|
|
manager = input_event_manager.InputEventManager()
|
|
focusManager = mock.Mock()
|
|
focusManager.get_active_window.return_value = None
|
|
focusManager.get_locus_of_focus.return_value = None
|
|
scriptManager = mock.Mock()
|
|
activeScript = mock.Mock(app=None)
|
|
scriptManager.get_active_script.return_value = activeScript
|
|
keyboardEvent = mock.Mock()
|
|
|
|
with (
|
|
mock.patch.object(input_event_manager.cthulhu_state, "capturingKeys", False),
|
|
mock.patch.object(input_event_manager.cthulhu_state, "pendingSelfHostedFocus", None),
|
|
mock.patch.object(input_event_manager.focus_manager, "get_manager", return_value=focusManager),
|
|
mock.patch.object(input_event_manager.script_manager, "get_manager", return_value=scriptManager),
|
|
mock.patch.object(input_event_manager.input_event, "KeyboardEvent", return_value=keyboardEvent),
|
|
mock.patch.object(input_event_manager.AXUtilities, "can_be_active_window", return_value=False),
|
|
mock.patch.object(input_event_manager.AXUtilities, "find_active_window", return_value=None),
|
|
mock.patch.object(manager, "_get_top_level_window", return_value=None),
|
|
mock.patch.object(manager, "_should_pass_through_for_active_xterm", return_value=False),
|
|
mock.patch.object(manager, "_active_x11_window_differs_from", return_value=False),
|
|
mock.patch.object(manager, "last_event_was_keyboard", return_value=False),
|
|
mock.patch.object(input_event_manager.debug, "print_message"),
|
|
):
|
|
result = manager.process_keyboard_event(
|
|
mock.Mock(),
|
|
True,
|
|
79,
|
|
65429,
|
|
0,
|
|
"KP_Home",
|
|
)
|
|
|
|
self.assertTrue(result)
|
|
activeScript.removeKeyGrabs.assert_not_called()
|
|
keyboardEvent.process.assert_called_once_with()
|
|
|
|
def test_keyboard_press_suspends_grabs_and_passes_through_when_active_xterm_is_focused(self):
|
|
manager = input_event_manager.InputEventManager()
|
|
staleWindow = object()
|
|
staleFocus = object()
|
|
focusManager = mock.Mock()
|
|
focusManager.get_active_window.return_value = staleWindow
|
|
focusManager.get_locus_of_focus.return_value = staleFocus
|
|
scriptManager = mock.Mock()
|
|
activeScript = mock.Mock(app=None)
|
|
scriptManager.get_active_script.return_value = activeScript
|
|
keyboardEvent = mock.Mock()
|
|
|
|
with (
|
|
mock.patch.object(input_event_manager.cthulhu_state, "capturingKeys", False),
|
|
mock.patch.object(input_event_manager.cthulhu_state, "pendingSelfHostedFocus", None),
|
|
mock.patch.object(input_event_manager.focus_manager, "get_manager", return_value=focusManager),
|
|
mock.patch.object(input_event_manager.script_manager, "get_manager", return_value=scriptManager),
|
|
mock.patch.object(input_event_manager.input_event, "KeyboardEvent", return_value=keyboardEvent),
|
|
mock.patch.object(manager, "_should_pass_through_for_active_xterm", return_value=True),
|
|
mock.patch.object(input_event_manager.AXUtilities, "can_be_active_window") as canBeActiveWindow,
|
|
mock.patch.object(input_event_manager.AXUtilities, "find_active_window") as findActiveWindow,
|
|
mock.patch.object(input_event_manager.debug, "print_message"),
|
|
):
|
|
result = manager.process_keyboard_event(
|
|
mock.Mock(),
|
|
True,
|
|
79,
|
|
65429,
|
|
0,
|
|
"KP_Home",
|
|
)
|
|
|
|
self.assertFalse(result)
|
|
activeScript.removeKeyGrabs.assert_called_once_with()
|
|
canBeActiveWindow.assert_not_called()
|
|
findActiveWindow.assert_not_called()
|
|
keyboardEvent.process.assert_not_called()
|
|
|
|
def test_finds_focused_atspi_window_for_active_x11_pid(self):
|
|
manager = input_event_manager.InputEventManager()
|
|
app = object()
|
|
unfocusedWindow = object()
|
|
focusedWindow = object()
|
|
|
|
with (
|
|
mock.patch.object(manager, "_get_active_x11_window_pid", return_value=23875),
|
|
mock.patch.object(
|
|
input_event_manager.AXUtilitiesApplication,
|
|
"get_application_with_pid",
|
|
return_value=app,
|
|
),
|
|
mock.patch.object(
|
|
input_event_manager.AXObject,
|
|
"iter_children",
|
|
return_value=iter([unfocusedWindow, focusedWindow]),
|
|
),
|
|
mock.patch.object(input_event_manager.AXUtilities, "is_frame", return_value=True),
|
|
mock.patch.object(input_event_manager.AXUtilities, "is_window", return_value=False),
|
|
mock.patch.object(input_event_manager.AXUtilities, "is_dialog_or_alert", return_value=False),
|
|
mock.patch.object(input_event_manager.AXUtilities, "is_focused", return_value=False),
|
|
mock.patch.object(
|
|
input_event_manager.AXUtilities,
|
|
"get_focused_object",
|
|
side_effect=[None, object()],
|
|
),
|
|
mock.patch.object(input_event_manager.debug, "print_tokens"),
|
|
):
|
|
result = manager._find_active_x11_atspi_window()
|
|
|
|
self.assertIs(result, focusedWindow)
|
|
|
|
def test_keyboard_event_recovers_context_from_active_x11_pid_window(self):
|
|
manager = input_event_manager.InputEventManager()
|
|
recoveredWindow = object()
|
|
focusManager = mock.Mock()
|
|
focusManager.get_active_window.return_value = None
|
|
focusManager.get_locus_of_focus.return_value = None
|
|
scriptManager = mock.Mock()
|
|
activeScript = mock.Mock(app=None)
|
|
scriptManager.get_active_script.return_value = activeScript
|
|
keyboardEvent = mock.Mock()
|
|
keyboardEvent.is_modifier_key.return_value = False
|
|
|
|
with (
|
|
mock.patch.object(input_event_manager.cthulhu_state, "capturingKeys", False),
|
|
mock.patch.object(input_event_manager.cthulhu_state, "pendingSelfHostedFocus", None),
|
|
mock.patch.object(input_event_manager.focus_manager, "get_manager", return_value=focusManager),
|
|
mock.patch.object(input_event_manager.script_manager, "get_manager", return_value=scriptManager),
|
|
mock.patch.object(input_event_manager.input_event, "KeyboardEvent", return_value=keyboardEvent),
|
|
mock.patch.object(input_event_manager.AXUtilities, "can_be_active_window", return_value=False),
|
|
mock.patch.object(input_event_manager.AXUtilities, "find_active_window", return_value=None),
|
|
mock.patch.object(manager, "_find_active_x11_atspi_window", return_value=recoveredWindow),
|
|
mock.patch.object(manager, "last_event_was_keyboard", return_value=False),
|
|
mock.patch.object(input_event_manager.debug, "print_message"),
|
|
mock.patch.object(input_event_manager.debug, "print_tokens"),
|
|
):
|
|
result = manager.process_keyboard_event(
|
|
mock.Mock(),
|
|
True,
|
|
79,
|
|
65429,
|
|
0,
|
|
"KP_Home",
|
|
)
|
|
|
|
self.assertTrue(result)
|
|
focusManager.set_active_window.assert_called_once_with(recoveredWindow)
|
|
activeScript.removeKeyGrabs.assert_not_called()
|
|
keyboardEvent.set_window.assert_called_once_with(recoveredWindow)
|
|
keyboardEvent.process.assert_called_once_with()
|
|
|
|
def test_keyboard_release_suspends_grabs_and_passes_through_when_active_xterm_is_focused(self):
|
|
manager = input_event_manager.InputEventManager()
|
|
focusManager = mock.Mock()
|
|
focusManager.get_active_window.return_value = None
|
|
focusManager.get_locus_of_focus.return_value = None
|
|
scriptManager = mock.Mock()
|
|
activeScript = mock.Mock(app=None)
|
|
scriptManager.get_active_script.return_value = activeScript
|
|
keyboardEvent = mock.Mock()
|
|
|
|
with (
|
|
mock.patch.object(input_event_manager.cthulhu_state, "capturingKeys", False),
|
|
mock.patch.object(input_event_manager.cthulhu_state, "pendingSelfHostedFocus", None),
|
|
mock.patch.object(input_event_manager.focus_manager, "get_manager", return_value=focusManager),
|
|
mock.patch.object(input_event_manager.script_manager, "get_manager", return_value=scriptManager),
|
|
mock.patch.object(input_event_manager.input_event, "KeyboardEvent", return_value=keyboardEvent),
|
|
mock.patch.object(manager, "_should_pass_through_for_active_xterm", return_value=True),
|
|
mock.patch.object(input_event_manager.debug, "print_message"),
|
|
):
|
|
result = manager.process_keyboard_event(
|
|
mock.Mock(),
|
|
False,
|
|
79,
|
|
65429,
|
|
0,
|
|
"KP_Home",
|
|
)
|
|
|
|
self.assertFalse(result)
|
|
activeScript.removeKeyGrabs.assert_called_once_with()
|
|
keyboardEvent.process.assert_not_called()
|
|
|
|
def test_xterm_grab_suspension_is_idempotent(self):
|
|
manager = input_event_manager.InputEventManager()
|
|
activeScript = mock.Mock()
|
|
scriptManager = mock.Mock()
|
|
scriptManager.get_active_script.return_value = activeScript
|
|
|
|
with mock.patch.object(input_event_manager.script_manager, "get_manager", return_value=scriptManager):
|
|
manager._suspend_key_grabs_for_xterm()
|
|
manager._suspend_key_grabs_for_xterm()
|
|
|
|
activeScript.removeKeyGrabs.assert_called_once_with()
|
|
|
|
def test_xterm_grab_restore_readds_grabs_for_same_active_script(self):
|
|
manager = input_event_manager.InputEventManager()
|
|
activeScript = mock.Mock()
|
|
scriptManager = mock.Mock()
|
|
scriptManager.get_active_script.return_value = activeScript
|
|
|
|
with mock.patch.object(input_event_manager.script_manager, "get_manager", return_value=scriptManager):
|
|
manager._suspend_key_grabs_for_xterm()
|
|
manager._restore_key_grabs_after_xterm()
|
|
|
|
activeScript.removeKeyGrabs.assert_called_once_with()
|
|
activeScript.addKeyGrabs.assert_called_once_with()
|
|
|
|
def test_xterm_grab_restore_does_not_readd_grabs_for_inactive_script(self):
|
|
manager = input_event_manager.InputEventManager()
|
|
oldScript = mock.Mock()
|
|
newScript = mock.Mock()
|
|
scriptManager = mock.Mock()
|
|
scriptManager.get_active_script.side_effect = [oldScript, newScript]
|
|
|
|
with mock.patch.object(input_event_manager.script_manager, "get_manager", return_value=scriptManager):
|
|
manager._suspend_key_grabs_for_xterm()
|
|
manager._restore_key_grabs_after_xterm()
|
|
|
|
oldScript.removeKeyGrabs.assert_called_once_with()
|
|
oldScript.addKeyGrabs.assert_not_called()
|
|
|
|
def test_identifier_is_xterm_matches_exact_xterm_only(self):
|
|
self.assertTrue(input_event_manager.InputEventManager._identifier_is_xterm("xterm"))
|
|
self.assertTrue(input_event_manager.InputEventManager._identifier_is_xterm("/usr/bin/xterm"))
|
|
self.assertFalse(input_event_manager.InputEventManager._identifier_is_xterm("uxterm"))
|
|
self.assertFalse(input_event_manager.InputEventManager._identifier_is_xterm("gnome-terminal"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|