blob: 5d618c2e4423fe677d79912b7192f456b1cab69a [file] [log] [blame]
Alexandros Frantzis5d6acf82017-12-04 15:34:03 +02001/*
2 * Copyright © 2012 Intel Corporation
3 * Copyright © 2013 Collabora, Ltd.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27#include "config.h"
28
29#include <linux/input.h>
30
Alexandros Frantzisdb907b72018-02-20 14:06:26 +020031#include "input-timestamps-helper.h"
Alexandros Frantzis21808582017-12-13 13:27:55 +020032#include "shared/timespec-util.h"
Alexandros Frantzis5d6acf82017-12-04 15:34:03 +020033#include "weston-test-client-helper.h"
Pekka Paalanen701676d2019-11-13 15:45:10 +020034#include "weston-test-fixture-compositor.h"
35
36static enum test_result_code
37fixture_setup(struct weston_test_harness *harness)
38{
39 struct compositor_setup setup;
40
41 compositor_setup_defaults(&setup);
42
43 return weston_test_harness_execute_as_client(harness, &setup);
44}
45DECLARE_FIXTURE_SETUP(fixture_setup);
Alexandros Frantzis5d6acf82017-12-04 15:34:03 +020046
Alexandros Frantzis21808582017-12-13 13:27:55 +020047static const struct timespec t0 = { .tv_sec = 0, .tv_nsec = 100000000 };
48static const struct timespec t1 = { .tv_sec = 1, .tv_nsec = 1000001 };
49static const struct timespec t2 = { .tv_sec = 2, .tv_nsec = 2000001 };
Alexandros Frantzisdb907b72018-02-20 14:06:26 +020050static const struct timespec t_other = { .tv_sec = 123, .tv_nsec = 456 };
Alexandros Frantzis21808582017-12-13 13:27:55 +020051
52static void
53send_motion(struct client *client, const struct timespec *time, int x, int y)
54{
55 uint32_t tv_sec_hi, tv_sec_lo, tv_nsec;
56
57 timespec_to_proto(time, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
58 weston_test_move_pointer(client->test->weston_test, tv_sec_hi, tv_sec_lo,
59 tv_nsec, x, y);
60 client_roundtrip(client);
61}
62
63static void
64send_button(struct client *client, const struct timespec *time,
65 uint32_t button, 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_button(client->test->weston_test, tv_sec_hi, tv_sec_lo,
71 tv_nsec, button, state);
72 client_roundtrip(client);
73}
74
Alexandros Frantzis5d6acf82017-12-04 15:34:03 +020075static void
Alexandros Frantzisb0341ae2017-12-18 12:16:55 +020076send_axis(struct client *client, const struct timespec *time, uint32_t axis,
77 double value)
78{
79 uint32_t tv_sec_hi, tv_sec_lo, tv_nsec;
80
81 timespec_to_proto(time, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
82 weston_test_send_axis(client->test->weston_test, tv_sec_hi, tv_sec_lo,
83 tv_nsec, axis, wl_fixed_from_double(value));
84 client_roundtrip(client);
85}
86
87static void
Alexandros Frantzis5d6acf82017-12-04 15:34:03 +020088check_pointer(struct client *client, int x, int y)
89{
90 int sx, sy;
91
92 /* check that the client got the global pointer update */
93 assert(client->test->pointer_x == x);
94 assert(client->test->pointer_y == y);
95
96 /* Does global pointer map onto the surface? */
97 if (surface_contains(client->surface, x, y)) {
98 /* check that the surface has the pointer focus */
99 assert(client->input->pointer->focus == client->surface);
100
101 /*
102 * check that the local surface pointer maps
103 * to the global pointer.
104 */
105 sx = client->input->pointer->x + client->surface->x;
106 sy = client->input->pointer->y + client->surface->y;
107 assert(sx == x);
108 assert(sy == y);
109 } else {
110 /*
111 * The global pointer does not map onto surface. So
112 * check that it doesn't have the pointer focus.
113 */
114 assert(client->input->pointer->focus == NULL);
115 }
116}
117
118static void
119check_pointer_move(struct client *client, int x, int y)
120{
Alexandros Frantzis21808582017-12-13 13:27:55 +0200121 send_motion(client, &t0, x, y);
Alexandros Frantzis5d6acf82017-12-04 15:34:03 +0200122 check_pointer(client, x, y);
123}
124
Alexandros Frantzis903e4452017-12-04 15:34:04 +0200125static struct client *
126create_client_with_pointer_focus(int x, int y, int w, int h)
127{
128 struct client *cl = create_client_and_test_surface(x, y, w, h);
129 assert(cl);
130 /* Move the pointer inside the surface to ensure that the surface
131 * has the pointer focus. */
132 check_pointer_move(cl, x, y);
133 return cl;
134}
135
Alexandros Frantzis5d6acf82017-12-04 15:34:03 +0200136TEST(test_pointer_top_left)
137{
138 struct client *client;
139 int x, y;
140
141 client = create_client_and_test_surface(46, 76, 111, 134);
142 assert(client);
143
144 /* move pointer outside top left */
145 x = client->surface->x - 1;
146 y = client->surface->y - 1;
147 assert(!surface_contains(client->surface, x, y));
148 check_pointer_move(client, x, y);
149
150 /* move pointer on top left */
151 x += 1; y += 1;
152 assert(surface_contains(client->surface, x, y));
153 check_pointer_move(client, x, y);
154
155 /* move pointer outside top left */
156 x -= 1; y -= 1;
157 assert(!surface_contains(client->surface, x, y));
158 check_pointer_move(client, x, y);
159}
160
161TEST(test_pointer_bottom_left)
162{
163 struct client *client;
164 int x, y;
165
166 client = create_client_and_test_surface(99, 100, 100, 98);
167 assert(client);
168
169 /* move pointer outside bottom left */
170 x = client->surface->x - 1;
171 y = client->surface->y + client->surface->height;
172 assert(!surface_contains(client->surface, x, y));
173 check_pointer_move(client, x, y);
174
175 /* move pointer on bottom left */
176 x += 1; y -= 1;
177 assert(surface_contains(client->surface, x, y));
178 check_pointer_move(client, x, y);
179
180 /* move pointer outside bottom left */
181 x -= 1; y += 1;
182 assert(!surface_contains(client->surface, x, y));
183 check_pointer_move(client, x, y);
184}
185
186TEST(test_pointer_top_right)
187{
188 struct client *client;
189 int x, y;
190
191 client = create_client_and_test_surface(48, 100, 67, 100);
192 assert(client);
193
194 /* move pointer outside top right */
195 x = client->surface->x + client->surface->width;
196 y = client->surface->y - 1;
197 assert(!surface_contains(client->surface, x, y));
198 check_pointer_move(client, x, y);
199
200 /* move pointer on top right */
201 x -= 1; y += 1;
202 assert(surface_contains(client->surface, x, y));
203 check_pointer_move(client, x, y);
204
205 /* move pointer outside top right */
206 x += 1; y -= 1;
207 assert(!surface_contains(client->surface, x, y));
208 check_pointer_move(client, x, y);
209}
210
211TEST(test_pointer_bottom_right)
212{
213 struct client *client;
214 int x, y;
215
216 client = create_client_and_test_surface(100, 123, 100, 69);
217 assert(client);
218
219 /* move pointer outside bottom right */
220 x = client->surface->x + client->surface->width;
221 y = client->surface->y + client->surface->height;
222 assert(!surface_contains(client->surface, x, y));
223 check_pointer_move(client, x, y);
224
225 /* move pointer on bottom right */
226 x -= 1; y -= 1;
227 assert(surface_contains(client->surface, x, y));
228 check_pointer_move(client, x, y);
229
230 /* move pointer outside bottom right */
231 x += 1; y += 1;
232 assert(!surface_contains(client->surface, x, y));
233 check_pointer_move(client, x, y);
234}
235
236TEST(test_pointer_top_center)
237{
238 struct client *client;
239 int x, y;
240
241 client = create_client_and_test_surface(100, 201, 100, 50);
242 assert(client);
243
244 /* move pointer outside top center */
245 x = client->surface->x + client->surface->width/2;
246 y = client->surface->y - 1;
247 assert(!surface_contains(client->surface, x, y));
248 check_pointer_move(client, x, y);
249
250 /* move pointer on top center */
251 y += 1;
252 assert(surface_contains(client->surface, x, y));
253 check_pointer_move(client, x, y);
254
255 /* move pointer outside top center */
256 y -= 1;
257 assert(!surface_contains(client->surface, x, y));
258 check_pointer_move(client, x, y);
259}
260
261TEST(test_pointer_bottom_center)
262{
263 struct client *client;
264 int x, y;
265
266 client = create_client_and_test_surface(100, 45, 67, 100);
267 assert(client);
268
269 /* move pointer outside bottom center */
270 x = client->surface->x + client->surface->width/2;
271 y = client->surface->y + client->surface->height;
272 assert(!surface_contains(client->surface, x, y));
273 check_pointer_move(client, x, y);
274
275 /* move pointer on bottom center */
276 y -= 1;
277 assert(surface_contains(client->surface, x, y));
278 check_pointer_move(client, x, y);
279
280 /* move pointer outside bottom center */
281 y += 1;
282 assert(!surface_contains(client->surface, x, y));
283 check_pointer_move(client, x, y);
284}
285
286TEST(test_pointer_left_center)
287{
288 struct client *client;
289 int x, y;
290
291 client = create_client_and_test_surface(167, 45, 78, 100);
292 assert(client);
293
294 /* move pointer outside left center */
295 x = client->surface->x - 1;
296 y = client->surface->y + client->surface->height/2;
297 assert(!surface_contains(client->surface, x, y));
298 check_pointer_move(client, x, y);
299
300 /* move pointer on left center */
301 x += 1;
302 assert(surface_contains(client->surface, x, y));
303 check_pointer_move(client, x, y);
304
305 /* move pointer outside left center */
306 x -= 1;
307 assert(!surface_contains(client->surface, x, y));
308 check_pointer_move(client, x, y);
309}
310
311TEST(test_pointer_right_center)
312{
313 struct client *client;
314 int x, y;
315
316 client = create_client_and_test_surface(110, 37, 100, 46);
317 assert(client);
318
319 /* move pointer outside right center */
320 x = client->surface->x + client->surface->width;
321 y = client->surface->y + client->surface->height/2;
322 assert(!surface_contains(client->surface, x, y));
323 check_pointer_move(client, x, y);
324
325 /* move pointer on right center */
326 x -= 1;
327 assert(surface_contains(client->surface, x, y));
328 check_pointer_move(client, x, y);
329
330 /* move pointer outside right center */
331 x += 1;
332 assert(!surface_contains(client->surface, x, y));
333 check_pointer_move(client, x, y);
334}
335
336TEST(test_pointer_surface_move)
337{
338 struct client *client;
339
340 client = create_client_and_test_surface(100, 100, 100, 100);
341 assert(client);
342
343 /* move pointer outside of client */
344 assert(!surface_contains(client->surface, 50, 50));
345 check_pointer_move(client, 50, 50);
346
347 /* move client center to pointer */
348 move_client(client, 0, 0);
349 assert(surface_contains(client->surface, 50, 50));
350 check_pointer(client, 50, 50);
351}
352
Alexandros Frantzis903e4452017-12-04 15:34:04 +0200353TEST(pointer_motion_events)
Alexandros Frantzis5d6acf82017-12-04 15:34:03 +0200354{
Alexandros Frantzis903e4452017-12-04 15:34:04 +0200355 struct client *client = create_client_with_pointer_focus(100, 100,
356 100, 100);
357 struct pointer *pointer = client->input->pointer;
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200358 struct input_timestamps *input_ts =
359 input_timestamps_create_for_pointer(client);
Alexandros Frantzis5d6acf82017-12-04 15:34:03 +0200360
Alexandros Frantzis21808582017-12-13 13:27:55 +0200361 send_motion(client, &t1, 150, 150);
Alexandros Frantzis5d6acf82017-12-04 15:34:03 +0200362 assert(pointer->x == 50);
363 assert(pointer->y == 50);
Alexandros Frantzis21808582017-12-13 13:27:55 +0200364 assert(pointer->motion_time_msec == timespec_to_msec(&t1));
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200365 assert(timespec_eq(&pointer->motion_time_timespec, &t1));
366
367 input_timestamps_destroy(input_ts);
Alexandros Frantzis903e4452017-12-04 15:34:04 +0200368}
369
370TEST(pointer_button_events)
371{
372 struct client *client = create_client_with_pointer_focus(100, 100,
373 100, 100);
374 struct pointer *pointer = client->input->pointer;
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200375 struct input_timestamps *input_ts =
376 input_timestamps_create_for_pointer(client);
Alexandros Frantzis903e4452017-12-04 15:34:04 +0200377
378 assert(pointer->button == 0);
379 assert(pointer->state == 0);
Alexandros Frantzis5d6acf82017-12-04 15:34:03 +0200380
Alexandros Frantzis21808582017-12-13 13:27:55 +0200381 send_button(client, &t1, BTN_LEFT, WL_POINTER_BUTTON_STATE_PRESSED);
Alexandros Frantzis5d6acf82017-12-04 15:34:03 +0200382 assert(pointer->button == BTN_LEFT);
383 assert(pointer->state == WL_POINTER_BUTTON_STATE_PRESSED);
Alexandros Frantzis21808582017-12-13 13:27:55 +0200384 assert(pointer->button_time_msec == timespec_to_msec(&t1));
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200385 assert(timespec_eq(&pointer->button_time_timespec, &t1));
Alexandros Frantzis5d6acf82017-12-04 15:34:03 +0200386
Alexandros Frantzis21808582017-12-13 13:27:55 +0200387 send_button(client, &t2, BTN_LEFT, WL_POINTER_BUTTON_STATE_RELEASED);
Alexandros Frantzis5d6acf82017-12-04 15:34:03 +0200388 assert(pointer->button == BTN_LEFT);
389 assert(pointer->state == WL_POINTER_BUTTON_STATE_RELEASED);
Alexandros Frantzis21808582017-12-13 13:27:55 +0200390 assert(pointer->button_time_msec == timespec_to_msec(&t2));
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200391 assert(timespec_eq(&pointer->button_time_timespec, &t2));
392
393 input_timestamps_destroy(input_ts);
Alexandros Frantzis5d6acf82017-12-04 15:34:03 +0200394}
Alexandros Frantzisb0341ae2017-12-18 12:16:55 +0200395
396TEST(pointer_axis_events)
397{
398 struct client *client = create_client_with_pointer_focus(100, 100,
399 100, 100);
400 struct pointer *pointer = client->input->pointer;
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200401 struct input_timestamps *input_ts =
402 input_timestamps_create_for_pointer(client);
Alexandros Frantzisb0341ae2017-12-18 12:16:55 +0200403
404 send_axis(client, &t1, 1, 1.0);
405 assert(pointer->axis == 1);
406 assert(pointer->axis_value == 1.0);
407 assert(pointer->axis_time_msec == timespec_to_msec(&t1));
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200408 assert(timespec_eq(&pointer->axis_time_timespec, &t1));
Alexandros Frantzisb0341ae2017-12-18 12:16:55 +0200409
410 send_axis(client, &t2, 2, 0.0);
411 assert(pointer->axis == 2);
412 assert(pointer->axis_stop_time_msec == timespec_to_msec(&t2));
Alexandros Frantzisdb907b72018-02-20 14:06:26 +0200413 assert(timespec_eq(&pointer->axis_stop_time_timespec, &t2));
414
415 input_timestamps_destroy(input_ts);
416}
417
418TEST(pointer_timestamps_stop_after_input_timestamps_object_is_destroyed)
419{
420 struct client *client = create_client_with_pointer_focus(100, 100,
421 100, 100);
422 struct pointer *pointer = client->input->pointer;
423 struct input_timestamps *input_ts =
424 input_timestamps_create_for_pointer(client);
425
426 send_button(client, &t1, BTN_LEFT, WL_POINTER_BUTTON_STATE_PRESSED);
427 assert(pointer->button == BTN_LEFT);
428 assert(pointer->state == WL_POINTER_BUTTON_STATE_PRESSED);
429 assert(pointer->button_time_msec == timespec_to_msec(&t1));
430 assert(timespec_eq(&pointer->button_time_timespec, &t1));
431
432 input_timestamps_destroy(input_ts);
433
434 send_button(client, &t2, BTN_LEFT, WL_POINTER_BUTTON_STATE_RELEASED);
435 assert(pointer->button == BTN_LEFT);
436 assert(pointer->state == WL_POINTER_BUTTON_STATE_RELEASED);
437 assert(pointer->button_time_msec == timespec_to_msec(&t2));
438 assert(timespec_is_zero(&pointer->button_time_timespec));
439}
440
441TEST(pointer_timestamps_stop_after_client_releases_wl_pointer)
442{
443 struct client *client = create_client_with_pointer_focus(100, 100,
444 100, 100);
445 struct pointer *pointer = client->input->pointer;
446 struct input_timestamps *input_ts =
447 input_timestamps_create_for_pointer(client);
448
449 send_motion(client, &t1, 150, 150);
450 assert(pointer->x == 50);
451 assert(pointer->y == 50);
452 assert(pointer->motion_time_msec == timespec_to_msec(&t1));
453 assert(timespec_eq(&pointer->motion_time_timespec, &t1));
454
455 wl_pointer_release(client->input->pointer->wl_pointer);
456
457 /* Set input_timestamp to an arbitrary value (different from t1, t2
458 * and 0) and check that it is not changed by sending the event.
459 * This is preferred over just checking for 0, since 0 is used
460 * internally for resetting the timestamp after handling an input
461 * event and checking for it here may lead to false negatives. */
462 pointer->input_timestamp = t_other;
463 send_motion(client, &t2, 175, 175);
464 assert(timespec_eq(&pointer->input_timestamp, &t_other));
465
466 input_timestamps_destroy(input_ts);
Alexandros Frantzisb0341ae2017-12-18 12:16:55 +0200467}