xref: /linux/drivers/hid/hid-input-test.c (revision c297aa7d3fb6755890b78b483e82c9cf07370d50)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  HID to Linux Input mapping
4  *
5  *  Copyright (c) 2022 José Expósito <jose.exposito89@gmail.com>
6  */
7 
8 #include <kunit/test.h>
9 
10 static void hid_test_input_set_battery_charge_status(struct kunit *test)
11 {
12 	struct hid_device *dev;
13 	bool handled;
14 
15 	dev = kunit_kzalloc(test, sizeof(*dev), GFP_KERNEL);
16 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
17 
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);
21 
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);
25 
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);
29 }
30 
31 static void hid_test_input_get_battery_property(struct kunit *test)
32 {
33 	struct power_supply *psy;
34 	struct hid_device *dev;
35 	union power_supply_propval val;
36 	int ret;
37 
38 	dev = kunit_kzalloc(test, sizeof(*dev), GFP_KERNEL);
39 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
40 	dev->battery_avoid_query = true;
41 
42 	psy = kunit_kzalloc(test, sizeof(*psy), GFP_KERNEL);
43 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, psy);
44 	psy->drv_data = dev;
45 
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);
51 
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);
57 
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);
63 }
64 
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),
68 	{ }
69 };
70 
71 static struct kunit_suite hid_input_test_suite = {
72 	.name = "hid_input",
73 	.test_cases = hid_input_tests,
74 };
75 
76 kunit_test_suite(hid_input_test_suite);
77 
78 MODULE_DESCRIPTION("HID input KUnit tests");
79 MODULE_LICENSE("GPL");
80 MODULE_AUTHOR("José Expósito <jose.exposito89@gmail.com>");
81