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