Update contrib.
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2001, 2003 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 * Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #define G_LOG_DOMAIN "TestIfaceInherit"
23 #undef G_DISABLE_ASSERT
24 #undef G_DISABLE_CHECKS
25 #undef G_DISABLE_CAST_CHECKS
27 #include <glib-object.h>
29 #include "testcommon.h"
30 #include "testmodule.h"
33 #include "mrt2_glib2_test.h"
34 #endif // __SYMBIAN32__
35 /* This test tests inheritance of interface. We two object
36 * class BaseObject and DerivedObject we add an interface
39 * I1) Before DerivedObject is registered
40 * I2) After DerivedObject is registered, but before
41 * DerivedObject is class initialized
42 * I3) During DerivedObject's class_init
43 * I4) After DerivedObject's class init
45 * We also do some tests of overriding.
47 * I5) We add an interface to BaseObject, then add the same
48 * interface to DerivedObject. (Note that this is only legal
49 * before DerivedObject's class_init; the results of
50 * g_type_interface_peek() are not allowed to change from one
51 * non-NULL vtable to another non-NULL vtable)
55 * BaseObject, a parent class for DerivedObject
57 #define BASE_TYPE_OBJECT (base_object_get_type ())
58 typedef struct _BaseObject BaseObject;
59 typedef struct _BaseObjectClass BaseObjectClass;
63 GObject parent_instance;
65 struct _BaseObjectClass
67 GObjectClass parent_class;
70 static GType base_object_get_type (void);
71 static GType derived_object_get_type (void);
74 * DerivedObject, the child class of DerivedObject
76 #define DERIVED_TYPE_OBJECT (derived_object_get_type ())
77 typedef struct _DerivedObject DerivedObject;
78 typedef struct _DerivedObjectClass DerivedObjectClass;
82 BaseObject parent_instance;
84 struct _DerivedObjectClass
86 BaseObjectClass parent_class;
92 typedef struct _TestIfaceClass TestIfaceClass;
93 typedef struct _TestIfaceClass TestIface1Class;
94 typedef struct _TestIfaceClass TestIface2Class;
95 typedef struct _TestIfaceClass TestIface3Class;
96 typedef struct _TestIfaceClass TestIface4Class;
97 typedef struct _TestIfaceClass TestIface5Class;
99 struct _TestIfaceClass
101 GTypeInterface base_iface;
105 #define TEST_TYPE_IFACE1 (test_iface1_get_type ())
106 #define TEST_TYPE_IFACE2 (test_iface2_get_type ())
107 #define TEST_TYPE_IFACE3 (test_iface3_get_type ())
108 #define TEST_TYPE_IFACE4 (test_iface4_get_type ())
109 #define TEST_TYPE_IFACE5 (test_iface5_get_type ())
111 static DEFINE_IFACE (TestIface1, test_iface1, NULL, NULL)
112 static DEFINE_IFACE (TestIface2, test_iface2, NULL, NULL)
113 static DEFINE_IFACE (TestIface3, test_iface3, NULL, NULL)
114 static DEFINE_IFACE (TestIface4, test_iface4, NULL, NULL)
115 static DEFINE_IFACE (TestIface5, test_iface5, NULL, NULL)
118 add_interface (GType object_type,
120 GInterfaceInitFunc init_func)
122 GInterfaceInfo iface_info = { NULL, NULL, NULL };
124 iface_info.interface_init = init_func;
126 g_type_add_interface_static (object_type, iface_type, &iface_info);
130 init_base_interface (TestIfaceClass *iface)
136 add_base_interface (GType object_type,
139 add_interface (object_type, iface_type,
140 (GInterfaceInitFunc)init_base_interface);
144 interface_is_base (GType object_type,
147 gpointer g_class = g_type_class_peek (object_type);
148 TestIfaceClass *iface = g_type_interface_peek (g_class, iface_type);
149 return iface && iface->val == 21;
153 init_derived_interface (TestIfaceClass *iface)
159 add_derived_interface (GType object_type,
162 add_interface (object_type, iface_type,
163 (GInterfaceInitFunc)init_derived_interface);
167 interface_is_derived (GType object_type,
170 gpointer g_class = g_type_class_peek (object_type);
171 TestIfaceClass *iface = g_type_interface_peek (g_class, iface_type);
172 return iface && iface->val == 42;
176 derived_object_class_init (BaseObjectClass *class)
178 add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE3);
181 static DEFINE_TYPE(BaseObject, base_object,
184 static DEFINE_TYPE(DerivedObject, derived_object,
185 derived_object_class_init, NULL, NULL,
193 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);
194 g_set_print_handler(mrtPrintHandler);
195 #endif /*__SYMBIAN32__*/
196 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
197 G_LOG_LEVEL_WARNING |
198 G_LOG_LEVEL_CRITICAL);
201 /* Register BaseObject */
204 add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE5);
206 /* Class init BaseObject */
207 g_type_class_ref (BASE_TYPE_OBJECT);
209 add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE1);
211 /* Register DerivedObject */
214 add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE2);
215 add_derived_interface (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE5);
217 /* Class init DerivedObject */
218 g_type_class_ref (DERIVED_TYPE_OBJECT);
220 add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE4);
222 /* Check that all the non-overridden interfaces were properly inherited
224 g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE1));
225 g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE2));
226 g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE3));
227 g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE4));
229 /* Check that all the overridden interfaces were properly overridden
231 g_assert (interface_is_derived (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE5));
234 testResultXml("ifaceinherit");
235 #endif // __SYMBIAN32__