os/ossrv/glib/tsrc/BC/tests/gobject/defaultiface.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/defaultiface.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,340 @@
     1.4 +/* GObject - GLib Type, Object, Parameter and Signal Library
     1.5 + * Copyright (C) 2001, 2003 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 "TestDefaultIface"
    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 +#include "testcommon.h"
    1.33 +#include "testmodule.h"
    1.34 +#include "mambaz.h"
    1.35 +#include <stdlib.h>
    1.36 +
    1.37 +#ifdef SYMBIAN
    1.38 +#include "mrt2_glib2_test.h"
    1.39 +#endif //SYMBIAN
    1.40 +
    1.41 +static GType test_dynamic_iface_type;
    1.42 +static GType registered_inteface;
    1.43 +static GType registered_enum;
    1.44 +static GType registered_flag;
    1.45 +
    1.46 +static gboolean dynamic_iface_init = FALSE;
    1.47 +GType baz_type;
    1.48 +
    1.49 +
    1.50 +/* This test tests getting the default vtable for an interface
    1.51 + * and the initialization and finalization of such default
    1.52 + * interfaces.
    1.53 + *
    1.54 + * We test this both for static and for dynamic interfaces.
    1.55 + */
    1.56 +
    1.57 +/**********************************************************************
    1.58 + * Static interface tests
    1.59 + **********************************************************************/
    1.60 +
    1.61 +typedef struct _TestStaticIfaceClass TestStaticIfaceClass;
    1.62 +
    1.63 +struct _TestStaticIfaceClass
    1.64 +{
    1.65 +  GTypeInterface base_iface;
    1.66 +  guint val;
    1.67 +};
    1.68 +
    1.69 +#define TEST_TYPE_STATIC_IFACE (test_static_iface_get_type ())
    1.70 +
    1.71 +static void
    1.72 +test_static_iface_default_init (TestStaticIfaceClass *iface)
    1.73 +{
    1.74 +  iface->val = 42;
    1.75 +}
    1.76 +
    1.77 +DEFINE_IFACE (TestStaticIface, test_static_iface,
    1.78 +	      NULL, test_static_iface_default_init)
    1.79 +
    1.80 +static void
    1.81 +test_static_iface (void)
    1.82 +{
    1.83 +  TestStaticIfaceClass *static_iface;
    1.84 +
    1.85 +  /* Not loaded until we call ref for the first time */
    1.86 +  static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
    1.87 +  g_assert (static_iface == NULL);
    1.88 +
    1.89 +  /* Ref loads */
    1.90 +  static_iface = g_type_default_interface_ref (TEST_TYPE_STATIC_IFACE);
    1.91 +  g_assert (static_iface && static_iface->val == 42);
    1.92 +
    1.93 +  /* Peek then works */
    1.94 +  static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
    1.95 +  g_assert (static_iface && static_iface->val == 42);
    1.96 +  
    1.97 +  /* Unref does nothing */
    1.98 +  g_type_default_interface_unref (static_iface);
    1.99 +  
   1.100 +  /* And peek still works */
   1.101 +  static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
   1.102 +  g_assert (static_iface && static_iface->val == 42);
   1.103 +}
   1.104 +
   1.105 +/**********************************************************************
   1.106 + * Dynamic interface tests
   1.107 + **********************************************************************/
   1.108 +
   1.109 +typedef struct _TestDynamicIfaceClass TestDynamicIfaceClass;
   1.110 +
   1.111 +struct _TestDynamicIfaceClass
   1.112 +{
   1.113 +  GTypeInterface base_iface;
   1.114 +  guint val;
   1.115 +};
   1.116 +
   1.117 +
   1.118 +#define TEST_TYPE_DYNAMIC_IFACE (test_dynamic_iface_type)
   1.119 +
   1.120 +static void
   1.121 +test_dynamic_iface_default_init (TestStaticIfaceClass *iface)
   1.122 +{
   1.123 +  dynamic_iface_init = TRUE;
   1.124 +  iface->val = 42;
   1.125 +}
   1.126 +
   1.127 +static void
   1.128 +test_dynamic_iface_default_finalize (TestStaticIfaceClass *iface)
   1.129 +{
   1.130 +  dynamic_iface_init = FALSE;
   1.131 +}
   1.132 +
   1.133 +static void
   1.134 +test_dynamic_iface_register (GTypeModule *module)
   1.135 +{
   1.136 +  static const GTypeInfo iface_info =			
   1.137 +    {								
   1.138 +      sizeof (TestDynamicIfaceClass),
   1.139 +      (GBaseInitFunc)	   NULL,
   1.140 +      (GBaseFinalizeFunc)  NULL,				
   1.141 +      (GClassInitFunc)     test_dynamic_iface_default_init,
   1.142 +      (GClassFinalizeFunc) test_dynamic_iface_default_finalize
   1.143 +    };							
   1.144 +
   1.145 +  test_dynamic_iface_type = g_type_module_register_type (module, G_TYPE_INTERFACE,
   1.146 +							 "TestDynamicIface", &iface_info, 0);
   1.147 +}
   1.148 +
   1.149 +static void test_module_add_interface(GTypeModule* module)
   1.150 +{
   1.151 +    static const GTypeInfo info = {
   1.152 +      sizeof (MamanBazClass),
   1.153 +      NULL,   /* base_init */
   1.154 +      NULL,   /* base_finalize */
   1.155 +      NULL,   /* class_init */
   1.156 +      NULL,   /* class_finalize */
   1.157 +      NULL,   /* class_data */
   1.158 +      sizeof (MamanBaz),
   1.159 +      0,      /* n_preallocs */
   1.160 +      baz_instance_init    /* instance_init */
   1.161 +    };
   1.162 +    static const GInterfaceInfo ibaz_info = {
   1.163 +      (GInterfaceInitFunc) baz_interface_init,    /* interface_init */
   1.164 +      NULL,               /* interface_finalize */
   1.165 +      NULL                /* interface_data */
   1.166 +    };
   1.167 +    baz_type = g_type_module_register_type (module, G_TYPE_OBJECT,
   1.168 +                                   "MamanBazType",
   1.169 +                                   &info, 0);
   1.170 +
   1.171 +	g_type_module_add_interface     (module,baz_type,MAMAN_TYPE_IBAZ, &ibaz_info);
   1.172 +  
   1.173 +}
   1.174 +
   1.175 +
   1.176 +static void test_g_type_module_register_flags(GTypeModule* module)
   1.177 +{
   1.178 +	static GFlagsValue flags_array[] =
   1.179 +	{
   1.180 +		{ 1, "EOne", "One"},
   1.181 +		{ 2, "ETwo", "Two"},
   1.182 +		{ 4, "EFour", "Four"},
   1.183 +		{ 0, NULL, NULL},
   1.184 +	};
   1.185 +
   1.186 +	registered_flag =  g_type_module_register_flags(module, "egFlag",flags_array);
   1.187 +}
   1.188 +
   1.189 +
   1.190 +static void test_g_type_module_register_enum(GTypeModule* module)
   1.191 +{
   1.192 +	static GEnumValue enum_array[] =
   1.193 +	{
   1.194 +		{ 1, "EOne", "One"},
   1.195 +		{ 2, "ETwo", "Two"},
   1.196 +		{ 4, "EFour", "Four"},
   1.197 +		{ 0, NULL, NULL},
   1.198 +	};
   1.199 +
   1.200 +	registered_enum =  g_type_module_register_enum(module, "egEnum", enum_array);
   1.201 +		
   1.202 +}
   1.203 +
   1.204 +
   1.205 +static void
   1.206 +module_register (GTypeModule *module)
   1.207 +{
   1.208 +  test_dynamic_iface_register (module);
   1.209 +  test_module_add_interface(module);
   1.210 +  test_g_type_module_register_flags(module);
   1.211 +  test_g_type_module_register_enum(module);
   1.212 +}
   1.213 +
   1.214 +static void
   1.215 +test_dynamic_iface (void)
   1.216 +{
   1.217 +  GTypeModule *module;
   1.218 +  TestDynamicIfaceClass *dynamic_iface;
   1.219 +
   1.220 +  module = test_module_new (module_register);
   1.221 +
   1.222 +  /* Not loaded until we call ref for the first time */
   1.223 +  dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
   1.224 +  g_assert (dynamic_iface == NULL);
   1.225 +
   1.226 +  /* Ref loads */
   1.227 +  dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE);
   1.228 +  g_assert (dynamic_iface_init);
   1.229 +  g_assert (dynamic_iface && dynamic_iface->val == 42);
   1.230 +
   1.231 +  /* Peek then works */
   1.232 +  dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
   1.233 +  g_assert (dynamic_iface && dynamic_iface->val == 42);
   1.234 +  
   1.235 +  /* Unref causes finalize */
   1.236 +  g_type_default_interface_unref (dynamic_iface);
   1.237 +  g_assert (!dynamic_iface_init);
   1.238 +
   1.239 +  /* Peek returns NULL */
   1.240 +  dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
   1.241 +  g_assert (dynamic_iface == NULL);
   1.242 +  
   1.243 +  /* Ref reloads */
   1.244 +  dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE);
   1.245 +  g_assert (dynamic_iface_init);
   1.246 +  g_assert (dynamic_iface && dynamic_iface->val == 42);
   1.247 +
   1.248 +  /* And Unref causes finalize once more*/
   1.249 +  g_type_default_interface_unref (dynamic_iface);
   1.250 +  g_assert (!dynamic_iface_init);
   1.251 +}
   1.252 +
   1.253 +
   1.254 +void test_flags()
   1.255 +{
   1.256 +	GFlagsValue* retrievedValue;
   1.257 +	GFlagsClass* pPointer = g_type_class_ref(registered_flag);
   1.258 +	if(pPointer)
   1.259 +	{
   1.260 +		retrievedValue = g_flags_get_value_by_name(pPointer,"EOne");
   1.261 +		g_assert(retrievedValue && retrievedValue->value == 1);
   1.262 +		retrievedValue = g_flags_get_value_by_name(pPointer,"EFive");
   1.263 +		g_assert(retrievedValue == NULL);
   1.264 +		g_type_class_unref(pPointer);
   1.265 +	}
   1.266 +}
   1.267 +
   1.268 +void test_enum()
   1.269 +{
   1.270 +	GEnumValue* retrievedValue;
   1.271 +	GEnumClass* pPointer = g_type_class_ref(registered_enum);
   1.272 +	if(pPointer)
   1.273 +	{
   1.274 +		retrievedValue = g_enum_get_value_by_name(pPointer,"EOne");
   1.275 +		g_assert(retrievedValue && retrievedValue->value == 1);
   1.276 +		retrievedValue = g_enum_get_value_by_name(pPointer,"EFive");
   1.277 +		g_assert(retrievedValue == NULL);
   1.278 +		g_type_class_unref(pPointer);
   1.279 +	}
   1.280 +}
   1.281 +
   1.282 +void test_interface()
   1.283 +{
   1.284 +	MamanBaz* pPointer =     g_object_new(baz_type,NULL);
   1.285 +	if(pPointer)
   1.286 +	{
   1.287 +		g_assert(0xdeadbeaf == pPointer->instance_member);
   1.288 +		maman_ibaz_do_action (MAMAN_IBAZ(pPointer));
   1.289 +		g_assert(10 == pPointer->instance_member);
   1.290 +		g_object_unref(pPointer);
   1.291 +	}
   1.292 +	
   1.293 +}
   1.294 +
   1.295 +
   1.296 +gboolean    my_cache_func(gpointer cache_data, GTypeClass *g_class)
   1.297 +{
   1.298 +	if(MAMAN_IS_BAZ_CLASS(g_class))
   1.299 +	{
   1.300 +		g_assert(strcmp("hello",(char*) cache_data) == 0);
   1.301 +		g_type_class_ref(baz_type); 
   1.302 +
   1.303 +		g_type_class_unref_uncached (g_class);
   1.304 +		return TRUE;
   1.305 +	}
   1.306 +	else
   1.307 +	{
   1.308 +		return FALSE;
   1.309 +	}
   1.310 +}
   1.311 +
   1.312 +int
   1.313 +main (int   argc,
   1.314 +      char *argv[])
   1.315 +{
   1.316 +  char* data;	
   1.317 +  #ifdef SYMBIAN
   1.318 +  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.319 +  g_set_print_handler(mrtPrintHandler);
   1.320 +  #endif /*SYMBIAN*/
   1.321 +  
   1.322 +  g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
   1.323 +			  G_LOG_LEVEL_WARNING |
   1.324 +			  G_LOG_LEVEL_CRITICAL);
   1.325 +  g_type_init ();
   1.326 +
   1.327 +  data = (char*) malloc(6);
   1.328 +  strcpy(data,"hello");
   1.329 +
   1.330 +  g_type_add_class_cache_func(data, my_cache_func);
   1.331 +
   1.332 +  test_static_iface ();
   1.333 +  test_dynamic_iface ();
   1.334 +  test_flags();
   1.335 +  test_enum();
   1.336 +  test_interface();
   1.337 +
   1.338 +  #ifdef SYMBIAN
   1.339 +  testResultXml("defaultiface");
   1.340 +  #endif //SYMBIAN
   1.341 +  
   1.342 +  return 0;
   1.343 +}