Add bug references to some tests
[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 (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   gboolean unused_data = FALSE;
338
339   binding = g_object_bind_property_full (source, "value",
340                                          target, "value",
341                                          G_BINDING_BIDIRECTIONAL,
342                                          celsius_to_fahrenheit,
343                                          fahrenheit_to_celsius,
344                                          &unused_data, data_free);
345
346   g_object_set (source, "value", 24.0, NULL);
347   g_assert_cmpfloat (target->value, ==, ((9 * 24.0 / 5) + 32.0));
348
349   g_object_set (target, "value", 69.0, NULL);
350   g_assert_cmpfloat (source->value, ==, (5 * (69.0 - 32.0) / 9));
351
352   g_object_unref (source);
353   g_object_unref (target);
354
355   g_assert (unused_data);
356 }
357
358 static void
359 binding_transform_closure (void)
360 {
361   BindingSource *source = g_object_new (binding_source_get_type (), NULL);
362   BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
363   GBinding *binding;
364   gboolean unused_data_1 = FALSE, unused_data_2 = FALSE;
365   GClosure *c2f_clos, *f2c_clos;
366
367   c2f_clos = g_cclosure_new (G_CALLBACK (celsius_to_fahrenheit), &unused_data_1, (GClosureNotify) data_free);
368
369   f2c_clos = g_cclosure_new (G_CALLBACK (fahrenheit_to_celsius), &unused_data_2, (GClosureNotify) data_free);
370
371   binding = g_object_bind_property_with_closures (source, "value",
372                                                   target, "value",
373                                                   G_BINDING_BIDIRECTIONAL,
374                                                   c2f_clos,
375                                                   f2c_clos);
376
377   g_object_set (source, "value", 24.0, NULL);
378   g_assert_cmpfloat (target->value, ==, ((9 * 24.0 / 5) + 32.0));
379
380   g_object_set (target, "value", 69.0, NULL);
381   g_assert_cmpfloat (source->value, ==, (5 * (69.0 - 32.0) / 9));
382
383   g_object_unref (source);
384   g_object_unref (target);
385
386   g_assert (unused_data_1);
387   g_assert (unused_data_2);
388 }
389
390 static void
391 binding_chain (void)
392 {
393   BindingSource *a = g_object_new (binding_source_get_type (), NULL);
394   BindingSource *b = g_object_new (binding_source_get_type (), NULL);
395   BindingSource *c = g_object_new (binding_source_get_type (), NULL);
396   GBinding *binding_1, *binding_2;
397
398   g_test_bug ("621782");
399
400   /* A -> B, B -> C */
401   binding_1 = g_object_bind_property (a, "foo", b, "foo", G_BINDING_BIDIRECTIONAL);
402   binding_2 = g_object_bind_property (b, "foo", c, "foo", G_BINDING_BIDIRECTIONAL);
403
404   /* verify the chain */
405   g_object_set (a, "foo", 42, NULL);
406   g_assert_cmpint (a->foo, ==, b->foo);
407   g_assert_cmpint (b->foo, ==, c->foo);
408
409   /* unbind A -> B and B -> C */
410   g_object_unref (binding_1);
411   g_object_unref (binding_2);
412
413   /* bind A -> C directly */
414   binding_2 = g_object_bind_property (a, "foo", c, "foo", G_BINDING_BIDIRECTIONAL);
415
416   /* verify the chain is broken */
417   g_object_set (a, "foo", 47, NULL);
418   g_assert_cmpint (a->foo, !=, b->foo);
419   g_assert_cmpint (a->foo, ==, c->foo);
420
421   g_object_unref (a);
422   g_object_unref (b);
423   g_object_unref (c);
424 }
425
426 static void
427 binding_sync_create (void)
428 {
429   BindingSource *source = g_object_new (binding_source_get_type (),
430                                         "foo", 42,
431                                         NULL);
432   BindingTarget *target = g_object_new (binding_target_get_type (),
433                                         "bar", 47,
434                                         NULL);
435   GBinding *binding;
436
437   binding = g_object_bind_property (source, "foo",
438                                     target, "bar",
439                                     G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
440
441   g_assert_cmpint (source->foo, ==, 42);
442   g_assert_cmpint (target->bar, ==, 42);
443
444   g_object_set (source, "foo", 47, NULL);
445   g_assert_cmpint (source->foo, ==, target->bar);
446
447   g_object_unref (binding);
448
449   g_object_set (target, "bar", 49, NULL);
450
451   binding = g_object_bind_property (source, "foo",
452                                     target, "bar",
453                                     G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
454   g_assert_cmpint (source->foo, ==, 47);
455   g_assert_cmpint (target->bar, ==, 47);
456
457   g_object_unref (source);
458   g_object_unref (target);
459 }
460
461 static void
462 binding_invert_boolean (void)
463 {
464   BindingSource *source = g_object_new (binding_source_get_type (),
465                                         "toggle", TRUE,
466                                         NULL);
467   BindingTarget *target = g_object_new (binding_target_get_type (),
468                                         "toggle", FALSE,
469                                         NULL);
470   GBinding *binding;
471
472   binding = g_object_bind_property (source, "toggle",
473                                     target, "toggle",
474                                     G_BINDING_DEFAULT | G_BINDING_INVERT_BOOLEAN);
475
476   g_assert (source->toggle);
477   g_assert (!target->toggle);
478
479   g_object_set (source, "toggle", FALSE, NULL);
480   g_assert (!source->toggle);
481   g_assert (target->toggle);
482
483   g_object_unref (binding);
484   g_object_unref (source);
485   g_object_unref (target);
486 }
487
488 int
489 main (int argc, char *argv[])
490 {
491   g_type_init ();
492   g_test_init (&argc, &argv, NULL);
493
494   g_test_bug_base ("http://bugzilla.gnome.org/");
495
496   g_test_add_func ("/binding/default", binding_default);
497   g_test_add_func ("/binding/bidirectional", binding_bidirectional);
498   g_test_add_func ("/binding/transform", binding_transform);
499   g_test_add_func ("/binding/transform-closure", binding_transform_closure);
500   g_test_add_func ("/binding/chain", binding_chain);
501   g_test_add_func ("/binding/sync-create", binding_sync_create);
502   g_test_add_func ("/binding/invert-boolean", binding_invert_boolean);
503
504   return g_test_run ();
505 }