glib/tsrc/BC/tests/gobject/defaultiface.c
changeset 0 e4d67989cc36
child 18 47c74d1534e1
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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 "TestDefaultIface"
       
    22 
       
    23 #undef G_DISABLE_ASSERT
       
    24 #undef G_DISABLE_CHECKS
       
    25 #undef G_DISABLE_CAST_CHECKS
       
    26 
       
    27 #include <glib-object.h>
       
    28 
       
    29 #include "testcommon.h"
       
    30 #include "testmodule.h"
       
    31 #include "mambaz.h"
       
    32 
       
    33 #ifdef SYMBIAN
       
    34 #include "mrt2_glib2_test.h"
       
    35 #endif //SYMBIAN
       
    36 
       
    37 static GType test_dynamic_iface_type;
       
    38 static GType registered_inteface;
       
    39 static GType registered_enum;
       
    40 static GType registered_flag;
       
    41 
       
    42 static gboolean dynamic_iface_init = FALSE;
       
    43 GType baz_type;
       
    44 
       
    45 
       
    46 /* This test tests getting the default vtable for an interface
       
    47  * and the initialization and finalization of such default
       
    48  * interfaces.
       
    49  *
       
    50  * We test this both for static and for dynamic interfaces.
       
    51  */
       
    52 
       
    53 /**********************************************************************
       
    54  * Static interface tests
       
    55  **********************************************************************/
       
    56 
       
    57 typedef struct _TestStaticIfaceClass TestStaticIfaceClass;
       
    58 
       
    59 struct _TestStaticIfaceClass
       
    60 {
       
    61   GTypeInterface base_iface;
       
    62   guint val;
       
    63 };
       
    64 
       
    65 #define TEST_TYPE_STATIC_IFACE (test_static_iface_get_type ())
       
    66 
       
    67 static void
       
    68 test_static_iface_default_init (TestStaticIfaceClass *iface)
       
    69 {
       
    70   iface->val = 42;
       
    71 }
       
    72 
       
    73 DEFINE_IFACE (TestStaticIface, test_static_iface,
       
    74 	      NULL, test_static_iface_default_init)
       
    75 
       
    76 static void
       
    77 test_static_iface (void)
       
    78 {
       
    79   TestStaticIfaceClass *static_iface;
       
    80 
       
    81   /* Not loaded until we call ref for the first time */
       
    82   static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
       
    83   g_assert (static_iface == NULL);
       
    84 
       
    85   /* Ref loads */
       
    86   static_iface = g_type_default_interface_ref (TEST_TYPE_STATIC_IFACE);
       
    87   g_assert (static_iface && static_iface->val == 42);
       
    88 
       
    89   /* Peek then works */
       
    90   static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
       
    91   g_assert (static_iface && static_iface->val == 42);
       
    92   
       
    93   /* Unref does nothing */
       
    94   g_type_default_interface_unref (static_iface);
       
    95   
       
    96   /* And peek still works */
       
    97   static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
       
    98   g_assert (static_iface && static_iface->val == 42);
       
    99 }
       
   100 
       
   101 /**********************************************************************
       
   102  * Dynamic interface tests
       
   103  **********************************************************************/
       
   104 
       
   105 typedef struct _TestDynamicIfaceClass TestDynamicIfaceClass;
       
   106 
       
   107 struct _TestDynamicIfaceClass
       
   108 {
       
   109   GTypeInterface base_iface;
       
   110   guint val;
       
   111 };
       
   112 
       
   113 
       
   114 #define TEST_TYPE_DYNAMIC_IFACE (test_dynamic_iface_type)
       
   115 
       
   116 static void
       
   117 test_dynamic_iface_default_init (TestStaticIfaceClass *iface)
       
   118 {
       
   119   dynamic_iface_init = TRUE;
       
   120   iface->val = 42;
       
   121 }
       
   122 
       
   123 static void
       
   124 test_dynamic_iface_default_finalize (TestStaticIfaceClass *iface)
       
   125 {
       
   126   dynamic_iface_init = FALSE;
       
   127 }
       
   128 
       
   129 static void
       
   130 test_dynamic_iface_register (GTypeModule *module)
       
   131 {
       
   132   static const GTypeInfo iface_info =			
       
   133     {								
       
   134       sizeof (TestDynamicIfaceClass),
       
   135       (GBaseInitFunc)	   NULL,
       
   136       (GBaseFinalizeFunc)  NULL,				
       
   137       (GClassInitFunc)     test_dynamic_iface_default_init,
       
   138       (GClassFinalizeFunc) test_dynamic_iface_default_finalize
       
   139     };							
       
   140 
       
   141   test_dynamic_iface_type = g_type_module_register_type (module, G_TYPE_INTERFACE,
       
   142 							 "TestDynamicIface", &iface_info, 0);
       
   143 }
       
   144 
       
   145 static void test_module_add_interface(GTypeModule* module)
       
   146 {
       
   147     static const GTypeInfo info = {
       
   148       sizeof (MamanBazClass),
       
   149       NULL,   /* base_init */
       
   150       NULL,   /* base_finalize */
       
   151       NULL,   /* class_init */
       
   152       NULL,   /* class_finalize */
       
   153       NULL,   /* class_data */
       
   154       sizeof (MamanBaz),
       
   155       0,      /* n_preallocs */
       
   156       baz_instance_init    /* instance_init */
       
   157     };
       
   158     static const GInterfaceInfo ibaz_info = {
       
   159       (GInterfaceInitFunc) baz_interface_init,    /* interface_init */
       
   160       NULL,               /* interface_finalize */
       
   161       NULL                /* interface_data */
       
   162     };
       
   163     baz_type = g_type_module_register_type (module, G_TYPE_OBJECT,
       
   164                                    "MamanBazType",
       
   165                                    &info, 0);
       
   166 
       
   167 	g_type_module_add_interface     (module,baz_type,MAMAN_TYPE_IBAZ, &ibaz_info);
       
   168   
       
   169 }
       
   170 
       
   171 
       
   172 static void test_g_type_module_register_flags(GTypeModule* module)
       
   173 {
       
   174 	static GFlagsValue flags_array[] =
       
   175 	{
       
   176 		{ 1, "EOne", "One"},
       
   177 		{ 2, "ETwo", "Two"},
       
   178 		{ 4, "EFour", "Four"},
       
   179 		{ 0, NULL, NULL},
       
   180 	};
       
   181 
       
   182 	registered_flag =  g_type_module_register_flags(module, "egFlag",flags_array);
       
   183 }
       
   184 
       
   185 
       
   186 static void test_g_type_module_register_enum(GTypeModule* module)
       
   187 {
       
   188 	static GEnumValue enum_array[] =
       
   189 	{
       
   190 		{ 1, "EOne", "One"},
       
   191 		{ 2, "ETwo", "Two"},
       
   192 		{ 4, "EFour", "Four"},
       
   193 		{ 0, NULL, NULL},
       
   194 	};
       
   195 
       
   196 	registered_enum =  g_type_module_register_enum(module, "egEnum", enum_array);
       
   197 		
       
   198 }
       
   199 
       
   200 
       
   201 static void
       
   202 module_register (GTypeModule *module)
       
   203 {
       
   204   test_dynamic_iface_register (module);
       
   205   test_module_add_interface(module);
       
   206   test_g_type_module_register_flags(module);
       
   207   test_g_type_module_register_enum(module);
       
   208 }
       
   209 
       
   210 static void
       
   211 test_dynamic_iface (void)
       
   212 {
       
   213   GTypeModule *module;
       
   214   TestDynamicIfaceClass *dynamic_iface;
       
   215 
       
   216   module = test_module_new (module_register);
       
   217 
       
   218   /* Not loaded until we call ref for the first time */
       
   219   dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
       
   220   g_assert (dynamic_iface == NULL);
       
   221 
       
   222   /* Ref loads */
       
   223   dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE);
       
   224   g_assert (dynamic_iface_init);
       
   225   g_assert (dynamic_iface && dynamic_iface->val == 42);
       
   226 
       
   227   /* Peek then works */
       
   228   dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
       
   229   g_assert (dynamic_iface && dynamic_iface->val == 42);
       
   230   
       
   231   /* Unref causes finalize */
       
   232   g_type_default_interface_unref (dynamic_iface);
       
   233   g_assert (!dynamic_iface_init);
       
   234 
       
   235   /* Peek returns NULL */
       
   236   dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
       
   237   g_assert (dynamic_iface == NULL);
       
   238   
       
   239   /* Ref reloads */
       
   240   dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE);
       
   241   g_assert (dynamic_iface_init);
       
   242   g_assert (dynamic_iface && dynamic_iface->val == 42);
       
   243 
       
   244   /* And Unref causes finalize once more*/
       
   245   g_type_default_interface_unref (dynamic_iface);
       
   246   g_assert (!dynamic_iface_init);
       
   247 }
       
   248 
       
   249 
       
   250 void test_flags()
       
   251 {
       
   252 	GFlagsValue* retrievedValue;
       
   253 	GFlagsClass* pPointer = g_type_class_ref(registered_flag);
       
   254 	if(pPointer)
       
   255 	{
       
   256 		retrievedValue = g_flags_get_value_by_name(pPointer,"EOne");
       
   257 		g_assert(retrievedValue && retrievedValue->value == 1);
       
   258 		retrievedValue = g_flags_get_value_by_name(pPointer,"EFive");
       
   259 		g_assert(retrievedValue == NULL);
       
   260 		g_type_class_unref(pPointer);
       
   261 	}
       
   262 }
       
   263 
       
   264 void test_enum()
       
   265 {
       
   266 	GEnumValue* retrievedValue;
       
   267 	GEnumClass* pPointer = g_type_class_ref(registered_enum);
       
   268 	if(pPointer)
       
   269 	{
       
   270 		retrievedValue = g_enum_get_value_by_name(pPointer,"EOne");
       
   271 		g_assert(retrievedValue && retrievedValue->value == 1);
       
   272 		retrievedValue = g_enum_get_value_by_name(pPointer,"EFive");
       
   273 		g_assert(retrievedValue == NULL);
       
   274 		g_type_class_unref(pPointer);
       
   275 	}
       
   276 }
       
   277 
       
   278 void test_interface()
       
   279 {
       
   280 	MamanBaz* pPointer =     g_object_new(baz_type,NULL);
       
   281 	if(pPointer)
       
   282 	{
       
   283 		g_assert(0xdeadbeaf == pPointer->instance_member);
       
   284 		maman_ibaz_do_action (MAMAN_IBAZ(pPointer));
       
   285 		g_assert(10 == pPointer->instance_member);
       
   286 		g_object_unref(pPointer);
       
   287 	}
       
   288 	
       
   289 }
       
   290 
       
   291 
       
   292 gboolean    my_cache_func(gpointer cache_data, GTypeClass *g_class)
       
   293 {
       
   294 	if(MAMAN_IS_BAZ_CLASS(g_class))
       
   295 	{
       
   296 		g_assert(strcmp("hello",(char*) cache_data) == 0);
       
   297 		g_type_class_ref(baz_type); 
       
   298 
       
   299 		g_type_class_unref_uncached (g_class);
       
   300 		return TRUE;
       
   301 	}
       
   302 	else
       
   303 	{
       
   304 		return FALSE;
       
   305 	}
       
   306 }
       
   307 
       
   308 int
       
   309 main (int   argc,
       
   310       char *argv[])
       
   311 {
       
   312   char* data;	
       
   313   #ifdef SYMBIAN
       
   314   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);
       
   315   g_set_print_handler(mrtPrintHandler);
       
   316   #endif /*SYMBIAN*/
       
   317   
       
   318   g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
       
   319 			  G_LOG_LEVEL_WARNING |
       
   320 			  G_LOG_LEVEL_CRITICAL);
       
   321   g_type_init ();
       
   322 
       
   323   data = (char*) malloc(6);
       
   324   strcpy(data,"hello");
       
   325 
       
   326   g_type_add_class_cache_func(data, my_cache_func);
       
   327 
       
   328   test_static_iface ();
       
   329   test_dynamic_iface ();
       
   330   test_flags();
       
   331   test_enum();
       
   332   test_interface();
       
   333 
       
   334   #ifdef SYMBIAN
       
   335   testResultXml("defaultiface");
       
   336   #endif //SYMBIAN
       
   337   
       
   338   return 0;
       
   339 }