2 * Copyright © 2012 Intel Corporation
3 * Copyright © 2013 Collabora, Ltd.
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:
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.
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
29 #include <linux/input.h>
31 #include "input-timestamps-helper.h"
32 #include "shared/timespec-util.h"
33 #include "weston-test-client-helper.h"
35 static const struct timespec t0 = { .tv_sec = 0, .tv_nsec = 100000000 };
36 static const struct timespec t1 = { .tv_sec = 1, .tv_nsec = 1000001 };
37 static const struct timespec t2 = { .tv_sec = 2, .tv_nsec = 2000001 };
38 static const struct timespec t_other = { .tv_sec = 123, .tv_nsec = 456 };
41 send_motion(struct client *client, const struct timespec *time, int x, int y)
43 uint32_t tv_sec_hi, tv_sec_lo, tv_nsec;
45 timespec_to_proto(time, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
46 weston_test_move_pointer(client->test->weston_test, tv_sec_hi, tv_sec_lo,
48 client_roundtrip(client);
52 send_button(struct client *client, const struct timespec *time,
53 uint32_t button, uint32_t state)
55 uint32_t tv_sec_hi, tv_sec_lo, tv_nsec;
57 timespec_to_proto(time, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
58 weston_test_send_button(client->test->weston_test, tv_sec_hi, tv_sec_lo,
59 tv_nsec, button, state);
60 client_roundtrip(client);
64 send_axis(struct client *client, const struct timespec *time, uint32_t axis,
67 uint32_t tv_sec_hi, tv_sec_lo, tv_nsec;
69 timespec_to_proto(time, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
70 weston_test_send_axis(client->test->weston_test, tv_sec_hi, tv_sec_lo,
71 tv_nsec, axis, wl_fixed_from_double(value));
72 client_roundtrip(client);
76 check_pointer(struct client *client, int x, int y)
80 /* check that the client got the global pointer update */
81 assert(client->test->pointer_x == x);
82 assert(client->test->pointer_y == y);
84 /* Does global pointer map onto the surface? */
85 if (surface_contains(client->surface, x, y)) {
86 /* check that the surface has the pointer focus */
87 assert(client->input->pointer->focus == client->surface);
90 * check that the local surface pointer maps
91 * to the global pointer.
93 sx = client->input->pointer->x + client->surface->x;
94 sy = client->input->pointer->y + client->surface->y;
99 * The global pointer does not map onto surface. So
100 * check that it doesn't have the pointer focus.
102 assert(client->input->pointer->focus == NULL);
107 check_pointer_move(struct client *client, int x, int y)
109 send_motion(client, &t0, x, y);
110 check_pointer(client, x, y);
113 static struct client *
114 create_client_with_pointer_focus(int x, int y, int w, int h)
116 struct client *cl = create_client_and_test_surface(x, y, w, h);
118 /* Move the pointer inside the surface to ensure that the surface
119 * has the pointer focus. */
120 check_pointer_move(cl, x, y);
124 TEST(test_pointer_top_left)
126 struct client *client;
129 client = create_client_and_test_surface(46, 76, 111, 134);
132 /* move pointer outside top left */
133 x = client->surface->x - 1;
134 y = client->surface->y - 1;
135 assert(!surface_contains(client->surface, x, y));
136 check_pointer_move(client, x, y);
138 /* move pointer on top left */
140 assert(surface_contains(client->surface, x, y));
141 check_pointer_move(client, x, y);
143 /* move pointer outside top left */
145 assert(!surface_contains(client->surface, x, y));
146 check_pointer_move(client, x, y);
149 TEST(test_pointer_bottom_left)
151 struct client *client;
154 client = create_client_and_test_surface(99, 100, 100, 98);
157 /* move pointer outside bottom left */
158 x = client->surface->x - 1;
159 y = client->surface->y + client->surface->height;
160 assert(!surface_contains(client->surface, x, y));
161 check_pointer_move(client, x, y);
163 /* move pointer on bottom left */
165 assert(surface_contains(client->surface, x, y));
166 check_pointer_move(client, x, y);
168 /* move pointer outside bottom left */
170 assert(!surface_contains(client->surface, x, y));
171 check_pointer_move(client, x, y);
174 TEST(test_pointer_top_right)
176 struct client *client;
179 client = create_client_and_test_surface(48, 100, 67, 100);
182 /* move pointer outside top right */
183 x = client->surface->x + client->surface->width;
184 y = client->surface->y - 1;
185 assert(!surface_contains(client->surface, x, y));
186 check_pointer_move(client, x, y);
188 /* move pointer on top right */
190 assert(surface_contains(client->surface, x, y));
191 check_pointer_move(client, x, y);
193 /* move pointer outside top right */
195 assert(!surface_contains(client->surface, x, y));
196 check_pointer_move(client, x, y);
199 TEST(test_pointer_bottom_right)
201 struct client *client;
204 client = create_client_and_test_surface(100, 123, 100, 69);
207 /* move pointer outside bottom right */
208 x = client->surface->x + client->surface->width;
209 y = client->surface->y + client->surface->height;
210 assert(!surface_contains(client->surface, x, y));
211 check_pointer_move(client, x, y);
213 /* move pointer on bottom right */
215 assert(surface_contains(client->surface, x, y));
216 check_pointer_move(client, x, y);
218 /* move pointer outside bottom right */
220 assert(!surface_contains(client->surface, x, y));
221 check_pointer_move(client, x, y);
224 TEST(test_pointer_top_center)
226 struct client *client;
229 client = create_client_and_test_surface(100, 201, 100, 50);
232 /* move pointer outside top center */
233 x = client->surface->x + client->surface->width/2;
234 y = client->surface->y - 1;
235 assert(!surface_contains(client->surface, x, y));
236 check_pointer_move(client, x, y);
238 /* move pointer on top center */
240 assert(surface_contains(client->surface, x, y));
241 check_pointer_move(client, x, y);
243 /* move pointer outside top center */
245 assert(!surface_contains(client->surface, x, y));
246 check_pointer_move(client, x, y);
249 TEST(test_pointer_bottom_center)
251 struct client *client;
254 client = create_client_and_test_surface(100, 45, 67, 100);
257 /* move pointer outside bottom center */
258 x = client->surface->x + client->surface->width/2;
259 y = client->surface->y + client->surface->height;
260 assert(!surface_contains(client->surface, x, y));
261 check_pointer_move(client, x, y);
263 /* move pointer on bottom center */
265 assert(surface_contains(client->surface, x, y));
266 check_pointer_move(client, x, y);
268 /* move pointer outside bottom center */
270 assert(!surface_contains(client->surface, x, y));
271 check_pointer_move(client, x, y);
274 TEST(test_pointer_left_center)
276 struct client *client;
279 client = create_client_and_test_surface(167, 45, 78, 100);
282 /* move pointer outside left center */
283 x = client->surface->x - 1;
284 y = client->surface->y + client->surface->height/2;
285 assert(!surface_contains(client->surface, x, y));
286 check_pointer_move(client, x, y);
288 /* move pointer on left center */
290 assert(surface_contains(client->surface, x, y));
291 check_pointer_move(client, x, y);
293 /* move pointer outside left center */
295 assert(!surface_contains(client->surface, x, y));
296 check_pointer_move(client, x, y);
299 TEST(test_pointer_right_center)
301 struct client *client;
304 client = create_client_and_test_surface(110, 37, 100, 46);
307 /* move pointer outside right center */
308 x = client->surface->x + client->surface->width;
309 y = client->surface->y + client->surface->height/2;
310 assert(!surface_contains(client->surface, x, y));
311 check_pointer_move(client, x, y);
313 /* move pointer on right center */
315 assert(surface_contains(client->surface, x, y));
316 check_pointer_move(client, x, y);
318 /* move pointer outside right center */
320 assert(!surface_contains(client->surface, x, y));
321 check_pointer_move(client, x, y);
324 TEST(test_pointer_surface_move)
326 struct client *client;
328 client = create_client_and_test_surface(100, 100, 100, 100);
331 /* move pointer outside of client */
332 assert(!surface_contains(client->surface, 50, 50));
333 check_pointer_move(client, 50, 50);
335 /* move client center to pointer */
336 move_client(client, 0, 0);
337 assert(surface_contains(client->surface, 50, 50));
338 check_pointer(client, 50, 50);
341 TEST(pointer_motion_events)
343 struct client *client = create_client_with_pointer_focus(100, 100,
345 struct pointer *pointer = client->input->pointer;
346 struct input_timestamps *input_ts =
347 input_timestamps_create_for_pointer(client);
349 send_motion(client, &t1, 150, 150);
350 assert(pointer->x == 50);
351 assert(pointer->y == 50);
352 assert(pointer->motion_time_msec == timespec_to_msec(&t1));
353 assert(timespec_eq(&pointer->motion_time_timespec, &t1));
355 input_timestamps_destroy(input_ts);
358 TEST(pointer_button_events)
360 struct client *client = create_client_with_pointer_focus(100, 100,
362 struct pointer *pointer = client->input->pointer;
363 struct input_timestamps *input_ts =
364 input_timestamps_create_for_pointer(client);
366 assert(pointer->button == 0);
367 assert(pointer->state == 0);
369 send_button(client, &t1, BTN_LEFT, WL_POINTER_BUTTON_STATE_PRESSED);
370 assert(pointer->button == BTN_LEFT);
371 assert(pointer->state == WL_POINTER_BUTTON_STATE_PRESSED);
372 assert(pointer->button_time_msec == timespec_to_msec(&t1));
373 assert(timespec_eq(&pointer->button_time_timespec, &t1));
375 send_button(client, &t2, BTN_LEFT, WL_POINTER_BUTTON_STATE_RELEASED);
376 assert(pointer->button == BTN_LEFT);
377 assert(pointer->state == WL_POINTER_BUTTON_STATE_RELEASED);
378 assert(pointer->button_time_msec == timespec_to_msec(&t2));
379 assert(timespec_eq(&pointer->button_time_timespec, &t2));
381 input_timestamps_destroy(input_ts);
384 TEST(pointer_axis_events)
386 struct client *client = create_client_with_pointer_focus(100, 100,
388 struct pointer *pointer = client->input->pointer;
389 struct input_timestamps *input_ts =
390 input_timestamps_create_for_pointer(client);
392 send_axis(client, &t1, 1, 1.0);
393 assert(pointer->axis == 1);
394 assert(pointer->axis_value == 1.0);
395 assert(pointer->axis_time_msec == timespec_to_msec(&t1));
396 assert(timespec_eq(&pointer->axis_time_timespec, &t1));
398 send_axis(client, &t2, 2, 0.0);
399 assert(pointer->axis == 2);
400 assert(pointer->axis_stop_time_msec == timespec_to_msec(&t2));
401 assert(timespec_eq(&pointer->axis_stop_time_timespec, &t2));
403 input_timestamps_destroy(input_ts);
406 TEST(pointer_timestamps_stop_after_input_timestamps_object_is_destroyed)
408 struct client *client = create_client_with_pointer_focus(100, 100,
410 struct pointer *pointer = client->input->pointer;
411 struct input_timestamps *input_ts =
412 input_timestamps_create_for_pointer(client);
414 send_button(client, &t1, BTN_LEFT, WL_POINTER_BUTTON_STATE_PRESSED);
415 assert(pointer->button == BTN_LEFT);
416 assert(pointer->state == WL_POINTER_BUTTON_STATE_PRESSED);
417 assert(pointer->button_time_msec == timespec_to_msec(&t1));
418 assert(timespec_eq(&pointer->button_time_timespec, &t1));
420 input_timestamps_destroy(input_ts);
422 send_button(client, &t2, BTN_LEFT, WL_POINTER_BUTTON_STATE_RELEASED);
423 assert(pointer->button == BTN_LEFT);
424 assert(pointer->state == WL_POINTER_BUTTON_STATE_RELEASED);
425 assert(pointer->button_time_msec == timespec_to_msec(&t2));
426 assert(timespec_is_zero(&pointer->button_time_timespec));
429 TEST(pointer_timestamps_stop_after_client_releases_wl_pointer)
431 struct client *client = create_client_with_pointer_focus(100, 100,
433 struct pointer *pointer = client->input->pointer;
434 struct input_timestamps *input_ts =
435 input_timestamps_create_for_pointer(client);
437 send_motion(client, &t1, 150, 150);
438 assert(pointer->x == 50);
439 assert(pointer->y == 50);
440 assert(pointer->motion_time_msec == timespec_to_msec(&t1));
441 assert(timespec_eq(&pointer->motion_time_timespec, &t1));
443 wl_pointer_release(client->input->pointer->wl_pointer);
445 /* Set input_timestamp to an arbitrary value (different from t1, t2
446 * and 0) and check that it is not changed by sending the event.
447 * This is preferred over just checking for 0, since 0 is used
448 * internally for resetting the timestamp after handling an input
449 * event and checking for it here may lead to false negatives. */
450 pointer->input_timestamp = t_other;
451 send_motion(client, &t2, 175, 175);
452 assert(timespec_eq(&pointer->input_timestamp, &t_other));
454 input_timestamps_destroy(input_ts);