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