1*84e872a0SLloyd Pique /*
2*84e872a0SLloyd Pique * Copyright © 2013 Marek Chalupa
3*84e872a0SLloyd Pique *
4*84e872a0SLloyd Pique * Permission is hereby granted, free of charge, to any person obtaining
5*84e872a0SLloyd Pique * a copy of this software and associated documentation files (the
6*84e872a0SLloyd Pique * "Software"), to deal in the Software without restriction, including
7*84e872a0SLloyd Pique * without limitation the rights to use, copy, modify, merge, publish,
8*84e872a0SLloyd Pique * distribute, sublicense, and/or sell copies of the Software, and to
9*84e872a0SLloyd Pique * permit persons to whom the Software is furnished to do so, subject to
10*84e872a0SLloyd Pique * the following conditions:
11*84e872a0SLloyd Pique *
12*84e872a0SLloyd Pique * The above copyright notice and this permission notice (including the
13*84e872a0SLloyd Pique * next paragraph) shall be included in all copies or substantial
14*84e872a0SLloyd Pique * portions of the Software.
15*84e872a0SLloyd Pique *
16*84e872a0SLloyd Pique * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17*84e872a0SLloyd Pique * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*84e872a0SLloyd Pique * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19*84e872a0SLloyd Pique * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20*84e872a0SLloyd Pique * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21*84e872a0SLloyd Pique * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22*84e872a0SLloyd Pique * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23*84e872a0SLloyd Pique * SOFTWARE.
24*84e872a0SLloyd Pique */
25*84e872a0SLloyd Pique
26*84e872a0SLloyd Pique #include <assert.h>
27*84e872a0SLloyd Pique
28*84e872a0SLloyd Pique #include "wayland-server.h"
29*84e872a0SLloyd Pique #include "test-runner.h"
30*84e872a0SLloyd Pique
31*84e872a0SLloyd Pique static void
signal_notify(struct wl_listener * listener,void * data)32*84e872a0SLloyd Pique signal_notify(struct wl_listener *listener, void *data)
33*84e872a0SLloyd Pique {
34*84e872a0SLloyd Pique /* only increase counter*/
35*84e872a0SLloyd Pique ++(*((int *) data));
36*84e872a0SLloyd Pique }
37*84e872a0SLloyd Pique
TEST(signal_init)38*84e872a0SLloyd Pique TEST(signal_init)
39*84e872a0SLloyd Pique {
40*84e872a0SLloyd Pique struct wl_signal signal;
41*84e872a0SLloyd Pique
42*84e872a0SLloyd Pique wl_signal_init(&signal);
43*84e872a0SLloyd Pique
44*84e872a0SLloyd Pique /* Test if listeners' list is initialized */
45*84e872a0SLloyd Pique assert(&signal.listener_list == signal.listener_list.next
46*84e872a0SLloyd Pique && "Maybe wl_signal implementation changed?");
47*84e872a0SLloyd Pique assert(signal.listener_list.next == signal.listener_list.prev
48*84e872a0SLloyd Pique && "Maybe wl_signal implementation changed?");
49*84e872a0SLloyd Pique }
50*84e872a0SLloyd Pique
TEST(signal_add_get)51*84e872a0SLloyd Pique TEST(signal_add_get)
52*84e872a0SLloyd Pique {
53*84e872a0SLloyd Pique struct wl_signal signal;
54*84e872a0SLloyd Pique
55*84e872a0SLloyd Pique /* we just need different values of notify */
56*84e872a0SLloyd Pique struct wl_listener l1 = {.notify = (wl_notify_func_t) 0x1};
57*84e872a0SLloyd Pique struct wl_listener l2 = {.notify = (wl_notify_func_t) 0x2};
58*84e872a0SLloyd Pique struct wl_listener l3 = {.notify = (wl_notify_func_t) 0x3};
59*84e872a0SLloyd Pique /* one real, why not */
60*84e872a0SLloyd Pique struct wl_listener l4 = {.notify = signal_notify};
61*84e872a0SLloyd Pique
62*84e872a0SLloyd Pique wl_signal_init(&signal);
63*84e872a0SLloyd Pique
64*84e872a0SLloyd Pique wl_signal_add(&signal, &l1);
65*84e872a0SLloyd Pique wl_signal_add(&signal, &l2);
66*84e872a0SLloyd Pique wl_signal_add(&signal, &l3);
67*84e872a0SLloyd Pique wl_signal_add(&signal, &l4);
68*84e872a0SLloyd Pique
69*84e872a0SLloyd Pique assert(wl_signal_get(&signal, signal_notify) == &l4);
70*84e872a0SLloyd Pique assert(wl_signal_get(&signal, (wl_notify_func_t) 0x3) == &l3);
71*84e872a0SLloyd Pique assert(wl_signal_get(&signal, (wl_notify_func_t) 0x2) == &l2);
72*84e872a0SLloyd Pique assert(wl_signal_get(&signal, (wl_notify_func_t) 0x1) == &l1);
73*84e872a0SLloyd Pique
74*84e872a0SLloyd Pique /* get should not be destructive */
75*84e872a0SLloyd Pique assert(wl_signal_get(&signal, signal_notify) == &l4);
76*84e872a0SLloyd Pique assert(wl_signal_get(&signal, (wl_notify_func_t) 0x3) == &l3);
77*84e872a0SLloyd Pique assert(wl_signal_get(&signal, (wl_notify_func_t) 0x2) == &l2);
78*84e872a0SLloyd Pique assert(wl_signal_get(&signal, (wl_notify_func_t) 0x1) == &l1);
79*84e872a0SLloyd Pique }
80*84e872a0SLloyd Pique
TEST(signal_emit_to_one_listener)81*84e872a0SLloyd Pique TEST(signal_emit_to_one_listener)
82*84e872a0SLloyd Pique {
83*84e872a0SLloyd Pique int count = 0;
84*84e872a0SLloyd Pique int counter;
85*84e872a0SLloyd Pique
86*84e872a0SLloyd Pique struct wl_signal signal;
87*84e872a0SLloyd Pique struct wl_listener l1 = {.notify = signal_notify};
88*84e872a0SLloyd Pique
89*84e872a0SLloyd Pique wl_signal_init(&signal);
90*84e872a0SLloyd Pique wl_signal_add(&signal, &l1);
91*84e872a0SLloyd Pique
92*84e872a0SLloyd Pique for (counter = 0; counter < 100; counter++)
93*84e872a0SLloyd Pique wl_signal_emit(&signal, &count);
94*84e872a0SLloyd Pique
95*84e872a0SLloyd Pique assert(counter == count);
96*84e872a0SLloyd Pique }
97*84e872a0SLloyd Pique
TEST(signal_emit_to_more_listeners)98*84e872a0SLloyd Pique TEST(signal_emit_to_more_listeners)
99*84e872a0SLloyd Pique {
100*84e872a0SLloyd Pique int count = 0;
101*84e872a0SLloyd Pique int counter;
102*84e872a0SLloyd Pique
103*84e872a0SLloyd Pique struct wl_signal signal;
104*84e872a0SLloyd Pique struct wl_listener l1 = {.notify = signal_notify};
105*84e872a0SLloyd Pique struct wl_listener l2 = {.notify = signal_notify};
106*84e872a0SLloyd Pique struct wl_listener l3 = {.notify = signal_notify};
107*84e872a0SLloyd Pique
108*84e872a0SLloyd Pique wl_signal_init(&signal);
109*84e872a0SLloyd Pique wl_signal_add(&signal, &l1);
110*84e872a0SLloyd Pique wl_signal_add(&signal, &l2);
111*84e872a0SLloyd Pique wl_signal_add(&signal, &l3);
112*84e872a0SLloyd Pique
113*84e872a0SLloyd Pique for (counter = 0; counter < 100; counter++)
114*84e872a0SLloyd Pique wl_signal_emit(&signal, &count);
115*84e872a0SLloyd Pique
116*84e872a0SLloyd Pique assert(3 * counter == count);
117*84e872a0SLloyd Pique }
118*84e872a0SLloyd Pique
119*84e872a0SLloyd Pique struct signal_emit_mutable_data {
120*84e872a0SLloyd Pique int count;
121*84e872a0SLloyd Pique struct wl_listener *remove_listener;
122*84e872a0SLloyd Pique };
123*84e872a0SLloyd Pique
124*84e872a0SLloyd Pique static void
signal_notify_mutable(struct wl_listener * listener,void * data)125*84e872a0SLloyd Pique signal_notify_mutable(struct wl_listener *listener, void *data)
126*84e872a0SLloyd Pique {
127*84e872a0SLloyd Pique struct signal_emit_mutable_data *test_data = data;
128*84e872a0SLloyd Pique test_data->count++;
129*84e872a0SLloyd Pique }
130*84e872a0SLloyd Pique
131*84e872a0SLloyd Pique static void
signal_notify_and_remove_mutable(struct wl_listener * listener,void * data)132*84e872a0SLloyd Pique signal_notify_and_remove_mutable(struct wl_listener *listener, void *data)
133*84e872a0SLloyd Pique {
134*84e872a0SLloyd Pique struct signal_emit_mutable_data *test_data = data;
135*84e872a0SLloyd Pique signal_notify_mutable(listener, test_data);
136*84e872a0SLloyd Pique wl_list_remove(&test_data->remove_listener->link);
137*84e872a0SLloyd Pique }
138*84e872a0SLloyd Pique
TEST(signal_emit_mutable)139*84e872a0SLloyd Pique TEST(signal_emit_mutable)
140*84e872a0SLloyd Pique {
141*84e872a0SLloyd Pique struct signal_emit_mutable_data data = {0};
142*84e872a0SLloyd Pique
143*84e872a0SLloyd Pique /* l2 will remove l3 before l3 is notified */
144*84e872a0SLloyd Pique struct wl_signal signal;
145*84e872a0SLloyd Pique struct wl_listener l1 = {.notify = signal_notify_mutable};
146*84e872a0SLloyd Pique struct wl_listener l2 = {.notify = signal_notify_and_remove_mutable};
147*84e872a0SLloyd Pique struct wl_listener l3 = {.notify = signal_notify_mutable};
148*84e872a0SLloyd Pique
149*84e872a0SLloyd Pique wl_signal_init(&signal);
150*84e872a0SLloyd Pique wl_signal_add(&signal, &l1);
151*84e872a0SLloyd Pique wl_signal_add(&signal, &l2);
152*84e872a0SLloyd Pique wl_signal_add(&signal, &l3);
153*84e872a0SLloyd Pique
154*84e872a0SLloyd Pique data.remove_listener = &l3;
155*84e872a0SLloyd Pique wl_signal_emit_mutable(&signal, &data);
156*84e872a0SLloyd Pique
157*84e872a0SLloyd Pique assert(data.count == 2);
158*84e872a0SLloyd Pique }
159