os/ossrv/glib/tsrc/BC/tests/gobject/ifaceinit.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 "TestIfaceInit"
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
#ifdef SYMBIAN
sl@0
    31
#include "mrt2_glib2_test.h"
sl@0
    32
#endif //SYMBIAN
sl@0
    33
sl@0
    34
/* What this test tests is the ability to add interfaces dynamically; in
sl@0
    35
 * particular adding interfaces to a class while that class is being
sl@0
    36
 * initialized.
sl@0
    37
 *
sl@0
    38
 * The test defines 5 interfaces:
sl@0
    39
 * 
sl@0
    40
 * - TestIface1 is added before the class is initialized
sl@0
    41
 * - TestIface2 is added in base_object_base_init()
sl@0
    42
 * - TestIface3 is added in test_iface1_base_init()
sl@0
    43
 * - TestIface4 is added in test_object_class_init()
sl@0
    44
 * - TestIface5 is added in test_object_test_iface1_init()
sl@0
    45
 * - TestIface6 is added after the class is initialized
sl@0
    46
 */
sl@0
    47
sl@0
    48
/* All 6 interfaces actually share the same class structure, though
sl@0
    49
 * we use separate typedefs
sl@0
    50
 */
sl@0
    51
typedef struct _TestIfaceClass TestIfaceClass;
sl@0
    52
sl@0
    53
struct _TestIfaceClass
sl@0
    54
{
sl@0
    55
  GTypeInterface base_iface;
sl@0
    56
  guint val;
sl@0
    57
  guint base_val;
sl@0
    58
  guint default_val;
sl@0
    59
};
sl@0
    60
sl@0
    61
#define TEST_TYPE_IFACE1           (test_iface1_get_type ())
sl@0
    62
#define TEST_IFACE1_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE1, TestIface1Class))
sl@0
    63
typedef struct _TestIface1      TestIface1;
sl@0
    64
typedef struct _TestIfaceClass  TestIface1Class;
sl@0
    65
sl@0
    66
static void test_iface1_base_init    (TestIface1Class *iface);
sl@0
    67
static void test_iface1_default_init (TestIface1Class *iface, gpointer class_data);
sl@0
    68
sl@0
    69
static DEFINE_IFACE(TestIface1, test_iface1, test_iface1_base_init, test_iface1_default_init)
sl@0
    70
sl@0
    71
#define TEST_TYPE_IFACE2           (test_iface2_get_type ())
sl@0
    72
#define TEST_IFACE2_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE2, TestIface2Class))
sl@0
    73
typedef struct _TestIface2      TestIface2;
sl@0
    74
typedef struct _TestIfaceClass  TestIface2Class;
sl@0
    75
sl@0
    76
static void test_iface2_base_init (TestIface2Class *iface);
sl@0
    77
sl@0
    78
static DEFINE_IFACE(TestIface2, test_iface2, test_iface2_base_init, NULL)
sl@0
    79
sl@0
    80
#define TEST_TYPE_IFACE3           (test_iface3_get_type ())
sl@0
    81
#define TEST_IFACE3_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE3, TestIface3Class))
sl@0
    82
typedef struct _TestIface3      TestIface3;
sl@0
    83
typedef struct _TestIfaceClass  TestIface3Class;
sl@0
    84
sl@0
    85
static void  test_iface3_base_init (TestIface3Class *iface);
sl@0
    86
sl@0
    87
static DEFINE_IFACE(TestIface3, test_iface3, test_iface3_base_init, NULL)
sl@0
    88
sl@0
    89
#define TEST_TYPE_IFACE4           (test_iface4_get_type ())
sl@0
    90
#define TEST_IFACE4_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE4, TestIface4Class))
sl@0
    91
typedef struct _TestIface4      TestIface4;
sl@0
    92
typedef struct _TestIfaceClass  TestIface4Class;
sl@0
    93
sl@0
    94
static void  test_iface4_base_init (TestIface4Class *iface);
sl@0
    95
sl@0
    96
static DEFINE_IFACE(TestIface4, test_iface4, test_iface4_base_init, NULL)
sl@0
    97
