os/ossrv/glib/tsrc/BC/tests/gobject/ifaceinit.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/ifaceinit.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,432 @@
     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 "TestIfaceInit"
    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 +#ifdef SYMBIAN
    1.34 +#include "mrt2_glib2_test.h"
    1.35 +#endif //SYMBIAN
    1.36 +
    1.37 +/* What this test tests is the ability to add interfaces dynamically; in
    1.38 + * particular adding interfaces to a class while that class is being
    1.39 + * initialized.
    1.40 + *
    1.41 + * The test defines 5 interfaces:
    1.42 + * 
    1.43 + * - TestIface1 is added before the class is initialized
    1.44 + * - TestIface2 is added in base_object_base_init()
    1.45 + * - TestIface3 is added in test_iface1_base_init()
    1.46 + * - TestIface4 is added in test_object_class_init()
    1.47 + * - TestIface5 is added in test_object_test_iface1_init()
    1.48 + * - TestIface6 is added after the class is initialized
    1.49 + */
    1.50 +
    1.51 +/* All 6 interfaces actually share the same class structure, though
    1.52 + * we use separate typedefs
    1.53 + */
    1.54 +typedef struct _TestIfaceClass TestIfaceClass;
    1.55 +
    1.56 +struct _TestIfaceClass
    1.57 +{
    1.58 +  GTypeInterface base_iface;
    1.59 +  guint val;
    1.60 +  guint base_val;
    1.61 +  guint default_val;
    1.62 +};
    1.63 +
    1.64 +#define TEST_TYPE_IFACE1           (test_iface1_get_type ())
    1.65 +#define TEST_IFACE1_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE1, TestIface1Class))
    1.66 +typedef struct _TestIface1      TestIface1;
    1.67 +typedef struct _TestIfaceClass  TestIface1Class;
    1.68 +
    1.69 +static void test_iface1_base_init    (TestIface1Class *iface);
    1.70 +static void test_iface1_default_init (TestIface1Class *iface, gpointer class_data);
    1.71 +
    1.72 +static DEFINE_IFACE(TestIface1, test_iface1, test_iface1_base_init, test_iface1_default_init)
    1.73 +
    1.74 +#define TEST_TYPE_IFACE2           (test_iface2_get_type ())
    1.75 +#define TEST_IFACE2_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE2, TestIface2Class))
    1.76 +typedef struct _TestIface2      TestIface2;
    1.77 +typedef struct _TestIfaceClass  TestIface2Class;
    1.78 +
    1.79 +static void test_iface2_base_init (TestIface2Class *iface);
    1.80 +
    1.81 +static DEFINE_IFACE(TestIface2, test_iface2, test_iface2_base_init, NULL)
    1.82 +
    1.83 +#define TEST_TYPE_IFACE3           (test_iface3_get_type ())
    1.84 +#define TEST_IFACE3_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE3, TestIface3Class))
    1.85 +typedef struct _TestIface3      TestIface3;
    1.86 +typedef struct _TestIfaceClass  TestIface3Class;
    1.87 +
    1.88 +static void  test_iface3_base_init (TestIface3Class *iface);
    1.89 +
    1.90 +static DEFINE_IFACE(TestIface3, test_iface3, test_iface3_base_init, NULL)
    1.91 +
    1.92 +#define TEST_TYPE_IFACE4           (test_iface4_get_type ())
    1.93 +#define TEST_IFACE4_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE4, TestIface4Class))
    1.94 +typedef struct _TestIface4      TestIface4;
    1.95 +typedef struct _TestIfaceClass  TestIface4Class;
    1.96 +
    1.97 +static void  test_iface4_base_init (TestIface4Class *iface);
    1.98 +
    1.99 +static DEFINE_IFACE(TestIface4, test_iface4, test_iface4_base_init, NULL)
   1.100 +
   1.101 +#define TEST_TYPE_IFACE5           (test_iface5_get_type ())
   1.102 +#define TEST_IFACE5_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE5, TestIface5Class))
   1.103 +typedef struct _TestIface5      TestIface5;
   1.104 +typedef struct _TestIfaceClass  TestIface5Class;
   1.105 +
   1.106 +static void  test_iface5_base_init (TestIface5Class *iface);
   1.107 +
   1.108 +static DEFINE_IFACE(TestIface5, test_iface5, test_iface5_base_init, NULL)
   1.109 +
   1.110 +#define TEST_TYPE_IFACE6           (test_iface6_get_type ())
   1.111 +#define TEST_IFACE6_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE6, TestIface6Class))
   1.112 +typedef struct _TestIface6      TestIface6;
   1.113 +typedef struct _TestIfaceClass  TestIface6Class;
   1.114 +
   1.115 +static void  test_iface6_base_init (TestIface6Class *iface);
   1.116 +
   1.117 +static DEFINE_IFACE(TestIface6, test_iface6, test_iface6_base_init, NULL)
   1.118 +
   1.119 +/*
   1.120 + * BaseObject, a parent class for TestObject
   1.121 + */
   1.122 +#define BASE_TYPE_OBJECT          (base_object_get_type ())
   1.123 +typedef struct _BaseObject        BaseObject;
   1.124 +typedef struct _BaseObjectClass   BaseObjectClass;
   1.125 +
   1.126 +struct _BaseObject
   1.127 +{
   1.128 +  GObject parent_instance;
   1.129 +};
   1.130 +struct _BaseObjectClass
   1.131 +{
   1.132 +  GObjectClass parent_class;
   1.133 +};
   1.134 +
   1.135 +/*
   1.136 + * TestObject, a parent class for TestObject
   1.137 + */
   1.138 +#define TEST_TYPE_OBJECT          (test_object_get_type ())
   1.139 +typedef struct _TestObject        TestObject;
   1.140 +typedef struct _TestObjectClass   TestObjectClass;
   1.141 +
   1.142 +struct _TestObject
   1.143 +{
   1.144 +  BaseObject parent_instance;
   1.145 +};
   1.146 +struct _TestObjectClass
   1.147 +{
   1.148 +  BaseObjectClass parent_class;
   1.149 +};
   1.150 +
   1.151 +#define TEST_CALLED_ONCE() G_STMT_START { \
   1.152 +  static gboolean called = 0;           \
   1.153 +  g_assert (!called);                   \
   1.154 +  called = TRUE;                        \
   1.155 +} G_STMT_END
   1.156 +
   1.157 +#define CHECK_IFACE_TWICE(iface) G_STMT_START {                                 \
   1.158 +  static guint n_calls = 0;                                                     \
   1.159 +  n_calls++;                                                                    \
   1.160 +  g_assert (n_calls <= 2);                                                      \
   1.161 +  g_assert (G_TYPE_IS_INTERFACE (((GTypeInterface*) iface)->g_type));           \
   1.162 +  if (n_calls == 1)                                                             \
   1.163 +    g_assert (((GTypeInterface*) iface)->g_instance_type == 0);                 \
   1.164 +  else                                                                          \
   1.165 +    g_assert (G_TYPE_IS_OBJECT (((GTypeInterface*) iface)->g_instance_type));   \
   1.166 +} G_STMT_END
   1.167 +
   1.168 +#define ADD_IFACE(n)  G_STMT_START {				\
   1.169 +  static GInterfaceInfo iface_info = {				\
   1.170 +    (GInterfaceInitFunc)test_object_test_iface##n##_init,	\
   1.171 +    NULL, NULL };						\
   1.172 +								\
   1.173 +  g_type_add_interface_static (TEST_TYPE_OBJECT,		\
   1.174 +			       test_iface##n##_get_type (),	\
   1.175 +			       &iface_info);			\
   1.176 +								\
   1.177 +} G_STMT_END
   1.178 +
   1.179 +static gboolean base1, base2, base3, base4, base5, base6;
   1.180 +static gboolean iface1, iface2, iface3, iface4, iface5, iface6;
   1.181 +
   1.182 +static void test_object_test_iface1_init (TestIface1Class *iface);
   1.183 +static void test_object_test_iface2_init (TestIface1Class *iface);
   1.184 +static void test_object_test_iface3_init (TestIface3Class *iface);
   1.185 +static void test_object_test_iface4_init (TestIface4Class *iface);
   1.186 +static void test_object_test_iface5_init (TestIface5Class *iface);
   1.187 +static void test_object_test_iface6_init (TestIface6Class *iface);
   1.188 +
   1.189 +static GType test_object_get_type (void);
   1.190 +
   1.191 +static void
   1.192 +test_object_test_iface1_init (TestIface1Class *iface)
   1.193 +{
   1.194 +  TEST_CALLED_ONCE();
   1.195 +
   1.196 +  g_assert (iface->default_val == 0x111111);
   1.197 +
   1.198 +  iface->val = 0x10001;
   1.199 +
   1.200 +  ADD_IFACE(5);
   1.201 +
   1.202 +  iface1 = TRUE;
   1.203 +}
   1.204 +
   1.205 +static void
   1.206 +test_object_test_iface2_init (TestIface2Class *iface)
   1.207 +{
   1.208 +  TEST_CALLED_ONCE();
   1.209 +  
   1.210 +  iface->val = 0x20002;
   1.211 +  
   1.212 +  iface2 = TRUE;
   1.213 +}
   1.214 +
   1.215 +static void
   1.216 +test_object_test_iface3_init (TestIface3Class *iface)
   1.217 +{
   1.218 +  TEST_CALLED_ONCE();
   1.219 +  
   1.220 +  iface->val = 0x30003;
   1.221 +  
   1.222 +  iface3 = TRUE;
   1.223 +}
   1.224 +
   1.225 +static void
   1.226 +test_object_test_iface4_init (TestIface4Class *iface)
   1.227 +{
   1.228 +  TEST_CALLED_ONCE();
   1.229 +
   1.230 +  iface->val = 0x40004;
   1.231 +  
   1.232 +  iface4 = TRUE;
   1.233 +}
   1.234 +
   1.235 +static void
   1.236 +test_object_test_iface5_init (TestIface5Class *iface)
   1.237 +{
   1.238 +  TEST_CALLED_ONCE();
   1.239 +
   1.240 +  iface->val = 0x50005;
   1.241 +  
   1.242 +  iface5 = TRUE;
   1.243 +}
   1.244 +
   1.245 +static void
   1.246 +test_object_test_iface6_init (TestIface6Class *iface)
   1.247 +{
   1.248 +  TEST_CALLED_ONCE();
   1.249 +
   1.250 +  iface->val = 0x60006;
   1.251 +  
   1.252 +  iface6 = TRUE;
   1.253 +}
   1.254 +
   1.255 +static void
   1.256 +test_iface1_default_init (TestIface1Class *iface,
   1.257 +                          gpointer         class_data)
   1.258 +{
   1.259 +  TEST_CALLED_ONCE();
   1.260 +  g_assert (iface->base_iface.g_type == TEST_TYPE_IFACE1);
   1.261 +  g_assert (iface->base_iface.g_instance_type == 0);
   1.262 +  g_assert (iface->base_val == 0x110011);
   1.263 +  g_assert (iface->val == 0);
   1.264 +  g_assert (iface->default_val == 0);
   1.265 +  iface->default_val = 0x111111;
   1.266 +}
   1.267 +
   1.268 +static void
   1.269 +test_iface1_base_init (TestIface1Class *iface)
   1.270 +{
   1.271 +  static guint n_calls = 0;
   1.272 +  n_calls++;
   1.273 +  g_assert (n_calls <= 2);
   1.274 +
   1.275 +  if (n_calls == 1)
   1.276 +    {
   1.277 +      iface->base_val = 0x110011;
   1.278 +      g_assert (iface->default_val == 0);
   1.279 +    }
   1.280 +  else
   1.281 +    {
   1.282 +      g_assert (iface->base_val == 0x110011);
   1.283 +      g_assert (iface->default_val == 0x111111);
   1.284 +    }
   1.285 +
   1.286 +  if (n_calls == 1)
   1.287 +    ADD_IFACE(3);
   1.288 +  
   1.289 +  base1 = TRUE;
   1.290 +}
   1.291 +
   1.292 +static void
   1.293 +test_iface2_base_init (TestIface2Class *iface)
   1.294 +{
   1.295 +  CHECK_IFACE_TWICE (iface);
   1.296 +
   1.297 +  iface->base_val = 0x220022;
   1.298 +  
   1.299 +  base2 = TRUE;
   1.300 +}
   1.301 +
   1.302 +static void
   1.303 +test_iface3_base_init (TestIface3Class *iface)
   1.304 +{
   1.305 +  CHECK_IFACE_TWICE (iface);
   1.306 +
   1.307 +  iface->base_val = 0x330033;
   1.308 +  
   1.309 +  base3 = TRUE;
   1.310 +}
   1.311 +
   1.312 +static void
   1.313 +test_iface4_base_init (TestIface4Class *iface)
   1.314 +{
   1.315 +  CHECK_IFACE_TWICE (iface);
   1.316 +
   1.317 +  iface->base_val = 0x440044;
   1.318 +
   1.319 +  base4 = TRUE;
   1.320 +}
   1.321 +
   1.322 +static void
   1.323 +test_iface5_base_init (TestIface5Class *iface)
   1.324 +{
   1.325 +  CHECK_IFACE_TWICE (iface);
   1.326 +
   1.327 +  iface->base_val = 0x550055;
   1.328 +
   1.329 +  base5 = TRUE;
   1.330 +}
   1.331 +
   1.332 +static void
   1.333 +test_iface6_base_init (TestIface6Class *iface)
   1.334 +{
   1.335 +  CHECK_IFACE_TWICE (iface);
   1.336 +
   1.337 +  iface->base_val = 0x660066;
   1.338 +  
   1.339 +  base6 = TRUE;
   1.340 +}
   1.341 +
   1.342 +static void
   1.343 +base_object_base_init (BaseObjectClass *class)
   1.344 +{
   1.345 +  static int n_called = 0;
   1.346 +  n_called++;
   1.347 +  
   1.348 +  /* The second time this is called is for TestObject */
   1.349 +  if (n_called == 2)
   1.350 +    {
   1.351 +      ADD_IFACE(2);
   1.352 +      
   1.353 +      /* No interface base init functions should have been called yet
   1.354 +       */
   1.355 +      g_assert (!base1 && !base2 && !base3 && !base4 && !base5 && !base6);
   1.356 +      g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
   1.357 +    }
   1.358 +}
   1.359 +
   1.360 +static void
   1.361 +test_object_class_init (TestObjectClass *class)
   1.362 +{
   1.363 +  ADD_IFACE(4);
   1.364 +
   1.365 +  /* At this point, the base init functions for all interfaces that have
   1.366 +   * been added should be called, but no interface init functions.
   1.367 +   */
   1.368 +  g_assert (base1 && base2 && base3 && base4 && !base5 && !base6);
   1.369 +  g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
   1.370 +}
   1.371 +
   1.372 +static DEFINE_TYPE(BaseObject, base_object,
   1.373 +		   NULL, base_object_base_init, NULL,
   1.374 +		   G_TYPE_OBJECT)
   1.375 +static DEFINE_TYPE(TestObject, test_object,
   1.376 +		   test_object_class_init, NULL, NULL,
   1.377 +		   BASE_TYPE_OBJECT)
   1.378 +
   1.379 +int
   1.380 +main (int   argc,
   1.381 +      char *argv[])
   1.382 +{
   1.383 +
   1.384 +
   1.385 +  TestObject *object;
   1.386 +  TestObjectClass *object_class;
   1.387 +  TestIfaceClass *iface;
   1.388 +  #ifdef SYMBIAN
   1.389 +  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.390 +  g_set_print_handler(mrtPrintHandler);
   1.391 +  #endif /*SYMBIAN*/
   1.392 +  g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
   1.393 +			  G_LOG_LEVEL_WARNING |
   1.394 +			  G_LOG_LEVEL_CRITICAL);
   1.395 +  g_type_init ();
   1.396 +
   1.397 +  /* We force the interfaces to be registered in a different order
   1.398 +   * than we add them, so our logic doesn't always deal with interfaces
   1.399 +   * added at the end.
   1.400 +   */
   1.401 +  (void)TEST_TYPE_IFACE4;
   1.402 +  (void)TEST_TYPE_IFACE2;
   1.403 +  (void)TEST_TYPE_IFACE6;
   1.404 +  (void)TEST_TYPE_IFACE5;
   1.405 +  (void)TEST_TYPE_IFACE3;
   1.406 +  (void)TEST_TYPE_IFACE1;
   1.407 +
   1.408 +  ADD_IFACE(1);
   1.409 +
   1.410 +  object_class = g_type_class_ref (TEST_TYPE_OBJECT);
   1.411 +
   1.412 +  ADD_IFACE(6);
   1.413 +
   1.414 +  /* All base and interface init functions should have been called
   1.415 +   */
   1.416 +  g_assert (base1 && base2 && base3 && base4 && base5 && base6);
   1.417 +  g_assert (iface1 && iface2 && iface3 && iface4 && iface5 && iface6);
   1.418 +  
   1.419 +  object = g_object_new (TEST_TYPE_OBJECT, NULL);
   1.420 +
   1.421 +  iface = TEST_IFACE1_GET_CLASS (object);
   1.422 +  g_assert (iface && iface->val == 0x10001 && iface->base_val == 0x110011);
   1.423 +  iface = TEST_IFACE3_GET_CLASS (object);
   1.424 +  g_assert (iface && iface->val == 0x30003 && iface->base_val == 0x330033);
   1.425 +  iface = TEST_IFACE4_GET_CLASS (object);
   1.426 +  g_assert (iface && iface->val == 0x40004 && iface->base_val == 0x440044);
   1.427 +  iface = TEST_IFACE5_GET_CLASS (object);
   1.428 +  g_assert (iface && iface->val == 0x50005 && iface->base_val == 0x550055);
   1.429 +  iface = TEST_IFACE6_GET_CLASS (object);
   1.430 +  g_assert (iface && iface->val == 0x60006 && iface->base_val == 0x660066);
   1.431 +  #ifdef SYMBIAN
   1.432 +  testResultXml("ifaceinit");
   1.433 +  #endif /*SYMBIAN*/
   1.434 +  return 0;
   1.435 +}