First public contribution.
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2001, 2003 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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
21 //#define G_LOG_DOMAIN "TestIfaceProperties"
23 #undef G_DISABLE_ASSERT
24 #undef G_DISABLE_CHECKS
25 #undef G_DISABLE_CAST_CHECKS
29 #include <glib-object.h>
31 #include <gobject_global.h>
33 #include "testcommon.h"
36 #include "mrt2_glib2_test.h"
40 #include <glib_global.h>
41 static guint foo_signal_id = 0; //Amarjeet
44 MAMAN_BAR_CONSTRUCT_NAME = 1,
45 MAMAN_BAR_PAPA_NUMBER,
49 /* This test tests interface properties, implementing interface
50 * properties and #GParamSpecOverride.
52 * Four properties are tested:
54 * prop1: Defined in TestIface, Implemented in BaseObject with a GParamSpecOverride
55 * prop2: Defined in TestIface, Implemented in BaseObject with a new property
56 * prop3: Defined in TestIface, Implemented in BaseObject, Overridden in DerivedObject
57 * prop4: Defined in BaseObject, Overridden in DerivedObject
60 static GType base_object_get_type ();
61 static GType derived_object_get_type ();
78 * BaseObject, a parent class for DerivedObject
80 #define BASE_TYPE_OBJECT (base_object_get_type ())
81 #define BASE_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BASE_TYPE_OBJECT, BaseObject))
82 typedef struct _BaseObject BaseObject;
83 typedef struct _BaseObjectClass BaseObjectClass;
87 GObject parent_instance;
94 struct _BaseObjectClass
96 GObjectClass parent_class;
99 GObjectClass *base_parent_class;
102 * DerivedObject, the child class of DerivedObject
104 #define DERIVED_TYPE_OBJECT (derived_object_get_type ())
105 typedef struct _DerivedObject DerivedObject;
106 typedef struct _DerivedObjectClass DerivedObjectClass;
108 struct _DerivedObject
110 BaseObject parent_instance;
112 struct _DerivedObjectClass
114 BaseObjectClass parent_class;
120 typedef struct _TestIfaceClass TestIfaceClass;
122 struct _TestIfaceClass
124 GTypeInterface base_iface;
127 #define TEST_TYPE_IFACE (test_iface_get_type ())
129 /* The paramspecs installed on our interface
131 static GParamSpec *iface_spec1, *iface_spec2, *iface_spec3;
133 /* The paramspecs inherited by our derived object
135 static GParamSpec *inherited_spec1, *inherited_spec2, *inherited_spec3, *inherited_spec4;
140 test_iface_default_init (TestIfaceClass *iface_vtable)
142 inherited_spec1 = iface_spec1 = g_param_spec_int ("prop1",
148 (G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
149 g_object_interface_install_property (iface_vtable, iface_spec1);
151 iface_spec2 = g_param_spec_int ("prop2",
158 g_object_interface_install_property (iface_vtable, iface_spec2);
160 inherited_spec3 = iface_spec3 = g_param_spec_int ("prop3",
167 g_object_interface_install_property (iface_vtable, iface_spec3);
170 static DEFINE_IFACE (TestIface, test_iface, NULL, test_iface_default_init)
174 base_object_constructor (GType type,
175 guint n_construct_properties,
176 GObjectConstructParam *construct_properties)
178 /* The constructor is the one place where a GParamSpecOverride is visible
179 * to the outside world, so we do a bunch of checks here
181 GValue value1 = { 0, };
182 GValue value2 = { 0, };
185 g_assert (n_construct_properties == 1);
187 pspec = construct_properties->pspec;
189 /* Check we got the param spec we expected
191 g_assert (G_IS_PARAM_SPEC_OVERRIDE (pspec));
192 g_assert (pspec->param_id == BASE_PROP1);
193 g_assert (strcmp (g_param_spec_get_name (pspec), "prop1") == 0);
194 g_assert (g_param_spec_get_redirect_target (pspec) == iface_spec1);
196 /* Test redirection of the nick and blurb to the redirect target
198 g_assert (strcmp (g_param_spec_get_nick (pspec), "Prop1") == 0);
199 g_assert (strcmp (g_param_spec_get_blurb (pspec), "Property 1") == 0);
201 /* Test forwarding of the various GParamSpec methods to the redirect target
203 g_value_init (&value1, G_TYPE_INT);
204 g_value_init (&value2, G_TYPE_INT);
206 g_param_value_set_default (pspec, &value1);
207 g_assert (g_value_get_int (&value1) == 42);
209 g_value_reset (&value1);
210 g_value_set_int (&value1, 0x10000);
211 g_assert (g_param_value_validate (pspec, &value1));
212 g_assert (g_value_get_int (&value1) == 0xFFFF);
213 g_assert (!g_param_value_validate (pspec, &value1));
215 g_value_reset (&value1);
216 g_value_set_int (&value1, 1);
217 g_value_set_int (&value2, 2);
218 g_assert (g_param_values_cmp (pspec, &value1, &value2) < 0);
219 g_assert (g_param_values_cmp (pspec, &value2, &value1) > 0);
221 g_value_unset (&value1);
222 g_value_unset (&value2);
224 return base_parent_class->constructor (type,
225 n_construct_properties,
226 construct_properties);
230 base_object_set_property (GObject *object,
235 BaseObject *base_object = BASE_OBJECT (object);
240 g_assert (pspec == inherited_spec1);
241 base_object->val1 = g_value_get_int (value);
244 g_assert (pspec == inherited_spec2);
245 base_object->val2 = g_value_get_int (value);
248 g_assert_not_reached ();
251 g_assert_not_reached ();
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260 base_object_get_property (GObject *object,
265 BaseObject *base_object = BASE_OBJECT (object);
270 g_assert (pspec == inherited_spec1);
271 g_value_set_int (value, base_object->val1);
274 g_assert (pspec == inherited_spec2);
275 g_value_set_int (value, base_object->val2);
278 g_assert_not_reached ();
281 g_assert_not_reached ();
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290 base_object_notify (GObject *object,
293 /* The property passed to notify is the redirect target, not the
296 g_assert (pspec == inherited_spec1 ||
297 pspec == inherited_spec2 ||
298 pspec == inherited_spec3 ||
299 pspec == inherited_spec4);
303 base_object_class_init (BaseObjectClass *class)
305 GObjectClass *object_class = G_OBJECT_CLASS (class);
307 base_parent_class= g_type_class_peek_parent (class);
309 object_class->constructor = base_object_constructor;
310 object_class->set_property = base_object_set_property;
311 object_class->get_property = base_object_get_property;
312 object_class->notify = base_object_notify;
314 g_object_class_override_property (object_class, BASE_PROP1, "prop1");
316 /* We override this one using a real property, not GParamSpecOverride
317 * We change the flags from READONLY to READWRITE to show that we
318 * can make the flags less restrictive
320 inherited_spec2 = g_param_spec_int ("prop2",
327 g_object_class_install_property (object_class, BASE_PROP2, inherited_spec2);
329 g_object_class_override_property (object_class, BASE_PROP3, "prop3");
331 inherited_spec4 = g_param_spec_int ("prop4",
338 g_object_class_install_property (object_class, BASE_PROP4, inherited_spec4);
342 base_object_init (BaseObject *base_object)
344 base_object->val1 = 42;
347 static DEFINE_TYPE_FULL (BaseObject, base_object,
348 base_object_class_init, NULL, base_object_init,
350 INTERFACE (NULL, TEST_TYPE_IFACE))
353 derived_object_set_property (GObject *object,
358 BaseObject *base_object = BASE_OBJECT (object);
363 g_assert (pspec == inherited_spec3);
364 base_object->val3 = g_value_get_int (value);
367 g_assert (pspec == inherited_spec4);
368 base_object->val4 = g_value_get_int (value);
371 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
377 derived_object_get_property (GObject *object,
382 BaseObject *base_object = BASE_OBJECT (object);
387 g_assert (pspec == inherited_spec3);
388 g_value_set_int (value, base_object->val3);
391 g_assert (pspec == inherited_spec4);
392 g_value_set_int (value, base_object->val4);
395 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
401 derived_object_class_init (DerivedObjectClass *class)
403 GObjectClass *object_class = G_OBJECT_CLASS (class);
405 object_class->set_property = derived_object_set_property;
406 object_class->get_property = derived_object_get_property;
408 /* Overriding a property that is itself overridding an interface property */
409 g_object_class_override_property (object_class, DERIVED_PROP3, "prop3");
411 /* Overriding a property not from an interface */
412 g_object_class_override_property (object_class, DERIVED_PROP4, "prop4");
415 static DEFINE_TYPE (DerivedObject, derived_object,
416 derived_object_class_init, NULL, NULL,
419 /* Helper function for testing ...list_properties()
422 assert_in_properties (GParamSpec *param_spec,
423 GParamSpec **properties,
427 gboolean found = FALSE;
429 for (i = 0; i < n_properties; i++)
431 if (properties[i] == param_spec)
437 /**** Dummy function which does nothing *******/
438 gboolean MyFunc (gpointer cache_data,
444 void MyFunc1 (gpointer data,
456 GObject *object, *object1;
457 GObjectClass *object_class;
458 GParamSpec **properties;
459 gpointer p ="Data",p1 = "Data1",ret;
462 GParamSpec* paramSpec;
464 GDestroyNotify destroy;
465 gpointer wkPtr= "Weak Pointer";
468 GTypeClassCacheFunc cache_func = MyFunc;
470 GEnumClass *type_class;
472 GCallback callback_func;
473 GClosure *closure_ret;
474 gpointer notify_data= "Notify";
475 gpointer notify_data1;
476 GClosureNotify notify_func;
477 gpointer pre_marshal_data = "MarshalData";
478 GClosureNotify pre_marshal_notify;
479 gpointer post_marshal_data = "PostMarshal";
480 GClosureNotify post_marshal_notify = MyFunc1;
481 GFlagsClass *flags_class;
482 GFlagsValue* flagValue;
483 GTypeModule *module1;
485 gpointer marshal_data;
486 gpointer invocation_hint;
490 gpointer user_data= "name";
493 const GEnumValue static_values;
494 static const GFlagsValue const_static_values[] = {
495 { G_IO_IN, "G_IO_IN", "in" },
496 { G_IO_OUT, "G_IO_OUT", "out" },
497 { G_IO_PRI, "G_IO_PRI", "pri" },
498 { G_IO_ERR, "G_IO_ERR", "err" },
499 { G_IO_HUP, "G_IO_HUP", "hup" },
500 { G_IO_NVAL, "G_IO_NVAL", "nval" },
506 gint val2, val3, val4;
508 GSignalEmissionHook hook_func;
510 GDestroyNotify data_destroy;
512 GValue orig = { 0, };
513 GValue xform = { 0, };
514 GEnumValue values[] = { {0,"0","0"}, {1,"1","1"}};
517 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);
520 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
521 G_LOG_LEVEL_WARNING |
522 G_LOG_LEVEL_CRITICAL);
525 object = g_object_new (DERIVED_TYPE_OBJECT, NULL);
527 /* Test setting and getting the properties
531 memset (&xform, 0, sizeof (GValue));
532 g_value_init (&xform, G_TYPE_CHAR);
534 g_object_set_data(object , "Key",p);
535 ret = g_object_get_data(object , "Key");
537 g_assert (!strcmp(str , "Data"));
539 g_object_set_data_full (object, "Key1", p1, destroy);
540 ret = g_object_get_data(object , "Key1");
542 g_assert (!strcmp(str , "Data1"));
544 g_object_set_qdata (object, 1, p1);
546 g_object_set_qdata_full (object, 1, p1, destroy);
548 ret = g_object_get_qdata (object, 1);
552 g_assert (!strcmp(str , "Data1"));
554 ret = g_object_steal_data (object , "Key1");
558 g_assert (!strcmp(str , "Data1"));
560 ret = g_object_steal_qdata (object , 1);
564 g_assert (!strcmp(str , "Data1"));
566 g_object_run_dispose(object);
568 g_object_add_weak_pointer (object, wkPtr);
570 g_object_remove_weak_pointer(object, wkPtr);
572 g_assert (g_type_qname (G_TYPE_INTERFACE) == 5);
574 g_type_query (G_TYPE_OBJECT, &query);
576 g_assert (query.type == 80);
578 g_type_set_qdata (G_TYPE_OBJECT ,1 , wkPtr);
580 ret = g_type_get_qdata (G_TYPE_OBJECT ,1);
584 g_assert (!strcmp(str , "Weak Pointer"));
586 g_type_add_class_cache_func (wkPtr, cache_func);
588 g_type_remove_class_cache_func (wkPtr, cache_func);
590 g_assert (g_type_next_base (G_TYPE_OBJECT, G_TYPE_INT) == 0);
592 type_class = g_type_class_ref (G_TYPE_ENUM);
594 g_assert (g_enum_get_value_by_nick (G_ENUM_CLASS (type_class),"Nick") == NULL) ;
596 g_assert ((g_enum_register_static ("Name", &enum_ret)) != 0);
598 g_assert((g_cclosure_new_object((GCallback)MyFunc1, object)) != NULL);
600 closure_ret = g_closure_new_object(sizeof(GClosure),object);
602 g_assert((g_closure_new_object(sizeof(GClosure),object)) != NULL);
604 g_closure_add_finalize_notifier (closure_ret, notify_data, MyFunc1);
606 g_closure_remove_finalize_notifier (closure_ret, notify_data, MyFunc1);
608 closure_ret->n_guards = 0; //Changed the structure to pass the below api.
610 g_closure_add_marshal_guards (closure_ret, pre_marshal_data, MyFunc1, post_marshal_data, MyFunc1);
612 g_assert(g_pointer_type_register_static ("G_TYPE_OBJECT") != 0);
614 paramSpec = g_param_spec_boolean("String1", "Hello", "World", FALSE, flags);
616 g_assert( !strcmp(paramSpec->name , "String1"));
618 flags_class = g_type_class_ref(G_TYPE_FLAGS);
620 flagValue = g_flags_get_value_by_name (flags_class, "Name");
622 g_assert (flagValue == NULL);
624 flagValue = g_flags_get_value_by_nick (flags_class, "Name");
626 g_assert (flagValue == NULL);
628 object1 = g_object_new (DERIVED_TYPE_OBJECT, NULL);
630 t = g_flags_register_static ("GIOCondition", const_static_values);
634 ret = g_object_connect (object1, NULL);
636 g_assert (ret != NULL);
638 g_object_disconnect(object1, NULL);
640 paramSpec = g_param_spec_uchar ("papa-number",
642 "Set/Get papa's number",
643 0 /* minimum value */,
644 10 /* maximum value */,
645 2 /* default value */,
648 object_set = G_OBJECT (NULL);
650 g_value_init (&val1, paramSpec->value_type);
653 testResultXml("g_test2");
654 #endif /* EMULATOR */