2020-02-07 21:28:29 +01:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import core.input
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-02-07 21:28:29 +01:00
|
|
|
class config(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.inputObject = core.input.Object()
|
|
|
|
self.anotherObject = core.input.Object()
|
2020-05-03 11:15:52 +02:00
|
|
|
self.someEvent = {
|
|
|
|
"button": core.input.LEFT_MOUSE,
|
|
|
|
"instance": self.inputObject.id,
|
|
|
|
}
|
|
|
|
self.anotherEvent = {
|
|
|
|
"button": core.input.RIGHT_MOUSE,
|
|
|
|
"instance": self.inputObject.id,
|
|
|
|
}
|
2020-02-07 21:28:29 +01:00
|
|
|
self.callback = unittest.mock.MagicMock()
|
|
|
|
self.callback2 = unittest.mock.MagicMock()
|
2020-05-03 11:15:52 +02:00
|
|
|
self.someCommand = "some sample command"
|
2020-02-07 21:28:29 +01:00
|
|
|
|
|
|
|
def test_callable_gets_called(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
core.input.register(self.inputObject, self.someEvent["button"], self.callback)
|
2020-02-07 21:28:29 +01:00
|
|
|
core.input.trigger(self.someEvent)
|
|
|
|
self.callback.assert_called_once_with(self.someEvent)
|
|
|
|
|
2020-04-02 16:21:07 +02:00
|
|
|
def test_nonexistent_callback(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
core.input.register(self.inputObject, self.someEvent["button"], self.callback)
|
2020-04-02 16:21:07 +02:00
|
|
|
core.input.trigger(self.anotherEvent)
|
|
|
|
self.callback.assert_not_called()
|
|
|
|
|
2020-02-07 21:28:29 +01:00
|
|
|
def test_different_events(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
core.input.register(self.inputObject, self.someEvent["button"], self.callback)
|
|
|
|
core.input.register(
|
|
|
|
self.inputObject, self.anotherEvent["button"], self.callback
|
|
|
|
)
|
|
|
|
core.input.register(
|
|
|
|
self.anotherObject, self.someEvent["button"], self.callback2
|
|
|
|
)
|
|
|
|
core.input.register(
|
|
|
|
self.anotherObject, self.anotherEvent["button"], self.callback2
|
|
|
|
)
|
2020-02-07 21:28:29 +01:00
|
|
|
core.input.trigger(self.someEvent)
|
|
|
|
core.input.trigger(self.anotherEvent)
|
|
|
|
self.callback.assert_any_call(self.someEvent)
|
|
|
|
self.callback.assert_any_call(self.anotherEvent)
|
|
|
|
self.callback2.assert_not_called()
|
|
|
|
|
|
|
|
def test_multiple_registrations(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
core.input.register(self.inputObject, self.someEvent["button"], self.callback)
|
|
|
|
core.input.register(self.inputObject, self.someEvent["button"], self.callback2)
|
2020-02-07 21:28:29 +01:00
|
|
|
core.input.trigger(self.someEvent)
|
|
|
|
self.callback.assert_called_once_with(self.someEvent)
|
|
|
|
self.callback2.assert_called_once_with(self.someEvent)
|
|
|
|
|
2020-02-08 14:09:14 +01:00
|
|
|
def test_event_names(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(core.input.button_name(core.input.LEFT_MOUSE), "left-mouse")
|
|
|
|
self.assertEqual(core.input.button_name(core.input.RIGHT_MOUSE), "right-mouse")
|
|
|
|
self.assertEqual(
|
|
|
|
core.input.button_name(core.input.MIDDLE_MOUSE), "middle-mouse"
|
|
|
|
)
|
|
|
|
self.assertEqual(core.input.button_name(core.input.WHEEL_UP), "wheel-up")
|
|
|
|
self.assertEqual(core.input.button_name(core.input.WHEEL_DOWN), "wheel-down")
|
|
|
|
self.assertEqual(core.input.button_name(12345), "n/a")
|
2020-02-08 14:09:14 +01:00
|
|
|
|
|
|
|
def test_non_callable_callback(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
with unittest.mock.patch("core.input.util.cli") as cli:
|
|
|
|
cli.execute.return_value = ""
|
|
|
|
core.input.register(
|
|
|
|
self.inputObject, self.someEvent["button"], self.someCommand
|
|
|
|
)
|
2020-02-08 14:09:14 +01:00
|
|
|
core.input.trigger(self.someEvent)
|
|
|
|
cli.execute.assert_called_once_with(self.someCommand, wait=False)
|
|
|
|
|
2020-04-04 08:17:35 +02:00
|
|
|
def test_non_existent_callback(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
with unittest.mock.patch("core.input.util.cli") as cli:
|
|
|
|
cli.execute.return_value = ""
|
|
|
|
cli.execute.side_effect = RuntimeError("some-error")
|
|
|
|
core.input.register(
|
|
|
|
self.inputObject, self.someEvent["button"], self.someCommand
|
|
|
|
)
|
2020-04-04 08:17:35 +02:00
|
|
|
try:
|
|
|
|
core.input.trigger(self.someEvent)
|
|
|
|
except Exception:
|
2020-05-03 11:15:52 +02:00
|
|
|
self.fail("input module propagated exception")
|
|
|
|
|
2020-04-04 08:17:35 +02:00
|
|
|
|
2020-02-07 21:28:29 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|