Skip to content

Commit

Permalink
tests: Add hidpp10 tests
Browse files Browse the repository at this point in the history
Related #1097
  • Loading branch information
MattHag committed Feb 29, 2024
1 parent 99f76b0 commit c4a82b9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/logitech_receiver/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

logger = logging.getLogger(__name__)

_hidpp10 = hidpp10.Hidpp10()
_R = hidpp10_constants.REGISTERS
_IR = hidpp10_constants.INFO_SUBREGISTERS

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
42 changes: 42 additions & 0 deletions tests/logitech_receiver/test_hidpp10.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit c4a82b9

Please sign in to comment.