From c4a82b9f7c66ce4dbbcafe1d615ab76d37a6093c Mon Sep 17 00:00:00 2001 From: Matthias Hagmann <16444067+MattHag@users.noreply.github.com> Date: Thu, 29 Feb 2024 00:52:48 +0100 Subject: [PATCH] tests: Add hidpp10 tests Related #1097 --- lib/logitech_receiver/receiver.py | 7 +++-- setup.py | 2 +- tests/logitech_receiver/test_hidpp10.py | 42 +++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 tests/logitech_receiver/test_hidpp10.py diff --git a/lib/logitech_receiver/receiver.py b/lib/logitech_receiver/receiver.py index ade67292e1..758327fcc1 100644 --- a/lib/logitech_receiver/receiver.py +++ b/lib/logitech_receiver/receiver.py @@ -26,6 +26,7 @@ logger = logging.getLogger(__name__) +_hidpp10 = hidpp10.Hidpp10() _R = hidpp10_constants.REGISTERS _IR = hidpp10_constants.INFO_SUBREGISTERS @@ -98,7 +99,7 @@ def __del__(self): @property def firmware(self): if self._firmware is None and self.handle: - self._firmware = hidpp10.get_firmware(self) + self._firmware = _hidpp10.get_firmware(self) return self._firmware # how many pairings remain (None for unknown, -1 for unlimited) @@ -124,12 +125,12 @@ def enable_connection_notifications(self, enable=True): ) else: set_flag_bits = 0 - ok = hidpp10.set_notification_flags(self, set_flag_bits) + ok = _hidpp10.set_notification_flags(self, set_flag_bits) if ok is None: logger.warning("%s: failed to %s receiver notifications", self, "enable" if enable else "disable") return None - flag_bits = hidpp10.get_notification_flags(self) + flag_bits = _hidpp10.get_notification_flags(self) flag_names = None if flag_bits is None else tuple(hidpp10_constants.NOTIFICATION_FLAG.flag_names(flag_bits)) if logger.isEnabledFor(logging.INFO): logger.info("%s: receiver notifications %s => %s", self, "enabled" if enable else "disabled", flag_names) diff --git a/setup.py b/setup.py index 9b40ddce68..80e26e8b16 100755 --- a/setup.py +++ b/setup.py @@ -86,7 +86,7 @@ def _data_files(): "report-descriptor": ["hid-parser"], "desktop-notifications": ["Notify (>= 0.7)"], "git-commit": ["python-git-info"], - "test": ["pytest", "pytest-cov"], + "test": ["pytest", "pytest-mock", "pytest-cov"], "dev": ["ruff"], }, package_dir={"": "lib"}, diff --git a/tests/logitech_receiver/test_hidpp10.py b/tests/logitech_receiver/test_hidpp10.py new file mode 100644 index 0000000000..955c20701b --- /dev/null +++ b/tests/logitech_receiver/test_hidpp10.py @@ -0,0 +1,42 @@ +import pytest + +from logitech_receiver import hidpp10, hidpp10_constants + + +class FakeDevice: + kind = "fake" + online = True + registers = [hidpp10_constants.REGISTERS.three_leds] + + def request(self, *params): + return b"fake request" + + def read_register(self, register_number, *params): + return "fake register" + + +@pytest.fixture +def setup_hidpp10(): + device = FakeDevice() + hid = hidpp10.Hidpp10() + + yield device, hid + + +def test_hidpp10(setup_hidpp10): + device, hid = setup_hidpp10 + + firmwares = hid.get_firmware(device) + + assert len(firmwares) == 3 + for firmware in firmwares: + assert firmware.kind in ["Firmware", "Bootloader", "Other"] + + +def test_set_3leds(setup_hidpp10, mocker): + device, hid = setup_hidpp10 + spy_write_register = mocker.spy(hidpp10, "write_register") + + hid.set_3leds(device) + + spy_write_register.assert_called_once_with(device, hidpp10_constants.REGISTERS.three_leds, 17, 17)