2 * Copyright © 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
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
34 #include "wayland-client.h"
35 #include "wayland-server.h"
36 #include "test-runner.h"
38 /* Ensure the connection doesn't fail due to lack of XDG_RUNTIME_DIR. */
40 require_xdg_runtime_dir(void)
42 char *val = getenv("XDG_RUNTIME_DIR");
43 assert(val && val[0] == '/' && "set $XDG_RUNTIME_DIR to run this test");
49 struct wl_display *display;
50 struct wl_listener listener;
51 struct wl_client *client;
55 client_created(struct wl_listener *listener, void *data)
57 struct compositor *c = wl_container_of(listener, c, listener);
62 check_client_list(struct compositor *compositor)
64 struct wl_list *client_list;
65 struct wl_client *client = NULL, *client_it;
68 client_list = wl_display_get_client_list(compositor->display);
69 wl_client_for_each(client_it, client_list) {
73 assert(num_clients == 1);
74 /* 'client_it' is not valid here, so we took a copy of the client in the loop.
75 * We could also do this assert in the loop directly, but in case it fails it is
76 * easier to understand the problem when we know that the previous assert passed,
77 * so that there is only one client but the wrong one. */
78 assert(compositor->client == client);
82 setup_compositor(struct compositor *compositor)
86 require_xdg_runtime_dir();
88 compositor->display = wl_display_create();
89 socket = wl_display_add_socket_auto(compositor->display);
91 compositor->listener.notify = client_created;
92 wl_display_add_client_created_listener(compositor->display, &compositor->listener);
98 cleanup_compositor(struct compositor *compositor)
100 wl_client_destroy(compositor->client);
101 wl_display_destroy(compositor->display);
104 TEST(new_client_connect)
107 struct compositor compositor = { 0 };
109 struct wl_display *display;
112 socket = setup_compositor(&compositor);
114 client.display = wl_display_connect(socket);
116 wl_event_loop_dispatch(wl_display_get_event_loop(compositor.display), 100);
118 assert(compositor.client != NULL);
120 check_client_list(&compositor);
124 wl_display_disconnect(client.display);
125 cleanup_compositor(&compositor);
128 struct resource_listener {
129 struct wl_listener listener;
134 resource_created(struct wl_listener *listener, void *data)
136 struct resource_listener *l;
137 l = wl_container_of(listener, l, listener);
144 struct compositor compositor = { 0 };
146 struct wl_display *display;
147 struct wl_callback *cb;
149 struct resource_listener resource_listener;
151 socket = setup_compositor(&compositor);
152 client.display = wl_display_connect(socket);
153 wl_event_loop_dispatch(wl_display_get_event_loop(compositor.display), 100);
155 resource_listener.count = 0;
156 resource_listener.listener.notify = resource_created;
157 wl_client_add_resource_created_listener(compositor.client,
158 &resource_listener.listener);
160 client.cb = wl_display_sync(client.display);
161 wl_display_flush(client.display);
162 wl_event_loop_dispatch(wl_display_get_event_loop(compositor.display), 100);
164 assert(resource_listener.count == 1);
166 wl_callback_destroy(client.cb);
167 wl_display_disconnect(client.display);
168 cleanup_compositor(&compositor);
170 /* This is defined to be safe also after client destruction */
171 wl_list_remove(&resource_listener.listener.link);