• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2012 Jonas Ådahl
3  *
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:
11  *
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.
15  *
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
23  * SOFTWARE.
24  */
25 
26 #include <stdlib.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdbool.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <assert.h>
34 
35 #include "wayland-client.h"
36 #include "wayland-server.h"
37 #include "test-runner.h"
38 #include "test-compositor.h"
39 
40 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
41 
42 static void
registry_handle_global(void * data,struct wl_registry * registry,uint32_t id,const char * interface,uint32_t version)43 registry_handle_global(void *data, struct wl_registry *registry,
44 		       uint32_t id, const char *interface, uint32_t version)
45 {
46 	int *pcounter = data;
47 	(*pcounter)++;
48 	assert(*pcounter == 1);
49 	wl_registry_destroy(registry);
50 }
51 
52 static const struct wl_registry_listener registry_listener = {
53 	registry_handle_global,
54 	NULL
55 };
56 
57 /* Test that destroying a proxy object doesn't result in any more
58  * callback being invoked, even though were many queued. */
59 static void
client_test_proxy_destroy(void)60 client_test_proxy_destroy(void)
61 {
62 	struct wl_display *display;
63 	struct wl_registry *registry;
64 	int counter = 0;
65 
66 	display = wl_display_connect(NULL);
67 	assert(display);
68 
69 	registry = wl_display_get_registry(display);
70 	assert(registry != NULL);
71 	wl_registry_add_listener(registry, &registry_listener,
72 				 &counter);
73 	assert(wl_display_roundtrip(display) != -1);
74 
75 	assert(counter == 1);
76 
77 	/* don't destroy the registry, we have already destroyed them
78 	 * in the global handler */
79 	wl_display_disconnect(display);
80 }
81 
82 struct multiple_queues_state {
83 	struct wl_display *display;
84 	struct wl_callback* callback2;
85 	bool done;
86 };
87 
88 static void
sync_callback(void * data,struct wl_callback * callback,uint32_t serial)89 sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
90 {
91 	struct multiple_queues_state *state = data;
92 
93 	state->done = true;
94 	wl_callback_destroy(callback);
95 
96 	wl_display_dispatch_pending(state->display);
97 
98 	wl_callback_destroy(state->callback2);
99 }
100 
101 static const struct wl_callback_listener sync_listener = {
102 	sync_callback
103 };
104 
105 /* Test that when receiving the first of two synchronization
106  * callback events, destroying the second one doesn't cause any
107  * errors even if the delete_id event is handled out of order. */
108 static void
client_test_multiple_queues(void)109 client_test_multiple_queues(void)
110 {
111 	struct wl_event_queue *queue;
112 	struct wl_callback *callback1;
113 	struct multiple_queues_state state;
114 	int ret = 0;
115 
116 	state.display = wl_display_connect(NULL);
117 	assert(state.display);
118 
119 	queue = wl_display_create_queue(state.display);
120 	assert(queue);
121 
122 	state.done = false;
123 	callback1 = wl_display_sync(state.display);
124 	assert(callback1 != NULL);
125 	wl_callback_add_listener(callback1, &sync_listener, &state);
126 	wl_proxy_set_queue((struct wl_proxy *) callback1, queue);
127 
128 	state.callback2 = wl_display_sync(state.display);
129 	assert(state.callback2 != NULL);
130 	wl_callback_add_listener(state.callback2, &sync_listener, NULL);
131 	wl_proxy_set_queue((struct wl_proxy *) state.callback2, queue);
132 
133 	wl_display_flush(state.display);
134 
135 	while (!state.done && !ret)
136 		ret = wl_display_dispatch_queue(state.display, queue);
137 
138 	wl_event_queue_destroy(queue);
139 	wl_display_disconnect(state.display);
140 
141 	exit(ret == -1 ? -1 : 0);
142 }
143 
144 static void
sync_callback_roundtrip(void * data,struct wl_callback * callback,uint32_t serial)145 sync_callback_roundtrip(void *data, struct wl_callback *callback, uint32_t serial)
146 {
147 	bool *done = data;
148 	*done = true;
149 }
150 
151 static const struct wl_callback_listener sync_listener_roundtrip = {
152 	sync_callback_roundtrip
153 };
154 
155 /* Test that doing a roundtrip on a queue only the events on that
156  * queue get dispatched. */
157 static void
client_test_queue_roundtrip(void)158 client_test_queue_roundtrip(void)
159 {
160 	struct wl_event_queue *queue;
161 	struct wl_callback *callback1;
162 	struct wl_callback *callback2;
163 	struct wl_display *display;
164 	bool done1 = false;
165 	bool done2 = false;
166 
167 	display = wl_display_connect(NULL);
168 	assert(display);
169 
170 	queue = wl_display_create_queue(display);
171 	assert(queue);
172 
173 	/* arm a callback on the default queue */
174 	callback1 = wl_display_sync(display);
175 	assert(callback1 != NULL);
176 	wl_callback_add_listener(callback1, &sync_listener_roundtrip, &done1);
177 
178 	/* arm a callback on the other queue */
179 	callback2 = wl_display_sync(display);
180 	assert(callback2 != NULL);
181 	wl_callback_add_listener(callback2, &sync_listener_roundtrip, &done2);
182 	wl_proxy_set_queue((struct wl_proxy *) callback2, queue);
183 
184 	/* roundtrip on default queue must not dispatch the other queue. */
185 	wl_display_roundtrip(display);
186 	assert(done1 == true);
187 	assert(done2 == false);
188 
189 	/* re-arm the sync callback on the default queue, so we see that
190 	 * wl_display_roundtrip_queue() does not dispatch the default queue. */
191 	wl_callback_destroy(callback1);
192 	done1 = false;
193 	callback1 = wl_display_sync(display);
194 	assert(callback1 != NULL);
195 	wl_callback_add_listener(callback1, &sync_listener_roundtrip, &done1);
196 
197 	wl_display_roundtrip_queue(display, queue);
198 	assert(done1 == false);
199 	assert(done2 == true);
200 
201 	wl_callback_destroy(callback1);
202 	wl_callback_destroy(callback2);
203 	wl_event_queue_destroy(queue);
204 
205 	wl_display_disconnect(display);
206 }
207 
208 static void
client_test_queue_proxy_wrapper(void)209 client_test_queue_proxy_wrapper(void)
210 {
211 	struct wl_event_queue *queue;
212 	struct wl_display *display;
213 	struct wl_display *display_wrapper;
214 	struct wl_callback *callback;
215 	bool done = false;
216 
217 	/*
218 	 * For an illustration of what usage would normally fail without using
219 	 * proxy wrappers, see the `client_test_queue_set_queue_race' test case.
220 	 */
221 
222 	display = wl_display_connect(NULL);
223 	assert(display);
224 
225 	/* Pretend we are in a separate thread where a thread-local queue is
226 	 * used. */
227 	queue = wl_display_create_queue(display);
228 	assert(queue);
229 
230 	display_wrapper = wl_proxy_create_wrapper(display);
231 	assert(display_wrapper);
232 	wl_proxy_set_queue((struct wl_proxy *) display_wrapper, queue);
233 	callback = wl_display_sync(display_wrapper);
234 	wl_proxy_wrapper_destroy(display_wrapper);
235 	assert(callback != NULL);
236 
237 	/* Pretend we are now another thread and dispatch the dispatch the main
238 	 * queue while also knowing our callback is read and queued. */
239 	wl_display_roundtrip(display);
240 
241 	/* Make sure that the pretend-to-be main thread didn't dispatch our
242 	 * callback, behind our back. */
243 	wl_callback_add_listener(callback, &sync_listener_roundtrip, &done);
244 	wl_display_flush(display);
245 
246 	assert(!done);
247 
248 	/* Make sure that we eventually end up dispatching our callback. */
249 	while (!done)
250 		assert(wl_display_dispatch_queue(display, queue) != -1);
251 
252 	wl_callback_destroy(callback);
253 	wl_event_queue_destroy(queue);
254 
255 	wl_display_disconnect(display);
256 }
257 
258 static void
client_test_queue_set_queue_race(void)259 client_test_queue_set_queue_race(void)
260 {
261 	struct wl_event_queue *queue;
262 	struct wl_display *display;
263 	struct wl_callback *callback;
264 	bool done = false;
265 
266 	/*
267 	 * This test illustrates the multi threading scenario which would fail
268 	 * without doing what is done in the `client_test_queue_proxy_wrapper'
269 	 * test.
270 	 */
271 
272 	display = wl_display_connect(NULL);
273 	assert(display);
274 
275 	/* Pretend we are in a separate thread where a thread-local queue is
276 	 * used. */
277 	queue = wl_display_create_queue(display);
278 	assert(queue);
279 
280 	callback = wl_display_sync(display);
281 	assert(callback != NULL);
282 
283 	/* Pretend we are now another thread and dispatch the dispatch the main
284 	 * queue while also knowing our callback is read, queued on the wrong
285 	 * queue, and dispatched. */
286 	wl_display_roundtrip(display);
287 
288 	/* Pretend we are back in the separate thread, and continue with setting
289 	 * up our callback. */
290 	wl_callback_add_listener(callback, &sync_listener_roundtrip, &done);
291 	wl_proxy_set_queue((struct wl_proxy *) callback, queue);
292 
293 	/* Roundtrip our separate thread queue to make sure any events are
294 	 * dispatched. */
295 	wl_display_roundtrip_queue(display, queue);
296 
297 	/* Verify that the callback has indeed been dropped. */
298 	assert(!done);
299 
300 	wl_callback_destroy(callback);
301 	wl_event_queue_destroy(queue);
302 
303 	wl_display_disconnect(display);
304 }
305 
306 static void
dummy_bind(struct wl_client * client,void * data,uint32_t version,uint32_t id)307 dummy_bind(struct wl_client *client,
308 	   void *data, uint32_t version, uint32_t id)
309 {
310 }
311 
TEST(queue_proxy_destroy)312 TEST(queue_proxy_destroy)
313 {
314 	struct display *d;
315 	const struct wl_interface *dummy_interfaces[] = {
316 		&wl_seat_interface,
317 		&wl_pointer_interface,
318 		&wl_keyboard_interface,
319 		&wl_surface_interface
320 	};
321 	unsigned int i;
322 
323 	d = display_create();
324 
325 	for (i = 0; i < ARRAY_LENGTH(dummy_interfaces); i++)
326 		wl_global_create(d->wl_display, dummy_interfaces[i],
327 				 dummy_interfaces[i]->version,
328 				 NULL, dummy_bind);
329 
330 	test_set_timeout(2);
331 
332 	client_create_noarg(d, client_test_proxy_destroy);
333 	display_run(d);
334 
335 	display_destroy(d);
336 }
337 
TEST(queue_multiple_queues)338 TEST(queue_multiple_queues)
339 {
340 	struct display *d = display_create();
341 
342 	test_set_timeout(2);
343 
344 	client_create_noarg(d, client_test_multiple_queues);
345 	display_run(d);
346 
347 	display_destroy(d);
348 }
349 
TEST(queue_roundtrip)350 TEST(queue_roundtrip)
351 {
352 	struct display *d = display_create();
353 
354 	test_set_timeout(2);
355 
356 	client_create_noarg(d, client_test_queue_roundtrip);
357 	display_run(d);
358 
359 	display_destroy(d);
360 }
361 
TEST(queue_set_queue_proxy_wrapper)362 TEST(queue_set_queue_proxy_wrapper)
363 {
364 	struct display *d = display_create();
365 
366 	test_set_timeout(2);
367 
368 	client_create_noarg(d, client_test_queue_proxy_wrapper);
369 	display_run(d);
370 
371 	display_destroy(d);
372 }
373 
TEST(queue_set_queue_race)374 TEST(queue_set_queue_race)
375 {
376 	struct display *d = display_create();
377 
378 	test_set_timeout(2);
379 
380 	client_create_noarg(d, client_test_queue_set_queue_race);
381 	display_run(d);
382 
383 	display_destroy(d);
384 }
385