1 #include <glib-object.h>
4 TEST_ENUM_NEGATIVE = -30,
11 TEST_UNSIGNED_ENUM_FOO = 1,
12 TEST_UNSIGNED_ENUM_BAR = 0x80000000
16 test_enum_get_type (void)
18 static volatile gsize g_define_type_id__volatile = 0;
20 if (g_once_init_enter (&g_define_type_id__volatile))
22 static const GFlagsValue values[] = {
23 { TEST_ENUM_NEGATIVE, "TEST_ENUM_NEGATIVE", "negative" },
24 { TEST_ENUM_NONE, "TEST_ENUM_NONE", "none" },
25 { TEST_ENUM_FOO, "TEST_ENUM_FOO", "foo" },
26 { TEST_ENUM_BAR, "TEST_ENUM_BAR", "bar" },
29 GType g_define_type_id =
30 g_flags_register_static (g_intern_static_string ("TestEnum"), values);
31 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
34 return g_define_type_id__volatile;
38 test_unsigned_enum_get_type (void)
40 static volatile gsize g_define_type_id__volatile = 0;
42 if (g_once_init_enter (&g_define_type_id__volatile))
44 static const GFlagsValue values[] = {
45 { TEST_UNSIGNED_ENUM_FOO, "TEST_UNSIGNED_ENUM_FOO", "foo" },
46 { TEST_UNSIGNED_ENUM_BAR, "TEST_UNSIGNED_ENUM_BAR", "bar" },
49 GType g_define_type_id =
50 g_flags_register_static (g_intern_static_string ("TestUnsignedEnum"), values);
51 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
54 return g_define_type_id__volatile;
58 typedef struct _Test Test;
59 typedef struct _TestClass TestClass;
63 GObject parent_instance;
68 GObjectClass parent_class;
70 void (* variant_changed) (Test *, GVariant *);
73 static GType test_get_type (void);
74 G_DEFINE_TYPE (Test, test, G_TYPE_OBJECT)
77 test_init (Test *test)
82 test_class_init (TestClass *klass)
84 g_signal_new ("generic-marshaller-1",
85 G_TYPE_FROM_CLASS (klass),
86 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
92 G_TYPE_CHAR, G_TYPE_UCHAR, G_TYPE_INT, G_TYPE_LONG, G_TYPE_POINTER, G_TYPE_DOUBLE, G_TYPE_FLOAT);
93 g_signal_new ("generic-marshaller-2",
94 G_TYPE_FROM_CLASS (klass),
95 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
101 G_TYPE_INT, test_enum_get_type(), G_TYPE_INT, test_unsigned_enum_get_type (), G_TYPE_INT);
102 g_signal_new ("generic-marshaller-enum-return-signed",
103 G_TYPE_FROM_CLASS (klass),
104 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
108 test_enum_get_type(),
110 g_signal_new ("generic-marshaller-enum-return-unsigned",
111 G_TYPE_FROM_CLASS (klass),
112 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
116 test_unsigned_enum_get_type(),
118 g_signal_new ("variant-changed-no-slot",
119 G_TYPE_FROM_CLASS (klass),
120 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
123 g_cclosure_marshal_VOID__VARIANT,
127 g_signal_new ("variant-changed",
128 G_TYPE_FROM_CLASS (klass),
129 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
130 G_STRUCT_OFFSET (TestClass, variant_changed),
132 g_cclosure_marshal_VOID__VARIANT,
139 test_variant_signal (void)
144 /* Tests that the signal emission consumes the variant,
145 * even if there are no handlers connected.
148 test = g_object_new (test_get_type (), NULL);
150 v = g_variant_new_boolean (TRUE);
152 g_assert (g_variant_is_floating (v));
153 g_signal_emit_by_name (test, "variant-changed-no-slot", v);
154 g_assert (!g_variant_is_floating (v));
157 v = g_variant_new_boolean (TRUE);
159 g_assert (g_variant_is_floating (v));
160 g_signal_emit_by_name (test, "variant-changed", v);
161 g_assert (!g_variant_is_floating (v));
164 g_object_unref (test);
168 on_generic_marshaller_1 (Test *obj,
178 g_assert_cmpint (v_schar, ==, 42);
179 g_assert_cmpint (v_uchar, ==, 43);
180 g_assert_cmpint (v_int, ==, 4096);
181 g_assert_cmpint (v_long, ==, 8192);
182 g_assert (v_pointer == NULL);
183 g_assert_cmpfloat (v_double, >, 0.0);
184 g_assert_cmpfloat (v_double, <, 1.0);
185 g_assert_cmpfloat (v_float, >, 5.0);
186 g_assert_cmpfloat (v_float, <, 6.0);
190 test_generic_marshaller_signal_1 (void)
193 test = g_object_new (test_get_type (), NULL);
195 g_signal_connect (test, "generic-marshaller-1", G_CALLBACK (on_generic_marshaller_1), NULL);
197 g_signal_emit_by_name (test, "generic-marshaller-1", 42, 43, 4096, 8192, NULL, 0.5, 5.5);
199 g_object_unref (test);
203 on_generic_marshaller_2 (Test *obj,
207 TestUnsignedEnum v_uenum,
210 g_assert_cmpint (v_int1, ==, 42);
211 g_assert_cmpint (v_enum, ==, TEST_ENUM_BAR);
212 g_assert_cmpint (v_int2, ==, 43);
213 g_assert_cmpint (v_uenum, ==, TEST_UNSIGNED_ENUM_BAR);
214 g_assert_cmpint (v_int3, ==, 44);
218 test_generic_marshaller_signal_2 (void)
221 test = g_object_new (test_get_type (), NULL);
223 g_signal_connect (test, "generic-marshaller-2", G_CALLBACK (on_generic_marshaller_2), NULL);
225 g_signal_emit_by_name (test, "generic-marshaller-2", 42, TEST_ENUM_BAR, 43, TEST_UNSIGNED_ENUM_BAR, 44);
227 g_object_unref (test);
231 on_generic_marshaller_enum_return_signed_1 (Test *obj)
233 return TEST_ENUM_NEGATIVE;
237 on_generic_marshaller_enum_return_signed_2 (Test *obj)
239 return TEST_ENUM_BAR;
243 test_generic_marshaller_signal_enum_return_signed (void)
249 test = g_object_new (test_get_type (), NULL);
251 /* Test return value NEGATIVE */
252 id = g_signal_connect (test,
253 "generic-marshaller-enum-return-signed",
254 G_CALLBACK (on_generic_marshaller_enum_return_signed_1),
256 g_signal_emit_by_name (test, "generic-marshaller-enum-return-signed", &retval);
257 g_assert_cmpint (retval, ==, TEST_ENUM_NEGATIVE);
258 g_signal_handler_disconnect (test, id);
260 /* Test return value BAR */
262 id = g_signal_connect (test,
263 "generic-marshaller-enum-return-signed",
264 G_CALLBACK (on_generic_marshaller_enum_return_signed_2),
266 g_signal_emit_by_name (test, "generic-marshaller-enum-return-signed", &retval);
267 g_assert_cmpint (retval, ==, TEST_ENUM_BAR);
268 g_signal_handler_disconnect (test, id);
270 g_object_unref (test);
273 static TestUnsignedEnum
274 on_generic_marshaller_enum_return_unsigned_1 (Test *obj)
276 return TEST_UNSIGNED_ENUM_FOO;
279 static TestUnsignedEnum
280 on_generic_marshaller_enum_return_unsigned_2 (Test *obj)
282 return TEST_UNSIGNED_ENUM_BAR;
286 test_generic_marshaller_signal_enum_return_unsigned (void)
290 TestUnsignedEnum retval = 0;
292 test = g_object_new (test_get_type (), NULL);
294 /* Test return value FOO */
295 id = g_signal_connect (test,
296 "generic-marshaller-enum-return-unsigned",
297 G_CALLBACK (on_generic_marshaller_enum_return_unsigned_1),
299 g_signal_emit_by_name (test, "generic-marshaller-enum-return-unsigned", &retval);
300 g_assert_cmpint (retval, ==, TEST_UNSIGNED_ENUM_FOO);
301 g_signal_handler_disconnect (test, id);
303 /* Test return value BAR */
305 id = g_signal_connect (test,
306 "generic-marshaller-enum-return-unsigned",
307 G_CALLBACK (on_generic_marshaller_enum_return_unsigned_2),
309 g_signal_emit_by_name (test, "generic-marshaller-enum-return-unsigned", &retval);
310 g_assert_cmpint (retval, ==, TEST_UNSIGNED_ENUM_BAR);
311 g_signal_handler_disconnect (test, id);
313 g_object_unref (test);
324 g_test_init (&argc, &argv, NULL);
326 g_test_add_func ("/gobject/signals/variant", test_variant_signal);
327 g_test_add_func ("/gobject/signals/generic-marshaller-1", test_generic_marshaller_signal_1);
328 g_test_add_func ("/gobject/signals/generic-marshaller-2", test_generic_marshaller_signal_2);
329 g_test_add_func ("/gobject/signals/generic-marshaller-enum-return-signed", test_generic_marshaller_signal_enum_return_signed);
330 g_test_add_func ("/gobject/signals/generic-marshaller-enum-return-unsigned", test_generic_marshaller_signal_enum_return_unsigned);
332 return g_test_run ();