summaryrefslogtreecommitdiff
path: root/drivers/hid/hid-input-test.c
blob: 77c2d45ac62a7f4ada96c995f2c8eb7cf33626ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// SPDX-License-Identifier: GPL-2.0+
/*
 *  HID to Linux Input mapping
 *
 *  Copyright (c) 2022 José Expósito <jose.exposito89@gmail.com>
 */

#include <kunit/test.h>

static void hid_test_input_set_battery_charge_status(struct kunit *test)
{
	struct hid_device *dev;
	bool handled;

	dev = kunit_kzalloc(test, sizeof(*dev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);

	handled = hidinput_set_battery_charge_status(dev, HID_DG_HEIGHT, 0);
	KUNIT_EXPECT_FALSE(test, handled);
	KUNIT_EXPECT_EQ(test, dev->battery_charge_status, POWER_SUPPLY_STATUS_UNKNOWN);

	handled = hidinput_set_battery_charge_status(dev, HID_BAT_CHARGING, 0);
	KUNIT_EXPECT_TRUE(test, handled);
	KUNIT_EXPECT_EQ(test, dev->battery_charge_status, POWER_SUPPLY_STATUS_DISCHARGING);

	handled = hidinput_set_battery_charge_status(dev, HID_BAT_CHARGING, 1);
	KUNIT_EXPECT_TRUE(test, handled);
	KUNIT_EXPECT_EQ(test, dev->battery_charge_status, POWER_SUPPLY_STATUS_CHARGING);
}

static void hid_test_input_get_battery_property(struct kunit *test)
{
	struct power_supply *psy;
	struct hid_device *dev;
	union power_supply_propval val;
	int ret;

	dev = kunit_kzalloc(test, sizeof(*dev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
	dev->battery_avoid_query = true;

	psy = kunit_kzalloc(test, sizeof(*psy), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, psy);
	psy->drv_data = dev;

	dev->battery_status = HID_BATTERY_UNKNOWN;
	dev->battery_charge_status = POWER_SUPPLY_STATUS_CHARGING;
	ret = hidinput_get_battery_property(psy, POWER_SUPPLY_PROP_STATUS, &val);
	KUNIT_EXPECT_EQ(test, ret, 0);
	KUNIT_EXPECT_EQ(test, val.intval, POWER_SUPPLY_STATUS_UNKNOWN);

	dev->battery_status = HID_BATTERY_REPORTED;
	dev->battery_charge_status = POWER_SUPPLY_STATUS_CHARGING;
	ret = hidinput_get_battery_property(psy, POWER_SUPPLY_PROP_STATUS, &val);
	KUNIT_EXPECT_EQ(test, ret, 0);
	KUNIT_EXPECT_EQ(test, val.intval, POWER_SUPPLY_STATUS_CHARGING);

	dev->battery_status = HID_BATTERY_REPORTED;
	dev->battery_charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
	ret = hidinput_get_battery_property(psy, POWER_SUPPLY_PROP_STATUS, &val);
	KUNIT_EXPECT_EQ(test, ret, 0);
	KUNIT_EXPECT_EQ(test, val.intval, POWER_SUPPLY_STATUS_DISCHARGING);
}

static struct kunit_case hid_input_tests[] = {
	KUNIT_CASE(hid_test_input_set_battery_charge_status),
	KUNIT_CASE(hid_test_input_get_battery_property),
	{ }
};

static struct kunit_suite hid_input_test_suite = {
	.name = "hid_input",
	.test_cases = hid_input_tests,
};

kunit_test_suite(hid_input_test_suite);

MODULE_DESCRIPTION("HID input KUnit tests");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("José Expósito <jose.exposito89@gmail.com>");