glib/tests/gobject/accumulator.c
changeset 18 47c74d1534e1
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     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.
       
     8  *
       
     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.
       
    13  *
       
    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.
       
    18  */
       
    19 
       
    20 #undef	G_LOG_DOMAIN
       
    21 #define	G_LOG_DOMAIN "TestAccumulator"
       
    22 
       
    23 #undef G_DISABLE_ASSERT
       
    24 #undef G_DISABLE_CHECKS
       
    25 #undef G_DISABLE_CAST_CHECKS
       
    26 
       
    27 #include <string.h>
       
    28 
       
    29 #include	<glib-object.h>
       
    30 
       
    31 #include "testmarshal.h"
       
    32 #include "testcommon.h"
       
    33 
       
    34 #ifdef __SYMBIAN32__
       
    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:
       
    39  *
       
    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.
       
    43  */
       
    44 
       
    45 /*
       
    46  * TestObject, a parent class for TestObject
       
    47  */
       
    48 #define TEST_TYPE_OBJECT          (test_object_get_type ())
       
    49 typedef struct _TestObject        TestObject;
       
    50 typedef struct _TestObjectClass   TestObjectClass;
       
    51 
       
    52 struct _TestObject
       
    53 {
       
    54   GObject parent_instance;
       
    55 };
       
    56 struct _TestObjectClass
       
    57 {
       
    58   GObjectClass parent_class;
       
    59 
       
    60   gchar*   (*test_signal1) (TestObject *tobject,
       
    61 			    gint        param);
       
    62   gboolean (*test_signal2) (TestObject *tobject,
       
    63 			    gint        param);
       
    64 };
       
    65 
       
    66 static GType test_object_get_type (void);
       
    67 
       
    68 static gboolean
       
    69 test_signal1_accumulator (GSignalInvocationHint *ihint,
       
    70 			  GValue                *return_accu,
       
    71 			  const GValue          *handler_return,
       
    72 			  gpointer               data)
       
    73 {
       
    74   const gchar *accu_string = g_value_get_string (return_accu);
       
    75   const gchar *new_string = g_value_get_string (handler_return);
       
    76   gchar *result_string;
       
    77 
       
    78   if (accu_string)
       
    79     result_string = g_strconcat (accu_string, new_string, NULL);
       
    80   else if (new_string)
       
    81     result_string = g_strdup (new_string);
       
    82   else
       
    83     result_string = NULL;
       
    84 
       
    85   g_value_set_string_take_ownership (return_accu, result_string);
       
    86 
       
    87   return TRUE;
       
    88 }
       
    89 
       
    90 gchar*
       
    91 test_object_signal1_callback_before (TestObject *tobject,
       
    92 				     gint        param,
       
    93 				     gpointer    data)
       
    94 {
       
    95   return g_strdup ("<before>");
       
    96 }
       
    97 
       
    98 gchar*
       
    99 test_object_real_signal1 (TestObject *tobject,
       
   100 			  gint        param)
       
   101 {
       
   102   return g_strdup ("<default>");
       
   103 }
       
   104 
       
   105 gchar*
       
   106 test_object_signal1_callback_after (TestObject *tobject,
       
   107 				    gint        param,
       
   108 				    gpointer    data)
       
   109 {
       
   110   return g_strdup ("<after>");
       
   111 }
       
   112 
       
   113 gboolean
       
   114 test_object_signal2_callback_before (TestObject *tobject,
       
   115 				     gint        param)
       
   116 {
       
   117   switch (param)
       
   118     {
       
   119     case 1: return TRUE;
       
   120     case 2: return FALSE;
       
   121     case 3: return FALSE;
       
   122     case 4: return FALSE;
       
   123     }
       
   124 
       
   125   g_assert_not_reached ();
       
   126   return FALSE;
       
   127 }
       
   128 
       
   129 gboolean
       
   130 test_object_real_signal2 (TestObject *tobject,
       
   131 			  gint        param)
       
   132 {
       
   133   switch (param)
       
   134     {
       
   135     case 1: g_assert_not_reached (); return FALSE;
       
   136     case 2: return TRUE;
       
   137     case 3: return FALSE;
       
   138     case 4: return FALSE;
       
   139     }
       
   140   
       
   141   g_assert_not_reached ();
       
   142   return FALSE;
       
   143 }
       
   144 
       
   145 gboolean
       
   146 test_object_signal2_callback_after (TestObject *tobject,
       
   147 				     gint        param)
       
   148 {
       
   149   switch (param)
       
   150     {
       
   151     case 1: g_assert_not_reached (); return FALSE;
       
   152     case 2: g_assert_not_reached (); return FALSE;
       
   153     case 3: return TRUE;
       
   154     case 4: return FALSE;
       
   155     }
       
   156       
       
   157   g_assert_not_reached ();
       
   158   return FALSE;
       
   159 }
       
   160 
       
   161 static void
       
   162 test_object_class_init (TestObjectClass *class)
       
   163 {
       
   164   class->test_signal1 = test_object_real_signal1;
       
   165   class->test_signal2 = test_object_real_signal2;
       
   166   
       
   167   g_signal_new ("test-signal1",
       
   168 		G_OBJECT_CLASS_TYPE (class),
       
   169 		G_SIGNAL_RUN_LAST,
       
   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),
       
   176 		G_SIGNAL_RUN_LAST,
       
   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);
       
   181 }
       
   182 
       
   183 static DEFINE_TYPE(TestObject, test_object,
       
   184 		   test_object_class_init, NULL, NULL,
       
   185 		   G_TYPE_OBJECT)
       
   186 
       
   187 int
       
   188 main (int   argc,
       
   189       char *argv[])
       
   190 {
       
   191   TestObject *object;
       
   192   gchar *string_result;
       
   193   gboolean bool_result;
       
   194 	#ifdef __SYMBIAN32__
       
   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);
       
   201   g_type_init ();
       
   202 
       
   203   object = g_object_new (TEST_TYPE_OBJECT, NULL);
       
   204 
       
   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);
       
   209   
       
   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);
       
   213 
       
   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);
       
   218   
       
   219   bool_result = FALSE;
       
   220   g_signal_emit_by_name (object, "test-signal2", 1, &bool_result);
       
   221   g_assert (bool_result == TRUE);
       
   222   bool_result = FALSE;
       
   223   g_signal_emit_by_name (object, "test-signal2", 2, &bool_result);
       
   224   g_assert (bool_result == TRUE);
       
   225   bool_result = FALSE;
       
   226   g_signal_emit_by_name (object, "test-signal2", 3, &bool_result);
       
   227   g_assert (bool_result == TRUE);
       
   228   bool_result = TRUE;
       
   229   g_signal_emit_by_name (object, "test-signal2", 4, &bool_result);
       
   230   g_assert (bool_result == FALSE);
       
   231 #ifdef __SYMBIAN32__
       
   232 testResultXml("accumulator");
       
   233 #endif  //__SYMBIAN32__
       
   234 
       
   235   return 0;
       
   236 }