os/ossrv/glib/tests/gobject/references.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tests/gobject/references.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,290 @@
     1.4 +/* GObject - GLib Type, Object, Parameter and Signal Library
     1.5 + * Copyright (C) 2005 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 "TestReferences"
    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	<glib-object.h>
    1.31 +
    1.32 +#ifdef __SYMBIAN32__
    1.33 +  #include "mrt2_glib2_test.h"
    1.34 +  #endif /*__SYMBIAN32__*/
    1.35 +/* This test tests weak and toggle references
    1.36 + */
    1.37 +
    1.38 +static GObject *global_object;
    1.39 +
    1.40 +static gboolean object_destroyed;
    1.41 +static gboolean weak_ref1_notified;
    1.42 +static gboolean weak_ref2_notified;
    1.43 +static gboolean toggle_ref1_weakened;
    1.44 +static gboolean toggle_ref1_strengthened;
    1.45 +static gboolean toggle_ref2_weakened;
    1.46 +static gboolean toggle_ref2_strengthened;
    1.47 +static gboolean toggle_ref3_weakened;
    1.48 +static gboolean toggle_ref3_strengthened;
    1.49 +
    1.50 +/*
    1.51 + * TestObject, a parent class for TestObject
    1.52 + */
    1.53 +#define TEST_TYPE_OBJECT          (test_object_get_type ())
    1.54 +typedef struct _TestObject        TestObject;
    1.55 +typedef struct _TestObjectClass   TestObjectClass;
    1.56 +
    1.57 +struct _TestObject
    1.58 +{
    1.59 +  GObject parent_instance;
    1.60 +};
    1.61 +struct _TestObjectClass
    1.62 +{
    1.63 +  GObjectClass parent_class;
    1.64 +};
    1.65 +
    1.66 +G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT);
    1.67 +
    1.68 +static void
    1.69 +test_object_finalize (GObject *object)
    1.70 +{
    1.71 +  object_destroyed = TRUE;
    1.72 +  
    1.73 +  G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
    1.74 +}
    1.75 +
    1.76 +static void
    1.77 +test_object_class_init (TestObjectClass *class)
    1.78 +{
    1.79 +  GObjectClass *object_class = G_OBJECT_CLASS (class);
    1.80 +
    1.81 +  object_class->finalize = test_object_finalize;
    1.82 +}
    1.83 +
    1.84 +static void
    1.85 +test_object_init (TestObject *test_object)
    1.86 +{
    1.87 +}
    1.88 +
    1.89 +static void
    1.90 +clear_flags (void)
    1.91 +{
    1.92 +  object_destroyed = FALSE;
    1.93 +  weak_ref1_notified = FALSE;
    1.94 +  weak_ref2_notified = FALSE;
    1.95 +  toggle_ref1_weakened = FALSE;
    1.96 +  toggle_ref1_strengthened = FALSE;
    1.97 +  toggle_ref2_weakened = FALSE;
    1.98 +  toggle_ref2_strengthened = FALSE;
    1.99 +  toggle_ref3_weakened = FALSE;
   1.100 +  toggle_ref3_strengthened = FALSE;
   1.101 +}
   1.102 +
   1.103 +static void
   1.104 +weak_ref1 (gpointer data,
   1.105 +	   GObject *object)
   1.106 +{
   1.107 +  g_assert (object == global_object);
   1.108 +  g_assert (data == GUINT_TO_POINTER (42));
   1.109 +
   1.110 +  weak_ref1_notified = TRUE;
   1.111 +}
   1.112 +
   1.113 +static void
   1.114 +weak_ref2 (gpointer data,
   1.115 +	   GObject *object)
   1.116 +{
   1.117 +  g_assert (object == global_object);
   1.118 +  g_assert (data == GUINT_TO_POINTER (24));
   1.119 +
   1.120 +  weak_ref2_notified = TRUE;
   1.121 +}
   1.122 +
   1.123 +static void
   1.124 +toggle_ref1 (gpointer data,
   1.125 +	     GObject *object,
   1.126 +	     gboolean is_last_ref)
   1.127 +{
   1.128 +  g_assert (object == global_object);
   1.129 +  g_assert (data == GUINT_TO_POINTER (42));
   1.130 +
   1.131 +  if (is_last_ref)
   1.132 +    toggle_ref1_weakened = TRUE;
   1.133 +  else
   1.134 +    toggle_ref1_strengthened = TRUE;
   1.135 +}
   1.136 +
   1.137 +static void
   1.138 +toggle_ref2 (gpointer data,
   1.139 +	     GObject *object,
   1.140 +	     gboolean is_last_ref)
   1.141 +{
   1.142 +  g_assert (object == global_object);
   1.143 +  g_assert (data == GUINT_TO_POINTER (24));
   1.144 +
   1.145 +  if (is_last_ref)
   1.146 +    toggle_ref2_weakened = TRUE;
   1.147 +  else
   1.148 +    toggle_ref2_strengthened = TRUE;
   1.149 +}
   1.150 +
   1.151 +static void
   1.152 +toggle_ref3 (gpointer data,
   1.153 +	     GObject *object,
   1.154 +	     gboolean is_last_ref)
   1.155 +{
   1.156 +  g_assert (object == global_object);
   1.157 +  g_assert (data == GUINT_TO_POINTER (34));
   1.158 +
   1.159 +  if (is_last_ref)
   1.160 +    {
   1.161 +      toggle_ref3_weakened = TRUE;
   1.162 +      g_object_remove_toggle_ref (object, toggle_ref3, GUINT_TO_POINTER (34));
   1.163 +    }
   1.164 +  else
   1.165 +    toggle_ref3_strengthened = TRUE;
   1.166 +}
   1.167 +
   1.168 +int
   1.169 +main (int   argc,
   1.170 +      char *argv[])
   1.171 +{
   1.172 +  GObject *object;
   1.173 +	
   1.174 +  g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
   1.175 +			  G_LOG_LEVEL_WARNING |
   1.176 +			  G_LOG_LEVEL_CRITICAL);
   1.177 +  g_type_init ();
   1.178 +
   1.179 +  #ifdef __SYMBIAN32__
   1.180 +  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.181 +  #endif /*__SYMBIAN32__*/
   1.182 +  /* Test basic weak reference operation
   1.183 +   */
   1.184 +  global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
   1.185 +  
   1.186 +  g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
   1.187 +
   1.188 +  clear_flags ();
   1.189 +  g_object_unref (object);
   1.190 +  g_assert (weak_ref1_notified == TRUE);
   1.191 +  g_assert (object_destroyed == TRUE);
   1.192 +
   1.193 +  /* Test two weak references at once
   1.194 +   */
   1.195 +  global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
   1.196 +  
   1.197 +  g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
   1.198 +  g_object_weak_ref (object, weak_ref2, GUINT_TO_POINTER (24));
   1.199 +
   1.200 +  clear_flags ();
   1.201 +  g_object_unref (object);
   1.202 +  g_assert (weak_ref1_notified == TRUE);
   1.203 +  g_assert (weak_ref2_notified == TRUE);
   1.204 +  g_assert (object_destroyed == TRUE);
   1.205 +
   1.206 +  /* Test remove weak references
   1.207 +   */
   1.208 +  global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
   1.209 +  
   1.210 +  g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
   1.211 +  g_object_weak_ref (object, weak_ref2, GUINT_TO_POINTER (24));
   1.212 +  g_object_weak_unref (object, weak_ref1, GUINT_TO_POINTER (42));
   1.213 +
   1.214 +  clear_flags ();
   1.215 +  g_object_unref (object);
   1.216 +  g_assert (weak_ref1_notified == FALSE);
   1.217 +  g_assert (weak_ref2_notified == TRUE);
   1.218 +  g_assert (object_destroyed == TRUE);
   1.219 +
   1.220 +  /* Test basic toggle reference operation
   1.221 +   */
   1.222 +  global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
   1.223 +  
   1.224 +  g_object_add_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
   1.225 +
   1.226 +  clear_flags ();
   1.227 +  g_object_unref (object);
   1.228 +  g_assert (toggle_ref1_weakened == TRUE);
   1.229 +  g_assert (toggle_ref1_strengthened == FALSE);
   1.230 +  g_assert (object_destroyed == FALSE);
   1.231 +
   1.232 +  clear_flags ();
   1.233 +  g_object_ref (object);
   1.234 +  g_assert (toggle_ref1_weakened == FALSE);
   1.235 +  g_assert (toggle_ref1_strengthened == TRUE);
   1.236 +  g_assert (object_destroyed == FALSE);
   1.237 +
   1.238 +  g_object_unref (object);
   1.239 +
   1.240 +  clear_flags ();
   1.241 +  g_object_remove_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
   1.242 +  g_assert (toggle_ref1_weakened == FALSE);
   1.243 +  g_assert (toggle_ref1_strengthened == FALSE);
   1.244 +  g_assert (object_destroyed == TRUE);
   1.245 +
   1.246 +  global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
   1.247 +
   1.248 +  /* Test two toggle references at once
   1.249 +   */
   1.250 +  g_object_add_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
   1.251 +  g_object_add_toggle_ref (object, toggle_ref2, GUINT_TO_POINTER (24));
   1.252 +
   1.253 +  clear_flags ();
   1.254 +  g_object_unref (object);
   1.255 +  g_assert (toggle_ref1_weakened == FALSE);
   1.256 +  g_assert (toggle_ref1_strengthened == FALSE);
   1.257 +  g_assert (toggle_ref2_weakened == FALSE);
   1.258 +  g_assert (toggle_ref2_strengthened == FALSE);
   1.259 +  g_assert (object_destroyed == FALSE);
   1.260 +
   1.261 +  clear_flags ();
   1.262 +  g_object_remove_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
   1.263 +  g_assert (toggle_ref1_weakened == FALSE);
   1.264 +  g_assert (toggle_ref1_strengthened == FALSE);
   1.265 +  g_assert (toggle_ref2_weakened == TRUE);
   1.266 +  g_assert (toggle_ref2_strengthened == FALSE);
   1.267 +  g_assert (object_destroyed == FALSE);
   1.268 +
   1.269 +  clear_flags ();
   1.270 +  g_object_remove_toggle_ref (object, toggle_ref2, GUINT_TO_POINTER (24));
   1.271 +  g_assert (toggle_ref1_weakened == FALSE);
   1.272 +  g_assert (toggle_ref1_strengthened == FALSE);
   1.273 +  g_assert (toggle_ref2_weakened == FALSE);
   1.274 +  g_assert (toggle_ref2_strengthened == FALSE);
   1.275 +  g_assert (object_destroyed == TRUE);
   1.276 +  
   1.277 +  /* Test a toggle reference that removes itself
   1.278 +   */
   1.279 +  global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
   1.280 +  
   1.281 +  g_object_add_toggle_ref (object, toggle_ref3, GUINT_TO_POINTER (34));
   1.282 +
   1.283 +  clear_flags ();
   1.284 +  g_object_unref (object);
   1.285 +  g_assert (toggle_ref3_weakened == TRUE);
   1.286 +  g_assert (toggle_ref3_strengthened == FALSE);
   1.287 +  g_assert (object_destroyed == TRUE);
   1.288 +
   1.289 +  #ifdef __SYMBIAN32__
   1.290 +  testResultXml("references");
   1.291 +  #endif /*__SYMBIAN32__*/
   1.292 +  return 0;
   1.293 +}