Update contrib.
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2001, 2003 Red Hat, Inc.
3 * Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved.
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"
35 #include "mrt2_glib2_test.h"
36 #endif //__SYMBIAN32__
37 /* What this test tests is the behavior of signal accumulators
38 * Two accumulators are tested:
40 * 1: A custom accumulator that appends the returned strings
41 * 2: The standard g_signal_accumulator_true_handled that stops
42 * emission on TRUE returns.
46 * TestObject, a parent class for TestObject
48 #define TEST_TYPE_OBJECT (test_object_get_type ())
49 typedef struct _TestObject TestObject;
50 typedef struct _TestObjectClass TestObjectClass;
54 GObject parent_instance;
56 struct _TestObjectClass
58 GObjectClass parent_class;
60 gchar* (*test_signal1) (TestObject *tobject,
62 gboolean (*test_signal2) (TestObject *tobject,
66 static GType test_object_get_type (void);
69 test_signal1_accumulator (GSignalInvocationHint *ihint,
71 const GValue *handler_return,
74 const gchar *accu_string = g_value_get_string (return_accu);
75 const gchar *new_string = g_value_get_string (handler_return);
79 result_string = g_strconcat (accu_string, new_string, NULL);
81 result_string = g_strdup (new_string);
85 g_value_set_string_take_ownership (return_accu, result_string);
91 test_object_signal1_callback_before (TestObject *tobject,
95 return g_strdup ("<before>");
99 test_object_real_signal1 (TestObject *tobject,
102 return g_strdup ("<default>");
106 test_object_signal1_callback_after (TestObject *tobject,
110 return g_strdup ("<after>");
114 test_object_signal2_callback_before (TestObject *tobject,
120 case 2: return FALSE;
121 case 3: return FALSE;
122 case 4: return FALSE;
125 g_assert_not_reached ();
130 test_object_real_signal2 (TestObject *tobject,
135 case 1: g_assert_not_reached (); return FALSE;
137 case 3: return FALSE;
138 case 4: return FALSE;
141 g_assert_not_reached ();
146 test_object_signal2_callback_after (TestObject *tobject,
151 case 1: g_assert_not_reached (); return FALSE;
152 case 2: g_assert_not_reached (); return FALSE;
154 case 4: return FALSE;
157 g_assert_not_reached ();
162 test_object_class_init (TestObjectClass *class)
164 class->test_signal1 = test_object_real_signal1;
165 class->test_signal2 = test_object_real_signal2;
167 g_signal_new ("test-signal1",
168 G_OBJECT_CLASS_TYPE (class),
170 G_STRUCT_OFFSET (TestObjectClass, test_signal1),
171 test_signal1_accumulator, NULL,
172 test_marshal_STRING__INT,
173 G_TYPE_STRING, 1, G_TYPE_INT);
174 g_signal_new ("test-signal2",
175 G_OBJECT_CLASS_TYPE (class),
177 G_STRUCT_OFFSET (TestObjectClass, test_signal2),
178 g_signal_accumulator_true_handled, NULL,
179 test_marshal_BOOLEAN__INT,
180 G_TYPE_BOOLEAN, 1, G_TYPE_INT);
183 static DEFINE_TYPE(TestObject, test_object,
184 test_object_class_init, NULL, NULL,
192 gchar *string_result;
193 gboolean bool_result;
195 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
196 g_set_print_handler(mrtPrintHandler);
197 #endif /*__SYMBIAN32__*/
198 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
199 G_LOG_LEVEL_WARNING |
200 G_LOG_LEVEL_CRITICAL);
203 object = g_object_new (TEST_TYPE_OBJECT, NULL);
205 g_signal_connect (object, "test-signal1",
206 G_CALLBACK (test_object_signal1_callback_before), NULL);
207 g_signal_connect_after (object, "test-signal1",
208 G_CALLBACK (test_object_signal1_callback_after), NULL);
210 g_signal_emit_by_name (object, "test-signal1", 0, &string_result);
211 g_assert (strcmp (string_result, "<before><default><after>") == 0);
212 g_free (string_result);
214 g_signal_connect (object, "test-signal2",
215 G_CALLBACK (test_object_signal2_callback_before), NULL);
216 g_signal_connect_after (object, "test-signal2",
217 G_CALLBACK (test_object_signal2_callback_after), NULL);
220 g_signal_emit_by_name (object, "test-signal2", 1, &bool_result);
221 g_assert (bool_result == TRUE);
223 g_signal_emit_by_name (object, "test-signal2", 2, &bool_result);
224 g_assert (bool_result == TRUE);
226 g_signal_emit_by_name (object, "test-signal2", 3, &bool_result);
227 g_assert (bool_result == TRUE);
229 g_signal_emit_by_name (object, "test-signal2", 4, &bool_result);
230 g_assert (bool_result == FALSE);
232 testResultXml("accumulator");
233 #endif //__SYMBIAN32__