blob: 01ffa0f8aa53c792fc23635537d7d2de7da378c6 [file] [log] [blame]
U. Artie Eoff840d5f92012-10-01 15:21:16 -07001/*
2 * Copyright © 2012 Intel Corporation
3 *
Bryce Harrington2cc92972015-06-11 15:39:40 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
U. Artie Eoff840d5f92012-10-01 15:21:16 -070011 *
Bryce Harrington2cc92972015-06-11 15:39:40 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
U. Artie Eoff840d5f92012-10-01 15:21:16 -070024 */
25
Bryce Harrington12cc4052014-11-19 17:18:33 -080026#include "config.h"
27
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030028#include <stdint.h>
29
Alexandros Frantzis2b442482018-02-20 14:05:50 +020030#include "input-timestamps-helper.h"
Alexandros Frantzisdae224c2017-12-13 13:27:57 +020031#include "shared/timespec-util.h"
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080032#include "weston-test-client-helper.h"
Pekka Paalanen701676d2019-11-13 15:45:10 +020033#include "weston-test-fixture-compositor.h"
34
35static enum test_result_code
36fixture_setup(struct weston_test_harness *harness)
37{
38 struct compositor_setup setup;
39
40 compositor_setup_defaults(&setup);
41
42 return weston_test_harness_execute_as_client(harness, &setup);
43}
44DECLARE_FIXTURE_SETUP(fixture_setup);
U. Artie Eoff840d5f92012-10-01 15:21:16 -070045
Alexandros Frantzisdae224c2017-12-13 13:27:57 +020046static const struct timespec t1 = { .tv_sec = 1, .tv_nsec = 1000001 };
47static const struct timespec t2 = { .tv_sec = 2, .tv_nsec = 2000001 };
Alexandros Frantzis2b442482018-02-20 14:05:50 +020048static const struct timespec t_other = { .tv_sec = 123, .tv_nsec = 456 };
Alexandros Frantzisdae224c2017-12-13 13:27:57 +020049
50static struct client *
51create_client_with_keyboard_focus(void)
52{
53 struct client *cl = create_client_and_test_surface(10, 10, 1, 1);
54 assert(cl);
55
56 weston_test_activate_surface(cl->test->weston_test,
57 cl->surface->wl_surface);
58 client_roundtrip(cl);
59
60 return cl;
61}
62
63static void
64send_key(struct client *client, const struct timespec *time,
65 uint32_t key, uint32_t state)
66{
67 uint32_t tv_sec_hi, tv_sec_lo, tv_nsec;
68
69 timespec_to_proto(time, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
70 weston_test_send_key(client->test->weston_test, tv_sec_hi, tv_sec_lo,
71 tv_nsec, key, state);
72 client_roundtrip(client);
73}
74
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080075TEST(simple_keyboard_test)
U. Artie Eoff840d5f92012-10-01 15:21:16 -070076{
Alexandros Frantzisdae224c2017-12-13 13:27:57 +020077 struct client *client = create_client_with_keyboard_focus();
78 struct keyboard *keyboard = client->input->keyboard;
79 struct surface *expect_focus = client->surface;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080080 uint32_t expect_key = 0;
81 uint32_t expect_state = 0;
U. Artie Eoff840d5f92012-10-01 15:21:16 -070082
Dawid Gajownik74a635b2015-08-06 17:12:19 -030083 while (1) {
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080084 assert(keyboard->key == expect_key);
85 assert(keyboard->state == expect_state);
86 assert(keyboard->focus == expect_focus);
87
88 if (keyboard->state == WL_KEYBOARD_KEY_STATE_PRESSED) {
89 expect_state = WL_KEYBOARD_KEY_STATE_RELEASED;
Alexandros Frantzisdae224c2017-12-13 13:27:57 +020090 send_key(client, &t1, expect_key, expect_state);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080091 } else if (keyboard->focus) {
92 expect_focus = NULL;
Derek Foremanf6a65922015-02-24 09:32:14 -060093 weston_test_activate_surface(
94 client->test->weston_test, NULL);
Alexandros Frantzisdae224c2017-12-13 13:27:57 +020095 client_roundtrip(client);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080096 } else if (expect_key < 10) {
97 expect_key++;
98 expect_focus = client->surface;
99 expect_state = WL_KEYBOARD_KEY_STATE_PRESSED;
Derek Foremanf6a65922015-02-24 09:32:14 -0600100 weston_test_activate_surface(
101 client->test->weston_test,
102 expect_focus->wl_surface);
Alexandros Frantzisdae224c2017-12-13 13:27:57 +0200103 send_key(client, &t1, expect_key, expect_state);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800104 } else {
105 break;
106 }
U. Artie Eoff840d5f92012-10-01 15:21:16 -0700107 }
Pekka Paalanenef8d6522021-06-15 16:31:16 +0300108
109 client_destroy(client);
U. Artie Eoff840d5f92012-10-01 15:21:16 -0700110}
Alexandros Frantzisdae224c2017-12-13 13:27:57 +0200111
112TEST(keyboard_key_event_time)
113{
114 struct client *client = create_client_with_keyboard_focus();
115 struct keyboard *keyboard = client->input->keyboard;
Alexandros Frantzis2b442482018-02-20 14:05:50 +0200116 struct input_timestamps *input_ts =
117 input_timestamps_create_for_keyboard(client);
Alexandros Frantzisdae224c2017-12-13 13:27:57 +0200118
119 send_key(client, &t1, 1, WL_KEYBOARD_KEY_STATE_PRESSED);
120 assert(keyboard->key_time_msec == timespec_to_msec(&t1));
Alexandros Frantzis2b442482018-02-20 14:05:50 +0200121 assert(timespec_eq(&keyboard->key_time_timespec, &t1));
Alexandros Frantzisdae224c2017-12-13 13:27:57 +0200122
123 send_key(client, &t2, 1, WL_KEYBOARD_KEY_STATE_RELEASED);
124 assert(keyboard->key_time_msec == timespec_to_msec(&t2));
Alexandros Frantzis2b442482018-02-20 14:05:50 +0200125 assert(timespec_eq(&keyboard->key_time_timespec, &t2));
126
127 input_timestamps_destroy(input_ts);
Pekka Paalanenef8d6522021-06-15 16:31:16 +0300128
129 client_destroy(client);
Alexandros Frantzis2b442482018-02-20 14:05:50 +0200130}
131
132TEST(keyboard_timestamps_stop_after_input_timestamps_object_is_destroyed)
133{
134 struct client *client = create_client_with_keyboard_focus();
135 struct keyboard *keyboard = client->input->keyboard;
136 struct input_timestamps *input_ts =
137 input_timestamps_create_for_keyboard(client);
138
139 send_key(client, &t1, 1, WL_KEYBOARD_KEY_STATE_PRESSED);
140 assert(keyboard->key_time_msec == timespec_to_msec(&t1));
141 assert(timespec_eq(&keyboard->key_time_timespec, &t1));
142
143 input_timestamps_destroy(input_ts);
144
145 send_key(client, &t2, 1, WL_KEYBOARD_KEY_STATE_RELEASED);
146 assert(keyboard->key_time_msec == timespec_to_msec(&t2));
147 assert(timespec_is_zero(&keyboard->key_time_timespec));
Pekka Paalanenef8d6522021-06-15 16:31:16 +0300148
149 client_destroy(client);
Alexandros Frantzis2b442482018-02-20 14:05:50 +0200150}
151
152TEST(keyboard_timestamps_stop_after_client_releases_wl_keyboard)
153{
154 struct client *client = create_client_with_keyboard_focus();
155 struct keyboard *keyboard = client->input->keyboard;
156 struct input_timestamps *input_ts =
157 input_timestamps_create_for_keyboard(client);
158
159 send_key(client, &t1, 1, WL_KEYBOARD_KEY_STATE_PRESSED);
160 assert(keyboard->key_time_msec == timespec_to_msec(&t1));
161 assert(timespec_eq(&keyboard->key_time_timespec, &t1));
162
163 wl_keyboard_release(client->input->keyboard->wl_keyboard);
164
165 /* Set input_timestamp to an arbitrary value (different from t1, t2
166 * and 0) and check that it is not changed by sending the event.
167 * This is preferred over just checking for 0, since 0 is used
168 * internally for resetting the timestamp after handling an input
169 * event and checking for it here may lead to false negatives. */
170 keyboard->input_timestamp = t_other;
171 send_key(client, &t2, 1, WL_KEYBOARD_KEY_STATE_RELEASED);
172 assert(timespec_eq(&keyboard->input_timestamp, &t_other));
173
174 input_timestamps_destroy(input_ts);
Pekka Paalanenef8d6522021-06-15 16:31:16 +0300175
176 free(client->input->keyboard);
177 client->input->keyboard = NULL;
178 client_destroy(client);
Alexandros Frantzisdae224c2017-12-13 13:27:57 +0200179}