Merge remote-tracking branch 'gvdb/master'
[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   gdouble value;
11   gboolean toggle;
12 } BindingSource;
13
14 typedef struct _BindingSourceClass
15 {
16   GObjectClass parent_class;
17 } BindingSourceClass;
18
19 enum
20 {
21   PROP_SOURCE_0,
22
23   PROP_SOURCE_FOO,
24   PROP_SOURCE_VALUE,
25   PROP_SOURCE_TOGGLE
26 };
27
28 G_DEFINE_TYPE (BindingSource, binding_source, G_TYPE_OBJECT);
29
30 static void
31 binding_source_set_property (GObject      *gobject,
32                              guint         prop_id,
33                              const GValue *value,
34                              GParamSpec   *pspec)
35 {
36   BindingSource *source = (BindingSource *) gobject;
37
38   switch (prop_id)
39     {
40     case PROP_SOURCE_FOO:
41       source->foo = g_value_get_int (value);
42       break;
43
44     case PROP_SOURCE_VALUE:
45       source->value = g_value_get_double (value);
46       break;
47
48     case PROP_SOURCE_TOGGLE:
49       source->toggle = g_value_get_boolean (value);
50       break;
51
52     default:
53       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
54     }
55 }
56
57 static void
58 binding_source_get_property (GObject    *gobject,
59                              guint       prop_id,
60                              GValue     *value,
61                              GParamSpec *pspec)
62 {
63   BindingSource *source = (BindingSource *) gobject;
64
65   switch (prop_id)
66     {
67     case PROP_SOURCE_FOO:
68       g_value_set_int (value, source->foo);
69       break;
70
71     case PROP_SOURCE_VALUE:
72       g_value_set_double (value, source->value);
73       break;
74
75     case PROP_SOURCE_TOGGLE:
76       g_value_set_boolean (value, source->toggle);
77       break;
78
79     default:
80       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
81     }
82 }
83
84 static void
85 binding_source_class_init (BindingSourceClass *klass)
86 {
87   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
88
89   gobject_class->set_property = binding_source_set_property;
90   gobject_class->get_property = binding_source_get_property;
91
92   g_object_class_install_property (gobject_class, PROP_SOURCE_FOO,
93                                    g_param_spec_int ("foo", "Foo", "Foo",
94                                                      -1, 100,
95                                                      0,
96                                                      G_PARAM_READWRITE));
97   g_object_class_install_property (gobject_class, PROP_SOURCE_VALUE,
98                                    g_param_spec_double ("value", "Value", "Value",
99                                                         -100.0, 200.0,
100                                                         0.0,
101                                                         G_PARAM_READWRITE));
102   g_object_class_install_property (gobject_class, PROP_SOURCE_TOGGLE,
103                                    g_param_spec_boolean ("toggle", "Toggle", "Toggle",
104                                                          FALSE,
105                                                          G_PARAM_READWRITE));
106 }
107
108 static void
109 binding_source_init (BindingSource *self)
110 {
111 }
112
113 typedef struct _BindingTarget
114 {
115   GObject parent_instance;
116
117   gint bar;
118   gdouble value;
119   gboolean toggle;
120 } BindingTarget;
121
122 typedef struct _BindingTargetClass
123 {
124   GObjectClass parent_class;
125 } BindingTargetClass;
126
127 enum
128 {
129   PROP_TARGET_0,
130
131   PROP_TARGET_BAR,
132   PROP_TARGET_VALUE,
133   PROP_TARGET_TOGGLE
134 };
135
136 G_DEFINE_TYPE (BindingTarget, binding_target, G_TYPE_OBJECT);
137
138 static void
139 binding_target_set_property (GObject      *gobject,
140                              guint         prop_id,
141                              const GValue *value,
142                              GParamSpec   *pspec)
143 {
144   BindingTarget *target = (BindingTarget *) gobject;
145
146   switch (prop_id)
147     {
148     case PROP_TARGET_BAR:
149       target->bar = g_value_get_int (value);
150       break;
151
152     case PROP_TARGET_VALUE:
153       target->value = g_value_get_double (value);
154       break;
155
156     case PROP_TARGET_TOGGLE:
157       target->toggle = g_value_get_boolean (value);
158       break;
159
160     default:
161       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
162     }
163 }
164
165 static void
166 binding_target_get_property (GObject    *gobject,
167                              guint       prop_id,
168                              GValue     *value,
169                              GParamSpec *pspec)
170 {
171   BindingTarget *target = (BindingTarget *) gobject;
172
173   switch (prop_id)
174     {
175     case PROP_TARGET_BAR:
176       g_value_set_int (value, target->bar);
177       break;
178
179     case PROP_TARGET_VALUE:
180       g_value_set_double (value, target->value);
181       break;
182
183     case PROP_TARGET_TOGGLE:
184       g_value_set_boolean (value, target->toggle);
185       break;
186
187     default:
188       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
189     }
190 }
191
192 static void
193 binding_target_class_init (BindingTargetClass *klass)
194 {
195   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
196
197   gobject_class->set_property = binding_target_set_property;
198   gobject_class->get_property = binding_target_get_property;
199
200   g_object_class_install_property (gobject_class, PROP_TARGET_BAR,
201                                    g_param_spec_int ("bar", "Bar", "Bar",
202                                                      -1, 100,
203                                                      0,
204                                                      G_PARAM_READWRITE));
205   g_object_class_install_property (gobject_class, PROP_TARGET_VALUE,
206                                    g_param_spec_double ("value", "Value", "Value",
207                                                         -100.0, 200.0,
208                                                         0.0,
209                                                         G_PARAM_READWRITE));
210   g_object_class_install_property (gobject_class, PROP_TARGET_TOGGLE,
211                                    g_param_spec_boolean ("toggle", "Toggle", "Toggle",
212                                                          FALSE,
213                                                          G_PARAM_READWRITE));
214 }
215
216 static void
217 binding_target_init (BindingTarget *self)
218 {
219 }
220
221 static gboolean
222 celsius_to_fahrenheit (GBinding     *binding,
223                        const GValue *source_value,
224                        GValue       *target_value,
225                        gpointer      user_data G_GNUC_UNUSED)
226 {
227   gdouble celsius, fahrenheit;
228
229   g_assert (G_VALUE_HOLDS (source_value, G_TYPE_DOUBLE));
230   g_assert (G_VALUE_HOLDS (target_value, G_TYPE_DOUBLE));
231
232   celsius = g_value_get_double (source_value);
233   fahrenheit = (9 * celsius / 5) + 32.0;
234
235   if (g_test_verbose ())
236     g_print ("Converting %.2fC to %.2fF\n", celsius, fahrenheit);
237
238   g_value_set_double (target_value, fahrenheit);
239
240   return TRUE;
241 }
242
243 static gboolean
244 fahrenheit_to_celsius (GBinding     *binding,
245                        const GValue *source_value,
246                        GValue       *target_value,
247                        gpointer      user_data G_GNUC_UNUSED)
248 {
249   gdouble celsius, fahrenheit;
250
251   g_assert (G_VALUE_HOLDS (source_value, G_TYPE_DOUBLE));
252   g_assert (G_VALUE_HOLDS (target_value, G_TYPE_DOUBLE));
253
254   fahrenheit = g_value_get_double (source_value);
255   celsius = 5 * (fahrenheit - 32.0) / 9;
256
257   if (g_test_verbose ())
258     g_print ("Converting %.2fF to %.2fC\n", fahrenheit, celsius);
259
260   g_value_set_double (target_value, celsius);
261
262   return TRUE;
263 }
264
265 static void
266 binding_default (void)
267 {
268   BindingSource *source = g_object_new (binding_source_get_type (), NULL);
269   BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
270   GBinding *binding;
271
272   binding = g_object_bind_property (source, "foo",
273                                     target, "bar",
274                                     G_BINDING_DEFAULT);
275
276   g_assert ((BindingSource *) g_binding_get_source (binding) == source);
277   g_assert ((BindingTarget *) g_binding_get_target (binding) == target);
278   g_assert_cmpstr (g_binding_get_source_property (binding), ==, "foo");
279   g_assert_cmpstr (g_binding_get_target_property (binding), ==, "bar");
280   g_assert_cmpint (g_binding_get_flags (binding), ==, G_BINDING_DEFAULT);
281
282   g_object_set (source, "foo", 42, NULL);
283   g_assert_cmpint (source->foo, ==, target->bar);
284
285   g_object_set (target, "bar", 47, NULL);
286   g_assert_cmpint (source->foo, !=, target->bar);
287
288   g_object_unref (binding);
289
290   g_object_set (source, "foo", 0, NULL);
291   g_assert_cmpint (source->foo, !=, target->bar);
292
293   g_object_unref (source);
294   g_object_unref (target);
295 }
296
297 static void
298 binding_bidirectional (void)
299 {
300   BindingSource *source = g_object_new (binding_source_get_type (), NULL);
301   BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
302   GBinding *binding;
303
304   binding = g_object_bind_property (source, "foo",
305                                     target, "bar",
306                                     G_BINDING_BIDIRECTIONAL);
307
308   g_object_set (source, "foo", 42, NULL);
309   g_assert_cmpint (source->foo, ==, target->bar);
310
311   g_object_set (target, "bar", 47, NULL);
312   g_assert_cmpint (source->foo, ==, target->bar);
313
314   g_object_unref (binding);
315
316   g_object_set (source, "foo", 0, NULL);
317   g_assert_cmpint (source->foo, !=, target->bar);
318
319   g_object_unref (source);
320   g_object_unref (target);
321 }
322
323 static void
324 data_free (gpointer data)
325 {
326   gboolean *b = data;
327
328   *b = TRUE;
329 }
330
331 static void
332 binding_transform_default (void)
333 {
334   BindingSource *source = g_object_new (binding_source_get_type (), NULL);
335   BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
336   GBinding *binding;
337   gpointer src, trg;
338   gchar *src_prop, *trg_prop;
339   GBindingFlags flags;
340
341   binding = g_object_bind_property (source, "foo",
342                                     target, "value",
343                                     G_BINDING_BIDIRECTIONAL);
344
345   g_object_get (binding,
346                 "source", &src,
347                 "source-property", &src_prop,
348                 "target", &trg,
349                 "target-property", &trg_prop,
350                 "flags", &flags,
351                 NULL);
352   g_assert (src == source);
353   g_assert (trg == target);
354   g_assert_cmpstr (src_prop, ==, "foo");
355   g_assert_cmpstr (trg_prop, ==, "value");
356   g_assert_cmpint (flags, ==, G_BINDING_BIDIRECTIONAL);
357   g_object_unref (src);
358   g_object_unref (trg);
359   g_free (src_prop);
360   g_free (trg_prop);
361
362   g_object_set (source, "foo", 24, NULL);
363   g_assert_cmpfloat (target->value, ==, 24.0);
364
365   g_object_set (target, "value", 69.0, NULL);
366   g_assert_cmpint (source->foo, ==, 69);
367
368   g_object_unref (target);
369   g_object_unref (source);
370 }
371
372 static void
373 binding_transform (void)
374 {
375   BindingSource *source = g_object_new (binding_source_get_type (), NULL);
376   BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
377   GBinding *binding G_GNUC_UNUSED;
378   gboolean unused_data = FALSE;
379
380   binding = g_object_bind_property_full (source, "value",
381                                          target, "value",
382                                          G_BINDING_BIDIRECTIONAL,
383                                          celsius_to_fahrenheit,
384                                          fahrenheit_to_celsius,
385                                          &unused_data, data_free);
386
387   g_object_set (source, "value", 24.0, NULL);
388   g_assert_cmpfloat (target->value, ==, ((9 * 24.0 / 5) + 32.0));
389
390   g_object_set (target, "value", 69.0, NULL);
391   g_assert_cmpfloat (source->value, ==, (5 * (69.0 - 32.0) / 9));
392
393   g_object_unref (source);
394   g_object_unref (target);
395
396   g_assert (unused_data);
397 }
398
399 static void
400 binding_transform_closure (void)
401 {
402   BindingSource *source = g_object_new (binding_source_get_type (), NULL);
403   BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
404   GBinding *binding G_GNUC_UNUSED;
405   gboolean unused_data_1 = FALSE, unused_data_2 = FALSE;
406   GClosure *c2f_clos, *f2c_clos;
407
408   c2f_clos = g_cclosure_new (G_CALLBACK (celsius_to_fahrenheit), &unused_data_1, (GClosureNotify) data_free);
409
410   f2c_clos = g_cclosure_new (G_CALLBACK (fahrenheit_to_celsius), &unused_data_2, (GClosureNotify) data_free);
411
412   binding = g_object_bind_property_with_closures (source, "value",
413                                                   target, "value",
414                                                   G_BINDING_BIDIRECTIONAL,
415                                                   c2f_clos,
416                                                   f2c_clos);
417
418   g_object_set (source, "value", 24.0, NULL);
419   g_assert_cmpfloat (target->value, ==, ((9 * 24.0 / 5) + 32.0));
420
421   g_object_set (target, "value", 69.0, NULL);
422   g_assert_cmpfloat (source->value, ==, (5 * (69.0 - 32.0) / 9));
423
424   g_object_unref (source);
425   g_object_unref (target);
426
427   g_assert (unused_data_1);
428   g_assert (unused_data_2);
429 }
430
431 static void
432 binding_chain (void)
433 {
434   BindingSource *a = g_object_new (binding_source_get_type (), NULL);
435   BindingSource *b = g_object_new (binding_source_get_type (), NULL);
436   BindingSource *c = g_object_new (binding_source_get_type (), NULL);
437   GBinding *binding_1, *binding_2;
438
439   g_test_bug ("621782");
440
441   /* A -> B, B -> C */
442   binding_1 = g_object_bind_property (a, "foo", b, "foo", G_BINDING_BIDIRECTIONAL);
443   binding_2 = g_object_bind_property (b, "foo", c, "foo", G_BINDING_BIDIRECTIONAL);
444
445   /* verify the chain */
446   g_object_set (a, "foo", 42, NULL);
447   g_assert_cmpint (a->foo, ==, b->foo);
448   g_assert_cmpint (b->foo, ==, c->foo);
449
450   /* unbind A -> B and B -> C */
451   g_object_unref (binding_1);
452   g_object_unref (binding_2);
453
454   /* bind A -> C directly */
455   binding_2 = g_object_bind_property (a, "foo", c, "foo", G_BINDING_BIDIRECTIONAL);
456
457   /* verify the chain is broken */
458   g_object_set (a, "foo", 47, NULL);
459   g_assert_cmpint (a->foo, !=, b->foo);
460   g_assert_cmpint (a->foo, ==, c->foo);
461
462   g_object_unref (a);
463   g_object_unref (b);
464   g_object_unref (c);
465 }
466
467 static void
468 binding_sync_create (void)
469 {
470   BindingSource *source = g_object_new (binding_source_get_type (),
471                                         "foo", 42,
472                                         NULL);
473   BindingTarget *target = g_object_new (binding_target_get_type (),
474                                         "bar", 47,
475                                         NULL);
476   GBinding *binding;
477
478   binding = g_object_bind_property (source, "foo",
479                                     target, "bar",
480                                     G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
481
482   g_assert_cmpint (source->foo, ==, 42);
483   g_assert_cmpint (target->bar, ==, 42);
484
485   g_object_set (source, "foo", 47, NULL);
486   g_assert_cmpint (source->foo, ==, target->bar);
487
488   g_object_unref (binding);
489
490   g_object_set (target, "bar", 49, NULL);
491
492   binding = g_object_bind_property (source, "foo",
493                                     target, "bar",
494                                     G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
495   g_assert_cmpint (source->foo, ==, 47);
496   g_assert_cmpint (target->bar, ==, 47);
497
498   g_object_unref (source);
499   g_object_unref (target);
500 }
501
502 static void
503 binding_invert_boolean (void)
504 {
505   BindingSource *source = g_object_new (binding_source_get_type (),
506                                         "toggle", TRUE,
507                                         NULL);
508   BindingTarget *target = g_object_new (binding_target_get_type (),
509                                         "toggle", FALSE,
510                                         NULL);
511   GBinding *binding;
512
513   binding = g_object_bind_property (source, "toggle",
514                                     target, "toggle",
515                                     G_BINDING_BIDIRECTIONAL | G_BINDING_INVERT_BOOLEAN);
516
517   g_assert (source->toggle);
518   g_assert (!target->toggle);
519
520   g_object_set (source, "toggle", FALSE, NULL);
521   g_assert (!source->toggle);
522   g_assert (target->toggle);
523
524   g_object_set (target, "toggle", FALSE, NULL);
525   g_assert (source->toggle);
526   g_assert (!target->toggle);
527
528   g_object_unref (binding);
529   g_object_unref (source);
530   g_object_unref (target);
531 }
532
533 int
534 main (int argc, char *argv[])
535 {
536   g_type_init ();
537   g_test_init (&argc, &argv, NULL);
538
539   g_test_bug_base ("http://bugzilla.gnome.org/");
540
541   g_test_add_func ("/binding/default", binding_default);
542   g_test_add_func ("/binding/bidirectional", binding_bidirectional);
543   g_test_add_func ("/binding/transform", binding_transform);
544   g_test_add_func ("/binding/transform-default", binding_transform_default);
545   g_test_add_func ("/binding/transform-closure", binding_transform_closure);
546   g_test_add_func ("/binding/chain", binding_chain);
547   g_test_add_func ("/binding/sync-create", binding_sync_create);
548   g_test_add_func ("/binding/invert-boolean", binding_invert_boolean);
549
550   return g_test_run ();
551 }