2 * Copyright © 2012 Jonas Ådahl
4 * 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:
12 * 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.
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
26 #define _GNU_SOURCE /* For memrchr */
34 #include <sys/types.h>
39 #include "wayland-client.h"
40 #include "wayland-server.h"
41 #include "test-runner.h"
42 #include "test-compositor.h"
44 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
47 registry_handle_global(void *data, struct wl_registry *registry,
48 uint32_t id, const char *interface, uint32_t version)
52 assert(*pcounter == 1);
53 wl_registry_destroy(registry);
56 static const struct wl_registry_listener registry_listener = {
57 registry_handle_global,
61 /* Test that destroying a proxy object doesn't result in any more
62 * callback being invoked, even though were many queued. */
64 client_test_proxy_destroy(void)
66 struct wl_display *display;
67 struct wl_registry *registry;
70 display = wl_display_connect(NULL);
73 registry = wl_display_get_registry(display);
74 assert(registry != NULL);
75 wl_registry_add_listener(registry, ®istry_listener,
77 assert(wl_display_roundtrip(display) != -1);
81 /* don't destroy the registry, we have already destroyed them
82 * in the global handler */
83 wl_display_disconnect(display);
86 struct multiple_queues_state {
87 struct wl_display *display;
88 struct wl_callback* callback2;
93 sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
95 struct multiple_queues_state *state = data;
98 wl_callback_destroy(callback);
100 wl_display_dispatch_pending(state->display);
102 wl_callback_destroy(state->callback2);
105 static const struct wl_callback_listener sync_listener = {
109 /* Test that when receiving the first of two synchronization
110 * callback events, destroying the second one doesn't cause any
111 * errors even if the delete_id event is handled out of order. */
113 client_test_multiple_queues(void)
115 struct wl_event_queue *queue;
116 struct wl_callback *callback1;
117 struct multiple_queues_state state;
120 state.display = wl_display_connect(NULL);
121 assert(state.display);
123 queue = wl_display_create_queue(state.display);
127 callback1 = wl_display_sync(state.display);
128 assert(callback1 != NULL);
129 wl_callback_add_listener(callback1, &sync_listener, &state);
130 wl_proxy_set_queue((struct wl_proxy *) callback1, queue);
132 state.callback2 = wl_display_sync(state.display);
133 assert(state.callback2 != NULL);
134 wl_callback_add_listener(state.callback2, &sync_listener, NULL);
135 wl_proxy_set_queue((struct wl_proxy *) state.callback2, queue);
137 wl_display_flush(state.display);
139 while (!state.done && !ret)
140 ret = wl_display_dispatch_queue(state.display, queue);
142 wl_event_queue_destroy(queue);
143 wl_display_disconnect(state.display);
145 exit(ret == -1 ? -1 : 0);
149 sync_callback_roundtrip(void *data, struct wl_callback *callback, uint32_t serial)
155 static const struct wl_callback_listener sync_listener_roundtrip = {
156 sync_callback_roundtrip
159 /* Test that doing a roundtrip on a queue only the events on that
160 * queue get dispatched. */
162 client_test_queue_roundtrip(void)
164 struct wl_event_queue *queue;
165 struct wl_callback *callback1;
166 struct wl_callback *callback2;
167 struct wl_display *display;
171 display = wl_display_connect(NULL);
174 queue = wl_display_create_queue(display);
177 /* arm a callback on the default queue */
178 callback1 = wl_display_sync(display);
179 assert(callback1 != NULL);
180 wl_callback_add_listener(callback1, &sync_listener_roundtrip, &done1);
182 /* arm a callback on the other queue */
183 callback2 = wl_display_sync(display);
184 assert(callback2 != NULL);
185 wl_callback_add_listener(callback2, &sync_listener_roundtrip, &done2);
186 wl_proxy_set_queue((struct wl_proxy *) callback2, queue);
188 /* roundtrip on default queue must not dispatch the other queue. */
189 wl_display_roundtrip(display);
190 assert(done1 == true);
191 assert(done2 == false);
193 /* re-arm the sync callback on the default queue, so we see that
194 * wl_display_roundtrip_queue() does not dispatch the default queue. */
195 wl_callback_destroy(callback1);
197 callback1 = wl_display_sync(display);
198 assert(callback1 != NULL);
199 wl_callback_add_listener(callback1, &sync_listener_roundtrip, &done1);
201 wl_display_roundtrip_queue(display, queue);
202 assert(done1 == false);
203 assert(done2 == true);
205 wl_callback_destroy(callback1);
206 wl_callback_destroy(callback2);
207 wl_event_queue_destroy(queue);
209 wl_display_disconnect(display);
213 client_test_queue_proxy_wrapper(void)
215 struct wl_event_queue *queue;
216 struct wl_display *display;
217 struct wl_display *display_wrapper;
218 struct wl_callback *callback;
222 * For an illustration of what usage would normally fail without using
223 * proxy wrappers, see the `client_test_queue_set_queue_race' test case.
226 display = wl_display_connect(NULL);
229 /* Pretend we are in a separate thread where a thread-local queue is
231 queue = wl_display_create_queue(display);
234 display_wrapper = wl_proxy_create_wrapper(display);
235 assert(display_wrapper);
236 wl_proxy_set_queue((struct wl_proxy *) display_wrapper, queue);
237 callback = wl_display_sync(display_wrapper);
238 wl_proxy_wrapper_destroy(display_wrapper);
239 assert(callback != NULL);
241 /* Pretend we are now another thread and dispatch the dispatch the main
242 * queue while also knowing our callback is read and queued. */
243 wl_display_roundtrip(display);
245 /* Make sure that the pretend-to-be main thread didn't dispatch our
246 * callback, behind our back. */
247 wl_callback_add_listener(callback, &sync_listener_roundtrip, &done);
248 wl_display_flush(display);
252 /* Make sure that we eventually end up dispatching our callback. */
254 assert(wl_display_dispatch_queue(display, queue) != -1);
256 wl_callback_destroy(callback);
257 wl_event_queue_destroy(queue);
259 wl_display_disconnect(display);
263 client_test_queue_set_queue_race(void)
265 struct wl_event_queue *queue;
266 struct wl_display *display;
267 struct wl_callback *callback;
271 * This test illustrates the multi threading scenario which would fail
272 * without doing what is done in the `client_test_queue_proxy_wrapper'
276 display = wl_display_connect(NULL);
279 /* Pretend we are in a separate thread where a thread-local queue is
281 queue = wl_display_create_queue(display);
284 callback = wl_display_sync(display);
285 assert(callback != NULL);
287 /* Pretend we are now another thread and dispatch the dispatch the main
288 * queue while also knowing our callback is read, queued on the wrong
289 * queue, and dispatched. */
290 wl_display_roundtrip(display);
292 /* Pretend we are back in the separate thread, and continue with setting
293 * up our callback. */
294 wl_callback_add_listener(callback, &sync_listener_roundtrip, &done);
295 wl_proxy_set_queue((struct wl_proxy *) callback, queue);
297 /* Roundtrip our separate thread queue to make sure any events are
299 wl_display_roundtrip_queue(display, queue);
301 /* Verify that the callback has indeed been dropped. */
304 wl_callback_destroy(callback);
305 wl_event_queue_destroy(queue);
307 wl_display_disconnect(display);
311 maybe_map_file(int fd, size_t *len)
315 *len = lseek(fd, 0, SEEK_END);
316 data = mmap(0, *len, PROT_READ, MAP_PRIVATE, fd, 0);
322 map_file(int fd, size_t *len)
326 data = maybe_map_file(fd, len);
327 assert(data != MAP_FAILED && "Failed to mmap file");
333 last_line_of(char *s)
335 size_t len = strlen(s);
338 last = memrchr(s, '\n', len);
339 /* If we found a newline at end of string, find the previous one. */
340 if (last && last[1] == 0)
341 last = memrchr(s, '\n', len - 1);
342 /* If we have a newline, the last line starts after the newline.
343 * Otherwise, the whole string is the last line. */
353 client_test_queue_destroy_with_attached_proxies(void)
355 struct wl_event_queue *queue;
356 struct wl_display *display;
357 struct wl_display *display_wrapper;
358 struct wl_callback *callback;
361 char callback_name[24];
364 display = wl_display_connect(NULL);
367 /* Pretend we are in a separate thread where a thread-local queue is
369 queue = wl_display_create_queue(display);
372 /* Create a sync dispatching events on the thread-local queue. */
373 display_wrapper = wl_proxy_create_wrapper(display);
374 assert(display_wrapper);
375 wl_proxy_set_queue((struct wl_proxy *) display_wrapper, queue);
376 callback = wl_display_sync(display_wrapper);
377 wl_proxy_wrapper_destroy(display_wrapper);
378 assert(callback != NULL);
380 /* Destroy the queue before the attached object. */
381 wl_event_queue_destroy(queue);
383 /* Check that the log contains some information about the attached
384 * wl_callback proxy. */
385 log = map_file(client_log_fd, &log_len);
386 ret = snprintf(callback_name, sizeof(callback_name), "wl_callback@%u",
387 wl_proxy_get_id((struct wl_proxy *) callback));
388 assert(ret > 0 && ret < (int)sizeof(callback_name) &&
389 "callback name creation failed (possibly truncated)");
390 assert(strstr(last_line_of(log), callback_name));
391 munmap(log, log_len);
393 wl_callback_destroy(callback);
395 wl_display_disconnect(display);
399 client_test_queue_proxy_event_to_destroyed_queue(void)
401 struct wl_event_queue *queue;
402 struct wl_display *display;
403 struct wl_display *display_wrapper;
404 struct wl_callback *callback;
406 display = wl_display_connect(NULL);
409 /* Pretend we are in a separate thread where a thread-local queue is
411 queue = wl_display_create_queue(display);
414 /* Create a sync dispatching events on the thread-local queue. */
415 display_wrapper = wl_proxy_create_wrapper(display);
416 assert(display_wrapper);
417 wl_proxy_set_queue((struct wl_proxy *) display_wrapper, queue);
418 callback = wl_display_sync(display_wrapper);
419 wl_proxy_wrapper_destroy(display_wrapper);
420 assert(callback != NULL);
421 wl_display_flush(display);
423 /* Destroy the queue before the attached object. */
424 wl_event_queue_destroy(queue);
426 /* During this roundtrip we should receive the done event on 'callback',
427 * try to queue it to the destroyed queue, and abort. */
428 wl_display_roundtrip(display);
430 wl_callback_destroy(callback);
432 wl_display_disconnect(display);
436 client_test_queue_destroy_default_with_attached_proxies(void)
438 struct wl_display *display;
439 struct wl_callback *callback;
442 char callback_name[24];
445 display = wl_display_connect(NULL);
448 /* Create a sync dispatching events on the default queue. */
449 callback = wl_display_sync(display);
450 assert(callback != NULL);
452 /* Destroy the default queue (by disconnecting) before the attached
454 wl_display_disconnect(display);
456 /* Check that the log does not contain any warning about the attached
457 * wl_callback proxy. */
458 log = maybe_map_file(client_log_fd, &log_len);
459 ret = snprintf(callback_name, sizeof(callback_name), "wl_callback@%u",
460 wl_proxy_get_id((struct wl_proxy *) callback));
461 assert(ret > 0 && ret < (int)sizeof(callback_name) &&
462 "callback name creation failed (possibly truncated)");
463 assert(log == MAP_FAILED || strstr(log, callback_name) == NULL);
464 if (log != MAP_FAILED)
465 munmap(log, log_len);
467 /* HACK: Directly free the memory of the wl_callback proxy to appease
468 * ASan. We would normally use wl_callback_destroy(), but since we have
469 * destroyed the associated wl_display, using this function would lead
470 * to memory errors. */
475 dummy_bind(struct wl_client *client,
476 void *data, uint32_t version, uint32_t id)
480 TEST(queue_proxy_destroy)
483 const struct wl_interface *dummy_interfaces[] = {
485 &wl_pointer_interface,
486 &wl_keyboard_interface,
487 &wl_surface_interface
491 d = display_create();
493 for (i = 0; i < ARRAY_LENGTH(dummy_interfaces); i++)
494 wl_global_create(d->wl_display, dummy_interfaces[i],
495 dummy_interfaces[i]->version,
500 client_create_noarg(d, client_test_proxy_destroy);
506 TEST(queue_multiple_queues)
508 struct display *d = display_create();
512 client_create_noarg(d, client_test_multiple_queues);
518 TEST(queue_roundtrip)
520 struct display *d = display_create();
524 client_create_noarg(d, client_test_queue_roundtrip);
530 TEST(queue_set_queue_proxy_wrapper)
532 struct display *d = display_create();
536 client_create_noarg(d, client_test_queue_proxy_wrapper);
542 TEST(queue_set_queue_race)
544 struct display *d = display_create();
548 client_create_noarg(d, client_test_queue_set_queue_race);
554 TEST(queue_destroy_with_attached_proxies)
556 struct display *d = display_create();
560 client_create_noarg(d, client_test_queue_destroy_with_attached_proxies);
566 TEST(queue_proxy_event_to_destroyed_queue)
568 struct display *d = display_create();
569 struct client_info *ci;
571 size_t client_log_len;
575 ci = client_create_noarg(d, client_test_queue_proxy_event_to_destroyed_queue);
578 /* Check that the final line in the log mentions the expected reason
580 client_log = map_file(ci->log_fd, &client_log_len);
581 assert(!strcmp(last_line_of(client_log),
582 "Tried to add event to destroyed queue\n"));
583 munmap(client_log, client_log_len);
585 /* Check that the client aborted. */
586 display_destroy_expect_signal(d, SIGABRT);
589 TEST(queue_destroy_default_with_attached_proxies)
591 struct display *d = display_create();
595 client_create_noarg(d, client_test_queue_destroy_default_with_attached_proxies);