test: add a bunch of test for click behavior on touchpads
[platform/upstream/libinput.git] / test / touchpad.c
1 /*
2  * Copyright © 2014 Red Hat, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include <config.h>
24
25 #include <check.h>
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(touchpad_1fg_motion)
35 {
36         struct litest_device *dev = litest_current_device();
37         struct libinput *li = dev->libinput;
38         struct libinput_event *event;
39         struct libinput_event_pointer *ptrev;
40
41         litest_drain_events(li);
42
43         litest_touch_down(dev, 0, 50, 50);
44         litest_touch_move_to(dev, 0, 50, 50, 80, 50, 5);
45         litest_touch_up(dev, 0);
46
47         libinput_dispatch(li);
48
49         event = libinput_get_event(li);
50         ck_assert(event != NULL);
51
52         while (event) {
53                 ck_assert_int_eq(libinput_event_get_type(event),
54                                  LIBINPUT_EVENT_POINTER_MOTION);
55
56                 ptrev = libinput_event_get_pointer_event(event);
57                 ck_assert_int_ge(libinput_event_pointer_get_dx(ptrev), 0);
58                 ck_assert_int_eq(libinput_event_pointer_get_dy(ptrev), 0);
59                 libinput_event_destroy(event);
60                 event = libinput_get_event(li);
61         }
62 }
63 END_TEST
64
65 START_TEST(touchpad_2fg_no_motion)
66 {
67         struct litest_device *dev = litest_current_device();
68         struct libinput *li = dev->libinput;
69         struct libinput_event *event;
70
71         litest_drain_events(li);
72
73         litest_touch_down(dev, 0, 20, 20);
74         litest_touch_down(dev, 1, 70, 20);
75         litest_touch_move_to(dev, 0, 20, 20, 80, 80, 5);
76         litest_touch_move_to(dev, 1, 70, 20, 80, 50, 5);
77         litest_touch_up(dev, 1);
78         litest_touch_up(dev, 0);
79
80         libinput_dispatch(li);
81
82         event = libinput_get_event(li);
83         while (event) {
84                 ck_assert_int_ne(libinput_event_get_type(event),
85                                  LIBINPUT_EVENT_POINTER_MOTION);
86                 libinput_event_destroy(event);
87                 event = libinput_get_event(li);
88         }
89 }
90 END_TEST
91
92 static void
93 assert_button_event(struct libinput *li, int button,
94                     enum libinput_button_state state)
95 {
96         struct libinput_event *event;
97         struct libinput_event_pointer *ptrev;
98
99         libinput_dispatch(li);
100         event = libinput_get_event(li);
101
102         ck_assert(event != NULL);
103         ck_assert_int_eq(libinput_event_get_type(event),
104                          LIBINPUT_EVENT_POINTER_BUTTON);
105         ptrev = libinput_event_get_pointer_event(event);
106         ck_assert_int_eq(libinput_event_pointer_get_button(ptrev),
107                          button);
108         ck_assert_int_eq(libinput_event_pointer_get_button_state(ptrev),
109                          state);
110         libinput_event_destroy(event);
111 }
112
113 START_TEST(touchpad_1fg_tap)
114 {
115         struct litest_device *dev = litest_current_device();
116         struct libinput *li = dev->libinput;
117         struct libinput_event *event;
118
119         litest_drain_events(li);
120
121         litest_touch_down(dev, 0, 50, 50);
122         litest_touch_up(dev, 0);
123
124         libinput_dispatch(li);
125
126         assert_button_event(li, BTN_LEFT,
127                             LIBINPUT_BUTTON_STATE_PRESSED);
128         usleep(300000); /* tap-n-drag timeout */
129         assert_button_event(li, BTN_LEFT,
130                             LIBINPUT_BUTTON_STATE_RELEASED);
131
132         libinput_dispatch(li);
133         event = libinput_get_event(li);
134         ck_assert(event == NULL);
135 }
136 END_TEST
137
138 START_TEST(touchpad_1fg_tap_n_drag)
139 {
140         struct litest_device *dev = litest_current_device();
141         struct libinput *li = dev->libinput;
142         struct libinput_event *event;
143
144         litest_drain_events(li);
145
146         litest_touch_down(dev, 0, 50, 50);
147         litest_touch_up(dev, 0);
148         litest_touch_down(dev, 0, 50, 50);
149         litest_touch_move_to(dev, 0, 50, 50, 80, 80, 5);
150         litest_touch_up(dev, 0);
151
152         libinput_dispatch(li);
153
154         assert_button_event(li, BTN_LEFT,
155                             LIBINPUT_BUTTON_STATE_PRESSED);
156
157         libinput_dispatch(li);
158         while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION) {
159                 event = libinput_get_event(li);
160                 libinput_event_destroy(event);
161                 libinput_dispatch(li);
162         }
163
164         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
165
166         /* lift finger, set down again, should continue dragging */
167         litest_touch_down(dev, 0, 50, 50);
168         litest_touch_move_to(dev, 0, 50, 50, 80, 80, 5);
169         litest_touch_up(dev, 0);
170
171         libinput_dispatch(li);
172         while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION) {
173                 event = libinput_get_event(li);
174                 libinput_event_destroy(event);
175                 libinput_dispatch(li);
176         }
177
178         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
179
180         usleep(300000); /* tap-n-drag timeout */
181
182         assert_button_event(li, BTN_LEFT,
183                             LIBINPUT_BUTTON_STATE_RELEASED);
184
185         litest_assert_empty_queue(li);
186 }
187 END_TEST
188
189 START_TEST(touchpad_2fg_tap)
190 {
191         struct litest_device *dev = litest_current_device();
192         struct libinput *li = dev->libinput;
193
194         litest_drain_events(dev->libinput);
195
196         litest_touch_down(dev, 0, 50, 50);
197         litest_touch_down(dev, 1, 70, 70);
198         litest_touch_up(dev, 0);
199         litest_touch_up(dev, 1);
200
201         libinput_dispatch(li);
202
203         assert_button_event(li, BTN_RIGHT,
204                             LIBINPUT_BUTTON_STATE_PRESSED);
205         usleep(300000); /* tap-n-drag timeout */
206         assert_button_event(li, BTN_RIGHT,
207                             LIBINPUT_BUTTON_STATE_RELEASED);
208
209         litest_assert_empty_queue(li);
210 }
211 END_TEST
212
213 START_TEST(touchpad_1fg_tap_click)
214 {
215         struct litest_device *dev = litest_current_device();
216         struct libinput *li = dev->libinput;
217
218         litest_drain_events(dev->libinput);
219
220         /* finger down, button click, finger up
221            -> only one button left event pair */
222         litest_touch_down(dev, 0, 50, 50);
223         litest_event(dev, EV_KEY, BTN_LEFT, 1);
224         litest_event(dev, EV_SYN, SYN_REPORT, 0);
225         litest_event(dev, EV_KEY, BTN_LEFT, 0);
226         litest_event(dev, EV_SYN, SYN_REPORT, 0);
227         litest_touch_up(dev, 0);
228
229         libinput_dispatch(li);
230
231         assert_button_event(li, BTN_LEFT,
232                             LIBINPUT_BUTTON_STATE_PRESSED);
233         assert_button_event(li, BTN_LEFT,
234                             LIBINPUT_BUTTON_STATE_RELEASED);
235
236         litest_assert_empty_queue(li);
237 }
238 END_TEST
239
240 START_TEST(touchpad_2fg_tap_click)
241 {
242         struct litest_device *dev = litest_current_device();
243         struct libinput *li = dev->libinput;
244
245         litest_drain_events(dev->libinput);
246
247         /* two fingers down, button click, fingers up
248            -> only one button left event pair */
249         litest_touch_down(dev, 0, 50, 50);
250         litest_touch_down(dev, 1, 70, 50);
251         litest_event(dev, EV_KEY, BTN_LEFT, 1);
252         litest_event(dev, EV_SYN, SYN_REPORT, 0);
253         litest_event(dev, EV_KEY, BTN_LEFT, 0);
254         litest_event(dev, EV_SYN, SYN_REPORT, 0);
255         litest_touch_up(dev, 1);
256         litest_touch_up(dev, 0);
257
258         libinput_dispatch(li);
259
260         assert_button_event(li, BTN_LEFT,
261                             LIBINPUT_BUTTON_STATE_PRESSED);
262         assert_button_event(li, BTN_LEFT,
263                             LIBINPUT_BUTTON_STATE_RELEASED);
264
265         litest_assert_empty_queue(li);
266 }
267 END_TEST
268
269 START_TEST(touchpad_2fg_tap_click_apple)
270 {
271         struct litest_device *dev = litest_current_device();
272         struct libinput *li = dev->libinput;
273
274         litest_drain_events(dev->libinput);
275
276         /* two fingers down, button click, fingers up
277            -> only one button right event pair
278            (apple have clickfinger enabled by default) */
279         litest_touch_down(dev, 0, 50, 50);
280         litest_touch_down(dev, 1, 70, 50);
281         litest_event(dev, EV_KEY, BTN_LEFT, 1);
282         litest_event(dev, EV_SYN, SYN_REPORT, 0);
283         litest_event(dev, EV_KEY, BTN_LEFT, 0);
284         litest_event(dev, EV_SYN, SYN_REPORT, 0);
285         litest_touch_up(dev, 1);
286         litest_touch_up(dev, 0);
287
288         libinput_dispatch(li);
289
290         assert_button_event(li, BTN_RIGHT,
291                             LIBINPUT_BUTTON_STATE_PRESSED);
292         assert_button_event(li, BTN_RIGHT,
293                             LIBINPUT_BUTTON_STATE_RELEASED);
294
295         litest_assert_empty_queue(li);
296 }
297 END_TEST
298
299 START_TEST(touchpad_1fg_double_tap_click)
300 {
301         struct litest_device *dev = litest_current_device();
302         struct libinput *li = dev->libinput;
303
304         litest_drain_events(dev->libinput);
305
306         /* one finger down, up, down, button click, finger up
307            -> two button left event pairs */
308         litest_touch_down(dev, 0, 50, 50);
309         litest_touch_up(dev, 0);
310         litest_touch_down(dev, 0, 50, 50);
311         litest_event(dev, EV_KEY, BTN_LEFT, 1);
312         litest_event(dev, EV_SYN, SYN_REPORT, 0);
313         litest_event(dev, EV_KEY, BTN_LEFT, 0);
314         litest_event(dev, EV_SYN, SYN_REPORT, 0);
315         litest_touch_up(dev, 0);
316
317         libinput_dispatch(li);
318
319         assert_button_event(li, BTN_LEFT,
320                             LIBINPUT_BUTTON_STATE_PRESSED);
321         assert_button_event(li, BTN_LEFT,
322                             LIBINPUT_BUTTON_STATE_RELEASED);
323         assert_button_event(li, BTN_LEFT,
324                             LIBINPUT_BUTTON_STATE_PRESSED);
325         assert_button_event(li, BTN_LEFT,
326                             LIBINPUT_BUTTON_STATE_RELEASED);
327
328         litest_assert_empty_queue(li);
329 }
330 END_TEST
331
332 START_TEST(touchpad_1fg_tap_n_drag_click)
333 {
334         struct litest_device *dev = litest_current_device();
335         struct libinput *li = dev->libinput;
336         struct libinput_event *event;
337
338         litest_drain_events(dev->libinput);
339
340         /* one finger down, up, down, move, button click, finger up
341            -> two button left event pairs, motion allowed */
342         litest_touch_down(dev, 0, 50, 50);
343         litest_touch_up(dev, 0);
344         litest_touch_down(dev, 0, 50, 50);
345         litest_touch_move_to(dev, 0, 50, 50, 80, 50, 10);
346
347         assert_button_event(li, BTN_LEFT,
348                             LIBINPUT_BUTTON_STATE_PRESSED);
349
350         libinput_dispatch(li);
351
352         ck_assert_int_eq(libinput_next_event_type(li),
353                          LIBINPUT_EVENT_POINTER_MOTION);
354         while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION) {
355                 event = libinput_get_event(li);
356                 libinput_event_destroy(event);
357                 libinput_dispatch(li);
358         }
359
360         litest_event(dev, EV_KEY, BTN_LEFT, 1);
361         litest_event(dev, EV_SYN, SYN_REPORT, 0);
362
363         assert_button_event(li, BTN_LEFT,
364                             LIBINPUT_BUTTON_STATE_RELEASED);
365         assert_button_event(li, BTN_LEFT,
366                             LIBINPUT_BUTTON_STATE_PRESSED);
367
368         litest_event(dev, EV_KEY, BTN_LEFT, 0);
369         litest_event(dev, EV_SYN, SYN_REPORT, 0);
370         litest_touch_up(dev, 0);
371
372         libinput_dispatch(li);
373
374         assert_button_event(li, BTN_LEFT,
375                             LIBINPUT_BUTTON_STATE_RELEASED);
376
377         litest_assert_empty_queue(li);
378 }
379 END_TEST
380
381 START_TEST(touchpad_1fg_clickfinger)
382 {
383         struct litest_device *dev = litest_create_device(LITEST_BCM5974);
384         struct libinput *li = dev->libinput;
385
386         litest_drain_events(li);
387
388         litest_touch_down(dev, 0, 50, 50);
389         litest_event(dev, EV_KEY, BTN_LEFT, 1);
390         litest_event(dev, EV_SYN, SYN_REPORT, 0);
391         litest_event(dev, EV_KEY, BTN_LEFT, 0);
392         litest_event(dev, EV_SYN, SYN_REPORT, 0);
393         litest_touch_up(dev, 0);
394
395         libinput_dispatch(li);
396
397         assert_button_event(li, BTN_LEFT,
398                             LIBINPUT_BUTTON_STATE_PRESSED);
399         assert_button_event(li, BTN_LEFT,
400                             LIBINPUT_BUTTON_STATE_RELEASED);
401
402         litest_delete_device(dev);
403 }
404 END_TEST
405
406 START_TEST(touchpad_2fg_clickfinger)
407 {
408         struct litest_device *dev = litest_create_device(LITEST_BCM5974);
409         struct libinput *li = dev->libinput;
410
411         litest_drain_events(li);
412
413         litest_touch_down(dev, 0, 50, 50);
414         litest_touch_down(dev, 1, 70, 70);
415         litest_event(dev, EV_KEY, BTN_LEFT, 1);
416         litest_event(dev, EV_SYN, SYN_REPORT, 0);
417         litest_event(dev, EV_KEY, BTN_LEFT, 0);
418         litest_event(dev, EV_SYN, SYN_REPORT, 0);
419         litest_touch_up(dev, 0);
420         litest_touch_up(dev, 1);
421
422         libinput_dispatch(li);
423
424         assert_button_event(li, BTN_RIGHT,
425                             LIBINPUT_BUTTON_STATE_PRESSED);
426         assert_button_event(li, BTN_RIGHT,
427                             LIBINPUT_BUTTON_STATE_RELEASED);
428
429         litest_delete_device(dev);
430 }
431 END_TEST
432
433 START_TEST(touchpad_btn_left)
434 {
435         struct litest_device *dev = litest_current_device();
436         struct libinput *li = dev->libinput;
437
438         litest_drain_events(li);
439
440         litest_event(dev, EV_KEY, BTN_LEFT, 1);
441         litest_event(dev, EV_SYN, SYN_REPORT, 0);
442         litest_event(dev, EV_KEY, BTN_LEFT, 0);
443         litest_event(dev, EV_SYN, SYN_REPORT, 0);
444
445         libinput_dispatch(li);
446
447         assert_button_event(li, BTN_LEFT,
448                             LIBINPUT_BUTTON_STATE_PRESSED);
449         assert_button_event(li, BTN_LEFT,
450                             LIBINPUT_BUTTON_STATE_RELEASED);
451 }
452 END_TEST
453
454 START_TEST(clickpad_btn_left)
455 {
456         struct litest_device *dev = litest_current_device();
457         struct libinput *li = dev->libinput;
458
459         litest_drain_events(li);
460
461         /* A clickpad always needs a finger down to tell where the
462            click happens */
463         litest_event(dev, EV_KEY, BTN_LEFT, 1);
464         litest_event(dev, EV_SYN, SYN_REPORT, 0);
465         litest_event(dev, EV_KEY, BTN_LEFT, 0);
466         litest_event(dev, EV_SYN, SYN_REPORT, 0);
467
468         libinput_dispatch(li);
469         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
470 }
471 END_TEST
472
473 START_TEST(clickpad_click_n_drag)
474 {
475         struct litest_device *dev = litest_current_device();
476         struct libinput *li = dev->libinput;
477         struct libinput_event *event;
478
479         litest_drain_events(li);
480
481         litest_touch_down(dev, 0, 50, 50);
482         litest_event(dev, EV_KEY, BTN_LEFT, 1);
483         litest_event(dev, EV_SYN, SYN_REPORT, 0);
484
485         libinput_dispatch(li);
486         assert_button_event(li, BTN_LEFT,
487                             LIBINPUT_BUTTON_STATE_PRESSED);
488
489         libinput_dispatch(li);
490         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
491
492         /* now put a second finger down */
493         litest_touch_down(dev, 1, 70, 70);
494         litest_touch_move_to(dev, 1, 70, 70, 80, 50, 5);
495         litest_touch_up(dev, 1);
496
497         libinput_dispatch(li);
498         ck_assert_int_eq(libinput_next_event_type(li),
499                          LIBINPUT_EVENT_POINTER_MOTION);
500         do {
501                 event = libinput_get_event(li);
502                 libinput_event_destroy(event);
503                 libinput_dispatch(li);
504         } while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION);
505
506         litest_event(dev, EV_KEY, BTN_LEFT, 0);
507         litest_event(dev, EV_SYN, SYN_REPORT, 0);
508         litest_touch_up(dev, 0);
509
510         assert_button_event(li, BTN_LEFT,
511                             LIBINPUT_BUTTON_STATE_RELEASED);
512 }
513 END_TEST
514
515 int main(int argc, char **argv) {
516
517         litest_add("touchpad:motion", touchpad_1fg_motion, LITEST_TOUCHPAD, LITEST_ANY);
518         litest_add("touchpad:motion", touchpad_2fg_no_motion, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
519
520         litest_add("touchpad:tap", touchpad_1fg_tap, LITEST_TOUCHPAD, LITEST_ANY);
521         litest_add("touchpad:tap", touchpad_1fg_tap_n_drag, LITEST_TOUCHPAD, LITEST_ANY);
522         litest_add("touchpad:tap", touchpad_2fg_tap, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
523         litest_add("touchpad:tap", touchpad_1fg_tap_click, LITEST_TOUCHPAD, LITEST_ANY);
524         litest_add("touchpad:tap", touchpad_2fg_tap_click, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH|LITEST_APPLE_CLICKPAD);
525         litest_add("touchpad:tap", touchpad_2fg_tap_click_apple, LITEST_APPLE_CLICKPAD, LITEST_ANY);
526         /* Real buttons don't interfere with tapping, so don't run those for
527            pads with buttons */
528         litest_add("touchpad:tap", touchpad_1fg_double_tap_click, LITEST_CLICKPAD, LITEST_ANY);
529         litest_add("touchpad:tap", touchpad_1fg_tap_n_drag_click, LITEST_CLICKPAD, LITEST_ANY);
530
531         litest_add_no_device("touchpad:clickfinger", touchpad_1fg_clickfinger);
532         litest_add_no_device("touchpad:clickfinger", touchpad_2fg_clickfinger);
533
534         litest_add("touchpad:click", touchpad_btn_left, LITEST_TOUCHPAD, LITEST_CLICKPAD);
535         litest_add("touchpad:click", clickpad_btn_left, LITEST_CLICKPAD, LITEST_ANY);
536         litest_add("touchpad:click", clickpad_click_n_drag, LITEST_CLICKPAD, LITEST_SINGLE_TOUCH);
537
538         return litest_run(argc, argv);
539 }