66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
import gi
|
|
|
|
gi.require_version("Gdk", "3.0")
|
|
gi.require_version("Gtk", "3.0")
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
|
|
|
|
soundGeneratorModule = sys.modules.get("cthulhu.sound_generator")
|
|
if soundGeneratorModule is not None and not hasattr(soundGeneratorModule, "SoundGenerator"):
|
|
class _StubSoundGenerator:
|
|
pass
|
|
|
|
soundGeneratorModule.SoundGenerator = _StubSoundGenerator
|
|
|
|
from gi.repository import Gdk
|
|
|
|
from cthulhu import input_event
|
|
|
|
|
|
class KeyboardEventConsumptionTests(unittest.TestCase):
|
|
def test_script_consumed_key_without_handler_is_treated_as_consumed(self):
|
|
testScript = mock.Mock()
|
|
testScript.app = None
|
|
testScript.keyBindings.getInputHandler.return_value = None
|
|
testScript.shouldConsumeKeyboardEvent.return_value = True
|
|
testScript.learnModePresenter.is_active.return_value = False
|
|
testScript.presentKeyboardEvent.return_value = False
|
|
|
|
keyboardEvent = input_event.KeyboardEvent(
|
|
True,
|
|
36,
|
|
Gdk.KEY_Return,
|
|
0,
|
|
"Return",
|
|
)
|
|
keyboardEvent.set_script(testScript)
|
|
keyboardEvent.set_object(None)
|
|
keyboardEvent.set_window(None)
|
|
|
|
with (
|
|
mock.patch("cthulhu.input_event.cthulhu_state.capturingKeys", False),
|
|
mock.patch("cthulhu.input_event.cthulhu_state.bypassNextCommand", False),
|
|
):
|
|
keyboardEvent._finalize_initialization()
|
|
self.assertTrue(keyboardEvent._should_consume)
|
|
self.assertEqual(
|
|
keyboardEvent._consume_reason,
|
|
"Script consumed without handler",
|
|
)
|
|
|
|
didConsume, resultReason = keyboardEvent._process()
|
|
|
|
self.assertTrue(didConsume)
|
|
self.assertEqual(resultReason, "Consumed during shouldConsume")
|
|
testScript.presentationInterrupt.assert_called_once()
|
|
testScript.presentKeyboardEvent.assert_called_once_with(keyboardEvent)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|