1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/gobject/ifaceinherit.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,240 @@
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 "TestIfaceInherit"
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 +#include "testmodule.h"
1.34 +
1.35 +#ifdef SYMBIAN
1.36 +#include "mrt2_glib2_test.h"
1.37 +#endif // SYMBIAN
1.38 +
1.39 +
1.40 +/* This test tests inheritance of interface. We two object
1.41 + * class BaseObject and DerivedObject we add an interface
1.42 + * to BaseObject:
1.43 + *
1.44 + * I1) Before DerivedObject is registered
1.45 + * I2) After DerivedObject is registered, but before
1.46 + * DerivedObject is class initialized
1.47 + * I3) During DerivedObject's class_init
1.48 + * I4) After DerivedObject's class init
1.49 + *
1.50 + * We also do some tests of overriding.
1.51 + *
1.52 + * I5) We add an interface to BaseObject, then add the same
1.53 + * interface to DerivedObject. (Note that this is only legal
1.54 + * before DerivedObject's class_init; the results of
1.55 + * g_type_interface_peek() are not allowed to change from one
1.56 + * non-NULL vtable to another non-NULL vtable)
1.57 + */
1.58 +
1.59 +/*
1.60 + * BaseObject, a parent class for DerivedObject
1.61 + */
1.62 +#define BASE_TYPE_OBJECT (base_object_get_type ())
1.63 +typedef struct _BaseObject BaseObject;
1.64 +typedef struct _BaseObjectClass BaseObjectClass;
1.65 +
1.66 +struct _BaseObject
1.67 +{
1.68 + GObject parent_instance;
1.69 +};
1.70 +struct _BaseObjectClass
1.71 +{
1.72 + GObjectClass parent_class;
1.73 +};
1.74 +
1.75 +static GType base_object_get_type ();
1.76 +static GType derived_object_get_type ();
1.77 +
1.78 +/*
1.79 + * DerivedObject, the child class of DerivedObject
1.80 + */
1.81 +#define DERIVED_TYPE_OBJECT (derived_object_get_type ())
1.82 +typedef struct _DerivedObject DerivedObject;
1.83 +typedef struct _DerivedObjectClass DerivedObjectClass;
1.84 +
1.85 +struct _DerivedObject
1.86 +{
1.87 + BaseObject parent_instance;
1.88 +};
1.89 +struct _DerivedObjectClass
1.90 +{
1.91 + BaseObjectClass parent_class;
1.92 +};
1.93 +
1.94 +/*
1.95 + * The interfaces
1.96 + */
1.97 +typedef struct _TestIfaceClass TestIfaceClass;
1.98 +typedef struct _TestIfaceClass TestIface1Class;
1.99 +typedef struct _TestIfaceClass TestIface2Class;
1.100 +typedef struct _TestIfaceClass TestIface3Class;
1.101 +typedef struct _TestIfaceClass TestIface4Class;
1.102 +typedef struct _TestIfaceClass TestIface5Class;
1.103 +
1.104 +struct _TestIfaceClass
1.105 +{
1.106 + GTypeInterface base_iface;
1.107 + guint val;
1.108 +};
1.109 +
1.110 +#define TEST_TYPE_IFACE1 (test_iface1_get_type ())
1.111 +#define TEST_TYPE_IFACE2 (test_iface2_get_type ())
1.112 +#define TEST_TYPE_IFACE3 (test_iface3_get_type ())
1.113 +#define TEST_TYPE_IFACE4 (test_iface4_get_type ())
1.114 +#define TEST_TYPE_IFACE5 (test_iface5_get_type ())
1.115 +
1.116 +static DEFINE_IFACE (TestIface1, test_iface1, NULL, NULL)
1.117 +static DEFINE_IFACE (TestIface2, test_iface2, NULL, NULL)
1.118 +static DEFINE_IFACE (TestIface3, test_iface3, NULL, NULL)
1.119 +static DEFINE_IFACE (TestIface4, test_iface4, NULL, NULL)
1.120 +static DEFINE_IFACE (TestIface5, test_iface5, NULL, NULL)
1.121 +
1.122 +static void
1.123 +add_interface (GType object_type,
1.124 + GType iface_type,
1.125 + GInterfaceInitFunc init_func)
1.126 +{
1.127 + GInterfaceInfo iface_info = { NULL, NULL, NULL };
1.128 +
1.129 + iface_info.interface_init = init_func;
1.130 +
1.131 + g_type_add_interface_static (object_type, iface_type, &iface_info);
1.132 +}
1.133 +
1.134 +static void
1.135 +init_base_interface (TestIfaceClass *iface)
1.136 +{
1.137 + iface->val = 21;
1.138 +}
1.139 +
1.140 +static void
1.141 +add_base_interface (GType object_type,
1.142 + GType iface_type)
1.143 +{
1.144 + add_interface (object_type, iface_type,
1.145 + (GInterfaceInitFunc)init_base_interface);
1.146 +}
1.147 +
1.148 +static gboolean
1.149 +interface_is_base (GType object_type,
1.150 + GType iface_type)
1.151 +{
1.152 + gpointer g_class = g_type_class_peek (object_type);
1.153 + TestIfaceClass *iface = g_type_interface_peek (g_class, iface_type);
1.154 + return iface && iface->val == 21;
1.155 +}
1.156 +
1.157 +static void
1.158 +init_derived_interface (TestIfaceClass *iface)
1.159 +{
1.160 + iface->val = 42;
1.161 +}
1.162 +
1.163 +static void
1.164 +add_derived_interface (GType object_type,
1.165 + GType iface_type)
1.166 +{
1.167 + add_interface (object_type, iface_type,
1.168 + (GInterfaceInitFunc)init_derived_interface);
1.169 +}
1.170 +
1.171 +static gboolean
1.172 +interface_is_derived (GType object_type,
1.173 + GType iface_type)
1.174 +{
1.175 + gpointer g_class = g_type_class_peek (object_type);
1.176 + TestIfaceClass *iface = g_type_interface_peek (g_class, iface_type);
1.177 + return iface && iface->val == 42;
1.178 +}
1.179 +
1.180 +static void
1.181 +derived_object_class_init (BaseObjectClass *class)
1.182 +{
1.183 + add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE3);
1.184 +}
1.185 +
1.186 +static DEFINE_TYPE(BaseObject, base_object,
1.187 + NULL, NULL, NULL,
1.188 + G_TYPE_OBJECT)
1.189 +static DEFINE_TYPE(DerivedObject, derived_object,
1.190 + derived_object_class_init, NULL, NULL,
1.191 + BASE_TYPE_OBJECT)
1.192 +
1.193 +int
1.194 +main (int argc,
1.195 + char *argv[])
1.196 +{
1.197 +
1.198 +#ifdef SYMBIAN
1.199 + 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.200 + g_set_print_handler(mrtPrintHandler);
1.201 + #endif /*SYMBIAN*/
1.202 + g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
1.203 + G_LOG_LEVEL_WARNING |
1.204 + G_LOG_LEVEL_CRITICAL);
1.205 + g_type_init ();
1.206 +
1.207 + /* Register BaseObject */
1.208 + BASE_TYPE_OBJECT;
1.209 +
1.210 + add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE5);
1.211 +
1.212 + /* Class init BaseObject */
1.213 + g_type_class_ref (BASE_TYPE_OBJECT);
1.214 +
1.215 + add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE1);
1.216 +
1.217 + /* Register DerivedObject */
1.218 + DERIVED_TYPE_OBJECT;
1.219 +
1.220 + add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE2);
1.221 + add_derived_interface (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE5);
1.222 +
1.223 + /* Class init DerivedObject */
1.224 + g_type_class_ref (DERIVED_TYPE_OBJECT);
1.225 +
1.226 + add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE4);
1.227 +
1.228 + /* Check that all the non-overridden interfaces were properly inherited
1.229 + */
1.230 + g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE1));
1.231 + g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE2));
1.232 + g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE3));
1.233 + g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE4));
1.234 +
1.235 + /* Check that all the overridden interfaces were properly overridden
1.236 + */
1.237 + g_assert (interface_is_derived (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE5));
1.238 +
1.239 +#ifdef SYMBIAN
1.240 +testResultXml("ifaceinherit");
1.241 +#endif // SYMBIAN
1.242 + return 0;
1.243 +}