os/ossrv/glib/tsrc/BC/tests/gobject/defaultiface.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* GObject - GLib Type, Object, Parameter and Signal Library
sl@0
     2
 * Copyright (C) 2001, 2003 Red Hat, Inc.
sl@0
     3
 * Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
sl@0
     4
 * This library is free software; you can redistribute it and/or
sl@0
     5
 * modify it under the terms of the GNU Lesser General Public
sl@0
     6
 * License as published by the Free Software Foundation; either
sl@0
     7
 * version 2 of the License, or (at your option) any later version.
sl@0
     8
 *
sl@0
     9
 * This library is distributed in the hope that it will be useful,
sl@0
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
sl@0
    12
 * Lesser General Public License for more details.
sl@0
    13
 *
sl@0
    14
 * You should have received a copy of the GNU Lesser General
sl@0
    15
 * Public License along with this library; if not, write to the
sl@0
    16
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
sl@0
    17
 * Boston, MA 02111-1307, USA.
sl@0
    18
 */
sl@0
    19
sl@0
    20
#undef	G_LOG_DOMAIN
sl@0
    21
#define	G_LOG_DOMAIN "TestDefaultIface"
sl@0
    22
sl@0
    23
#undef G_DISABLE_ASSERT
sl@0
    24
#undef G_DISABLE_CHECKS
sl@0
    25
#undef G_DISABLE_CAST_CHECKS
sl@0
    26
sl@0
    27
#include <glib-object.h>
sl@0
    28
sl@0
    29
#include "testcommon.h"
sl@0
    30
#include "testmodule.h"
sl@0
    31
#include "mambaz.h"
sl@0
    32
#include <stdlib.h>
sl@0
    33
sl@0
    34
#ifdef SYMBIAN
sl@0
    35
#include "mrt2_glib2_test.h"
sl@0
    36
#endif //SYMBIAN
sl@0
    37
sl@0
    38
static GType test_dynamic_iface_type;
sl@0
    39
static GType registered_inteface;
sl@0
    40
static GType registered_enum;
sl@0
    41
static GType registered_flag;
sl@0
    42
sl@0
    43
static gboolean dynamic_iface_init = FALSE;
sl@0
    44
GType baz_type;
sl@0
    45
sl@0
    46
sl@0
    47
/* This test tests getting the default vtable for an interface
sl@0
    48
 * and the initialization and finalization of such default
sl@0
    49
 * interfaces.
sl@0
    50
 *
sl@0
    51
 * We test this both for static and for dynamic interfaces.
sl@0
    52
 */
sl@0
    53
sl@0
    54
/**********************************************************************
sl@0
    55
 * Static interface tests
sl@0
    56
 **********************************************************************/
sl@0
    57
sl@0
    58
typedef struct _TestStaticIfaceClass TestStaticIfaceClass;
sl@0
    59
sl@0
    60
struct _TestStaticIfaceClass
sl@0
    61
{
sl@0
    62
  GTypeInterface base_iface;
sl@0
    63
  guint val;
sl@0
    64
};
sl@0
    65
sl@0
    66
#define TEST_TYPE_STATIC_IFACE (test_static_iface_get_type ())
sl@0
    67
sl@0
    68
static void
sl@0
    69
test_static_iface_default_init (TestStaticIfaceClass *iface)
sl@0
    70
{
sl@0
    71
  iface->val = 42;
sl@0
    72
}
sl@0
    73
sl@0
    74
DEFINE_IFACE (TestStaticIface, test_static_iface,
sl@0
    75
	      NULL, test_static_iface_default_init)
sl@0
    76
sl@0
    77
static void
sl@0
    78
