Add some more cases to the app-id unit tests
[glib.git] / tests / gobject / override.c
blob96fc80860b80158325cbcfbf6b7083c45bcb82d8
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.
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 of the License, or (at your option) any later version.
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.
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/>.
20 #undef G_LOG_DOMAIN
21 #define G_LOG_DOMAIN "TestOverride"
23 #undef G_DISABLE_ASSERT
24 #undef G_DISABLE_CHECKS
25 #undef G_DISABLE_CAST_CHECKS
27 #undef VERBOSE
29 #include <string.h>
31 #include <glib.h>
32 #include <glib-object.h>
34 #include "testcommon.h"
36 static guint foo_signal_id = 0;
37 static guint bar_signal_id = 0;
38 static guint baz_signal_id = 0;
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);
45 static void record (const gchar *str);
47 #define TEST_TYPE_I (test_i_get_type ())
49 typedef struct _TestI TestI;
50 typedef struct _TestIClass TestIClass;
52 struct _TestIClass
54 GTypeInterface base_iface;
57 static void
58 test_i_foo (TestI *self)
60 record ("TestI::foo");
63 static void
64 test_i_default_init (gpointer g_class)
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);
76 static DEFINE_IFACE (TestI, test_i, NULL, test_i_default_init)
78 #define TEST_TYPE_A (test_a_get_type())
80 typedef struct _TestA TestA;
81 typedef struct _TestAClass TestAClass;
83 struct _TestA {
84 GObject parent;
86 struct _TestAClass {
87 GObjectClass parent_class;
89 void (* bar) (TestA *self);
92 static void
93 test_a_foo (TestI *self)
95 GValue args[1] = { G_VALUE_INIT };
97 record ("TestA::foo");
99 g_value_init (&args[0], TEST_TYPE_A);
100 g_value_set_object (&args[0], self);
102 g_assert (g_signal_get_invocation_hint (self)->signal_id == foo_signal_id);
103 g_signal_chain_from_overridden (args, NULL);
105 g_value_unset (&args[0]);
108 static void
109 test_a_bar (TestA *self)
111 record ("TestA::bar");
114 static gchar *
115 test_a_baz (TestA *self,
116 GObject *object,
117 gpointer pointer)
119 record ("TestA::baz");
121 g_assert (object == G_OBJECT (self));
122 g_assert (GPOINTER_TO_INT (pointer) == 23);
124 return g_strdup ("TestA::baz");
127 static void
128 test_a_class_init (TestAClass *class)
130 class->bar = test_a_bar;
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);
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);
151 static void
152 test_a_interface_init (TestIClass *iface)
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));
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))
165 #define TEST_TYPE_B (test_b_get_type())
167 typedef struct _TestB TestB;
168 typedef struct _TestBClass TestBClass;
170 struct _TestB {
171 TestA parent;
173 struct _TestBClass {
174 TestAClass parent_class;
177 static void
178 test_b_foo (TestI *self)
180 GValue args[1] = { G_VALUE_INIT };
182 record ("TestB::foo");
184 g_value_init (&args[0], TEST_TYPE_A);
185 g_value_set_object (&args[0], self);
187 g_assert (g_signal_get_invocation_hint (self)->signal_id == foo_signal_id);
188 g_signal_chain_from_overridden (args, NULL);
190 g_value_unset (&args[0]);
193 static void
194 test_b_bar (TestA *self)
196 GValue args[1] = { G_VALUE_INIT };
198 record ("TestB::bar");
200 g_value_init (&args[0], TEST_TYPE_A);
201 g_value_set_object (&args[0], self);
203 g_assert (g_signal_get_invocation_hint (self)->signal_id == bar_signal_id);
204 g_signal_chain_from_overridden (args, NULL);
206 g_value_unset (&args[0]);
209 static gchar *
210 test_b_baz (TestA *self,
211 GObject *object,
212 gpointer pointer)
214 gchar *retval = NULL;
216 record ("TestB::baz");
218 g_assert (object == G_OBJECT (self));
219 g_assert (GPOINTER_TO_INT (pointer) == 23);
221 g_signal_chain_from_overridden_handler (self, object, pointer, &retval);
223 if (retval)
225 gchar *tmp = g_strconcat (retval , ",TestB::baz", NULL);
226 g_free (retval);
227 retval = tmp;
230 return retval;
233 static void
234 test_b_class_init (TestBClass *class)
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));
249 static DEFINE_TYPE (TestB, test_b,
250 test_b_class_init, NULL, NULL,
251 TEST_TYPE_A)
253 #define TEST_TYPE_C (test_c_get_type())
255 typedef struct _TestC TestC;
256 typedef struct _TestCClass TestCClass;
258 struct _TestC {
259 TestB parent;
261 struct _TestCClass {
262 TestBClass parent_class;
265 static void
266 test_c_foo (TestI *self)
268 GValue args[1] = { G_VALUE_INIT };
270 record ("TestC::foo");
272 g_value_init (&args[0], TEST_TYPE_A);
273 g_value_set_object (&args[0], self);
275 g_assert (g_signal_get_invocation_hint (self)->signal_id == foo_signal_id);
276 g_signal_chain_from_overridden (args, NULL);
278 g_value_unset (&args[0]);
281 static void
282 test_c_bar (TestA *self)
284 GValue args[1] = { G_VALUE_INIT };
286 record ("TestC::bar");
288 g_value_init (&args[0], TEST_TYPE_A);
289 g_value_set_object (&args[0], self);
291 g_assert (g_signal_get_invocation_hint (self)->signal_id == bar_signal_id);
292 g_signal_chain_from_overridden (args, NULL);
294 g_value_unset (&args[0]);
297 static gchar *
298 test_c_baz (TestA *self,
299 GObject *object,
300 gpointer pointer)
302 gchar *retval = NULL;
304 record ("TestC::baz");
306 g_assert (object == G_OBJECT (self));
307 g_assert (GPOINTER_TO_INT (pointer) == 23);
309 g_signal_chain_from_overridden_handler (self, object, pointer, &retval);
311 if (retval)
313 gchar *tmp = g_strconcat (retval , ",TestC::baz", NULL);
314 g_free (retval);
315 retval = tmp;
318 return retval;
321 static void
322 test_c_class_init (TestBClass *class)
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));
338 static DEFINE_TYPE (TestC, test_c,
339 test_c_class_init, NULL, NULL,
340 TEST_TYPE_B)
342 static GString *test_string = NULL;
343 gboolean failed = FALSE;
345 static void
346 record (const gchar *str)
348 if (test_string->len)
349 g_string_append_c (test_string, ',');
350 g_string_append (test_string, str);
353 static void
354 test (GType type,
355 const gchar *signal,
356 const gchar *expected,
357 const gchar *expected_retval)
359 GObject *self = g_object_new (type, NULL);
361 test_string = g_string_new (NULL);
363 if (strcmp (signal, "baz"))
365 g_signal_emit_by_name (self, signal);
367 else
369 gchar *ret;
371 g_signal_emit_by_name (self, signal, self, GINT_TO_POINTER (23), &ret);
373 if (strcmp (ret, expected_retval) != 0)
374 failed = TRUE;
376 g_free (ret);
379 #ifndef VERBOSE
380 if (strcmp (test_string->str, expected) != 0)
381 #endif
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);
390 if (strcmp (test_string->str, expected) != 0)
391 failed = TRUE;
394 g_string_free (test_string, TRUE);
395 g_object_unref (self);
399 main (int argc, char **argv)
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);
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");
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");
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");
417 return failed ? 1 : 0;