Update contrib.
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 * Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
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, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #define G_LOG_DOMAIN "TestOverride"
25 #undef G_DISABLE_ASSERT
26 #undef G_DISABLE_CHECKS
27 #undef G_DISABLE_CAST_CHECKS
34 #include <glib-object.h>
36 #include "testcommon.h"
38 #include "mrt2_glib2_test.h"
41 static guint foo_signal_id = 0;
42 static guint bar_signal_id = 0;
44 static GType test_i_get_type (void);
45 static GType test_a_get_type (void);
46 static GType test_b_get_type (void);
47 static GType test_c_get_type (void);
49 static void record (const gchar *str);
51 #define TEST_TYPE_I (test_i_get_type ())
53 typedef struct _TestI TestI;
54 typedef struct _TestIClass TestIClass;
58 GTypeInterface base_iface;
62 test_i_foo (TestI *self)
64 record ("TestI::foo");
68 test_i_default_init (gpointer g_class)
70 foo_signal_id = g_signal_newv ("foo",
73 g_cclosure_new(G_CALLBACK(test_i_foo),
76 g_cclosure_marshal_VOID__VOID,
77 G_TYPE_NONE, 0, NULL);
80 static DEFINE_IFACE (TestI, test_i, NULL, test_i_default_init)
82 #define TEST_TYPE_A (test_a_get_type())
84 typedef struct _TestA TestA;
85 typedef struct _TestAClass TestAClass;
91 GObjectClass parent_class;
93 void (* bar) (TestA *self);
97 test_a_foo (TestI *self)
99 GValue args[1] = { { 0, } };
101 record ("TestA::foo");
103 g_value_init (&args[0], TEST_TYPE_A);
104 g_value_set_object (&args[0], self);
106 g_assert (g_signal_get_invocation_hint (self)->signal_id == foo_signal_id);
107 g_signal_chain_from_overridden (args, NULL);
109 g_value_unset (&args[0]);
113 test_a_bar (TestA *self)
115 record ("TestA::bar");
119 test_a_class_init (TestAClass *class)
121 class->bar = test_a_bar;
123 bar_signal_id = g_signal_new ("bar",
126 G_STRUCT_OFFSET (TestAClass, bar),
128 g_cclosure_marshal_VOID__VOID,
129 G_TYPE_NONE, 0, NULL);
133 test_a_interface_init (TestIClass *iface)
135 g_signal_override_class_closure (foo_signal_id,
137 g_cclosure_new (G_CALLBACK (test_a_foo),
141 static DEFINE_TYPE_FULL (TestA, test_a,
142 test_a_class_init, NULL, NULL,
144 INTERFACE (test_a_interface_init, TEST_TYPE_I))
146 #define TEST_TYPE_B (test_b_get_type())
148 typedef struct _TestB TestB;
149 typedef struct _TestBClass TestBClass;
155 TestAClass parent_class;
159 test_b_foo (TestA *self)
161 GValue args[1] = { { 0, } };
163 record ("TestB::foo");
165 g_value_init (&args[0], TEST_TYPE_A);
166 g_value_set_object (&args[0], self);
168 g_assert (g_signal_get_invocation_hint (self)->signal_id == foo_signal_id);
169 g_signal_chain_from_overridden (args, NULL);
171 g_value_unset (&args[0]);
175 test_b_bar (TestI *self)
177 GValue args[1] = { { 0, } };
179 record ("TestB::bar");
181 g_value_init (&args[0], TEST_TYPE_A);
182 g_value_set_object (&args[0], self);
184 g_assert (g_signal_get_invocation_hint (self)->signal_id == bar_signal_id);
185 g_signal_chain_from_overridden (args, NULL);
187 g_value_unset (&args[0]);
191 test_b_class_init (TestBClass *class)
193 g_signal_override_class_closure (foo_signal_id,
195 g_cclosure_new (G_CALLBACK (test_b_foo),
197 g_signal_override_class_closure (bar_signal_id,
199 g_cclosure_new (G_CALLBACK (test_b_bar),
203 static DEFINE_TYPE (TestB, test_b,
204 test_b_class_init, NULL, NULL,
207 #define TEST_TYPE_C (test_c_get_type())
209 typedef struct _TestC TestC;
210 typedef struct _TestCClass TestCClass;
216 TestBClass parent_class;
220 test_c_foo (TestA *self)
222 GValue args[1] = { { 0, } };
224 record ("TestC::foo");
226 g_value_init (&args[0], TEST_TYPE_A);
227 g_value_set_object (&args[0], self);
229 g_assert (g_signal_get_invocation_hint (self)->signal_id == foo_signal_id);
230 g_signal_chain_from_overridden (args, NULL);
232 g_value_unset (&args[0]);
236 test_c_bar (TestI *self)
238 GValue args[1] = { { 0, } };
240 record ("TestC::bar");
242 g_value_init (&args[0], TEST_TYPE_A);
243 g_value_set_object (&args[0], self);
245 g_assert (g_signal_get_invocation_hint (self)->signal_id == bar_signal_id);
246 g_signal_chain_from_overridden (args, NULL);
248 g_value_unset (&args[0]);
252 test_c_class_init (TestBClass *class)
254 g_signal_override_class_closure (foo_signal_id,
256 g_cclosure_new (G_CALLBACK (test_c_foo),
258 g_signal_override_class_closure (bar_signal_id,
260 g_cclosure_new (G_CALLBACK (test_c_bar),
265 static DEFINE_TYPE (TestC, test_c,
266 test_c_class_init, NULL, NULL,
269 static GString *test_string = NULL;
270 gboolean failed = FALSE;
273 record (const gchar *str)
275 if (test_string->len)
276 g_string_append_c (test_string, ',');
277 g_string_append (test_string, str);
283 const gchar *expected)
285 GObject *self = g_object_new (type, NULL);
287 test_string = g_string_new (NULL);
289 g_signal_emit_by_name (self, signal, 0);
292 if (strcmp (test_string->str, expected) != 0)
295 g_printerr ("*** emitting %s on a %s instance\n"
298 signal, g_type_name (type),
302 if (strcmp (test_string->str, expected) != 0)
306 g_string_free (test_string, TRUE);
310 main (int argc, char **argv)
313 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
314 g_set_print_handler(mrtPrintHandler);
316 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
317 G_LOG_LEVEL_WARNING |
318 G_LOG_LEVEL_CRITICAL);
321 test (TEST_TYPE_A, "foo", "TestA::foo,TestI::foo");
322 test (TEST_TYPE_A, "bar", "TestA::bar");
324 test (TEST_TYPE_B, "foo", "TestB::foo,TestA::foo,TestI::foo");
325 test (TEST_TYPE_B, "bar", "TestB::bar,TestA::bar");
327 test (TEST_TYPE_C, "foo", "TestC::foo,TestB::foo,TestA::foo,TestI::foo");
328 test (TEST_TYPE_C, "bar", "TestC::bar,TestB::bar,TestA::bar");
332 testResultXml("override");
334 return failed ? 1 : 0;