test_static_iface (void)
sl@0
    79
{
sl@0
    80
  TestStaticIfaceClass *static_iface;
sl@0
    81
sl@0
    82
  /* Not loaded until we call ref for the first time */
sl@0
    83
  static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
sl@0
    84
  g_assert (static_iface == NULL);
sl@0
    85
sl@0
    86
  /* Ref loads */
sl@0
    87
  static_iface = g_type_default_interface_ref (TEST_TYPE_STATIC_IFACE);
sl@0
    88
  g_assert (static_iface && static_iface->val == 42);
sl@0
    89
sl@0
    90
  /* Peek then works */
sl@0
    91
  static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
sl@0
    92
  g_assert (static_iface && static_iface->val == 42);
sl@0
    93
  
sl@0
    94
  /* Unref does nothing */
sl@0
    95
  g_type_default_interface_unref (static_iface);
sl@0
    96
  
sl@0
    97
  /* And peek still works */
sl@0
    98
  static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
sl@0
    99
  g_assert (static_iface && static_iface->val == 42);
sl@0
   100
}
sl@0
   101
sl@0
   102
/**********************************************************************
sl@0
   103
 * Dynamic interface tests
sl@0
   104
 **********************************************************************/
sl@0
   105
sl@0
   106
typedef struct _TestDynamicIfaceClass TestDynamicIfaceClass;
sl@0
   107
sl@0
   108
struct _TestDynamicIfaceClass
sl@0
   109
{
sl@0
   110
  GTypeInterface base_iface;
sl@0
   111
  guint val;
sl@0
   112
};
sl@0
   113
sl@0
   114
sl@0
   115
#define TEST_TYPE_DYNAMIC_IFACE (test_dynamic_iface_type)
sl@0
   116
sl@0
   117
static void
sl@0
   118
test_dynamic_iface_default_init (TestStaticIfaceClass *iface)
sl@0
   119
{
sl@0
   120
  dynamic_iface_init = TRUE;
sl@0
   121
  iface->val = 42;
sl@0
   122
}
sl@0
   123
sl@0
   124
static void
sl@0
   125
test_dynamic_iface_default_finalize (TestStaticIfaceClass *iface)
sl@0
   126
{
sl@0
   127
  dynamic_iface_init = FALSE;
sl@0
   128
}
sl@0
   129
sl@0
   130
static void
sl@0
   131
test_dynamic_iface_register (GTypeModule *module)
sl@0
   132
{
sl@0
   133
  static const GTypeInfo iface_info =			
sl@0
   134
    {								
sl@0
   135
      sizeof (TestDynamicIfaceClass),
sl@0
   136
      (GBaseInitFunc)	   NULL,
sl@0
   137
      (GBaseFinalizeFunc)  NULL,				
sl@0
   138
      (GClassInitFunc)     test_dynamic_iface_default_init,
sl@0
   139
      (GClassFinalizeFunc) test_dynamic_iface_default_finalize
sl@0
   140
    };							
sl@0
   141
sl@0
   142
  test_dynamic_iface_type = g_type_module_register_type (module, G_TYPE_INTERFACE,
sl@0
   143
							 "TestDynamicIface", &iface_info, 0);
sl@0
   144
}
sl@0
   145
sl@0
   146
static void test_module_add_interface(GTypeModule* module)
sl@0
   147
{
sl@0
   148
    static const GTypeInfo info = {
sl@0
   149
      sizeof (MamanBazClass),
sl@0
   150
      NULL,   /* base_init */
sl@0
   151
      NULL,   /* base_finalize */
sl@0
   152
      NULL,   /* class_init */
sl@0
   153
      NULL,   /* class_finalize */
sl@0
   154
      NULL,   /* class_data */
sl@0
   155
      sizeof (MamanBaz),
sl@0
   156
      0,      /* n_preallocs */
sl@0
   157
      baz_instance_init    /* instance_init */
sl@0
   158
    };
sl@0
   159
    static const GInterfaceInfo ibaz_info = {
sl@0
   160
      (GInterfaceInitFunc) baz_interface_init,    /* interface_init */
sl@0
   161
      NULL,               /* interface_finalize */
sl@0
   162
      NULL                /* interface_data */
sl@0
   163
    };
sl@0
   164
    baz_type = g_type_module_register_type (module, G_TYPE_OBJECT,
sl@0
   165
                                   "MamanBazType",
sl@0
   166
                                   &info, 0);
sl@0
   167
sl@0
   168
	g_type_module_add_interface     (module,baz_type,MAMAN_TYPE_IBAZ, &ibaz_info);
sl@0
   169
  
sl@0
   170
}
sl@0
   171
