Update Hungarian translation
[glib.git] / gobject / tests / signals.c
blob23dd49d62f323c6973915319c87461ad6b9fd377
1 #include <glib-object.h>
2 #include "marshalers.h"
4 #define g_assert_cmpflags(type,n1, cmp, n2) G_STMT_START { \
5 type __n1 = (n1), __n2 = (n2); \
6 if (__n1 cmp __n2) ; else \
7 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
8 #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'i'); \
9 } G_STMT_END
10 #define g_assert_cmpenum(type,n1, cmp, n2) G_STMT_START { \
11 type __n1 = (n1), __n2 = (n2); \
12 if (__n1 cmp __n2) ; else \
13 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
14 #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'i'); \
15 } G_STMT_END
17 typedef enum {
18 TEST_ENUM_NEGATIVE = -30,
19 TEST_ENUM_NONE = 0,
20 TEST_ENUM_FOO = 1,
21 TEST_ENUM_BAR = 2
22 } TestEnum;
24 typedef enum {
25 TEST_UNSIGNED_ENUM_FOO = 1,
26 TEST_UNSIGNED_ENUM_BAR = 42
27 /* Don't test 0x80000000 for now- nothing appears to do this in
28 * practice, and it triggers GValue/GEnum bugs on ppc64.
30 } TestUnsignedEnum;
32 static void
33 custom_marshal_VOID__INVOCATIONHINT (GClosure *closure,
34 GValue *return_value G_GNUC_UNUSED,
35 guint n_param_values,
36 const GValue *param_values,
37 gpointer invocation_hint,
38 gpointer marshal_data)
40 typedef void (*GMarshalFunc_VOID__INVOCATIONHINT) (gpointer data1,
41 gpointer invocation_hint,
42 gpointer data2);
43 GMarshalFunc_VOID__INVOCATIONHINT callback;
44 GCClosure *cc = (GCClosure*) closure;
45 gpointer data1, data2;
47 g_return_if_fail (n_param_values == 2);
49 if (G_CCLOSURE_SWAP_DATA (closure))
51 data1 = closure->data;
52 data2 = g_value_peek_pointer (param_values + 0);
54 else
56 data1 = g_value_peek_pointer (param_values + 0);
57 data2 = closure->data;
59 callback = (GMarshalFunc_VOID__INVOCATIONHINT) (marshal_data ? marshal_data : cc->callback);
61 callback (data1,
62 invocation_hint,
63 data2);
66 static GType
67 test_enum_get_type (void)
69 static volatile gsize g_define_type_id__volatile = 0;
71 if (g_once_init_enter (&g_define_type_id__volatile))
73 static const GEnumValue values[] = {
74 { TEST_ENUM_NEGATIVE, "TEST_ENUM_NEGATIVE", "negative" },
75 { TEST_ENUM_NONE, "TEST_ENUM_NONE", "none" },
76 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
77 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
78 { 0, NULL, NULL }
80 GType g_define_type_id =
81 g_enum_register_static (g_intern_static_string ("TestEnum"), values);
82 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
85 return g_define_type_id__volatile;
88 static GType
89 test_unsigned_enum_get_type (void)
91 static volatile gsize g_define_type_id__volatile = 0;
93 if (g_once_init_enter (&g_define_type_id__volatile))
95 static const GEnumValue values[] = {
96 { TEST_UNSIGNED_ENUM_FOO, "TEST_UNSIGNED_ENUM_FOO", "foo" },
97 { TEST_UNSIGNED_ENUM_BAR, "TEST_UNSIGNED_ENUM_BAR", "bar" },
98 { 0, NULL, NULL }
100 GType g_define_type_id =
101 g_enum_register_static (g_intern_static_string ("TestUnsignedEnum"), values);
102 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
105 return g_define_type_id__volatile;
108 typedef enum {
109 MY_ENUM_VALUE = 1,
110 } MyEnum;
112 static const GEnumValue my_enum_values[] =
114 { MY_ENUM_VALUE, "the first value", "one" },
115 { 0, NULL, NULL }
118 typedef enum {
119 MY_FLAGS_FIRST_BIT = (1 << 0),
120 MY_FLAGS_THIRD_BIT = (1 << 2),
121 MY_FLAGS_LAST_BIT = (1 << 31)
122 } MyFlags;
124 static const GFlagsValue my_flag_values[] =
126 { MY_FLAGS_FIRST_BIT, "the first bit", "first-bit" },
127 { MY_FLAGS_THIRD_BIT, "the third bit", "third-bit" },
128 { MY_FLAGS_LAST_BIT, "the last bit", "last-bit" },
129 { 0, NULL, NULL }
132 static GType enum_type;
133 static GType flags_type;
135 static guint simple_id;
136 static guint simple2_id;
138 typedef struct _Test Test;
139 typedef struct _TestClass TestClass;
141 struct _Test
143 GObject parent_instance;
146 static void all_types_handler (Test *test, int i, gboolean b, char c, guchar uc, guint ui, glong l, gulong ul, MyEnum e, MyFlags f, float fl, double db, char *str, GParamSpec *param, GBytes *bytes, gpointer ptr, Test *obj, GVariant *var, gint64 i64, guint64 ui64);
148 struct _TestClass
150 GObjectClass parent_class;
152 void (* variant_changed) (Test *, GVariant *);
153 void (* all_types) (Test *test, int i, gboolean b, char c, guchar uc, guint ui, glong l, gulong ul, MyEnum e, MyFlags f, float fl, double db, char *str, GParamSpec *param, GBytes *bytes, gpointer ptr, Test *obj, GVariant *var, gint64 i64, guint64 ui64);
154 void (* all_types_null) (Test *test, int i, gboolean b, char c, guchar uc, guint ui, glong l, gulong ul, MyEnum e, MyFlags f, float fl, double db, char *str, GParamSpec *param, GBytes *bytes, gpointer ptr, Test *obj, GVariant *var, gint64 i64, guint64 ui64);
157 static GType test_get_type (void);
158 G_DEFINE_TYPE (Test, test, G_TYPE_OBJECT)
160 static void
161 test_init (Test *test)
165 static void
166 test_class_init (TestClass *klass)
168 guint s;
170 enum_type = g_enum_register_static ("MyEnum", my_enum_values);
171 flags_type = g_flags_register_static ("MyFlag", my_flag_values);
173 klass->all_types = all_types_handler;
175 simple_id = g_signal_new ("simple",
176 G_TYPE_FROM_CLASS (klass),
177 G_SIGNAL_RUN_LAST,
179 NULL, NULL,
180 NULL,
181 G_TYPE_NONE,
183 simple2_id = g_signal_new ("simple-2",
184 G_TYPE_FROM_CLASS (klass),
185 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE,
187 NULL, NULL,
188 NULL,
189 G_TYPE_NONE,
191 g_signal_new ("generic-marshaller-1",
192 G_TYPE_FROM_CLASS (klass),
193 G_SIGNAL_RUN_LAST,
195 NULL, NULL,
196 NULL,
197 G_TYPE_NONE,
199 G_TYPE_CHAR, G_TYPE_UCHAR, G_TYPE_INT, G_TYPE_LONG, G_TYPE_POINTER, G_TYPE_DOUBLE, G_TYPE_FLOAT);
200 g_signal_new ("generic-marshaller-2",
201 G_TYPE_FROM_CLASS (klass),
202 G_SIGNAL_RUN_LAST,
204 NULL, NULL,
205 NULL,
206 G_TYPE_NONE,
208 G_TYPE_INT, test_enum_get_type(), G_TYPE_INT, test_unsigned_enum_get_type (), G_TYPE_INT);
209 g_signal_new ("generic-marshaller-enum-return-signed",
210 G_TYPE_FROM_CLASS (klass),
211 G_SIGNAL_RUN_LAST,
213 NULL, NULL,
214 NULL,
215 test_enum_get_type(),
217 g_signal_new ("generic-marshaller-enum-return-unsigned",
218 G_TYPE_FROM_CLASS (klass),
219 G_SIGNAL_RUN_LAST,
221 NULL, NULL,
222 NULL,
223 test_unsigned_enum_get_type(),
225 g_signal_new ("generic-marshaller-int-return",
226 G_TYPE_FROM_CLASS (klass),
227 G_SIGNAL_RUN_LAST,
229 NULL, NULL,
230 NULL,
231 G_TYPE_INT,
233 s = g_signal_new ("va-marshaller-int-return",
234 G_TYPE_FROM_CLASS (klass),
235 G_SIGNAL_RUN_LAST,
237 NULL, NULL,
238 test_INT__VOID,
239 G_TYPE_INT,
241 g_signal_set_va_marshaller (s, G_TYPE_FROM_CLASS (klass),
242 test_INT__VOIDv);
243 g_signal_new ("generic-marshaller-uint-return",
244 G_TYPE_FROM_CLASS (klass),
245 G_SIGNAL_RUN_LAST,
247 NULL, NULL,
248 NULL,
249 G_TYPE_UINT,
251 s = g_signal_new ("va-marshaller-uint-return",
252 G_TYPE_FROM_CLASS (klass),
253 G_SIGNAL_RUN_LAST,
255 NULL, NULL,
256 test_INT__VOID,
257 G_TYPE_UINT,
259 g_signal_set_va_marshaller (s, G_TYPE_FROM_CLASS (klass),
260 test_UINT__VOIDv);
261 g_signal_new ("custom-marshaller",
262 G_TYPE_FROM_CLASS (klass),
263 G_SIGNAL_RUN_LAST,
265 NULL, NULL,
266 custom_marshal_VOID__INVOCATIONHINT,
267 G_TYPE_NONE,
269 G_TYPE_POINTER);
270 g_signal_new ("variant-changed-no-slot",
271 G_TYPE_FROM_CLASS (klass),
272 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
274 NULL, NULL,
275 g_cclosure_marshal_VOID__VARIANT,
276 G_TYPE_NONE,
278 G_TYPE_VARIANT);
279 g_signal_new ("variant-changed",
280 G_TYPE_FROM_CLASS (klass),
281 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
282 G_STRUCT_OFFSET (TestClass, variant_changed),
283 NULL, NULL,
284 g_cclosure_marshal_VOID__VARIANT,
285 G_TYPE_NONE,
287 G_TYPE_VARIANT);
288 g_signal_new ("all-types",
289 G_TYPE_FROM_CLASS (klass),
290 G_SIGNAL_RUN_LAST,
291 G_STRUCT_OFFSET (TestClass, all_types),
292 NULL, NULL,
293 test_VOID__INT_BOOLEAN_CHAR_UCHAR_UINT_LONG_ULONG_ENUM_FLAGS_FLOAT_DOUBLE_STRING_PARAM_BOXED_POINTER_OBJECT_VARIANT_INT64_UINT64,
294 G_TYPE_NONE,
296 G_TYPE_INT,
297 G_TYPE_BOOLEAN,
298 G_TYPE_CHAR,
299 G_TYPE_UCHAR,
300 G_TYPE_UINT,
301 G_TYPE_LONG,
302 G_TYPE_ULONG,
303 enum_type,
304 flags_type,
305 G_TYPE_FLOAT,
306 G_TYPE_DOUBLE,
307 G_TYPE_STRING,
308 G_TYPE_PARAM_LONG,
309 G_TYPE_BYTES,
310 G_TYPE_POINTER,
311 test_get_type (),
312 G_TYPE_VARIANT,
313 G_TYPE_INT64,
314 G_TYPE_UINT64);
315 s = g_signal_new ("all-types-va",
316 G_TYPE_FROM_CLASS (klass),
317 G_SIGNAL_RUN_LAST,
318 G_STRUCT_OFFSET (TestClass, all_types),
319 NULL, NULL,
320 test_VOID__INT_BOOLEAN_CHAR_UCHAR_UINT_LONG_ULONG_ENUM_FLAGS_FLOAT_DOUBLE_STRING_PARAM_BOXED_POINTER_OBJECT_VARIANT_INT64_UINT64,
321 G_TYPE_NONE,
323 G_TYPE_INT,
324 G_TYPE_BOOLEAN,
325 G_TYPE_CHAR,
326 G_TYPE_UCHAR,
327 G_TYPE_UINT,
328 G_TYPE_LONG,
329 G_TYPE_ULONG,
330 enum_type,
331 flags_type,
332 G_TYPE_FLOAT,
333 G_TYPE_DOUBLE,
334 G_TYPE_STRING,
335 G_TYPE_PARAM_LONG,
336 G_TYPE_BYTES,
337 G_TYPE_POINTER,
338 test_get_type (),
339 G_TYPE_VARIANT,
340 G_TYPE_INT64,
341 G_TYPE_UINT64);
342 g_signal_set_va_marshaller (s, G_TYPE_FROM_CLASS (klass),
343 test_VOID__INT_BOOLEAN_CHAR_UCHAR_UINT_LONG_ULONG_ENUM_FLAGS_FLOAT_DOUBLE_STRING_PARAM_BOXED_POINTER_OBJECT_VARIANT_INT64_UINT64v);
345 g_signal_new ("all-types-generic",
346 G_TYPE_FROM_CLASS (klass),
347 G_SIGNAL_RUN_LAST,
348 G_STRUCT_OFFSET (TestClass, all_types),
349 NULL, NULL,
350 NULL,
351 G_TYPE_NONE,
353 G_TYPE_INT,
354 G_TYPE_BOOLEAN,
355 G_TYPE_CHAR,
356 G_TYPE_UCHAR,
357 G_TYPE_UINT,
358 G_TYPE_LONG,
359 G_TYPE_ULONG,
360 enum_type,
361 flags_type,
362 G_TYPE_FLOAT,
363 G_TYPE_DOUBLE,
364 G_TYPE_STRING,
365 G_TYPE_PARAM_LONG,
366 G_TYPE_BYTES,
367 G_TYPE_POINTER,
368 test_get_type (),
369 G_TYPE_VARIANT,
370 G_TYPE_INT64,
371 G_TYPE_UINT64);
372 g_signal_new ("all-types-null",
373 G_TYPE_FROM_CLASS (klass),
374 G_SIGNAL_RUN_LAST,
375 G_STRUCT_OFFSET (TestClass, all_types_null),
376 NULL, NULL,
377 test_VOID__INT_BOOLEAN_CHAR_UCHAR_UINT_LONG_ULONG_ENUM_FLAGS_FLOAT_DOUBLE_STRING_PARAM_BOXED_POINTER_OBJECT_VARIANT_INT64_UINT64,
378 G_TYPE_NONE,
380 G_TYPE_INT,
381 G_TYPE_BOOLEAN,
382 G_TYPE_CHAR,
383 G_TYPE_UCHAR,
384 G_TYPE_UINT,
385 G_TYPE_LONG,
386 G_TYPE_ULONG,
387 enum_type,
388 flags_type,
389 G_TYPE_FLOAT,
390 G_TYPE_DOUBLE,
391 G_TYPE_STRING,
392 G_TYPE_PARAM_LONG,
393 G_TYPE_BYTES,
394 G_TYPE_POINTER,
395 test_get_type (),
396 G_TYPE_VARIANT,
397 G_TYPE_INT64,
398 G_TYPE_UINT64);
399 g_signal_new ("all-types-empty",
400 G_TYPE_FROM_CLASS (klass),
401 G_SIGNAL_RUN_LAST,
403 NULL, NULL,
404 test_VOID__INT_BOOLEAN_CHAR_UCHAR_UINT_LONG_ULONG_ENUM_FLAGS_FLOAT_DOUBLE_STRING_PARAM_BOXED_POINTER_OBJECT_VARIANT_INT64_UINT64,
405 G_TYPE_NONE,
407 G_TYPE_INT,
408 G_TYPE_BOOLEAN,
409 G_TYPE_CHAR,
410 G_TYPE_UCHAR,
411 G_TYPE_UINT,
412 G_TYPE_LONG,
413 G_TYPE_ULONG,
414 enum_type,
415 flags_type,
416 G_TYPE_FLOAT,
417 G_TYPE_DOUBLE,
418 G_TYPE_STRING,
419 G_TYPE_PARAM_LONG,
420 G_TYPE_BYTES,
421 G_TYPE_POINTER,
422 test_get_type (),
423 G_TYPE_VARIANT,
424 G_TYPE_INT64,
425 G_TYPE_UINT64);
428 typedef struct _Test Test2;
429 typedef struct _TestClass Test2Class;
431 static GType test2_get_type (void);
432 G_DEFINE_TYPE (Test2, test2, G_TYPE_OBJECT)
434 static void
435 test2_init (Test2 *test)
439 static void
440 test2_class_init (Test2Class *klass)
444 static void
445 test_variant_signal (void)
447 Test *test;
448 GVariant *v;
450 /* Tests that the signal emission consumes the variant,
451 * even if there are no handlers connected.
454 test = g_object_new (test_get_type (), NULL);
456 v = g_variant_new_boolean (TRUE);
457 g_variant_ref (v);
458 g_assert (g_variant_is_floating (v));
459 g_signal_emit_by_name (test, "variant-changed-no-slot", v);
460 g_assert (!g_variant_is_floating (v));
461 g_variant_unref (v);
463 v = g_variant_new_boolean (TRUE);
464 g_variant_ref (v);
465 g_assert (g_variant_is_floating (v));
466 g_signal_emit_by_name (test, "variant-changed", v);
467 g_assert (!g_variant_is_floating (v));
468 g_variant_unref (v);
470 g_object_unref (test);
473 static void
474 on_generic_marshaller_1 (Test *obj,
475 gint8 v_schar,
476 guint8 v_uchar,
477 gint v_int,
478 glong v_long,
479 gpointer v_pointer,
480 gdouble v_double,
481 gfloat v_float,
482 gpointer user_data)
484 g_assert_cmpint (v_schar, ==, 42);
485 g_assert_cmpint (v_uchar, ==, 43);
486 g_assert_cmpint (v_int, ==, 4096);
487 g_assert_cmpint (v_long, ==, 8192);
488 g_assert (v_pointer == NULL);
489 g_assert_cmpfloat (v_double, >, 0.0);
490 g_assert_cmpfloat (v_double, <, 1.0);
491 g_assert_cmpfloat (v_float, >, 5.0);
492 g_assert_cmpfloat (v_float, <, 6.0);
495 static void
496 test_generic_marshaller_signal_1 (void)
498 Test *test;
499 test = g_object_new (test_get_type (), NULL);
501 g_signal_connect (test, "generic-marshaller-1", G_CALLBACK (on_generic_marshaller_1), NULL);
503 g_signal_emit_by_name (test, "generic-marshaller-1", 42, 43, 4096, 8192, NULL, 0.5, 5.5);
505 g_object_unref (test);
508 static void
509 on_generic_marshaller_2 (Test *obj,
510 gint v_int1,
511 TestEnum v_enum,
512 gint v_int2,
513 TestUnsignedEnum v_uenum,
514 gint v_int3)
516 g_assert_cmpint (v_int1, ==, 42);
517 g_assert_cmpint (v_enum, ==, TEST_ENUM_BAR);
518 g_assert_cmpint (v_int2, ==, 43);
519 g_assert_cmpint (v_uenum, ==, TEST_UNSIGNED_ENUM_BAR);
520 g_assert_cmpint (v_int3, ==, 44);
523 static void
524 test_generic_marshaller_signal_2 (void)
526 Test *test;
527 test = g_object_new (test_get_type (), NULL);
529 g_signal_connect (test, "generic-marshaller-2", G_CALLBACK (on_generic_marshaller_2), NULL);
531 g_signal_emit_by_name (test, "generic-marshaller-2", 42, TEST_ENUM_BAR, 43, TEST_UNSIGNED_ENUM_BAR, 44);
533 g_object_unref (test);
536 static TestEnum
537 on_generic_marshaller_enum_return_signed_1 (Test *obj)
539 return TEST_ENUM_NEGATIVE;
542 static TestEnum
543 on_generic_marshaller_enum_return_signed_2 (Test *obj)
545 return TEST_ENUM_BAR;
548 static void
549 test_generic_marshaller_signal_enum_return_signed (void)
551 Test *test;
552 guint id;
553 TestEnum retval = 0;
555 test = g_object_new (test_get_type (), NULL);
557 /* Test return value NEGATIVE */
558 id = g_signal_connect (test,
559 "generic-marshaller-enum-return-signed",
560 G_CALLBACK (on_generic_marshaller_enum_return_signed_1),
561 NULL);
562 g_signal_emit_by_name (test, "generic-marshaller-enum-return-signed", &retval);
563 g_assert_cmpint (retval, ==, TEST_ENUM_NEGATIVE);
564 g_signal_handler_disconnect (test, id);
566 /* Test return value BAR */
567 retval = 0;
568 id = g_signal_connect (test,
569 "generic-marshaller-enum-return-signed",
570 G_CALLBACK (on_generic_marshaller_enum_return_signed_2),
571 NULL);
572 g_signal_emit_by_name (test, "generic-marshaller-enum-return-signed", &retval);
573 g_assert_cmpint (retval, ==, TEST_ENUM_BAR);
574 g_signal_handler_disconnect (test, id);
576 g_object_unref (test);
579 static TestUnsignedEnum
580 on_generic_marshaller_enum_return_unsigned_1 (Test *obj)
582 return TEST_UNSIGNED_ENUM_FOO;
585 static TestUnsignedEnum
586 on_generic_marshaller_enum_return_unsigned_2 (Test *obj)
588 return TEST_UNSIGNED_ENUM_BAR;
591 static void
592 test_generic_marshaller_signal_enum_return_unsigned (void)
594 Test *test;
595 guint id;
596 TestUnsignedEnum retval = 0;
598 test = g_object_new (test_get_type (), NULL);
600 /* Test return value FOO */
601 id = g_signal_connect (test,
602 "generic-marshaller-enum-return-unsigned",
603 G_CALLBACK (on_generic_marshaller_enum_return_unsigned_1),
604 NULL);
605 g_signal_emit_by_name (test, "generic-marshaller-enum-return-unsigned", &retval);
606 g_assert_cmpint (retval, ==, TEST_UNSIGNED_ENUM_FOO);
607 g_signal_handler_disconnect (test, id);
609 /* Test return value BAR */
610 retval = 0;
611 id = g_signal_connect (test,
612 "generic-marshaller-enum-return-unsigned",
613 G_CALLBACK (on_generic_marshaller_enum_return_unsigned_2),
614 NULL);
615 g_signal_emit_by_name (test, "generic-marshaller-enum-return-unsigned", &retval);
616 g_assert_cmpint (retval, ==, TEST_UNSIGNED_ENUM_BAR);
617 g_signal_handler_disconnect (test, id);
619 g_object_unref (test);
622 /**********************/
624 static gint
625 on_generic_marshaller_int_return_signed_1 (Test *obj)
627 return -30;
630 static gint
631 on_generic_marshaller_int_return_signed_2 (Test *obj)
633 return 2;
636 static void
637 test_generic_marshaller_signal_int_return (void)
639 Test *test;
640 guint id;
641 gint retval = 0;
643 test = g_object_new (test_get_type (), NULL);
645 /* Test return value -30 */
646 id = g_signal_connect (test,
647 "generic-marshaller-int-return",
648 G_CALLBACK (on_generic_marshaller_int_return_signed_1),
649 NULL);
650 g_signal_emit_by_name (test, "generic-marshaller-int-return", &retval);
651 g_assert_cmpint (retval, ==, -30);
652 g_signal_handler_disconnect (test, id);
654 /* Test return value positive */
655 retval = 0;
656 id = g_signal_connect (test,
657 "generic-marshaller-int-return",
658 G_CALLBACK (on_generic_marshaller_int_return_signed_2),
659 NULL);
660 g_signal_emit_by_name (test, "generic-marshaller-int-return", &retval);
661 g_assert_cmpint (retval, ==, 2);
662 g_signal_handler_disconnect (test, id);
664 /* Same test for va marshaller */
666 /* Test return value -30 */
667 id = g_signal_connect (test,
668 "va-marshaller-int-return",
669 G_CALLBACK (on_generic_marshaller_int_return_signed_1),
670 NULL);
671 g_signal_emit_by_name (test, "va-marshaller-int-return", &retval);
672 g_assert_cmpint (retval, ==, -30);
673 g_signal_handler_disconnect (test, id);
675 /* Test return value positive */
676 retval = 0;
677 id = g_signal_connect (test,
678 "va-marshaller-int-return",
679 G_CALLBACK (on_generic_marshaller_int_return_signed_2),
680 NULL);
681 g_signal_emit_by_name (test, "va-marshaller-int-return", &retval);
682 g_assert_cmpint (retval, ==, 2);
683 g_signal_handler_disconnect (test, id);
685 g_object_unref (test);
688 static guint
689 on_generic_marshaller_uint_return_1 (Test *obj)
691 return 1;
694 static guint
695 on_generic_marshaller_uint_return_2 (Test *obj)
697 return G_MAXUINT;
700 static void
701 test_generic_marshaller_signal_uint_return (void)
703 Test *test;
704 guint id;
705 guint retval = 0;
707 test = g_object_new (test_get_type (), NULL);
709 id = g_signal_connect (test,
710 "generic-marshaller-uint-return",
711 G_CALLBACK (on_generic_marshaller_uint_return_1),
712 NULL);
713 g_signal_emit_by_name (test, "generic-marshaller-uint-return", &retval);
714 g_assert_cmpint (retval, ==, 1);
715 g_signal_handler_disconnect (test, id);
717 retval = 0;
718 id = g_signal_connect (test,
719 "generic-marshaller-uint-return",
720 G_CALLBACK (on_generic_marshaller_uint_return_2),
721 NULL);
722 g_signal_emit_by_name (test, "generic-marshaller-uint-return", &retval);
723 g_assert_cmpint (retval, ==, G_MAXUINT);
724 g_signal_handler_disconnect (test, id);
726 /* Same test for va marshaller */
728 id = g_signal_connect (test,
729 "va-marshaller-uint-return",
730 G_CALLBACK (on_generic_marshaller_uint_return_1),
731 NULL);
732 g_signal_emit_by_name (test, "va-marshaller-uint-return", &retval);
733 g_assert_cmpint (retval, ==, 1);
734 g_signal_handler_disconnect (test, id);
736 retval = 0;
737 id = g_signal_connect (test,
738 "va-marshaller-uint-return",
739 G_CALLBACK (on_generic_marshaller_uint_return_2),
740 NULL);
741 g_signal_emit_by_name (test, "va-marshaller-uint-return", &retval);
742 g_assert_cmpint (retval, ==, G_MAXUINT);
743 g_signal_handler_disconnect (test, id);
745 g_object_unref (test);
748 static const GSignalInvocationHint dont_use_this = { 0, };
750 static void
751 custom_marshaller_callback (Test *test,
752 GSignalInvocationHint *hint,
753 gpointer unused)
755 GSignalInvocationHint *ihint;
757 g_assert (hint != &dont_use_this);
759 ihint = g_signal_get_invocation_hint (test);
761 g_assert_cmpuint (hint->signal_id, ==, ihint->signal_id);
762 g_assert_cmpuint (hint->detail , ==, ihint->detail);
763 g_assert_cmpflags (GSignalFlags, hint->run_type, ==, ihint->run_type);
766 static void
767 test_custom_marshaller (void)
769 Test *test;
771 test = g_object_new (test_get_type (), NULL);
773 g_signal_connect (test,
774 "custom-marshaller",
775 G_CALLBACK (custom_marshaller_callback),
776 NULL);
778 g_signal_emit_by_name (test, "custom-marshaller", &dont_use_this);
780 g_object_unref (test);
783 static int all_type_handlers_count = 0;
785 static void
786 all_types_handler (Test *test, int i, gboolean b, char c, guchar uc, guint ui, glong l, gulong ul, MyEnum e, MyFlags f, float fl, double db, char *str, GParamSpec *param, GBytes *bytes, gpointer ptr, Test *obj, GVariant *var, gint64 i64, guint64 ui64)
788 all_type_handlers_count++;
790 g_assert_cmpint (i, ==, 42);
791 g_assert_cmpint (b, ==, TRUE);
792 g_assert_cmpint (c, ==, 17);
793 g_assert_cmpuint (uc, ==, 140);
794 g_assert_cmpuint (ui, ==, G_MAXUINT - 42);
795 g_assert_cmpint (l, ==, -1117);
796 g_assert_cmpuint (ul, ==, G_MAXULONG - 999);
797 g_assert_cmpenum (MyEnum, e, ==, MY_ENUM_VALUE);
798 g_assert_cmpflags (MyFlags, f, ==, MY_FLAGS_FIRST_BIT | MY_FLAGS_THIRD_BIT | MY_FLAGS_LAST_BIT);
799 g_assert_cmpfloat (fl, ==, 0.25);
800 g_assert_cmpfloat (db, ==, 1.5);
801 g_assert_cmpstr (str, ==, "Test");
802 g_assert_cmpstr (g_param_spec_get_nick (param), ==, "nick");
803 g_assert_cmpstr (g_bytes_get_data (bytes, NULL), ==, "Blah");
804 g_assert (ptr == &enum_type);
805 g_assert_cmpuint (g_variant_get_uint16 (var), == , 99);
806 g_assert_cmpint (i64, ==, G_MAXINT64 - 1234);
807 g_assert_cmpuint (ui64, ==, G_MAXUINT64 - 123456);
810 static void
811 all_types_handler_cb (Test *test, int i, gboolean b, char c, guchar uc, guint ui, glong l, gulong ul, MyEnum 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)
813 g_assert (user_data == &flags_type);
814 all_types_handler (test, i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, obj, var, i64, ui64);
817 static void
818 test_all_types (void)
820 Test *test;
822 int i = 42;
823 gboolean b = TRUE;
824 char c = 17;
825 guchar uc = 140;
826 guint ui = G_MAXUINT - 42;
827 glong l = -1117;
828 gulong ul = G_MAXULONG - 999;
829 MyEnum e = MY_ENUM_VALUE;
830 MyFlags f = MY_FLAGS_FIRST_BIT | MY_FLAGS_THIRD_BIT | MY_FLAGS_LAST_BIT;
831 float fl = 0.25;
832 double db = 1.5;
833 char *str = "Test";
834 GParamSpec *param = g_param_spec_long ("param", "nick", "blurb", 0, 10, 4, 0);
835 GBytes *bytes = g_bytes_new_static ("Blah", 5);
836 gpointer ptr = &enum_type;
837 GVariant *var = g_variant_new_uint16 (99);
838 gint64 i64;
839 guint64 ui64;
840 g_variant_ref_sink (var);
841 i64 = G_MAXINT64 - 1234;
842 ui64 = G_MAXUINT64 - 123456;
844 test = g_object_new (test_get_type (), NULL);
846 all_type_handlers_count = 0;
848 g_signal_emit_by_name (test, "all-types",
849 i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
850 g_signal_emit_by_name (test, "all-types-va",
851 i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
852 g_signal_emit_by_name (test, "all-types-generic",
853 i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
854 g_signal_emit_by_name (test, "all-types-empty",
855 i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
856 g_signal_emit_by_name (test, "all-types-null",
857 i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
859 g_assert_cmpint (all_type_handlers_count, ==, 3);
861 all_type_handlers_count = 0;
863 g_signal_connect (test, "all-types", G_CALLBACK (all_types_handler_cb), &flags_type);
864 g_signal_connect (test, "all-types-va", G_CALLBACK (all_types_handler_cb), &flags_type);
865 g_signal_connect (test, "all-types-generic", G_CALLBACK (all_types_handler_cb), &flags_type);
866 g_signal_connect (test, "all-types-empty", G_CALLBACK (all_types_handler_cb), &flags_type);
867 g_signal_connect (test, "all-types-null", G_CALLBACK (all_types_handler_cb), &flags_type);
869 g_signal_emit_by_name (test, "all-types",
870 i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
871 g_signal_emit_by_name (test, "all-types-va",
872 i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
873 g_signal_emit_by_name (test, "all-types-generic",
874 i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
875 g_signal_emit_by_name (test, "all-types-empty",
876 i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
877 g_signal_emit_by_name (test, "all-types-null",
878 i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
880 g_assert_cmpint (all_type_handlers_count, ==, 3 + 5);
882 all_type_handlers_count = 0;
884 g_signal_connect (test, "all-types", G_CALLBACK (all_types_handler_cb), &flags_type);
885 g_signal_connect (test, "all-types-va", G_CALLBACK (all_types_handler_cb), &flags_type);
886 g_signal_connect (test, "all-types-generic", G_CALLBACK (all_types_handler_cb), &flags_type);
887 g_signal_connect (test, "all-types-empty", G_CALLBACK (all_types_handler_cb), &flags_type);
888 g_signal_connect (test, "all-types-null", G_CALLBACK (all_types_handler_cb), &flags_type);
890 g_signal_emit_by_name (test, "all-types",
891 i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
892 g_signal_emit_by_name (test, "all-types-va",
893 i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
894 g_signal_emit_by_name (test, "all-types-generic",
895 i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
896 g_signal_emit_by_name (test, "all-types-empty",
897 i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
898 g_signal_emit_by_name (test, "all-types-null",
899 i, b, c, uc, ui, l, ul, e, f, fl, db, str, param, bytes, ptr, test, var, i64, ui64);
901 g_assert_cmpint (all_type_handlers_count, ==, 3 + 5 + 5);
903 g_object_unref (test);
904 g_param_spec_unref (param);
905 g_bytes_unref (bytes);
906 g_variant_unref (var);
909 static void
910 test_connect (void)
912 GObject *test;
913 gint retval;
915 test = g_object_new (test_get_type (), NULL);
917 g_object_connect (test,
918 "signal::generic-marshaller-int-return",
919 G_CALLBACK (on_generic_marshaller_int_return_signed_1),
920 NULL,
921 "object-signal::va-marshaller-int-return",
922 G_CALLBACK (on_generic_marshaller_int_return_signed_2),
923 NULL,
924 NULL);
925 g_signal_emit_by_name (test, "generic-marshaller-int-return", &retval);
926 g_assert_cmpint (retval, ==, -30);
927 g_signal_emit_by_name (test, "va-marshaller-int-return", &retval);
928 g_assert_cmpint (retval, ==, 2);
930 g_object_disconnect (test,
931 "any-signal",
932 G_CALLBACK (on_generic_marshaller_int_return_signed_1),
933 NULL,
934 "any-signal::va-marshaller-int-return",
935 G_CALLBACK (on_generic_marshaller_int_return_signed_2),
936 NULL,
937 NULL);
939 g_object_unref (test);
942 static void
943 simple_handler1 (GObject *sender,
944 GObject *target)
946 g_object_unref (target);
949 static void
950 simple_handler2 (GObject *sender,
951 GObject *target)
953 g_object_unref (target);
956 static void
957 test_destroy_target_object (void)
959 Test *sender, *target1, *target2;
961 sender = g_object_new (test_get_type (), NULL);
962 target1 = g_object_new (test_get_type (), NULL);
963 target2 = g_object_new (test_get_type (), NULL);
964 g_signal_connect_object (sender, "simple", G_CALLBACK (simple_handler1), target1, 0);
965 g_signal_connect_object (sender, "simple", G_CALLBACK (simple_handler2), target2, 0);
966 g_signal_emit_by_name (sender, "simple");
967 g_object_unref (sender);
970 static gboolean
971 hook_func (GSignalInvocationHint *ihint,
972 guint n_params,
973 const GValue *params,
974 gpointer data)
976 gint *count = data;
978 (*count)++;
980 return TRUE;
983 static void
984 test_emission_hook (void)
986 GObject *test1, *test2;
987 gint count = 0;
988 gulong hook;
990 test1 = g_object_new (test_get_type (), NULL);
991 test2 = g_object_new (test_get_type (), NULL);
993 hook = g_signal_add_emission_hook (simple_id, 0, hook_func, &count, NULL);
994 g_assert_cmpint (count, ==, 0);
995 g_signal_emit_by_name (test1, "simple");
996 g_assert_cmpint (count, ==, 1);
997 g_signal_emit_by_name (test2, "simple");
998 g_assert_cmpint (count, ==, 2);
999 g_signal_remove_emission_hook (simple_id, hook);
1000 g_signal_emit_by_name (test1, "simple");
1001 g_assert_cmpint (count, ==, 2);
1003 g_object_unref (test1);
1004 g_object_unref (test2);
1007 static void
1008 simple_cb (gpointer instance, gpointer data)
1010 GSignalInvocationHint *ihint;
1012 ihint = g_signal_get_invocation_hint (instance);
1014 g_assert_cmpstr (g_signal_name (ihint->signal_id), ==, "simple");
1016 g_signal_emit_by_name (instance, "simple-2");
1019 static void
1020 simple2_cb (gpointer instance, gpointer data)
1022 GSignalInvocationHint *ihint;
1024 ihint = g_signal_get_invocation_hint (instance);
1026 g_assert_cmpstr (g_signal_name (ihint->signal_id), ==, "simple-2");
1029 static void
1030 test_invocation_hint (void)
1032 GObject *test;
1034 test = g_object_new (test_get_type (), NULL);
1036 g_signal_connect (test, "simple", G_CALLBACK (simple_cb), NULL);
1037 g_signal_connect (test, "simple-2", G_CALLBACK (simple2_cb), NULL);
1038 g_signal_emit_by_name (test, "simple");
1040 g_object_unref (test);
1043 static gboolean
1044 in_set (const gchar *s,
1045 const gchar *set[])
1047 gint i;
1049 for (i = 0; set[i]; i++)
1051 if (g_strcmp0 (s, set[i]) == 0)
1052 return TRUE;
1055 return FALSE;
1058 static void
1059 test_introspection (void)
1061 guint *ids;
1062 guint n_ids;
1063 const gchar *name;
1064 gint i;
1065 const gchar *names[] = {
1066 "simple",
1067 "simple-2",
1068 "generic-marshaller-1",
1069 "generic-marshaller-2",
1070 "generic-marshaller-enum-return-signed",
1071 "generic-marshaller-enum-return-unsigned",
1072 "generic-marshaller-int-return",
1073 "va-marshaller-int-return",
1074 "generic-marshaller-uint-return",
1075 "va-marshaller-uint-return",
1076 "variant-changed-no-slot",
1077 "variant-changed",
1078 "all-types",
1079 "all-types-va",
1080 "all-types-generic",
1081 "all-types-null",
1082 "all-types-empty",
1083 "custom-marshaller",
1084 NULL
1086 GSignalQuery query;
1088 ids = g_signal_list_ids (test_get_type (), &n_ids);
1089 g_assert_cmpuint (n_ids, ==, g_strv_length ((gchar**)names));
1091 for (i = 0; i < n_ids; i++)
1093 name = g_signal_name (ids[i]);
1094 g_assert (in_set (name, names));
1097 g_signal_query (simple_id, &query);
1098 g_assert_cmpuint (query.signal_id, ==, simple_id);
1099 g_assert_cmpstr (query.signal_name, ==, "simple");
1100 g_assert (query.itype == test_get_type ());
1101 g_assert (query.signal_flags == G_SIGNAL_RUN_LAST);
1102 g_assert (query.return_type == G_TYPE_NONE);
1103 g_assert_cmpuint (query.n_params, ==, 0);
1105 g_free (ids);
1108 static void
1109 test_handler (gpointer instance, gpointer data)
1111 gint *count = data;
1113 (*count)++;
1116 static void
1117 test_block_handler (void)
1119 GObject *test1, *test2;
1120 gint count1 = 0;
1121 gint count2 = 0;
1122 gulong handler1, handler;
1124 test1 = g_object_new (test_get_type (), NULL);
1125 test2 = g_object_new (test_get_type (), NULL);
1127 handler1 = g_signal_connect (test1, "simple", G_CALLBACK (test_handler), &count1);
1128 g_signal_connect (test2, "simple", G_CALLBACK (test_handler), &count2);
1130 handler = g_signal_handler_find (test1, G_SIGNAL_MATCH_ID, simple_id, 0, NULL, NULL, NULL);
1132 g_assert (handler == handler1);
1134 g_assert_cmpint (count1, ==, 0);
1135 g_assert_cmpint (count2, ==, 0);
1137 g_signal_emit_by_name (test1, "simple");
1138 g_signal_emit_by_name (test2, "simple");
1140 g_assert_cmpint (count1, ==, 1);
1141 g_assert_cmpint (count2, ==, 1);
1143 g_signal_handler_block (test1, handler1);
1145 g_signal_emit_by_name (test1, "simple");
1146 g_signal_emit_by_name (test2, "simple");
1148 g_assert_cmpint (count1, ==, 1);
1149 g_assert_cmpint (count2, ==, 2);
1151 g_signal_handler_unblock (test1, handler1);
1153 g_signal_emit_by_name (test1, "simple");
1154 g_signal_emit_by_name (test2, "simple");
1156 g_assert_cmpint (count1, ==, 2);
1157 g_assert_cmpint (count2, ==, 3);
1159 g_assert_cmpuint (g_signal_handlers_block_matched (test1, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, test_block_handler, NULL), ==, 0);
1160 g_assert_cmpuint (g_signal_handlers_block_matched (test2, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, test_handler, NULL), ==, 1);
1162 g_signal_emit_by_name (test1, "simple");
1163 g_signal_emit_by_name (test2, "simple");
1165 g_assert_cmpint (count1, ==, 3);
1166 g_assert_cmpint (count2, ==, 3);
1168 g_signal_handlers_unblock_matched (test2, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, test_handler, NULL);
1170 g_object_unref (test1);
1171 g_object_unref (test2);
1174 static void
1175 stop_emission (gpointer instance, gpointer data)
1177 g_signal_stop_emission (instance, simple_id, 0);
1180 static void
1181 stop_emission_by_name (gpointer instance, gpointer data)
1183 g_signal_stop_emission_by_name (instance, "simple");
1186 static void
1187 dont_reach (gpointer instance, gpointer data)
1189 g_assert_not_reached ();
1192 static void
1193 test_stop_emission (void)
1195 GObject *test1;
1196 gulong handler;
1198 test1 = g_object_new (test_get_type (), NULL);
1199 handler = g_signal_connect (test1, "simple", G_CALLBACK (stop_emission), NULL);
1200 g_signal_connect_after (test1, "simple", G_CALLBACK (dont_reach), NULL);
1202 g_signal_emit_by_name (test1, "simple");
1204 g_signal_handler_disconnect (test1, handler);
1205 g_signal_connect (test1, "simple", G_CALLBACK (stop_emission_by_name), NULL);
1207 g_signal_emit_by_name (test1, "simple");
1209 g_object_unref (test1);
1212 static void
1213 test_signal_disconnect_wrong_object (void)
1215 Test *object, *object2;
1216 Test2 *object3;
1217 guint signal_id;
1219 object = g_object_new (test_get_type (), NULL);
1220 object2 = g_object_new (test_get_type (), NULL);
1221 object3 = g_object_new (test2_get_type (), NULL);
1223 signal_id = g_signal_connect (object,
1224 "simple",
1225 G_CALLBACK (simple_handler1),
1226 NULL);
1228 /* disconnect from the wrong object (same type), should warn */
1229 g_test_expect_message ("GLib-GObject", G_LOG_LEVEL_WARNING,
1230 "*: instance '*' has no handler with id '*'");
1231 g_signal_handler_disconnect (object2, signal_id);
1232 g_test_assert_expected_messages ();
1234 /* and from an object of the wrong type */
1235 g_test_expect_message ("GLib-GObject", G_LOG_LEVEL_WARNING,
1236 "*: instance '*' has no handler with id '*'");
1237 g_signal_handler_disconnect (object3, signal_id);
1238 g_test_assert_expected_messages ();
1240 /* it's still connected */
1241 g_assert (g_signal_handler_is_connected (object, signal_id));
1243 g_object_unref (object);
1244 g_object_unref (object2);
1245 g_object_unref (object3);
1248 /* --- */
1251 main (int argc,
1252 char *argv[])
1254 g_test_init (&argc, &argv, NULL);
1256 g_test_add_func ("/gobject/signals/all-types", test_all_types);
1257 g_test_add_func ("/gobject/signals/variant", test_variant_signal);
1258 g_test_add_func ("/gobject/signals/destroy-target-object", test_destroy_target_object);
1259 g_test_add_func ("/gobject/signals/generic-marshaller-1", test_generic_marshaller_signal_1);
1260 g_test_add_func ("/gobject/signals/generic-marshaller-2", test_generic_marshaller_signal_2);
1261 g_test_add_func ("/gobject/signals/generic-marshaller-enum-return-signed", test_generic_marshaller_signal_enum_return_signed);
1262 g_test_add_func ("/gobject/signals/generic-marshaller-enum-return-unsigned", test_generic_marshaller_signal_enum_return_unsigned);
1263 g_test_add_func ("/gobject/signals/generic-marshaller-int-return", test_generic_marshaller_signal_int_return);
1264 g_test_add_func ("/gobject/signals/generic-marshaller-uint-return", test_generic_marshaller_signal_uint_return);
1265 g_test_add_func ("/gobject/signals/custom-marshaller", test_custom_marshaller);
1266 g_test_add_func ("/gobject/signals/connect", test_connect);
1267 g_test_add_func ("/gobject/signals/emission-hook", test_emission_hook);
1268 g_test_add_func ("/gobject/signals/introspection", test_introspection);
1269 g_test_add_func ("/gobject/signals/block-handler", test_block_handler);
1270 g_test_add_func ("/gobject/signals/stop-emission", test_stop_emission);
1271 g_test_add_func ("/gobject/signals/invocation-hint", test_invocation_hint);
1272 g_test_add_func ("/gobject/signals/test-disconnection-wrong-object", test_signal_disconnect_wrong_object);
1274 return g_test_run ();