sl@0
    98
#define TEST_TYPE_IFACE5           (test_iface5_get_type ())
sl@0
    99
#define TEST_IFACE5_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE5, TestIface5Class))
sl@0
   100
typedef struct _TestIface5      TestIface5;
sl@0
   101
typedef struct _TestIfaceClass  TestIface5Class;
sl@0
   102
sl@0
   103
static void  test_iface5_base_init (TestIface5Class *iface);
sl@0
   104
sl@0
   105
static DEFINE_IFACE(TestIface5, test_iface5, test_iface5_base_init, NULL)
sl@0
   106
sl@0
   107
#define TEST_TYPE_IFACE6           (test_iface6_get_type ())
sl@0
   108
#define TEST_IFACE6_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE6, TestIface6Class))
sl@0
   109
typedef struct _TestIface6      TestIface6;
sl@0
   110
typedef struct _TestIfaceClass  TestIface6Class;
sl@0
   111
sl@0
   112
static void  test_iface6_base_init (TestIface6Class *iface);
sl@0
   113
sl@0
   114
static DEFINE_IFACE(TestIface6, test_iface6, test_iface6_base_init, NULL)
sl@0
   115
sl@0
   116
/*
sl@0
   117
 * BaseObject, a parent class for TestObject
sl@0
   118
 */
sl@0
   119
#define BASE_TYPE_OBJECT          (base_object_get_type ())
sl@0
   120
typedef struct _BaseObject        BaseObject;
sl@0
   121
typedef struct _BaseObjectClass   BaseObjectClass;
sl@0
   122
sl@0
   123
struct _BaseObject
sl@0
   124
{
sl@0
   125
  GObject parent_instance;
sl@0
   126
};
sl@0
   127
struct _BaseObjectClass
sl@0
   128
{
sl@0
   129
  GObjectClass parent_class;
sl@0
   130
};
sl@0
   131
sl@0
   132
/*
sl@0
   133
 * TestObject, a parent class for TestObject
sl@0
   134
 */
sl@0
   135
#define TEST_TYPE_OBJECT          (test_object_get_type ())
sl@0
   136
typedef struct _TestObject        TestObject;
sl@0
   137
typedef struct _TestObjectClass   TestObjectClass;
sl@0
   138
sl@0
   139
struct _TestObject
sl@0
   140
{
sl@0
   141
  BaseObject parent_instance;
sl@0
   142
};
sl@0
   143
struct _TestObjectClass
sl@0
   144
{
sl@0
   145
  BaseObjectClass parent_class;
sl@0
   146
};
sl@0
   147
sl@0
   148
#define TEST_CALLED_ONCE() G_STMT_START { \
sl@0
   149
  static gboolean called = 0;           \
sl@0
   150
  g_assert (!called);                   \
sl@0
   151
  called = TRUE;                        \
sl@0
   152
} G_STMT_END
sl@0
   153
sl@0
   154
#define CHECK_IFACE_TWICE(iface) G_STMT_START {                                 \
sl@0
   155
  static guint n_calls = 0;                                                     \
sl@0
   156
  n_calls++;                                                                    \
sl@0
   157
  g_assert (n_calls <= 2);                                                      \
sl@0
   158
  g_assert (G_TYPE_IS_INTERFACE (((GTypeInterface*) iface)->g_type));           \
sl@0
   159
  if (n_calls == 1)                                                             \
sl@0
   160
    g_assert (((GTypeInterface*) iface)->g_instance_type == 0);                 \
sl@0
   161
  else                                                                          \
sl@0
   162
    g_assert (G_TYPE_IS_OBJECT (((GTypeInterface*) iface)->g_instance_type));   \
sl@0
   163
} G_STMT_END
sl@0
   164
sl@0
   165
#define ADD_IFACE(n)  G_STMT_START {				\
sl@0
   166
  static GInterfaceInfo iface_info = {				\
sl@0
   167
    (GInterfaceInitFunc)test_object_test_iface##n##_init,	\
sl@0
   168
    NULL, NULL };						\
sl@0
   169
								\
