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