1d3ead481f353c1da98400b7012851f26fcb7617
[platform/upstream/glib.git] / gobject / tests / binding.c
1 #include <stdlib.h>
2 #include <gstdio.h>
3 #include <glib-object.h>
4
5 typedef struct _BindingSource
6 {
7   GObject parent_instance;
8
9   gint foo;
10   gint bar;
11   gdouble double_value;
12   gboolean toggle;
13 } BindingSource;
14
15 typedef struct _BindingSourceClass
16 {
17   GObjectClass parent_class;
18 } BindingSourceClass;
19
20 enum
21 {
22   PROP_SOURCE_0,
23
24   PROP_SOURCE_FOO,
25   PROP_SOURCE_BAR,
26   PROP_SOURCE_DOUBLE_VALUE,
27   PROP_SOURCE_TOGGLE
28 };
29
30 static GType binding_source_get_type (void);
31 G_DEFINE_TYPE (BindingSource, binding_source, G_TYPE_OBJECT)
32
33 static void
34 binding_source_set_property (GObject      *gobject,
35                              guint         prop_id,
36                              const GValue *value,
37                              GParamSpec   *pspec)
38 {
39   BindingSource *source = (BindingSource *) gobject;
40
41   switch (prop_id)
42     {
43     case PROP_SOURCE_FOO:
44       source->foo = g_value_get_int (value);
45       break;
46
47     case PROP_SOURCE_BAR:
48       source->bar = g_value_get_int (value);
49       break;
50
51     case PROP_SOURCE_DOUBLE_VALUE:
52       source->double_value = g_value_get_double (value);
53       break;
54
55     case PROP_SOURCE_TOGGLE:
56       source->toggle = g_value_get_boolean (value);
57       break;
58
59     default:
60       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
61     }
62 }
63
64 static void
65 binding_source_get_property (GObject    *gobject,
66                              guint       prop_id,
67                              GValue     *value,
68                              GParamSpec *pspec)
69 {
70   BindingSource *source = (BindingSource *) gobject;
71
72   switch (prop_id)
73     {
74     case PROP_SOURCE_FOO:
75       g_value_set_int (value, source->foo);
76       break;
77
78     case PROP_SOURCE_BAR:
79       g_value_set_int (value, source->bar);
80       break;
81
82     case PROP_SOURCE_DOUBLE_VALUE:
83       g_value_set_double (value, source->double_value);
84       break;
85
86     case PROP_SOURCE_TOGGLE:
87       g_value_set_boolean (value, source->toggle);
88       break;
89
90     default:
91       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
92     }
93 }
94
95 static void
96 binding_source_class_init (BindingSourceClass *klass)
97 {
98   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
99
100   gobject_class->set_property = binding_source_set_property;
101   gobject_class->get_property = binding_source_get_property;
102
103   g_object_class_install_property (gobject_class, PROP_SOURCE_FOO,
104                                    g_param_spec_int ("foo", "Foo", "Foo",
105                                                      -1, 100,
106                                                      0,
107                                                      G_PARAM_READWRITE));
108   g_object_class_install_property (gobject_class, PROP_SOURCE_BAR,
109                                    g_param_spec_int ("bar", "Bar", "Bar",
110                                                      -1, 100,
111                                                      0,
112                                                      G_PARAM_READWRITE));
113   g_object_class_install_property (gobject_class, PROP_SOURCE_DOUBLE_VALUE,
114                                    g_param_spec_double ("double-value", "Value", "Value",
115                                                         -100.0, 200.0,
116                                                         0.0,
117                                                         G_PARAM_READWRITE));
118   g_object_class_install_property (gobject_class, PROP_SOURCE_TOGGLE,
119                                    g_param_spec_boolean ("toggle", "Toggle", "Toggle",
120                                                          FALSE,
121                                                          G_PARAM_READWRITE));
122 }
123
124 static void
125 binding_source_init (BindingSource *self)
126 {
127 }
128
129 typedef struct _BindingTarget
130 {
131   GObject parent_instance;
132
133   gint bar;
134   gdouble double_value;
135   gboolean toggle;
136 } BindingTarget;
137
138 typedef struct _BindingTargetClass
139 {
140   GObjectClass parent_class;
141 } BindingTargetClass;
142
143 enum
144 {
145   PROP_TARGET_0,
146
147   PROP_TARGET_BAR,
148   PROP_TARGET_DOUBLE_VALUE,
149   PROP_TARGET_TOGGLE
150 };
151
152 static GType binding_target_get_type (void);
153 G_DEFINE_TYPE (BindingTarget, binding_target, G_TYPE_OBJECT)
154
155 static void
156 binding_target_set_property (GObject      *gobject,
157                              guint         prop_id,
158                              const GValue *value,
159                              GParamSpec   *pspec)
160 {
161   BindingTarget *target = (BindingTarget *) gobject;
162
163   switch (prop_id)
164     {
165     case PROP_TARGET_BAR:
166       target->bar = g_value_get_int (value);
167       break;
168
169     case PROP_TARGET_DOUBLE_VALUE:
170       target->double_value = g_value_get_double (value);
171       break;
172
173     case PROP_TARGET_TOGGLE:
174       target->toggle = g_value_get_boolean (value);
175       break;
176
177     default:
178       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
179     }
180 }
181
182 static void
183 binding_target_get_property (GObject    *gobject,
184                              guint       prop_id,
185                              GValue     *value,
186                              GParamSpec *pspec)
187 {
188   BindingTarget *target = (BindingTarget *) gobject;
189
190   switch (prop_id)
191     {
192     case PROP_TARGET_BAR:
193       g_value_set_int (value, target->bar);
194       break;
195
196     case PROP_TARGET_DOUBLE_VALUE:
197       g_value_set_double (value, target->double_value);
198       break;
199
200     case PROP_TARGET_TOGGLE:
201       g_value_set_boolean (value, target->toggle);
202       break;
203
204     default:
205       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
206     }
207 }
208
209 static void
210 binding_target_class_init (BindingTargetClass *klass)
211 {
212   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
213
214   gobject_class->set_property = binding_target_set_property;
215   gobject_class->get_property = binding_target_get_property;
216
217   g_object_class_install_property (gobject_class, PROP_TARGET_BAR,
218                                    g_param_spec_int ("bar", "Bar", "Bar",
219                                                      -1, 100,
220                                                      0,
221                                                      G_PARAM_READWRITE));
222   g_object_class_install_property (gobject_class, PROP_TARGET_DOUBLE_VALUE,
223                                    g_param_spec_double ("double-value", "Value", "Value",
224                                                         -100.0, 200.0,
225                                                         0.0,
226                                                         G_PARAM_READWRITE));
227   g_object_class_install_property (gobject_class, PROP_TARGET_TOGGLE,
228                                    g_param_spec_boolean ("toggle", "Toggle", "Toggle",
229                                                          FALSE,
230                                                          G_PARAM_READWRITE));
231 }
232
233 static void
234 binding_target_init (BindingTarget *self)
235 {
236 }
237
238 static gboolean
239 celsius_to_fahrenheit (GBinding     *binding,
240                        const GValue *from_value,
241                        GValue       *to_value,
242                        gpointer      user_data G_GNUC_UNUSED)
243 {
244   gdouble celsius, fahrenheit;
245
246   g_assert_true (G_VALUE_HOLDS (from_value, G_TYPE_DOUBLE));
247   g_assert_true (G_VALUE_HOLDS (to_value, G_TYPE_DOUBLE));
248
249   celsius = g_value_get_double (from_value);
250   fahrenheit = (9 * celsius / 5) + 32.0;
251
252   if (g_test_verbose ())
253     g_printerr ("Converting %.2fC to %.2fF\n", celsius, fahrenheit);
254
255   g_value_set_double (to_value, fahrenheit);
256
257   return TRUE;
258 }
259
260 static gboolean
261 fahrenheit_to_celsius (GBinding     *binding,
262                        const GValue *from_value,
263                        GValue       *to_value,
264                        gpointer      user_data G_GNUC_UNUSED)
265 {
266   gdouble celsius, fahrenheit;
267
268   g_assert_true (G_VALUE_HOLDS (from_value, G_TYPE_DOUBLE));
269   g_assert_true (G_VALUE_HOLDS (to_value, G_TYPE_DOUBLE));
270
271   fahrenheit = g_value_get_double (from_value);
272   celsius = 5 * (fahrenheit - 32.0) / 9;
273
274   if (g_test_verbose ())
275     g_printerr ("Converting %.2fF to %.2fC\n", fahrenheit, celsius);
276
277   g_value_set_double (to_value, celsius);
278
279   return TRUE;
280 }
281
282 static void
283 binding_default (void)
284 {
285   BindingSource *source = g_object_new (binding_source_get_type (), NULL);
286   BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
287   GBinding *binding;
288
289   binding = g_object_bind_property (source, "foo",
290                                     target, "bar",
291                                     G_BINDING_DEFAULT);
292
293   g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
294   g_assert_true ((BindingSource *) g_binding_get_source (binding) == source);
295   g_assert_true ((BindingTarget *) g_binding_get_target (binding) == target);
296   g_assert_cmpstr (g_binding_get_source_property (binding), ==, "foo");
297   g_assert_cmpstr (g_binding_get_target_property (binding), ==, "bar");
298   g_assert_cmpint (g_binding_get_flags (binding), ==, G_BINDING_DEFAULT);
299
300   g_object_set (source, "foo", 42, NULL);
301   g_assert_cmpint (source->foo, ==, target->bar);
302
303   g_object_set (target, "bar", 47, NULL);
304   g_assert_cmpint (source->foo, !=, target->bar);
305
306   g_object_unref (binding);
307
308   g_object_set (source, "foo", 0, NULL);
309   g_assert_cmpint (source->foo, !=, target->bar);
310
311   g_object_unref (source);
312   g_object_unref (target);
313   g_assert_null (binding);
314 }
315
316 static void
317 binding_canonicalisation (void)
318 {
319   BindingSource *source = g_object_new (binding_source_get_type (), NULL);
320   BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
321   GBinding *binding;
322
323   g_test_summary ("Test that bindings set up with non-canonical property names work");
324
325   binding = g_object_bind_property (source, "double_value",
326                                     target, "double_value",
327                                     G_BINDING_DEFAULT);
328
329   g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
330   g_assert_true ((BindingSource *) g_binding_get_source (binding) == source);
331   g_assert_true ((BindingTarget *) g_binding_get_target (binding) == target);
332   g_assert_cmpstr (g_binding_get_source_property (binding), ==, "double-value");
333   g_assert_cmpstr (g_binding_get_target_property (binding), ==, "double-value");
334   g_assert_cmpint (g_binding_get_flags (binding), ==, G_BINDING_DEFAULT);
335
336   g_object_set (source, "double-value", 24.0, NULL);
337   g_assert_cmpfloat (target->double_value, ==, source->double_value);
338
339   g_object_set (target, "double-value", 69.0, NULL);
340   g_assert_cmpfloat (source->double_value, !=, target->double_value);
341
342   g_object_unref (target);
343   g_object_unref (source);
344   g_assert_null (binding);
345 }
346
347 static void
348 binding_bidirectional (void)
349 {
350   BindingSource *source = g_object_new (binding_source_get_type (), NULL);
351   BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
352   GBinding *binding;
353
354   binding = g_object_bind_property (source, "foo",
355                                     target, "bar",
356                                     G_BINDING_BIDIRECTIONAL);
357   g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
358
359   g_object_set (source, "foo", 42, NULL);
360   g_assert_cmpint (source->foo, ==, target->bar);
361
362   g_object_set (target, "bar", 47, NULL);
363   g_assert_cmpint (source->foo, ==, target->bar);
364
365   g_object_unref (binding);
366
367   g_object_set (source, "foo", 0, NULL);
368   g_assert_cmpint (source->foo, !=, target->bar);
369
370   g_object_unref (source);
371   g_object_unref (target);
372   g_assert_null (binding);
373 }
374
375 static void
376 data_free (gpointer data)
377 {
378   gboolean *b = data;
379
380   *b = TRUE;
381 }
382
383 static void
384 binding_transform_default (void)
385 {
386   BindingSource *source = g_object_new (binding_source_get_type (), NULL);
387   BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
388   GBinding *binding;
389   gpointer src, trg;
390   gchar *src_prop, *trg_prop;
391   GBindingFlags flags;
392
393   binding = g_object_bind_property (source, "foo",
394                                     target, "double-value",
395                                     G_BINDING_BIDIRECTIONAL);
396
397   g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
398
399   g_object_get (binding,
400                 "source", &src,
401                 "source-property", &src_prop,
402                 "target", &trg,
403                 "target-property", &trg_prop,
404                 "flags", &flags,
405                 NULL);
406   g_assert_true (src == source);
407   g_assert_true (trg == target);
408   g_assert_cmpstr (src_prop, ==, "foo");
409   g_assert_cmpstr (trg_prop, ==, "double-value");
410   g_assert_cmpint (flags, ==, G_BINDING_BIDIRECTIONAL);
411   g_object_unref (src);
412   g_object_unref (trg);
413   g_free (src_prop);
414   g_free (trg_prop);
415
416   g_object_set (source, "foo", 24, NULL);
417   g_assert_cmpfloat (target->double_value, ==, 24.0);
418
419   g_object_set (target, "double-value", 69.0, NULL);
420   g_assert_cmpint (source->foo, ==, 69);
421
422   g_object_unref (target);
423   g_object_unref (source);
424   g_assert_null (binding);
425 }
426
427 static void
428 binding_transform (void)
429 {
430   BindingSource *source = g_object_new (binding_source_get_type (), NULL);
431   BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
432   GBinding *binding G_GNUC_UNUSED;
433   gboolean unused_data = FALSE;
434
435   binding = g_object_bind_property_full (source, "double-value",
436                                          target, "double-value",
437                                          G_BINDING_BIDIRECTIONAL,
438                                          celsius_to_fahrenheit,
439                                          fahrenheit_to_celsius,
440                                          &unused_data, data_free);
441
442   g_object_set (source, "double-value", 24.0, NULL);
443   g_assert_cmpfloat (target->double_value, ==, ((9 * 24.0 / 5) + 32.0));
444
445   g_object_set (target, "double-value", 69.0, NULL);
446   g_assert_cmpfloat (source->double_value, ==, (5 * (69.0 - 32.0) / 9));
447
448   g_object_unref (source);
449   g_object_unref (target);
450
451   g_assert_true (unused_data);
452 }
453
454 static void
455 binding_transform_closure (void)
456 {
457   BindingSource *source = g_object_new (binding_source_get_type (), NULL);
458   BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
459   GBinding *binding G_GNUC_UNUSED;
460   gboolean unused_data_1 = FALSE, unused_data_2 = FALSE;
461   GClosure *c2f_clos, *f2c_clos;
462
463   c2f_clos = g_cclosure_new (G_CALLBACK (celsius_to_fahrenheit), &unused_data_1, (GClosureNotify) data_free);
464
465   f2c_clos = g_cclosure_new (G_CALLBACK (fahrenheit_to_celsius), &unused_data_2, (GClosureNotify) data_free);
466
467   binding = g_object_bind_property_with_closures (source, "double-value",
468                                                   target, "double-value",
469                                                   G_BINDING_BIDIRECTIONAL,
470                                                   c2f_clos,
471                                                   f2c_clos);
472
473   g_object_set (source, "double-value", 24.0, NULL);
474   g_assert_cmpfloat (target->double_value, ==, ((9 * 24.0 / 5) + 32.0));
475
476   g_object_set (target, "double-value", 69.0, NULL);
477   g_assert_cmpfloat (source->double_value, ==, (5 * (69.0 - 32.0) / 9));
478
479   g_object_unref (source);
480   g_object_unref (target);
481
482   g_assert_true (unused_data_1);
483   g_assert_true (unused_data_2);
484 }
485
486 static void
487 binding_chain (void)
488 {
489   BindingSource *a = g_object_new (binding_source_get_type (), NULL);
490   BindingSource *b = g_object_new (binding_source_get_type (), NULL);
491   BindingSource *c = g_object_new (binding_source_get_type (), NULL);
492   GBinding *binding_1, *binding_2;
493
494   g_test_bug_base ("http://bugzilla.gnome.org/");
495   g_test_bug ("621782");
496
497   /* A -> B, B -> C */
498   binding_1 = g_object_bind_property (a, "foo", b, "foo", G_BINDING_BIDIRECTIONAL);
499   g_object_add_weak_pointer (G_OBJECT (binding_1), (gpointer *) &binding_1);
500
501   binding_2 = g_object_bind_property (b, "foo", c, "foo", G_BINDING_BIDIRECTIONAL);
502   g_object_add_weak_pointer (G_OBJECT (binding_2), (gpointer *) &binding_2);
503
504   /* verify the chain */
505   g_object_set (a, "foo", 42, NULL);
506   g_assert_cmpint (a->foo, ==, b->foo);
507   g_assert_cmpint (b->foo, ==, c->foo);
508
509   /* unbind A -> B and B -> C */
510   g_object_unref (binding_1);
511   g_assert_null (binding_1);
512   g_object_unref (binding_2);
513   g_assert_null (binding_2);
514
515   /* bind A -> C directly */
516   binding_2 = g_object_bind_property (a, "foo", c, "foo", G_BINDING_BIDIRECTIONAL);
517
518   /* verify the chain is broken */
519   g_object_set (a, "foo", 47, NULL);
520   g_assert_cmpint (a->foo, !=, b->foo);
521   g_assert_cmpint (a->foo, ==, c->foo);
522
523   g_object_unref (a);
524   g_object_unref (b);
525   g_object_unref (c);
526 }
527
528 static void
529 binding_sync_create (void)
530 {
531   BindingSource *source = g_object_new (binding_source_get_type (),
532                                         "foo", 42,
533                                         NULL);
534   BindingTarget *target = g_object_new (binding_target_get_type (),
535                                         "bar", 47,
536                                         NULL);
537   GBinding *binding;
538
539   binding = g_object_bind_property (source, "foo",
540                                     target, "bar",
541                                     G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
542
543   g_assert_cmpint (source->foo, ==, 42);
544   g_assert_cmpint (target->bar, ==, 42);
545
546   g_object_set (source, "foo", 47, NULL);
547   g_assert_cmpint (source->foo, ==, target->bar);
548
549   g_object_unref (binding);
550
551   g_object_set (target, "bar", 49, NULL);
552
553   binding = g_object_bind_property (source, "foo",
554                                     target, "bar",
555                                     G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
556   g_assert_cmpint (source->foo, ==, 47);
557   g_assert_cmpint (target->bar, ==, 47);
558
559   g_object_unref (source);
560   g_object_unref (target);
561 }
562
563 static void
564 binding_invert_boolean (void)
565 {
566   BindingSource *source = g_object_new (binding_source_get_type (),
567                                         "toggle", TRUE,
568                                         NULL);
569   BindingTarget *target = g_object_new (binding_target_get_type (),
570                                         "toggle", FALSE,
571                                         NULL);
572   GBinding *binding;
573
574   binding = g_object_bind_property (source, "toggle",
575                                     target, "toggle",
576                                     G_BINDING_BIDIRECTIONAL | G_BINDING_INVERT_BOOLEAN);
577
578   g_assert_true (source->toggle);
579   g_assert_false (target->toggle);
580
581   g_object_set (source, "toggle", FALSE, NULL);
582   g_assert_false (source->toggle);
583   g_assert_true (target->toggle);
584
585   g_object_set (target, "toggle", FALSE, NULL);
586   g_assert_true (source->toggle);
587   g_assert_false (target->toggle);
588
589   g_object_unref (binding);
590   g_object_unref (source);
591   g_object_unref (target);
592 }
593
594 static void
595 binding_same_object (void)
596 {
597   BindingSource *source = g_object_new (binding_source_get_type (),
598                                         "foo", 100,
599                                         "bar", 50,
600                                         NULL);
601   GBinding *binding;
602
603   binding = g_object_bind_property (source, "foo",
604                                     source, "bar",
605                                     G_BINDING_BIDIRECTIONAL);
606   g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
607
608   g_object_set (source, "foo", 10, NULL);
609   g_assert_cmpint (source->foo, ==, 10);
610   g_assert_cmpint (source->bar, ==, 10);
611   g_object_set (source, "bar", 30, NULL);
612   g_assert_cmpint (source->foo, ==, 30);
613   g_assert_cmpint (source->bar, ==, 30);
614
615   g_object_unref (source);
616   g_assert_null (binding);
617 }
618
619 static void
620 binding_unbind (void)
621 {
622   BindingSource *source = g_object_new (binding_source_get_type (), NULL);
623   BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
624   GBinding *binding;
625
626   binding = g_object_bind_property (source, "foo",
627                                     target, "bar",
628                                     G_BINDING_DEFAULT);
629   g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
630
631   g_object_set (source, "foo", 42, NULL);
632   g_assert_cmpint (source->foo, ==, target->bar);
633
634   g_object_set (target, "bar", 47, NULL);
635   g_assert_cmpint (source->foo, !=, target->bar);
636
637   g_binding_unbind (binding);
638   g_assert_null (binding);
639
640   g_object_set (source, "foo", 0, NULL);
641   g_assert_cmpint (source->foo, !=, target->bar);
642
643   g_object_unref (source);
644   g_object_unref (target);
645
646
647   /* g_binding_unbind() has a special case for this */
648   source = g_object_new (binding_source_get_type (), NULL);
649   binding = g_object_bind_property (source, "foo",
650                                     source, "bar",
651                                     G_BINDING_DEFAULT);
652   g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
653
654   g_binding_unbind (binding);
655   g_assert_null (binding);
656
657   g_object_unref (source);
658 }
659
660 /* When source or target die, so does the binding if there is no other ref */
661 static void
662 binding_unbind_weak (void)
663 {
664   GBinding *binding;
665   BindingSource *source;
666   BindingTarget *target;
667
668   /* first source, then target */
669   source = g_object_new (binding_source_get_type (), NULL);
670   target = g_object_new (binding_target_get_type (), NULL);
671   binding = g_object_bind_property (source, "foo",
672                                     target, "bar",
673                                     G_BINDING_DEFAULT);
674   g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
675   g_assert_nonnull (binding);
676   g_object_unref (source);
677   g_assert_null (binding);
678   g_object_unref (target);
679   g_assert_null (binding);
680
681   /* first target, then source */
682   source = g_object_new (binding_source_get_type (), NULL);
683   target = g_object_new (binding_target_get_type (), NULL);
684   binding = g_object_bind_property (source, "foo",
685                                     target, "bar",
686                                     G_BINDING_DEFAULT);
687   g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
688   g_assert_nonnull (binding);
689   g_object_unref (target);
690   g_assert_null (binding);
691   g_object_unref (source);
692   g_assert_null (binding);
693
694   /* target and source are the same */
695   source = g_object_new (binding_source_get_type (), NULL);
696   binding = g_object_bind_property (source, "foo",
697                                     source, "bar",
698                                     G_BINDING_DEFAULT);
699   g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
700   g_assert_nonnull (binding);
701   g_object_unref (source);
702   g_assert_null (binding);
703 }
704
705 /* Test that every call to unbind() after the first is a noop */
706 static void
707 binding_unbind_multiple (void)
708 {
709   BindingSource *source = g_object_new (binding_source_get_type (), NULL);
710   BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
711   GBinding *binding;
712   guint i;
713
714   g_test_bug ("1373");
715
716   binding = g_object_bind_property (source, "foo",
717                                     target, "bar",
718                                     G_BINDING_DEFAULT);
719   g_object_ref (binding);
720   g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
721   g_assert_nonnull (binding);
722
723   /* this shouldn't crash */
724   for (i = 0; i < 50; i++)
725     {
726       g_binding_unbind (binding);
727       g_assert_nonnull (binding);
728     }
729
730   g_object_unref (binding);
731   g_assert_null (binding);
732
733   g_object_unref (source);
734   g_object_unref (target);
735 }
736
737 static void
738 binding_fail (void)
739 {
740   BindingSource *source = g_object_new (binding_source_get_type (), NULL);
741   BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
742   GBinding *binding;
743
744   /* double -> boolean is not supported */
745   binding = g_object_bind_property (source, "double-value",
746                                     target, "toggle",
747                                     G_BINDING_DEFAULT);
748   g_object_add_weak_pointer (G_OBJECT (binding), (gpointer *) &binding);
749
750   g_test_expect_message ("GLib-GObject", G_LOG_LEVEL_WARNING,
751                          "*Unable to convert*double*boolean*");
752   g_object_set (source, "double-value", 1.0, NULL);
753   g_test_assert_expected_messages ();
754
755   g_object_unref (source);
756   g_object_unref (target);
757   g_assert_null (binding);
758 }
759
760 int
761 main (int argc, char *argv[])
762 {
763   g_test_init (&argc, &argv, NULL);
764
765   g_test_bug_base ("https://gitlab.gnome.org/GNOME/glib/issues/");
766
767   g_test_add_func ("/binding/default", binding_default);
768   g_test_add_func ("/binding/canonicalisation", binding_canonicalisation);
769   g_test_add_func ("/binding/bidirectional", binding_bidirectional);
770   g_test_add_func ("/binding/transform", binding_transform);
771   g_test_add_func ("/binding/transform-default", binding_transform_default);
772   g_test_add_func ("/binding/transform-closure", binding_transform_closure);
773   g_test_add_func ("/binding/chain", binding_chain);
774   g_test_add_func ("/binding/sync-create", binding_sync_create);
775   g_test_add_func ("/binding/invert-boolean", binding_invert_boolean);
776   g_test_add_func ("/binding/same-object", binding_same_object);
777   g_test_add_func ("/binding/unbind", binding_unbind);
778   g_test_add_func ("/binding/unbind-weak", binding_unbind_weak);
779   g_test_add_func ("/binding/unbind-multiple", binding_unbind_multiple);
780   g_test_add_func ("/binding/fail", binding_fail);
781
782   return g_test_run ();
783 }