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,
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_object_class_init (TestObjectClass *class)
161 class->test_signal1 = test_object_real_signal1;
162 class->test_signal2 = test_object_real_signal2;
164 g_signal_new ("test-signal1",
165 G_OBJECT_CLASS_TYPE (class),
167 G_STRUCT_OFFSET (TestObjectClass, test_signal1),
168 test_signal1_accumulator, NULL,
169 test_marshal_STRING__INT,
170 G_TYPE_STRING, 1, G_TYPE_INT);
171 g_signal_new ("test-signal2",
172 G_OBJECT_CLASS_TYPE (class),
174 G_STRUCT_OFFSET (TestObjectClass, test_signal2),
175 g_signal_accumulator_true_handled, NULL,
176 test_marshal_BOOLEAN__INT,
177 G_TYPE_BOOLEAN, 1, G_TYPE_INT);
180 static DEFINE_TYPE(TestObject, test_object,
181 test_object_class_init, NULL, NULL,
189 gchar *string_result;
190 gboolean bool_result;
192 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
193 G_LOG_LEVEL_WARNING |
194 G_LOG_LEVEL_CRITICAL);
197 object = g_object_new (TEST_TYPE_OBJECT, NULL);
199 g_signal_connect (object, "test-signal1",
200 G_CALLBACK (test_object_signal1_callback_before), NULL);
201 g_signal_connect_after (object, "test-signal1",
202 G_CALLBACK (test_object_signal1_callback_after), NULL);
204 g_signal_emit_by_name (object, "test-signal1", 0, &string_result);
205 g_assert (strcmp (string_result, "<before><default><after>") == 0);
206 g_free (string_result);
208 g_signal_connect (object, "test-signal2",
209 G_CALLBACK (test_object_signal2_callback_before), NULL);
210 g_signal_connect_after (object, "test-signal2",
211 G_CALLBACK (test_object_signal2_callback_after), NULL);
214 g_signal_emit_by_name (object, "test-signal2", 1, &bool_result);
215 g_assert (bool_result == TRUE);
217 g_signal_emit_by_name (object, "test-signal2", 2, &bool_result);
218 g_assert (bool_result == TRUE);
220 g_signal_emit_by_name (object, "test-signal2", 3, &bool_result);
221 g_assert (bool_result == TRUE);
223 g_signal_emit_by_name (object, "test-signal2", 4, &bool_result);
224 g_assert (bool_result == FALSE);