Skip to content

Commit

Permalink
tests: extend tests for device.py
Browse files Browse the repository at this point in the history
  • Loading branch information
pfps committed Apr 12, 2024
1 parent f2f3213 commit cee23bd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
10 changes: 8 additions & 2 deletions tests/logitech_receiver/hidpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,28 @@ def adjust_responses_index(index, responses): # change index-4 responses to ind
Response("003412", 0x81F1, "02"), # firmware
Response("002345", 0x81F1, "03"), # firmware
Response("003456", 0x81F1, "04"), # firmware
Response("050050", 0x8107), # battery status
]

r_keyboard_2 = [ # a HID++ 2.0 keyboard
Response(4.2, 0x0010), # ping
Response("010001", 0x0000, "0001"), # feature set at 0x01
Response("020003", 0x0000, "1000"), # battery status at 0x02
Response("020003", 0x0000, "0020"), # CONFIG_CHANGE at 0x02
Response("030001", 0x0000, "0003"), # device information at 0x03
Response("040003", 0x0000, "0100"), # unknown 0100 at 0x04
Response("050003", 0x0000, "1B04"), # reprogrammable keys V4 at 0x05
Response("060003", 0x0000, "0007"), # device friendly name at 0x06
Response("070003", 0x0000, "0005"), # device name at 0x07
Response("080003", 0x0000, "1000"), # battery status at 0x08
Response("08", 0x0100), # 8 features
Response("00010001", 0x0110, "01"), # feature set at 0x01
Response("10000001", 0x0110, "02"), # battery status at 0x02
Response("00200003", 0x0110, "02"), # CONFIG_CHANGE at 0x02
Response("00030001", 0x0110, "03"), # device information at 0x03
Response("01000003", 0x0110, "04"), # unknown 0100 at 0x04
Response("1B040003", 0x0110, "05"), # reprogrammable keys V4 at 0x05
Response("00070003", 0x0000, "06"), # device friendly name at 0x06
Response("00050003", 0x0000, "07"), # device name at 0x07
Response("10000001", 0x0110, "08"), # battery status at 0x02
Response("0212345678000D1234567890ABAA01", 0x0300), # device information
Response("04", 0x0500), # reprogrammable keys V4
Response("00110012AB010203CD00", 0x0510, "00"), # reprogrammable keys V4
Expand All @@ -113,6 +116,7 @@ def adjust_responses_index(index, responses): # change index-4 responses to ind
Response("4142434445464748494A4B4C4D4E4F", 0x0710, "00"),
Response("505152530000000000000000000000", 0x0710, "0F"),
Response("00", 0x0720),
Response("12345678", 0x0800), # battery status
]

