1 // SPDX-License-Identifier: GPL-2.0+
3 * HID to Linux Input mapping
5 * Copyright (c) 2022 José Expósito <jose.exposito89@gmail.com>
8 #include <kunit/test.h>
10 static void hid_test_input_set_battery_charge_status(struct kunit *test)
12 struct hid_device *dev;
15 dev = kunit_kzalloc(test, sizeof(*dev), GFP_KERNEL);
16 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
18 handled = hidinput_set_battery_charge_status(dev, HID_DG_HEIGHT, 0);
19 KUNIT_EXPECT_FALSE(test, handled);
20 KUNIT_EXPECT_EQ(test, dev->battery_charge_status, POWER_SUPPLY_STATUS_UNKNOWN);
22 handled = hidinput_set_battery_charge_status(dev, HID_BAT_CHARGING, 0);
23 KUNIT_EXPECT_TRUE(test, handled);
24 KUNIT_EXPECT_EQ(test, dev->battery_charge_status, POWER_SUPPLY_STATUS_DISCHARGING);
26 handled = hidinput_set_battery_charge_status(dev, HID_BAT_CHARGING, 1);
27 KUNIT_EXPECT_TRUE(test, handled);
28 KUNIT_EXPECT_EQ(test, dev->battery_charge_status, POWER_SUPPLY_STATUS_CHARGING);
31 static void hid_test_input_get_battery_property(struct kunit *test)
33 struct power_supply *psy;
34 struct hid_device *dev;
35 union power_supply_propval val;
38 dev = kunit_kzalloc(test, sizeof(*dev), GFP_KERNEL);
39 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
40 dev->battery_avoid_query = true;
42 psy = kunit_kzalloc(test, sizeof(*psy), GFP_KERNEL);
43 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, psy);
46 dev->battery_status = HID_BATTERY_UNKNOWN;
47 dev->battery_charge_status = POWER_SUPPLY_STATUS_CHARGING;
48 ret = hidinput_get_battery_property(psy, POWER_SUPPLY_PROP_STATUS, &val);
49 KUNIT_EXPECT_EQ(test, ret, 0);
50 KUNIT_EXPECT_EQ(test, val.intval, POWER_SUPPLY_STATUS_UNKNOWN);
52 dev->battery_status = HID_BATTERY_REPORTED;
53 dev->battery_charge_status = POWER_SUPPLY_STATUS_CHARGING;
54 ret = hidinput_get_battery_property(psy, POWER_SUPPLY_PROP_STATUS, &val);
55 KUNIT_EXPECT_EQ(test, ret, 0);
56 KUNIT_EXPECT_EQ(test, val.intval, POWER_SUPPLY_STATUS_CHARGING);
58 dev->battery_status = HID_BATTERY_REPORTED;
59 dev->battery_charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
60 ret = hidinput_get_battery_property(psy, POWER_SUPPLY_PROP_STATUS, &val);
61 KUNIT_EXPECT_EQ(test, ret, 0);
62 KUNIT_EXPECT_EQ(test, val.intval, POWER_SUPPLY_STATUS_DISCHARGING);
65 static struct kunit_case hid_input_tests[] = {
66 KUNIT_CASE(hid_test_input_set_battery_charge_status),
67 KUNIT_CASE(hid_test_input_get_battery_property),
71 static struct kunit_suite hid_input_test_suite = {
73 .test_cases = hid_input_tests,
76 kunit_test_suite(hid_input_test_suite);
78 MODULE_DESCRIPTION("HID input KUnit tests");
79 MODULE_LICENSE("GPL");
80 MODULE_AUTHOR("José Expósito <jose.exposito89@gmail.com>");