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.
23 #include <glib-object.h>
25 #include "testcommon.h"
27 /* This test tests interface properties, implementing interface
28 * properties and #GParamSpecOverride.
30 * Four properties are tested:
32 * prop1: Defined in TestIface, Implemented in BaseObject with a GParamSpecOverride
33 * prop2: Defined in TestIface, Implemented in BaseObject with a new property
34 * prop3: Defined in TestIface, Implemented in BaseObject, Overridden in DerivedObject
35 * prop4: Defined in BaseObject, Overridden in DerivedObject
38 static GType base_object_get_type (void);
39 static GType derived_object_get_type (void);
56 * BaseObject, a parent class for DerivedObject
58 #define BASE_TYPE_OBJECT (base_object_get_type ())
59 #define BASE_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BASE_TYPE_OBJECT, BaseObject))
60 typedef struct _BaseObject BaseObject;
61 typedef struct _BaseObjectClass BaseObjectClass;
65 GObject parent_instance;
72 struct _BaseObjectClass
74 GObjectClass parent_class;
77 GObjectClass *base_parent_class;
80 * DerivedObject, the child class of DerivedObject
82 #define DERIVED_TYPE_OBJECT (derived_object_get_type ())
83 typedef struct _DerivedObject DerivedObject;
84 typedef struct _DerivedObjectClass DerivedObjectClass;
88 BaseObject parent_instance;
90 struct _DerivedObjectClass
92 BaseObjectClass parent_class;
98 typedef struct _TestIfaceClass TestIfaceClass;
100 struct _TestIfaceClass
102 GTypeInterface base_iface;
105 #define TEST_TYPE_IFACE (test_iface_get_type ())
107 /* The paramspecs installed on our interface
109 static GParamSpec *iface_spec1, *iface_spec2, *iface_spec3;
111 /* The paramspecs inherited by our derived object
113 static GParamSpec *inherited_spec1, *inherited_spec2, *inherited_spec3, *inherited_spec4;
116 test_iface_default_init (TestIfaceClass *iface_vtable)
118 inherited_spec1 = iface_spec1 = g_param_spec_int ("prop1",
124 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
125 g_object_interface_install_property (iface_vtable, iface_spec1);
127 iface_spec2 = g_param_spec_int ("prop2",
134 g_object_interface_install_property (iface_vtable, iface_spec2);
136 inherited_spec3 = iface_spec3 = g_param_spec_int ("prop3",
143 g_object_interface_install_property (iface_vtable, iface_spec3);
146 static DEFINE_IFACE (TestIface, test_iface, NULL, test_iface_default_init)
150 base_object_constructor (GType type,
151 guint n_construct_properties,
152 GObjectConstructParam *construct_properties)
154 /* The constructor is the one place where a GParamSpecOverride is visible
155 * to the outside world, so we do a bunch of checks here
157 GValue value1 = { 0, };
158 GValue value2 = { 0, };
161 g_assert (n_construct_properties == 1);
163 pspec = construct_properties->pspec;
165 /* Check we got the param spec we expected
167 g_assert (G_IS_PARAM_SPEC_OVERRIDE (pspec));
168 g_assert (pspec->param_id == BASE_PROP1);
169 g_assert (strcmp (g_param_spec_get_name (pspec), "prop1") == 0);
170 g_assert (g_param_spec_get_redirect_target (pspec) == iface_spec1);
172 /* Test redirection of the nick and blurb to the redirect target
174 g_assert (strcmp (g_param_spec_get_nick (pspec), "Prop1") == 0);
175 g_assert (strcmp (g_param_spec_get_blurb (pspec), "Property 1") == 0);
177 /* Test forwarding of the various GParamSpec methods to the redirect target
179 g_value_init (&value1, G_TYPE_INT);
180 g_value_init (&value2, G_TYPE_INT);
182 g_param_value_set_default (pspec, &value1);
183 g_assert (g_value_get_int (&value1) == 42);
185 g_value_reset (&value1);
186 g_value_set_int (&value1, 0x10000);
187 g_assert (g_param_value_validate (pspec, &value1));
188 g_assert (g_value_get_int (&value1) == 0xFFFF);
189 g_assert (!g_param_value_validate (pspec, &value1));
191 g_value_reset (&value1);
192 g_value_set_int (&value1, 1);
193 g_value_set_int (&value2, 2);
194 g_assert (g_param_values_cmp (pspec, &value1, &value2) < 0);
195 g_assert (g_param_values_cmp (pspec, &value2, &value1) > 0);
197 g_value_unset (&value1);
198 g_value_unset (&value2);
200 return base_parent_class->constructor (type,
201 n_construct_properties,
202 construct_properties);
206 base_object_set_property (GObject *object,
211 BaseObject *base_object = BASE_OBJECT (object);
216 g_assert (pspec == inherited_spec1);
217 base_object->val1 = g_value_get_int (value);
220 g_assert (pspec == inherited_spec2);
221 base_object->val2 = g_value_get_int (value);
224 g_assert_not_reached ();
227 g_assert_not_reached ();
230 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236 base_object_get_property (GObject *object,
241 BaseObject *base_object = BASE_OBJECT (object);
246 g_assert (pspec == inherited_spec1);
247 g_value_set_int (value, base_object->val1);
250 g_assert (pspec == inherited_spec2);
251 g_value_set_int (value, base_object->val2);
254 g_assert_not_reached ();
257 g_assert_not_reached ();
260 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266 base_object_notify (GObject *object,
269 /* The property passed to notify is the redirect target, not the
272 g_assert (pspec == inherited_spec1 ||
273 pspec == inherited_spec2 ||
274 pspec == inherited_spec3 ||
275 pspec == inherited_spec4);
279 base_object_class_init (BaseObjectClass *class)
281 GObjectClass *object_class = G_OBJECT_CLASS (class);
283 base_parent_class= g_type_class_peek_parent (class);
285 object_class->constructor = base_object_constructor;
286 object_class->set_property = base_object_set_property;
287 object_class->get_property = base_object_get_property;
288 object_class->notify = base_object_notify;
290 g_object_class_override_property (object_class, BASE_PROP1, "prop1");
292 /* We override this one using a real property, not GParamSpecOverride
293 * We change the flags from READONLY to READWRITE to show that we
294 * can make the flags less restrictive
296 inherited_spec2 = g_param_spec_int ("prop2",
303 g_object_class_install_property (object_class, BASE_PROP2, inherited_spec2);
305 g_object_class_override_property (object_class, BASE_PROP3, "prop3");
307 inherited_spec4 = g_param_spec_int ("prop4",
314 g_object_class_install_property (object_class, BASE_PROP4, inherited_spec4);
318 base_object_init (BaseObject *base_object)
320 base_object->val1 = 42;
323 static DEFINE_TYPE_FULL (BaseObject, base_object,
324 base_object_class_init, NULL, base_object_init,
326 INTERFACE (NULL, TEST_TYPE_IFACE))
329 derived_object_set_property (GObject *object,
334 BaseObject *base_object = BASE_OBJECT (object);
339 g_assert (pspec == inherited_spec3);
340 base_object->val3 = g_value_get_int (value);
343 g_assert (pspec == inherited_spec4);
344 base_object->val4 = g_value_get_int (value);
347 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
353 derived_object_get_property (GObject *object,
358 BaseObject *base_object = BASE_OBJECT (object);
363 g_assert (pspec == inherited_spec3);
364 g_value_set_int (value, base_object->val3);
367 g_assert (pspec == inherited_spec4);
368 g_value_set_int (value, base_object->val4);
371 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
377 derived_object_class_init (DerivedObjectClass *class)
379 GObjectClass *object_class = G_OBJECT_CLASS (class);
381 object_class->set_property = derived_object_set_property;
382 object_class->get_property = derived_object_get_property;
384 /* Overriding a property that is itself overridding an interface property */
385 g_object_class_override_property (object_class, DERIVED_PROP3, "prop3");
387 /* Overriding a property not from an interface */
388 g_object_class_override_property (object_class, DERIVED_PROP4, "prop4");
391 static DEFINE_TYPE (DerivedObject, derived_object,
392 derived_object_class_init, NULL, NULL,
395 /* Helper function for testing ...list_properties() */
397 assert_in_properties (GParamSpec *param_spec,
398 GParamSpec **properties,
402 gboolean found = FALSE;
404 for (i = 0; i < n_properties; i++)
406 if (properties[i] == param_spec)
413 /* Test setting and getting the properties */
418 gint val1, val2, val3, val4;
420 object = g_object_new (DERIVED_TYPE_OBJECT, NULL);
422 g_object_set (object,
428 g_object_get (object,
435 g_assert (val1 == 0x0101);
436 g_assert (val2 == 0x0202);
437 g_assert (val3 == 0x0303);
438 g_assert (val4 == 0x0404);
440 g_object_unref (object);
443 /* Test that the right spec is passed on explicit notifications */
449 object = g_object_new (DERIVED_TYPE_OBJECT, NULL);
451 g_object_freeze_notify (G_OBJECT (object));
452 g_object_notify (G_OBJECT (object), "prop1");
453 g_object_notify (G_OBJECT (object), "prop2");
454 g_object_notify (G_OBJECT (object), "prop3");
455 g_object_notify (G_OBJECT (object), "prop4");
456 g_object_thaw_notify (G_OBJECT (object));
458 g_object_unref (object);
461 /* Test g_object_class_find_property() for overridden properties */
463 test_find_overridden (void)
465 GObjectClass *object_class;
467 object_class = g_type_class_peek (DERIVED_TYPE_OBJECT);
469 g_assert (g_object_class_find_property (object_class, "prop1") == inherited_spec1);
470 g_assert (g_object_class_find_property (object_class, "prop2") == inherited_spec2);
471 g_assert (g_object_class_find_property (object_class, "prop3") == inherited_spec3);
472 g_assert (g_object_class_find_property (object_class, "prop4") == inherited_spec4);
475 /* Test g_object_class_list_properties() for overridden properties */
477 test_list_overridden (void)
479 GObjectClass *object_class;
480 GParamSpec **properties;
483 object_class = g_type_class_peek (DERIVED_TYPE_OBJECT);
485 properties = g_object_class_list_properties (object_class, &n_properties);
486 g_assert (n_properties == 4);
487 assert_in_properties (inherited_spec1, properties, n_properties);
488 assert_in_properties (inherited_spec2, properties, n_properties);
489 assert_in_properties (inherited_spec3, properties, n_properties);
490 assert_in_properties (inherited_spec4, properties, n_properties);
494 /* Test g_object_interface_find_property() */
496 test_find_interface (void)
498 TestIfaceClass *iface;
500 iface = g_type_default_interface_peek (TEST_TYPE_IFACE);
502 g_assert (g_object_interface_find_property (iface, "prop1") == iface_spec1);
503 g_assert (g_object_interface_find_property (iface, "prop2") == iface_spec2);
504 g_assert (g_object_interface_find_property (iface, "prop3") == iface_spec3);
507 /* Test g_object_interface_list_properties() */
509 test_list_interface (void)
511 TestIfaceClass *iface;
512 GParamSpec **properties;
515 iface = g_type_default_interface_peek (TEST_TYPE_IFACE);
517 properties = g_object_interface_list_properties (iface, &n_properties);
518 g_assert (n_properties == 3);
519 assert_in_properties (iface_spec1, properties, n_properties);
520 assert_in_properties (iface_spec2, properties, n_properties);
521 assert_in_properties (iface_spec3, properties, n_properties);
525 /* Base2Object, which implements the interface but fails
526 * to override some of its properties
528 #define BASE2_TYPE_OBJECT (base2_object_get_type ())
529 #define BASE2_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BASE2_TYPE_OBJECT, Base2Object))
531 typedef struct _Base2Object Base2Object;
532 typedef struct _Base2ObjectClass Base2ObjectClass;
535 base2_object_test_iface_init (TestIfaceClass *iface)
547 GObject parent_instance;
550 struct _Base2ObjectClass
552 GObjectClass parent_class;
555 static GType base2_object_get_type (void);
556 G_DEFINE_TYPE_WITH_CODE (Base2Object, base2_object, G_TYPE_OBJECT,
557 G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE,
558 base2_object_test_iface_init))
561 base2_object_get_property (GObject *object,
569 g_value_set_int (value, 0);
572 g_value_set_int (value, 0);
575 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
581 base2_object_set_property (GObject *object,
593 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
599 base2_object_class_init (Base2ObjectClass *class)
601 GObjectClass *object_class = G_OBJECT_CLASS (class);
603 object_class->set_property = base2_object_set_property;
604 object_class->get_property = base2_object_get_property;
606 g_object_class_override_property (object_class, BASE2_PROP1, "prop1");
607 g_object_class_override_property (object_class, BASE2_PROP2, "prop2");
611 base2_object_init (Base2Object *object)
616 test_not_overridden (void)
618 g_test_bug ("637738");
620 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT|G_TEST_TRAP_SILENCE_STDERR))
624 object = g_object_new (BASE2_TYPE_OBJECT, NULL);
625 g_object_unref (object);
628 g_test_trap_assert_failed ();
629 g_test_trap_assert_stderr ("*Base2Object doesn't implement property 'prop3' from interface 'TestIface'*");
633 main (int argc, char *argv[])
637 g_test_init (&argc, &argv, NULL);
638 g_test_bug_base ("http://bugzilla.gnome.org/");
640 g_test_add_func ("/interface/properties/set", test_set);
641 g_test_add_func ("/interface/properties/notify", test_notify);
642 g_test_add_func ("/interface/properties/find-overridden", test_find_overridden);
643 g_test_add_func ("/interface/properties/list-overridden", test_list_overridden);
644 g_test_add_func ("/interface/properties/find-interface", test_find_interface);
645 g_test_add_func ("/interface/properties/list-interface", test_list_interface);
646 g_test_add_func ("/interface/properties/not-overridden", test_not_overridden);
648 return g_test_run ();