Add some more cases to the app-id unit tests
[glib.git] / tests / gobject / accumulator.c
blob29aa4edda6ac0cb89ca635f5d337552d994260ea
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/>.
18 #undef G_LOG_DOMAIN
19 #define G_LOG_DOMAIN "TestAccumulator"
21 #undef G_DISABLE_ASSERT
22 #undef G_DISABLE_CHECKS
23 #undef G_DISABLE_CAST_CHECKS
25 #include <string.h>
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;
47 struct _TestObject
49 GObject parent_instance;
51 struct _TestObjectClass
53 GObjectClass parent_class;
55 gchar* (*test_signal1) (TestObject *tobject,
56 gint param);
57 gboolean (*test_signal2) (TestObject *tobject,
58 gint param);
59 GVariant* (*test_signal3) (TestObject *tobject,
60 gboolean *weak_ptr);
63 static GType test_object_get_type (void);
65 static gboolean
66 test_signal1_accumulator (GSignalInvocationHint *ihint,
67 GValue *return_accu,
68 const GValue *handler_return,
69 gpointer data)
71 const gchar *accu_string = g_value_get_string (return_accu);
72 const gchar *new_string = g_value_get_string (handler_return);
73 gchar *result_string;
75 if (accu_string)
76 result_string = g_strconcat (accu_string, new_string, NULL);
77 else if (new_string)
78 result_string = g_strdup (new_string);
79 else
80 result_string = NULL;
82 g_value_set_string_take_ownership (return_accu, result_string);
84 return TRUE;
87 static gchar *
88 test_object_signal1_callback_before (TestObject *tobject,
89 gint param,
90 gpointer data)
92 return g_strdup ("<before>");
95 static gchar *
96 test_object_real_signal1 (TestObject *tobject,
97 gint param)
99 return g_strdup ("<default>");
102 static gchar *
103 test_object_signal1_callback_after (TestObject *tobject,
104 gint param,
105 gpointer data)
107 return g_strdup ("<after>");
110 static gboolean
111 test_object_signal2_callback_before (TestObject *tobject,
112 gint param)
114 switch (param)
116 case 1: return TRUE;
117 case 2: return FALSE;
118 case 3: return FALSE;
119 case 4: return FALSE;
122 g_assert_not_reached ();
123 return FALSE;
126 static gboolean
127 test_object_real_signal2 (TestObject *tobject,
128 gint param)
130 switch (param)
132 case 1: g_assert_not_reached (); return FALSE;
133 case 2: return TRUE;
134 case 3: return FALSE;
135 case 4: return FALSE;
138 g_assert_not_reached ();
139 return FALSE;
142 static gboolean
143 test_object_signal2_callback_after (TestObject *tobject,
144 gint param)
146 switch (param)
148 case 1: g_assert_not_reached (); return FALSE;
149 case 2: g_assert_not_reached (); return FALSE;
150 case 3: return TRUE;
151 case 4: return FALSE;
154 g_assert_not_reached ();
155 return FALSE;
158 static gboolean
159 test_signal3_accumulator (GSignalInvocationHint *ihint,
160 GValue *return_accu,
161 const GValue *handler_return,
162 gpointer data)
164 GVariant *variant;
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.
178 typedef struct {
179 char *mem;
180 gsize n;
181 gboolean *weak_ptr;
182 } VariantData;
184 static void
185 free_data (VariantData *data)
187 *(data->weak_ptr) = TRUE;
188 g_free (data->mem);
189 g_slice_free (VariantData, data);
192 static GVariant *
193 test_object_real_signal3 (TestObject *tobject,
194 gboolean *weak_ptr)
196 GVariant *variant;
197 VariantData *data;
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"),
208 data->mem,
209 data->n,
210 TRUE,
211 (GDestroyNotify) free_data,
212 data);
213 return g_variant_ref_sink (variant);
216 static void
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),
225 G_SIGNAL_RUN_LAST,
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),
232 G_SIGNAL_RUN_LAST,
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),
239 G_SIGNAL_RUN_LAST,
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,
248 G_TYPE_OBJECT)
251 main (int argc,
252 char *argv[])
254 TestObject *object;
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);
280 bool_result = FALSE;
281 g_signal_emit_by_name (object, "test-signal2", 1, &bool_result);
282 g_assert (bool_result == TRUE);
283 bool_result = FALSE;
284 g_signal_emit_by_name (object, "test-signal2", 2, &bool_result);
285 g_assert (bool_result == TRUE);
286 bool_result = FALSE;
287 g_signal_emit_by_name (object, "test-signal2", 3, &bool_result);
288 g_assert (bool_result == TRUE);
289 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);
306 return 0;