sl@0
   170
  g_type_add_interface_static (TEST_TYPE_OBJECT,		\
sl@0
   171
			       test_iface##n##_get_type (),	\
sl@0
   172
			       &iface_info);			\
sl@0
   173
								\
sl@0
   174
} G_STMT_END
sl@0
   175
sl@0
   176
static gboolean base1, base2, base3, base4, base5, base6;
sl@0
   177
static gboolean iface1, iface2, iface3, iface4, iface5, iface6;
sl@0
   178
sl@0
   179
static void test_object_test_iface1_init (TestIface1Class *iface);
sl@0
   180
static void test_object_test_iface2_init (TestIface1Class *iface);
sl@0
   181
static void test_object_test_iface3_init (TestIface3Class *iface);
sl@0
   182
static void test_object_test_iface4_init (TestIface4Class *iface);
sl@0
   183
static void test_object_test_iface5_init (TestIface5Class *iface);
sl@0
   184
static void test_object_test_iface6_init (TestIface6Class *iface);
sl@0
   185
sl@0
   186
static GType test_object_get_type (void);
sl@0
   187
sl@0
   188
static void
sl@0
   189
test_object_test_iface1_init (TestIface1Class *iface)
sl@0
   190
{
sl@0
   191
  TEST_CALLED_ONCE();
sl@0
   192
sl@0
   193
  g_assert (iface->default_val == 0x111111);
sl@0
   194
sl@0
   195
  iface->val = 0x10001;
sl@0
   196
sl@0
   197
  ADD_IFACE(5);
sl@0
   198
sl@0
   199
  iface1 = TRUE;
sl@0
   200
}
sl@0
   201
sl@0
   202
static void
sl@0
   203
test_object_test_iface2_init (TestIface2Class *iface)
sl@0
   204
{
sl@0
   205
  TEST_CALLED_ONCE();
sl@0
   206
  
sl@0
   207
  iface->val = 0x20002;
sl@0
   208
  
sl@0
   209
  iface2 = TRUE;
sl@0
   210
}
sl@0
   211
sl@0
   212
static void
sl@0
   213
test_object_test_iface3_init (TestIface3Class *iface)
sl@0
   214
{
sl@0
   215
  TEST_CALLED_ONCE();
sl@0
   216
  
sl@0
   217
  iface->val = 0x30003;
sl@0
   218
  
sl@0
   219
  iface3 = TRUE;
sl@0
   220
}
sl@0
   221
sl@0
   222
static void
sl@0
   223
test_object_test_iface4_init (TestIface4Class *iface)
sl@0
   224
{
sl@0
   225
  TEST_CALLED_ONCE();
sl@0
   226
sl@0
   227
  iface->val = 0x40004;
sl@0
   228
  
sl@0
   229
  iface4 = TRUE;
sl@0
   230
}
sl@0
   231
sl@0
   232
static void
sl@0
   233
test_object_test_iface5_init (TestIface5Class *iface)
sl@0
   234
{
sl@0
   235
  TEST_CALLED_ONCE();
sl@0
   236
sl@0
   237
  iface->val = 0x50005;
sl@0
   238
  
sl@0
   239
  iface5 = TRUE;
sl@0
   240
}
sl@0
   241
sl@0
   242
static void
sl@0
   243
test_object_test_iface6_init (TestIface6Class *iface)
sl@0
   244
{
sl@0
   245
  TEST_CALLED_ONCE();
sl@0
   246
sl@0
   247
  iface->val = 0x60006;
sl@0
   248
  
sl@0
   249
  iface6 = TRUE;
sl@0
   250
}
sl@0
   251
sl@0
   252
static void
sl@0
   253
test_iface1_default_init (TestIface1Class *iface,
sl@0
   254
                          gpointer         class_data)
sl@0
   255
{
sl@0
   256
  TEST_CALLED_ONCE();
sl@0
   257
  g_assert (iface->base_iface.g_type == TEST_TYPE_IFACE1);
sl@0
   258
  g_assert (iface->base_iface.g_instance_type == 0);
sl@0
   259
  g_assert (iface->base_val == 0x110011);
sl@0
   260
  g_assert (iface->val == 0);
sl@0
   261
  g_assert (iface->default_val == 0);
sl@0
   262
  iface->default_val = 0x111111;
sl@0
   263
}
sl@0
   264
