sl@0: /* GObject - GLib Type, Object, Parameter and Signal Library sl@0: * Copyright (C) 2001, 2003 Red Hat, Inc. sl@0: * Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved. sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General sl@0: * Public License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place, Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: */ sl@0: sl@0: #undef G_LOG_DOMAIN sl@0: #define G_LOG_DOMAIN "TestDefaultIface" sl@0: sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_DISABLE_CHECKS sl@0: #undef G_DISABLE_CAST_CHECKS sl@0: sl@0: #include sl@0: sl@0: #include "testcommon.h" sl@0: #include "testmodule.h" sl@0: #ifdef __SYMBIAN32__ sl@0: #include "mrt2_glib2_test.h" sl@0: #endif //__SYMBIAN32__ sl@0: sl@0: /* This test tests getting the default vtable for an interface sl@0: * and the initialization and finalization of such default sl@0: * interfaces. sl@0: * sl@0: * We test this both for static and for dynamic interfaces. sl@0: */ sl@0: sl@0: /********************************************************************** sl@0: * Static interface tests sl@0: **********************************************************************/ sl@0: sl@0: typedef struct _TestStaticIfaceClass TestStaticIfaceClass; sl@0: sl@0: struct _TestStaticIfaceClass sl@0: { sl@0: GTypeInterface base_iface; sl@0: guint val; sl@0: }; sl@0: sl@0: #define TEST_TYPE_STATIC_IFACE (test_static_iface_get_type ()) sl@0: sl@0: static void sl@0: test_static_iface_default_init (TestStaticIfaceClass *iface) sl@0: { sl@0: iface->val = 42; sl@0: } sl@0: sl@0: DEFINE_IFACE (TestStaticIface, test_static_iface, sl@0: NULL, test_static_iface_default_init) sl@0: sl@0: static void sl@0: test_static_iface (void) sl@0: { sl@0: TestStaticIfaceClass *static_iface; sl@0: sl@0: /* Not loaded until we call ref for the first time */ sl@0: static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE); sl@0: g_assert (static_iface == NULL); sl@0: sl@0: /* Ref loads */ sl@0: static_iface = g_type_default_interface_ref (TEST_TYPE_STATIC_IFACE); sl@0: g_assert (static_iface && static_iface->val == 42); sl@0: sl@0: /* Peek then works */ sl@0: static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE); sl@0: g_assert (static_iface && static_iface->val == 42); sl@0: sl@0: /* Unref does nothing */ sl@0: g_type_default_interface_unref (static_iface); sl@0: sl@0: /* And peek still works */ sl@0: static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE); sl@0: g_assert (static_iface && static_iface->val == 42); sl@0: } sl@0: sl@0: /********************************************************************** sl@0: * Dynamic interface tests sl@0: **********************************************************************/ sl@0: sl@0: typedef struct _TestDynamicIfaceClass TestDynamicIfaceClass; sl@0: sl@0: struct _TestDynamicIfaceClass sl@0: { sl@0: GTypeInterface base_iface; sl@0: guint val; sl@0: }; sl@0: sl@0: static GType test_dynamic_iface_type; sl@0: static gboolean dynamic_iface_init = FALSE; sl@0: sl@0: #define TEST_TYPE_DYNAMIC_IFACE (test_dynamic_iface_type) sl@0: sl@0: static void sl@0: test_dynamic_iface_default_init (TestStaticIfaceClass *iface) sl@0: { sl@0: dynamic_iface_init = TRUE; sl@0: iface->val = 42; sl@0: } sl@0: sl@0: static void sl@0: test_dynamic_iface_default_finalize (TestStaticIfaceClass *iface) sl@0: { sl@0: dynamic_iface_init = FALSE; sl@0: } sl@0: sl@0: static void sl@0: test_dynamic_iface_register (GTypeModule *module) sl@0: { sl@0: static const GTypeInfo iface_info = sl@0: { sl@0: sizeof (TestDynamicIfaceClass), sl@0: (GBaseInitFunc) NULL, sl@0: (GBaseFinalizeFunc) NULL, sl@0: (GClassInitFunc) test_dynamic_iface_default_init, sl@0: (GClassFinalizeFunc) test_dynamic_iface_default_finalize sl@0: }; sl@0: sl@0: test_dynamic_iface_type = g_type_module_register_type (module, G_TYPE_INTERFACE, sl@0: "TestDynamicIface", &iface_info, 0); sl@0: } sl@0: sl@0: static void sl@0: module_register (GTypeModule *module) sl@0: { sl@0: test_dynamic_iface_register (module); sl@0: } sl@0: sl@0: static void sl@0: test_dynamic_iface (void) sl@0: { sl@0: GTypeModule *module; sl@0: TestDynamicIfaceClass *dynamic_iface; sl@0: sl@0: module = test_module_new (module_register); sl@0: sl@0: /* Not loaded until we call ref for the first time */ sl@0: dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE); sl@0: g_assert (dynamic_iface == NULL); sl@0: sl@0: /* Ref loads */ sl@0: dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE); sl@0: g_assert (dynamic_iface_init); sl@0: g_assert (dynamic_iface && dynamic_iface->val == 42); sl@0: sl@0: /* Peek then works */ sl@0: dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE); sl@0: g_assert (dynamic_iface && dynamic_iface->val == 42); sl@0: sl@0: /* Unref causes finalize */ sl@0: g_type_default_interface_unref (dynamic_iface); sl@0: g_assert (!dynamic_iface_init); sl@0: sl@0: /* Peek returns NULL */ sl@0: dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE); sl@0: g_assert (dynamic_iface == NULL); sl@0: sl@0: /* Ref reloads */ sl@0: dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE); sl@0: g_assert (dynamic_iface_init); sl@0: g_assert (dynamic_iface && dynamic_iface->val == 42); sl@0: sl@0: /* And Unref causes finalize once more*/ sl@0: g_type_default_interface_unref (dynamic_iface); sl@0: g_assert (!dynamic_iface_init); sl@0: } sl@0: sl@0: int sl@0: main (int argc, sl@0: char *argv[]) sl@0: { sl@0: #ifdef __SYMBIAN32__ sl@0: 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: g_set_print_handler(mrtPrintHandler); sl@0: #endif /*__SYMBIAN32__*/ sl@0: g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) | sl@0: G_LOG_LEVEL_WARNING | sl@0: G_LOG_LEVEL_CRITICAL); sl@0: g_type_init (); sl@0: sl@0: test_static_iface (); sl@0: test_dynamic_iface (); sl@0: #ifdef __SYMBIAN32__ sl@0: testResultXml("defaultiface"); sl@0: #endif //__SYMBIAN32__ sl@0: sl@0: return 0; sl@0: }