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
28#include "wayland-server.h"
29#include "test-runner.h"
30
31static void
32signal_notify(struct wl_listener *listener, void *data)
33{
34 /* only increase counter*/
35 ++(*((int *) data));
36}
37
38TEST(signal_init)
39{
40 struct wl_signal signal;
41
42 wl_signal_init(signal: &signal);
43
44 /* Test if listeners' list is initialized */
45 assert(&signal.listener_list == signal.listener_list.next
46 && "Maybe wl_signal implementation changed?");
47 assert(signal.listener_list.next == signal.listener_list.prev
48 && "Maybe wl_signal implementation changed?");
49}
50
51TEST(signal_add_get)
52{
53 struct wl_signal signal;
54
55 /* we just need different values of notify */
56 struct wl_listener l1 = {.notify = (wl_notify_func_t) 0x1};
57 struct wl_listener l2 = {.notify = (wl_notify_func_t) 0x2};
58 struct wl_listener l3 = {.notify = (wl_notify_func_t) 0x3};
59 /* one real, why not */
60 struct wl_listener l4 = {.notify = signal_notify};
61
62 wl_signal_init(signal: &signal);
63
64 wl_signal_add(signal: &signal, listener: &l1);
65 wl_signal_add(signal: &signal, listener: &l2);
66 wl_signal_add(signal: &signal, listener: &l3);
67 wl_signal_add(signal: &signal, listener: &l4);
68
69 assert(wl_signal_get(&signal, signal_notify) == &l4);
70 assert(wl_signal_get(&signal, (wl_notify_func_t) 0x3) == &l3);
71 assert(wl_signal_get(&signal, (wl_notify_func_t) 0x2) == &l2);
72 assert(wl_signal_get(&signal, (wl_notify_func_t) 0x1) == &l1);
73
74 /* get should not be destructive */
75 assert(wl_signal_get(&signal, signal_notify) == &l4);
76 assert(wl_signal_get(&signal, (wl_notify_func_t) 0x3) == &l3);
77 assert(wl_signal_get(&signal, (wl_notify_func_t) 0x2) == &l2);
78 assert(wl_signal_get(&signal, (wl_notify_func_t) 0x1) == &l1);
79}
80
81TEST(signal_emit_to_one_listener)
82{
83 int count = 0;
84 int counter;
85
86 struct wl_signal signal;
87 struct wl_listener l1 = {.notify = signal_notify};
88
89 wl_signal_init(signal: &signal);
90 wl_signal_add(signal: &signal, listener: &l1);
91
92 for (counter = 0; counter < 100; counter++)
93 wl_signal_emit(signal: &signal, data: &count);
94
95 assert(counter == count);
96}
97
98TEST(signal_emit_to_more_listeners)
99{
100 int count = 0;
101 int counter;
102
103 struct wl_signal signal;
104 struct wl_listener l1 = {.notify = signal_notify};
105 struct wl_listener l2 = {.notify = signal_notify};
106 struct wl_listener l3 = {.notify = signal_notify};
107
108 wl_signal_init(signal: &signal);
109 wl_signal_add(signal: &signal, listener: &l1);
110 wl_signal_add(signal: &signal, listener: &l2);
111 wl_signal_add(signal: &signal, listener: &l3);
112
113 for (counter = 0; counter < 100; counter++)
114 wl_signal_emit(signal: &signal, data: &count);
115
116 assert(3 * counter == count);
117}
118

source code of gtk/subprojects/wayland/tests/signal-test.c