1 /*
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
31 #include "input-timestamps-helper.h"
32 #include "shared/timespec-util.h"
33 #include "weston-test-client-helper.h"
34 #include "weston-test-fixture-compositor.h"
35
36 static enum test_result_code
fixture_setup(struct weston_test_harness * harness)37 fixture_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 }
45 DECLARE_FIXTURE_SETUP(fixture_setup);
46
47 static const struct timespec t0 = { .tv_sec = 0, .tv_nsec = 100000000 };
48 static const struct timespec t1 = { .tv_sec = 1, .tv_nsec = 1000001 };
49 static const struct timespec t2 = { .tv_sec = 2, .tv_nsec = 2000001 };
50 static const struct timespec t_other = { .tv_sec = 123, .tv_nsec = 456 };
51
52 static void
send_motion(struct client * client,const struct timespec * time,int x,int y)53 send_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
63 static void
send_button(struct client * client,const struct timespec * time,uint32_t button,uint32_t state)64 send_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
75 static void
send_axis(struct client * client,const struct timespec * time,uint32_t axis,double value)76 send_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
87 static void
check_pointer(struct client * client,int x,int y)88 check_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
118 static void
check_pointer_move(struct client * client,int x,int y)119 check_pointer_move(struct client *client, int x, int y)
120 {
121 send_motion(client, &t0, x, y);
122 check_pointer(client, x, y);
123 }
124
125 static struct client *
create_client_with_pointer_focus(int x,int y,int w,int h)126 create_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
TEST(test_pointer_top_left)136 TEST(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
TEST(test_pointer_bottom_left)161 TEST(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
TEST(test_pointer_top_right)186 TEST(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
TEST(test_pointer_bottom_right)211 TEST(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
TEST(test_pointer_top_center)236 TEST(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
TEST(test_pointer_bottom_center)261 TEST(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
TEST(test_pointer_left_center)286 TEST(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
TEST(test_pointer_right_center)311 TEST(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
TEST(test_pointer_surface_move)336 TEST(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
TEST(pointer_motion_events)353 TEST(pointer_motion_events)
354 {
355 struct client *client = create_client_with_pointer_focus(100, 100,
356 100, 100);
357 struct pointer *pointer = client->input->pointer;
358 struct input_timestamps *input_ts =
359 input_timestamps_create_for_pointer(client);
360
361 send_motion(client, &t1, 150, 150);
362 assert(pointer->x == 50);
363 assert(pointer->y == 50);
364 assert(pointer->motion_time_msec == timespec_to_msec(&t1));
365 assert(timespec_eq(&pointer->motion_time_timespec, &t1));
366
367 input_timestamps_destroy(input_ts);
368 }
369
TEST(pointer_button_events)370 TEST(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;
375 struct input_timestamps *input_ts =
376 input_timestamps_create_for_pointer(client);
377
378 assert(pointer->button == 0);
379 assert(pointer->state == 0);
380
381 send_button(client, &t1, BTN_LEFT, WL_POINTER_BUTTON_STATE_PRESSED);
382 assert(pointer->button == BTN_LEFT);
383 assert(pointer->state == WL_POINTER_BUTTON_STATE_PRESSED);
384 assert(pointer->button_time_msec == timespec_to_msec(&t1));
385 assert(timespec_eq(&pointer->button_time_timespec, &t1));
386
387 send_button(client, &t2, BTN_LEFT, WL_POINTER_BUTTON_STATE_RELEASED);
388 assert(pointer->button == BTN_LEFT);
389 assert(pointer->state == WL_POINTER_BUTTON_STATE_RELEASED);
390 assert(pointer->button_time_msec == timespec_to_msec(&t2));
391 assert(timespec_eq(&pointer->button_time_timespec, &t2));
392
393 input_timestamps_destroy(input_ts);
394 }
395
TEST(pointer_axis_events)396 TEST(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;
401 struct input_timestamps *input_ts =
402 input_timestamps_create_for_pointer(client);
403
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));
408 assert(timespec_eq(&pointer->axis_time_timespec, &t1));
409
410 send_axis(client, &t2, 2, 0.0);
411 assert(pointer->axis == 2);
412 assert(pointer->axis_stop_time_msec == timespec_to_msec(&t2));
413 assert(timespec_eq(&pointer->axis_stop_time_timespec, &t2));
414
415 input_timestamps_destroy(input_ts);
416 }
417
TEST(pointer_timestamps_stop_after_input_timestamps_object_is_destroyed)418 TEST(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
TEST(pointer_timestamps_stop_after_client_releases_wl_pointer)441 TEST(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);
467 }
468