1*84e872a0SLloyd Pique /*
2*84e872a0SLloyd Pique * Copyright © 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
3*84e872a0SLloyd Pique *
4*84e872a0SLloyd Pique * Permission is hereby granted, free of charge, to any person obtaining
5*84e872a0SLloyd Pique * a copy of this software and associated documentation files (the
6*84e872a0SLloyd Pique * "Software"), to deal in the Software without restriction, including
7*84e872a0SLloyd Pique * without limitation the rights to use, copy, modify, merge, publish,
8*84e872a0SLloyd Pique * distribute, sublicense, and/or sell copies of the Software, and to
9*84e872a0SLloyd Pique * permit persons to whom the Software is furnished to do so, subject to
10*84e872a0SLloyd Pique * the following conditions:
11*84e872a0SLloyd Pique *
12*84e872a0SLloyd Pique * The above copyright notice and this permission notice (including the
13*84e872a0SLloyd Pique * next paragraph) shall be included in all copies or substantial
14*84e872a0SLloyd Pique * portions of the Software.
15*84e872a0SLloyd Pique *
16*84e872a0SLloyd Pique * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17*84e872a0SLloyd Pique * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*84e872a0SLloyd Pique * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19*84e872a0SLloyd Pique * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20*84e872a0SLloyd Pique * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21*84e872a0SLloyd Pique * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22*84e872a0SLloyd Pique * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23*84e872a0SLloyd Pique * SOFTWARE.
24*84e872a0SLloyd Pique */
25*84e872a0SLloyd Pique
26*84e872a0SLloyd Pique #include <stdlib.h>
27*84e872a0SLloyd Pique #include <assert.h>
28*84e872a0SLloyd Pique #include <errno.h>
29*84e872a0SLloyd Pique #include <string.h>
30*84e872a0SLloyd Pique #include <stdio.h>
31*84e872a0SLloyd Pique #include <sys/un.h>
32*84e872a0SLloyd Pique #include <unistd.h>
33*84e872a0SLloyd Pique
34*84e872a0SLloyd Pique #include "wayland-client.h"
35*84e872a0SLloyd Pique #include "wayland-server.h"
36*84e872a0SLloyd Pique #include "test-runner.h"
37*84e872a0SLloyd Pique
38*84e872a0SLloyd Pique /* Ensure the connection doesn't fail due to lack of XDG_RUNTIME_DIR. */
39*84e872a0SLloyd Pique static const char *
require_xdg_runtime_dir(void)40*84e872a0SLloyd Pique require_xdg_runtime_dir(void)
41*84e872a0SLloyd Pique {
42*84e872a0SLloyd Pique char *val = getenv("XDG_RUNTIME_DIR");
43*84e872a0SLloyd Pique assert(val && val[0] == '/' && "set $XDG_RUNTIME_DIR to run this test");
44*84e872a0SLloyd Pique
45*84e872a0SLloyd Pique return val;
46*84e872a0SLloyd Pique }
47*84e872a0SLloyd Pique
48*84e872a0SLloyd Pique struct compositor {
49*84e872a0SLloyd Pique struct wl_display *display;
50*84e872a0SLloyd Pique struct wl_listener listener;
51*84e872a0SLloyd Pique struct wl_client *client;
52*84e872a0SLloyd Pique };
53*84e872a0SLloyd Pique
54*84e872a0SLloyd Pique static void
client_created(struct wl_listener * listener,void * data)55*84e872a0SLloyd Pique client_created(struct wl_listener *listener, void *data)
56*84e872a0SLloyd Pique {
57*84e872a0SLloyd Pique struct compositor *c = wl_container_of(listener, c, listener);
58*84e872a0SLloyd Pique c->client = data;
59*84e872a0SLloyd Pique }
60*84e872a0SLloyd Pique
61*84e872a0SLloyd Pique static void
check_client_list(struct compositor * compositor)62*84e872a0SLloyd Pique check_client_list(struct compositor *compositor)
63*84e872a0SLloyd Pique {
64*84e872a0SLloyd Pique struct wl_list *client_list;
65*84e872a0SLloyd Pique struct wl_client *client, *client_it;
66*84e872a0SLloyd Pique int num_clients = 0;
67*84e872a0SLloyd Pique
68*84e872a0SLloyd Pique client_list = wl_display_get_client_list(compositor->display);
69*84e872a0SLloyd Pique wl_client_for_each(client_it, client_list) {
70*84e872a0SLloyd Pique num_clients++;
71*84e872a0SLloyd Pique client = client_it;
72*84e872a0SLloyd Pique }
73*84e872a0SLloyd Pique assert(num_clients == 1);
74*84e872a0SLloyd Pique /* 'client_it' is not valid here, so we took a copy of the client in the loop.
75*84e872a0SLloyd Pique * We could also do this assert in the loop directly, but in case it fails it is
76*84e872a0SLloyd Pique * easier to understand the problem when we know that the previous assert passed,
77*84e872a0SLloyd Pique * so that there is only one client but the wrong one. */
78*84e872a0SLloyd Pique assert(compositor->client == client);
79*84e872a0SLloyd Pique }
80*84e872a0SLloyd Pique
81*84e872a0SLloyd Pique static const char *
setup_compositor(struct compositor * compositor)82*84e872a0SLloyd Pique setup_compositor(struct compositor *compositor)
83*84e872a0SLloyd Pique {
84*84e872a0SLloyd Pique const char *socket;
85*84e872a0SLloyd Pique
86*84e872a0SLloyd Pique require_xdg_runtime_dir();
87*84e872a0SLloyd Pique
88*84e872a0SLloyd Pique compositor->display = wl_display_create();
89*84e872a0SLloyd Pique socket = wl_display_add_socket_auto(compositor->display);
90*84e872a0SLloyd Pique
91*84e872a0SLloyd Pique compositor->listener.notify = client_created;
92*84e872a0SLloyd Pique wl_display_add_client_created_listener(compositor->display, &compositor->listener);
93*84e872a0SLloyd Pique
94*84e872a0SLloyd Pique return socket;
95*84e872a0SLloyd Pique }
96*84e872a0SLloyd Pique
97*84e872a0SLloyd Pique static void
cleanup_compositor(struct compositor * compositor)98*84e872a0SLloyd Pique cleanup_compositor(struct compositor *compositor)
99*84e872a0SLloyd Pique {
100*84e872a0SLloyd Pique wl_client_destroy(compositor->client);
101*84e872a0SLloyd Pique wl_display_destroy(compositor->display);
102*84e872a0SLloyd Pique }
103*84e872a0SLloyd Pique
TEST(new_client_connect)104*84e872a0SLloyd Pique TEST(new_client_connect)
105*84e872a0SLloyd Pique {
106*84e872a0SLloyd Pique const char *socket;
107*84e872a0SLloyd Pique struct compositor compositor = { 0 };
108*84e872a0SLloyd Pique struct {
109*84e872a0SLloyd Pique struct wl_display *display;
110*84e872a0SLloyd Pique } client;
111*84e872a0SLloyd Pique
112*84e872a0SLloyd Pique socket = setup_compositor(&compositor);
113*84e872a0SLloyd Pique
114*84e872a0SLloyd Pique client.display = wl_display_connect(socket);
115*84e872a0SLloyd Pique
116*84e872a0SLloyd Pique wl_event_loop_dispatch(wl_display_get_event_loop(compositor.display), 100);
117*84e872a0SLloyd Pique
118*84e872a0SLloyd Pique assert(compositor.client != NULL);
119*84e872a0SLloyd Pique
120*84e872a0SLloyd Pique check_client_list(&compositor);
121*84e872a0SLloyd Pique
122*84e872a0SLloyd Pique
123*84e872a0SLloyd Pique
124*84e872a0SLloyd Pique wl_display_disconnect(client.display);
125*84e872a0SLloyd Pique cleanup_compositor(&compositor);
126*84e872a0SLloyd Pique }
127*84e872a0SLloyd Pique
128*84e872a0SLloyd Pique struct resource_listener {
129*84e872a0SLloyd Pique struct wl_listener listener;
130*84e872a0SLloyd Pique int count;
131*84e872a0SLloyd Pique };
132*84e872a0SLloyd Pique
133*84e872a0SLloyd Pique static void
resource_created(struct wl_listener * listener,void * data)134*84e872a0SLloyd Pique resource_created(struct wl_listener *listener, void *data)
135*84e872a0SLloyd Pique {
136*84e872a0SLloyd Pique struct resource_listener *l;
137*84e872a0SLloyd Pique l = wl_container_of(listener, l, listener);
138*84e872a0SLloyd Pique l->count++;
139*84e872a0SLloyd Pique }
140*84e872a0SLloyd Pique
TEST(new_resource)141*84e872a0SLloyd Pique TEST(new_resource)
142*84e872a0SLloyd Pique {
143*84e872a0SLloyd Pique const char *socket;
144*84e872a0SLloyd Pique struct compositor compositor = { 0 };
145*84e872a0SLloyd Pique struct {
146*84e872a0SLloyd Pique struct wl_display *display;
147*84e872a0SLloyd Pique struct wl_callback *cb;
148*84e872a0SLloyd Pique } client;
149*84e872a0SLloyd Pique struct resource_listener resource_listener;
150*84e872a0SLloyd Pique
151*84e872a0SLloyd Pique socket = setup_compositor(&compositor);
152*84e872a0SLloyd Pique client.display = wl_display_connect(socket);
153*84e872a0SLloyd Pique wl_event_loop_dispatch(wl_display_get_event_loop(compositor.display), 100);
154*84e872a0SLloyd Pique
155*84e872a0SLloyd Pique resource_listener.count = 0;
156*84e872a0SLloyd Pique resource_listener.listener.notify = resource_created;
157*84e872a0SLloyd Pique wl_client_add_resource_created_listener(compositor.client,
158*84e872a0SLloyd Pique &resource_listener.listener);
159*84e872a0SLloyd Pique
160*84e872a0SLloyd Pique client.cb = wl_display_sync(client.display);
161*84e872a0SLloyd Pique wl_display_flush(client.display);
162*84e872a0SLloyd Pique wl_event_loop_dispatch(wl_display_get_event_loop(compositor.display), 100);
163*84e872a0SLloyd Pique
164*84e872a0SLloyd Pique assert(resource_listener.count == 1);
165*84e872a0SLloyd Pique
166*84e872a0SLloyd Pique wl_callback_destroy(client.cb);
167*84e872a0SLloyd Pique wl_display_disconnect(client.display);
168*84e872a0SLloyd Pique cleanup_compositor(&compositor);
169*84e872a0SLloyd Pique
170*84e872a0SLloyd Pique /* This is defined to be safe also after client destruction */
171*84e872a0SLloyd Pique wl_list_remove(&resource_listener.listener.link);
172*84e872a0SLloyd Pique }
173