xref: /aosp_15_r20/external/wayland/tests/client-test.c (revision 84e872a0dc482bffdb63672969dd03a827d67c73)
1*84e872a0SLloyd Pique /*
2*84e872a0SLloyd Pique  * Copyright © 2012 Intel Corporation
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 <stdio.h>
27*84e872a0SLloyd Pique #include <stdlib.h>
28*84e872a0SLloyd Pique #include <stdarg.h>
29*84e872a0SLloyd Pique #include <string.h>
30*84e872a0SLloyd Pique #include <assert.h>
31*84e872a0SLloyd Pique #include <sys/socket.h>
32*84e872a0SLloyd Pique #include <unistd.h>
33*84e872a0SLloyd Pique #include <errno.h>
34*84e872a0SLloyd Pique #include <sys/types.h>
35*84e872a0SLloyd Pique #include <sys/stat.h>
36*84e872a0SLloyd Pique 
37*84e872a0SLloyd Pique #include "wayland-private.h"
38*84e872a0SLloyd Pique #include "wayland-server.h"
39*84e872a0SLloyd Pique #include "test-runner.h"
40*84e872a0SLloyd Pique 
41*84e872a0SLloyd Pique struct client_destroy_listener {
42*84e872a0SLloyd Pique 	struct wl_listener listener;
43*84e872a0SLloyd Pique 	bool done;
44*84e872a0SLloyd Pique 	struct wl_listener late_listener;
45*84e872a0SLloyd Pique 	bool late_done;
46*84e872a0SLloyd Pique 	struct wl_listener resource_listener;
47*84e872a0SLloyd Pique 	bool resource_done;
48*84e872a0SLloyd Pique };
49*84e872a0SLloyd Pique 
50*84e872a0SLloyd Pique static void
client_destroy_notify(struct wl_listener * l,void * data)51*84e872a0SLloyd Pique client_destroy_notify(struct wl_listener *l, void *data)
52*84e872a0SLloyd Pique {
53*84e872a0SLloyd Pique 	struct client_destroy_listener *listener =
54*84e872a0SLloyd Pique 		wl_container_of(l, listener, listener);
55*84e872a0SLloyd Pique 
56*84e872a0SLloyd Pique 	listener->done = true;
57*84e872a0SLloyd Pique 	assert(!listener->resource_done);
58*84e872a0SLloyd Pique 	assert(!listener->late_done);
59*84e872a0SLloyd Pique }
60*84e872a0SLloyd Pique 
61*84e872a0SLloyd Pique static void
client_resource_destroy_notify(struct wl_listener * l,void * data)62*84e872a0SLloyd Pique client_resource_destroy_notify(struct wl_listener *l, void *data)
63*84e872a0SLloyd Pique {
64*84e872a0SLloyd Pique 	struct client_destroy_listener *listener =
65*84e872a0SLloyd Pique 		wl_container_of(l, listener, resource_listener);
66*84e872a0SLloyd Pique 
67*84e872a0SLloyd Pique 	assert(listener->done);
68*84e872a0SLloyd Pique 	listener->resource_done = true;
69*84e872a0SLloyd Pique 	assert(!listener->late_done);
70*84e872a0SLloyd Pique }
71*84e872a0SLloyd Pique 
72*84e872a0SLloyd Pique static void
client_late_destroy_notify(struct wl_listener * l,void * data)73*84e872a0SLloyd Pique client_late_destroy_notify(struct wl_listener *l, void *data)
74*84e872a0SLloyd Pique {
75*84e872a0SLloyd Pique 	struct client_destroy_listener *listener =
76*84e872a0SLloyd Pique 		wl_container_of(l, listener, late_listener);
77*84e872a0SLloyd Pique 
78*84e872a0SLloyd Pique 	assert(listener->done);
79*84e872a0SLloyd Pique 	assert(listener->resource_done);
80*84e872a0SLloyd Pique 	listener->late_done = true;
81*84e872a0SLloyd Pique }
82*84e872a0SLloyd Pique 
TEST(client_destroy_listener)83*84e872a0SLloyd Pique TEST(client_destroy_listener)
84*84e872a0SLloyd Pique {
85*84e872a0SLloyd Pique 	struct wl_display *display;
86*84e872a0SLloyd Pique 	struct wl_client *client;
87*84e872a0SLloyd Pique 	struct wl_resource *resource;
88*84e872a0SLloyd Pique 	struct client_destroy_listener a, b;
89*84e872a0SLloyd Pique 	int s[2];
90*84e872a0SLloyd Pique 
91*84e872a0SLloyd Pique 	assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
92*84e872a0SLloyd Pique 	display = wl_display_create();
93*84e872a0SLloyd Pique 	assert(display);
94*84e872a0SLloyd Pique 	client = wl_client_create(display, s[0]);
95*84e872a0SLloyd Pique 	assert(client);
96*84e872a0SLloyd Pique 
97*84e872a0SLloyd Pique 	resource = wl_resource_create(client, &wl_callback_interface, 1, 0);
98*84e872a0SLloyd Pique 	assert(resource);
99*84e872a0SLloyd Pique 
100*84e872a0SLloyd Pique 	a.listener.notify = client_destroy_notify;
101*84e872a0SLloyd Pique 	a.done = false;
102*84e872a0SLloyd Pique 	a.resource_listener.notify = client_resource_destroy_notify;
103*84e872a0SLloyd Pique 	a.resource_done = false;
104*84e872a0SLloyd Pique 	a.late_listener.notify = client_late_destroy_notify;
105*84e872a0SLloyd Pique 	a.late_done = false;
106*84e872a0SLloyd Pique 	wl_client_add_destroy_listener(client, &a.listener);
107*84e872a0SLloyd Pique 	wl_resource_add_destroy_listener(resource, &a.resource_listener);
108*84e872a0SLloyd Pique 	wl_client_add_destroy_late_listener(client, &a.late_listener);
109*84e872a0SLloyd Pique 
110*84e872a0SLloyd Pique 	assert(wl_client_get_destroy_listener(client, client_destroy_notify) ==
111*84e872a0SLloyd Pique 	       &a.listener);
112*84e872a0SLloyd Pique 	assert(wl_resource_get_destroy_listener(resource, client_resource_destroy_notify) ==
113*84e872a0SLloyd Pique 	       &a.resource_listener);
114*84e872a0SLloyd Pique 	assert(wl_client_get_destroy_late_listener(client, client_late_destroy_notify) ==
115*84e872a0SLloyd Pique 	       &a.late_listener);
116*84e872a0SLloyd Pique 
117*84e872a0SLloyd Pique 	b.listener.notify = client_destroy_notify;
118*84e872a0SLloyd Pique 	b.done = false;
119*84e872a0SLloyd Pique 	b.resource_listener.notify = client_resource_destroy_notify;
120*84e872a0SLloyd Pique 	b.resource_done = false;
121*84e872a0SLloyd Pique 	b.late_listener.notify = client_late_destroy_notify;
122*84e872a0SLloyd Pique 	b.late_done = false;
123*84e872a0SLloyd Pique 	wl_client_add_destroy_listener(client, &b.listener);
124*84e872a0SLloyd Pique 	wl_resource_add_destroy_listener(resource, &b.resource_listener);
125*84e872a0SLloyd Pique 	wl_client_add_destroy_late_listener(client, &b.late_listener);
126*84e872a0SLloyd Pique 
127*84e872a0SLloyd Pique 	wl_list_remove(&a.listener.link);
128*84e872a0SLloyd Pique 	wl_list_remove(&a.resource_listener.link);
129*84e872a0SLloyd Pique 	wl_list_remove(&a.late_listener.link);
130*84e872a0SLloyd Pique 
131*84e872a0SLloyd Pique 	wl_client_destroy(client);
132*84e872a0SLloyd Pique 
133*84e872a0SLloyd Pique 	assert(!a.done);
134*84e872a0SLloyd Pique 	assert(!a.resource_done);
135*84e872a0SLloyd Pique 	assert(!a.late_done);
136*84e872a0SLloyd Pique 	assert(b.done);
137*84e872a0SLloyd Pique 	assert(b.resource_done);
138*84e872a0SLloyd Pique 	assert(b.late_done);
139*84e872a0SLloyd Pique 
140*84e872a0SLloyd Pique 	close(s[0]);
141*84e872a0SLloyd Pique 	close(s[1]);
142*84e872a0SLloyd Pique 
143*84e872a0SLloyd Pique 	wl_display_destroy(display);
144*84e872a0SLloyd Pique }
145*84e872a0SLloyd Pique 
146