Debian Codegraph @

test/test-trackball.c

raw
1 /*
2 * Copyright © 2016 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <config.h>
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <libinput.h>
29 #include <unistd.h>
30
31 #include "libinput-util.h"
32 #include "litest.h"
33
34 START_TEST(trackball_rotation_config_defaults)
35 {
36 struct litest_device *dev = litest_current_device();
37 struct libinput_device *device = dev->libinput_device;
38 int angle;
39
40 litest_assert(libinput_device_config_rotation_is_available(device));
41
42 angle = libinput_device_config_rotation_get_angle(device);
43 litest_assert_int_eq(angle, 0);
44 angle = libinput_device_config_rotation_get_default_angle(device);
45 litest_assert_int_eq(angle, 0);
46 }
47 END_TEST
48
49 START_TEST(trackball_rotation_config_invalid_range)
50 {
51 struct litest_device *dev = litest_current_device();
52 struct libinput_device *device = dev->libinput_device;
53 enum libinput_config_status status;
54
55 status = libinput_device_config_rotation_set_angle(device, 360);
56 litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_INVALID);
57 status = libinput_device_config_rotation_set_angle(device, 361);
58 litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_INVALID);
59 status = libinput_device_config_rotation_set_angle(device, -1);
60 litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_INVALID);
61 }
62 END_TEST
63
64 START_TEST(trackball_rotation_config_no_rotation)
65 {
66 struct litest_device *dev = litest_current_device();
67 struct libinput_device *device = dev->libinput_device;
68 enum libinput_config_status status;
69 int angle;
70
71 litest_assert(!libinput_device_config_rotation_is_available(device));
72
73 angle = libinput_device_config_rotation_get_angle(device);
74 litest_assert_int_eq(angle, 0);
75 angle = libinput_device_config_rotation_get_default_angle(device);
76 litest_assert_int_eq(angle, 0);
77
78 /* 0 always succeeds */
79 status = libinput_device_config_rotation_set_angle(device, 0);
80 litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_SUCCESS);
81
82 for (angle = 1; angle < 360; angle++) {
83 status = libinput_device_config_rotation_set_angle(device, angle);
84 litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_UNSUPPORTED);
85 }
86 }
87 END_TEST
88
89 START_TEST(trackball_rotation_config_right_angle)
90 {
91 struct litest_device *dev = litest_current_device();
92 struct libinput_device *device = dev->libinput_device;
93 enum libinput_config_status status;
94 int angle;
95
96 litest_assert(libinput_device_config_rotation_is_available(device));
97
98 for (angle = 0; angle < 360; angle += 90) {
99 status = libinput_device_config_rotation_set_angle(device, angle);
100 litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_SUCCESS);
101 }
102 }
103 END_TEST
104
105 START_TEST(trackball_rotation_config_odd_angle)
106 {
107 struct litest_device *dev = litest_current_device();
108 struct libinput_device *device = dev->libinput_device;
109 enum libinput_config_status status;
110 int angle;
111
112 litest_assert(libinput_device_config_rotation_is_available(device));
113
114 for (angle = 0; angle < 360; angle++) {
115 status = libinput_device_config_rotation_set_angle(device, angle);
116 litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_SUCCESS);
117 }
118 }
119 END_TEST
120
121 START_TEST(trackball_rotation_x)
122 {
123 struct litest_device *dev = litest_current_device();
124 struct libinput *li = dev->libinput;
125 struct libinput_device *device = dev->libinput_device;
126 struct libinput_event *event;
127 struct libinput_event_pointer *ptrev;
128 int angle;
129 double dx, dy;
130
131 litest_drain_events(li);
132
133 for (angle = 0; angle < 360; angle++) {
134 libinput_device_config_rotation_set_angle(device, angle);
135
136 litest_event(dev, EV_REL, REL_X, 1);
137 litest_event(dev, EV_SYN, SYN_REPORT, 0);
138 litest_dispatch(li);
139
140 event = libinput_get_event(li);
141 ptrev = litest_is_motion_event(event);
142
143 /* Test unaccelerated because pointer accel may mangle the
144 other coords */
145 dx = libinput_event_pointer_get_dx_unaccelerated(ptrev);
146 dy = libinput_event_pointer_get_dy_unaccelerated(ptrev);
147
148 switch (angle) {
149 case 0:
150 litest_assert_double_eq(dx, 1.0);
151 litest_assert_double_eq(dy, 0.0);
152 break;
153 case 90:
154 litest_assert_double_eq(dx, 0.0);
155 litest_assert_double_eq(dy, 1.0);
156 break;
157 case 180:
158 litest_assert_double_eq(dx, -1.0);
159 litest_assert_double_eq(dy, 0.0);
160 break;
161 case 270:
162 litest_assert_double_eq(dx, 0.0);
163 litest_assert_double_eq(dy, -1.0);
164 break;
165 }
166 libinput_event_destroy(event);
167 }
168 }
169 END_TEST
170
171 START_TEST(trackball_rotation_y)
172 {
173 struct litest_device *dev = litest_current_device();
174 struct libinput *li = dev->libinput;
175 struct libinput_device *device = dev->libinput_device;
176 struct libinput_event *event;
177 struct libinput_event_pointer *ptrev;
178 int angle;
179 double dx, dy;
180
181 litest_drain_events(li);
182
183 for (angle = 0; angle < 360; angle++) {
184 libinput_device_config_rotation_set_angle(device, angle);
185
186 litest_event(dev, EV_REL, REL_Y, 1);
187 litest_event(dev, EV_SYN, SYN_REPORT, 0);
188 litest_dispatch(li);
189
190 event = libinput_get_event(li);
191 ptrev = litest_is_motion_event(event);
192
193 /* Test unaccelerated because pointer accel may mangle the
194 other coords */
195 dx = libinput_event_pointer_get_dx_unaccelerated(ptrev);
196 dy = libinput_event_pointer_get_dy_unaccelerated(ptrev);
197
198 switch (angle) {
199 case 0:
200 litest_assert_double_eq(dx, 0.0);
201 litest_assert_double_eq(dy, 1.0);
202 break;
203 case 90:
204 litest_assert_double_eq(dx, -1.0);
205 litest_assert_double_eq(dy, 0.0);
206 break;
207 case 180:
208 litest_assert_double_eq(dx, 0.0);
209 litest_assert_double_eq(dy, -1.0);
210 break;
211 case 270:
212 litest_assert_double_eq(dx, 1.0);
213 litest_assert_double_eq(dy, 0.0);
214 break;
215 }
216 libinput_event_destroy(event);
217 }
218 }
219 END_TEST
220
221 START_TEST(trackball_rotation_accel)
222 {
223 struct litest_device *dev = litest_current_device();
224 struct libinput *li = dev->libinput;
225 struct libinput_device *device = dev->libinput_device;
226 struct libinput_event *event;
227 struct libinput_event_pointer *ptrev;
228 double dx, dy;
229
230 litest_drain_events(li);
231
232 /* Pointer accel mangles the coordinates, so we only test one angle
233 * and rely on the unaccelerated tests above to warn us when
234 * something's off */
235 libinput_device_config_rotation_set_angle(device, 90);
236
237 litest_event(dev, EV_REL, REL_Y, 1);
238 litest_event(dev, EV_REL, REL_X, 1);
239 litest_event(dev, EV_SYN, SYN_REPORT, 0);
240 litest_dispatch(li);
241
242 event = libinput_get_event(li);
243 ptrev = litest_is_motion_event(event);
244
245 dx = libinput_event_pointer_get_dx(ptrev);
246 dy = libinput_event_pointer_get_dy(ptrev);
247
248 litest_assert_double_lt(dx, 0.0);
249 litest_assert_double_gt(dy, 0.0);
250 libinput_event_destroy(event);
251 }
252 END_TEST
253
254 TEST_COLLECTION(trackball)
255 {
256 /* clang-format off */
257 litest_add(trackball_rotation_config_defaults, LITEST_TRACKBALL, LITEST_ANY);
258 litest_add(trackball_rotation_config_invalid_range, LITEST_TRACKBALL, LITEST_ANY);
259 litest_add(trackball_rotation_config_no_rotation, LITEST_POINTINGSTICK, LITEST_ANY);
260 litest_add(trackball_rotation_config_right_angle, LITEST_TRACKBALL, LITEST_ANY);
261 litest_add(trackball_rotation_config_odd_angle, LITEST_TRACKBALL, LITEST_ANY);
262 litest_add(trackball_rotation_x, LITEST_TRACKBALL, LITEST_ANY);
263 litest_add(trackball_rotation_y, LITEST_TRACKBALL, LITEST_ANY);
264 litest_add(trackball_rotation_accel, LITEST_TRACKBALL, LITEST_ANY);
265 /* clang-format on */
266 }