xref: /aosp_15_r20/external/wayland/tests/proxy-test.c (revision 84e872a0dc482bffdb63672969dd03a827d67c73)
1*84e872a0SLloyd Pique /*
2*84e872a0SLloyd Pique  * Copyright (c) 2019 Red Hat, Inc.
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 <assert.h>
27*84e872a0SLloyd Pique #include <string.h>
28*84e872a0SLloyd Pique 
29*84e872a0SLloyd Pique #include "wayland-server.h"
30*84e872a0SLloyd Pique #include "wayland-client.h"
31*84e872a0SLloyd Pique #include "test-runner.h"
32*84e872a0SLloyd Pique 
33*84e872a0SLloyd Pique static struct {
34*84e872a0SLloyd Pique 	struct wl_display *display;
35*84e872a0SLloyd Pique 	struct wl_event_loop *loop;
36*84e872a0SLloyd Pique 	int sync_count;
37*84e872a0SLloyd Pique } server;
38*84e872a0SLloyd Pique 
39*84e872a0SLloyd Pique static struct {
40*84e872a0SLloyd Pique 	struct wl_display *display;
41*84e872a0SLloyd Pique 	struct wl_callback *callback_a;
42*84e872a0SLloyd Pique 	struct wl_callback *callback_b;
43*84e872a0SLloyd Pique 	int callback_count;
44*84e872a0SLloyd Pique } client;
45*84e872a0SLloyd Pique 
46*84e872a0SLloyd Pique static const char *tag_a = "tag";
47*84e872a0SLloyd Pique static const char *tag_b = "tag";
48*84e872a0SLloyd Pique 
49*84e872a0SLloyd Pique static void
callback_done(void * data,struct wl_callback * cb,uint32_t time)50*84e872a0SLloyd Pique callback_done(void *data, struct wl_callback *cb, uint32_t time)
51*84e872a0SLloyd Pique {
52*84e872a0SLloyd Pique 	const char * const *expected_tag;
53*84e872a0SLloyd Pique 	const char * const *tag;
54*84e872a0SLloyd Pique 
55*84e872a0SLloyd Pique 	if (cb == client.callback_a)
56*84e872a0SLloyd Pique 		expected_tag = &tag_a;
57*84e872a0SLloyd Pique 	else if (cb == client.callback_b)
58*84e872a0SLloyd Pique 		expected_tag = &tag_b;
59*84e872a0SLloyd Pique 	else
60*84e872a0SLloyd Pique 		assert(!"unexpected callback");
61*84e872a0SLloyd Pique 
62*84e872a0SLloyd Pique 	tag = wl_proxy_get_tag((struct wl_proxy *) cb);
63*84e872a0SLloyd Pique 
64*84e872a0SLloyd Pique 	assert(tag == expected_tag);
65*84e872a0SLloyd Pique 	assert(strcmp(*tag, "tag") == 0);
66*84e872a0SLloyd Pique 
67*84e872a0SLloyd Pique 	wl_callback_destroy(cb);
68*84e872a0SLloyd Pique 
69*84e872a0SLloyd Pique 	client.callback_count++;
70*84e872a0SLloyd Pique }
71*84e872a0SLloyd Pique 
72*84e872a0SLloyd Pique static const struct wl_callback_listener callback_listener = {
73*84e872a0SLloyd Pique 	callback_done,
74*84e872a0SLloyd Pique };
75*84e872a0SLloyd Pique 
76*84e872a0SLloyd Pique static void
logger_func(void * user_data,enum wl_protocol_logger_type type,const struct wl_protocol_logger_message * message)77*84e872a0SLloyd Pique logger_func(void *user_data,
78*84e872a0SLloyd Pique 	    enum wl_protocol_logger_type type,
79*84e872a0SLloyd Pique 	    const struct wl_protocol_logger_message *message)
80*84e872a0SLloyd Pique {
81*84e872a0SLloyd Pique 	if (type != WL_PROTOCOL_LOGGER_REQUEST)
82*84e872a0SLloyd Pique 		return;
83*84e872a0SLloyd Pique 
84*84e872a0SLloyd Pique 	assert(strcmp(wl_resource_get_class(message->resource),
85*84e872a0SLloyd Pique 		      "wl_display") == 0);
86*84e872a0SLloyd Pique 	assert(strcmp(message->message->name, "sync") == 0);
87*84e872a0SLloyd Pique 
88*84e872a0SLloyd Pique 	server.sync_count++;
89*84e872a0SLloyd Pique }
90*84e872a0SLloyd Pique 
TEST(proxy_tag)91*84e872a0SLloyd Pique TEST(proxy_tag)
92*84e872a0SLloyd Pique {
93*84e872a0SLloyd Pique 	const char *socket;
94*84e872a0SLloyd Pique 	struct wl_protocol_logger *logger;
95*84e872a0SLloyd Pique 
96*84e872a0SLloyd Pique 	assert(&tag_a != &tag_b);
97*84e872a0SLloyd Pique 
98*84e872a0SLloyd Pique 	server.display = wl_display_create();
99*84e872a0SLloyd Pique 	assert(server.display);
100*84e872a0SLloyd Pique 	server.loop = wl_display_get_event_loop(server.display);
101*84e872a0SLloyd Pique 	assert(server.loop);
102*84e872a0SLloyd Pique 	socket = wl_display_add_socket_auto(server.display);
103*84e872a0SLloyd Pique 	assert(socket);
104*84e872a0SLloyd Pique 	logger = wl_display_add_protocol_logger(server.display,
105*84e872a0SLloyd Pique 						logger_func, NULL);
106*84e872a0SLloyd Pique 	assert(logger);
107*84e872a0SLloyd Pique 
108*84e872a0SLloyd Pique 	client.display = wl_display_connect(socket);
109*84e872a0SLloyd Pique 	assert(client.display);
110*84e872a0SLloyd Pique 
111*84e872a0SLloyd Pique 	client.callback_a = wl_display_sync(client.display);
112*84e872a0SLloyd Pique 	wl_callback_add_listener(client.callback_a, &callback_listener, NULL);
113*84e872a0SLloyd Pique 	wl_proxy_set_tag((struct wl_proxy *) client.callback_a,
114*84e872a0SLloyd Pique 			 &tag_a);
115*84e872a0SLloyd Pique 
116*84e872a0SLloyd Pique 	client.callback_b = wl_display_sync(client.display);
117*84e872a0SLloyd Pique 	wl_callback_add_listener(client.callback_b, &callback_listener, NULL);
118*84e872a0SLloyd Pique 	wl_proxy_set_tag((struct wl_proxy *) client.callback_b,
119*84e872a0SLloyd Pique 			 &tag_b);
120*84e872a0SLloyd Pique 
121*84e872a0SLloyd Pique 	wl_display_flush(client.display);
122*84e872a0SLloyd Pique 
123*84e872a0SLloyd Pique 	while (server.sync_count < 2) {
124*84e872a0SLloyd Pique 		wl_event_loop_dispatch(server.loop, -1);
125*84e872a0SLloyd Pique 		wl_display_flush_clients(server.display);
126*84e872a0SLloyd Pique 	}
127*84e872a0SLloyd Pique 
128*84e872a0SLloyd Pique 	wl_display_dispatch(client.display);
129*84e872a0SLloyd Pique 
130*84e872a0SLloyd Pique 	assert(client.callback_count == 2);
131*84e872a0SLloyd Pique 
132*84e872a0SLloyd Pique 	wl_protocol_logger_destroy(logger);
133*84e872a0SLloyd Pique 	wl_display_disconnect(client.display);
134*84e872a0SLloyd Pique 	wl_event_loop_dispatch(server.loop, 100);
135*84e872a0SLloyd Pique 
136*84e872a0SLloyd Pique 	wl_display_destroy(server.display);
137*84e872a0SLloyd Pique }
138