Skip to content

Commit

Permalink
tests: expand tests for settings_templates
Browse files Browse the repository at this point in the history
  • Loading branch information
pfps committed Mar 30, 2024
1 parent 55290d2 commit 6e683d6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 107 deletions.
77 changes: 0 additions & 77 deletions lib/logitech_receiver/hidpp20.py
Original file line number Diff line number Diff line change
Expand Up @@ -1372,83 +1372,6 @@ def show(self):
_yaml.SafeLoader.add_constructor("!OnboardProfiles", OnboardProfiles.from_yaml)
_yaml.add_representer(OnboardProfiles, OnboardProfiles.to_yaml)

#
#
#


class ExtendedDpi:
"""Information about the DPI possibilities from EXTENDED_ADJUSTABLE_DPI feature"""

def __init__(self, device):
self._device = device
self.has_y = False
reply = device.feature_request(FEATURE.EXTENDED_ADJUSTABLE_DPI.feature, 0x10, 0x00)
self.levels = reply[1]
self.has_y = bool(reply[2] & 0x01)
self.has_lod = bool(reply[2] & 0x02)
self.has_profile = bool(reply[2] & 0x08)
dpilist_x = self.produce_dpi_list(FEATURE.EXTENDED_ADJUSTABLE_DPI, 0x20, device, 0)
dpilist_y = self.produce_dpi_list(FEATURE.EXTENDED_ADJUSTABLE_DPI, 0x20, device, 1) if self.has_y else []
print("DPY LIST X", dpilist_x)
print("DPY LIST Y", dpilist_y)
self.read()

@staticmethod
def produce_dpi_list(feature, function, device, direction):
reply = device.feature_request(feature, function, 0x00, direction, 0x00)
assert reply, "Oops, DPI list cannot be retrieved!"
dpi_bytes = reply[3:]
i = 1
while dpi_bytes[-2:] != b"\x00\x00":
reply = device.feature_request(feature, function, 0x00, direction, i)
assert reply, "Oops, DPI list cannot be retrieved!"
dpi_bytes += reply[3:]
i += 1
dpi_list = []
i = 0
while i < len(dpi_bytes):
val = _bytes2int(dpi_bytes[i : i + 2])
if val == 0:
break
if val >> 13 == 0b111:
step = val & 0x1FFF
last = _bytes2int(dpi_bytes[i + 2 : i + 4])
assert len(dpi_list) > 0 and last > dpi_list[-1], f"Invalid DPI list item: {val!r}"
dpi_list += range(dpi_list[-1] + step, last + 1, step)
i += 4
else:
dpi_list.append(val)
i += 2
return dpi_list

def read_list(self, reply, size):
list = []
for i in range(0, self.levels * size + 1, size):
if reply[i : i + 1] != b"\x00\x00":
list.append(_bytes2int(reply[i : i + size]))
return list

def read(self):
reply = self._device.feature_request(self.feature, 0x50, 0x00)
self.x = _bytes2int(reply[1:3])
self.y = _bytes2int(reply[5:7])
self.lod = reply[9]
self.default_x = _bytes2int(reply[3:5])
self.default_y = _bytes2int(reply[7:9])
self.x_list = self.read_list(self._device.feature_request(self.feature, 0x30, 0x00, 0)[2:], 2)
self.y_list = self.read_list(self._device.feature_request(self.feature, 0x30, 0x00, 1)[2:], 2)
self.lod_list = self.read_list(self._device.feature_request(self.feature, 0x40, 0x00)[1:], 1)

def set(self, x, y, lod):
self.x = x
self.y = y
self.lod = lod

def write_current(self):
data_bytes = _int2bytes(self.x, 2) + _int2bytes(self.y, 2) + _int2bytes(self.lod, 1)
return self._device.feature_request(self.feature, 0x60, 0x00, data_bytes)