sl@0
   172
sl@0
   173
static void test_g_type_module_register_flags(GTypeModule* module)
sl@0
   174
{
sl@0
   175
	static GFlagsValue flags_array[] =
sl@0
   176
	{
sl@0
   177
		{ 1, "EOne", "One"},
sl@0
   178
		{ 2, "ETwo", "Two"},
sl@0
   179
		{ 4, "EFour", "Four"},
sl@0
   180
		{ 0, NULL, NULL},
sl@0
   181
	};
sl@0
   182
sl@0
   183
	registered_flag =  g_type_module_register_flags(module, "egFlag",flags_array);
sl@0
   184
}
sl@0
   185
sl@0
   186
sl@0
   187
static void test_g_type_module_register_enum(GTypeModule* module)
sl@0
   188
{
sl@0
   189
	static GEnumValue enum_array[] =
sl@0
   190
	{
sl@0
   191
		{ 1, "EOne", "One"},
sl@0
   192
		{ 2, "ETwo", "Two"},
sl@0
   193
		{ 4, "EFour", "Four"},
sl@0
   194
		{ 0, NULL, NULL},
sl@0
   195
	};
sl@0
   196
sl@0
   197
	registered_enum =  g_type_module_register_enum(module, "egEnum", enum_array);
sl@0
   198
		
sl@0
   199
}
sl@0
   200
sl@0
   201
sl@0
   202
static void
sl@0
   203
module_register (GTypeModule *module)
sl@0
   204
{
sl@0
   205
  test_dynamic_iface_register (module);
sl@0
   206
  test_module_add_interface(module);
sl@0
   207
  test_g_type_module_register_flags(module);
sl@0
   208
  test_g_type_module_register_enum(module);
sl@0
   209
}
sl@0
   210
sl@0
   211
static void
sl@0
   212
test_dynamic_iface (void)
sl@0
   213
{
sl@0
   214
  GTypeModule *module;
sl@0
   215
  TestDynamicIfaceClass *dynamic_iface;
sl@0
   216
sl@0
   217
  module = test_module_new (module_register);
sl@0
   218
sl@0
   219
  /* Not loaded until we call ref for the first time */
sl@0
   220
  dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
sl@0
   221
  g_assert (dynamic_iface == NULL);
sl@0
   222
sl@0
   223
  /* Ref loads */
sl@0
   224
  dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE);
sl@0
   225
  g_assert (dynamic_iface_init);
sl@0
   226
  g_assert (dynamic_iface && dynamic_iface->val == 42);
sl@0
   227
sl@0
   228
  /* Peek then works */
sl@0
   229
  dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
sl@0
   230
  g_assert (dynamic_iface && dynamic_iface->val == 42);
sl@0
   231
  
sl@0
   232
  /* Unref causes finalize */
sl@0
   233
  g_type_default_interface_unref (dynamic_iface);
sl@0
   234
  g_assert (!dynamic_iface_init);
sl@0
   235
sl@0
   236
  /* Peek returns NULL */
sl@0
   237
  dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
sl@0
   238
  g_assert (dynamic_iface == NULL);
sl@0
   239
  
sl@0
   240
  /* Ref reloads */
sl@0
   241
  dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE);
sl@0
   242
  g_assert (dynamic_iface_init);
sl@0
   243
  g_assert (dynamic_iface && dynamic_iface->val == 42);
sl@0
   244
sl@0
   245
  /* And Unref causes finalize once more*/
sl@0
   246
  g_type_default_interface_unref (dynamic_iface);
sl@0
   247
  g_assert (!dynamic_iface_init);
sl@0
   248
}
sl@0
   249
sl@0
   250
sl@0
   251
