queue-test.c 9.82 KB
Newer Older
1 2 3
/*
 * Copyright © 2012 Jonas Ådahl
 *
4 5 6 7 8 9 10
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
11
 *
12 13 14 15 16 17 18 19 20 21 22 23
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
24 25 26
 */

#include <stdlib.h>
27
#include <stdint.h>
28
#include <stdio.h>
29
#include <stdbool.h>
30 31 32 33 34 35 36 37
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>

#include "wayland-client.h"
#include "wayland-server.h"
#include "test-runner.h"
38
#include "test-compositor.h"
39 40 41 42 43 44 45 46 47

#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])

static void
registry_handle_global(void *data, struct wl_registry *registry,
		       uint32_t id, const char *interface, uint32_t version)
{
	int *pcounter = data;
	(*pcounter)++;
48
	assert(*pcounter == 1);
49 50 51 52 53 54 55 56
	wl_registry_destroy(registry);
}

static const struct wl_registry_listener registry_listener = {
	registry_handle_global,
	NULL
};

57 58
/* Test that destroying a proxy object doesn't result in any more
 * callback being invoked, even though were many queued. */
59
static void
60 61 62 63 64 65
client_test_proxy_destroy(void)
{
	struct wl_display *display;
	struct wl_registry *registry;
	int counter = 0;

66 67
	display = wl_display_connect(NULL);
	assert(display);
68 69

	registry = wl_display_get_registry(display);
70
	assert(registry != NULL);
71 72
	wl_registry_add_listener(registry, &registry_listener,
				 &counter);
73
	assert(wl_display_roundtrip(display) != -1);
74

75
	assert(counter == 1);
76

77 78
	/* don't destroy the registry, we have already destroyed them
	 * in the global handler */
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	wl_display_disconnect(display);
}

struct multiple_queues_state {
	struct wl_display *display;
	struct wl_callback* callback2;
	bool done;
};

static void
sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
{
	struct multiple_queues_state *state = data;

	state->done = true;
	wl_callback_destroy(callback);

	wl_display_dispatch_pending(state->display);

	wl_callback_destroy(state->callback2);
}

static const struct wl_callback_listener sync_listener = {
	sync_callback
};

/* Test that when receiving the first of two synchronization
 * callback events, destroying the second one doesn't cause any
 * errors even if the delete_id event is handled out of order. */
108
static void
109 110 111 112 113 114 115
client_test_multiple_queues(void)
{
	struct wl_event_queue *queue;
	struct wl_callback *callback1;
	struct multiple_queues_state state;
	int ret = 0;

116 117
	state.display = wl_display_connect(NULL);
	assert(state.display);
118 119

	queue = wl_display_create_queue(state.display);
120
	assert(queue);
121 122 123

	state.done = false;
	callback1 = wl_display_sync(state.display);
124
	assert(callback1 != NULL);
125 126 127 128
	wl_callback_add_listener(callback1, &sync_listener, &state);
	wl_proxy_set_queue((struct wl_proxy *) callback1, queue);

	state.callback2 = wl_display_sync(state.display);
129
	assert(state.callback2 != NULL);
130 131 132 133 134 135 136 137
	wl_callback_add_listener(state.callback2, &sync_listener, NULL);
	wl_proxy_set_queue((struct wl_proxy *) state.callback2, queue);

	wl_display_flush(state.display);

	while (!state.done && !ret)
		ret = wl_display_dispatch_queue(state.display, queue);

138
	wl_event_queue_destroy(queue);
139 140
	wl_display_disconnect(state.display);

141
	exit(ret == -1 ? -1 : 0);
142 143
}

144 145 146 147 148 149 150 151 152 153 154 155 156
static void
sync_callback_roundtrip(void *data, struct wl_callback *callback, uint32_t serial)
{
	bool *done = data;
	*done = true;
}

static const struct wl_callback_listener sync_listener_roundtrip = {
	sync_callback_roundtrip
};

/* Test that doing a roundtrip on a queue only the events on that
 * queue get dispatched. */
