os/ossrv/glib/tests/gobject/accumulator.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* GObject - GLib Type, Object, Parameter and Signal Library
sl@0
     2
 * Copyright (C) 2001, 2003 Red Hat, Inc.
sl@0
     3
 * Portions copyright (c) 2006-2009 Nokia Corporation.  All rights reserved.
sl@0
     4
 * This library is free software; you can redistribute it and/or
sl@0
     5
 * modify it under the terms of the GNU Lesser General Public
sl@0
     6
 * License as published by the Free Software Foundation; either
sl@0
     7
 * version 2 of the License, or (at your option) any later version.
sl@0
     8
 *
sl@0
     9
 * This library is distributed in the hope that it will be useful,
sl@0
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
sl@0
    12
 * Lesser General Public License for more details.
sl@0
    13
 *
sl@0
    14
 * You should have received a copy of the GNU Lesser General
sl@0
    15
 * Public License along with this library; if not, write to the
sl@0
    16
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
sl@0
    17
 * Boston, MA 02111-1307, USA.
sl@0
    18
 */
sl@0
    19
sl@0
    20
#undef	G_LOG_DOMAIN
sl@0
    21
#define	G_LOG_DOMAIN "TestAccumulator"
sl@0
    22
sl@0
    23
#undef G_DISABLE_ASSERT
sl@0
    24
#undef G_DISABLE_CHECKS
sl@0
    25
#undef G_DISABLE_CAST_CHECKS
sl@0
    26
sl@0
    27
#include <string.h>
sl@0
    28
sl@0
    29
#include	<glib-object.h>
sl@0
    30
sl@0
    31
#include "testmarshal.h"
sl@0
    32
#include "testcommon.h"
sl@0
    33
sl@0
    34
#ifdef __SYMBIAN32__
sl@0
    35
#include "mrt2_glib2_test.h"
sl@0
    36
#endif //__SYMBIAN32__
sl@0
    37
/* What this test tests is the behavior of signal accumulators
sl@0
    38
 * Two accumulators are tested:
sl@0
    39
 *
sl@0
    40
 * 1: A custom accumulator that appends the returned strings
sl@0
    41
 * 2: The standard g_signal_accumulator_true_handled that stops
sl@0
    42
 *    emission on TRUE returns.
sl@0
    43
 */
sl@0
    44
sl@0
    45
/*
sl@0
    46
 * TestObject, a parent class for TestObject
sl@0
    47
 */
sl@0
    48
#define TEST_TYPE_OBJECT          (test_object_get_type ())
sl@0
    49
typedef struct _TestObject        TestObject;
sl@0
    50
typedef struct _TestObjectClass   TestObjectClass;
sl@0
    51
sl@0
    52
struct _TestObject
sl@0
    53
{
sl@0
    54
  GObject parent_instance;
sl@0
    55
};
sl@0
    56
struct _TestObjectClass
sl@0
    57
{
sl@0
    58
  GObjectClass parent_class;
sl@0
    59
sl@0
    60
  gchar*   (*test_signal1) (TestObject *tobject,
sl@0
    61
			    gint        param);
sl@0
    62
  gboolean (*test_signal2) (TestObject *tobject,
sl@0
    63
			    gint        param);
sl@0
    64
};
sl@0
    65
sl@0
    66
static GType test_object_get_type (void);
sl@0
    67
sl@0
    68
static gboolean
sl@0
    69
test_signal1_accumulator (GSignalInvocationHint *ihint,
sl@0
    70
			  GValue                *return_accu,
sl@0
    71
			  const GValue          *handler_return,
sl@0
    72
			  gpointer               data)
