1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/testgobject.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,431 @@
1.4 +/* GObject - GLib Type, Object, Parameter and Signal Library
1.5 + * Copyright (C) 2001 Red Hat, Inc.
1.6 + * Portion Copyright © 2008-09 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 +#include <string.h>
1.24 +
1.25 +#undef G_LOG_DOMAIN
1.26 +#define G_LOG_DOMAIN "TestObject"
1.27 +#include <glib-object.h>
1.28 +
1.29 +#ifdef SYMBIAN
1.30 +#include "mrt2_glib2_test.h"
1.31 +#endif /*SYMBIAN*/
1.32 +
1.33 +
1.34 +/* --- TestIface --- */
1.35 +#define TEST_TYPE_IFACE (test_iface_get_type ())
1.36 +#define TEST_IFACE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_TYPE_IFACE, TestIface))
1.37 +#define TEST_IS_IFACE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEST_TYPE_IFACE))
1.38 +#define TEST_IFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass))
1.39 +typedef struct _TestIface TestIface;
1.40 +typedef struct _TestIfaceClass TestIfaceClass;
1.41 +struct _TestIfaceClass
1.42 +{
1.43 + GTypeInterface base_iface;
1.44 + void (*print_string) (TestIface *tiobj,
1.45 + const gchar *string);
1.46 +};
1.47 +static void iface_base_init (TestIfaceClass *iface);
1.48 +static void iface_base_finalize (TestIfaceClass *iface);
1.49 +static void print_foo (TestIface *tiobj,
1.50 + const gchar *string);
1.51 +GType
1.52 +test_iface_get_type (void)
1.53 +{
1.54 + static GType test_iface_type = 0;
1.55 +
1.56 + if (!test_iface_type)
1.57 + {
1.58 + static const GTypeInfo test_iface_info =
1.59 + {
1.60 + sizeof (TestIfaceClass),
1.61 + (GBaseInitFunc) iface_base_init, /* base_init */
1.62 + (GBaseFinalizeFunc) iface_base_finalize, /* base_finalize */
1.63 + };
1.64 +
1.65 + test_iface_type = g_type_register_static (G_TYPE_INTERFACE, "TestIface", &test_iface_info, 0);
1.66 + g_type_interface_add_prerequisite (test_iface_type, G_TYPE_OBJECT);
1.67 + }
1.68 +
1.69 + return test_iface_type;
1.70 +}
1.71 +static guint iface_base_init_count = 0;
1.72 +static void
1.73 +iface_base_init (TestIfaceClass *iface)
1.74 +{
1.75 + iface_base_init_count++;
1.76 + if (iface_base_init_count == 1)
1.77 + {
1.78 + /* add signals here */
1.79 + }
1.80 +}
1.81 +static void
1.82 +iface_base_finalize (TestIfaceClass *iface)
1.83 +{
1.84 + iface_base_init_count--;
1.85 + if (iface_base_init_count == 0)
1.86 + {
1.87 + /* destroy signals here */
1.88 + }
1.89 +}
1.90 +static void
1.91 +print_foo (TestIface *tiobj,
1.92 + const gchar *string)
1.93 +{
1.94 + if (!string)
1.95 + string = "<NULL>";
1.96 +}
1.97 +
1.98 +static void
1.99 +test_object_test_iface_init (gpointer giface,
1.100 + gpointer iface_data)
1.101 +{
1.102 + TestIfaceClass *iface = giface;
1.103 +
1.104 + g_assert (iface_data == GUINT_TO_POINTER (42));
1.105 +
1.106 + g_assert (G_TYPE_FROM_INTERFACE (iface) == TEST_TYPE_IFACE);
1.107 +
1.108 + /* assert iface_base_init() was already called */
1.109 + g_assert (iface_base_init_count > 0);
1.110 +
1.111 + /* initialize stuff */
1.112 + iface->print_string = print_foo;
1.113 +}
1.114 +void
1.115 +iface_print_string (TestIface *tiobj,
1.116 + const gchar *string)
1.117 +{
1.118 + TestIfaceClass *iface;
1.119 +
1.120 + g_return_if_fail (TEST_IS_IFACE (tiobj));
1.121 + g_return_if_fail (G_IS_OBJECT (tiobj)); /* ensured through prerequisite */
1.122 +
1.123 + iface = TEST_IFACE_GET_CLASS (tiobj);
1.124 + g_object_ref (tiobj);
1.125 + iface->print_string (tiobj, string);
1.126 + g_object_unref (tiobj);
1.127 +}
1.128 +
1.129 +
1.130 +/* --- TestObject --- */
1.131 +#define TEST_TYPE_OBJECT (test_object_get_type ())
1.132 +#define TEST_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), TEST_TYPE_OBJECT, TestObject))
1.133 +#define TEST_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TEST_TYPE_OBJECT, TestObjectClass))
1.134 +#define TEST_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), TEST_TYPE_OBJECT))
1.135 +#define TEST_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TEST_TYPE_OBJECT))
1.136 +#define TEST_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TEST_TYPE_OBJECT, TestObjectClass))
1.137 +#define TEST_OBJECT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TEST_TYPE_OBJECT, TestObjectPrivate))
1.138 +typedef struct _TestObject TestObject;
1.139 +typedef struct _TestObjectClass TestObjectClass;
1.140 +typedef struct _TestObjectPrivate TestObjectPrivate;
1.141 +struct _TestObject
1.142 +{
1.143 + GObject parent_instance;
1.144 +};
1.145 +struct _TestObjectClass
1.146 +{
1.147 + GObjectClass parent_class;
1.148 +
1.149 + gchar* (*test_signal) (TestObject *tobject,
1.150 + TestIface *iface_object,
1.151 + gpointer tdata);
1.152 +};
1.153 +struct _TestObjectPrivate
1.154 +{
1.155 + int dummy1;
1.156 + gdouble dummy2;
1.157 +};
1.158 +static void test_object_class_init (TestObjectClass *class);
1.159 +static void test_object_init (TestObject *tobject);
1.160 +static gboolean test_signal_accumulator (GSignalInvocationHint *ihint,
1.161 + GValue *return_accu,
1.162 + const GValue *handler_return,
1.163 + gpointer data);
1.164 +static gchar* test_object_test_signal (TestObject *tobject,
1.165 + TestIface *iface_object,
1.166 + gpointer tdata);
1.167 +GType
1.168 +test_object_get_type (void)
1.169 +{
1.170 + static GType test_object_type = 0;
1.171 +
1.172 + if (!test_object_type)
1.173 + {
1.174 + static const GTypeInfo test_object_info =
1.175 + {
1.176 + sizeof (TestObjectClass),
1.177 + NULL, /* base_init */
1.178 + NULL, /* base_finalize */
1.179 + (GClassInitFunc) test_object_class_init,
1.180 + NULL, /* class_finalize */
1.181 + NULL, /* class_data */
1.182 + sizeof (TestObject),
1.183 + 5, /* n_preallocs */
1.184 + (GInstanceInitFunc) test_object_init,
1.185 + };
1.186 + GInterfaceInfo iface_info = { test_object_test_iface_init, NULL, GUINT_TO_POINTER (42) };
1.187 +
1.188 + test_object_type = g_type_register_static (G_TYPE_OBJECT, "TestObject", &test_object_info, 0);
1.189 + g_type_add_interface_static (test_object_type, TEST_TYPE_IFACE, &iface_info);
1.190 + }
1.191 +
1.192 + return test_object_type;
1.193 +}
1.194 +static void
1.195 +test_object_class_init (TestObjectClass *class)
1.196 +{
1.197 + /* GObjectClass *gobject_class = G_OBJECT_CLASS (class); */
1.198 +
1.199 + class->test_signal = test_object_test_signal;
1.200 +
1.201 + g_signal_new ("test-signal",
1.202 + G_OBJECT_CLASS_TYPE (class),
1.203 + G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP,
1.204 + G_STRUCT_OFFSET (TestObjectClass, test_signal),
1.205 + test_signal_accumulator, NULL,
1.206 + g_cclosure_marshal_STRING__OBJECT_POINTER,
1.207 + G_TYPE_STRING, 2, TEST_TYPE_IFACE, G_TYPE_POINTER);
1.208 +
1.209 + g_type_class_add_private (class, sizeof (TestObjectPrivate));
1.210 +}
1.211 +static void
1.212 +test_object_init (TestObject *tobject)
1.213 +{
1.214 + TestObjectPrivate *priv;
1.215 +
1.216 + priv = TEST_OBJECT_GET_PRIVATE (tobject);
1.217 +
1.218 + g_assert (priv);
1.219 + g_assert ((gchar *)priv >= (gchar *)tobject + sizeof (TestObject));
1.220 +
1.221 + priv->dummy1 = 54321;
1.222 +}
1.223 +/* Check to see if private data initialization in the
1.224 + * instance init function works.
1.225 + */
1.226 +static void
1.227 +test_object_check_private_init (TestObject *tobject)
1.228 +{
1.229 + TestObjectPrivate *priv;
1.230 +
1.231 + priv = TEST_OBJECT_GET_PRIVATE (tobject);
1.232 +
1.233 + g_assert (priv->dummy1 == 54321);
1.234 +}
1.235 +static gboolean
1.236 +test_signal_accumulator (GSignalInvocationHint *ihint,
1.237 + GValue *return_accu,
1.238 + const GValue *handler_return,
1.239 + gpointer data)
1.240 +{
1.241 + gchar *accu_string = (char *)g_value_get_string (return_accu);
1.242 + gchar *new_string = (char *)g_value_get_string (handler_return);
1.243 + gchar *result_string;
1.244 +
1.245 + if (accu_string)
1.246 + result_string = g_strconcat (accu_string, new_string, NULL);
1.247 + else if (new_string)
1.248 + result_string = g_strdup (new_string);
1.249 + else
1.250 + result_string = NULL;
1.251 +
1.252 + g_value_take_string (return_accu, result_string);
1.253 +
1.254 + return TRUE;
1.255 +}
1.256 +static gchar*
1.257 +test_object_test_signal (TestObject *tobject,
1.258 + TestIface *iface_object,
1.259 + gpointer tdata)
1.260 +{
1.261 + g_return_val_if_fail (TEST_IS_IFACE (iface_object), NULL);
1.262 +
1.263 + return g_strdup ("<default_handler>");
1.264 +}
1.265 +
1.266 +
1.267 +/* --- TestIface for DerivedObject --- */
1.268 +static void
1.269 +print_bar (TestIface *tiobj,
1.270 + const gchar *string)
1.271 +{
1.272 + TestIfaceClass *parent_iface;
1.273 +
1.274 + g_return_if_fail (TEST_IS_IFACE (tiobj));
1.275 +
1.276 + if (!string)
1.277 + string = "<NULL>";
1.278 +
1.279 + parent_iface = g_type_interface_peek_parent (TEST_IFACE_GET_CLASS (tiobj));
1.280 + parent_iface->print_string (tiobj, string);
1.281 +
1.282 + g_assert (g_type_interface_peek_parent (parent_iface) == NULL);
1.283 +}
1.284 +
1.285 +static void
1.286 +derived_object_test_iface_init (gpointer giface,
1.287 + gpointer iface_data)
1.288 +{
1.289 + TestIfaceClass *iface = giface;
1.290 +
1.291 + g_assert (iface_data == GUINT_TO_POINTER (87));
1.292 +
1.293 + g_assert (G_TYPE_FROM_INTERFACE (iface) == TEST_TYPE_IFACE);
1.294 +
1.295 + /* assert test_object_test_iface_init() was already called */
1.296 + g_assert (iface->print_string == print_foo);
1.297 +
1.298 + /* override stuff */
1.299 + iface->print_string = print_bar;
1.300 +}
1.301 +
1.302 +
1.303 +/* --- DerivedObject --- */
1.304 +#define DERIVED_TYPE_OBJECT (derived_object_get_type ())
1.305 +#define DERIVED_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), DERIVED_TYPE_OBJECT, DerivedObject))
1.306 +#define DERIVED_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DERIVED_TYPE_OBJECT, DerivedObjectClass))
1.307 +#define DERIVED_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), DERIVED_TYPE_OBJECT))
1.308 +#define DERIVED_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DERIVED_TYPE_OBJECT))
1.309 +#define DERIVED_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DERIVED_TYPE_OBJECT, DerivedObjectClass))
1.310 +#define DERIVED_OBJECT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DERIVED_TYPE_OBJECT, DerivedObjectPrivate))
1.311 +typedef struct _DerivedObject DerivedObject;
1.312 +typedef struct _TestObjectClass DerivedObjectClass;
1.313 +typedef struct _DerivedObjectPrivate DerivedObjectPrivate;
1.314 +struct _DerivedObject
1.315 +{
1.316 + TestObject parent_instance;
1.317 + int dummy1;
1.318 + int dummy2;
1.319 +};
1.320 +struct _DerivedObjectPrivate
1.321 +{
1.322 + char dummy;
1.323 +};
1.324 +static void derived_object_class_init (DerivedObjectClass *class);
1.325 +static void derived_object_init (DerivedObject *dobject);
1.326 +GType
1.327 +derived_object_get_type (void)
1.328 +{
1.329 + static GType derived_object_type = 0;
1.330 +
1.331 + if (!derived_object_type)
1.332 + {
1.333 + static const GTypeInfo derived_object_info =
1.334 + {
1.335 + sizeof (DerivedObjectClass),
1.336 + NULL, /* base_init */
1.337 + NULL, /* base_finalize */
1.338 + (GClassInitFunc) derived_object_class_init,
1.339 + NULL, /* class_finalize */
1.340 + NULL, /* class_data */
1.341 + sizeof (DerivedObject),
1.342 + 5, /* n_preallocs */
1.343 + (GInstanceInitFunc) derived_object_init,
1.344 + };
1.345 + GInterfaceInfo iface_info = { derived_object_test_iface_init, NULL, GUINT_TO_POINTER (87) };
1.346 +
1.347 + derived_object_type = g_type_register_static (TEST_TYPE_OBJECT, "DerivedObject", &derived_object_info, 0);
1.348 + g_type_add_interface_static (derived_object_type, TEST_TYPE_IFACE, &iface_info);
1.349 + }
1.350 +
1.351 + return derived_object_type;
1.352 +}
1.353 +static void
1.354 +derived_object_class_init (DerivedObjectClass *class)
1.355 +{
1.356 + g_type_class_add_private (class, sizeof (DerivedObjectPrivate));
1.357 +}
1.358 +static void
1.359 +derived_object_init (DerivedObject *dobject)
1.360 +{
1.361 + TestObjectPrivate *test_priv;
1.362 + DerivedObjectPrivate *derived_priv;
1.363 +
1.364 + derived_priv = DERIVED_OBJECT_GET_PRIVATE (dobject);
1.365 +
1.366 + g_assert (derived_priv);
1.367 + g_assert ((gchar *)derived_priv >= (gchar *)TEST_OBJECT_GET_PRIVATE (dobject) + sizeof (TestObjectPrivate));
1.368 +
1.369 + test_priv = TEST_OBJECT_GET_PRIVATE (dobject);
1.370 +
1.371 + g_assert (test_priv);
1.372 + g_assert ((gchar *)test_priv >= (gchar *)dobject + sizeof (TestObject));
1.373 +
1.374 +}
1.375 +
1.376 +/* --- main --- */
1.377 +int
1.378 +main (int argc,
1.379 + char *argv[])
1.380 +{
1.381 + GTypeInfo info = { 0, };
1.382 + GTypeFundamentalInfo finfo = { 0, };
1.383 + GType type;
1.384 + TestObject *sigarg;
1.385 + DerivedObject *dobject;
1.386 + TestObjectPrivate *priv;
1.387 + gchar *string = NULL;
1.388 +
1.389 + #ifdef SYMBIAN
1.390 + 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.391 + g_set_print_handler(mrtPrintHandler);
1.392 + #endif /*SYMBIAN*/
1.393 +
1.394 +
1.395 + g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
1.396 + G_LOG_LEVEL_WARNING |
1.397 + G_LOG_LEVEL_CRITICAL);
1.398 + g_type_init_with_debug_flags (G_TYPE_DEBUG_OBJECTS | G_TYPE_DEBUG_SIGNALS);
1.399 +
1.400 + /* test new fundamentals */
1.401 + g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST) == g_type_fundamental_next ());
1.402 + type = g_type_register_fundamental (g_type_fundamental_next (), "FooShadow1", &info, &finfo, 0);
1.403 + g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 1) == g_type_fundamental_next ());
1.404 + type = g_type_register_fundamental (g_type_fundamental_next (), "FooShadow2", &info, &finfo, 0);
1.405 + g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 2) == g_type_fundamental_next ());
1.406 + g_assert (g_type_from_name ("FooShadow1") == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST));
1.407 + g_assert (g_type_from_name ("FooShadow2") == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 1));
1.408 +
1.409 + /* to test past class initialization interface setups, create the class here */
1.410 + g_type_class_ref (TEST_TYPE_OBJECT);
1.411 +
1.412 + dobject = g_object_new (DERIVED_TYPE_OBJECT, NULL);
1.413 + test_object_check_private_init (TEST_OBJECT (dobject));
1.414 +
1.415 + sigarg = g_object_new (TEST_TYPE_OBJECT, NULL);
1.416 +
1.417 + g_signal_emit_by_name (dobject, "test-signal", sigarg, NULL, &string);
1.418 + g_assert (strcmp (string, "<default_handler><default_handler>") == 0);
1.419 + g_free (string);
1.420 +
1.421 + iface_print_string (TEST_IFACE (sigarg), "iface-string-from-test-type");
1.422 + iface_print_string (TEST_IFACE (dobject), "iface-string-from-derived-type");
1.423 +
1.424 + priv = TEST_OBJECT_GET_PRIVATE (dobject);
1.425 + g_assert (priv->dummy1 == 54321);
1.426 +
1.427 + g_object_unref (sigarg);
1.428 + g_object_unref (dobject);
1.429 +
1.430 +#ifdef SYMBIAN
1.431 + testResultXml("testgobject");
1.432 +#endif /* EMULATOR */
1.433 + return 0;
1.434 +}