1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2001, 2003 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #define G_LOG_DOMAIN "TestAccumulator"
21 #undef G_DISABLE_ASSERT
22 #undef G_DISABLE_CHECKS
23 #undef G_DISABLE_CAST_CHECKS
27 #include <glib-object.h>
29 #include "testmarshal.h"
30 #include "testcommon.h"
32 /* What this test tests is the behavior of signal accumulators
33 * Two accumulators are tested:
35 * 1: A custom accumulator that appends the returned strings
36 * 2: The standard g_signal_accumulator_true_handled that stops
37 * emission on TRUE returns.
41 * TestObject, a parent class for TestObject
43 #define TEST_TYPE_OBJECT (test_object_get_type ())
44 typedef struct _TestObject TestObject
;
45 typedef struct _TestObjectClass TestObjectClass
;
49 GObject parent_instance
;
51 struct _TestObjectClass
53 GObjectClass parent_class
;
55 gchar
* (*test_signal1
) (TestObject
*tobject
,
57 gboolean (*test_signal2
) (TestObject
*tobject
,
59 GVariant
* (*test_signal3
) (TestObject
*tobject
,
63 static GType
test_object_get_type (void);
66 test_signal1_accumulator (GSignalInvocationHint
*ihint
,
68 const GValue
*handler_return
,
71 const gchar
*accu_string
= g_value_get_string (return_accu
);
72 const gchar
*new_string
= g_value_get_string (handler_return
);
76 result_string
= g_strconcat (accu_string
, new_string
, NULL
);
78 result_string
= g_strdup (new_string
);
82 g_value_set_string_take_ownership (return_accu
, result_string
);
88 test_object_signal1_callback_before (TestObject
*tobject
,
92 return g_strdup ("<before>");
96 test_object_real_signal1 (TestObject
*tobject
,
99 return g_strdup ("<default>");
103 test_object_signal1_callback_after (TestObject
*tobject
,
107 return g_strdup ("<after>");
111 test_object_signal2_callback_before (TestObject
*tobject
,
117 case 2: return FALSE
;
118 case 3: return FALSE
;
119 case 4: return FALSE
;
122 g_assert_not_reached ();
127 test_object_real_signal2 (TestObject
*tobject
,
132 case 1: g_assert_not_reached (); return FALSE
;
134 case 3: return FALSE
;
135 case 4: return FALSE
;
138 g_assert_not_reached ();
143 test_object_signal2_callback_after (TestObject
*tobject
,
148 case 1: g_assert_not_reached (); return FALSE
;
149 case 2: g_assert_not_reached (); return FALSE
;
151 case 4: return FALSE
;
154 g_assert_not_reached ();
159 test_signal3_accumulator (GSignalInvocationHint
*ihint
,
161 const GValue
*handler_return
,
166 variant
= g_value_get_variant (handler_return
);
167 g_assert (!g_variant_is_floating (variant
));
169 g_value_set_variant (return_accu
, variant
);
171 return variant
== NULL
;
174 /* To be notified when the variant is finalised, we construct
175 * it from data with a custom GDestroyNotify.
185 free_data (VariantData
*data
)
187 *(data
->weak_ptr
) = TRUE
;
189 g_slice_free (VariantData
, data
);
193 test_object_real_signal3 (TestObject
*tobject
,
199 variant
= g_variant_ref_sink (g_variant_new_uint32 (42));
200 data
= g_slice_new (VariantData
);
201 data
->weak_ptr
= weak_ptr
;
202 data
->n
= g_variant_get_size (variant
);
203 data
->mem
= g_malloc (data
->n
);
204 g_variant_store (variant
, data
->mem
);
205 g_variant_unref (variant
);
207 variant
= g_variant_new_from_data (G_VARIANT_TYPE ("u"),
211 (GDestroyNotify
) free_data
,
213 return g_variant_ref_sink (variant
);
217 test_object_class_init (TestObjectClass
*class)
219 class->test_signal1
= test_object_real_signal1
;
220 class->test_signal2
= test_object_real_signal2
;
221 class->test_signal3
= test_object_real_signal3
;
223 g_signal_new ("test-signal1",
224 G_OBJECT_CLASS_TYPE (class),
226 G_STRUCT_OFFSET (TestObjectClass
, test_signal1
),
227 test_signal1_accumulator
, NULL
,
228 test_marshal_STRING__INT
,
229 G_TYPE_STRING
, 1, G_TYPE_INT
);
230 g_signal_new ("test-signal2",
231 G_OBJECT_CLASS_TYPE (class),
233 G_STRUCT_OFFSET (TestObjectClass
, test_signal2
),
234 g_signal_accumulator_true_handled
, NULL
,
235 test_marshal_BOOLEAN__INT
,
236 G_TYPE_BOOLEAN
, 1, G_TYPE_INT
);
237 g_signal_new ("test-signal3",
238 G_OBJECT_CLASS_TYPE (class),
240 G_STRUCT_OFFSET (TestObjectClass
, test_signal3
),
241 test_signal3_accumulator
, NULL
,
242 test_marshal_VARIANT__POINTER
,
243 G_TYPE_VARIANT
, 1, G_TYPE_POINTER
);
246 static DEFINE_TYPE(TestObject
, test_object
,
247 test_object_class_init
, NULL
, NULL
,
255 gchar
*string_result
;
256 gboolean bool_result
;
257 gboolean variant_finalised
;
258 GVariant
*variant_result
;
260 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK
) |
261 G_LOG_LEVEL_WARNING
|
262 G_LOG_LEVEL_CRITICAL
);
264 object
= g_object_new (TEST_TYPE_OBJECT
, NULL
);
266 g_signal_connect (object
, "test-signal1",
267 G_CALLBACK (test_object_signal1_callback_before
), NULL
);
268 g_signal_connect_after (object
, "test-signal1",
269 G_CALLBACK (test_object_signal1_callback_after
), NULL
);
271 g_signal_emit_by_name (object
, "test-signal1", 0, &string_result
);
272 g_assert (strcmp (string_result
, "<before><default><after>") == 0);
273 g_free (string_result
);
275 g_signal_connect (object
, "test-signal2",
276 G_CALLBACK (test_object_signal2_callback_before
), NULL
);
277 g_signal_connect_after (object
, "test-signal2",
278 G_CALLBACK (test_object_signal2_callback_after
), NULL
);
281 g_signal_emit_by_name (object
, "test-signal2", 1, &bool_result
);
282 g_assert (bool_result
== TRUE
);
284 g_signal_emit_by_name (object
, "test-signal2", 2, &bool_result
);
285 g_assert (bool_result
== TRUE
);
287 g_signal_emit_by_name (object
, "test-signal2", 3, &bool_result
);
288 g_assert (bool_result
== TRUE
);
290 g_signal_emit_by_name (object
, "test-signal2", 4, &bool_result
);
291 g_assert (bool_result
== FALSE
);
293 variant_finalised
= FALSE
;
294 variant_result
= NULL
;
295 g_signal_emit_by_name (object
, "test-signal3", &variant_finalised
, &variant_result
);
296 g_assert (variant_result
!= NULL
);
297 g_assert (!g_variant_is_floating (variant_result
));
299 /* Test that variant_result had refcount 1 */
300 g_assert (!variant_finalised
);
301 g_variant_unref (variant_result
);
302 g_assert (variant_finalised
);
304 g_object_unref (object
);