r_mouse_1 = [ # a HID++ 1.0 mouse
Expand Down Expand Up @@ -299,6 +303,7 @@ def adjust_responses_index(index, responses): # change index-4 responses to ind
complex_responses_1 = [ # COLOR_LED_EFFECTS
Response(4.2, 0x0010), # ping
Response("010001", 0x0000, "0001"), # FEATURE SET at x01
Response("020001", 0x0000, "0020"), # CONFIG_CHANGE at x02
Response("0A", 0x0100), # 10 features
Response("070001", 0x0000, "8070"), # COLOR_LED_EFFECTS at 0x07
*effects_responses_1,
Expand All @@ -307,6 +312,7 @@ def adjust_responses_index(index, responses): # change index-4 responses to ind
complex_responses_2 = [ # RGB_EFFECTS + reprogrammable keys + persistent actions
Response(4.2, 0x0010), # ping
Response("010001", 0x0000, "0001"), # FEATURE SET at x01
Response("020001", 0x0000, "0020"), # CONFIG_CHANGE at x02
Response("0A", 0x0100), # 10 features
Response("070001", 0x0000, "8071"), # RGB_EFFECTS at 0x07
*effects_responses_2,
Expand Down
63 changes: 52 additions & 11 deletions tests/logitech_receiver/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,17 @@ def __init__(self, responses, *args, **kwargs):
[
(di_CCCC, hidpp.r_empty, 1.0, type(None), None, None, None, None, None),
(di_C318, hidpp.r_empty, 1.0, type(None), None, None, None, None, None),
(di_B530, hidpp.r_keyboard_1, 2.0, type(None), 0, 0, 0, None, None),
(di_B530, hidpp.r_keyboard_1, 1.0, type(None), None, None, None, None, None),
(di_B530, hidpp.r_keyboard_2, 2.0, type(None), 4, 0, 0, None, None),
(di_B530, hidpp.complex_responses_1, 4.5, hidpp20.LEDEffectsInfo, 0, 0, 0, None, None),
(di_B530, hidpp.complex_responses_2, 4.5, hidpp20.RGBEffectsInfo, 8, 3, 1, True, True),
],
)
def test_Device_complex(device_info, responses, protocol, led, keys, remap, gestures, backlight, profiles):
def test_Device_complex(device_info, responses, protocol, led, keys, remap, gestures, backlight, profiles, mocker):
test_device = TestDevice(responses, None, None, online=True, device_info=device_info)
test_device._name = "TestDevice"
test_device._protocol = protocol
spy_request = mocker.spy(test_device, "request")

assert type(test_device.led_effects) == led
if keys is None:
Expand All @@ -274,18 +276,57 @@ def test_Device_complex(device_info, responses, protocol, led, keys, remap, gest
assert (test_device.backlight is None) == (backlight is None)
assert (test_device.profiles is None) == (profiles is None)

test_device.set_configuration(55)
if protocol > 1.0:
spy_request.assert_called_with(0x210, 55, no_reply=False)
test_device.reset()
if protocol > 1.0:
spy_request.assert_called_with(0x210, 0, no_reply=False)

""" TODO

settings
set configuration
reset
persister
battery
set_battery_info
read_battery
enable_connection_notifications
@pytest.mark.parametrize(
"device_info, responses, protocol, p, persister, settings",
[
(di_CCCC, hidpp.r_empty, 1.0, None, None, 0),
(di_C318, hidpp.r_empty, 1.0, {}, {}, 0),
(di_C318, hidpp.r_keyboard_1, 1.0, {"n": "n"}, {"n": "n"}, 1),
(di_B530, hidpp.r_keyboard_2, 4.5, {"m": "m"}, {"m": "m"}, 1),
(di_C068, hidpp.r_mouse_1, 1.0, {"o": "o"}, {"o": "o"}, 2),
(di_C08A, hidpp.r_mouse_2, 4.5, {"p": "p"}, {"p": "p"}, 0),
],
)
def test_Device_settings(device_info, responses, protocol, p, persister, settings, mocker):
mocker.patch("solaar.configuration.persister", return_value=p)
test_device = TestDevice(responses, None, None, online=True, device_info=device_info)
test_device._name = "TestDevice"
test_device._protocol = protocol

assert test_device.persister == persister
assert len(test_device.settings) == settings


@pytest.mark.parametrize(
"device_info, responses, protocol, battery, changed",
[
(di_C318, hidpp.r_empty, 1.0, None, {"active": True, "alert": 0, "reason": None}),
(di_C318, hidpp.r_keyboard_1, 1.0, common.Battery(50, None, 0, None), {"active": True, "alert": 0, "reason": None}),
(di_B530, hidpp.r_keyboard_2, 4.5, common.Battery(18, 52, None, None), {"active": True, "alert": 0, "reason": None}),
],
)
def test_Device_battery(device_info, responses, protocol, battery, changed, mocker):
test_device = TestDevice(responses, None, None, online=True, device_info=device_info)
test_device._name = "TestDevice"
test_device._protocol = protocol
spy_changed = mocker.spy(test_device, "changed")

assert test_device.battery() == battery
test_device.read_battery()
spy_changed.assert_called_with(**changed)


""" TODO
changed
enable_connection_notifications
add_notification_handler
remove_notification_handler
handle_notification
Expand Down
6 changes: 3 additions & 3 deletions tests/logitech_receiver/test_hidpp20_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_FeaturesArray_check(device, expected_result, expected_count):
"device, expected0, expected1, expected2, expected5, expected5v",
[
(device_zerofeatures, None, None, None, None, None),
(device_standard, 0x0000, 0x0001, 0x1000, hidpp20_constants.FEATURE.REPROG_CONTROLS_V4, 3),
(device_standard, 0x0000, 0x0001, 0x0020, hidpp20_constants.FEATURE.REPROG_CONTROLS_V4, 3),
],
)
def test_FeaturesArray_get_feature(device, expected0, expected1, expected2, expected5, expected5v):
Expand Down Expand Up @@ -94,13 +94,13 @@ def test_FeaturesArray_get_feature(device, expected0, expected1, expected2, expe
[
(hidpp20_constants.FEATURE.ROOT, 0),
(hidpp20_constants.FEATURE.FEATURE_SET, 1),
(hidpp20_constants.FEATURE.BATTERY_STATUS, 2),
(hidpp20_constants.FEATURE.CONFIG_CHANGE, 2),
(hidpp20_constants.FEATURE.DEVICE_FW_VERSION, 3),
(common.NamedInt(256, "unknown:0100"), 4),
(hidpp20_constants.FEATURE.REPROG_CONTROLS_V4, 5),
(None, 6),
(None, 7),
(None, 8),
(hidpp20_constants.FEATURE.BATTERY_STATUS, 8),
],
),
],
Expand Down

0 comments on commit cee23bd

Please sign in to comment.