1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tests/gobject/accumulator.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,236 @@
1.4 +/* GObject - GLib Type, Object, Parameter and Signal Library
1.5 + * Copyright (C) 2001, 2003 Red Hat, Inc.
1.6 + * Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved.
1.7 + * This library is free software; you can redistribute it and/or
1.8 + * modify it under the terms of the GNU Lesser General Public
1.9 + * License as published by the Free Software Foundation; either
1.10 + * version 2 of the License, or (at your option) any later version.
1.11 + *
1.12 + * This library is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.15 + * Lesser General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU Lesser General
1.18 + * Public License along with this library; if not, write to the
1.19 + * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1.20 + * Boston, MA 02111-1307, USA.
1.21 + */
1.22 +
1.23 +#undef G_LOG_DOMAIN
1.24 +#define G_LOG_DOMAIN "TestAccumulator"
1.25 +
1.26 +#undef G_DISABLE_ASSERT
1.27 +#undef G_DISABLE_CHECKS
1.28 +#undef G_DISABLE_CAST_CHECKS
1.29 +
1.30 +#include <string.h>
1.31 +
1.32 +#include <glib-object.h>
1.33 +
1.34 +#include "testmarshal.h"
1.35 +#include "testcommon.h"
1.36 +
1.37 +#ifdef __SYMBIAN32__
1.38 +#include "mrt2_glib2_test.h"
1.39 +#endif //__SYMBIAN32__
1.40 +/* What this test tests is the behavior of signal accumulators
1.41 + * Two accumulators are tested:
1.42 + *
1.43 + * 1: A custom accumulator that appends the returned strings
1.44 + * 2: The standard g_signal_accumulator_true_handled that stops
1.45 + * emission on TRUE returns.
1.46 + */
1.47 +
1.48 +/*
1.49 + * TestObject, a parent class for TestObject
1.50 + */
1.51 +#define TEST_TYPE_OBJECT (test_object_get_type ())
1.52 +typedef struct _TestObject TestObject;
1.53 +typedef struct _TestObjectClass TestObjectClass;
1.54 +
1.55 +struct _TestObject
1.56 +{
1.57 + GObject parent_instance;
1.58 +};
1.59 +struct _TestObjectClass
1.60 +{
1.61 + GObjectClass parent_class;
1.62 +
1.63 + gchar* (*test_signal1) (TestObject *tobject,
1.64 + gint param);
1.65 + gboolean (*test_signal2) (TestObject *tobject,
1.66 + gint param);
1.67 +};
1.68 +
1.69 +static GType test_object_get_type (void);
1.70 +
1.71 +static gboolean
1.72 +test_signal1_accumulator (GSignalInvocationHint *ihint,
1.73 + GValue *return_accu,
1.74 + const GValue *handler_return,
1.75 + gpointer data)
1.76 +{
1.77 + const gchar *accu_string = g_value_get_string (return_accu);
1.78 + const gchar *new_string = g_value_get_string (handler_return);
1.79 + gchar *result_string;
1.80 +
1.81 + if (accu_string)
1.82 + result_string = g_strconcat (accu_string, new_string, NULL);
1.83 + else if (new_string)
1.84 + result_string = g_strdup (new_string);
1.85 + else
1.86 + result_string = NULL;
1.87 +
1.88 + g_value_set_string_take_ownership (return_accu, result_string);
1.89 +
1.90 + return TRUE;
1.91 +}
1.92 +
1.93 +gchar*
1.94 +test_object_signal1_callback_before (TestObject *tobject,
1.95 + gint param,
1.96 + gpointer data)
1.97 +{
1.98 + return g_strdup ("<before>");
1.99 +}
1.100 +
1.101 +gchar*
1.102 +test_object_real_signal1 (TestObject *tobject,
1.103 + gint param)
1.104 +{
1.105 + return g_strdup ("<default>");
1.106 +}
1.107 +
1.108 +gchar*
1.109 +test_object_signal1_callback_after (TestObject *tobject,
1.110 + gint param,
1.111 + gpointer data)
1.112 +{
1.113 + return g_strdup ("<after>");
1.114 +}
1.115 +
1.116 +gboolean
1.117 +test_object_signal2_callback_before (TestObject *tobject,
1.118 + gint param)
1.119 +{
1.120 + switch (param)
1.121 + {
1.122 + case 1: return TRUE;
1.123 + case 2: return FALSE;
1.124 + case 3: return FALSE;
1.125 + case 4: return FALSE;
1.126 + }
1.127 +
1.128 + g_assert_not_reached ();
1.129 + return FALSE;
1.130 +}
1.131 +
1.132 +gboolean
1.133 +test_object_real_signal2 (TestObject *tobject,
1.134 + gint param)
1.135 +{
1.136 + switch (param)
1.137 + {
1.138 + case 1: g_assert_not_reached (); return FALSE;
1.139 + case 2: return TRUE;
1.140 + case 3: return FALSE;
1.141 + case 4: return FALSE;
1.142 + }
1.143 +
1.144 + g_assert_not_reached ();
1.145 + return FALSE;
1.146 +}
1.147 +
1.148 +gboolean
1.149 +test_object_signal2_callback_after (TestObject *tobject,
1.150 + gint param)
1.151 +{
1.152 + switch (param)
1.153 + {
1.154 + case 1: g_assert_not_reached (); return FALSE;
1.155 + case 2: g_assert_not_reached (); return FALSE;
1.156 + case 3: return TRUE;
1.157 + case 4: return FALSE;
1.158 + }
1.159 +
1.160 + g_assert_not_reached ();
1.161 + return FALSE;
1.162 +}
1.163 +
1.164 +static void
1.165 +test_object_class_init (TestObjectClass *class)
1.166 +{
1.167 + class->test_signal1 = test_object_real_signal1;
1.168 + class->test_signal2 = test_object_real_signal2;
1.169 +
1.170 + g_signal_new ("test-signal1",
1.171 + G_OBJECT_CLASS_TYPE (class),
1.172 + G_SIGNAL_RUN_LAST,
1.173 + G_STRUCT_OFFSET (TestObjectClass, test_signal1),
1.174 + test_signal1_accumulator, NULL,
1.175 + test_marshal_STRING__INT,
1.176 + G_TYPE_STRING, 1, G_TYPE_INT);
1.177 + g_signal_new ("test-signal2",
1.178 + G_OBJECT_CLASS_TYPE (class),
1.179 + G_SIGNAL_RUN_LAST,
1.180 + G_STRUCT_OFFSET (TestObjectClass, test_signal2),
1.181 + g_signal_accumulator_true_handled, NULL,
1.182 + test_marshal_BOOLEAN__INT,
1.183 + G_TYPE_BOOLEAN, 1, G_TYPE_INT);
1.184 +}
1.185 +
1.186 +static DEFINE_TYPE(TestObject, test_object,
1.187 + test_object_class_init, NULL, NULL,
1.188 + G_TYPE_OBJECT)
1.189 +
1.190 +int
1.191 +main (int argc,
1.192 + char *argv[])
1.193 +{
1.194 + TestObject *object;
1.195 + gchar *string_result;
1.196 + gboolean bool_result;
1.197 + #ifdef __SYMBIAN32__
1.198 + 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);
1.199 + g_set_print_handler(mrtPrintHandler);
1.200 + #endif /*__SYMBIAN32__*/
1.201 + g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
1.202 + G_LOG_LEVEL_WARNING |
1.203 + G_LOG_LEVEL_CRITICAL);
1.204 + g_type_init ();
1.205 +
1.206 + object = g_object_new (TEST_TYPE_OBJECT, NULL);
1.207 +
1.208 + g_signal_connect (object, "test-signal1",
1.209 + G_CALLBACK (test_object_signal1_callback_before), NULL);
1.210 + g_signal_connect_after (object, "test-signal1",
1.211 + G_CALLBACK (test_object_signal1_callback_after), NULL);
1.212 +
1.213 + g_signal_emit_by_name (object, "test-signal1", 0, &string_result);
1.214 + g_assert (strcmp (string_result, "<before><default><after>") == 0);
1.215 + g_free (string_result);
1.216 +
1.217 + g_signal_connect (object, "test-signal2",
1.218 + G_CALLBACK (test_object_signal2_callback_before), NULL);
1.219 + g_signal_connect_after (object, "test-signal2",
1.220 + G_CALLBACK (test_object_signal2_callback_after), NULL);
1.221 +
1.222 + bool_result = FALSE;
1.223 + g_signal_emit_by_name (object, "test-signal2", 1, &bool_result);
1.224 + g_assert (bool_result == TRUE);
1.225 + bool_result = FALSE;
1.226 + g_signal_emit_by_name (object, "test-signal2", 2, &bool_result);
1.227 + g_assert (bool_result == TRUE);
1.228 + bool_result = FALSE;
1.229 + g_signal_emit_by_name (object, "test-signal2", 3, &bool_result);
1.230 + g_assert (bool_result == TRUE);
1.231 + bool_result = TRUE;
1.232 + g_signal_emit_by_name (object, "test-signal2", 4, &bool_result);
1.233 + g_assert (bool_result == FALSE);
1.234 +#ifdef __SYMBIAN32__
1.235 +testResultXml("accumulator");
1.236 +#endif //__SYMBIAN32__
1.237 +
1.238 + return 0;
1.239 +}