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 "testcommon.h"
33 /* This test tests interface properties, implementing interface
34 * properties and #GParamSpecOverride.
36 * Four properties are tested:
38 * prop1: Defined in TestIface, Implemented in BaseObject with a GParamSpecOverride
39 * prop2: Defined in TestIface, Implemented in BaseObject with a new property
40 * prop3: Defined in TestIface, Implemented in BaseObject, Overridden in DerivedObject
41 * prop4: Defined in BaseObject, Overridden in DerivedObject
44 static GType base_object_get_type ();
45 static GType derived_object_get_type ();
62 * BaseObject, a parent class for DerivedObject
64 #define BASE_TYPE_OBJECT (base_object_get_type ())
65 #define BASE_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BASE_TYPE_OBJECT, BaseObject))
66 typedef struct _BaseObject BaseObject;
67 typedef struct _BaseObjectClass BaseObjectClass;
71 GObject parent_instance;
78 struct _BaseObjectClass
80 GObjectClass parent_class;
83 GObjectClass *base_parent_class;
86 * DerivedObject, the child class of DerivedObject
88 #define DERIVED_TYPE_OBJECT (derived_object_get_type ())
89 typedef struct _DerivedObject DerivedObject;
90 typedef struct _DerivedObjectClass DerivedObjectClass;
94 BaseObject parent_instance;
96 struct _DerivedObjectClass
98 BaseObjectClass parent_class;
104 typedef struct _TestIfaceClass TestIfaceClass;
106 struct _TestIfaceClass
108 GTypeInterface base_iface;
111 #define TEST_TYPE_IFACE (test_iface_get_type ())
113 /* The paramspecs installed on our interface
115 static GParamSpec *iface_spec1, *iface_spec2, *iface_spec3;
117 /* The paramspecs inherited by our derived object
119 static GParamSpec *inherited_spec1, *inherited_spec2, *inherited_spec3, *inherited_spec4;
122 test_iface_default_init (TestIfaceClass *iface_vtable)
124 inherited_spec1 = iface_spec1 = g_param_spec_int ("prop1",
130 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
131 g_object_interface_install_property (iface_vtable, iface_spec1);
133 iface_spec2 = g_param_spec_int ("prop2",
140 g_object_interface_install_property (iface_vtable, iface_spec2);
142 inherited_spec3 = iface_spec3 = g_param_spec_int ("prop3",
149 g_object_interface_install_property (iface_vtable, iface_spec3);
152 static DEFINE_IFACE (TestIface, test_iface, NULL, test_iface_default_init)
156 base_object_constructor (GType type,
157 guint n_construct_properties,
158 GObjectConstructParam *construct_properties)
160 /* The constructor is the one place where a GParamSpecOverride is visible
161 * to the outside world, so we do a bunch of checks here
163 GValue value1 = { 0, };
164 GValue value2 = { 0, };
167 g_assert (n_construct_properties == 1);
169 pspec = construct_properties->pspec;
171 /* Check we got the param spec we expected
173 g_assert (G_IS_PARAM_SPEC_OVERRIDE (pspec));
174 g_assert (pspec->param_id == BASE_PROP1);
175 g_assert (strcmp (g_param_spec_get_name (pspec), "prop1") == 0);
176 g_assert (g_param_spec_get_redirect_target (pspec) == iface_spec1);
178 /* Test redirection of the nick and blurb to the redirect target
180 g_assert (strcmp (g_param_spec_get_nick (pspec), "Prop1") == 0);
181 g_assert (strcmp (g_param_spec_get_blurb (pspec), "Property 1") == 0);
183 /* Test forwarding of the various GParamSpec methods to the redirect target
185 g_value_init (&value1, G_TYPE_INT);
186 g_value_init (&value2, G_TYPE_INT);
188 g_param_value_set_default (pspec, &value1);
189 g_assert (g_value_get_int (&value1) == 42);
191 g_value_reset (&value1);
192 g_value_set_int (&value1, 0x10000);
193 g_assert (g_param_value_validate (pspec, &value1));
194 g_assert (g_value_get_int (&value1) == 0xFFFF);
195 g_assert (!g_param_value_validate (pspec, &value1));
197 g_value_reset (&value1);
198 g_value_set_int (&value1, 1);
199 g_value_set_int (&value2, 2);
200 g_assert (g_param_values_cmp (pspec, &value1, &value2) < 0);
201 g_assert (g_param_values_cmp (pspec, &value2, &value1) > 0);
203 g_value_unset (&value1);
204 g_value_unset (&value2);
206 return base_parent_class->constructor (type,
207 n_construct_properties,
208 construct_properties);
212 base_object_set_property (GObject *object,
217 BaseObject *base_object = BASE_OBJECT (object);
222 g_assert (pspec == inherited_spec1);
223 base_object->val1 = g_value_get_int (value);
226 g_assert (pspec == inherited_spec2);
227 base_object->val2 = g_value_get_int (value);
230 g_assert_not_reached ();
233 g_assert_not_reached ();
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 base_object_get_property (GObject *object,
247 BaseObject *base_object = BASE_OBJECT (object);
252 g_assert (pspec == inherited_spec1);
253 g_value_set_int (value, base_object->val1);
256 g_assert (pspec == inherited_spec2);
257 g_value_set_int (value, base_object->val2);
260 g_assert_not_reached ();
263 g_assert_not_reached ();
266 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
272 base_object_notify (GObject *object,
275 /* The property passed to notify is the redirect target, not the
278 g_assert (pspec == inherited_spec1 ||
279 pspec == inherited_spec2 ||
280 pspec == inherited_spec3 ||
281 pspec == inherited_spec4);
285 base_object_class_init (BaseObjectClass *class)
287 GObjectClass *object_class = G_OBJECT_CLASS (class);
289 base_parent_class= g_type_class_peek_parent (class);
291 object_class->constructor = base_object_constructor;
292 object_class->set_property = base_object_set_property;
293 object_class->get_property = base_object_get_property;
294 object_class->notify = base_object_notify;
296 g_object_class_override_property (object_class, BASE_PROP1, "prop1");
298 /* We override this one using a real property, not GParamSpecOverride
299 * We change the flags from READONLY to READWRITE to show that we
300 * can make the flags less restrictive
302 inherited_spec2 = g_param_spec_int ("prop2",
309 g_object_class_install_property (object_class, BASE_PROP2, inherited_spec2);
311 g_object_class_override_property (object_class, BASE_PROP3, "prop3");
313 inherited_spec4 = g_param_spec_int ("prop4",
320 g_object_class_install_property (object_class, BASE_PROP4, inherited_spec4);
324 base_object_init (BaseObject *base_object)
326 base_object->val1 = 42;
329 static DEFINE_TYPE_FULL (BaseObject, base_object,
330 base_object_class_init, NULL, base_object_init,
332 INTERFACE (NULL, TEST_TYPE_IFACE))
335 derived_object_set_property (GObject *object,
340 BaseObject *base_object = BASE_OBJECT (object);
345 g_assert (pspec == inherited_spec3);
346 base_object->val3 = g_value_get_int (value);
349 g_assert (pspec == inherited_spec4);
350 base_object->val4 = g_value_get_int (value);
353 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
359 derived_object_get_property (GObject *object,
364 BaseObject *base_object = BASE_OBJECT (object);
369 g_assert (pspec == inherited_spec3);
370 g_value_set_int (value, base_object->val3);
373 g_assert (pspec == inherited_spec4);
374 g_value_set_int (value, base_object->val4);
377 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
383 derived_object_class_init (DerivedObjectClass *class)
385 GObjectClass *object_class = G_OBJECT_CLASS (class);
387 object_class->set_property = derived_object_set_property;
388 object_class->get_property = derived_object_get_property;
390 /* Overriding a property that is itself overridding an interface property */
391 g_object_class_override_property (object_class, DERIVED_PROP3, "prop3");
393 /* Overriding a property not from an interface */
394 g_object_class_override_property (object_class, DERIVED_PROP4, "prop4");
397 static DEFINE_TYPE (DerivedObject, derived_object,
398 derived_object_class_init, NULL, NULL,
401 /* Helper function for testing ...list_properties()
404 assert_in_properties (GParamSpec *param_spec,
405 GParamSpec **properties,
409 gboolean found = FALSE;
411 for (i = 0; i < n_properties; i++)
413 if (properties[i] == param_spec)
425 GObjectClass *object_class;
426 TestIfaceClass *iface_vtable;
427 GParamSpec **properties;
430 gint val1, val2, val3, val4;
432 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
433 G_LOG_LEVEL_WARNING |
434 G_LOG_LEVEL_CRITICAL);
437 object = g_object_new (DERIVED_TYPE_OBJECT, NULL);
439 /* Test setting and getting the properties
441 g_object_set (object,
447 g_object_get (object,
454 g_assert (val1 == 0x0101);
455 g_assert (val2 == 0x0202);
456 g_assert (val3 == 0x0303);
457 g_assert (val4 == 0x0404);
459 /* Test that the right spec is passed on explicit notifications
461 g_object_freeze_notify (G_OBJECT (object));
462 g_object_notify (G_OBJECT (object), "prop1");
463 g_object_notify (G_OBJECT (object), "prop2");
464 g_object_notify (G_OBJECT (object), "prop3");
465 g_object_notify (G_OBJECT (object), "prop4");
466 g_object_thaw_notify (G_OBJECT (object));
468 /* Test g_object_class_find_property() for overridden properties
470 object_class = G_OBJECT_GET_CLASS (object);
472 g_assert (g_object_class_find_property (object_class, "prop1") == inherited_spec1);
473 g_assert (g_object_class_find_property (object_class, "prop2") == inherited_spec2);
474 g_assert (g_object_class_find_property (object_class, "prop3") == inherited_spec3);
475 g_assert (g_object_class_find_property (object_class, "prop4") == inherited_spec4);
477 /* Test g_object_class_list_properties() for overridden properties
479 properties = g_object_class_list_properties (object_class, &n_properties);
480 g_assert (n_properties == 4);
481 assert_in_properties (inherited_spec1, properties, n_properties);
482 assert_in_properties (inherited_spec2, properties, n_properties);
483 assert_in_properties (inherited_spec3, properties, n_properties);
484 assert_in_properties (inherited_spec4, properties, n_properties);
487 /* Test g_object_interface_find_property()
489 iface_vtable = g_type_default_interface_peek (TEST_TYPE_IFACE);
491 g_assert (g_object_interface_find_property (iface_vtable, "prop1") == iface_spec1);
492 g_assert (g_object_interface_find_property (iface_vtable, "prop2") == iface_spec2);
493 g_assert (g_object_interface_find_property (iface_vtable, "prop3") == iface_spec3);
495 /* Test g_object_interface_list_properties()
497 properties = g_object_interface_list_properties (iface_vtable, &n_properties);
498 g_assert (n_properties == 3);
499 assert_in_properties (iface_spec1, properties, n_properties);
500 assert_in_properties (iface_spec2, properties, n_properties);
501 assert_in_properties (iface_spec3, properties, n_properties);
504 g_object_unref (object);