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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #define G_LOG_DOMAIN "TestAccumulator"
23 #undef G_DISABLE_ASSERT
24 #undef G_DISABLE_CHECKS
25 #undef G_DISABLE_CAST_CHECKS
29 #include <glib-object.h>
31 #include "testmarshal.h"
32 #include "testcommon.h"
34 /* What this test tests is the behavior of signal accumulators
35 * Two accumulators are tested:
37 * 1: A custom accumulator that appends the returned strings
38 * 2: The standard g_signal_accumulator_true_handled that stops
39 * emission on TRUE returns.
43 * TestObject, a parent class for TestObject
45 #define TEST_TYPE_OBJECT (test_object_get_type ())
46 typedef struct _TestObject TestObject;
47 typedef struct _TestObjectClass TestObjectClass;
51 GObject parent_instance;
53 struct _TestObjectClass
55 GObjectClass parent_class;
57 gchar* (*test_signal1) (TestObject *tobject,
59 gboolean (*test_signal2) (TestObject *tobject,
61 GVariant* (*test_signal3) (TestObject *tobject,
65 static GType test_object_get_type (void);
68 test_signal1_accumulator (GSignalInvocationHint *ihint,
70 const GValue *handler_return,
73 const gchar *accu_string = g_value_get_string (return_accu);
74 const gchar *new_string = g_value_get_string (handler_return);
78 result_string = g_strconcat (accu_string, new_string, NULL);
80 result_string = g_strdup (new_string);
84 g_value_set_string_take_ownership (return_accu, result_string);
90 test_object_signal1_callback_before (TestObject *tobject,
94 return g_strdup ("<before>");
98 test_object_real_signal1 (TestObject *tobject,
101 return g_strdup ("<default>");
105 test_object_signal1_callback_after (TestObject *tobject,
109 return g_strdup ("<after>");
113 test_object_signal2_callback_before (TestObject *tobject,
119 case 2: return FALSE;
120 case 3: return FALSE;
121 case 4: return FALSE;
124 g_assert_not_reached ();
129 test_object_real_signal2 (TestObject *tobject,
134 case 1: g_assert_not_reached (); return FALSE;
136 case 3: return FALSE;
137 case 4: return FALSE;
140 g_assert_not_reached ();
145 test_object_signal2_callback_after (TestObject *tobject,
150 case 1: g_assert_not_reached (); return FALSE;
151 case 2: g_assert_not_reached (); return FALSE;
153 case 4: return FALSE;
156 g_assert_not_reached ();
161 test_signal3_accumulator (GSignalInvocationHint *ihint,
163 const GValue *handler_return,
168 variant = g_value_get_variant (handler_return);
169 g_assert (!g_variant_is_floating (variant));
171 g_value_set_variant (return_accu, variant);
173 return variant == NULL;
176 /* To be notified when the variant is finalised, we construct
177 * it from data with a custom GDestroyNotify.
187 free_data (VariantData *data)
189 *(data->weak_ptr) = TRUE;
191 g_slice_free (VariantData, data);
195 test_object_real_signal3 (TestObject *tobject,
201 variant = g_variant_ref_sink (g_variant_new_uint32 (42));
202 data = g_slice_new (VariantData);
203 data->weak_ptr = weak_ptr;
204 data->n = g_variant_get_size (variant);
205 data->mem = g_malloc (data->n);
206 g_variant_store (variant, data->mem);
207 g_variant_unref (variant);
209 variant = g_variant_new_from_data (G_VARIANT_TYPE ("u"),
213 (GDestroyNotify) free_data,
215 return g_variant_ref_sink (variant);
219 test_object_class_init (TestObjectClass *class)
221 class->test_signal1 = test_object_real_signal1;
222 class->test_signal2 = test_object_real_signal2;
223 class->test_signal3 = test_object_real_signal3;
225 g_signal_new ("test-signal1",
226 G_OBJECT_CLASS_TYPE (class),
228 G_STRUCT_OFFSET (TestObjectClass, test_signal1),
229 test_signal1_accumulator, NULL,
230 test_marshal_STRING__INT,
231 G_TYPE_STRING, 1, G_TYPE_INT);
232 g_signal_new ("test-signal2",
233 G_OBJECT_CLASS_TYPE (class),
235 G_STRUCT_OFFSET (TestObjectClass, test_signal2),
236 g_signal_accumulator_true_handled, NULL,
237 test_marshal_BOOLEAN__INT,
238 G_TYPE_BOOLEAN, 1, G_TYPE_INT);
239 g_signal_new ("test-signal3",
240 G_OBJECT_CLASS_TYPE (class),
242 G_STRUCT_OFFSET (TestObjectClass, test_signal3),
243 test_signal3_accumulator, NULL,
244 test_marshal_VARIANT__POINTER,
245 G_TYPE_VARIANT, 1, G_TYPE_POINTER);
248 static DEFINE_TYPE(TestObject, test_object,
249 test_object_class_init, NULL, NULL,
257 gchar *string_result;
258 gboolean bool_result;
259 gboolean variant_finalised;
260 GVariant *variant_result;
262 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
263 G_LOG_LEVEL_WARNING |
264 G_LOG_LEVEL_CRITICAL);
267 object = g_object_new (TEST_TYPE_OBJECT, NULL);
269 g_signal_connect (object, "test-signal1",
270 G_CALLBACK (test_object_signal1_callback_before), NULL);
271 g_signal_connect_after (object, "test-signal1",
272 G_CALLBACK (test_object_signal1_callback_after), NULL);
274 g_signal_emit_by_name (object, "test-signal1", 0, &string_result);
275 g_assert (strcmp (string_result, "<before><default><after>") == 0);
276 g_free (string_result);
278 g_signal_connect (object, "test-signal2",
279 G_CALLBACK (test_object_signal2_callback_before), NULL);
280 g_signal_connect_after (object, "test-signal2",
281 G_CALLBACK (test_object_signal2_callback_after), NULL);
284 g_signal_emit_by_name (object, "test-signal2", 1, &bool_result);
285 g_assert (bool_result == TRUE);
287 g_signal_emit_by_name (object, "test-signal2", 2, &bool_result);
288 g_assert (bool_result == TRUE);
290 g_signal_emit_by_name (object, "test-signal2", 3, &bool_result);
291 g_assert (bool_result == TRUE);
293 g_signal_emit_by_name (object, "test-signal2", 4, &bool_result);
294 g_assert (bool_result == FALSE);
296 variant_finalised = FALSE;
297 variant_result = NULL;
298 g_signal_emit_by_name (object, "test-signal3", &variant_finalised, &variant_result);
299 g_assert (variant_result != NULL);
300 g_assert (!g_variant_is_floating (variant_result));
302 /* Test that variant_result had refcount 1 */
303 g_assert (!variant_finalised);
304 g_variant_unref (variant_result);
305 g_assert (variant_finalised);