sl@0
    73
{
sl@0
    74
  const gchar *accu_string = g_value_get_string (return_accu);
sl@0
    75
  const gchar *new_string = g_value_get_string (handler_return);
sl@0
    76
  gchar *result_string;
sl@0
    77
sl@0
    78
  if (accu_string)
sl@0
    79
    result_string = g_strconcat (accu_string, new_string, NULL);
sl@0
    80
  else if (new_string)
sl@0
    81
    result_string = g_strdup (new_string);
sl@0
    82
  else
sl@0
    83
    result_string = NULL;
sl@0
    84
sl@0
    85
  g_value_set_string_take_ownership (return_accu, result_string);
sl@0
    86
sl@0
    87
  return TRUE;
sl@0
    88
}
sl@0
    89
sl@0
    90
gchar*
sl@0
    91
test_object_signal1_callback_before (TestObject *tobject,
sl@0
    92
				     gint        param,
sl@0
    93
				     gpointer    data)
sl@0
    94
{
sl@0
    95
  return g_strdup ("<before>");
sl@0
    96
}
sl@0
    97
sl@0
    98
gchar*
sl@0
    99
test_object_real_signal1 (TestObject *tobject,
sl@0
   100
			  gint        param)
sl@0
   101
{
sl@0
   102
  return g_strdup ("<default>");
sl@0
   103
}
sl@0
   104
sl@0
   105
gchar*
sl@0
   106
test_object_signal1_callback_after (TestObject *tobject,
sl@0
   107
				    gint        param,
sl@0
   108
				    gpointer    data)
sl@0
   109
{
sl@0
   110
  return g_strdup ("<after>");
sl@0
   111
}
sl@0
   112
sl@0
   113
gboolean
sl@0
   114
test_object_signal2_callback_before (TestObject *tobject,
sl@0
   115
				     gint        param)
sl@0
   116
{
sl@0
   117
  switch (param)
sl@0
   118
    {
sl@0
   119
    case 1: return TRUE;
sl@0
   120
    case 2: return FALSE;
sl@0
   121
    case 3: return FALSE;
sl@0
   122
    case 4: return FALSE;
sl@0
   123
    }
sl@0
   124
sl@0
   125
  g_assert_not_reached ();
sl@0
   126
  return FALSE;
sl@0
   127
}
sl@0
   128
sl@0
   129
gboolean
sl@0
   130
test_object_real_signal2 (TestObject *tobject,
sl@0
   131
			  gint        param)
sl@0
   132
{
sl@0
   133
  switch (param)
sl@0
   134
    {
sl@0
   135
    case 1: g_assert_not_reached (); return FALSE;
sl@0
   136
    case 2: return TRUE;
sl@0
   137
    case 3: return FALSE;
sl@0
   138
    case 4: return FALSE;
sl@0
   139
    }
sl@0
   140
  
sl@0
   141
  g_assert_not_reached ();
sl@0
   142
  return FALSE;
sl@0
   143
}
sl@0
   144
sl@0
   145
gboolean
sl@0
   146
test_object_signal2_callback_after (TestObject *tobject,
sl@0
   147
				     gint        param)
sl@0
   148
{
sl@0
   149
  switch (param)
sl@0
   150
    {
sl@0
   151
    case 1: g_assert_not_reached (); return FALSE;
sl@0
   152
    case 2: g_assert_not_reached (); return FALSE;
sl@0
   153
    case 3: return TRUE;
sl@0
   154
    case 4: return FALSE;
sl@0
   155
    }
sl@0
   156
      
sl@0
   157
  g_assert_not_reached ();
sl@0
   158
  return FALSE;
sl@0
   159
}
sl@0
   160
sl@0
   161
static void
sl@0
   162
