toytoolkit: Don't draw shadows for maximized windows.
[profile/ivi/weston.git] / tests / test-runner.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 #define _GNU_SOURCE
24
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <sys/socket.h>
34
35 #include "test-runner.h"
36
37 static void
38 test_client_cleanup(struct weston_process *proc, int status)
39 {
40         struct test_client *client =
41                 container_of(proc, struct test_client, proc);
42
43         fprintf(stderr, "server: test client exited, status %d\n", status);
44
45         client->status = status;
46         client->done = 1;
47
48         assert(client->status == 0);
49
50         if (client->terminate)
51                 wl_display_terminate(client->compositor->wl_display);
52 }
53
54 static int
55 test_client_data(int fd, uint32_t mask, void *data)
56 {
57         struct test_client *client = data;
58         struct wl_event_loop *loop;
59         int len;
60
61         len = read(client->fd, client->buf, sizeof client->buf);
62         assert(len >= 0);
63         fprintf(stderr, "server: got %.*s from client\n", len - 1, client->buf);
64         assert(client->buf[len - 1] == '\n');
65         client->buf[len - 1] = '\0';
66
67         loop = wl_display_get_event_loop(client->compositor->wl_display);
68         wl_event_loop_add_idle(loop, (void *) client->handle, client);
69
70         return 1;
71 }
72
73 struct test_client *
74 test_client_launch(struct weston_compositor *compositor, const char *file_name)
75 {
76         struct test_client *client;
77         struct wl_event_loop *loop;
78         int ret, sv[2], client_fd;
79         char buf[256];
80
81         client = malloc(sizeof *client);
82         assert(client);
83         ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv);
84         assert(ret == 0);
85
86         client_fd = dup(sv[0]);
87         assert(client_fd >= 0);
88         snprintf(buf, sizeof buf, "%d", client_fd);
89         setenv("TEST_SOCKET", buf, 1);
90         snprintf(buf, sizeof buf, "%s/%s", getenv("abs_builddir"), file_name);
91         fprintf(stderr, "server: launching %s\n", buf);
92
93         client->terminate = 0;
94         client->compositor = compositor;
95         client->client = weston_client_launch(compositor,
96                                               &client->proc, buf,
97                                               test_client_cleanup);
98         assert(client->client);
99         close(sv[0]);
100         client->fd = sv[1];
101
102         loop = wl_display_get_event_loop(compositor->wl_display);
103         wl_event_loop_add_fd(loop, client->fd, WL_EVENT_READABLE,
104                              test_client_data, client);
105
106         return client;
107 }
108
109 void
110 test_client_send(struct test_client *client, const char *fmt, ...)
111 {
112         char buf[256];
113         va_list ap;
114         int len;
115
116         va_start(ap, fmt);
117         len = vsnprintf(buf, sizeof buf, fmt, ap);
118         va_end(ap);
119
120         fprintf(stderr, "server: sending %s", buf);
121
122         assert(write(client->fd, buf, len) == len);
123 }
124
125 extern const struct test __start_test_section, __stop_test_section;
126
127 static void
128 run_test(void *data)
129 {
130         struct weston_compositor *compositor = data;
131         const struct test *t;
132
133         for (t = &__start_test_section; t < &__stop_test_section; t++)
134                 t->run(compositor);
135 }
136
137 int
138 module_init(struct weston_compositor *compositor);
139
140 WL_EXPORT int
141 module_init(struct weston_compositor *compositor)
142 {
143         struct wl_event_loop *loop;
144
145         loop = wl_display_get_event_loop(compositor->wl_display);
146
147         wl_event_loop_add_idle(loop, run_test, compositor);
148
149         return 0;
150 }