From 885b0afbb5e1e9fc1cd6b2affbf4a80e794924f6 Mon Sep 17 00:00:00 2001 From: MattHag <16444067+MattHag@users.noreply.github.com> Date: Wed, 6 Nov 2024 21:37:26 +0100 Subject: [PATCH] Introduce integrationtests for listeners --- lib/hidapi/hidapi_impl.py | 8 ++++---- tests/integrationtests/test_device_monitor.py | 17 +++++++++++++++++ tests/integrationtests/test_events_listener.py | 16 ++++++++++++++++ tests/integrationtests/test_solaar_listener.py | 15 +++++++++++++++ tests/integrationtests/test_task_runner.py | 15 +++++++++++++++ 5 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 tests/integrationtests/test_device_monitor.py create mode 100644 tests/integrationtests/test_events_listener.py create mode 100644 tests/integrationtests/test_solaar_listener.py create mode 100644 tests/integrationtests/test_task_runner.py diff --git a/lib/hidapi/hidapi_impl.py b/lib/hidapi/hidapi_impl.py index 9e04153a21..062e47e7d9 100644 --- a/lib/hidapi/hidapi_impl.py +++ b/lib/hidapi/hidapi_impl.py @@ -171,7 +171,7 @@ class HIDError(Exception): pass -def _enumerate_devices(): +def _enumerate_devices() -> list: """Returns all HID devices which are potentially useful to us""" devices = [] c_devices = _hidapi.hid_enumerate(0, 0) @@ -201,7 +201,7 @@ def _enumerate_devices(): # Use a separate thread to check if devices have been removed or connected -class _DeviceMonitor(Thread): +class DeviceMonitor(Thread): def __init__(self, device_callback, polling_delay=5.0): self.device_callback = device_callback self.polling_delay = polling_delay @@ -359,11 +359,11 @@ def device_callback(action: str, device): # Removed devices will be detected by Solaar directly pass - monitor = _DeviceMonitor(device_callback=device_callback) + monitor = DeviceMonitor(device_callback=device_callback) monitor.start() -def enumerate(filter_func) -> DeviceInfo: +def enumerate(filter_func: Callable) -> DeviceInfo: """Enumerate the HID Devices. List all the HID devices attached to the system, optionally filtering by diff --git a/tests/integrationtests/test_device_monitor.py b/tests/integrationtests/test_device_monitor.py new file mode 100644 index 0000000000..19ab035067 --- /dev/null +++ b/tests/integrationtests/test_device_monitor.py @@ -0,0 +1,17 @@ +import platform + +import pytest + +from hidapi.hidapi_impl import DeviceMonitor + + +@pytest.mark.skipif(platform.system() == "Linux", reason="Test for non Linux platforms") +def test_device_monitor(mocker): + mock_callback = mocker.Mock() + + monitor = DeviceMonitor(device_callback=mock_callback, polling_delay=0.1) + monitor.start() + + monitor.join(5) + + mock_callback.assert_not_called() diff --git a/tests/integrationtests/test_events_listener.py b/tests/integrationtests/test_events_listener.py new file mode 100644 index 0000000000..8c08c85ae6 --- /dev/null +++ b/tests/integrationtests/test_events_listener.py @@ -0,0 +1,16 @@ +from logitech_receiver.listener import EventsListener + + +def test_events_listener(mocker): + receiver = mocker.MagicMock() + status_callback = mocker.MagicMock() + + e = EventsListener(receiver, status_callback) + e.start() + + assert bool(e) + + e.stop() + + assert not bool(e) + assert status_callback.call_count == 0 diff --git a/tests/integrationtests/test_solaar_listener.py b/tests/integrationtests/test_solaar_listener.py new file mode 100644 index 0000000000..166d601b07 --- /dev/null +++ b/tests/integrationtests/test_solaar_listener.py @@ -0,0 +1,15 @@ +from solaar.listener import SolaarListener + + +def test_solaar_listener(mocker): + receiver = mocker.MagicMock() + receiver.handle = 1 + receiver.path = "dsda" + status_callback = mocker.MagicMock() + + rl = SolaarListener(receiver, status_callback) + # rl.run() + # rl.stop() + + assert not rl.is_alive() + assert status_callback.call_count == 0 diff --git a/tests/integrationtests/test_task_runner.py b/tests/integrationtests/test_task_runner.py new file mode 100644 index 0000000000..0ff7c3e894 --- /dev/null +++ b/tests/integrationtests/test_task_runner.py @@ -0,0 +1,15 @@ +from solaar import tasks + + +def run_task(): + print("Hi!") + + +def test_task_runner(mocker): + tr = tasks.TaskRunner(name="Testrunner") + tr.queue.put((run_task, {}, {})) + # tr.run() + # tr.stop() + # assert tr.alive + # tr.stop() + # assert not tr.alive