3 #include <glib-object.h>
5 typedef struct _TestObject {
6 GObject parent_instance;
12 typedef struct _TestObjectClass {
13 GObjectClass parent_class;
16 enum { PROP_0, PROP_FOO, PROP_BAR, PROP_BAZ, N_PROPERTIES };
18 static GParamSpec *properties[N_PROPERTIES] = { NULL, };
20 static GType test_object_get_type (void);
21 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT);
24 test_object_set_foo (TestObject *obj,
31 g_assert (properties[PROP_FOO] != NULL);
32 g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_FOO]);
37 test_object_set_bar (TestObject *obj,
46 g_assert (properties[PROP_BAR] != NULL);
47 g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAR]);
52 test_object_set_baz (TestObject *obj,
55 if (g_strcmp0 (obj->baz, baz) != 0)
58 obj->baz = g_strdup (baz);
60 g_assert (properties[PROP_BAZ] != NULL);
61 g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAZ]);
66 test_object_finalize (GObject *gobject)
68 g_free (((TestObject *) gobject)->baz);
70 /* When the ref_count of an object is zero it is still
71 * possible to notify the property, but it should do
72 * nothing and silenty quit (bug #705570)
74 g_object_notify (gobject, "foo");
75 g_object_notify_by_pspec (gobject, properties[PROP_BAR]);
77 G_OBJECT_CLASS (test_object_parent_class)->finalize (gobject);
81 test_object_set_property (GObject *gobject,
86 TestObject *tobj = (TestObject *) gobject;
88 g_assert_cmpint (prop_id, !=, 0);
89 g_assert_cmpint (prop_id, !=, N_PROPERTIES);
90 g_assert (pspec == properties[prop_id]);
95 test_object_set_foo (tobj, g_value_get_int (value));
99 test_object_set_bar (tobj, g_value_get_boolean (value));
103 test_object_set_baz (tobj, g_value_get_string (value));
107 g_assert_not_reached ();
112 test_object_get_property (GObject *gobject,
117 TestObject *tobj = (TestObject *) gobject;
119 g_assert_cmpint (prop_id, !=, 0);
120 g_assert_cmpint (prop_id, !=, N_PROPERTIES);
121 g_assert (pspec == properties[prop_id]);
126 g_value_set_int (value, tobj->foo);
130 g_value_set_boolean (value, tobj->bar);
134 g_value_set_string (value, tobj->baz);
138 g_assert_not_reached ();
143 test_object_class_init (TestObjectClass *klass)
145 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
147 properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "Foo",
151 properties[PROP_BAR] = g_param_spec_boolean ("bar", "Bar", "Bar",
154 properties[PROP_BAZ] = g_param_spec_string ("baz", "Baz", "Baz",
158 gobject_class->set_property = test_object_set_property;
159 gobject_class->get_property = test_object_get_property;
160 gobject_class->finalize = test_object_finalize;
162 g_object_class_install_properties (gobject_class, N_PROPERTIES, properties);
166 test_object_init (TestObject *self)
170 self->baz = g_strdup ("Hello");
174 properties_install (void)
176 TestObject *obj = g_object_new (test_object_get_type (), NULL);
179 g_assert (properties[PROP_FOO] != NULL);
181 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "foo");
182 g_assert (properties[PROP_FOO] == pspec);
184 g_object_unref (obj);
193 on_notify (GObject *gobject,
195 TestNotifyClosure *clos)
197 g_assert (clos->pspec == pspec);
198 g_assert_cmpstr (clos->name, ==, pspec->name);
202 properties_notify (void)
204 TestObject *obj = g_object_new (test_object_get_type (), NULL);
205 TestNotifyClosure clos;
207 g_assert (properties[PROP_FOO] != NULL);
210 clos.pspec = properties[PROP_FOO];
212 g_signal_connect (obj, "notify", G_CALLBACK (on_notify), &clos);
213 g_object_set (obj, "foo", 47, NULL);
215 g_object_unref (obj);
219 properties_construct (void)
226 g_test_bug ("630357");
228 /* more than 16 args triggers a realloc in g_object_new_valist() */
229 obj = g_object_new (test_object_get_type (),
252 g_object_get (obj, "foo", &val, NULL);
253 g_assert (val == 18);
254 g_object_get (obj, "bar", &b, NULL);
256 g_object_get (obj, "baz", &s, NULL);
257 g_assert_cmpstr (s, ==, "boo");
260 g_object_unref (obj);
264 main (int argc, char *argv[])
266 g_test_init (&argc, &argv, NULL);
268 g_test_bug_base ("http://bugzilla.gnome.org/");
270 g_test_add_func ("/properties/install", properties_install);
271 g_test_add_func ("/properties/notify", properties_notify);
272 g_test_add_func ("/properties/construct", properties_construct);
274 return g_test_run ();