Make context reference counted
[platform/upstream/libinput.git] / test / keyboard.c
1 /*
2  * Copyright © 2014 Jonas Ådahl <jadahl@gmail.com>
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 <stdio.h>
27
28 #include "litest.h"
29
30 START_TEST(keyboard_seat_key_count)
31 {
32         const int num_devices = 4;
33         struct litest_device *devices[num_devices];
34         struct libinput *libinput;
35         struct libinput_event *ev;
36         struct libinput_event_keyboard *kev;
37         int i;
38         int seat_key_count;
39         int expected_key_button_count = 0;
40         char device_name[255];
41
42         libinput = litest_create_context();
43         for (i = 0; i < num_devices; ++i) {
44                 sprintf(device_name, "Generic keyboard (%d)", i);
45                 devices[i] = litest_add_device_with_overrides(libinput,
46                                                               LITEST_KEYBOARD,
47                                                               device_name,
48                                                               NULL, NULL, NULL);
49         }
50
51         for (i = 0; i < num_devices; ++i)
52                 litest_keyboard_key(devices[i], KEY_A, true);
53
54         libinput_dispatch(libinput);
55         while ((ev = libinput_get_event(libinput))) {
56                 if (libinput_event_get_type(ev) !=
57                     LIBINPUT_EVENT_KEYBOARD_KEY) {
58                         libinput_event_destroy(ev);
59                         libinput_dispatch(libinput);
60                         continue;
61                 }
62
63                 kev = libinput_event_get_keyboard_event(ev);
64                 ck_assert_notnull(kev);
65                 ck_assert_int_eq(libinput_event_keyboard_get_key(kev), KEY_A);
66                 ck_assert_int_eq(libinput_event_keyboard_get_key_state(kev),
67                                  LIBINPUT_KEYBOARD_KEY_STATE_PRESSED);
68
69                 ++expected_key_button_count;
70                 seat_key_count =
71                         libinput_event_keyboard_get_seat_key_count(kev);
72                 ck_assert_int_eq(expected_key_button_count, seat_key_count);
73
74                 libinput_event_destroy(ev);
75                 libinput_dispatch(libinput);
76         }
77
78         ck_assert_int_eq(seat_key_count, num_devices);
79
80         for (i = 0; i < num_devices; ++i)
81                 litest_keyboard_key(devices[i], KEY_A, false);
82
83         libinput_dispatch(libinput);
84         while ((ev = libinput_get_event(libinput))) {
85                 if (libinput_event_get_type(ev) !=
86                     LIBINPUT_EVENT_KEYBOARD_KEY) {
87                         libinput_event_destroy(ev);
88                         libinput_dispatch(libinput);
89                         continue;
90                 }
91
92                 kev = libinput_event_get_keyboard_event(ev);
93                 ck_assert_notnull(kev);
94                 ck_assert_int_eq(libinput_event_keyboard_get_key(kev), KEY_A);
95                 ck_assert_int_eq(libinput_event_keyboard_get_key_state(kev),
96                                  LIBINPUT_KEYBOARD_KEY_STATE_RELEASED);
97
98                 --expected_key_button_count;
99                 seat_key_count =
100                         libinput_event_keyboard_get_seat_key_count(kev);
101                 ck_assert_int_eq(expected_key_button_count, seat_key_count);
102
103                 libinput_event_destroy(ev);
104                 libinput_dispatch(libinput);
105         }
106
107         ck_assert_int_eq(seat_key_count, 0);
108
109         for (i = 0; i < num_devices; ++i)
110                 litest_delete_device(devices[i]);
111         libinput_unref(libinput);
112 }
113 END_TEST
114
115 int
116 main(int argc, char **argv)
117 {
118         litest_add_no_device("keyboard:seat key count", keyboard_seat_key_count);
119
120         return litest_run(argc, argv);
121 }