2 * Copyright © 2012 Intel Corporation
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.
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
27 #include "wayland-server.h"
28 #include "test-runner.h"
31 fd_dispatch(int fd, uint32_t mask, void *data)
41 TEST(event_loop_post_dispatch_check)
43 struct wl_event_loop *loop = wl_event_loop_create();
44 struct wl_event_source *source;
47 source = wl_event_loop_add_fd(loop, 1, WL_EVENT_READABLE,
48 fd_dispatch, &dispatch_ran);
49 wl_event_source_check(source);
51 wl_event_loop_dispatch(loop, 0);
54 wl_event_source_remove(source);
55 wl_event_loop_destroy(loop);
58 struct free_source_context {
59 struct wl_event_source *source1, *source2;
65 free_source_callback(int fd, uint32_t mask, void *data)
67 struct free_source_context *context = data;
71 /* Remove other source */
72 if (fd == context->p1[0]) {
73 wl_event_source_remove(context->source2);
74 context->source2 = NULL;
75 } else if (fd == context->p2[0]) {
76 wl_event_source_remove(context->source1);
77 context->source1 = NULL;
85 TEST(event_loop_free_source_with_data)
87 struct wl_event_loop *loop = wl_event_loop_create();
88 struct free_source_context context;
91 /* This test is a little tricky to get right, since we don't
92 * have any guarantee from the event loop (ie epoll) on the
93 * order of which it reports events. We want to have one
94 * source free the other, but we don't know which one is going
95 * to run first. So we add two fd sources with a callback
96 * that frees the other source and check that only one of them
97 * run (and that we don't crash, of course).
101 assert(pipe(context.p1) == 0);
102 assert(pipe(context.p2) == 0);
104 wl_event_loop_add_fd(loop, context.p1[0], WL_EVENT_READABLE,
105 free_source_callback, &context);
106 assert(context.source1);
108 wl_event_loop_add_fd(loop, context.p2[0], WL_EVENT_READABLE,
109 free_source_callback, &context);
110 assert(context.source2);
113 assert(write(context.p1[1], &data, sizeof data) == sizeof data);
114 assert(write(context.p2[1], &data, sizeof data) == sizeof data);
116 wl_event_loop_dispatch(loop, 0);
118 assert(context.count == 1);
121 wl_event_source_remove(context.source1);
123 wl_event_source_remove(context.source2);
124 wl_event_loop_destroy(loop);
126 assert(close(context.p1[0]) == 0);
127 assert(close(context.p1[1]) == 0);
128 assert(close(context.p2[0]) == 0);
129 assert(close(context.p2[1]) == 0);
133 signal_callback(int signal_number, void *data)
137 assert(signal_number == SIGUSR1);
143 TEST(event_loop_signal)
145 struct wl_event_loop *loop = wl_event_loop_create();
146 struct wl_event_source *source;
149 source = wl_event_loop_add_signal(loop, SIGUSR1,
150 signal_callback, &got_it);
151 wl_event_loop_dispatch(loop, 0);
153 kill(getpid(), SIGUSR1);
154 wl_event_loop_dispatch(loop, 0);
157 wl_event_source_remove(source);
158 wl_event_loop_destroy(loop);
163 timer_callback(void *data)
172 TEST(event_loop_timer)
174 struct wl_event_loop *loop = wl_event_loop_create();
175 struct wl_event_source *source;
178 source = wl_event_loop_add_timer(loop, timer_callback, &got_it);
179 wl_event_source_timer_update(source, 10);
180 wl_event_loop_dispatch(loop, 0);
182 wl_event_loop_dispatch(loop, 20);
185 wl_event_source_remove(source);
186 wl_event_loop_destroy(loop);