sl@0
   265
static void
sl@0
   266
test_iface1_base_init (TestIface1Class *iface)
sl@0
   267
{
sl@0
   268
  static guint n_calls = 0;
sl@0
   269
  n_calls++;
sl@0
   270
  g_assert (n_calls <= 2);
sl@0
   271
sl@0
   272
  if (n_calls == 1)
sl@0
   273
    {
sl@0
   274
      iface->base_val = 0x110011;
sl@0
   275
      g_assert (iface->default_val == 0);
sl@0
   276
    }
sl@0
   277
  else
sl@0
   278
    {
sl@0
   279
      g_assert (iface->base_val == 0x110011);
sl@0
   280
      g_assert (iface->default_val == 0x111111);
sl@0
   281
    }
sl@0
   282
sl@0
   283
  if (n_calls == 1)
sl@0
   284
    ADD_IFACE(3);
sl@0
   285
  
sl@0
   286
  base1 = TRUE;
sl@0
   287
}
sl@0
   288
sl@0
   289
static void
sl@0
   290
test_iface2_base_init (TestIface2Class *iface)
sl@0
   291
{
sl@0
   292
  CHECK_IFACE_TWICE (iface);
sl@0
   293
sl@0
   294
  iface->base_val = 0x220022;
sl@0
   295
  
sl@0
   296
  base2 = TRUE;
sl@0
   297
}
sl@0
   298
sl@0
   299
static void
sl@0
   300
test_iface3_base_init (TestIface3Class *iface)
sl@0
   301
{
sl@0
   302
  CHECK_IFACE_TWICE (iface);
sl@0
   303
sl@0
   304
  iface->base_val = 0x330033;
sl@0
   305
  
sl@0
   306
  base3 = TRUE;
sl@0
   307
}
sl@0
   308
sl@0
   309
static void
sl@0
   310
test_iface4_base_init (TestIface4Class *iface)
sl@0
   311
{
sl@0
   312
  CHECK_IFACE_TWICE (iface);
sl@0
   313
sl@0
   314
  iface->base_val = 0x440044;
sl@0
   315
sl@0
   316
  base4 = TRUE;
sl@0
   317
}
sl@0
   318
sl@0
   319
static void
sl@0
   320
test_iface5_base_init (TestIface5Class *iface)
sl@0
   321
{
sl@0
   322
  CHECK_IFACE_TWICE (iface);
sl@0
   323
sl@0
   324
  iface->base_val = 0x550055;
sl@0
   325
sl@0
   326
  base5 = TRUE;
sl@0
   327
}
sl@0
   328
sl@0
   329
static void
sl@0
   330
test_iface6_base_init (TestIface6Class *iface)
sl@0
   331
{
sl@0
   332
  CHECK_IFACE_TWICE (iface);
sl@0
   333
sl@0
   334
  iface->base_val = 0x660066;
sl@0
   335
  
sl@0
   336
  base6 = TRUE;
sl@0
   337
}
sl@0
   338
sl@0
   339
static void
sl@0
   340
base_object_base_init (BaseObjectClass *class)
sl@0
   341
{
sl@0
   342
  static int n_called = 0;
sl@0
   343
  n_called++;
sl@0
   344
  
sl@0
   345
  /* The second time this is called is for TestObject */
sl@0
   346
  if (n_called == 2)
sl@0
   347
    {
sl@0
   348
      ADD_IFACE(2);
sl@0
   349
      
sl@0
   350
      /* No interface base init functions should have been called yet
sl@0
   351
       */
sl@0
   352
      g_assert (!base1 && !base2 && !base3 && !base4 && !base5 && !base6);
sl@0
   353
      g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
sl@0
   354
    }
sl@0
   355
}
sl@0
   356
sl@0
   357
static void
sl@0
   358