def feature_request(device, feature, function=0x00, *params, no_reply=False):
if device.online and device.features:
Expand Down
77 changes: 47 additions & 30 deletions tests/logitech_receiver/test_setting_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,53 +381,28 @@ class FeatureTest:
hidpp.Response("05", 0x0C30, "05"),
),
Setup(
FeatureTest(settings_templates.AdjustableDpi, 800, 400, 0x30, "000190"),
FeatureTest(settings_templates.AdjustableDpi, 800, 400, version=0x03),
common.NamedInts.list([400, 800, 1600]),
hidpp.Response("040003", 0x0000, "2201"), # ADJUSTABLE_DPI
hidpp.Response("000190032006400000", 0x0410, "000000"),
hidpp.Response("000320", 0x0420),
hidpp.Response("000190", 0x0430, "000190"),
),
Setup(
FeatureTest(settings_templates.AdjustableDpi, 256, 512, 0x30, "000200"),
FeatureTest(settings_templates.AdjustableDpi, 256, 512, version=0x03),
common.NamedInts.list([256, 512]),
hidpp.Response("040003", 0x0000, "2201"), # ADJUSTABLE_DPI
hidpp.Response("000100e10002000000", 0x0410, "000000"),
hidpp.Response("000100", 0x0420),
hidpp.Response("000200", 0x0430, "000200"),
),
Setup(
FeatureTest(settings_templates.AdjustableDpi, 400, 800, version=0x03),
common.NamedInts.list([400, 800, 1200, 1600]),
hidpp.Response("000190E19006400000000000000000", 0x0410),
hidpp.Response("000190E19006400000000000000000", 0x0410, "000000"),
hidpp.Response("000190", 0x0420),
hidpp.Response("000320", 0x0430, "000320"),
),
Setup(
FeatureTest(settings_templates.ExtendedAdjustableDpi, 256, 512, 0x60, "000200"),
common.NamedInts.list([256, 512]),
hidpp.Response("090000", 0x0000, "2202"), # EXTENDED_ADJUSTABLE_DPI
hidpp.Response("000000", 0x0910, "00"), # no y direction
hidpp.Response("0000000100e10002000000", 0x0920, "000000"),
hidpp.Response("000100", 0x0950),
hidpp.Response("000200", 0x0960, "000200"),
),
Setup(
FeatureTest(settings_templates.ExtendedAdjustableDpi, 0x64, 0x164, 0x60, "0001640164"),
common.NamedInts.list([0x064, 0x074, 0x084, 0x0A4, 0x0C4, 0x0E4, 0x0124, 0x0164, 0x01C4]),
hidpp.Response("090000", 0x0000, "2202"), # EXTENDED_ADJUSTABLE_DPI
hidpp.Response("000001", 0x0910, "00"), # supports y direction
hidpp.Response("0000000064E0100084E02000C4E02000", 0x0920, "000000"),
hidpp.Response("000001E4E0400124E0400164E06001C4", 0x0920, "000001"),
hidpp.Response("00000000000000000000000000000000", 0x0920, "000002"),
hidpp.Response("0000000064E0100084E02000C4E02000", 0x0920, "000100"),
hidpp.Response("000001E4E0400124E0400164E06001C4", 0x0920, "000101"),
hidpp.Response("00000000000000000000000000000000", 0x0920, "000102"),
hidpp.Response("000064", 0x0950),
hidpp.Response("0001640164", 0x0960, "0001640164"),
),
Setup(
FeatureTest(settings_templates.Multiplatform, 0, 1, 0x30, "FF01"),
FeatureTest(settings_templates.Multiplatform, 0, 1),
common.NamedInts(**{"MacOS 0.1-0.5": 0, "iOS 0.1-0.7": 1, "Linux 0.2-0.9": 2, "Windows 0.3-0.9": 3}),
hidpp.Response("020004000001", 0x0400),
hidpp.Response("00FF200000010005", 0x0410, "00"),
Expand Down Expand Up @@ -620,6 +595,48 @@ def test_simple_template(test, mocker, mock_gethostname):
hidpp.Response("02FF0000", 0x0410, "02FF0000"), # write one value
hidpp.Response("00", 0x0470, "00"), # finish
),
Setup(
FeatureTest(settings_templates.ExtendedAdjustableDpi, {0: 256}, {0: 512}, 2, offset=0x9),
{common.NamedInt(0, "X"): common.NamedInts.list([256, 512])},
hidpp.Response("000000", 0x0910, "00"), # no y direction, no lod
hidpp.Response("0000000100e10002000000", 0x0920, "000000"),
hidpp.Response("00010000000000000000", 0x0950),
hidpp.Response("000100000000", 0x0960, "000100000000"),
hidpp.Response("000200000000", 0x0960, "000200000000"),
),
Setup(
FeatureTest(settings_templates.ExtendedAdjustableDpi, {0: 0x64, 1: 0xE4}, {0: 0x164}, 2, offset=0x9),
{
common.NamedInt(0, "X"): common.NamedInts.list([0x064, 0x074, 0x084, 0x0A4, 0x0C4, 0x0E4, 0x0124, 0x0164, 0x01C4]),
common.NamedInt(1, "Y"): common.NamedInts.list([0x064, 0x074, 0x084, 0x0A4, 0x0C4, 0x0E4, 0x0124, 0x0164]),
},
hidpp.Response("000001", 0x0910, "00"), # supports y direction, no lod
hidpp.Response("0000000064E0100084E02000C4E02000", 0x0920, "000000"),
hidpp.Response("000001E4E0400124E0400164E06001C4", 0x0920, "000001"),
hidpp.Response("00000000000000000000000000000000", 0x0920, "000002"),
hidpp.Response("0000000064E0100084E02000C4E02000", 0x0920, "000100"),
hidpp.Response("000001E4E0400124E040016400000000", 0x0920, "000101"),
hidpp.Response("000064007400E4007400", 0x0950),
hidpp.Response("00006400E400", 0x0960, "00006400E400"),
hidpp.Response("00016400E400", 0x0960, "00016400E400"),
),
Setup(
FeatureTest(settings_templates.ExtendedAdjustableDpi, {0: 0x64, 1: 0xE4, 2: 1}, {1: 0x164}, 2, offset=0x9),
{
common.NamedInt(0, "X"): common.NamedInts.list([0x064, 0x074, 0x084, 0x0A4, 0x0C4, 0x0E4, 0x0124, 0x0164, 0x01C4]),
common.NamedInt(1, "Y"): common.NamedInts.list([0x064, 0x074, 0x084, 0x0A4, 0x0C4, 0x0E4, 0x0124, 0x0164]),
common.NamedInt(2, "LOD"): common.NamedInts(LOW=0, MEDIUM=1, HIGH=2),
},
hidpp.Response("000003", 0x0910, "00"), # supports y direction and lod
hidpp.Response("0000000064E0100084E02000C4E02000", 0x0920, "000000"),
hidpp.Response("000001E4E0400124E0400164E06001C4", 0x0920, "000001"),
hidpp.Response("00000000000000000000000000000000", 0x0920, "000002"),
hidpp.Response("0000000064E0100084E02000C4E02000", 0x0920, "000100"),
hidpp.Response("000001E4E0400124E040016400000000", 0x0920, "000101"),
hidpp.Response("000064007400E4007401", 0x0950),
hidpp.Response("00006400E401", 0x0960, "00006400E401"),
hidpp.Response("000064016401", 0x0960, "000064016401"),
),
]


Expand All @@ -630,7 +647,7 @@ def test_key_template(test, mocker):
spy_request = mocker.spy(device, "request")

setting = settings_templates.check_feature(device, tst.sclass)
assert setting
assert setting is not None
if isinstance(setting, list):
setting = setting[0]
if isinstance(test.choices, dict):
Expand Down

0 comments on commit 6e683d6

Please sign in to comment.