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 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT);
23 test_object_set_foo (TestObject *obj,
30 g_assert (properties[PROP_FOO] != NULL);
31 g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_FOO]);
36 test_object_set_bar (TestObject *obj,
45 g_assert (properties[PROP_BAR] != NULL);
46 g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAR]);
51 test_object_set_baz (TestObject *obj,
54 if (g_strcmp0 (obj->baz, baz) != 0)
57 obj->baz = g_strdup (baz);
59 g_assert (properties[PROP_BAZ] != NULL);
60 g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAZ]);
65 test_object_finalize (GObject *gobject)
67 g_free (((TestObject *) gobject)->baz);
69 G_OBJECT_CLASS (test_object_parent_class)->finalize (gobject);
73 test_object_set_property (GObject *gobject,
78 TestObject *tobj = (TestObject *) gobject;
80 g_assert_cmpint (prop_id, !=, 0);
81 g_assert_cmpint (prop_id, !=, N_PROPERTIES);
82 g_assert (pspec == properties[prop_id]);
87 test_object_set_foo (tobj, g_value_get_int (value));
91 test_object_set_bar (tobj, g_value_get_boolean (value));
95 test_object_set_baz (tobj, g_value_get_string (value));
99 g_assert_not_reached ();
104 test_object_get_property (GObject *gobject,
109 TestObject *tobj = (TestObject *) gobject;
111 g_assert_cmpint (prop_id, !=, 0);
112 g_assert_cmpint (prop_id, !=, N_PROPERTIES);
113 g_assert (pspec == properties[prop_id]);
118 g_value_set_int (value, tobj->foo);
122 g_value_set_boolean (value, tobj->bar);
126 g_value_set_string (value, tobj->baz);
130 g_assert_not_reached ();
135 test_object_class_init (TestObjectClass *klass)
137 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
139 properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "Foo",
143 properties[PROP_BAR] = g_param_spec_boolean ("bar", "Bar", "Bar",
146 properties[PROP_BAZ] = g_param_spec_string ("baz", "Baz", "Baz",
150 gobject_class->set_property = test_object_set_property;
151 gobject_class->get_property = test_object_get_property;
152 gobject_class->finalize = test_object_finalize;
154 g_object_class_install_properties (gobject_class, N_PROPERTIES, properties);
158 test_object_init (TestObject *self)
162 self->baz = g_strdup ("Hello");
166 properties_install (void)
168 TestObject *obj = g_object_new (test_object_get_type (), NULL);
171 g_assert (properties[PROP_FOO] != NULL);
173 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "foo");
174 g_assert (properties[PROP_FOO] == pspec);
176 g_object_unref (obj);
185 on_notify (GObject *gobject,
187 TestNotifyClosure *clos)
189 g_assert (clos->pspec == pspec);
190 g_assert_cmpstr (clos->name, ==, pspec->name);
194 properties_notify (void)
196 TestObject *obj = g_object_new (test_object_get_type (), NULL);
197 TestNotifyClosure clos;
199 g_assert (properties[PROP_FOO] != NULL);
202 clos.pspec = properties[PROP_FOO];
204 g_signal_connect (obj, "notify", G_CALLBACK (on_notify), &clos);
205 g_object_set (obj, "foo", 47, NULL);
207 g_object_unref (obj);
211 properties_construct (void)
216 g_test_bug ("630357");
218 /* more than 16 args triggers a realloc in g_object_new_valist() */
219 obj = g_object_new (test_object_get_type (),
240 g_object_get (obj, "foo", &val, NULL);
241 g_assert (val == 18);
243 g_object_unref (obj);
247 main (int argc, char *argv[])
250 g_test_init (&argc, &argv, NULL);
252 g_test_bug_base ("http://bugzilla.gnome.org/");
254 g_test_add_func ("/properties/install", properties_install);
255 g_test_add_func ("/properties/notify", properties_notify);
256 g_test_add_func ("/properties/construct", properties_construct);
258 return g_test_run ();