1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * override.c: Closure override test program
3 * Copyright (C) 2001, James Henstridge
4 * Copyright (C) 2003, Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #undef G_LOG_DOMAIN
21 #define G_LOG_DOMAIN "TestOverride"
22
23 #undef G_DISABLE_ASSERT
24 #undef G_DISABLE_CHECKS
25 #undef G_DISABLE_CAST_CHECKS
26
27 #undef VERBOSE
28
29 #include <string.h>
30
31 #include <glib.h>
32 #include <glib-object.h>
33
34 #include "testcommon.h"
35
36 static guint foo_signal_id = 0;
37 static guint bar_signal_id = 0;
38 static guint baz_signal_id = 0;
39
40 static GType test_i_get_type (void);
41 static GType test_a_get_type (void);
42 static GType test_b_get_type (void);
43 static GType test_c_get_type (void);
44
45 static void record (const gchar *str);
46
47 #define TEST_TYPE_I (test_i_get_type ())
48
49 typedef struct _TestI TestI;
50 typedef struct _TestIClass TestIClass;
51
52 struct _TestIClass
53 {
54 GTypeInterface base_iface;
55 };
56
57 static void
test_i_foo(TestI * self)58 test_i_foo (TestI *self)
59 {
60 record ("TestI::foo");
61 }
62
63 static void
test_i_default_init(gpointer g_class)64 test_i_default_init (gpointer g_class)
65 {
66 foo_signal_id = g_signal_newv ("foo",
67 TEST_TYPE_I,
68 G_SIGNAL_RUN_LAST,
69 g_cclosure_new(G_CALLBACK(test_i_foo),
70 NULL, NULL),
71 NULL, NULL,
72 g_cclosure_marshal_VOID__VOID,
73 G_TYPE_NONE, 0, NULL);
74 }
75
76 static DEFINE_IFACE (TestI, test_i, NULL, test_i_default_init)
77
78 #define TEST_TYPE_A (test_a_get_type())
79
80 typedef struct _TestA TestA;
81 typedef struct _TestAClass TestAClass;
82
83 struct _TestA {
84 GObject parent;
85 };
86 struct _TestAClass {
87 GObjectClass parent_class;
88
89 void (* bar) (TestA *self);
90 };
91
92 static void
test_a_foo(TestI * self)93 test_a_foo (TestI *self)
94 {
95 GValue args[1] = { G_VALUE_INIT };
96
97 record ("TestA::foo");
98
99 g_value_init (&args[0], TEST_TYPE_A);
100 g_value_set_object (&args[0], self);
101
102 g_assert (g_signal_get_invocation_hint (self)->signal_id == foo_signal_id);
103 g_signal_chain_from_overridden (args, NULL);
104
105 g_value_unset (&args[0]);
106 }
107
108 static void
test_a_bar(TestA * self)109 test_a_bar (TestA *self)
110 {
111 record ("TestA::bar");
112 }
113
114 static gchar *
test_a_baz(TestA * self,GObject * object,gpointer pointer)115 test_a_baz (TestA *self,
116 GObject *object,
117 gpointer pointer)
118 {
119 record ("TestA::baz");
120
121 g_assert (object == G_OBJECT (self));
122 g_assert (GPOINTER_TO_INT (pointer) == 23);
123
124 return g_strdup ("TestA::baz");
125 }
126
127 static void
test_a_class_init(TestAClass * class)128 test_a_class_init (TestAClass *class)
129 {
130 class->bar = test_a_bar;
131
132 bar_signal_id = g_signal_new ("bar",
133 TEST_TYPE_A,
134 G_SIGNAL_RUN_LAST,
135 G_STRUCT_OFFSET (TestAClass, bar),
136 NULL, NULL,
137 g_cclosure_marshal_VOID__VOID,
138 G_TYPE_NONE, 0, NULL);
139
140 baz_signal_id = g_signal_new_class_handler ("baz",
141 TEST_TYPE_A,
142 G_SIGNAL_RUN_LAST,
143 G_CALLBACK (test_a_baz),
144 NULL, NULL,
145 g_cclosure_marshal_STRING__OBJECT_POINTER,
146 G_TYPE_STRING, 2,
147 G_TYPE_OBJECT,
148 G_TYPE_POINTER);
149 }
150
151 static void
test_a_interface_init(TestIClass * iface)152 test_a_interface_init (TestIClass *iface)
153 {
154 g_signal_override_class_closure (foo_signal_id,
155 TEST_TYPE_A,
156 g_cclosure_new (G_CALLBACK (test_a_foo),
157 NULL, NULL));
158 }
159
160 static DEFINE_TYPE_FULL (TestA, test_a,
161 test_a_class_init, NULL, NULL,
162 G_TYPE_OBJECT,
163 INTERFACE (test_a_interface_init, TEST_TYPE_I))
164
165 #define TEST_TYPE_B (test_b_get_type())
166
167 typedef struct _TestB TestB;
168 typedef struct _TestBClass TestBClass;
169
170 struct _TestB {
171 TestA parent;
172 };
173 struct _TestBClass {
174 TestAClass parent_class;
175 };
176
177 static void
test_b_foo(TestI * self)178 test_b_foo (TestI *self)
179 {
180 GValue args[1] = { G_VALUE_INIT };
181
182 record ("TestB::foo");
183
184 g_value_init (&args[0], TEST_TYPE_A);
185 g_value_set_object (&args[0], self);
186
187 g_assert (g_signal_get_invocation_hint (self)->signal_id == foo_signal_id);
188 g_signal_chain_from_overridden (args, NULL);
189
190 g_value_unset (&args[0]);
191 }
192
193 static void
test_b_bar(TestA * self)194 test_b_bar (TestA *self)
195 {
196 GValue args[1] = { G_VALUE_INIT };
197
198 record ("TestB::bar");
199
200 g_value_init (&args[0], TEST_TYPE_A);
201 g_value_set_object (&args[0], self);
202
203 g_assert (g_signal_get_invocation_hint (self)->signal_id == bar_signal_id);
204 g_signal_chain_from_overridden (args, NULL);
205
206 g_value_unset (&args[0]);
207 }
208
209 static gchar *
test_b_baz(TestA * self,GObject * object,gpointer pointer)210 test_b_baz (TestA *self,
211 GObject *object,
212 gpointer pointer)
213 {
214 gchar *retval = NULL;
215
216 record ("TestB::baz");
217
218 g_assert (object == G_OBJECT (self));
219 g_assert (GPOINTER_TO_INT (pointer) == 23);
220
221 g_signal_chain_from_overridden_handler (self, object, pointer, &retval);
222
223 if (retval)
224 {
225 gchar *tmp = g_strconcat (retval , ",TestB::baz", NULL);
226 g_free (retval);
227 retval = tmp;
228 }
229
230 return retval;
231 }
232
233 static void
test_b_class_init(TestBClass * class)234 test_b_class_init (TestBClass *class)
235 {
236 g_signal_override_class_closure (foo_signal_id,
237 TEST_TYPE_B,
238 g_cclosure_new (G_CALLBACK (test_b_foo),
239 NULL, NULL));
240 g_signal_override_class_closure (bar_signal_id,
241 TEST_TYPE_B,
242 g_cclosure_new (G_CALLBACK (test_b_bar),
243 NULL, NULL));
244 g_signal_override_class_handler ("baz",
245 TEST_TYPE_B,
246 G_CALLBACK (test_b_baz));
247 }
248
249 static DEFINE_TYPE (TestB, test_b,
250 test_b_class_init, NULL, NULL,
251 TEST_TYPE_A)
252
253 #define TEST_TYPE_C (test_c_get_type())
254
255 typedef struct _TestC TestC;
256 typedef struct _TestCClass TestCClass;
257
258 struct _TestC {
259 TestB parent;
260 };
261 struct _TestCClass {
262 TestBClass parent_class;
263 };
264
265 static void
test_c_foo(TestI * self)266 test_c_foo (TestI *self)
267 {
268 GValue args[1] = { G_VALUE_INIT };
269
270 record ("TestC::foo");
271
272 g_value_init (&args[0], TEST_TYPE_A);
273 g_value_set_object (&args[0], self);
274
275 g_assert (g_signal_get_invocation_hint (self)->signal_id == foo_signal_id);
276 g_signal_chain_from_overridden (args, NULL);
277
278 g_value_unset (&args[0]);
279 }
280
281 static void
test_c_bar(TestA * self)282 test_c_bar (TestA *self)
283 {
284 GValue args[1] = { G_VALUE_INIT };
285
286 record ("TestC::bar");
287
288 g_value_init (&args[0], TEST_TYPE_A);
289 g_value_set_object (&args[0], self);
290
291 g_assert (g_signal_get_invocation_hint (self)->signal_id == bar_signal_id);
292 g_signal_chain_from_overridden (args, NULL);
293
294 g_value_unset (&args[0]);
295 }
296
297 static gchar *
test_c_baz(TestA * self,GObject * object,gpointer pointer)298 test_c_baz (TestA *self,
299 GObject *object,
300 gpointer pointer)
301 {
302 gchar *retval = NULL;
303
304 record ("TestC::baz");
305
306 g_assert (object == G_OBJECT (self));
307 g_assert (GPOINTER_TO_INT (pointer) == 23);
308
309 g_signal_chain_from_overridden_handler (self, object, pointer, &retval);
310
311 if (retval)
312 {
313 gchar *tmp = g_strconcat (retval , ",TestC::baz", NULL);
314 g_free (retval);
315 retval = tmp;
316 }
317
318 return retval;
319 }
320
321 static void
test_c_class_init(TestBClass * class)322 test_c_class_init (TestBClass *class)
323 {
324 g_signal_override_class_closure (foo_signal_id,
325 TEST_TYPE_C,
326 g_cclosure_new (G_CALLBACK (test_c_foo),
327 NULL, NULL));
328 g_signal_override_class_closure (bar_signal_id,
329 TEST_TYPE_C,
330 g_cclosure_new (G_CALLBACK (test_c_bar),
331 NULL, NULL));
332 g_signal_override_class_handler ("baz",
333 TEST_TYPE_C,
334 G_CALLBACK (test_c_baz));
335 }
336
337
338 static DEFINE_TYPE (TestC, test_c,
339 test_c_class_init, NULL, NULL,
340 TEST_TYPE_B)
341
342 static GString *test_string = NULL;
343 gboolean failed = FALSE;
344
345 static void
record(const gchar * str)346 record (const gchar *str)
347 {
348 if (test_string->len)
349 g_string_append_c (test_string, ',');
350 g_string_append (test_string, str);
351 }
352
353 static void
test(GType type,const gchar * signal,const gchar * expected,const gchar * expected_retval)354 test (GType type,
355 const gchar *signal,
356 const gchar *expected,
357 const gchar *expected_retval)
358 {
359 GObject *self = g_object_new (type, NULL);
360
361 test_string = g_string_new (NULL);
362
363 if (strcmp (signal, "baz"))
364 {
365 g_signal_emit_by_name (self, signal);
366 }
367 else
368 {
369 gchar *ret;
370
371 g_signal_emit_by_name (self, signal, self, GINT_TO_POINTER (23), &ret);
372
373 if (strcmp (ret, expected_retval) != 0)
374 failed = TRUE;
375
376 g_free (ret);
377 }
378
379 #ifndef VERBOSE
380 if (strcmp (test_string->str, expected) != 0)
381 #endif
382 {
383 g_printerr ("*** emitting %s on a %s instance\n"
384 " Expecting: %s\n"
385 " Got: %s\n",
386 signal, g_type_name (type),
387 expected,
388 test_string->str);
389
390 if (strcmp (test_string->str, expected) != 0)
391 failed = TRUE;
392 }
393
394 g_string_free (test_string, TRUE);
395 g_object_unref (self);
396 }
397
398 int
main(int argc,char ** argv)399 main (int argc, char **argv)
400 {
401 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
402 G_LOG_LEVEL_WARNING |
403 G_LOG_LEVEL_CRITICAL);
404
405 test (TEST_TYPE_A, "foo", "TestA::foo,TestI::foo", NULL);
406 test (TEST_TYPE_A, "bar", "TestA::bar", NULL);
407 test (TEST_TYPE_A, "baz", "TestA::baz", "TestA::baz");
408
409 test (TEST_TYPE_B, "foo", "TestB::foo,TestA::foo,TestI::foo", NULL);
410 test (TEST_TYPE_B, "bar", "TestB::bar,TestA::bar", NULL);
411 test (TEST_TYPE_B, "baz", "TestB::baz,TestA::baz", "TestA::baz,TestB::baz");
412
413 test (TEST_TYPE_C, "foo", "TestC::foo,TestB::foo,TestA::foo,TestI::foo", NULL);
414 test (TEST_TYPE_C, "bar", "TestC::bar,TestB::bar,TestA::bar", NULL);
415 test (TEST_TYPE_C, "baz", "TestC::baz,TestB::baz,TestA::baz", "TestA::baz,TestB::baz,TestC::baz");
416
417 return failed ? 1 : 0;
418 }
419