server: Add client destroy signal
[profile/ivi/wayland.git] / tests / client-test.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <sys/socket.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33
34 #include "../src/wayland-server.h"
35 #include "test-runner.h"
36
37 struct client_destroy_listener {
38         struct wl_listener listener;
39         int done;
40 };
41
42 static void
43 client_destroy_notify(struct wl_listener *l, void *data)
44 {
45         struct client_destroy_listener *listener =
46                 container_of(l, struct client_destroy_listener, listener);
47
48         listener->done = 1;
49 }
50
51 TEST(client_destroy_listener)
52 {
53         struct wl_display *display;
54         struct wl_client *client;
55         struct client_destroy_listener a, b;
56         int s[2];
57
58         assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
59         display = wl_display_create();
60         assert(display);
61         client = wl_client_create(display, s[0]);
62         assert(client);
63
64         a.listener.notify = client_destroy_notify;
65         a.done = 0;
66         wl_client_add_destroy_listener(client, &a.listener);
67
68         assert(wl_client_get_destroy_listener(client, client_destroy_notify) ==
69                &a.listener);
70
71         b.listener.notify = client_destroy_notify;
72         b.done = 0;
73         wl_client_add_destroy_listener(client, &b.listener);
74
75         wl_list_remove(&a.listener.link);
76
77         wl_client_destroy(client);
78
79         assert(!a.done);
80         assert(b.done);
81
82         close(s[0]);
83         close(s[1]);
84
85         wl_display_destroy(display);
86 }
87