f4b8240fdc42b5a23d28b6643595b6b179b14ab3
[platform/upstream/glib.git] / gobject / tests / signals.c
1 #include <glib-object.h>
2
3 typedef struct _Test Test;
4 typedef struct _TestClass TestClass;
5
6 struct _Test
7 {
8   GObject parent_instance;
9 };
10
11 struct _TestClass
12 {
13   GObjectClass parent_class;
14
15   void (* variant_changed) (Test *, GVariant *);
16 };
17
18 static GType test_get_type (void);
19 G_DEFINE_TYPE (Test, test, G_TYPE_OBJECT)
20
21 static void
22 test_init (Test *test)
23 {
24 }
25
26 static void
27 test_class_init (TestClass *klass)
28 {
29   g_signal_new ("variant-changed-no-slot",
30                 G_TYPE_FROM_CLASS (klass),
31                 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
32                 0,
33                 NULL, NULL,
34                 g_cclosure_marshal_VOID__VARIANT,
35                 G_TYPE_NONE,
36                 1,
37                 G_TYPE_VARIANT);
38   g_signal_new ("variant-changed",
39                 G_TYPE_FROM_CLASS (klass),
40                 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
41                 G_STRUCT_OFFSET (TestClass, variant_changed),
42                 NULL, NULL,
43                 g_cclosure_marshal_VOID__VARIANT,
44                 G_TYPE_NONE,
45                 1,
46                 G_TYPE_VARIANT);
47 }
48
49 static void
50 test_variant_signal (void)
51 {
52   Test *test;
53   GVariant *v;
54
55   /* Tests that the signal emission consumes the variant,
56    * even if there are no handlers connected.
57    */
58
59   test = g_object_new (test_get_type (), NULL);
60
61   v = g_variant_new_boolean (TRUE);
62   g_variant_ref (v);
63   g_assert (g_variant_is_floating (v));
64   g_signal_emit_by_name (test, "variant-changed-no-slot", v);
65   g_assert (!g_variant_is_floating (v));
66   g_variant_unref (v);
67
68   v = g_variant_new_boolean (TRUE);
69   g_variant_ref (v);
70   g_assert (g_variant_is_floating (v));
71   g_signal_emit_by_name (test, "variant-changed", v);
72   g_assert (!g_variant_is_floating (v));
73   g_variant_unref (v);
74
75   g_object_unref (test);
76 }
77
78 /* --- */
79
80 int
81 main (int argc,
82      char *argv[])
83 {
84   g_type_init ();
85
86   g_test_init (&argc, &argv, NULL);
87
88   g_test_add_func ("/gobject/signals/variant", test_variant_signal);
89
90   return g_test_run ();
91 }