1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tests/gobject/defaultiface.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,201 @@
1.4 +/* GObject - GLib Type, Object, Parameter and Signal Library
1.5 + * Copyright (C) 2001, 2003 Red Hat, Inc.
1.6 + * Portions copyright (c) 2006-2009 Nokia Corporation. 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 "TestDefaultIface"
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 +#ifdef __SYMBIAN32__
1.35 +#include "mrt2_glib2_test.h"
1.36 +#endif //__SYMBIAN32__
1.37 +
1.38 +/* This test tests getting the default vtable for an interface
1.39 + * and the initialization and finalization of such default
1.40 + * interfaces.
1.41 + *
1.42 + * We test this both for static and for dynamic interfaces.
1.43 + */
1.44 +
1.45 +/**********************************************************************
1.46 + * Static interface tests
1.47 + **********************************************************************/
1.48 +
1.49 +typedef struct _TestStaticIfaceClass TestStaticIfaceClass;
1.50 +
1.51 +struct _TestStaticIfaceClass
1.52 +{
1.53 + GTypeInterface base_iface;
1.54 + guint val;
1.55 +};
1.56 +
1.57 +#define TEST_TYPE_STATIC_IFACE (test_static_iface_get_type ())
1.58 +
1.59 +static void
1.60 +test_static_iface_default_init (TestStaticIfaceClass *iface)
1.61 +{
1.62 + iface->val = 42;
1.63 +}
1.64 +
1.65 +DEFINE_IFACE (TestStaticIface, test_static_iface,
1.66 + NULL, test_static_iface_default_init)
1.67 +
1.68 +static void
1.69 +test_static_iface (void)
1.70 +{
1.71 + TestStaticIfaceClass *static_iface;
1.72 +
1.73 + /* Not loaded until we call ref for the first time */
1.74 + static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
1.75 + g_assert (static_iface == NULL);
1.76 +
1.77 + /* Ref loads */
1.78 + static_iface = g_type_default_interface_ref (TEST_TYPE_STATIC_IFACE);
1.79 + g_assert (static_iface && static_iface->val == 42);
1.80 +
1.81 + /* Peek then works */
1.82 + static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
1.83 + g_assert (static_iface && static_iface->val == 42);
1.84 +
1.85 + /* Unref does nothing */
1.86 + g_type_default_interface_unref (static_iface);
1.87 +
1.88 + /* And peek still works */
1.89 + static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
1.90 + g_assert (static_iface && static_iface->val == 42);
1.91 +}
1.92 +
1.93 +/**********************************************************************
1.94 + * Dynamic interface tests
1.95 + **********************************************************************/
1.96 +
1.97 +typedef struct _TestDynamicIfaceClass TestDynamicIfaceClass;
1.98 +
1.99 +struct _TestDynamicIfaceClass
1.100 +{
1.101 + GTypeInterface base_iface;
1.102 + guint val;
1.103 +};
1.104 +
1.105 +static GType test_dynamic_iface_type;
1.106 +static gboolean dynamic_iface_init = FALSE;
1.107 +
1.108 +#define TEST_TYPE_DYNAMIC_IFACE (test_dynamic_iface_type)
1.109 +
1.110 +static void
1.111 +test_dynamic_iface_default_init (TestStaticIfaceClass *iface)
1.112 +{
1.113 + dynamic_iface_init = TRUE;
1.114 + iface->val = 42;
1.115 +}
1.116 +
1.117 +static void
1.118 +test_dynamic_iface_default_finalize (TestStaticIfaceClass *iface)
1.119 +{
1.120 + dynamic_iface_init = FALSE;
1.121 +}
1.122 +
1.123 +static void
1.124 +test_dynamic_iface_register (GTypeModule *module)
1.125 +{
1.126 + static const GTypeInfo iface_info =
1.127 + {
1.128 + sizeof (TestDynamicIfaceClass),
1.129 + (GBaseInitFunc) NULL,
1.130 + (GBaseFinalizeFunc) NULL,
1.131 + (GClassInitFunc) test_dynamic_iface_default_init,
1.132 + (GClassFinalizeFunc) test_dynamic_iface_default_finalize
1.133 + };
1.134 +
1.135 + test_dynamic_iface_type = g_type_module_register_type (module, G_TYPE_INTERFACE,
1.136 + "TestDynamicIface", &iface_info, 0);
1.137 +}
1.138 +
1.139 +static void
1.140 +module_register (GTypeModule *module)
1.141 +{
1.142 + test_dynamic_iface_register (module);
1.143 +}
1.144 +
1.145 +static void
1.146 +test_dynamic_iface (void)
1.147 +{
1.148 + GTypeModule *module;
1.149 + TestDynamicIfaceClass *dynamic_iface;
1.150 +
1.151 + module = test_module_new (module_register);
1.152 +
1.153 + /* Not loaded until we call ref for the first time */
1.154 + dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
1.155 + g_assert (dynamic_iface == NULL);
1.156 +
1.157 + /* Ref loads */
1.158 + dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE);
1.159 + g_assert (dynamic_iface_init);
1.160 + g_assert (dynamic_iface && dynamic_iface->val == 42);
1.161 +
1.162 + /* Peek then works */
1.163 + dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
1.164 + g_assert (dynamic_iface && dynamic_iface->val == 42);
1.165 +
1.166 + /* Unref causes finalize */
1.167 + g_type_default_interface_unref (dynamic_iface);
1.168 + g_assert (!dynamic_iface_init);
1.169 +
1.170 + /* Peek returns NULL */
1.171 + dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
1.172 + g_assert (dynamic_iface == NULL);
1.173 +
1.174 + /* Ref reloads */
1.175 + dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE);
1.176 + g_assert (dynamic_iface_init);
1.177 + g_assert (dynamic_iface && dynamic_iface->val == 42);
1.178 +
1.179 + /* And Unref causes finalize once more*/
1.180 + g_type_default_interface_unref (dynamic_iface);
1.181 + g_assert (!dynamic_iface_init);
1.182 +}
1.183 +
1.184 +int
1.185 +main (int argc,
1.186 + char *argv[])
1.187 +{
1.188 + #ifdef __SYMBIAN32__
1.189 + 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.190 + g_set_print_handler(mrtPrintHandler);
1.191 + #endif /*__SYMBIAN32__*/
1.192 + g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
1.193 + G_LOG_LEVEL_WARNING |
1.194 + G_LOG_LEVEL_CRITICAL);
1.195 + g_type_init ();
1.196 +
1.197 + test_static_iface ();
1.198 + test_dynamic_iface ();
1.199 + #ifdef __SYMBIAN32__
1.200 + testResultXml("defaultiface");
1.201 + #endif //__SYMBIAN32__
1.202 +
1.203 + return 0;
1.204 +}