sl@0: /* GObject - GLib Type, Object, Parameter and Signal Library sl@0: * Copyright (C) 2001, 2003 Red Hat, Inc. sl@0: * Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved. sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General sl@0: * Public License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place, Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: */ sl@0: sl@0: #undef G_LOG_DOMAIN sl@0: #define G_LOG_DOMAIN "TestAccumulator" sl@0: sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_DISABLE_CHECKS sl@0: #undef G_DISABLE_CAST_CHECKS sl@0: sl@0: #include sl@0: sl@0: #include sl@0: sl@0: #include "testmarshal.h" sl@0: #include "testcommon.h" sl@0: sl@0: #ifdef __SYMBIAN32__ sl@0: #include "mrt2_glib2_test.h" sl@0: #endif //__SYMBIAN32__ sl@0: /* What this test tests is the behavior of signal accumulators sl@0: * Two accumulators are tested: sl@0: * sl@0: * 1: A custom accumulator that appends the returned strings sl@0: * 2: The standard g_signal_accumulator_true_handled that stops sl@0: * emission on TRUE returns. sl@0: */ sl@0: sl@0: /* sl@0: * TestObject, a parent class for TestObject sl@0: */ sl@0: #define TEST_TYPE_OBJECT (test_object_get_type ()) sl@0: typedef struct _TestObject TestObject; sl@0: typedef struct _TestObjectClass TestObjectClass; sl@0: sl@0: struct _TestObject sl@0: { sl@0: GObject parent_instance; sl@0: }; sl@0: struct _TestObjectClass sl@0: { sl@0: GObjectClass parent_class; sl@0: sl@0: gchar* (*test_signal1) (TestObject *tobject, sl@0: gint param); sl@0: gboolean (*test_signal2) (TestObject *tobject, sl@0: gint param); sl@0: }; sl@0: sl@0: static GType test_object_get_type (void); sl@0: sl@0: static gboolean sl@0: test_signal1_accumulator (GSignalInvocationHint *ihint, sl@0: GValue *return_accu, sl@0: const GValue *handler_return, sl@0: gpointer data) sl@0: { sl@0: const gchar *accu_string = g_value_get_string (return_accu); sl@0: const gchar *new_string = g_value_get_string (handler_return); sl@0: gchar *result_string; sl@0: sl@0: if (accu_string) sl@0: result_string = g_strconcat (accu_string, new_string, NULL); sl@0: else if (new_string) sl@0: result_string = g_strdup (new_string); sl@0: else sl@0: result_string = NULL; sl@0: sl@0: g_value_set_string_take_ownership (return_accu, result_string); sl@0: sl@0: return TRUE; sl@0: } sl@0: sl@0: gchar* sl@0: test_object_signal1_callback_before (TestObject *tobject, sl@0: gint param, sl@0: gpointer data) sl@0: { sl@0: return g_strdup (""); sl@0: } sl@0: sl@0: gchar* sl@0: test_object_real_signal1 (TestObject *tobject, sl@0: gint param) sl@0: { sl@0: return g_strdup (""); sl@0: } sl@0: sl@0: gchar* sl@0: test_object_signal1_callback_after (TestObject *tobject, sl@0: gint param, sl@0: gpointer data) sl@0: { sl@0: return g_strdup (""); sl@0: } sl@0: sl@0: gboolean sl@0: test_object_signal2_callback_before (TestObject *tobject, sl@0: gint param) sl@0: { sl@0: switch (param) sl@0: { sl@0: case 1: return TRUE; sl@0: case 2: return FALSE; sl@0: case 3: return FALSE; sl@0: case 4: return FALSE; sl@0: } sl@0: sl@0: g_assert_not_reached (); sl@0: return FALSE; sl@0: } sl@0: sl@0: gboolean sl@0: test_object_real_signal2 (TestObject *tobject, sl@0: gint param) sl@0: { sl@0: switch (param) sl@0: { sl@0: case 1: g_assert_not_reached (); return FALSE; sl@0: case 2: return TRUE; sl@0: case 3: return FALSE; sl@0: case 4: return FALSE; sl@0: } sl@0: sl@0: g_assert_not_reached (); sl@0: return FALSE; sl@0: } sl@0: sl@0: gboolean sl@0: test_object_signal2_callback_after (TestObject *tobject, sl@0: gint param) sl@0: { sl@0: switch (param) sl@0: { sl@0: case 1: g_assert_not_reached (); return FALSE; sl@0: case 2: g_assert_not_reached (); return FALSE; sl@0: case 3: return TRUE; sl@0: case 4: return FALSE; sl@0: } sl@0: sl@0: g_assert_not_reached (); sl@0: return FALSE; sl@0: } sl@0: sl@0: static void sl@0: test_object_class_init (TestObjectClass *class) sl@0: { sl@0: class->test_signal1 = test_object_real_signal1; sl@0: class->test_signal2 = test_object_real_signal2; sl@0: sl@0: g_signal_new ("test-signal1", sl@0: G_OBJECT_CLASS_TYPE (class), sl@0: G_SIGNAL_RUN_LAST, sl@0: G_STRUCT_OFFSET (TestObjectClass, test_signal1), sl@0: test_signal1_accumulator, NULL, sl@0: test_marshal_STRING__INT, sl@0: G_TYPE_STRING, 1, G_TYPE_INT); sl@0: g_signal_new ("test-signal2", sl@0: G_OBJECT_CLASS_TYPE (class), sl@0: G_SIGNAL_RUN_LAST, sl@0: G_STRUCT_OFFSET (TestObjectClass, test_signal2), sl@0: g_signal_accumulator_true_handled, NULL, sl@0: test_marshal_BOOLEAN__INT, sl@0: G_TYPE_BOOLEAN, 1, G_TYPE_INT); sl@0: } sl@0: sl@0: static DEFINE_TYPE(TestObject, test_object, sl@0: test_object_class_init, NULL, NULL, sl@0: G_TYPE_OBJECT) sl@0: sl@0: int sl@0: main (int argc, sl@0: char *argv[]) sl@0: { sl@0: TestObject *object; sl@0: gchar *string_result; sl@0: gboolean bool_result; sl@0: #ifdef __SYMBIAN32__ sl@0: 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); sl@0: g_set_print_handler(mrtPrintHandler); sl@0: #endif /*__SYMBIAN32__*/ sl@0: g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) | sl@0: G_LOG_LEVEL_WARNING | sl@0: G_LOG_LEVEL_CRITICAL); sl@0: g_type_init (); sl@0: sl@0: object = g_object_new (TEST_TYPE_OBJECT, NULL); sl@0: sl@0: g_signal_connect (object, "test-signal1", sl@0: G_CALLBACK (test_object_signal1_callback_before), NULL); sl@0: g_signal_connect_after (object, "test-signal1", sl@0: G_CALLBACK (test_object_signal1_callback_after), NULL); sl@0: sl@0: g_signal_emit_by_name (object, "test-signal1", 0, &string_result); sl@0: g_assert (strcmp (string_result, "") == 0); sl@0: g_free (string_result); sl@0: sl@0: g_signal_connect (object, "test-signal2", sl@0: G_CALLBACK (test_object_signal2_callback_before), NULL); sl@0: g_signal_connect_after (object, "test-signal2", sl@0: G_CALLBACK (test_object_signal2_callback_after), NULL); sl@0: sl@0: bool_result = FALSE; sl@0: g_signal_emit_by_name (object, "test-signal2", 1, &bool_result); sl@0: g_assert (bool_result == TRUE); sl@0: bool_result = FALSE; sl@0: g_signal_emit_by_name (object, "test-signal2", 2, &bool_result); sl@0: g_assert (bool_result == TRUE); sl@0: bool_result = FALSE; sl@0: g_signal_emit_by_name (object, "test-signal2", 3, &bool_result); sl@0: g_assert (bool_result == TRUE); sl@0: bool_result = TRUE; sl@0: g_signal_emit_by_name (object, "test-signal2", 4, &bool_result); sl@0: g_assert (bool_result == FALSE); sl@0: #ifdef __SYMBIAN32__ sl@0: testResultXml("accumulator"); sl@0: #endif //__SYMBIAN32__ sl@0: sl@0: return 0; sl@0: }