|
1 /* GObject - GLib Type, Object, Parameter and Signal Library |
|
2 * Copyright (C) 2001, 2003 Red Hat, Inc. |
|
3 * |
|
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. |
|
13 * |
|
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. |
|
18 */ |
|
19 |
|
20 #undef G_LOG_DOMAIN |
|
21 #define G_LOG_DOMAIN "TestIfaceInherit" |
|
22 |
|
23 #undef G_DISABLE_ASSERT |
|
24 #undef G_DISABLE_CHECKS |
|
25 #undef G_DISABLE_CAST_CHECKS |
|
26 |
|
27 #include <glib-object.h> |
|
28 |
|
29 #include "testcommon.h" |
|
30 #include "testmodule.h" |
|
31 |
|
32 #ifdef __SYMBIAN32__ |
|
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 |
|
37 * to BaseObject: |
|
38 * |
|
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 |
|
44 * |
|
45 * We also do some tests of overriding. |
|
46 * |
|
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) |
|
52 */ |
|
53 |
|
54 /* |
|
55 * BaseObject, a parent class for DerivedObject |
|
56 */ |
|
57 #define BASE_TYPE_OBJECT (base_object_get_type ()) |
|
58 typedef struct _BaseObject BaseObject; |
|
59 typedef struct _BaseObjectClass BaseObjectClass; |
|
60 |
|
61 struct _BaseObject |
|
62 { |
|
63 GObject parent_instance; |
|
64 }; |
|
65 struct _BaseObjectClass |
|
66 { |
|
67 GObjectClass parent_class; |
|
68 }; |
|
69 |
|
70 static GType base_object_get_type (void); |
|
71 static GType derived_object_get_type (void); |
|
72 |
|
73 /* |
|
74 * DerivedObject, the child class of DerivedObject |
|
75 */ |
|
76 #define DERIVED_TYPE_OBJECT (derived_object_get_type ()) |
|
77 typedef struct _DerivedObject DerivedObject; |
|
78 typedef struct _DerivedObjectClass DerivedObjectClass; |
|
79 |
|
80 struct _DerivedObject |
|
81 { |
|
82 BaseObject parent_instance; |
|
83 }; |
|
84 struct _DerivedObjectClass |
|
85 { |
|
86 BaseObjectClass parent_class; |
|
87 }; |
|
88 |
|
89 /* |
|
90 * The interfaces |
|
91 */ |
|
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; |
|
98 |
|
99 struct _TestIfaceClass |
|
100 { |
|
101 GTypeInterface base_iface; |
|
102 guint val; |
|
103 }; |
|
104 |
|
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 ()) |
|
110 |
|
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) |
|
116 |
|
117 static void |
|
118 add_interface (GType object_type, |
|
119 GType iface_type, |
|
120 GInterfaceInitFunc init_func) |
|
121 { |
|
122 GInterfaceInfo iface_info = { NULL, NULL, NULL }; |
|
123 |
|
124 iface_info.interface_init = init_func; |
|
125 |
|
126 g_type_add_interface_static (object_type, iface_type, &iface_info); |
|
127 } |
|
128 |
|
129 static void |
|
130 init_base_interface (TestIfaceClass *iface) |
|
131 { |
|
132 iface->val = 21; |
|
133 } |
|
134 |
|
135 static void |
|
136 add_base_interface (GType object_type, |
|
137 GType iface_type) |
|
138 { |
|
139 add_interface (object_type, iface_type, |
|
140 (GInterfaceInitFunc)init_base_interface); |
|
141 } |
|
142 |
|
143 static gboolean |
|
144 interface_is_base (GType object_type, |
|
145 GType iface_type) |
|
146 { |
|
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; |
|
150 } |
|
151 |
|
152 static void |
|
153 init_derived_interface (TestIfaceClass *iface) |
|
154 { |
|
155 iface->val = 42; |
|
156 } |
|
157 |
|
158 static void |
|
159 add_derived_interface (GType object_type, |
|
160 GType iface_type) |
|
161 { |
|
162 add_interface (object_type, iface_type, |
|
163 (GInterfaceInitFunc)init_derived_interface); |
|
164 } |
|
165 |
|
166 static gboolean |
|
167 interface_is_derived (GType object_type, |
|
168 GType iface_type) |
|
169 { |
|
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; |
|
173 } |
|
174 |
|
175 static void |
|
176 derived_object_class_init (BaseObjectClass *class) |
|
177 { |
|
178 add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE3); |
|
179 } |
|
180 |
|
181 static DEFINE_TYPE(BaseObject, base_object, |
|
182 NULL, NULL, NULL, |
|
183 G_TYPE_OBJECT) |
|
184 static DEFINE_TYPE(DerivedObject, derived_object, |
|
185 derived_object_class_init, NULL, NULL, |
|
186 BASE_TYPE_OBJECT) |
|
187 |
|
188 int |
|
189 main (int argc, |
|
190 char *argv[]) |
|
191 { |
|
192 #ifdef __SYMBIAN32__ |
|
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); |
|
199 g_type_init (); |
|
200 |
|
201 /* Register BaseObject */ |
|
202 BASE_TYPE_OBJECT; |
|
203 |
|
204 add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE5); |
|
205 |
|
206 /* Class init BaseObject */ |
|
207 g_type_class_ref (BASE_TYPE_OBJECT); |
|
208 |
|
209 add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE1); |
|
210 |
|
211 /* Register DerivedObject */ |
|
212 DERIVED_TYPE_OBJECT; |
|
213 |
|
214 add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE2); |
|
215 add_derived_interface (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE5); |
|
216 |
|
217 /* Class init DerivedObject */ |
|
218 g_type_class_ref (DERIVED_TYPE_OBJECT); |
|
219 |
|
220 add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE4); |
|
221 |
|
222 /* Check that all the non-overridden interfaces were properly inherited |
|
223 */ |
|
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)); |
|
228 |
|
229 /* Check that all the overridden interfaces were properly overridden |
|
230 */ |
|
231 g_assert (interface_is_derived (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE5)); |
|
232 |
|
233 #ifdef __SYMBIAN32__ |
|
234 testResultXml("ifaceinherit"); |
|
235 #endif // __SYMBIAN32__ |
|
236 return 0; |
|
237 } |