Skip to content

Commit

Permalink
Introduce integrationtests for listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
MattHag committed Nov 6, 2024
1 parent b34729b commit 885b0af
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/hidapi/hidapi_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tests/integrationtests/test_device_monitor.py
Original file line number Diff line number Diff line change
@@ -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()
16 changes: 16 additions & 0 deletions tests/integrationtests/test_events_listener.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions tests/integrationtests/test_solaar_listener.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions tests/integrationtests/test_task_runner.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 885b0af

Please sign in to comment.