Skip to content

Commit

Permalink
improve test_event_reader a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
sezanzeb committed Dec 29, 2024
1 parent 62121fc commit c5e3457
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
1 change: 0 additions & 1 deletion tests/lib/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def quick_cleanup(log=True):
from inputremapper.injection.macros.macro import macro_variables
from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.gui.utils import debounce_manager
from inputremapper.injection.global_uinputs import GlobalUInputs

if log:
logger.info("Quick cleanup...")
Expand Down
35 changes: 23 additions & 12 deletions tests/unit/test_event_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,30 @@
class TestEventReader(unittest.IsolatedAsyncioTestCase):
def setUp(self):
self.gamepad_source = evdev.InputDevice(fixtures.gamepad.path)
self.source_hash = fixtures.gamepad.get_device_hash()
self.global_uinputs = GlobalUInputs(UInput)
self.mapping_parser = MappingParser(self.global_uinputs)
self.stop_event = asyncio.Event()
self.preset = Preset()
self.forward_uinput = evdev.UInput(name="test-forward-uinput")

self.global_uinputs.is_service = True
self.global_uinputs.prepare_all()

async def setup(self, source, mapping):
async def setup(self, mapping):
"""Set a EventReader up for the test and run it in the background."""
context = Context(mapping, {}, {}, self.mapping_parser)
context = Context(
mapping,
{},
{self.source_hash: self.forward_uinput},
self.mapping_parser,
)
context.uinput = evdev.UInput()
event_reader = EventReader(context, source, self.stop_event)
event_reader = EventReader(
context,
self.gamepad_source,
self.stop_event,
)
asyncio.ensure_future(event_reader.run())
await asyncio.sleep(0.1)
return context, event_reader
Expand Down Expand Up @@ -162,7 +173,7 @@ async def test_if_single_joystick_then(self):
config["output_code"] = REL_WHEEL_HI_RES
self.preset.add(Mapping(**config))

context, _ = await self.setup(self.gamepad_source, self.preset)
context, _ = await self.setup(self.preset)

gamepad_hash = get_device_hash(self.gamepad_source)
self.gamepad_source.push_events(
Expand Down Expand Up @@ -228,7 +239,7 @@ async def test_if_single_joystick_under_threshold(self):

# self.preset.set("gamepad.joystick.left_purpose", BUTTONS)
# self.preset.set("gamepad.joystick.right_purpose", BUTTONS)
context, _ = await self.setup(self.gamepad_source, self.preset)
context, _ = await self.setup(self.preset)

self.gamepad_source.push_events(
[
Expand All @@ -255,7 +266,7 @@ async def test_if_single_joystick_under_threshold(self):

@patch.object(Context, "reset")
async def test_reset_handlers_on_stop(self, reset_mock: MagicMock) -> None:
await self.setup(self.gamepad_source, self.preset)
await self.setup(self.preset)

self.stop_event.set()
await asyncio.sleep(0.1)
Expand All @@ -269,7 +280,7 @@ async def test_reset_handlers_after_unplugging(
stat_mock: MagicMock,
reset_mock: MagicMock,
) -> None:
await self.setup(self.gamepad_source, self.preset)
await self.setup(self.preset)

self.gamepad_source.push_events([InputEvent.key(BTN_A, 1)])
await asyncio.sleep(0.1)
Expand All @@ -279,11 +290,11 @@ async def test_reset_handlers_after_unplugging(
stat_mock().st_nlink = 0

# It seems that once a device is unplugged, asyncio stops waiting for new input
# from _source.fileno() or something. This is currently difficult to really
# replicate in tests, because fixtures and pipes and pending_events and stuff
# aren't created from scratch for each test. Deleting the file (to which
# InputDevice.fileno points) would break all other tests. Right now, I'm pushing
# another event to make the EventReader realize that st_nlink is 0. TODO
# from _source.fileno() or something. I didn't manage to replicate this
# behavior in tests using the fd of pending_events[fixtures.gamepad][0].fileno()
# and/or pending_events[fixtures.gamepad][1].fileno(). So instead, I push
# another event, so that the EventReader makes another iteration.
self.gamepad_source.push_events([InputEvent.key(BTN_A, 1)])
await asyncio.sleep(0.1)

reset_mock.assert_called_once()

0 comments on commit c5e3457

Please sign in to comment.