void test_flags()
sl@0
   252
{
sl@0
   253
	GFlagsValue* retrievedValue;
sl@0
   254
	GFlagsClass* pPointer = g_type_class_ref(registered_flag);
sl@0
   255
	if(pPointer)
sl@0
   256
	{
sl@0
   257
		retrievedValue = g_flags_get_value_by_name(pPointer,"EOne");
sl@0
   258
		g_assert(retrievedValue && retrievedValue->value == 1);
sl@0
   259
		retrievedValue = g_flags_get_value_by_name(pPointer,"EFive");
sl@0
   260
		g_assert(retrievedValue == NULL);
sl@0
   261
		g_type_class_unref(pPointer);
sl@0
   262
	}
sl@0
   263
}
sl@0
   264
sl@0
   265
void test_enum()
sl@0
   266
{
sl@0
   267
	GEnumValue* retrievedValue;
sl@0
   268
	GEnumClass* pPointer = g_type_class_ref(registered_enum);
sl@0
   269
	if(pPointer)
sl@0
   270
	{
sl@0
   271
		retrievedValue = g_enum_get_value_by_name(pPointer,"EOne");
sl@0
   272
		g_assert(retrievedValue && retrievedValue->value == 1);
sl@0
   273
		retrievedValue = g_enum_get_value_by_name(pPointer,"EFive");
sl@0
   274
		g_assert(retrievedValue == NULL);
sl@0
   275
		g_type_class_unref(pPointer);
sl@0
   276
	}
sl@0
   277
}
sl@0
   278
sl@0
   279
void test_interface()
sl@0
   280
{
sl@0
   281
	MamanBaz* pPointer =     g_object_new(baz_type,NULL);
sl@0
   282
	if(pPointer)
sl@0
   283
	{
sl@0
   284
		g_assert(0xdeadbeaf == pPointer->instance_member);
sl@0
   285
		maman_ibaz_do_action (MAMAN_IBAZ(pPointer));
sl@0
   286
		g_assert(10 == pPointer->instance_member);
sl@0
   287
		g_object_unref(pPointer);
sl@0
   288
	}
sl@0
   289
	
sl@0
   290
}
sl@0
   291
sl@0
   292
sl@0
   293
gboolean    my_cache_func(gpointer cache_data, GTypeClass *g_class)
sl@0
   294
{
sl@0
   295
	if(MAMAN_IS_BAZ_CLASS(g_class))
sl@0
   296
	{
sl@0
   297
		g_assert(strcmp("hello",(char*) cache_data) == 0);
sl@0
   298
		g_type_class_ref(baz_type); 
sl@0
   299
sl@0
   300
		g_type_class_unref_uncached (g_class);
sl@0
   301
		return TRUE;
sl@0
   302
	}
sl@0
   303
	else
sl@0
   304
	{
sl@0
   305
		return FALSE;
sl@0
   306
	}
sl@0
   307
}
sl@0
   308
sl@0
   309
int
sl@0
   310
main (int   argc,
sl@0
   311
      char *argv[])
sl@0
   312
{
sl@0
   313
  char* data;	
sl@0
   314
  #ifdef SYMBIAN
sl@0
   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);
sl@0
   316
  g_set_print_handler(mrtPrintHandler);
sl@0
   317
  #endif /*SYMBIAN*/
sl@0
   318
  
sl@0
   319
  g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
sl@0
   320
			  G_LOG_LEVEL_WARNING |
sl@0
   321
			  G_LOG_LEVEL_CRITICAL);
sl@0
   322
  g_type_init ();
sl@0
   323
sl@0
   324
  data = (char*) malloc(6);
sl@0
   325
  strcpy(data,"hello");
sl@0
   326
sl@0
   327
  g_type_add_class_cache_func(data, my_cache_func);
sl@0
   328
sl@0
   329
  test_static_iface ();
sl@0
   330
  test_dynamic_iface ();
sl@0
   331
  test_flags();
sl@0
   332
  test_enum();
sl@0
   333
  test_interface();
sl@0
   334
sl@0
   335
  #ifdef SYMBIAN
sl@0
   336
  testResultXml("defaultiface");
sl@0
   337
  #endif //SYMBIAN
sl@0
   338
  
sl@0
   339
  return 0;
sl@0
   340
}