|
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. |
|
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, write to the |
|
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
|
19 * Boston, MA 02111-1307, USA. |
|
20 */ |
|
21 |
|
22 #undef G_LOG_DOMAIN |
|
23 #define G_LOG_DOMAIN "TestOverride" |
|
24 |
|
25 #undef G_DISABLE_ASSERT |
|
26 #undef G_DISABLE_CHECKS |
|
27 #undef G_DISABLE_CAST_CHECKS |
|
28 |
|
29 #undef VERBOSE |
|
30 |
|
31 #include <string.h> |
|
32 |
|
33 #include <glib.h> |
|
34 #include <glib-object.h> |
|
35 |
|
36 #include "testcommon.h" |
|
37 #ifdef SYMBIAN |
|
38 #include "mrt2_glib2_test.h" |
|
39 #endif /*SYMBIAN*/ |
|
40 |
|
41 static guint foo_signal_id = 0; |
|
42 static guint bar_signal_id = 0; |
|
43 |
|
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); |
|
48 |
|
49 static void record (const gchar *str); |
|
50 |
|
51 #define TEST_TYPE_I (test_i_get_type ()) |
|
52 |
|
53 typedef struct _TestI TestI; |
|
54 typedef struct _TestIClass TestIClass; |
|
55 |
|
56 struct _TestIClass |
|
57 { |
|
58 GTypeInterface base_iface; |
|
59 }; |
|
60 |
|
61 static void |
|
62 test_i_foo (TestI *self) |
|
63 { |
|
64 record ("TestI::foo"); |
|
65 } |
|
66 |
|
67 static void |
|
68 test_i_default_init (gpointer g_class) |
|
69 { |
|
70 foo_signal_id = g_signal_newv ("foo", |
|
71 TEST_TYPE_I, |
|
72 G_SIGNAL_RUN_LAST, |
|
73 g_cclosure_new(G_CALLBACK(test_i_foo), |
|
74 NULL, NULL), |
|
75 NULL, NULL, |
|
76 g_cclosure_marshal_VOID__VOID, |
|
77 G_TYPE_NONE, 0, NULL); |
|
78 } |
|
79 |
|
80 static DEFINE_IFACE (TestI, test_i, NULL, test_i_default_init) |
|
81 |
|
82 #define TEST_TYPE_A (test_a_get_type()) |
|
83 |
|
84 typedef struct _TestA TestA; |
|
85 typedef struct _TestAClass TestAClass; |
|
86 |
|
87 struct _TestA { |
|
88 GObject parent; |
|
89 }; |
|
90 struct _TestAClass { |
|
91 GObjectClass parent_class; |
|
92 |
|
93 void (* bar) (TestA *self); |
|
94 }; |
|
95 |
|
96 static void |
|
97 test_a_foo (TestI *self) |
|
98 { |
|
99 GValue args[1] = { { 0, } }; |
|
100 |
|
101 record ("TestA::foo"); |
|
102 |
|
103 g_value_init (&args[0], TEST_TYPE_A); |
|
104 g_value_set_object (&args[0], self); |
|
105 |
|
106 g_assert (g_signal_get_invocation_hint (self)->signal_id == foo_signal_id); |
|
107 g_signal_chain_from_overridden (args, NULL); |
|
108 |
|
109 g_value_unset (&args[0]); |
|
110 } |
|
111 |
|
112 static void |
|
113 test_a_bar (TestA *self) |
|
114 { |
|
115 record ("TestA::bar"); |
|
116 } |
|
117 |
|
118 static void |
|
119 test_a_class_init (TestAClass *class) |
|
120 { |
|
121 class->bar = test_a_bar; |
|
122 |
|
123 bar_signal_id = g_signal_new ("bar", |
|
124 TEST_TYPE_A, |
|
125 G_SIGNAL_RUN_LAST, |
|
126 G_STRUCT_OFFSET (TestAClass, bar), |
|
127 NULL, NULL, |
|
128 g_cclosure_marshal_VOID__VOID, |
|
129 G_TYPE_NONE, 0, NULL); |
|
130 } |
|
131 |
|
132 static void |
|
133 test_a_interface_init (TestIClass *iface) |
|
134 { |
|
135 g_signal_override_class_closure (foo_signal_id, |
|
136 TEST_TYPE_A, |
|
137 g_cclosure_new (G_CALLBACK (test_a_foo), |
|
138 NULL, NULL)); |
|
139 } |
|
140 |
|
141 static DEFINE_TYPE_FULL (TestA, test_a, |
|
142 test_a_class_init, NULL, NULL, |
|
143 G_TYPE_OBJECT, |
|
144 INTERFACE (test_a_interface_init, TEST_TYPE_I)) |
|
145 |
|
146 #define TEST_TYPE_B (test_b_get_type()) |
|
147 |
|
148 typedef struct _TestB TestB; |
|
149 typedef struct _TestBClass TestBClass; |
|
150 |
|
151 struct _TestB { |
|
152 TestA parent; |
|
153 }; |
|
154 struct _TestBClass { |
|
155 TestAClass parent_class; |
|
156 }; |
|
157 |
|
158 static void |
|
159 test_b_foo (TestA *self) |
|
160 { |
|
161 GValue args[1] = { { 0, } }; |
|
162 |
|
163 record ("TestB::foo"); |
|
164 |
|
165 g_value_init (&args[0], TEST_TYPE_A); |
|
166 g_value_set_object (&args[0], self); |
|
167 |
|
168 g_assert (g_signal_get_invocation_hint (self)->signal_id == foo_signal_id); |
|
169 g_signal_chain_from_overridden (args, NULL); |
|
170 |
|
171 g_value_unset (&args[0]); |
|
172 } |
|
173 |
|
174 static void |
|
175 test_b_bar (TestI *self) |
|
176 { |
|
177 GValue args[1] = { { 0, } }; |
|
178 |
|
179 record ("TestB::bar"); |
|
180 |
|
181 g_value_init (&args[0], TEST_TYPE_A); |
|
182 g_value_set_object (&args[0], self); |
|
183 |
|
184 g_assert (g_signal_get_invocation_hint (self)->signal_id == bar_signal_id); |
|
185 g_signal_chain_from_overridden (args, NULL); |
|
186 |
|
187 g_value_unset (&args[0]); |
|
188 } |
|
189 |
|
190 static void |
|
191 test_b_class_init (TestBClass *class) |
|
192 { |
|
193 g_signal_override_class_closure (foo_signal_id, |
|
194 TEST_TYPE_B, |
|
195 g_cclosure_new (G_CALLBACK (test_b_foo), |
|
196 NULL, NULL)); |
|
197 g_signal_override_class_closure (bar_signal_id, |
|
198 TEST_TYPE_B, |
|
199 g_cclosure_new (G_CALLBACK (test_b_bar), |
|
200 NULL, NULL)); |
|
201 } |
|
202 |
|
203 static DEFINE_TYPE (TestB, test_b, |
|
204 test_b_class_init, NULL, NULL, |
|
205 TEST_TYPE_A) |
|
206 |
|
207 #define TEST_TYPE_C (test_c_get_type()) |
|
208 |
|
209 typedef struct _TestC TestC; |
|
210 typedef struct _TestCClass TestCClass; |
|
211 |
|
212 struct _TestC { |
|
213 TestB parent; |
|
214 }; |
|
215 struct _TestCClass { |
|
216 TestBClass parent_class; |
|
217 }; |
|
218 |
|
219 static void |
|
220 test_c_foo (TestA *self) |
|
221 { |
|
222 GValue args[1] = { { 0, } }; |
|
223 |
|
224 record ("TestC::foo"); |
|
225 |
|
226 g_value_init (&args[0], TEST_TYPE_A); |
|
227 g_value_set_object (&args[0], self); |
|
228 |
|
229 g_assert (g_signal_get_invocation_hint (self)->signal_id == foo_signal_id); |
|
230 g_signal_chain_from_overridden (args, NULL); |
|
231 |
|
232 g_value_unset (&args[0]); |
|
233 } |
|
234 |
|
235 static void |
|
236 test_c_bar (TestI *self) |
|
237 { |
|
238 GValue args[1] = { { 0, } }; |
|
239 |
|
240 record ("TestC::bar"); |
|
241 |
|
242 g_value_init (&args[0], TEST_TYPE_A); |
|
243 g_value_set_object (&args[0], self); |
|
244 |
|
245 g_assert (g_signal_get_invocation_hint (self)->signal_id == bar_signal_id); |
|
246 g_signal_chain_from_overridden (args, NULL); |
|
247 |
|
248 g_value_unset (&args[0]); |
|
249 } |
|
250 |
|
251 static void |
|
252 test_c_class_init (TestBClass *class) |
|
253 { |
|
254 g_signal_override_class_closure (foo_signal_id, |
|
255 TEST_TYPE_C, |
|
256 g_cclosure_new (G_CALLBACK (test_c_foo), |
|
257 NULL, NULL)); |
|
258 g_signal_override_class_closure (bar_signal_id, |
|
259 TEST_TYPE_C, |
|
260 g_cclosure_new (G_CALLBACK (test_c_bar), |
|
261 NULL, NULL)); |
|
262 } |
|
263 |
|
264 |
|
265 static DEFINE_TYPE (TestC, test_c, |
|
266 test_c_class_init, NULL, NULL, |
|
267 TEST_TYPE_B) |
|
268 |
|
269 static GString *test_string = NULL; |
|
270 gboolean failed = FALSE; |
|
271 |
|
272 static void |
|
273 record (const gchar *str) |
|
274 { |
|
275 if (test_string->len) |
|
276 g_string_append_c (test_string, ','); |
|
277 g_string_append (test_string, str); |
|
278 } |
|
279 |
|
280 static void |
|
281 test (GType type, |
|
282 const gchar *signal, |
|
283 const gchar *expected) |
|
284 { |
|
285 GObject *self = g_object_new (type, NULL); |
|
286 |
|
287 test_string = g_string_new (NULL); |
|
288 |
|
289 g_signal_emit_by_name (self, signal, 0); |
|
290 |
|
291 #ifndef VERBOSE |
|
292 if (strcmp (test_string->str, expected) != 0) |
|
293 #endif |
|
294 { |
|
295 g_printerr ("*** emitting %s on a %s instance\n" |
|
296 " Expecting: %s\n" |
|
297 " Got: %s\n", |
|
298 signal, g_type_name (type), |
|
299 expected, |
|
300 test_string->str); |
|
301 |
|
302 if (strcmp (test_string->str, expected) != 0) |
|
303 failed = TRUE; |
|
304 } |
|
305 |
|
306 g_string_free (test_string, TRUE); |
|
307 } |
|
308 |
|
309 int |
|
310 main (int argc, char **argv) |
|
311 { |
|
312 #ifdef SYMBIAN |
|
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); |
|
315 #endif /*SYMBIAN*/ |
|
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); |
|
319 g_type_init(); |
|
320 |
|
321 test (TEST_TYPE_A, "foo", "TestA::foo,TestI::foo"); |
|
322 test (TEST_TYPE_A, "bar", "TestA::bar"); |
|
323 |
|
324 test (TEST_TYPE_B, "foo", "TestB::foo,TestA::foo,TestI::foo"); |
|
325 test (TEST_TYPE_B, "bar", "TestB::bar,TestA::bar"); |
|
326 |
|
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"); |
|
329 |
|
330 |
|
331 #ifdef SYMBIAN |
|
332 testResultXml("override"); |
|
333 #endif /*SYMBIAN*/ |
|
334 return failed ? 1 : 0; |
|
335 } |