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>
38 #include "wayland-client.h"
39 #include "wayland-server.h"
40 #include "test-runner.h"
41 #include "test-compositor.h"
43 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
46 registry_handle_global(void *data, struct wl_registry *registry,
47 uint32_t id, const char *interface, uint32_t version)
51 assert(*pcounter == 1);
52 wl_registry_destroy(registry);
55 static const struct wl_registry_listener registry_listener = {
56 registry_handle_global,
60 /* Test that destroying a proxy object doesn't result in any more
61 * callback being invoked, even though were many queued. */
63 client_test_proxy_destroy(void)
65 struct wl_display *display;
66 struct wl_registry *registry;
69 display = wl_display_connect(NULL);
72 registry = wl_display_get_registry(display);
73 assert(registry != NULL);
74 wl_registry_add_listener(registry, ®istry_listener,
76 assert(wl_display_roundtrip(display) != -1);
80 /* don't destroy the registry, we have already destroyed them
81 * in the global handler */
82 wl_display_disconnect(display);
85 struct multiple_queues_state {
86 struct wl_display *display;
87 struct wl_callback* callback2;
92 sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
94 struct multiple_queues_state *state = data;
97 wl_callback_destroy(callback);
99 wl_display_dispatch_pending(state->display);
101 wl_callback_destroy(state->callback2);
104 static const struct wl_callback_listener sync_listener = {
108 /* Test that when receiving the first of two synchronization
109 * callback events, destroying the second one doesn't cause any
110 * errors even if the delete_id event is handled out of order. */
112 client_test_multiple_queues(void)
114 struct wl_event_queue *queue;
115 struct wl_callback *callback1;
116 struct multiple_queues_state state;
119 state.display = wl_display_connect(NULL);
120 assert(state.display);
122 queue = wl_display_create_queue(state.display);
126 callback1 = wl_display_sync(state.display);
127 assert(callback1 != NULL);
128 wl_callback_add_listener(callback1, &sync_listener, &state);
129 wl_proxy_set_queue((struct wl_proxy *) callback1, queue);
131 state.callback2 = wl_display_sync(state.display);
132 assert(state.callback2 != NULL);
133 wl_callback_add_listener(state.callback2, &sync_listener, NULL);
134 wl_proxy_set_queue((struct wl_proxy *) state.callback2, queue);
136 wl_display_flush(state.display);
138 while (!state.done && !ret)
139 ret = wl_display_dispatch_queue(state.display, queue);
141 wl_event_queue_destroy(queue);
142 wl_display_disconnect(state.display);
144 exit(ret == -1 ? -1 : 0);
148 sync_callback_roundtrip(void *data, struct wl_callback *callback, uint32_t serial)
154 static const struct wl_callback_listener sync_listener_roundtrip = {
155 sync_callback_roundtrip
158 /* Test that doing a roundtrip on a queue only the events on that
159 * queue get dispatched. */
161 client_test_queue_roundtrip(void)
163 struct wl_event_queue *queue;
164 struct wl_callback *callback1;
165 struct wl_callback *callback2;
166 struct wl_display *display;
170 display = wl_display_connect(NULL);
173 queue = wl_display_create_queue(display);
176 /* arm a callback on the default queue */
177 callback1 = wl_display_sync(display);
178 assert(callback1 != NULL);
179 wl_callback_add_listener(callback1, &sync_listener_roundtrip, &done1);
181 /* arm a callback on the other queue */
182 callback2 = wl_display_sync(display);
183 assert(callback2 != NULL);
184 wl_callback_add_listener(callback2, &sync_listener_roundtrip, &done2);
185 wl_proxy_set_queue((struct wl_proxy *) callback2, queue);
187 /* roundtrip on default queue must not dispatch the other queue. */
188 wl_display_roundtrip(display);
189 assert(done1 == true);
190 assert(done2 == false);
192 /* re-arm the sync callback on the default queue, so we see that
193 * wl_display_roundtrip_queue() does not dispatch the default queue. */
194 wl_callback_destroy(callback1);
196 callback1 = wl_display_sync(display);
197 assert(callback1 != NULL);
198 wl_callback_add_listener(callback1, &sync_listener_roundtrip, &done1);
200 wl_display_roundtrip_queue(display, queue);
201 assert(done1 == false);
202 assert(done2 == true);
204 wl_callback_destroy(callback1);
205 wl_callback_destroy(callback2);
206 wl_event_queue_destroy(queue);
208 wl_display_disconnect(display);
212 client_test_queue_proxy_wrapper(void)
214 struct wl_event_queue *queue;
215 struct wl_display *display;
216 struct wl_display *display_wrapper;
217 struct wl_callback *callback;
221 * For an illustration of what usage would normally fail without using
222 * proxy wrappers, see the `client_test_queue_set_queue_race' test case.
225 display = wl_display_connect(NULL);
228 /* Pretend we are in a separate thread where a thread-local queue is
230 queue = wl_display_create_queue(display);
233 display_wrapper = wl_proxy_create_wrapper(display);
234 assert(display_wrapper);
235 wl_proxy_set_queue((struct wl_proxy *) display_wrapper, queue);
236 callback = wl_display_sync(display_wrapper);
237 wl_proxy_wrapper_destroy(display_wrapper);
238 assert(callback != NULL);
240 /* Pretend we are now another thread and dispatch the dispatch the main
241 * queue while also knowing our callback is read and queued. */
242 wl_display_roundtrip(display);
244 /* Make sure that the pretend-to-be main thread didn't dispatch our
245 * callback, behind our back. */
246 wl_callback_add_listener(callback, &sync_listener_roundtrip, &done);
247 wl_display_flush(display);
251 /* Make sure that we eventually end up dispatching our callback. */
253 assert(wl_display_dispatch_queue(display, queue) != -1);
255 wl_callback_destroy(callback);
256 wl_event_queue_destroy(queue);
258 wl_display_disconnect(display);
262 client_test_queue_set_queue_race(void)
264 struct wl_event_queue *queue;
265 struct wl_display *display;
266 struct wl_callback *callback;
270 * This test illustrates the multi threading scenario which would fail
271 * without doing what is done in the `client_test_queue_proxy_wrapper'
275 display = wl_display_connect(NULL);
278 /* Pretend we are in a separate thread where a thread-local queue is
280 queue = wl_display_create_queue(display);
283 callback = wl_display_sync(display);
284 assert(callback != NULL);
286 /* Pretend we are now another thread and dispatch the dispatch the main
287 * queue while also knowing our callback is read, queued on the wrong
288 * queue, and dispatched. */
289 wl_display_roundtrip(display);
291 /* Pretend we are back in the separate thread, and continue with setting
292 * up our callback. */
293 wl_callback_add_listener(callback, &sync_listener_roundtrip, &done);
294 wl_proxy_set_queue((struct wl_proxy *) callback, queue);
296 /* Roundtrip our separate thread queue to make sure any events are
298 wl_display_roundtrip_queue(display, queue);
300 /* Verify that the callback has indeed been dropped. */
303 wl_callback_destroy(callback);
304 wl_event_queue_destroy(queue);
306 wl_display_disconnect(display);
310 map_file(int fd, size_t *len)
314 *len = lseek(fd, 0, SEEK_END);
315 data = mmap(0, *len, PROT_READ, MAP_PRIVATE, fd, 0);
316 assert(data != MAP_FAILED && "Failed to mmap file");
322 last_line_of(char *s)
324 size_t len = strlen(s);
327 last = memrchr(s, '\n', len);
328 /* If we found a newline at end of string, find the previous one. */
329 if (last && last[1] == 0)
330 last = memrchr(s, '\n', len - 1);
331 /* If we have a newline, the last line starts after the newline.
332 * Otherwise, the whole string is the last line. */
342 client_test_queue_destroy_with_attached_proxies(void)
344 struct wl_event_queue *queue;
345 struct wl_display *display;
346 struct wl_display *display_wrapper;
347 struct wl_callback *callback;
350 char callback_name[24];
353 display = wl_display_connect(NULL);
356 /* Pretend we are in a separate thread where a thread-local queue is
358 queue = wl_display_create_queue(display);
361 /* Create a sync dispatching events on the thread-local queue. */
362 display_wrapper = wl_proxy_create_wrapper(display);
363 assert(display_wrapper);
364 wl_proxy_set_queue((struct wl_proxy *) display_wrapper, queue);
365 callback = wl_display_sync(display_wrapper);
366 wl_proxy_wrapper_destroy(display_wrapper);
367 assert(callback != NULL);
369 /* Destroy the queue before the attached object. */
370 wl_event_queue_destroy(queue);
372 /* Check that the log contains some information about the attached
373 * wl_callback proxy. */
374 log = map_file(client_log_fd, &log_len);
375 ret = snprintf(callback_name, sizeof(callback_name), "wl_callback@%u",
376 wl_proxy_get_id((struct wl_proxy *) callback));
377 assert(ret > 0 && ret < (int)sizeof(callback_name) &&
378 "callback name creation failed (possibly truncated)");
379 assert(strstr(last_line_of(log), callback_name));
380 munmap(log, log_len);
382 wl_callback_destroy(callback);
384 wl_display_disconnect(display);
388 dummy_bind(struct wl_client *client,
389 void *data, uint32_t version, uint32_t id)
393 TEST(queue_proxy_destroy)
396 const struct wl_interface *dummy_interfaces[] = {
398 &wl_pointer_interface,
399 &wl_keyboard_interface,
400 &wl_surface_interface
404 d = display_create();
406 for (i = 0; i < ARRAY_LENGTH(dummy_interfaces); i++)
407 wl_global_create(d->wl_display, dummy_interfaces[i],
408 dummy_interfaces[i]->version,
413 client_create_noarg(d, client_test_proxy_destroy);
419 TEST(queue_multiple_queues)
421 struct display *d = display_create();
425 client_create_noarg(d, client_test_multiple_queues);
431 TEST(queue_roundtrip)
433 struct display *d = display_create();
437 client_create_noarg(d, client_test_queue_roundtrip);
443 TEST(queue_set_queue_proxy_wrapper)
445 struct display *d = display_create();
449 client_create_noarg(d, client_test_queue_proxy_wrapper);
455 TEST(queue_set_queue_race)
457 struct display *d = display_create();
461 client_create_noarg(d, client_test_queue_set_queue_race);
467 TEST(queue_destroy_with_attached_proxies)
469 struct display *d = display_create();
473 client_create_noarg(d, client_test_queue_destroy_with_attached_proxies);