Add some more cases to the app-id unit tests
[glib.git] / tests / gobject / testgobject.c
blob4fe36400b19114e1247834b3fae601191c1d5e20
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2001 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 #include <string.h>
20 #undef G_LOG_DOMAIN
21 #define G_LOG_DOMAIN "TestObject"
22 #include <glib-object.h>
24 /* --- TestIface --- */
25 #define TEST_TYPE_IFACE (test_iface_get_type ())
26 #define TEST_IFACE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_TYPE_IFACE, TestIface))
27 #define TEST_IS_IFACE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEST_TYPE_IFACE))
28 #define TEST_IFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass))
29 typedef struct _TestIface TestIface;
30 typedef struct _TestIfaceClass TestIfaceClass;
31 struct _TestIfaceClass
33 GTypeInterface base_iface;
34 void (*print_string) (TestIface *tiobj,
35 const gchar *string);
37 static void iface_base_init (TestIfaceClass *iface);
38 static void iface_base_finalize (TestIfaceClass *iface);
39 static void print_foo (TestIface *tiobj,
40 const gchar *string);
41 static GType
42 test_iface_get_type (void)
44 static GType test_iface_type = 0;
46 if (!test_iface_type)
48 const GTypeInfo test_iface_info =
50 sizeof (TestIfaceClass),
51 (GBaseInitFunc) iface_base_init, /* base_init */
52 (GBaseFinalizeFunc) iface_base_finalize, /* base_finalize */
55 test_iface_type = g_type_register_static (G_TYPE_INTERFACE, "TestIface", &test_iface_info, 0);
56 g_type_interface_add_prerequisite (test_iface_type, G_TYPE_OBJECT);
59 return test_iface_type;
61 static guint iface_base_init_count = 0;
62 static void
63 iface_base_init (TestIfaceClass *iface)
65 iface_base_init_count++;
66 if (iface_base_init_count == 1)
68 /* add signals here */
71 static void
72 iface_base_finalize (TestIfaceClass *iface)
74 iface_base_init_count--;
75 if (iface_base_init_count == 0)
77 /* destroy signals here */
80 static void
81 print_foo (TestIface *tiobj,
82 const gchar *string)
84 if (!string)
85 string = "<NULL>";
86 g_print ("Iface-FOO: \"%s\" from %p\n", string, tiobj);
88 static void
89 test_object_test_iface_init (gpointer giface,
90 gpointer iface_data)
92 TestIfaceClass *iface = giface;
94 g_assert (iface_data == GUINT_TO_POINTER (42));
96 g_assert (G_TYPE_FROM_INTERFACE (iface) == TEST_TYPE_IFACE);
98 /* assert iface_base_init() was already called */
99 g_assert (iface_base_init_count > 0);
101 /* initialize stuff */
102 iface->print_string = print_foo;
104 static void
105 iface_print_string (TestIface *tiobj,
106 const gchar *string)
108 TestIfaceClass *iface;
110 g_return_if_fail (TEST_IS_IFACE (tiobj));
111 g_return_if_fail (G_IS_OBJECT (tiobj)); /* ensured through prerequisite */
113 iface = TEST_IFACE_GET_CLASS (tiobj);
114 g_object_ref (tiobj);
115 iface->print_string (tiobj, string);
116 g_object_unref (tiobj);
120 /* --- TestObject --- */
121 #define TEST_TYPE_OBJECT (test_object_get_type ())
122 #define TEST_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), TEST_TYPE_OBJECT, TestObject))
123 #define TEST_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TEST_TYPE_OBJECT, TestObjectClass))
124 #define TEST_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), TEST_TYPE_OBJECT))
125 #define TEST_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TEST_TYPE_OBJECT))
126 #define TEST_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TEST_TYPE_OBJECT, TestObjectClass))
127 #define TEST_OBJECT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TEST_TYPE_OBJECT, TestObjectPrivate))
128 typedef struct _TestObject TestObject;
129 typedef struct _TestObjectClass TestObjectClass;
130 typedef struct _TestObjectPrivate TestObjectPrivate;
131 struct _TestObject
133 GObject parent_instance;
135 struct _TestObjectClass
137 GObjectClass parent_class;
139 gchar* (*test_signal) (TestObject *tobject,
140 TestIface *iface_object,
141 gpointer tdata);
143 struct _TestObjectPrivate
145 int dummy1;
146 gdouble dummy2;
148 static void test_object_class_init (TestObjectClass *class);
149 static void test_object_init (TestObject *tobject);
150 static gboolean test_signal_accumulator (GSignalInvocationHint *ihint,
151 GValue *return_accu,
152 const GValue *handler_return,
153 gpointer data);
154 static gchar* test_object_test_signal (TestObject *tobject,
155 TestIface *iface_object,
156 gpointer tdata);
157 static GType
158 test_object_get_type (void)
160 static GType test_object_type = 0;
162 if (!test_object_type)
164 const GTypeInfo test_object_info =
166 sizeof (TestObjectClass),
167 NULL, /* base_init */
168 NULL, /* base_finalize */
169 (GClassInitFunc) test_object_class_init,
170 NULL, /* class_finalize */
171 NULL, /* class_data */
172 sizeof (TestObject),
173 5, /* n_preallocs */
174 (GInstanceInitFunc) test_object_init,
176 GInterfaceInfo iface_info = { test_object_test_iface_init, NULL, GUINT_TO_POINTER (42) };
178 test_object_type = g_type_register_static (G_TYPE_OBJECT, "TestObject", &test_object_info, 0);
179 g_type_add_interface_static (test_object_type, TEST_TYPE_IFACE, &iface_info);
182 return test_object_type;
184 static void
185 test_object_class_init (TestObjectClass *class)
187 /* GObjectClass *gobject_class = G_OBJECT_CLASS (class); */
189 class->test_signal = test_object_test_signal;
191 g_signal_new ("test-signal",
192 G_OBJECT_CLASS_TYPE (class),
193 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP,
194 G_STRUCT_OFFSET (TestObjectClass, test_signal),
195 test_signal_accumulator, NULL,
196 g_cclosure_marshal_STRING__OBJECT_POINTER,
197 G_TYPE_STRING, 2, TEST_TYPE_IFACE, G_TYPE_POINTER);
199 g_type_class_add_private (class, sizeof (TestObjectPrivate));
201 static void
202 test_object_init (TestObject *tobject)
204 TestObjectPrivate *priv;
206 priv = TEST_OBJECT_GET_PRIVATE (tobject);
208 g_assert (priv);
209 g_assert ((gchar *)priv >= (gchar *)tobject + sizeof (TestObject));
211 priv->dummy1 = 54321;
213 /* Check to see if private data initialization in the
214 * instance init function works.
216 static void
217 test_object_check_private_init (TestObject *tobject)
219 TestObjectPrivate *priv;
221 priv = TEST_OBJECT_GET_PRIVATE (tobject);
223 g_print ("private data during initialization: %u == %u\n", priv->dummy1, 54321);
224 g_assert (priv->dummy1 == 54321);
226 static gboolean
227 test_signal_accumulator (GSignalInvocationHint *ihint,
228 GValue *return_accu,
229 const GValue *handler_return,
230 gpointer data)
232 const gchar *accu_string = g_value_get_string (return_accu);
233 const gchar *new_string = g_value_get_string (handler_return);
234 gchar *result_string;
236 if (accu_string)
237 result_string = g_strconcat (accu_string, new_string, NULL);
238 else if (new_string)
239 result_string = g_strdup (new_string);
240 else
241 result_string = NULL;
243 g_value_take_string (return_accu, result_string);
245 return TRUE;
247 static gchar*
248 test_object_test_signal (TestObject *tobject,
249 TestIface *iface_object,
250 gpointer tdata)
252 g_message ("::test_signal default_handler called");
254 g_return_val_if_fail (TEST_IS_IFACE (iface_object), NULL);
256 return g_strdup ("<default_handler>");
260 /* --- TestIface for DerivedObject --- */
261 static void
262 print_bar (TestIface *tiobj,
263 const gchar *string)
265 TestIfaceClass *parent_iface;
267 g_return_if_fail (TEST_IS_IFACE (tiobj));
269 if (!string)
270 string = "<NULL>";
271 g_print ("Iface-BAR: \"%s\" from %p\n", string, tiobj);
273 g_print ("chaining: ");
274 parent_iface = g_type_interface_peek_parent (TEST_IFACE_GET_CLASS (tiobj));
275 parent_iface->print_string (tiobj, string);
277 g_assert (g_type_interface_peek_parent (parent_iface) == NULL);
280 static void
281 derived_object_test_iface_init (gpointer giface,
282 gpointer iface_data)
284 TestIfaceClass *iface = giface;
286 g_assert (iface_data == GUINT_TO_POINTER (87));
288 g_assert (G_TYPE_FROM_INTERFACE (iface) == TEST_TYPE_IFACE);
290 /* assert test_object_test_iface_init() was already called */
291 g_assert (iface->print_string == print_foo);
293 /* override stuff */
294 iface->print_string = print_bar;
298 /* --- DerivedObject --- */
299 #define DERIVED_TYPE_OBJECT (derived_object_get_type ())
300 #define DERIVED_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), DERIVED_TYPE_OBJECT, DerivedObject))
301 #define DERIVED_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DERIVED_TYPE_OBJECT, DerivedObjectClass))
302 #define DERIVED_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), DERIVED_TYPE_OBJECT))
303 #define DERIVED_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DERIVED_TYPE_OBJECT))
304 #define DERIVED_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DERIVED_TYPE_OBJECT, DerivedObjectClass))
305 #define DERIVED_OBJECT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DERIVED_TYPE_OBJECT, DerivedObjectPrivate))
306 typedef struct _DerivedObject DerivedObject;
307 typedef struct _TestObjectClass DerivedObjectClass;
308 typedef struct _DerivedObjectPrivate DerivedObjectPrivate;
309 struct _DerivedObject
311 TestObject parent_instance;
312 int dummy1;
313 int dummy2;
315 struct _DerivedObjectPrivate
317 char dummy;
319 static void derived_object_class_init (DerivedObjectClass *class);
320 static void derived_object_init (DerivedObject *dobject);
321 static GType
322 derived_object_get_type (void)
324 static GType derived_object_type = 0;
326 if (!derived_object_type)
328 const GTypeInfo derived_object_info =
330 sizeof (DerivedObjectClass),
331 NULL, /* base_init */
332 NULL, /* base_finalize */
333 (GClassInitFunc) derived_object_class_init,
334 NULL, /* class_finalize */
335 NULL, /* class_data */
336 sizeof (DerivedObject),
337 5, /* n_preallocs */
338 (GInstanceInitFunc) derived_object_init,
340 GInterfaceInfo iface_info = { derived_object_test_iface_init, NULL, GUINT_TO_POINTER (87) };
342 derived_object_type = g_type_register_static (TEST_TYPE_OBJECT, "DerivedObject", &derived_object_info, 0);
343 g_type_add_interface_static (derived_object_type, TEST_TYPE_IFACE, &iface_info);
346 return derived_object_type;
348 static void
349 derived_object_class_init (DerivedObjectClass *class)
351 g_type_class_add_private (class, sizeof (DerivedObjectPrivate));
353 static void
354 derived_object_init (DerivedObject *dobject)
356 TestObjectPrivate *test_priv;
357 DerivedObjectPrivate *derived_priv;
359 derived_priv = DERIVED_OBJECT_GET_PRIVATE (dobject);
361 g_assert (derived_priv);
362 g_assert ((gchar *)derived_priv >= (gchar *)TEST_OBJECT_GET_PRIVATE (dobject) + sizeof (TestObjectPrivate));
364 test_priv = TEST_OBJECT_GET_PRIVATE (dobject);
366 g_assert (test_priv);
367 g_assert ((gchar *)test_priv >= (gchar *)dobject + sizeof (TestObject));
371 /* --- main --- */
373 main (int argc,
374 char *argv[])
376 GTypeInfo info = { 0, };
377 GTypeFundamentalInfo finfo = { 0, };
378 GType type;
379 TestObject *sigarg;
380 DerivedObject *dobject;
381 TestObjectPrivate *priv;
382 gchar *string = NULL;
384 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
385 G_LOG_LEVEL_WARNING |
386 G_LOG_LEVEL_CRITICAL);
388 /* test new fundamentals */
389 g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST) == g_type_fundamental_next ());
390 type = g_type_register_fundamental (g_type_fundamental_next (), "FooShadow1", &info, &finfo, 0);
391 g_assert (type == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST));
392 g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 1) == g_type_fundamental_next ());
393 type = g_type_register_fundamental (g_type_fundamental_next (), "FooShadow2", &info, &finfo, 0);
394 g_assert (type == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 1));
395 g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 2) == g_type_fundamental_next ());
396 g_assert (g_type_from_name ("FooShadow1") == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST));
397 g_assert (g_type_from_name ("FooShadow2") == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 1));
399 /* to test past class initialization interface setups, create the class here */
400 g_type_class_ref (TEST_TYPE_OBJECT);
402 dobject = g_object_new (DERIVED_TYPE_OBJECT, NULL);
403 test_object_check_private_init (TEST_OBJECT (dobject));
405 sigarg = g_object_new (TEST_TYPE_OBJECT, NULL);
407 g_print ("MAIN: emit test-signal:\n");
408 g_signal_emit_by_name (dobject, "test-signal", sigarg, NULL, &string);
409 g_message ("signal return: \"%s\"", string);
410 g_assert (strcmp (string, "<default_handler><default_handler>") == 0);
411 g_free (string);
413 g_print ("MAIN: call iface print-string on test and derived object:\n");
414 iface_print_string (TEST_IFACE (sigarg), "iface-string-from-test-type");
415 iface_print_string (TEST_IFACE (dobject), "iface-string-from-derived-type");
417 priv = TEST_OBJECT_GET_PRIVATE (dobject);
418 g_print ("private data after initialization: %u == %u\n", priv->dummy1, 54321);
419 g_assert (priv->dummy1 == 54321);
421 g_object_unref (sigarg);
422 g_object_unref (dobject);
424 g_message ("%s done", argv[0]);
426 return 0;