os/ossrv/glib/tsrc/BC/tests/gobject/accumulator.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* GObject - GLib Type, Object, Parameter and Signal Library
     2  * Copyright (C) 2001, 2003 Red Hat, Inc.
     3  * Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). 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 SYMBIAN
    35 #include "mrt2_glib2_test.h"
    36 #endif //SYMBIAN
    37 
    38 /* What this test tests is the behavior of signal accumulators
    39  * Two accumulators are tested:
    40  *
    41  * 1: A custom accumulator that appends the returned strings
    42  * 2: The standard g_signal_accumulator_true_handled that stops
    43  *    emission on TRUE returns.
    44  */
    45 
    46 /*
    47  * TestObject, a parent class for TestObject
    48  */
    49 #define TEST_TYPE_OBJECT          (test_object_get_type ())
    50 typedef struct _TestObject        TestObject;
    51 typedef struct _TestObjectClass   TestObjectClass;
    52 
    53 struct _TestObject
    54 {
    55   GObject parent_instance;
    56 };
    57 struct _TestObjectClass
    58 {
    59   GObjectClass parent_class;
    60 
    61   gchar*   (*test_signal1) (TestObject *tobject,
    62 			    gint        param);
    63   gboolean (*test_signal2) (TestObject *tobject,
    64 			    gint        param);
    65 };
    66 
    67 static GType test_object_get_type (void);
    68 
    69 static gboolean
    70 test_signal1_accumulator (GSignalInvocationHint *ihint,
    71 			  GValue                *return_accu,
    72 			  const GValue          *handler_return,
    73 			  gpointer               data)
    74 {
    75   const gchar *accu_string = g_value_get_string (return_accu);
    76   const gchar *new_string = g_value_get_string (handler_return);
    77   gchar *result_string;
    78 
    79   if (accu_string)
    80     result_string = g_strconcat (accu_string, new_string, NULL);
    81   else if (new_string)
    82     result_string = g_strdup (new_string);
    83   else
    84     result_string = NULL;
    85 
    86   g_value_set_string_take_ownership (return_accu, result_string);
    87 
    88   return TRUE;
    89 }
    90 
    91 gchar*
    92 test_object_signal1_callback_before (TestObject *tobject,
    93 				     gint        param,
    94 				     gpointer    data)
    95 {
    96   return g_strdup ("<before>");
    97 }
    98 
    99 gchar*
   100 test_object_real_signal1 (TestObject *tobject,
   101 			  gint        param)
   102 {
   103   return g_strdup ("<default>");
   104 }
   105 
   106 gchar*
   107 test_object_signal1_callback_after (TestObject *tobject,
   108 				    gint        param,
   109 				    gpointer    data)
   110 {
   111   return g_strdup ("<after>");
   112 }
   113 
   114 gboolean
   115 test_object_signal2_callback_before (TestObject *tobject,
   116 				     gint        param)
   117 {
   118   switch (param)
   119     {
   120     case 1: return TRUE;
   121     case 2: return FALSE;
   122     case 3: return FALSE;
   123     case 4: return FALSE;
   124     }
   125 
   126   g_assert_not_reached ();
   127   return FALSE;
   128 }
   129 
   130 gboolean
   131 test_object_real_signal2 (TestObject *tobject,
   132 			  gint        param)
   133 {
   134   switch (param)
   135     {
   136     case 1: g_assert_not_reached (); return FALSE;
   137     case 2: return TRUE;
   138     case 3: return FALSE;
   139     case 4: return FALSE;
   140     }
   141   
   142   g_assert_not_reached ();
   143   return FALSE;
   144 }
   145 
   146 gboolean
   147 test_object_signal2_callback_after (TestObject *tobject,
   148 				     gint        param)
   149 {
   150   switch (param)
   151     {
   152     case 1: g_assert_not_reached (); return FALSE;
   153     case 2: g_assert_not_reached (); return FALSE;
   154     case 3: return TRUE;
   155     case 4: return FALSE;
   156     }
   157       
   158   g_assert_not_reached ();
   159   return FALSE;
   160 }
   161 
   162 static void
   163 test_object_class_init (TestObjectClass *class)
   164 {
   165   class->test_signal1 = test_object_real_signal1;
   166   class->test_signal2 = test_object_real_signal2;
   167   
   168   g_signal_new ("test-signal1",
   169 		G_OBJECT_CLASS_TYPE (class),
   170 		G_SIGNAL_RUN_LAST,
   171 		G_STRUCT_OFFSET (TestObjectClass, test_signal1),
   172 		test_signal1_accumulator, NULL,
   173 		test_marshal_STRING__INT,
   174 		G_TYPE_STRING, 1, G_TYPE_INT);
   175   g_signal_new ("test-signal2",
   176 		G_OBJECT_CLASS_TYPE (class),
   177 		G_SIGNAL_RUN_LAST,
   178 		G_STRUCT_OFFSET (TestObjectClass, test_signal2),
   179 		g_signal_accumulator_true_handled, NULL,
   180 		test_marshal_BOOLEAN__INT,
   181 		G_TYPE_BOOLEAN, 1, G_TYPE_INT);
   182 }
   183 
   184 static DEFINE_TYPE(TestObject, test_object,
   185 		   test_object_class_init, NULL, NULL,
   186 		   G_TYPE_OBJECT)
   187 
   188 int
   189 main (int   argc,
   190       char *argv[])
   191 {
   192  
   193   TestObject *object;
   194   gchar *string_result;
   195   gboolean bool_result;
   196 	#ifdef SYMBIAN
   197   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);
   198   g_set_print_handler(mrtPrintHandler);
   199   #endif /*SYMBIAN*/
   200   g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
   201 			  G_LOG_LEVEL_WARNING |
   202 			  G_LOG_LEVEL_CRITICAL);
   203   g_type_init ();
   204 
   205   object = g_object_new (TEST_TYPE_OBJECT, NULL);
   206 
   207   g_signal_connect (object, "test-signal1",
   208 		    G_CALLBACK (test_object_signal1_callback_before), NULL);
   209   g_signal_connect_after (object, "test-signal1",
   210 			  G_CALLBACK (test_object_signal1_callback_after), NULL);
   211   
   212   g_signal_emit_by_name (object, "test-signal1", 0, &string_result);
   213   g_assert (strcmp (string_result, "<before><default><after>") == 0);
   214   g_free (string_result);
   215 
   216   g_signal_connect (object, "test-signal2",
   217 		    G_CALLBACK (test_object_signal2_callback_before), NULL);
   218   g_signal_connect_after (object, "test-signal2",
   219 			  G_CALLBACK (test_object_signal2_callback_after), NULL);
   220   
   221   bool_result = FALSE;
   222   g_signal_emit_by_name (object, "test-signal2", 1, &bool_result);
   223   g_assert (bool_result == TRUE);
   224   bool_result = FALSE;
   225   g_signal_emit_by_name (object, "test-signal2", 2, &bool_result);
   226   g_assert (bool_result == TRUE);
   227   bool_result = FALSE;
   228   g_signal_emit_by_name (object, "test-signal2", 3, &bool_result);
   229   g_assert (bool_result == TRUE);
   230   bool_result = TRUE;
   231   g_signal_emit_by_name (object, "test-signal2", 4, &bool_result);
   232   g_assert (bool_result == FALSE);
   233 #ifdef SYMBIAN
   234 testResultXml("accumulator");
   235 #endif  //SYMBIAN
   236 
   237 
   238   return 0;
   239 }