test_object_class_init (TestObjectClass *class)
sl@0
   359
{
sl@0
   360
  ADD_IFACE(4);
sl@0
   361
sl@0
   362
  /* At this point, the base init functions for all interfaces that have
sl@0
   363
   * been added should be called, but no interface init functions.
sl@0
   364
   */
sl@0
   365
  g_assert (base1 && base2 && base3 && base4 && !base5 && !base6);
sl@0
   366
  g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
sl@0
   367
}
sl@0
   368
sl@0
   369
static DEFINE_TYPE(BaseObject, base_object,
sl@0
   370
		   NULL, base_object_base_init, NULL,
sl@0
   371
		   G_TYPE_OBJECT)
sl@0
   372
static DEFINE_TYPE(TestObject, test_object,
sl@0
   373
		   test_object_class_init, NULL, NULL,
sl@0
   374
		   BASE_TYPE_OBJECT)
sl@0
   375
sl@0
   376
int
sl@0
   377
main (int   argc,
sl@0
   378
      char *argv[])
sl@0
   379
{
sl@0
   380
sl@0
   381
sl@0
   382
  TestObject *object;
sl@0
   383
  TestObjectClass *object_class;
sl@0
   384
  TestIfaceClass *iface;
sl@0
   385
  #ifdef SYMBIAN
sl@0
   386
  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
   387
  g_set_print_handler(mrtPrintHandler);
sl@0
   388
  #endif /*SYMBIAN*/
sl@0
   389
  g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
sl@0
   390
			  G_LOG_LEVEL_WARNING |
sl@0
   391
			  G_LOG_LEVEL_CRITICAL);
sl@0
   392
  g_type_init ();
sl@0
   393
sl@0
   394
  /* We force the interfaces to be registered in a different order
sl@0
   395
   * than we add them, so our logic doesn't always deal with interfaces
sl@0
   396
   * added at the end.
sl@0
   397
   */
sl@0
   398
  (void)TEST_TYPE_IFACE4;
sl@0
   399
  (void)TEST_TYPE_IFACE2;
sl@0
   400
  (void)TEST_TYPE_IFACE6;
sl@0
   401
  (void)TEST_TYPE_IFACE5;
sl@0
   402
  (void)TEST_TYPE_IFACE3;
sl@0
   403
  (void)TEST_TYPE_IFACE1;
sl@0
   404
sl@0
   405
  ADD_IFACE(1);
sl@0
   406
sl@0
   407
  object_class = g_type_class_ref (TEST_TYPE_OBJECT);
sl@0
   408
sl@0
   409
  ADD_IFACE(6);
sl@0
   410
sl@0
   411
  /* All base and interface init functions should have been called
sl@0
   412
   */
sl@0
   413
  g_assert (base1 && base2 && base3 && base4 && base5 && base6);
sl@0
   414
  g_assert (iface1 && iface2 && iface3 && iface4 && iface5 && iface6);
sl@0
   415
  
sl@0
   416
  object = g_object_new (TEST_TYPE_OBJECT, NULL);
sl@0
   417
sl@0
   418
  iface = TEST_IFACE1_GET_CLASS (object);
sl@0
   419
  g_assert (iface && iface->val == 0x10001 && iface->base_val == 0x110011);
sl@0
   420
  iface = TEST_IFACE3_GET_CLASS (object);
sl@0
   421
  g_assert (iface && iface->val == 0x30003 && iface->base_val == 0x330033);
sl@0
   422
  iface = TEST_IFACE4_GET_CLASS (object);
sl@0
   423
  g_assert (iface && iface->val == 0x40004 && iface->base_val == 0x440044);
sl@0
   424
  iface = TEST_IFACE5_GET_CLASS (object);
sl@0
   425
  g_assert (iface && iface->val == 0x50005 && iface->base_val == 0x550055);
sl@0
   426
  iface = TEST_IFACE6_GET_CLASS (object);
sl@0
   427
  g_assert (iface && iface->val == 0x60006 && iface->base_val == 0x660066);
sl@0
   428
  #ifdef SYMBIAN
sl@0
   429
  testResultXml("ifaceinit");
sl@0
   430
  #endif /*SYMBIAN*/
sl@0
   431
  return 0;
sl@0
   432
}