remove libds stuffs
[platform/core/uifw/libds-tizen.git] / tests / test-subsurface.c
1 #include <assert.h>
2 #include <stdbool.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 #include <wayland-server.h>
8 #include <wayland-client.h>
9 #include <libds/compositor.h>
10 #include <libds/surface.h>
11 #include <libds/subsurface.h>
12 #include <libds/log.h>
13
14 struct server_base
15 {
16     struct wl_display *display;
17     struct ds_compositor *compositor;
18     const char *socket;
19 };
20
21 const char *
22 test_server_init(struct server_base *server)
23 {
24     server->display = wl_display_create();
25     assert(server->display);
26     server->compositor = ds_compositor_create(server->display);
27     assert(server->compositor);
28     server->socket = wl_display_add_socket_auto(server->display);
29     assert(server->socket);
30
31     return server->socket;
32 }
33
34 void
35 test_server_finish(struct server_base *server)
36 {
37     wl_display_destroy(server->display);
38 }
39
40 struct client
41 {
42     struct wl_display *display;
43     struct wl_registry *registry;
44     struct wl_compositor *compositor;
45     struct wl_subcompositor *subcompositor;
46     struct wl_surface *surface;
47     struct wl_subsurface *subsurface;
48 };
49
50 static void
51 handle_global(void *data, struct wl_registry *registry, uint32_t id,
52         const char *interface, uint32_t version)
53 {
54     struct client *client = data;
55
56     if (strcmp(interface, "wl_compositor") == 0) {
57         client->compositor =
58             wl_registry_bind(registry, id, &wl_compositor_interface, version);
59     }
60     else if (strcmp(interface, "wl_subcompositor") == 0) {
61         client->subcompositor =
62             wl_registry_bind(registry, id, &wl_subcompositor_interface, version);
63     }
64 }
65
66 static const struct wl_registry_listener registry_listener = {
67     .global = handle_global,
68 };
69
70 void
71 test_client_init(struct client *client, const char *name)
72 {
73     client->display = wl_display_connect(name);
74     assert(client->display);
75     client->registry = wl_display_get_registry(client->display);
76     assert(client->registry);
77
78     wl_registry_add_listener(client->registry, &registry_listener, client);
79
80     wl_display_roundtrip(client->display);
81
82     assert(client->compositor);
83     assert(client->subcompositor);
84 }
85
86 void
87 test_client_finish(struct client *client)
88 {
89     wl_subcompositor_destroy(client->subcompositor);
90     wl_compositor_destroy(client->compositor);
91     wl_registry_destroy(client->registry);
92     wl_display_disconnect(client->display);
93 }
94
95 struct test_server
96 {
97     struct server_base base;
98     bool cb_called;
99
100     struct wl_listener new_surface;
101     struct wl_listener new_subsurface1;
102     struct wl_listener new_subsurface2;
103 };
104
105 static void
106 cb_new_subsurface1(struct wl_listener *listener, void *data)
107 {
108     struct ds_subsurface *subsurface = data;
109     struct test_server *server;
110
111     assert(subsurface);
112
113     server = wl_container_of(listener, server, new_subsurface1);
114     server->cb_called = true;
115     wl_display_terminate(server->base.display);
116 }
117
118 static void
119 cb_new_subsurface2(struct wl_listener *listener, void *data)
120 {
121     struct ds_subsurface *subsurface = data;
122     struct test_server *server;
123
124     assert(subsurface);
125
126     server = wl_container_of(listener, server, new_subsurface2);
127     server->cb_called = true;
128     wl_display_terminate(server->base.display);
129 }
130
131 static void
132 cb_new_surface(struct wl_listener *listener, void *data)
133 {
134     struct ds_surface *surface = data;
135     struct test_server *server;
136
137     server = wl_container_of(listener, server, new_surface);
138     if (!server->new_subsurface1.notify) {
139         server->new_subsurface1.notify = cb_new_subsurface1;
140         ds_surface_add_new_subsurface_listener(surface,
141                 &server->new_subsurface1);
142     }
143     else {
144         server->new_subsurface2.notify = cb_new_subsurface2;
145         ds_surface_add_new_subsurface_listener(surface,
146                 &server->new_subsurface2);
147     }
148 }
149
150 static void
151 run_client(const char *name)
152 {
153     struct client client;
154
155     test_client_init(&client, name);
156
157     struct wl_surface *surface =
158         wl_compositor_create_surface(client.compositor);
159
160     struct wl_surface *child_surface =
161         wl_compositor_create_surface(client.compositor);
162
163     struct wl_subsurface *subsurface =
164         wl_subcompositor_get_subsurface(client.subcompositor,
165                 child_surface, surface);
166
167     wl_display_roundtrip(client.display);
168
169     wl_subsurface_destroy(subsurface);
170     wl_surface_destroy(child_surface);
171     wl_surface_destroy(surface);
172
173     test_client_finish(&client);
174 }
175
176 static void
177 test_subsurface_create(void)
178 {
179     struct test_server server = {
180         .new_subsurface1 = { .notify = NULL },
181         .cb_called = false
182     };
183     pid_t pid;
184
185     const char *socket_name = test_server_init(&server.base);
186
187     pid = fork();
188     assert(pid != -1);
189
190     if (pid == 0) {
191         run_client(socket_name);
192         exit(0);
193     }
194
195     server.new_surface.notify = cb_new_surface;
196     ds_compositor_add_new_surface_listener(server.base.compositor,
197             &server.new_surface);
198
199     wl_display_run(server.base.display);
200
201     assert(server.cb_called);
202
203     test_server_finish(&server.base);
204 }
205
206 int
207 main(void)
208 {
209     test_subsurface_create();
210     return 0;
211 }