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'); \
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'); \
18 TEST_ENUM_NEGATIVE
= -30,
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.
33 custom_marshal_VOID__INVOCATIONHINT (GClosure
*closure
,
34 GValue
*return_value G_GNUC_UNUSED
,
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
,
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);
56 data1
= g_value_peek_pointer (param_values
+ 0);
57 data2
= closure
->data
;
59 callback
= (GMarshalFunc_VOID__INVOCATIONHINT
) (marshal_data
? marshal_data
: cc
->callback
);
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" },
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
;
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" },
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
;
112 static const GEnumValue my_enum_values
[] =
114 { MY_ENUM_VALUE
, "the first value", "one" },
119 MY_FLAGS_FIRST_BIT
= (1 << 0),
120 MY_FLAGS_THIRD_BIT
= (1 << 2),
121 MY_FLAGS_LAST_BIT
= (1 << 31)
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" },
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
;
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
);
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
)
161 test_init (Test
*test
)
166 test_class_init (TestClass
*klass
)
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
),
183 simple2_id
= g_signal_new ("simple-2",
184 G_TYPE_FROM_CLASS (klass
),
185 G_SIGNAL_RUN_LAST
| G_SIGNAL_NO_RECURSE
,
191 g_signal_new ("generic-marshaller-1",
192 G_TYPE_FROM_CLASS (klass
),
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
),
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
),
215 test_enum_get_type(),
217 g_signal_new ("generic-marshaller-enum-return-unsigned",
218 G_TYPE_FROM_CLASS (klass
),
223 test_unsigned_enum_get_type(),
225 g_signal_new ("generic-marshaller-int-return",
226 G_TYPE_FROM_CLASS (klass
),
233 s
= g_signal_new ("va-marshaller-int-return",
234 G_TYPE_FROM_CLASS (klass
),
241 g_signal_set_va_marshaller (s
, G_TYPE_FROM_CLASS (klass
),
243 g_signal_new ("generic-marshaller-uint-return",
244 G_TYPE_FROM_CLASS (klass
),
251 s
= g_signal_new ("va-marshaller-uint-return",
252 G_TYPE_FROM_CLASS (klass
),
259 g_signal_set_va_marshaller (s
, G_TYPE_FROM_CLASS (klass
),
261 g_signal_new ("custom-marshaller",
262 G_TYPE_FROM_CLASS (klass
),
266 custom_marshal_VOID__INVOCATIONHINT
,
270 g_signal_new ("variant-changed-no-slot",
271 G_TYPE_FROM_CLASS (klass
),
272 G_SIGNAL_RUN_LAST
| G_SIGNAL_MUST_COLLECT
,
275 g_cclosure_marshal_VOID__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
),
284 g_cclosure_marshal_VOID__VARIANT
,
288 g_signal_new ("all-types",
289 G_TYPE_FROM_CLASS (klass
),
291 G_STRUCT_OFFSET (TestClass
, all_types
),
293 test_VOID__INT_BOOLEAN_CHAR_UCHAR_UINT_LONG_ULONG_ENUM_FLAGS_FLOAT_DOUBLE_STRING_PARAM_BOXED_POINTER_OBJECT_VARIANT_INT64_UINT64
,
315 s
= g_signal_new ("all-types-va",
316 G_TYPE_FROM_CLASS (klass
),
318 G_STRUCT_OFFSET (TestClass
, all_types
),
320 test_VOID__INT_BOOLEAN_CHAR_UCHAR_UINT_LONG_ULONG_ENUM_FLAGS_FLOAT_DOUBLE_STRING_PARAM_BOXED_POINTER_OBJECT_VARIANT_INT64_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
),
348 G_STRUCT_OFFSET (TestClass
, all_types
),
372 g_signal_new ("all-types-null",
373 G_TYPE_FROM_CLASS (klass
),
375 G_STRUCT_OFFSET (TestClass
, all_types_null
),
377 test_VOID__INT_BOOLEAN_CHAR_UCHAR_UINT_LONG_ULONG_ENUM_FLAGS_FLOAT_DOUBLE_STRING_PARAM_BOXED_POINTER_OBJECT_VARIANT_INT64_UINT64
,
399 g_signal_new ("all-types-empty",
400 G_TYPE_FROM_CLASS (klass
),
404 test_VOID__INT_BOOLEAN_CHAR_UCHAR_UINT_LONG_ULONG_ENUM_FLAGS_FLOAT_DOUBLE_STRING_PARAM_BOXED_POINTER_OBJECT_VARIANT_INT64_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
)
435 test2_init (Test2
*test
)
440 test2_class_init (Test2Class
*klass
)
445 test_variant_signal (void)
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
);
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
));
463 v
= g_variant_new_boolean (TRUE
);
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
));
470 g_object_unref (test
);
474 on_generic_marshaller_1 (Test
*obj
,
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);
496 test_generic_marshaller_signal_1 (void)
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
);
509 on_generic_marshaller_2 (Test
*obj
,
513 TestUnsignedEnum v_uenum
,
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);
524 test_generic_marshaller_signal_2 (void)
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
);
537 on_generic_marshaller_enum_return_signed_1 (Test
*obj
)
539 return TEST_ENUM_NEGATIVE
;
543 on_generic_marshaller_enum_return_signed_2 (Test
*obj
)
545 return TEST_ENUM_BAR
;
549 test_generic_marshaller_signal_enum_return_signed (void)
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
),
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 */
568 id
= g_signal_connect (test
,
569 "generic-marshaller-enum-return-signed",
570 G_CALLBACK (on_generic_marshaller_enum_return_signed_2
),
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
;
592 test_generic_marshaller_signal_enum_return_unsigned (void)
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
),
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 */
611 id
= g_signal_connect (test
,
612 "generic-marshaller-enum-return-unsigned",
613 G_CALLBACK (on_generic_marshaller_enum_return_unsigned_2
),
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 /**********************/
625 on_generic_marshaller_int_return_signed_1 (Test
*obj
)
631 on_generic_marshaller_int_return_signed_2 (Test
*obj
)
637 test_generic_marshaller_signal_int_return (void)
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
),
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 */
656 id
= g_signal_connect (test
,
657 "generic-marshaller-int-return",
658 G_CALLBACK (on_generic_marshaller_int_return_signed_2
),
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
),
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 */
677 id
= g_signal_connect (test
,
678 "va-marshaller-int-return",
679 G_CALLBACK (on_generic_marshaller_int_return_signed_2
),
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
);
689 on_generic_marshaller_uint_return_1 (Test
*obj
)
695 on_generic_marshaller_uint_return_2 (Test
*obj
)
701 test_generic_marshaller_signal_uint_return (void)
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
),
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
);
718 id
= g_signal_connect (test
,
719 "generic-marshaller-uint-return",
720 G_CALLBACK (on_generic_marshaller_uint_return_2
),
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
),
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
);
737 id
= g_signal_connect (test
,
738 "va-marshaller-uint-return",
739 G_CALLBACK (on_generic_marshaller_uint_return_2
),
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, };
751 custom_marshaller_callback (Test
*test
,
752 GSignalInvocationHint
*hint
,
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
);
767 test_custom_marshaller (void)
771 test
= g_object_new (test_get_type (), NULL
);
773 g_signal_connect (test
,
775 G_CALLBACK (custom_marshaller_callback
),
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;
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);
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
);
818 test_all_types (void)
826 guint ui
= G_MAXUINT
- 42;
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
;
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);
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
);
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
),
921 "object-signal::va-marshaller-int-return",
922 G_CALLBACK (on_generic_marshaller_int_return_signed_2
),
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
,
932 G_CALLBACK (on_generic_marshaller_int_return_signed_1
),
934 "any-signal::va-marshaller-int-return",
935 G_CALLBACK (on_generic_marshaller_int_return_signed_2
),
939 g_object_unref (test
);
943 simple_handler1 (GObject
*sender
,
946 g_object_unref (target
);
950 simple_handler2 (GObject
*sender
,
953 g_object_unref (target
);
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
);
971 hook_func (GSignalInvocationHint
*ihint
,
973 const GValue
*params
,
984 test_emission_hook (void)
986 GObject
*test1
, *test2
;
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
);
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");
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");
1030 test_invocation_hint (void)
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
);
1044 in_set (const gchar
*s
,
1049 for (i
= 0; set
[i
]; i
++)
1051 if (g_strcmp0 (s
, set
[i
]) == 0)
1059 test_introspection (void)
1065 const gchar
*names
[] = {
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",
1080 "all-types-generic",
1083 "custom-marshaller",
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);
1109 test_handler (gpointer instance
, gpointer data
)
1117 test_block_handler (void)
1119 GObject
*test1
, *test2
;
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
);
1175 stop_emission (gpointer instance
, gpointer data
)
1177 g_signal_stop_emission (instance
, simple_id
, 0);
1181 stop_emission_by_name (gpointer instance
, gpointer data
)
1183 g_signal_stop_emission_by_name (instance
, "simple");
1187 dont_reach (gpointer instance
, gpointer data
)
1189 g_assert_not_reached ();
1193 test_stop_emission (void)
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
);
1213 test_signal_disconnect_wrong_object (void)
1215 Test
*object
, *object2
;
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
,
1225 G_CALLBACK (simple_handler1
),
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
);
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 ();