sl@0: /* GObject - GLib Type, Object, Parameter and Signal Library sl@0: * Copyright (C) 2001, 2003 Red Hat, Inc. sl@0: * Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). 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 "TestIfaceInit" 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: #ifdef SYMBIAN sl@0: #include "mrt2_glib2_test.h" sl@0: #endif //SYMBIAN sl@0: sl@0: /* What this test tests is the ability to add interfaces dynamically; in sl@0: * particular adding interfaces to a class while that class is being sl@0: * initialized. sl@0: * sl@0: * The test defines 5 interfaces: sl@0: * sl@0: * - TestIface1 is added before the class is initialized sl@0: * - TestIface2 is added in base_object_base_init() sl@0: * - TestIface3 is added in test_iface1_base_init() sl@0: * - TestIface4 is added in test_object_class_init() sl@0: * - TestIface5 is added in test_object_test_iface1_init() sl@0: * - TestIface6 is added after the class is initialized sl@0: */ sl@0: sl@0: /* All 6 interfaces actually share the same class structure, though sl@0: * we use separate typedefs sl@0: */ sl@0: typedef struct _TestIfaceClass TestIfaceClass; sl@0: sl@0: struct _TestIfaceClass sl@0: { sl@0: GTypeInterface base_iface; sl@0: guint val; sl@0: guint base_val; sl@0: guint default_val; sl@0: }; sl@0: sl@0: #define TEST_TYPE_IFACE1 (test_iface1_get_type ()) sl@0: #define TEST_IFACE1_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE1, TestIface1Class)) sl@0: typedef struct _TestIface1 TestIface1; sl@0: typedef struct _TestIfaceClass TestIface1Class; sl@0: sl@0: static void test_iface1_base_init (TestIface1Class *iface); sl@0: static void test_iface1_default_init (TestIface1Class *iface, gpointer class_data); sl@0: sl@0: static DEFINE_IFACE(TestIface1, test_iface1, test_iface1_base_init, test_iface1_default_init) sl@0: sl@0: #define TEST_TYPE_IFACE2 (test_iface2_get_type ()) sl@0: #define TEST_IFACE2_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE2, TestIface2Class)) sl@0: typedef struct _TestIface2 TestIface2; sl@0: typedef struct _TestIfaceClass TestIface2Class; sl@0: sl@0: static void test_iface2_base_init (TestIface2Class *iface); sl@0: sl@0: static DEFINE_IFACE(TestIface2, test_iface2, test_iface2_base_init, NULL) sl@0: sl@0: #define TEST_TYPE_IFACE3 (test_iface3_get_type ()) sl@0: #define TEST_IFACE3_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE3, TestIface3Class)) sl@0: typedef struct _TestIface3 TestIface3; sl@0: typedef struct _TestIfaceClass TestIface3Class; sl@0: sl@0: static void test_iface3_base_init (TestIface3Class *iface); sl@0: sl@0: static DEFINE_IFACE(TestIface3, test_iface3, test_iface3_base_init, NULL) sl@0: sl@0: #define TEST_TYPE_IFACE4 (test_iface4_get_type ()) sl@0: #define TEST_IFACE4_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE4, TestIface4Class)) sl@0: typedef struct _TestIface4 TestIface4; sl@0: typedef struct _TestIfaceClass TestIface4Class; sl@0: sl@0: static void test_iface4_base_init (TestIface4Class *iface); sl@0: sl@0: static DEFINE_IFACE(TestIface4, test_iface4, test_iface4_base_init, NULL) sl@0: sl@0: #define TEST_TYPE_IFACE5 (test_iface5_get_type ()) sl@0: #define TEST_IFACE5_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE5, TestIface5Class)) sl@0: typedef struct _TestIface5 TestIface5; sl@0: typedef struct _TestIfaceClass TestIface5Class; sl@0: sl@0: static void test_iface5_base_init (TestIface5Class *iface); sl@0: sl@0: static DEFINE_IFACE(TestIface5, test_iface5, test_iface5_base_init, NULL) sl@0: sl@0: #define TEST_TYPE_IFACE6 (test_iface6_get_type ()) sl@0: #define TEST_IFACE6_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE6, TestIface6Class)) sl@0: typedef struct _TestIface6 TestIface6; sl@0: typedef struct _TestIfaceClass TestIface6Class; sl@0: sl@0: static void test_iface6_base_init (TestIface6Class *iface); sl@0: sl@0: static DEFINE_IFACE(TestIface6, test_iface6, test_iface6_base_init, NULL) sl@0: sl@0: /* sl@0: * BaseObject, a parent class for TestObject sl@0: */ sl@0: #define BASE_TYPE_OBJECT (base_object_get_type ()) sl@0: typedef struct _BaseObject BaseObject; sl@0: typedef struct _BaseObjectClass BaseObjectClass; sl@0: sl@0: struct _BaseObject sl@0: { sl@0: GObject parent_instance; sl@0: }; sl@0: struct _BaseObjectClass sl@0: { sl@0: GObjectClass parent_class; sl@0: }; sl@0: sl@0: /* sl@0: * TestObject, a parent class for TestObject sl@0: */ sl@0: #define TEST_TYPE_OBJECT (test_object_get_type ()) sl@0: typedef struct _TestObject TestObject; sl@0: typedef struct _TestObjectClass TestObjectClass; sl@0: sl@0: struct _TestObject sl@0: { sl@0: BaseObject parent_instance; sl@0: }; sl@0: struct _TestObjectClass sl@0: { sl@0: BaseObjectClass parent_class; sl@0: }; sl@0: sl@0: #define TEST_CALLED_ONCE() G_STMT_START { \ sl@0: static gboolean called = 0; \ sl@0: g_assert (!called); \ sl@0: called = TRUE; \ sl@0: } G_STMT_END sl@0: sl@0: #define CHECK_IFACE_TWICE(iface) G_STMT_START { \ sl@0: static guint n_calls = 0; \ sl@0: n_calls++; \ sl@0: g_assert (n_calls <= 2); \ sl@0: g_assert (G_TYPE_IS_INTERFACE (((GTypeInterface*) iface)->g_type)); \ sl@0: if (n_calls == 1) \ sl@0: g_assert (((GTypeInterface*) iface)->g_instance_type == 0); \ sl@0: else \ sl@0: g_assert (G_TYPE_IS_OBJECT (((GTypeInterface*) iface)->g_instance_type)); \ sl@0: } G_STMT_END sl@0: sl@0: #define ADD_IFACE(n) G_STMT_START { \ sl@0: static GInterfaceInfo iface_info = { \ sl@0: (GInterfaceInitFunc)test_object_test_iface##n##_init, \ sl@0: NULL, NULL }; \ sl@0: \ sl@0: g_type_add_interface_static (TEST_TYPE_OBJECT, \ sl@0: test_iface##n##_get_type (), \ sl@0: &iface_info); \ sl@0: \ sl@0: } G_STMT_END sl@0: sl@0: static gboolean base1, base2, base3, base4, base5, base6; sl@0: static gboolean iface1, iface2, iface3, iface4, iface5, iface6; sl@0: sl@0: static void test_object_test_iface1_init (TestIface1Class *iface); sl@0: static void test_object_test_iface2_init (TestIface1Class *iface); sl@0: static void test_object_test_iface3_init (TestIface3Class *iface); sl@0: static void test_object_test_iface4_init (TestIface4Class *iface); sl@0: static void test_object_test_iface5_init (TestIface5Class *iface); sl@0: static void test_object_test_iface6_init (TestIface6Class *iface); sl@0: sl@0: static GType test_object_get_type (void); sl@0: sl@0: static void sl@0: test_object_test_iface1_init (TestIface1Class *iface) sl@0: { sl@0: TEST_CALLED_ONCE(); sl@0: sl@0: g_assert (iface->default_val == 0x111111); sl@0: sl@0: iface->val = 0x10001; sl@0: sl@0: ADD_IFACE(5); sl@0: sl@0: iface1 = TRUE; sl@0: } sl@0: sl@0: static void sl@0: test_object_test_iface2_init (TestIface2Class *iface) sl@0: { sl@0: TEST_CALLED_ONCE(); sl@0: sl@0: iface->val = 0x20002; sl@0: sl@0: iface2 = TRUE; sl@0: } sl@0: sl@0: static void sl@0: test_object_test_iface3_init (TestIface3Class *iface) sl@0: { sl@0: TEST_CALLED_ONCE(); sl@0: sl@0: iface->val = 0x30003; sl@0: sl@0: iface3 = TRUE; sl@0: } sl@0: sl@0: static void sl@0: test_object_test_iface4_init (TestIface4Class *iface) sl@0: { sl@0: TEST_CALLED_ONCE(); sl@0: sl@0: iface->val = 0x40004; sl@0: sl@0: iface4 = TRUE; sl@0: } sl@0: sl@0: static void sl@0: test_object_test_iface5_init (TestIface5Class *iface) sl@0: { sl@0: TEST_CALLED_ONCE(); sl@0: sl@0: iface->val = 0x50005; sl@0: sl@0: iface5 = TRUE; sl@0: } sl@0: sl@0: static void sl@0: test_object_test_iface6_init (TestIface6Class *iface) sl@0: { sl@0: TEST_CALLED_ONCE(); sl@0: sl@0: iface->val = 0x60006; sl@0: sl@0: iface6 = TRUE; sl@0: } sl@0: sl@0: static void sl@0: test_iface1_default_init (TestIface1Class *iface, sl@0: gpointer class_data) sl@0: { sl@0: TEST_CALLED_ONCE(); sl@0: g_assert (iface->base_iface.g_type == TEST_TYPE_IFACE1); sl@0: g_assert (iface->base_iface.g_instance_type == 0); sl@0: g_assert (iface->base_val == 0x110011); sl@0: g_assert (iface->val == 0); sl@0: g_assert (iface->default_val == 0); sl@0: iface->default_val = 0x111111; sl@0: } sl@0: sl@0: static void sl@0: test_iface1_base_init (TestIface1Class *iface) sl@0: { sl@0: static guint n_calls = 0; sl@0: n_calls++; sl@0: g_assert (n_calls <= 2); sl@0: sl@0: if (n_calls == 1) sl@0: { sl@0: iface->base_val = 0x110011; sl@0: g_assert (iface->default_val == 0); sl@0: } sl@0: else sl@0: { sl@0: g_assert (iface->base_val == 0x110011); sl@0: g_assert (iface->default_val == 0x111111); sl@0: } sl@0: sl@0: if (n_calls == 1) sl@0: ADD_IFACE(3); sl@0: sl@0: base1 = TRUE; sl@0: } sl@0: sl@0: static void sl@0: test_iface2_base_init (TestIface2Class *iface) sl@0: { sl@0: CHECK_IFACE_TWICE (iface); sl@0: sl@0: iface->base_val = 0x220022; sl@0: sl@0: base2 = TRUE; sl@0: } sl@0: sl@0: static void sl@0: test_iface3_base_init (TestIface3Class *iface) sl@0: { sl@0: CHECK_IFACE_TWICE (iface); sl@0: sl@0: iface->base_val = 0x330033; sl@0: sl@0: base3 = TRUE; sl@0: } sl@0: sl@0: static void sl@0: test_iface4_base_init (TestIface4Class *iface) sl@0: { sl@0: CHECK_IFACE_TWICE (iface); sl@0: sl@0: iface->base_val = 0x440044; sl@0: sl@0: base4 = TRUE; sl@0: } sl@0: sl@0: static void sl@0: test_iface5_base_init (TestIface5Class *iface) sl@0: { sl@0: CHECK_IFACE_TWICE (iface); sl@0: sl@0: iface->base_val = 0x550055; sl@0: sl@0: base5 = TRUE; sl@0: } sl@0: sl@0: static void sl@0: test_iface6_base_init (TestIface6Class *iface) sl@0: { sl@0: CHECK_IFACE_TWICE (iface); sl@0: sl@0: iface->base_val = 0x660066; sl@0: sl@0: base6 = TRUE; sl@0: } sl@0: sl@0: static void sl@0: base_object_base_init (BaseObjectClass *class) sl@0: { sl@0: static int n_called = 0; sl@0: n_called++; sl@0: sl@0: /* The second time this is called is for TestObject */ sl@0: if (n_called == 2) sl@0: { sl@0: ADD_IFACE(2); sl@0: sl@0: /* No interface base init functions should have been called yet sl@0: */ sl@0: g_assert (!base1 && !base2 && !base3 && !base4 && !base5 && !base6); sl@0: g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6); sl@0: } sl@0: } sl@0: sl@0: static void sl@0: test_object_class_init (TestObjectClass *class) sl@0: { sl@0: ADD_IFACE(4); sl@0: sl@0: /* At this point, the base init functions for all interfaces that have sl@0: * been added should be called, but no interface init functions. sl@0: */ sl@0: g_assert (base1 && base2 && base3 && base4 && !base5 && !base6); sl@0: g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6); sl@0: } sl@0: sl@0: static DEFINE_TYPE(BaseObject, base_object, sl@0: NULL, base_object_base_init, NULL, sl@0: G_TYPE_OBJECT) sl@0: static DEFINE_TYPE(TestObject, test_object, sl@0: test_object_class_init, NULL, NULL, sl@0: BASE_TYPE_OBJECT) sl@0: sl@0: int sl@0: main (int argc, sl@0: char *argv[]) sl@0: { sl@0: sl@0: sl@0: TestObject *object; sl@0: TestObjectClass *object_class; sl@0: TestIfaceClass *iface; sl@0: #ifdef SYMBIAN 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 /*SYMBIAN*/ 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: /* We force the interfaces to be registered in a different order sl@0: * than we add them, so our logic doesn't always deal with interfaces sl@0: * added at the end. sl@0: */ sl@0: (void)TEST_TYPE_IFACE4; sl@0: (void)TEST_TYPE_IFACE2; sl@0: (void)TEST_TYPE_IFACE6; sl@0: (void)TEST_TYPE_IFACE5; sl@0: (void)TEST_TYPE_IFACE3; sl@0: (void)TEST_TYPE_IFACE1; sl@0: sl@0: ADD_IFACE(1); sl@0: sl@0: object_class = g_type_class_ref (TEST_TYPE_OBJECT); sl@0: sl@0: ADD_IFACE(6); sl@0: sl@0: /* All base and interface init functions should have been called sl@0: */ sl@0: g_assert (base1 && base2 && base3 && base4 && base5 && base6); sl@0: g_assert (iface1 && iface2 && iface3 && iface4 && iface5 && iface6); sl@0: sl@0: object = g_object_new (TEST_TYPE_OBJECT, NULL); sl@0: sl@0: iface = TEST_IFACE1_GET_CLASS (object); sl@0: g_assert (iface && iface->val == 0x10001 && iface->base_val == 0x110011); sl@0: iface = TEST_IFACE3_GET_CLASS (object); sl@0: g_assert (iface && iface->val == 0x30003 && iface->base_val == 0x330033); sl@0: iface = TEST_IFACE4_GET_CLASS (object); sl@0: g_assert (iface && iface->val == 0x40004 && iface->base_val == 0x440044); sl@0: iface = TEST_IFACE5_GET_CLASS (object); sl@0: g_assert (iface && iface->val == 0x50005 && iface->base_val == 0x550055); sl@0: iface = TEST_IFACE6_GET_CLASS (object); sl@0: g_assert (iface && iface->val == 0x60006 && iface->base_val == 0x660066); sl@0: #ifdef SYMBIAN sl@0: testResultXml("ifaceinit"); sl@0: #endif /*SYMBIAN*/ sl@0: return 0; sl@0: }