summaryrefslogtreecommitdiff
path: root/gobject/tests/binding.c
diff options
context:
space:
mode:
Diffstat (limited to 'gobject/tests/binding.c')
-rw-r--r--gobject/tests/binding.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/gobject/tests/binding.c b/gobject/tests/binding.c
index e088ca7a4..eeba37ed9 100644
--- a/gobject/tests/binding.c
+++ b/gobject/tests/binding.c
@@ -625,6 +625,81 @@ binding_unbind (void)
g_object_unref (source);
}
+/* When source or target die, so does the binding if there is no other ref */
+static void
+binding_unbind_weak (void)
+{
+ GBinding *binding;
+ BindingSource *source;
+ BindingTarget *target;
+
+ /* first source, then target */
+ source = g_object_new (binding_source_get_type (), NULL);
+ target = g_object_new (binding_target_get_type (), NULL);
+ binding = g_object_bind_property (source, "foo",
+ target, "bar",
+ G_BINDING_DEFAULT);
+ g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
+ g_assert (binding != NULL);
+ g_object_unref (source);
+ g_assert (binding == NULL);
+ g_object_unref (target);
+ g_assert (binding == NULL);
+
+ /* first target, then source */
+ source = g_object_new (binding_source_get_type (), NULL);
+ target = g_object_new (binding_target_get_type (), NULL);
+ binding = g_object_bind_property (source, "foo",
+ target, "bar",
+ G_BINDING_DEFAULT);
+ g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
+ g_assert (binding != NULL);
+ g_object_unref (target);
+ g_assert (binding == NULL);
+ g_object_unref (source);
+ g_assert (binding == NULL);
+
+ /* target and source are the same */
+ source = g_object_new (binding_source_get_type (), NULL);
+ binding = g_object_bind_property (source, "foo",
+ source, "bar",
+ G_BINDING_DEFAULT);
+ g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
+ g_assert (binding != NULL);
+ g_object_unref (source);
+ g_assert (binding == NULL);
+}
+
+/* Test that every call to unbind() after the first is a noop */
+static void
+binding_unbind_multiple (void)
+{
+ BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+ BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
+ GBinding *binding;
+ int i;
+
+ binding = g_object_bind_property (source, "foo",
+ target, "bar",
+ G_BINDING_DEFAULT);
+ g_object_ref (binding);
+ g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
+ g_assert (binding != NULL);
+
+ /* this shouldn't crash */
+ for (i = 0; i < 50; i++)
+ {
+ g_binding_unbind (binding);
+ g_assert (binding != NULL);
+ }
+
+ g_object_unref (binding);
+ g_assert (binding == NULL);
+
+ g_object_unref (source);
+ g_object_unref (target);
+}
+
static void
binding_fail (void)
{
@@ -665,6 +740,8 @@ main (int argc, char *argv[])
g_test_add_func ("/binding/invert-boolean", binding_invert_boolean);
g_test_add_func ("/binding/same-object", binding_same_object);
g_test_add_func ("/binding/unbind", binding_unbind);
+ g_test_add_func ("/binding/unbind-weak", binding_unbind_weak);
+ g_test_add_func ("/binding/unbind-multiple", binding_unbind_multiple);
g_test_add_func ("/binding/fail", binding_fail);
return g_test_run ();