157
static void
158 159 160 161 162 163 164 165 166
client_test_queue_roundtrip(void)
{
	struct wl_event_queue *queue;
	struct wl_callback *callback1;
	struct wl_callback *callback2;
	struct wl_display *display;
	bool done1 = false;
	bool done2 = false;

167 168
	display = wl_display_connect(NULL);
	assert(display);
169 170

	queue = wl_display_create_queue(display);
171
	assert(queue);
172 173 174 175 176 177 178 179 180 181 182 183 184 185

	/* arm a callback on the default queue */
	callback1 = wl_display_sync(display);
	assert(callback1 != NULL);
	wl_callback_add_listener(callback1, &sync_listener_roundtrip, &done1);

	/* arm a callback on the other queue */
	callback2 = wl_display_sync(display);
	assert(callback2 != NULL);
	wl_callback_add_listener(callback2, &sync_listener_roundtrip, &done2);
	wl_proxy_set_queue((struct wl_proxy *) callback2, queue);

	/* roundtrip on default queue must not dispatch the other queue. */
	wl_display_roundtrip(display);
186 187
	assert(done1 == true);
	assert(done2 == false);
188 189 190 191 192 193 194 195 196 197

	/* re-arm the sync callback on the default queue, so we see that
	 * wl_display_roundtrip_queue() does not dispatch the default queue. */
	wl_callback_destroy(callback1);
	done1 = false;
	callback1 = wl_display_sync(display);
	assert(callback1 != NULL);
	wl_callback_add_listener(callback1, &sync_listener_roundtrip, &done1);

	wl_display_roundtrip_queue(display, queue);
198 199
	assert(done1 == false);
	assert(done2 == true);
200 201 202

	wl_callback_destroy(callback1);
	wl_callback_destroy(callback2);
203 204
	wl_event_queue_destroy(queue);

205
	wl_display_disconnect(display);
206 207
}

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
static void
client_test_queue_proxy_wrapper(void)
{
	struct wl_event_queue *queue;
	struct wl_display *display;
	struct wl_display *display_wrapper;
	struct wl_callback *callback;
	bool done = false;

	/*
	 * For an illustration of what usage would normally fail without using
	 * proxy wrappers, see the `client_test_queue_set_queue_race' test case.
	 */

	display = wl_display_connect(NULL);
	assert(display);

	/* Pretend we are in a separate thread where a thread-local queue is
	 * used. */
	queue = wl_display_create_queue(display);
	assert(queue);

	display_wrapper = wl_proxy_create_wrapper(display);
	assert(display_wrapper);
	wl_proxy_set_queue((struct wl_proxy *) display_wrapper, queue);
	callback = wl_display_sync(display_wrapper);
	wl_proxy_wrapper_destroy(display_wrapper);
	assert(callback != NULL);

	/* Pretend we are now another thread and dispatch the dispatch the main
	 * queue while also knowing our callback is read and queued. */
	wl_display_roundtrip(display);

	/* Make sure that the pretend-to-be main thread didn't dispatch our
	 * callback, behind our back. */
	wl_callback_add_listener(callback, &sync_listener_roundtrip, &done);
	wl_display_flush(display);

	assert(!done);

	/* Make sure that we eventually end up dispatching our callback. */
	while (!done)
		assert(wl_display_dispatch_queue(display, queue) != -1);

	wl_callback_destroy(callback);
	wl_event_queue_destroy(queue);

	wl_display_disconnect(display);
}

static void
client_test_queue_set_queue_race(void)
{
	struct wl_event_queue *queue;
	struct wl_display *display;
	struct wl_callback *callback;
	bool done = false;

	/*
	 * This test illustrates the multi threading scenario which would fail
	 * without doing what is done in the `client_test_queue_proxy_wrapper'
	 * test.
	 */

	display = wl_display_connect(NULL);
	assert(display);

	/* Pretend we are in a separate thread where a thread-local queue is
	 * used. */
	queue = wl_display_create_queue(display);
	assert(queue);

	callback = wl_display_sync(display);
	assert(callback != NULL);

	/* Pretend we are now another thread and dispatch the dispatch the main
	 * queue while also knowing our callback is read, queued on the wrong
	 * queue, and dispatched. */
	wl_display_roundtrip(display);

	/* Pretend we are back in the separate thread, and continue with setting
	 * up our callback. */
	wl_callback_add_listener(callback, &sync_listener_roundtrip, &done);
	wl_proxy_set_queue((struct wl_proxy *) callback, queue);

	/* Roundtrip our separate thread queue to make sure any events are
	 * dispatched. */
	wl_display_roundtrip_queue(display, queue);

	/* Verify that the callback has indeed been dropped. */
	assert(!done);

	wl_callback_destroy(callback);
	wl_event_queue_destroy(queue);

	wl_display_disconnect(display);
}

306 307 308 309 310 311
static void
dummy_bind(struct wl_client *client,
	   void *data, uint32_t version, uint32_t id)
{
}

312
TEST(queue_proxy_destroy)
313
{
314
	struct display *d;
315 316 317 318 319 320 321
	const struct wl_interface *dummy_interfaces[] = {
		&wl_seat_interface,
		&wl_pointer_interface,
		&wl_keyboard_interface,
		&wl_surface_interface
	};
	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 328
				 dummy_interfaces[i]->version,
				 NULL, dummy_bind);
329

330 331
	test_set_timeout(2);

332
	client_create_noarg(d, client_test_proxy_destroy);
333
	display_run(d);
334

335 336 337 338 339 340 341 342 343
	display_destroy(d);
}

TEST(queue_multiple_queues)
{
	struct display *d = display_create();

	test_set_timeout(2);

344
	client_create_noarg(d, client_test_multiple_queues);
345
	display_run(d);
346

347 348 349 350 351 352 353 354 355
	display_destroy(d);
}

TEST(queue_roundtrip)
{
	struct display *d = display_create();

	test_set_timeout(2);

356
	client_create_noarg(d, client_test_queue_roundtrip);
357
	display_run(d);
358

359
	display_destroy(d);
360
}
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

TEST(queue_set_queue_proxy_wrapper)
{
	struct display *d = display_create();

	test_set_timeout(2);

	client_create_noarg(d, client_test_queue_proxy_wrapper);
	display_run(d);

	display_destroy(d);
}

TEST(queue_set_queue_race)
{
	struct display *d = display_create();

	test_set_timeout(2);

	client_create_noarg(d, client_test_queue_set_queue_race);
	display_run(d);

	display_destroy(d);
}