From 1eb1d4b198861bd1c3d999f7dd967902f3a0c294 Mon Sep 17 00:00:00 2001 From: "Peter F. Patel-Schneider" Date: Thu, 4 Apr 2024 12:15:57 -0400 Subject: [PATCH] tests: extend tests for device.py --- tests/logitech_receiver/hidpp.py | 10 ++- tests/logitech_receiver/test_device.py | 63 +++++++++++++++---- .../logitech_receiver/test_hidpp20_complex.py | 6 +- 3 files changed, 63 insertions(+), 16 deletions(-) diff --git a/tests/logitech_receiver/hidpp.py b/tests/logitech_receiver/hidpp.py index 0a74d20e..e52446ab 100644 --- a/tests/logitech_receiver/hidpp.py +++ b/tests/logitech_receiver/hidpp.py @@ -80,25 +80,28 @@ r_keyboard_1 = [ # a HID++ 1.0 keyboard 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 @@ -113,6 +116,7 @@ r_keyboard_2 = [ # a HID++ 2.0 keyboard Response("4142434445464748494A4B4C4D4E4F", 0x0710, "00"), Response("505152530000000000000000000000", 0x0710, "0F"), Response("00", 0x0720), + Response("12345678", 0x0800), # battery status ] r_mouse_1 = [ # a HID++ 1.0 mouse @@ -299,6 +303,7 @@ responses_profiles_rom = [ # OnboardProfile in ROM 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, @@ -307,6 +312,7 @@ complex_responses_1 = [ # COLOR_LED_EFFECTS 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, diff --git a/tests/logitech_receiver/test_device.py b/tests/logitech_receiver/test_device.py index 783fdedd..13a39d8c 100644 --- a/tests/logitech_receiver/test_device.py +++ b/tests/logitech_receiver/test_device.py @@ -251,15 +251,17 @@ class TestDevice(device.Device): # a fully functional Device but its HID++ func [ (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: @@ -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) + + +@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 - - settings - set configuration - reset - persister - battery - set_battery_info - read_battery - enable_connection_notifications changed + enable_connection_notifications add_notification_handler remove_notification_handler handle_notification diff --git a/tests/logitech_receiver/test_hidpp20_complex.py b/tests/logitech_receiver/test_hidpp20_complex.py index 2c912f52..6e9201cd 100644 --- a/tests/logitech_receiver/test_hidpp20_complex.py +++ b/tests/logitech_receiver/test_hidpp20_complex.py @@ -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): @@ -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), ], ), ],