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 G_OBJECT_CLASS (test_object_parent_class
)->finalize (gobject
);
74 test_object_set_property (GObject
*gobject
,
79 TestObject
*tobj
= (TestObject
*) gobject
;
81 g_assert_cmpint (prop_id
, !=, 0);
82 g_assert_cmpint (prop_id
, !=, N_PROPERTIES
);
83 g_assert (pspec
== properties
[prop_id
]);
88 test_object_set_foo (tobj
, g_value_get_int (value
));
92 test_object_set_bar (tobj
, g_value_get_boolean (value
));
96 test_object_set_baz (tobj
, g_value_get_string (value
));
100 g_assert_not_reached ();
105 test_object_get_property (GObject
*gobject
,
110 TestObject
*tobj
= (TestObject
*) gobject
;
112 g_assert_cmpint (prop_id
, !=, 0);
113 g_assert_cmpint (prop_id
, !=, N_PROPERTIES
);
114 g_assert (pspec
== properties
[prop_id
]);
119 g_value_set_int (value
, tobj
->foo
);
123 g_value_set_boolean (value
, tobj
->bar
);
127 g_value_set_string (value
, tobj
->baz
);
131 g_assert_not_reached ();
136 test_object_class_init (TestObjectClass
*klass
)
138 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
140 properties
[PROP_FOO
] = g_param_spec_int ("foo", "Foo", "Foo",
144 properties
[PROP_BAR
] = g_param_spec_boolean ("bar", "Bar", "Bar",
147 properties
[PROP_BAZ
] = g_param_spec_string ("baz", "Baz", "Baz",
151 gobject_class
->set_property
= test_object_set_property
;
152 gobject_class
->get_property
= test_object_get_property
;
153 gobject_class
->finalize
= test_object_finalize
;
155 g_object_class_install_properties (gobject_class
, N_PROPERTIES
, properties
);
159 test_object_init (TestObject
*self
)
163 self
->baz
= g_strdup ("Hello");
167 properties_install (void)
169 TestObject
*obj
= g_object_new (test_object_get_type (), NULL
);
172 g_assert (properties
[PROP_FOO
] != NULL
);
174 pspec
= g_object_class_find_property (G_OBJECT_GET_CLASS (obj
), "foo");
175 g_assert (properties
[PROP_FOO
] == pspec
);
177 g_object_unref (obj
);
186 on_notify (GObject
*gobject
,
188 TestNotifyClosure
*clos
)
190 g_assert (clos
->pspec
== pspec
);
191 g_assert_cmpstr (clos
->name
, ==, pspec
->name
);
195 properties_notify (void)
197 TestObject
*obj
= g_object_new (test_object_get_type (), NULL
);
198 TestNotifyClosure clos
;
200 g_assert (properties
[PROP_FOO
] != NULL
);
203 clos
.pspec
= properties
[PROP_FOO
];
205 g_signal_connect (obj
, "notify", G_CALLBACK (on_notify
), &clos
);
206 g_object_set (obj
, "foo", 47, NULL
);
208 g_object_unref (obj
);
212 properties_construct (void)
219 g_test_bug ("630357");
221 /* more than 16 args triggers a realloc in g_object_new_valist() */
222 obj
= g_object_new (test_object_get_type (),
245 g_object_get (obj
, "foo", &val
, NULL
);
246 g_assert (val
== 18);
247 g_object_get (obj
, "bar", &b
, NULL
);
249 g_object_get (obj
, "baz", &s
, NULL
);
250 g_assert_cmpstr (s
, ==, "boo");
253 g_object_unref (obj
);
257 main (int argc
, char *argv
[])
259 g_test_init (&argc
, &argv
, NULL
);
261 g_test_bug_base ("http://bugzilla.gnome.org/");
263 g_test_add_func ("/properties/install", properties_install
);
264 g_test_add_func ("/properties/notify", properties_notify
);
265 g_test_add_func ("/properties/construct", properties_construct
);
267 return g_test_run ();