1 /*
2 * Copyright © 2013 Marek Chalupa
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 <sys/socket.h>
28 #include <unistd.h>
29 #include <stdint.h>
30
31 #include "wayland-server.h"
32 #include "test-runner.h"
33
TEST(create_resource_tst)34 TEST(create_resource_tst)
35 {
36 struct wl_display *display;
37 struct wl_client *client;
38 struct wl_resource *res;
39 struct wl_list *link;
40 int s[2];
41 uint32_t id;
42
43 assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
44 display = wl_display_create();
45 assert(display);
46 client = wl_client_create(display, s[0]);
47 assert(client);
48
49 res = wl_resource_create(client, &wl_seat_interface, 4, 0);
50 assert(res);
51
52 /* setters/getters */
53 assert(wl_resource_get_version(res) == 4);
54
55 assert(client == wl_resource_get_client(res));
56 id = wl_resource_get_id(res);
57 assert(wl_client_get_object(client, id) == res);
58
59 link = wl_resource_get_link(res);
60 assert(link);
61 assert(wl_resource_from_link(link) == res);
62
63 wl_resource_set_user_data(res, (void *) 0xbee);
64 assert(wl_resource_get_user_data(res) == (void *) 0xbee);
65
66 wl_resource_destroy(res);
67 wl_client_destroy(client);
68 wl_display_destroy(display);
69 close(s[1]);
70 }
71
72 static void
res_destroy_func(struct wl_resource * res)73 res_destroy_func(struct wl_resource *res)
74 {
75 assert(res);
76
77 _Bool *destr = wl_resource_get_user_data(res);
78 *destr = 1;
79 }
80
81 static _Bool notify_called = 0;
82 static void
destroy_notify(struct wl_listener * l,void * data)83 destroy_notify(struct wl_listener *l, void *data)
84 {
85 assert(l && data);
86 notify_called = 1;
87 }
88
TEST(destroy_res_tst)89 TEST(destroy_res_tst)
90 {
91 struct wl_display *display;
92 struct wl_client *client;
93 struct wl_resource *res;
94 int s[2];
95 unsigned id;
96 struct wl_list *link;
97
98 _Bool destroyed = 0;
99 struct wl_listener destroy_listener = {
100 .notify = &destroy_notify
101 };
102
103 assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
104 display = wl_display_create();
105 assert(display);
106 client = wl_client_create(display, s[0]);
107 assert(client);
108
109 res = wl_resource_create(client, &wl_seat_interface, 4, 0);
110 assert(res);
111 wl_resource_set_implementation(res, NULL, &destroyed, res_destroy_func);
112 wl_resource_add_destroy_listener(res, &destroy_listener);
113
114 id = wl_resource_get_id(res);
115 link = wl_resource_get_link(res);
116 assert(link);
117
118 wl_resource_destroy(res);
119 assert(destroyed);
120 assert(notify_called); /* check if signal was emitted */
121 assert(wl_client_get_object(client, id) == NULL);
122
123 res = wl_resource_create(client, &wl_seat_interface, 2, 0);
124 assert(res);
125 destroyed = 0;
126 notify_called = 0;
127 wl_resource_set_destructor(res, res_destroy_func);
128 wl_resource_set_user_data(res, &destroyed);
129 wl_resource_add_destroy_listener(res, &destroy_listener);
130 /* client should destroy the resource upon its destruction */
131 wl_client_destroy(client);
132 assert(destroyed);
133 assert(notify_called);
134
135 wl_display_destroy(display);
136 close(s[1]);
137 }
138
TEST(create_resource_with_same_id)139 TEST(create_resource_with_same_id)
140 {
141 struct wl_display *display;
142 struct wl_client *client;
143 struct wl_resource *res, *res2;
144 int s[2];
145 uint32_t id;
146
147 assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
148 display = wl_display_create();
149 assert(display);
150 client = wl_client_create(display, s[0]);
151 assert(client);
152
153 res = wl_resource_create(client, &wl_seat_interface, 2, 0);
154 assert(res);
155 id = wl_resource_get_id(res);
156 assert(wl_client_get_object(client, id) == res);
157
158 /* this one should replace the old one */
159 res2 = wl_resource_create(client, &wl_seat_interface, 1, id);
160 assert(res2 != NULL);
161 assert(wl_client_get_object(client, id) == res2);
162
163 wl_resource_destroy(res2);
164 wl_resource_destroy(res);
165
166 wl_client_destroy(client);
167 wl_display_destroy(display);
168 close(s[1]);
169 }
170