1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2019 Red Hat, Inc.
4 */
5
6 #include "config.h"
7 #include "test-common.h"
8
START_TEST(test_info)9 START_TEST(test_info)
10 {
11 struct libevdev *d = libevdev_new();
12
13 libevdev_set_name(d, "some name");
14 ck_assert_str_eq(libevdev_get_name(d), "some name");
15 libevdev_set_phys(d, "physical");
16 ck_assert_str_eq(libevdev_get_phys(d), "physical");
17 libevdev_set_uniq(d, "very unique");
18 ck_assert_str_eq(libevdev_get_uniq(d), "very unique");
19
20 libevdev_set_id_bustype(d, 1);
21 libevdev_set_id_vendor(d, 2);
22 libevdev_set_id_product(d, 3);
23 libevdev_set_id_version(d, 4);
24 ck_assert_int_eq(libevdev_get_id_bustype(d), 1);
25 ck_assert_int_eq(libevdev_get_id_vendor(d), 2);
26 ck_assert_int_eq(libevdev_get_id_product(d), 3);
27 ck_assert_int_eq(libevdev_get_id_version(d), 4);
28
29 libevdev_free(d);
30 }
31 END_TEST
32
START_TEST(test_properties)33 START_TEST(test_properties)
34 {
35 for (unsigned prop = 0; prop < INPUT_PROP_CNT; prop++) {
36 struct libevdev *d = libevdev_new();
37
38 ck_assert(!libevdev_has_property(d, prop));
39 libevdev_enable_property(d, prop);
40 ck_assert(libevdev_has_property(d, prop));
41 libevdev_free(d);
42 }
43 }
44 END_TEST
45
START_TEST(test_bits)46 START_TEST(test_bits)
47 {
48 for (unsigned type = 1; type < EV_CNT; type++) {
49 unsigned max = libevdev_event_type_get_max(type);
50
51 if((int)max == -1)
52 continue;
53
54 for (unsigned code = 0; code <= max; code++) {
55 struct libevdev *d = libevdev_new();
56 const struct input_absinfo abs = {
57 .minimum = 10,
58 .maximum = 20,
59 .fuzz = 30,
60 .flat = 40,
61 .resolution = 50,
62 };
63 const void *data = NULL;
64
65 if (type == EV_ABS || type == EV_REP)
66 data = &abs;
67
68 ck_assert(!libevdev_has_event_code(d, type, code));
69 libevdev_enable_event_code(d, type, code, data);
70 ck_assert(libevdev_has_event_code(d, type, code));
71 libevdev_free(d);
72 }
73 }
74 }
75 END_TEST
76
START_TEST(test_mt_slots_enable_disable)77 START_TEST(test_mt_slots_enable_disable)
78 {
79 struct libevdev *d = libevdev_new();
80 struct input_absinfo abs = {0};
81
82 abs.maximum = 5;
83 libevdev_enable_event_code(d, EV_ABS, ABS_MT_SLOT, &abs);
84 ck_assert(libevdev_has_event_code(d, EV_ABS, ABS_MT_SLOT));
85 ck_assert_int_eq(libevdev_get_num_slots(d), 6);
86
87 libevdev_disable_event_code(d, EV_ABS, ABS_MT_SLOT);
88 ck_assert(!libevdev_has_event_code(d, EV_ABS, ABS_MT_SLOT));
89 ck_assert_int_eq(libevdev_get_num_slots(d), -1);
90
91 abs.maximum = 2;
92 libevdev_enable_event_code(d, EV_ABS, ABS_MT_SLOT, &abs);
93 ck_assert(libevdev_has_event_code(d, EV_ABS, ABS_MT_SLOT));
94 ck_assert_int_eq(libevdev_get_num_slots(d), 3);
95
96 libevdev_free(d);
97 }
98 END_TEST
99
START_TEST(test_mt_slots_increase_decrease)100 START_TEST(test_mt_slots_increase_decrease)
101 {
102 struct libevdev *d = libevdev_new();
103 struct input_absinfo abs = {0};
104
105 abs.maximum = 5;
106 libevdev_enable_event_code(d, EV_ABS, ABS_MT_SLOT, &abs);
107 ck_assert(libevdev_has_event_code(d, EV_ABS, ABS_MT_SLOT));
108 ck_assert_int_eq(libevdev_get_num_slots(d), 6);
109
110 abs.maximum = 2;
111 libevdev_enable_event_code(d, EV_ABS, ABS_MT_SLOT, &abs);
112 ck_assert(libevdev_has_event_code(d, EV_ABS, ABS_MT_SLOT));
113 ck_assert_int_eq(libevdev_get_num_slots(d), 3);
114
115 abs.maximum = 6;
116 libevdev_enable_event_code(d, EV_ABS, ABS_MT_SLOT, &abs);
117 ck_assert(libevdev_has_event_code(d, EV_ABS, ABS_MT_SLOT));
118 ck_assert_int_eq(libevdev_get_num_slots(d), 7);
119
120 abs.maximum = 10;
121 libevdev_enable_event_code(d, EV_ABS, ABS_MT_SLOT, &abs);
122 ck_assert(libevdev_has_event_code(d, EV_ABS, ABS_MT_SLOT));
123 ck_assert_int_eq(libevdev_get_num_slots(d), 11);
124
125 libevdev_free(d);
126 }
127 END_TEST
128
START_TEST(test_mt_tracking_id)129 START_TEST(test_mt_tracking_id)
130 {
131 struct libevdev *d = libevdev_new();
132 struct input_absinfo abs = { .maximum = 5 };
133
134 libevdev_enable_event_code(d, EV_ABS, ABS_MT_SLOT, &abs);
135
136 /* Not yet enabled, so 0. This is technically undefined */
137 for (int slot = 0; slot < 5; slot++)
138 ck_assert_int_eq(libevdev_get_slot_value(d, 0, ABS_MT_TRACKING_ID), 0);
139
140 libevdev_enable_event_code(d, EV_ABS, ABS_MT_TRACKING_ID, &abs);
141
142 for (int slot = 0; slot < 5; slot++)
143 ck_assert_int_eq(libevdev_get_slot_value(d, 0, ABS_MT_TRACKING_ID), -1);
144
145 libevdev_free(d);
146 }
147 END_TEST
148
TEST_SUITE(event_name_suite)149 TEST_SUITE(event_name_suite)
150 {
151 Suite *s = suite_create("Context manipulation");
152
153 add_test(s, test_info);
154 add_test(s, test_properties);
155 add_test(s, test_bits);
156 add_test(s, test_mt_slots_enable_disable);
157 add_test(s, test_mt_slots_increase_decrease);
158 add_test(s, test_mt_tracking_id);
159
160 return s;
161 }
162