1 /*
2 * Copyright © 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
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 <stdlib.h>
27 #include <assert.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <sys/un.h>
32 #include <unistd.h>
33
34 #include "wayland-client.h"
35 #include "wayland-server.h"
36 #include "test-runner.h"
37
38 /* Ensure the connection doesn't fail due to lack of XDG_RUNTIME_DIR. */
39 static const char *
require_xdg_runtime_dir(void)40 require_xdg_runtime_dir(void)
41 {
42 char *val = getenv("XDG_RUNTIME_DIR");
43 assert(val && "set $XDG_RUNTIME_DIR to run this test");
44
45 return val;
46 }
47
48 struct compositor {
49 struct wl_display *display;
50 struct wl_event_loop *loop;
51 int message;
52 struct wl_client *client;
53 };
54
55 struct message {
56 enum wl_protocol_logger_type type;
57 const char *class;
58 int opcode;
59 const char *message_name;
60 int args_count;
61 } messages[] = {
62 {
63 .type = WL_PROTOCOL_LOGGER_REQUEST,
64 .class = "wl_display",
65 .opcode = 0,
66 .message_name = "sync",
67 .args_count = 1,
68 },
69 {
70 .type = WL_PROTOCOL_LOGGER_EVENT,
71 .class = "wl_callback",
72 .opcode = 0,
73 .message_name = "done",
74 .args_count = 1,
75 },
76 {
77 .type = WL_PROTOCOL_LOGGER_EVENT,
78 .class = "wl_display",
79 .opcode = 1,
80 .message_name = "delete_id",
81 .args_count = 1,
82 },
83 };
84
85 static void
logger_func(void * user_data,enum wl_protocol_logger_type type,const struct wl_protocol_logger_message * message)86 logger_func(void *user_data, enum wl_protocol_logger_type type,
87 const struct wl_protocol_logger_message *message)
88 {
89 struct compositor *c = user_data;
90 struct message *msg = &messages[c->message++];
91
92 assert(msg->type == type);
93 assert(strcmp(msg->class, wl_resource_get_class(message->resource)) == 0);
94 assert(msg->opcode == message->message_opcode);
95 assert(strcmp(msg->message_name, message->message->name) == 0);
96 assert(msg->args_count == message->arguments_count);
97
98 c->client = wl_resource_get_client(message->resource);
99 }
100
101 static void
callback_done(void * data,struct wl_callback * cb,uint32_t time)102 callback_done(void *data, struct wl_callback *cb, uint32_t time)
103 {
104 wl_callback_destroy(cb);
105 }
106
107 static const struct wl_callback_listener callback_listener = {
108 callback_done,
109 };
110
TEST(logger)111 TEST(logger)
112 {
113 test_set_timeout(1);
114
115 const char *socket;
116 struct compositor compositor = { 0 };
117 struct {
118 struct wl_display *display;
119 struct wl_callback *cb;
120 } client;
121 struct wl_protocol_logger *logger;
122
123 require_xdg_runtime_dir();
124
125 compositor.display = wl_display_create();
126 compositor.loop = wl_display_get_event_loop(compositor.display);
127 socket = wl_display_add_socket_auto(compositor.display);
128
129 logger = wl_display_add_protocol_logger(compositor.display,
130 logger_func, &compositor);
131
132 client.display = wl_display_connect(socket);
133 client.cb = wl_display_sync(client.display);
134 wl_callback_add_listener(client.cb, &callback_listener, NULL);
135 wl_display_flush(client.display);
136
137 while (compositor.message < 3) {
138 wl_event_loop_dispatch(compositor.loop, -1);
139 wl_display_flush_clients(compositor.display);
140 }
141
142 wl_display_dispatch(client.display);
143 wl_display_disconnect(client.display);
144
145 wl_client_destroy(compositor.client);
146 wl_protocol_logger_destroy(logger);
147 wl_display_destroy(compositor.display);
148 }
149