Rename the generated private data getter function
[platform/upstream/glib.git] / gobject / tests / signals.c
1 #include <glib-object.h>
2 #include "marshalers.h"
3
4 typedef enum {
5   TEST_ENUM_NEGATIVE = -30,
6   TEST_ENUM_NONE = 0,
7   TEST_ENUM_FOO = 1,
8   TEST_ENUM_BAR = 2
9 } TestEnum;
10
11 typedef enum {
12   TEST_UNSIGNED_ENUM_FOO = 1,
13   TEST_UNSIGNED_ENUM_BAR = 42
14   /* Don't test 0x80000000 for now- nothing appears to do this in
15    * practice, and it triggers GValue/GEnum bugs on ppc64.
16    */
17 } TestUnsignedEnum;
18
19 static GType
20 test_enum_get_type (void)
21 {
22   static volatile gsize g_define_type_id__volatile = 0;
23
24   if (g_once_init_enter (&g_define_type_id__volatile))
25     {
26       static const GEnumValue values[] = {
27         { TEST_ENUM_NEGATIVE, "TEST_ENUM_NEGATIVE", "negative" },
28         { TEST_ENUM_NONE, "TEST_ENUM_NONE", "none" },
29         { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
30         { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
31         { 0, NULL, NULL }
32       };
33       GType g_define_type_id =
34         g_enum_register_static (g_intern_static_string ("TestEnum"), values);
35       g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
36     }
37
38   return g_define_type_id__volatile;
39 }
40
41 static GType
42 test_unsigned_enum_get_type (void)
43 {
44   static volatile gsize g_define_type_id__volatile = 0;
45
46   if (g_once_init_enter (&g_define_type_id__volatile))
47     {
48       static const GEnumValue values[] = {
49         { TEST_UNSIGNED_ENUM_FOO, "TEST_UNSIGNED_ENUM_FOO", "foo" },
50         { TEST_UNSIGNED_ENUM_BAR, "TEST_UNSIGNED_ENUM_BAR", "bar" },
51         { 0, NULL, NULL }
52       };
53       GType g_define_type_id =
54         g_enum_register_static (g_intern_static_string ("TestUnsignedEnum"), values);
55       g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
56     }
57
58   return g_define_type_id__volatile;
59 }
60
61
62 static const GEnumValue my_enum_values[] =
63 {
64   { 1, "the first value", "one" },
65   { 0, NULL, NULL }
66 };
67
68 static const GFlagsValue my_flag_values[] =
69 {
70   { 1, "the first value", "one" },
71   { 0, NULL, NULL }
72 };
73
74 static GType enum_type;
75 static GType flags_type;
76
77 static guint simple_id;
78
79 typedef struct _Test Test;
80 typedef struct _TestClass TestClass;
81
82 struct _Test
83 {
84   GObject parent_instance;
85 };
86
87 static void all_types_handler (Test *test, int i, gboolean b, char c, guchar uc, guint ui, glong l, gulong ul, gint e, guint f, float fl, double db, char *str, GParamSpec *param, GBytes *bytes, gpointer ptr, Test *obj, GVariant *var, gint64 i64, guint64 ui64);
88
89 struct _TestClass
90 {
91   GObjectClass parent_class;
92
93   void (* variant_changed) (Test *, GVariant *);
94   void (* all_types) (Test *test, int i, gboolean b, char c, guchar uc, guint ui, glong l, gulong ul, gint e, guint f, float fl, double db, char *str, GParamSpec *param, GBytes *bytes, gpointer ptr, Test *obj, GVariant *var, gint64 i64, guint64 ui64);
95   void (* all_types_null) (Test *test, int i, gboolean b, char c, guchar uc, guint ui, glong l, gulong ul, gint e, guint f, float fl, double db, char *str, GParamSpec *param, GBytes *bytes, gpointer ptr, Test *obj, GVariant *var, gint64 i64, guint64 ui64);
96 };
97
98 static GType test_get_type (void);
99 G_DEFINE_TYPE (Test, test, G_TYPE_OBJECT)
100
101 static void
102 test_init (Test *test)
103 {
104 }
105
106 static void
107 test_class_init (TestClass *klass)
108 {
109   guint s;
110
111   enum_type = g_enum_register_static ("MyEnum", my_enum_values);
112   flags_type = g_flags_register_static ("MyFlag", my_flag_values);
113
114   klass->all_types = all_types_handler;
115
116   simple_id = g_signal_new ("simple",
117                 G_TYPE_FROM_CLASS (klass),
118                 G_SIGNAL_RUN_LAST,
119                 0,
120                 NULL, NULL,
121                 NULL,
122                 G_TYPE_NONE,
123                 0);
124   g_signal_new ("generic-marshaller-1",
125                 G_TYPE_FROM_CLASS (klass),
126                 G_SIGNAL_RUN_LAST,
127                 0,
128                 NULL, NULL,
129                 NULL,
130                 G_TYPE_NONE,
131                 7,
132                 G_TYPE_CHAR, G_TYPE_UCHAR, G_TYPE_INT, G_TYPE_LONG, G_TYPE_POINTER, G_TYPE_DOUBLE, G_TYPE_FLOAT);
133   g_signal_new ("generic-marshaller-2",
134                 G_TYPE_FROM_CLASS (klass),
135                 G_SIGNAL_RUN_LAST,
136                 0,
137                 NULL, NULL,
138                 NULL,
139                 G_TYPE_NONE,
140                 5,
141                 G_TYPE_INT, test_enum_get_type(), G_TYPE_INT, test_unsigned_enum_get_type (), G_TYPE_INT);
142   g_signal_new ("generic-marshaller-enum-return-signed",
143                 G_TYPE_FROM_CLASS (klass),
144                 G_SIGNAL_RUN_LAST,
145                 0,
146                 NULL, NULL,
147                 NULL,
148                 test_enum_get_type(),
149                 0);
150   g_signal_new ("generic-marshaller-enum-return-unsigned",
151                 G_TYPE_FROM_CLASS (klass),
152                 G_SIGNAL_RUN_LAST,
153                 0,
154                 NULL, NULL,
155                 NULL,
156                 test_unsigned_enum_get_type(),
157                 0);
158   g_signal_new ("generic-marshaller-int-return",
159                 G_TYPE_FROM_CLASS (klass),
160                 G_SIGNAL_RUN_LAST,
161                 0,
162                 NULL, NULL,
163                 NULL,
164                 G_TYPE_INT,
165                 0);
166   s = g_signal_new ("va-marshaller-int-return",
167                 G_TYPE_FROM_CLASS (klass),
168                 G_SIGNAL_RUN_LAST,
169                 0,
170                 NULL, NULL,
171                 test_INT__VOID,
172                 G_TYPE_INT,
173                 0);
174   g_signal_set_va_marshaller (s, G_TYPE_FROM_CLASS (klass),
175                               test_INT__VOIDv);
176   g_signal_new ("generic-marshaller-uint-return",
177                 G_TYPE_FROM_CLASS (klass),
178                 G_SIGNAL_RUN_LAST,
179                 0,
180                 NULL, NULL,
181                 NULL,
182                 G_TYPE_UINT,
183                 0);
184   s = g_signal_new ("va-marshaller-uint-return",
185                 G_TYPE_FROM_CLASS (klass),
186                 G_SIGNAL_RUN_LAST,
187                 0,
188                 NULL, NULL,
189                 test_INT__VOID,
190                 G_TYPE_UINT,
191                 0);
192   g_signal_set_va_marshaller (s, G_TYPE_FROM_CLASS (klass),
193                               test_UINT__VOIDv);
194   g_signal_new ("variant-changed-no-slot",
195                 G_TYPE_FROM_CLASS (klass),
196                 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
197                 0,
198                 NULL, NULL,
199                 g_cclosure_marshal_VOID__VARIANT,
200                 G_TYPE_NONE,
201                 1,
202                 G_TYPE_VARIANT);
203   g_signal_new ("variant-changed",
204                 G_TYPE_FROM_CLASS (klass),
205                 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
206                 G_STRUCT_OFFSET (TestClass, variant_changed),
207                 NULL, NULL,
208                 g_cclosure_marshal_VOID__VARIANT,
209                 G_TYPE_NONE,
210                 1,
211                 G_TYPE_VARIANT);
212   g_signal_new ("all-types",
213                 G_TYPE_FROM_CLASS (klass),
214                 G_SIGNAL_RUN_LAST,
215                 G_STRUCT_OFFSET (TestClass, all_types),
216                 NULL, NULL,
217                 test_VOID__INT_BOOLEAN_CHAR_UCHAR_UINT_LONG_ULONG_ENUM_FLAGS_FLOAT_DOUBLE_STRING_PARAM_BOXED_POINTER_OBJECT_VARIANT_INT64_UINT64,
218                 G_TYPE_NONE,
219                 19,
220                 G_TYPE_INT,
221                 G_TYPE_BOOLEAN,
222                 G_TYPE_CHAR,
223                 G_TYPE_UCHAR,
224                 G_TYPE_UINT,
225                 G_TYPE_LONG,
226                 G_TYPE_ULONG,
227                 enum_type,
228                 flags_type,
229                 G_TYPE_FLOAT,
230                 G_TYPE_DOUBLE,
231                 G_TYPE_STRING,
232                 G_TYPE_PARAM_LONG,
233                 G_TYPE_BYTES,
234                 G_TYPE_POINTER,
235                 test_get_type (),
236                 G_TYPE_VARIANT,
237                 G_TYPE_INT64,
238                 G_TYPE_UINT64);
239   s = g_signal_new ("all-types-va",
240                 G_TYPE_FROM_CLASS (klass),
241                 G_SIGNAL_RUN_LAST,
242                 G_STRUCT_OFFSET (TestClass, all_types),
243                 NULL, NULL,
244                 test_VOID__INT_BOOLEAN_CHAR_UCHAR_UINT_LONG_ULONG_ENUM_FLAGS_FLOAT_DOUBLE_STRING_PARAM_BOXED_POINTER_OBJECT_VARIANT_INT64_UINT64,
245                 G_TYPE_NONE,
246                 19,
247                 G_TYPE_INT,
248                 G_TYPE_BOOLEAN,
249                 G_TYPE_CHAR,
250                 G_TYPE_UCHAR,
251                 G_TYPE_UINT,
252                 G_TYPE_LONG,
253                 G_TYPE_ULONG,
254                 enum_type,
255                 flags_type,
256                 G_TYPE_FLOAT,
257                 G_TYPE_DOUBLE,
258                 G_TYPE_STRING,
259                 G_TYPE_PARAM_LONG,
260                 G_TYPE_BYTES,
261                 G_TYPE_POINTER,
262                 test_get_type (),
263                 G_TYPE_VARIANT,
264                 G_TYPE_INT64,
265                 G_TYPE_UINT64);
266   g_signal_set_va_marshaller (s, G_TYPE_FROM_CLASS (klass),
267                               test_VOID__INT_BOOLEAN_CHAR_UCHAR_UINT_LONG_ULONG_ENUM_FLAGS_FLOAT_DOUBLE_STRING_PARAM_BOXED_POINTER_OBJECT_VARIANT_INT64_UINT64v);
268
269   g_signal_new ("all-types-generic",
270                 G_TYPE_FROM_CLASS (klass),
271                 G_SIGNAL_RUN_LAST,
272                 G_STRUCT_OFFSET (TestClass, all_types),
273                 NULL, NULL,
274                 NULL,
275                 G_TYPE_NONE,
276                 19,
277                 G_TYPE_INT,
278                 G_TYPE_BOOLEAN,
279                 G_TYPE_CHAR,
280                 G_TYPE_UCHAR,
281                 G_TYPE_UINT,
282                 G_TYPE_LONG,
283                 G_TYPE_ULONG,
284                 enum_type,
285                 flags_type,
286                 G_TYPE_FLOAT,
287                 G_TYPE_DOUBLE,
288                 G_TYPE_STRING,
289                 G_TYPE_PARAM_LONG,
290                 G_TYPE_BYTES,
291                 G_TYPE_POINTER,
292                 test_get_type (),
293                 G_TYPE_VARIANT,
294                 G_TYPE_INT64,
295                 G_TYPE_UINT64);
296   g_signal_new ("all-types-null",
297                 G_TYPE_FROM_CLASS (klass),
298                 G_SIGNAL_RUN_LAST,
299                 G_STRUCT_OFFSET (TestClass, all_types_null),
300                 NULL, NULL,
301                 test_VOID__INT_BOOLEAN_CHAR_UCHAR_UINT_LONG_ULONG_ENUM_FLAGS_FLOAT_DOUBLE_STRING_PARAM_BOXED_POINTER_OBJECT_VARIANT_INT64_UINT64,
302                 G_TYPE_NONE,
303                 19,
304                 G_TYPE_INT,
305                 G_TYPE_BOOLEAN,
306                 G_TYPE_CHAR,
307                 G_TYPE_UCHAR,
308                 G_TYPE_UINT,
309                 G_TYPE_LONG,
310                 G_TYPE_ULONG,
311                 enum_type,
312                 flags_type,
313                 G_TYPE_FLOAT,
314                 G_TYPE_DOUBLE,
315                 G_TYPE_STRING,
316                 G_TYPE_PARAM_LONG,
317                 G_TYPE_BYTES,
318                 G_TYPE_POINTER,
319                 test_get_type (),
320                 G_TYPE_VARIANT,
321                 G_TYPE_INT64,
322                 G_TYPE_UINT64);
323   g_signal_new ("all-types-empty",
324                 G_TYPE_FROM_CLASS (klass),
325                 G_SIGNAL_RUN_LAST,
326                 0,
327                 NULL, NULL,
328                 test_VOID__INT_BOOLEAN_CHAR_UCHAR_UINT_LONG_ULONG_ENUM_FLAGS_FLOAT_DOUBLE_STRING_PARAM_BOXED_POINTER_OBJECT_VARIANT_INT64_UINT64,
329                 G_TYPE_NONE,
330                 19,
331                 G_TYPE_INT,
332                 G_TYPE_BOOLEAN,
333                 G_TYPE_CHAR,
334                 G_TYPE_UCHAR,
335                 G_TYPE_UINT,
336                 G_TYPE_LONG,
337                 G_TYPE_ULONG,
338                 enum_type,
339                 flags_type,
340                 G_TYPE_FLOAT,
341                 G_TYPE_DOUBLE,
342                 G_TYPE_STRING,
343                 G_TYPE_PARAM_LONG,
344                 G_TYPE_BYTES,
345                 G_TYPE_POINTER,
346                 test_get_type (),
347                 G_TYPE_VARIANT,
348                 G_TYPE_INT64,
349                 G_TYPE_UINT64);
350 }
351
352 static void
353 test_variant_signal (void)
354 {
355   Test *test;
356   GVariant *v;
357
358   /* Tests that the signal emission consumes the variant,
359    * even if there are no handlers connected.
360    */
361
362   test = g_object_new (test_get_type (), NULL);
363
364   v = g_variant_new_boolean (TRUE);
365   g_variant_ref (v);
366   g_assert (g_variant_is_floating (v));
367   g_signal_emit_by_name (test, "variant-changed-no-slot", v);
368   g_assert (!g_variant_is_floating (v));
369   g_variant_unref (v);
370
371   v = g_variant_new_boolean (TRUE);
372   g_variant_ref (v);
373   g_assert (g_variant_is_floating (v));
374   g_signal_emit_by_name (test, "variant-changed", v);
375   g_assert (!g_variant_is_floating (v));
376   g_variant_unref (v);
377
378   g_object_unref (test);
379 }
380
381 static void
382 on_generic_marshaller_1 (Test *obj,
383                          gint8 v_schar,
384                          guint8 v_uchar,
385                          gint v_int,
386                          glong v_long,
387                          gpointer v_pointer,
388                          gdouble v_double,
389                          gfloat v_float,
390                          gpointer user_data)
391 {
392   g_assert_cmpint (v_schar, ==, 42);
393   g_assert_cmpint (v_uchar, ==, 43);
394   g_assert_cmpint (v_int, ==, 4096);
395   g_assert_cmpint (v_long, ==, 8192);
396   g_assert (v_pointer == NULL);
397   g_assert_cmpfloat (v_double, >, 0.0);
398   g_assert_cmpfloat (v_double, <, 1.0);
399   g_assert_cmpfloat (v_float, >, 5.0);
400   g_assert_cmpfloat (v_float, <, 6.0);
401 }
402                          
403 static void
404 test_generic_marshaller_signal_1 (void)
405 {
406   Test *test;
407   test = g_object_new (test_get_type (), NULL);
408
409   g_signal_connect (test, "generic-marshaller-1", G_CALLBACK (on_generic_marshaller_1), NULL);
410
411   g_signal_emit_by_name (test, "generic-marshaller-1", 42, 43, 4096, 8192, NULL, 0.5, 5.5);
412
413   g_object_unref (test);
414 }
415
416 static void
417 on_generic_marshaller_2 (Test *obj,
418                          gint        v_int1,
419                          TestEnum    v_enum,
420                          gint        v_int2,
421                          TestUnsignedEnum v_uenum,
422                          gint        v_int3)
423 {
424   g_assert_cmpint (v_int1, ==, 42);
425   g_assert_cmpint (v_enum, ==, TEST_ENUM_BAR);
426   g_assert_cmpint (v_int2, ==, 43);
427   g_assert_cmpint (v_uenum, ==, TEST_UNSIGNED_ENUM_BAR);
428   g_assert_cmpint (v_int3, ==, 44);
429 }
430
431 static void
432 test_generic_marshaller_signal_2 (void)
433 {
434   Test *test;
435   test = g_object_new (test_get_type (), NULL);
436
437   g_signal_connect (test, "generic-marshaller-2", G_CALLBACK (on_generic_marshaller_2), NULL);
438
439   g_signal_emit_by_name (test, "generic-marshaller-2", 42, TEST_ENUM_BAR, 43, TEST_UNSIGNED_ENUM_BAR, 44);
440
441   g_object_unref (test);
442 }
443
444 static TestEnum
445 on_generic_marshaller_enum_return_signed_1 (Test *obj)
446 {
447   return TEST_ENUM_NEGATIVE;
448 }
449
450 static TestEnum
451 on_generic_marshaller_enum_return_signed_2 (Test *obj)
452 {
453   return TEST_ENUM_BAR;
454 }
455
456 static void
457 test_generic_marshaller_signal_enum_return_signed (void)
458 {
459   Test *test;
460   guint id;
461   TestEnum retval = 0;
462
463   test = g_object_new (test_get_type (), NULL);
464
465   /* Test return value NEGATIVE */
466   id = g_signal_connect (test,
467                          "generic-marshaller-enum-return-signed",
468                          G_CALLBACK (on_generic_marshaller_enum_return_signed_1),
469                          NULL);
470   g_signal_emit_by_name (test, "generic-marshaller-enum-return-signed", &retval);
471   g_assert_cmpint (retval, ==, TEST_ENUM_NEGATIVE);
472   g_signal_handler_disconnect (test, id);
473
474   /* Test return value BAR */
475   retval = 0;
476   id = g_signal_connect (test,
477                          "generic-marshaller-enum-return-signed",
478                          G_CALLBACK (on_generic_marshaller_enum_return_signed_2),
479                          NULL);
480   g_signal_emit_by_name (test, "generic-marshaller-enum-return-signed", &retval);
481   g_assert_cmpint (retval, ==, TEST_ENUM_BAR);
482   g_signal_handler_disconnect (test, id);
483
484   g_object_unref (test);
485 }
486
487 static TestUnsignedEnum
488 on_generic_marshaller_enum_return_unsigned_1 (Test *obj)
489 {
490   return TEST_UNSIGNED_ENUM_FOO;
491 }
492
493 static TestUnsignedEnum
494 on_generic_marshaller_enum_return_unsigned_2 (Test *obj)
495 {
496   return TEST_UNSIGNED_ENUM_BAR;
497 }
498
499 static void
500 test_generic_marshaller_signal_enum_return_unsigned (void)
501 {
502   Test *test;
503   guint id;
504   TestUnsignedEnum retval = 0;
505
506   test = g_object_new (test_get_type (), NULL);
507
508   /* Test return value FOO */
509   id = g_signal_connect (test,
510                          "generic-marshaller-enum-return-unsigned",
511                          G_CALLBACK (on_generic_marshaller_enum_return_unsigned_1),
512                          NULL);
513   g_signal_emit_by_name (test, "generic-marshaller-enum-return-unsigned", &retval);
514   g_assert_cmpint (retval, ==, TEST_UNSIGNED_ENUM_FOO);
515   g_signal_handler_disconnect (test, id);
516
517   /* Test return value BAR */
518   retval = 0;
519   id = g_signal_connect (test,
520                          "generic-marshaller-enum-return-unsigned",
521                          G_CALLBACK (on_generic_marshaller_enum_return_unsigned_2),
522                          NULL);
523   g_signal_emit_by_name (test, "generic-marshaller-enum-return-unsigned", &retval);
524   g_assert_cmpint (retval, ==, TEST_UNSIGNED_ENUM_BAR);
525   g_signal_handler_disconnect (test, id);
526
527   g_object_unref (test);
528 }
529
530 /**********************/
531
532 static gint
533 on_generic_marshaller_int_return_signed_1 (Test *obj)
534 {
535   return -30;
536 }
537
538 static gint
539 on_generic_marshaller_int_return_signed_2 (Test *obj)
540 {
541   return 2;
542 }
543
544 static void
545 test_generic_marshaller_signal_int_return (void)
546 {
547   Test *test;
548   guint id;
549   gint retval = 0;
550
551   test = g_object_new (test_get_type (), NULL);
552
553   /* Test return value -30 */
554   id = g_signal_connect (test,
555                          "generic-marshaller-int-return",
556                          G_CALLBACK (on_generic_marshaller_int_return_signed_1),
557                          NULL);
558   g_signal_emit_by_name (test, "generic-marshaller-int-return", &retval);
559   g_assert_cmpint (retval, ==, -30);
560   g_signal_handler_disconnect (test, id);
561
562   /* Test return value positive */
563   retval = 0;
564   id = g_signal_connect (test,
565                          "generic-marshaller-int-return",
566                          G_CALLBACK (on_generic_marshaller_int_return_signed_2),
567                          NULL);
568   g_signal_emit_by_name (test, "generic-marshaller-int-return", &retval);
569   g_assert_cmpint (retval, ==, 2);
570   g_signal_handler_disconnect (test, id);
571
572   /* Same test for va marshaller */
573
574   /* Test return value -30 */
575   id = g_signal_connect (test,
576                          "va-marshaller-int-return",
577                          G_CALLBACK (on_generic_marshaller_int_return_signed_1),
578                          NULL);
579   g_signal_emit_by_name (test, "va-marshaller-int-return", &retval);
580   g_assert_cmpint (retval, ==, -30);
581   g_signal_handler_disconnect (test, id);
582
583   /* Test return value positive */
584   retval = 0;
585   id = g_signal_connect (test,
586                          "va-marshaller-int-return",
587                          G_CALLBACK (on_generic_marshaller_int_return_signed_2),
588                          NULL);
589   g_signal_emit_by_name (test, "va-marshaller-int-return", &retval);
590   g_assert_cmpint (retval, ==, 2);
591   g_signal_handler_disconnect (test, id);
592
593   g_object_unref (test);
594 }
595
596 static guint
597 on_generic_marshaller_uint_return_1 (Test *obj)
598 {
599   return 1;
600 }
601
602 static guint
603 on_generic_marshaller_uint_return_2 (Test *obj)
604 {
605   return G_MAXUINT;
606 }
607
608 static void
609 test_generic_marshaller_signal_uint_return (void)
610 {
611   Test *test;
612   guint id;
613   guint retval = 0;
614
615   test = g_object_new (test_get_type (), NULL);
616
617   id = g_signal_connect (test,
618                          "generic-marshaller-uint-return",
619                          G_CALLBACK (on_generic_marshaller_uint_return_1),
620                          NULL);
621   g_signal_emit_by_name (test, "generic-marshaller-uint-return", &retval);
622   g_assert_cmpint (retval, ==, 1);
623   g_signal_handler_disconnect (test, id);
624
625   retval = 0;
626   id = g_signal_connect (test,
627                          "generic-marshaller-uint-return",
628                          G_CALLBACK (on_generic_marshaller_uint_return_2),
629                          NULL);
630   g_signal_emit_by_name (test, "generic-marshaller-uint-return", &retval);
631   g_assert_cmpint (retval, ==, G_MAXUINT);
632   g_signal_handler_disconnect (test, id);
633
634   /* Same test for va marshaller */
635
636   id = g_signal_connect (test,
637                          "va-marshaller-uint-return",
638                          G_CALLBACK (on_generic_marshaller_uint_return_1),
639                          NULL);
640   g_signal_emit_by_name (test, "va-marshaller-uint-return", &retval);
641   g_assert_cmpint (retval, ==, 1);
642   g_signal_handler_disconnect (test, id);
643
644   retval = 0;
645   id = g_signal_connect (test,
646                          "va-marshaller-uint-return",
647                          G_CALLBACK (on_generic_marshaller_uint_return_2),
648                          NULL);
649   g_signal_emit_by_name (test, "va-marshaller-uint-return", &retval);
650   g_assert_cmpint (retval, ==, G_MAXUINT);
651   g_signal_handler_disconnect (test, id);
652
653   g_object_unref (test);
654 }
655
656 static int all_type_handlers_count = 0;
657
658 static void
659 all_types_handler (Test *test, int i, gboolean b, char c, guchar uc, guint ui, glong l, gulong ul, gint e, guint f, float fl, double db, char *str, GParamSpec *param, GBytes *bytes, gpointer ptr, Test *obj, GVariant *var, gint64 i64, guint64 ui64)
660 {
661   all_type_handlers_count++;
662
663   g_assert_cmpint (i, ==, 42);
664   g_assert_cmpint (b, ==, TRUE);
665   g_assert_cmpint (c, ==, 17);
666   g_assert_cmpuint (uc, ==, 140);
667   g_assert_cmpuint (ui, ==, G_MAXUINT - 42);
668   g_assert_cmpint (l, ==, -1117);
669   g_assert_cmpuint (ul, ==, G_MAXULONG - 999);
670   g_assert_cmpint (e, ==, 1);
671   g_assert_cmpuint (f, ==, 0);
672   g_assert_cmpfloat (fl, ==, 0.25);
673   g_assert_cmpfloat (db, ==, 1.5);
674   g_assert_cmpstr (str, ==, "Test");
675   g_assert_cmpstr (g_param_spec_get_nick (param), ==, "nick");
676   g_assert_cmpstr (g_bytes_get_data (bytes, NULL), ==, "Blah");
677   g_assert (ptr == &enum_type);
678   g_assert_cmpuint (g_variant_get_uint16 (var), == , 99);
679   g_assert_cmpint (i64, ==, G_MAXINT64 - 1234);
680   g_assert_cmpuint (ui64, ==, G_MAXUINT64 - 123456);
681 }
682
683 static void
684 all_types_handler_cb (Test *test, int i, gboolean b, char c, guchar uc, guint ui, glong l, gulong ul, gint e, guint f, float fl, double db, char *str, GParamSpec *param, GBytes *bytes, gpointer ptr, Test *obj, GVariant *var, gint64 i64, guint64 ui64, gpointer user_data)
685 {
686   g_assert (user_data == &flags_type);
687   all_types_handler (test, i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, obj, var, i64, ui64);
688 }
689
690 static void
691 test_all_types (void)
692 {
693   Test *test;
694
695   int i = 42;
696   gboolean b = TRUE;
697   char c = 17;
698   guchar uc = 140;
699   guint ui = G_MAXUINT - 42;
700   glong l =  -1117;
701   gulong ul = G_MAXULONG - 999;
702   gint e = 1;
703   guint f = 0;
704   float fl = 0.25;
705   double db = 1.5;
706   char *str = "Test";
707   GParamSpec *param = g_param_spec_long  ("param", "nick", "blurb", 0, 10, 4, 0);
708   GBytes *bytes = g_bytes_new_static ("Blah", 5);
709   gpointer ptr = &enum_type;
710   GVariant *var = g_variant_new_uint16 (99);
711   gint64 i64;
712   guint64 ui64;
713   g_variant_ref_sink (var);
714   i64 = G_MAXINT64 - 1234;
715   ui64 = G_MAXUINT64 - 123456;
716
717   test = g_object_new (test_get_type (), NULL);
718
719   all_type_handlers_count = 0;
720
721   g_signal_emit_by_name (test, "all-types",
722                          i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
723   g_signal_emit_by_name (test, "all-types-va",
724                          i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
725   g_signal_emit_by_name (test, "all-types-generic",
726                          i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
727   g_signal_emit_by_name (test, "all-types-empty",
728                          i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
729   g_signal_emit_by_name (test, "all-types-null",
730                          i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
731
732   g_assert_cmpint (all_type_handlers_count, ==, 3);
733
734   all_type_handlers_count = 0;
735
736   g_signal_connect (test, "all-types", G_CALLBACK (all_types_handler_cb), &flags_type);
737   g_signal_connect (test, "all-types-va", G_CALLBACK (all_types_handler_cb), &flags_type);
738   g_signal_connect (test, "all-types-generic", G_CALLBACK (all_types_handler_cb), &flags_type);
739   g_signal_connect (test, "all-types-empty", G_CALLBACK (all_types_handler_cb), &flags_type);
740   g_signal_connect (test, "all-types-null", G_CALLBACK (all_types_handler_cb), &flags_type);
741
742   g_signal_emit_by_name (test, "all-types",
743                          i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
744   g_signal_emit_by_name (test, "all-types-va",
745                          i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
746   g_signal_emit_by_name (test, "all-types-generic",
747                          i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
748   g_signal_emit_by_name (test, "all-types-empty",
749                          i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
750   g_signal_emit_by_name (test, "all-types-null",
751                          i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
752
753   g_assert_cmpint (all_type_handlers_count, ==, 3 + 5);
754
755   all_type_handlers_count = 0;
756
757   g_signal_connect (test, "all-types", G_CALLBACK (all_types_handler_cb), &flags_type);
758   g_signal_connect (test, "all-types-va", G_CALLBACK (all_types_handler_cb), &flags_type);
759   g_signal_connect (test, "all-types-generic", G_CALLBACK (all_types_handler_cb), &flags_type);
760   g_signal_connect (test, "all-types-empty", G_CALLBACK (all_types_handler_cb), &flags_type);
761   g_signal_connect (test, "all-types-null", G_CALLBACK (all_types_handler_cb), &flags_type);
762
763   g_signal_emit_by_name (test, "all-types",
764                          i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
765   g_signal_emit_by_name (test, "all-types-va",
766                          i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
767   g_signal_emit_by_name (test, "all-types-generic",
768                          i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
769   g_signal_emit_by_name (test, "all-types-empty",
770                          i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
771   g_signal_emit_by_name (test, "all-types-null",
772                          i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
773
774   g_assert_cmpint (all_type_handlers_count, ==, 3 + 5 + 5);
775
776   g_object_unref (test);
777   g_param_spec_unref (param);
778   g_bytes_unref (bytes);
779   g_variant_unref (var);
780 }
781
782 static void
783 test_connect (void)
784 {
785   GObject *test;
786   gint retval;
787
788   test = g_object_new (test_get_type (), NULL);
789
790   g_object_connect (test,
791                     "signal::generic-marshaller-int-return",
792                     G_CALLBACK (on_generic_marshaller_int_return_signed_1),
793                     NULL,
794                     "object-signal::va-marshaller-int-return",
795                     G_CALLBACK (on_generic_marshaller_int_return_signed_2),
796                     NULL,
797                     NULL);
798   g_signal_emit_by_name (test, "generic-marshaller-int-return", &retval);
799   g_assert_cmpint (retval, ==, -30);
800   g_signal_emit_by_name (test, "va-marshaller-int-return", &retval);
801   g_assert_cmpint (retval, ==, 2);
802
803   g_object_disconnect (test,
804                        "any-signal",
805                        G_CALLBACK (on_generic_marshaller_int_return_signed_1),
806                        NULL,
807                        "any-signal::va-marshaller-int-return",
808                        G_CALLBACK (on_generic_marshaller_int_return_signed_2),
809                        NULL,
810                        NULL);
811
812   g_object_unref (test);
813 }
814
815 static void
816 simple_handler1 (GObject *sender,
817                  GObject *target)
818 {
819   g_object_unref (target);
820 }
821
822 static void
823 simple_handler2 (GObject *sender,
824                  GObject *target)
825 {
826   g_object_unref (target);
827 }
828
829 static void
830 test_destroy_target_object (void)
831 {
832   Test *sender, *target1, *target2;
833
834   sender = g_object_new (test_get_type (), NULL);
835   target1 = g_object_new (test_get_type (), NULL);
836   target2 = g_object_new (test_get_type (), NULL);
837   g_signal_connect_object (sender, "simple", G_CALLBACK (simple_handler1), target1, 0);
838   g_signal_connect_object (sender, "simple", G_CALLBACK (simple_handler2), target2, 0);
839   g_signal_emit_by_name (sender, "simple");
840   g_object_unref (sender);
841 }
842
843 static gboolean
844 hook_func (GSignalInvocationHint *ihint,
845            guint                  n_params,
846            const GValue          *params,
847            gpointer               data)
848 {
849   gint *count = data;
850
851   (*count)++;
852
853   return TRUE;
854 }
855
856 static void
857 test_emission_hook (void)
858 {
859   GObject *test1, *test2;
860   gint count = 0;
861   gulong hook;
862
863   test1 = g_object_new (test_get_type (), NULL);
864   test2 = g_object_new (test_get_type (), NULL);
865
866   hook = g_signal_add_emission_hook (simple_id, 0, hook_func, &count, NULL);
867   g_assert_cmpint (count, ==, 0);
868   g_signal_emit_by_name (test1, "simple");
869   g_assert_cmpint (count, ==, 1);
870   g_signal_emit_by_name (test2, "simple");
871   g_assert_cmpint (count, ==, 2);
872   g_signal_remove_emission_hook (simple_id, hook);
873   g_signal_emit_by_name (test1, "simple");
874   g_assert_cmpint (count, ==, 2);
875 }
876
877 static gboolean
878 in_set (const gchar *s,
879         const gchar *set[])
880 {
881   gint i;
882
883   for (i = 0; set[i]; i++)
884     {
885       if (g_strcmp0 (s, set[i]) == 0)
886         return TRUE;
887     }
888
889   return FALSE;
890 }
891
892 static void
893 test_introspection (void)
894 {
895   guint *ids;
896   guint n_ids;
897   const gchar *name;
898   gint i;
899   const gchar *names[] = {
900     "simple",
901     "generic-marshaller-1",
902     "generic-marshaller-2",
903     "generic-marshaller-enum-return-signed",
904     "generic-marshaller-enum-return-unsigned",
905     "generic-marshaller-int-return",
906     "va-marshaller-int-return",
907     "generic-marshaller-uint-return",
908     "va-marshaller-uint-return",
909     "variant-changed-no-slot",
910     "variant-changed",
911     "all-types",
912     "all-types-va",
913     "all-types-generic",
914     "all-types-null",
915     "all-types-empty",
916     NULL
917   };
918   GSignalQuery query;
919
920   ids = g_signal_list_ids (test_get_type (), &n_ids);
921   g_assert_cmpuint (n_ids, ==, g_strv_length ((gchar**)names));
922
923   for (i = 0; i < n_ids; i++)
924     {
925       name = g_signal_name (ids[i]);
926       g_assert (in_set (name, names));
927     }
928
929   g_signal_query (simple_id, &query);
930   g_assert_cmpuint (query.signal_id, ==, simple_id);
931   g_assert_cmpstr (query.signal_name, ==, "simple");
932   g_assert (query.itype == test_get_type ());
933   g_assert (query.signal_flags == G_SIGNAL_RUN_LAST);
934   g_assert (query.return_type == G_TYPE_NONE);
935   g_assert_cmpuint (query.n_params, ==, 0);
936 }
937
938 static void
939 test_handler (gpointer instance, gpointer data)
940 {
941   gint *count = data;
942
943   (*count)++;
944 }
945
946 static void
947 test_block_handler (void)
948 {
949   GObject *test1, *test2;
950   gint count1 = 0;
951   gint count2 = 0;
952   gulong handler1, handler;
953
954   test1 = g_object_new (test_get_type (), NULL);
955   test2 = g_object_new (test_get_type (), NULL);
956
957   handler1 = g_signal_connect (test1, "simple", G_CALLBACK (test_handler), &count1);
958   g_signal_connect (test2, "simple", G_CALLBACK (test_handler), &count2);
959
960   handler = g_signal_handler_find (test1, G_SIGNAL_MATCH_ID, simple_id, 0, NULL, NULL, NULL);
961
962   g_assert (handler == handler1);
963
964   g_assert_cmpint (count1, ==, 0);
965   g_assert_cmpint (count2, ==, 0);
966
967   g_signal_emit_by_name (test1, "simple");
968   g_signal_emit_by_name (test2, "simple");
969
970   g_assert_cmpint (count1, ==, 1);
971   g_assert_cmpint (count2, ==, 1);
972
973   g_signal_handler_block (test1, handler1);
974
975   g_signal_emit_by_name (test1, "simple");
976   g_signal_emit_by_name (test2, "simple");
977
978   g_assert_cmpint (count1, ==, 1);
979   g_assert_cmpint (count2, ==, 2);
980
981   g_signal_handler_unblock (test1, handler1);
982
983   g_signal_emit_by_name (test1, "simple");
984   g_signal_emit_by_name (test2, "simple");
985
986   g_assert_cmpint (count1, ==, 2);
987   g_assert_cmpint (count2, ==, 3);
988
989   g_assert_cmpuint (g_signal_handlers_block_matched (test1, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, test_block_handler, NULL), ==, 0);
990   g_assert_cmpuint (g_signal_handlers_block_matched (test2, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, test_handler, NULL), ==, 1);
991
992   g_signal_emit_by_name (test1, "simple");
993   g_signal_emit_by_name (test2, "simple");
994
995   g_assert_cmpint (count1, ==, 3);
996   g_assert_cmpint (count2, ==, 3);
997
998   g_signal_handlers_unblock_matched (test2, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, test_handler, NULL);
999
1000   g_object_unref (test1);
1001   g_object_unref (test2);
1002 }
1003
1004 static void
1005 stop_emission (gpointer instance, gpointer data)
1006 {
1007   g_signal_stop_emission (instance, simple_id, 0);
1008 }
1009
1010 static void
1011 stop_emission_by_name (gpointer instance, gpointer data)
1012 {
1013   g_signal_stop_emission_by_name (instance, "simple");
1014 }
1015
1016 static void
1017 dont_reach (gpointer instance, gpointer data)
1018 {
1019   g_assert_not_reached ();
1020 }
1021
1022 static void
1023 test_stop_emission (void)
1024 {
1025   GObject *test1;
1026   gulong handler;
1027
1028   test1 = g_object_new (test_get_type (), NULL);
1029   handler = g_signal_connect (test1, "simple", G_CALLBACK (stop_emission), NULL);
1030   g_signal_connect_after (test1, "simple", G_CALLBACK (dont_reach), NULL);
1031
1032   g_signal_emit_by_name (test1, "simple");
1033
1034   g_signal_handler_disconnect (test1, handler);
1035   g_signal_connect (test1, "simple", G_CALLBACK (stop_emission_by_name), NULL);
1036
1037   g_signal_emit_by_name (test1, "simple");
1038
1039   g_object_unref (test1);
1040 }
1041
1042 /* --- */
1043
1044 int
1045 main (int argc,
1046      char *argv[])
1047 {
1048   g_test_init (&argc, &argv, NULL);
1049
1050   g_test_add_func ("/gobject/signals/all-types", test_all_types);
1051   g_test_add_func ("/gobject/signals/variant", test_variant_signal);
1052   g_test_add_func ("/gobject/signals/destroy-target-object", test_destroy_target_object);
1053   g_test_add_func ("/gobject/signals/generic-marshaller-1", test_generic_marshaller_signal_1);
1054   g_test_add_func ("/gobject/signals/generic-marshaller-2", test_generic_marshaller_signal_2);
1055   g_test_add_func ("/gobject/signals/generic-marshaller-enum-return-signed", test_generic_marshaller_signal_enum_return_signed);
1056   g_test_add_func ("/gobject/signals/generic-marshaller-enum-return-unsigned", test_generic_marshaller_signal_enum_return_unsigned);
1057   g_test_add_func ("/gobject/signals/generic-marshaller-int-return", test_generic_marshaller_signal_int_return);
1058   g_test_add_func ("/gobject/signals/generic-marshaller-uint-return", test_generic_marshaller_signal_uint_return);
1059   g_test_add_func ("/gobject/signals/connect", test_connect);
1060   g_test_add_func ("/gobject/signals/emission-hook", test_emission_hook);
1061   g_test_add_func ("/gobject/signals/introspection", test_introspection);
1062   g_test_add_func ("/gobject/signals/block-handler", test_block_handler);
1063   g_test_add_func ("/gobject/signals/stop-emission", test_stop_emission);
1064
1065   return g_test_run ();
1066 }