test_object_class_init (TestObjectClass *class)
sl@0
   163
{
sl@0
   164
  class->test_signal1 = test_object_real_signal1;
sl@0
   165
  class->test_signal2 = test_object_real_signal2;
sl@0
   166
  
sl@0
   167
  g_signal_new ("test-signal1",
sl@0
   168
		G_OBJECT_CLASS_TYPE (class),
sl@0
   169
		G_SIGNAL_RUN_LAST,
sl@0
   170
		G_STRUCT_OFFSET (TestObjectClass, test_signal1),
sl@0
   171
		test_signal1_accumulator, NULL,
sl@0
   172
		test_marshal_STRING__INT,
sl@0
   173
		G_TYPE_STRING, 1, G_TYPE_INT);
sl@0
   174
  g_signal_new ("test-signal2",
sl@0
   175
		G_OBJECT_CLASS_TYPE (class),
sl@0
   176
		G_SIGNAL_RUN_LAST,
sl@0
   177
		G_STRUCT_OFFSET (TestObjectClass, test_signal2),
sl@0
   178
		g_signal_accumulator_true_handled, NULL,
sl@0
   179
		test_marshal_BOOLEAN__INT,
sl@0
   180
		G_TYPE_BOOLEAN, 1, G_TYPE_INT);
sl@0
   181
}
sl@0
   182
sl@0
   183
static DEFINE_TYPE(TestObject, test_object,
sl@0
   184
		   test_object_class_init, NULL, NULL,
sl@0
   185
		   G_TYPE_OBJECT)
sl@0
   186
sl@0
   187
int
sl@0
   188
main (int   argc,
sl@0
   189
      char *argv[])
sl@0
   190
{
sl@0
   191
  TestObject *object;
sl@0
   192
  gchar *string_result;
sl@0
   193
  gboolean bool_result;
sl@0
   194
	#ifdef __SYMBIAN32__
sl@0
   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);
sl@0
   196
  g_set_print_handler(mrtPrintHandler);
sl@0
   197
  #endif /*__SYMBIAN32__*/
sl@0
   198
  g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
sl@0
   199
			  G_LOG_LEVEL_WARNING |
sl@0
   200
			  G_LOG_LEVEL_CRITICAL);
sl@0
   201
  g_type_init ();
sl@0
   202
sl@0
   203
  object = g_object_new (TEST_TYPE_OBJECT, NULL);
sl@0
   204
sl@0
   205
  g_signal_connect (object, "test-signal1",
sl@0
   206
		    G_CALLBACK (test_object_signal1_callback_before), NULL);
sl@0
   207
  g_signal_connect_after (object, "test-signal1",
sl@0
   208
			  G_CALLBACK (test_object_signal1_callback_after), NULL);
sl@0
   209
  
sl@0
   210
  g_signal_emit_by_name (object, "test-signal1", 0, &string_result);
sl@0
   211
  g_assert (strcmp (string_result, "<before><default><after>") == 0);
sl@0
   212
  g_free (string_result);
sl@0
   213
sl@0
   214
  g_signal_connect (object, "test-signal2",
sl@0
   215
		    G_CALLBACK (test_object_signal2_callback_before), NULL);
sl@0
   216
  g_signal_connect_after (object, "test-signal2",
sl@0
   217
			  G_CALLBACK (test_object_signal2_callback_after), NULL);
sl@0
   218
  
sl@0
   219
  bool_result = FALSE;
sl@0
   220
  g_signal_emit_by_name (object, "test-signal2", 1, &bool_result);
sl@0
   221
  g_assert (bool_result == TRUE);
sl@0
   222
  bool_result = FALSE;
sl@0
   223
  g_signal_emit_by_name (object, "test-signal2", 2, &bool_result);
sl@0
   224
  g_assert (bool_result == TRUE);
sl@0
   225
  bool_result = FALSE;
sl@0
   226
  g_signal_emit_by_name (object, "test-signal2", 3, &bool_result);
sl@0
   227
  g_assert (bool_result == TRUE);
sl@0
   228
  bool_result = TRUE;
sl@0
   229
  g_signal_emit_by_name (object, "test-signal2", 4, &bool_result);
sl@0
   230
  g_assert (bool_result == FALSE);
sl@0
   231
#ifdef __SYMBIAN32__
sl@0
   232
testResultXml("accumulator");
sl@0
   233
#endif  //__SYMBIAN32__
sl@0
   234
sl@0
   235
  return 0;
sl@0
   236
}