3 #include <glib-object.h>
5 typedef struct _TestObject
{
6 GObject parent_instance
;
13 typedef struct _TestObjectClass
{
14 GObjectClass parent_class
;
17 enum { PROP_0
, PROP_FOO
, PROP_BAR
, PROP_BAZ
, PROP_QUUX
, N_PROPERTIES
};
19 static GParamSpec
*properties
[N_PROPERTIES
] = { NULL
, };
21 static GType
test_object_get_type (void);
22 G_DEFINE_TYPE (TestObject
, test_object
, G_TYPE_OBJECT
);
25 test_object_set_foo (TestObject
*obj
,
32 g_assert (properties
[PROP_FOO
] != NULL
);
33 g_object_notify_by_pspec (G_OBJECT (obj
), properties
[PROP_FOO
]);
38 test_object_set_bar (TestObject
*obj
,
47 g_assert (properties
[PROP_BAR
] != NULL
);
48 g_object_notify_by_pspec (G_OBJECT (obj
), properties
[PROP_BAR
]);
53 test_object_set_baz (TestObject
*obj
,
56 if (g_strcmp0 (obj
->baz
, baz
) != 0)
59 obj
->baz
= g_strdup (baz
);
61 g_assert (properties
[PROP_BAZ
] != NULL
);
62 g_object_notify_by_pspec (G_OBJECT (obj
), properties
[PROP_BAZ
]);
67 test_object_set_quux (TestObject
*obj
,
70 if (g_strcmp0 (obj
->quux
, quux
) != 0)
73 obj
->quux
= g_strdup (quux
);
75 g_assert (properties
[PROP_QUUX
] != NULL
);
76 g_object_notify_by_pspec (G_OBJECT (obj
), properties
[PROP_QUUX
]);
81 test_object_finalize (GObject
*gobject
)
83 g_free (((TestObject
*) gobject
)->baz
);
85 /* When the ref_count of an object is zero it is still
86 * possible to notify the property, but it should do
87 * nothing and silenty quit (bug #705570)
89 g_object_notify (gobject
, "foo");
90 g_object_notify_by_pspec (gobject
, properties
[PROP_BAR
]);
92 G_OBJECT_CLASS (test_object_parent_class
)->finalize (gobject
);
96 test_object_set_property (GObject
*gobject
,
101 TestObject
*tobj
= (TestObject
*) gobject
;
103 g_assert_cmpint (prop_id
, !=, 0);
104 g_assert_cmpint (prop_id
, !=, N_PROPERTIES
);
105 g_assert (pspec
== properties
[prop_id
]);
110 test_object_set_foo (tobj
, g_value_get_int (value
));
114 test_object_set_bar (tobj
, g_value_get_boolean (value
));
118 test_object_set_baz (tobj
, g_value_get_string (value
));
122 test_object_set_quux (tobj
, g_value_get_string (value
));
126 g_assert_not_reached ();
131 test_object_get_property (GObject
*gobject
,
136 TestObject
*tobj
= (TestObject
*) gobject
;
138 g_assert_cmpint (prop_id
, !=, 0);
139 g_assert_cmpint (prop_id
, !=, N_PROPERTIES
);
140 g_assert (pspec
== properties
[prop_id
]);
145 g_value_set_int (value
, tobj
->foo
);
149 g_value_set_boolean (value
, tobj
->bar
);
153 g_value_set_string (value
, tobj
->baz
);
157 g_value_set_string (value
, tobj
->quux
);
161 g_assert_not_reached ();
166 test_object_class_init (TestObjectClass
*klass
)
168 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
170 properties
[PROP_FOO
] = g_param_spec_int ("foo", "Foo", "Foo",
174 properties
[PROP_BAR
] = g_param_spec_boolean ("bar", "Bar", "Bar",
177 properties
[PROP_BAZ
] = g_param_spec_string ("baz", "Baz", "Baz",
180 properties
[PROP_QUUX
] = g_param_spec_string ("quux", "quux", "quux",
182 G_PARAM_READWRITE
| G_PARAM_EXPLICIT_NOTIFY
);
184 gobject_class
->set_property
= test_object_set_property
;
185 gobject_class
->get_property
= test_object_get_property
;
186 gobject_class
->finalize
= test_object_finalize
;
188 g_object_class_install_properties (gobject_class
, N_PROPERTIES
, properties
);
192 test_object_init (TestObject
*self
)
196 self
->baz
= g_strdup ("Hello");
201 properties_install (void)
203 TestObject
*obj
= g_object_new (test_object_get_type (), NULL
);
206 g_assert (properties
[PROP_FOO
] != NULL
);
208 pspec
= g_object_class_find_property (G_OBJECT_GET_CLASS (obj
), "foo");
209 g_assert (properties
[PROP_FOO
] == pspec
);
211 g_object_unref (obj
);
221 on_notify (GObject
*gobject
,
223 TestNotifyClosure
*clos
)
225 g_assert (clos
->pspec
== pspec
);
226 g_assert_cmpstr (clos
->name
, ==, pspec
->name
);
231 properties_notify (void)
233 TestObject
*obj
= g_object_new (test_object_get_type (), NULL
);
234 TestNotifyClosure clos
;
236 g_assert (properties
[PROP_FOO
] != NULL
);
237 g_assert (properties
[PROP_QUUX
] != NULL
);
238 g_signal_connect (obj
, "notify", G_CALLBACK (on_notify
), &clos
);
241 clos
.pspec
= properties
[PROP_FOO
];
244 g_object_set (obj
, "foo", 47, NULL
);
245 g_assert (clos
.fired
);
248 clos
.pspec
= properties
[PROP_BAZ
];
251 g_object_set (obj
, "baz", "something new", NULL
);
252 g_assert (clos
.fired
);
254 /* baz lacks explicit notify, so we will see this twice */
256 g_object_set (obj
, "baz", "something new", NULL
);
257 g_assert (clos
.fired
);
259 /* quux on the other hand, ... */
261 clos
.pspec
= properties
[PROP_QUUX
];
264 g_object_set (obj
, "quux", "something new", NULL
);
265 g_assert (clos
.fired
);
267 /* no change; no notify */
269 g_object_set (obj
, "quux", "something new", NULL
);
270 g_assert (!clos
.fired
);
273 g_object_unref (obj
);
277 GParamSpec
*pspec
[3];
282 on_notify2 (GObject
*gobject
,
286 g_assert (n
->pspec
[n
->pos
] == pspec
);
291 properties_notify_queue (void)
293 TestObject
*obj
= g_object_new (test_object_get_type (), NULL
);
296 g_assert (properties
[PROP_FOO
] != NULL
);
298 n
.pspec
[0] = properties
[PROP_BAZ
];
299 n
.pspec
[1] = properties
[PROP_BAR
];
300 n
.pspec
[2] = properties
[PROP_FOO
];
303 g_signal_connect (obj
, "notify", G_CALLBACK (on_notify2
), &n
);
305 g_object_freeze_notify (G_OBJECT (obj
));
306 g_object_set (obj
, "foo", 47, NULL
);
307 g_object_set (obj
, "bar", TRUE
, "foo", 42, "baz", "abc", NULL
);
308 g_object_thaw_notify (G_OBJECT (obj
));
309 g_assert (n
.pos
== 3);
311 g_object_unref (obj
);
315 properties_construct (void)
322 g_test_bug ("630357");
324 /* more than 16 args triggers a realloc in g_object_new_valist() */
325 obj
= g_object_new (test_object_get_type (),
348 g_object_get (obj
, "foo", &val
, NULL
);
349 g_assert (val
== 18);
350 g_object_get (obj
, "bar", &b
, NULL
);
352 g_object_get (obj
, "baz", &s
, NULL
);
353 g_assert_cmpstr (s
, ==, "boo");
356 g_object_unref (obj
);
360 main (int argc
, char *argv
[])
362 g_test_init (&argc
, &argv
, NULL
);
364 g_test_bug_base ("http://bugzilla.gnome.org/");
366 g_test_add_func ("/properties/install", properties_install
);
367 g_test_add_func ("/properties/notify", properties_notify
);
368 g_test_add_func ("/properties/notify-queue", properties_notify_queue
);
369 g_test_add_func ("/properties/construct", properties_construct
);
371 return g_test_run ();