5d2686accc5908568041f7155249d2cf6c0c5fe5
[platform/upstream/glib.git] / tests / gobject / ifaceinit.c
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  *
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 "TestIfaceInit"
22 #include        <glib-object.h>
23
24 #include "testcommon.h"
25
26 /* What this test tests is the ability to add interfaces dynamically; in
27  * particular adding interfaces to a class while that class is being
28  * initialized.
29  *
30  * The test defines 5 interfaces:
31  * 
32  * - TestIface1 is added before the class is initialized
33  * - TestIface2 is added in base_object_base_init()
34  * - TestIface3 is added in test_iface1_base_init()
35  * - TestIface4 is added in test_object_class_init()
36  * - TestIface5 is added in test_object_test_iface1_init()
37  * - TestIface6 is added after the class is initialized
38  */
39
40 /* All 6 interfaces actually share the same class structure, though
41  * we use separate typedefs
42  */
43 typedef struct _TestIfaceClass TestIfaceClass;
44
45 struct _TestIfaceClass
46 {
47   GTypeInterface base_iface;
48   guint val;
49   guint base_val;
50   guint default_val;
51 };
52
53 #define TEST_TYPE_IFACE1           (test_iface1_get_type ())
54 #define TEST_IFACE1_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE1, TestIface1Class))
55 typedef struct _TestIface1      TestIface1;
56 typedef struct _TestIfaceClass  TestIface1Class;
57
58 static void test_iface1_base_init    (TestIface1Class *iface);
59 static void test_iface1_default_init (TestIface1Class *iface, gpointer class_data);
60
61 static DEFINE_IFACE(TestIface1, test_iface1, test_iface1_base_init, test_iface1_default_init)
62
63 #define TEST_TYPE_IFACE2           (test_iface2_get_type ())
64 #define TEST_IFACE2_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE2, TestIface2Class))
65 typedef struct _TestIface2      TestIface2;
66 typedef struct _TestIfaceClass  TestIface2Class;
67
68 static void test_iface2_base_init (TestIface2Class *iface);
69
70 static DEFINE_IFACE(TestIface2, test_iface2, test_iface2_base_init, NULL)
71
72 #define TEST_TYPE_IFACE3           (test_iface3_get_type ())
73 #define TEST_IFACE3_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE3, TestIface3Class))
74 typedef struct _TestIface3      TestIface3;
75 typedef struct _TestIfaceClass  TestIface3Class;
76
77 static void  test_iface3_base_init (TestIface3Class *iface);
78
79 static DEFINE_IFACE(TestIface3, test_iface3, test_iface3_base_init, NULL)
80
81 #define TEST_TYPE_IFACE4           (test_iface4_get_type ())
82 #define TEST_IFACE4_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE4, TestIface4Class))
83 typedef struct _TestIface4      TestIface4;
84 typedef struct _TestIfaceClass  TestIface4Class;
85
86 static void  test_iface4_base_init (TestIface4Class *iface);
87
88 static DEFINE_IFACE(TestIface4, test_iface4, test_iface4_base_init, NULL)
89
90 #define TEST_TYPE_IFACE5           (test_iface5_get_type ())
91 #define TEST_IFACE5_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE5, TestIface5Class))
92 typedef struct _TestIface5      TestIface5;
93 typedef struct _TestIfaceClass  TestIface5Class;
94
95 static void  test_iface5_base_init (TestIface5Class *iface);
96
97 static DEFINE_IFACE(TestIface5, test_iface5, test_iface5_base_init, NULL)
98
99 #define TEST_TYPE_IFACE6           (test_iface6_get_type ())
100 #define TEST_IFACE6_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE6, TestIface6Class))
101 typedef struct _TestIface6      TestIface6;
102 typedef struct _TestIfaceClass  TestIface6Class;
103
104 static void  test_iface6_base_init (TestIface6Class *iface);
105
106 static DEFINE_IFACE(TestIface6, test_iface6, test_iface6_base_init, NULL)
107
108 /*
109  * BaseObject, a parent class for TestObject
110  */
111 #define BASE_TYPE_OBJECT          (base_object_get_type ())
112 typedef struct _BaseObject        BaseObject;
113 typedef struct _BaseObjectClass   BaseObjectClass;
114
115 struct _BaseObject
116 {
117   GObject parent_instance;
118 };
119 struct _BaseObjectClass
120 {
121   GObjectClass parent_class;
122 };
123
124 /*
125  * TestObject, a parent class for TestObject
126  */
127 #define TEST_TYPE_OBJECT          (test_object_get_type ())
128 typedef struct _TestObject        TestObject;
129 typedef struct _TestObjectClass   TestObjectClass;
130
131 struct _TestObject
132 {
133   BaseObject parent_instance;
134 };
135 struct _TestObjectClass
136 {
137   BaseObjectClass parent_class;
138 };
139
140 #define TEST_CALLED_ONCE() G_STMT_START { \
141   static gboolean called = 0;           \
142   g_assert (!called);                   \
143   called = TRUE;                        \
144 } G_STMT_END
145
146 #define CHECK_IFACE_TWICE(iface) G_STMT_START {                                 \
147   static guint n_calls = 0;                                                     \
148   n_calls++;                                                                    \
149   g_assert (n_calls <= 2);                                                      \
150   g_assert (G_TYPE_IS_INTERFACE (((GTypeInterface*) iface)->g_type));           \
151   if (n_calls == 1)                                                             \
152     g_assert (((GTypeInterface*) iface)->g_instance_type == 0);                 \
153   else                                                                          \
154     g_assert (G_TYPE_IS_OBJECT (((GTypeInterface*) iface)->g_instance_type));   \
155 } G_STMT_END
156
157 #define ADD_IFACE(n)  G_STMT_START {                            \
158   static GInterfaceInfo iface_info = {                          \
159     (GInterfaceInitFunc)test_object_test_iface##n##_init,       \
160     NULL, NULL };                                               \
161                                                                 \
162   g_type_add_interface_static (TEST_TYPE_OBJECT,                \
163                                test_iface##n##_get_type (),     \
164                                &iface_info);                    \
165                                                                 \
166 } G_STMT_END
167
168 static gboolean base1, base2, base3, base4, base5, base6;
169 static gboolean iface1, iface2, iface3, iface4, iface5, iface6;
170
171 static void test_object_test_iface1_init (TestIface1Class *iface);
172 static void test_object_test_iface2_init (TestIface1Class *iface);
173 static void test_object_test_iface3_init (TestIface3Class *iface);
174 static void test_object_test_iface4_init (TestIface4Class *iface);
175 static void test_object_test_iface5_init (TestIface5Class *iface);
176 static void test_object_test_iface6_init (TestIface6Class *iface);
177
178 static GType test_object_get_type (void);
179
180 static void
181 test_object_test_iface1_init (TestIface1Class *iface)
182 {
183   TEST_CALLED_ONCE();
184
185   g_assert (iface->default_val == 0x111111);
186
187   iface->val = 0x10001;
188
189   ADD_IFACE(5);
190
191   iface1 = TRUE;
192 }
193
194 static void
195 test_object_test_iface2_init (TestIface2Class *iface)
196 {
197   TEST_CALLED_ONCE();
198   
199   iface->val = 0x20002;
200   
201   iface2 = TRUE;
202 }
203
204 static void
205 test_object_test_iface3_init (TestIface3Class *iface)
206 {
207   TEST_CALLED_ONCE();
208   
209   iface->val = 0x30003;
210   
211   iface3 = TRUE;
212 }
213
214 static void
215 test_object_test_iface4_init (TestIface4Class *iface)
216 {
217   TEST_CALLED_ONCE();
218
219   iface->val = 0x40004;
220   
221   iface4 = TRUE;
222 }
223
224 static void
225 test_object_test_iface5_init (TestIface5Class *iface)
226 {
227   TEST_CALLED_ONCE();
228
229   iface->val = 0x50005;
230   
231   iface5 = TRUE;
232 }
233
234 static void
235 test_object_test_iface6_init (TestIface6Class *iface)
236 {
237   TEST_CALLED_ONCE();
238
239   iface->val = 0x60006;
240   
241   iface6 = TRUE;
242 }
243
244 static void
245 test_iface1_default_init (TestIface1Class *iface,
246                           gpointer         class_data)
247 {
248   TEST_CALLED_ONCE();
249   g_assert (iface->base_iface.g_type == TEST_TYPE_IFACE1);
250   g_assert (iface->base_iface.g_instance_type == 0);
251   g_assert (iface->base_val == 0x110011);
252   g_assert (iface->val == 0);
253   g_assert (iface->default_val == 0);
254   iface->default_val = 0x111111;
255 }
256
257 static void
258 test_iface1_base_init (TestIface1Class *iface)
259 {
260   static guint n_calls = 0;
261   n_calls++;
262   g_assert (n_calls <= 2);
263
264   if (n_calls == 1)
265     {
266       iface->base_val = 0x110011;
267       g_assert (iface->default_val == 0);
268     }
269   else
270     {
271       g_assert (iface->base_val == 0x110011);
272       g_assert (iface->default_val == 0x111111);
273     }
274
275   if (n_calls == 1)
276     ADD_IFACE(3);
277   
278   base1 = TRUE;
279 }
280
281 static void
282 test_iface2_base_init (TestIface2Class *iface)
283 {
284   CHECK_IFACE_TWICE (iface);
285
286   iface->base_val = 0x220022;
287   
288   base2 = TRUE;
289 }
290
291 static void
292 test_iface3_base_init (TestIface3Class *iface)
293 {
294   CHECK_IFACE_TWICE (iface);
295
296   iface->base_val = 0x330033;
297   
298   base3 = TRUE;
299 }
300
301 static void
302 test_iface4_base_init (TestIface4Class *iface)
303 {
304   CHECK_IFACE_TWICE (iface);
305
306   iface->base_val = 0x440044;
307
308   base4 = TRUE;
309 }
310
311 static void
312 test_iface5_base_init (TestIface5Class *iface)
313 {
314   CHECK_IFACE_TWICE (iface);
315
316   iface->base_val = 0x550055;
317
318   base5 = TRUE;
319 }
320
321 static void
322 test_iface6_base_init (TestIface6Class *iface)
323 {
324   CHECK_IFACE_TWICE (iface);
325
326   iface->base_val = 0x660066;
327   
328   base6 = TRUE;
329 }
330
331 static void
332 base_object_base_init (BaseObjectClass *class)
333 {
334   static int n_called = 0;
335   n_called++;
336   
337   /* The second time this is called is for TestObject */
338   if (n_called == 2)
339     {
340       ADD_IFACE(2);
341       
342       /* No interface base init functions should have been called yet
343        */
344       g_assert (!base1 && !base2 && !base3 && !base4 && !base5 && !base6);
345       g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
346     }
347 }
348
349 static void
350 test_object_class_init (TestObjectClass *class)
351 {
352   ADD_IFACE(4);
353
354   /* At this point, the base init functions for all interfaces that have
355    * been added should be called, but no interface init functions.
356    */
357   g_assert (base1 && base2 && base3 && base4 && !base5 && !base6);
358   g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
359 }
360
361 static DEFINE_TYPE(BaseObject, base_object,
362                    NULL, base_object_base_init, NULL,
363                    G_TYPE_OBJECT)
364 static DEFINE_TYPE(TestObject, test_object,
365                    test_object_class_init, NULL, NULL,
366                    BASE_TYPE_OBJECT)
367
368 int
369 main (int   argc,
370       char *argv[])
371 {
372   TestObject *object;
373   TestObjectClass *object_class;
374   TestIfaceClass *iface;
375         
376   g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
377                           G_LOG_LEVEL_WARNING |
378                           G_LOG_LEVEL_CRITICAL);
379   g_type_init ();
380
381   /* We force the interfaces to be registered in a different order
382    * than we add them, so our logic doesn't always deal with interfaces
383    * added at the end.
384    */
385   (void)TEST_TYPE_IFACE4;
386   (void)TEST_TYPE_IFACE2;
387   (void)TEST_TYPE_IFACE6;
388   (void)TEST_TYPE_IFACE5;
389   (void)TEST_TYPE_IFACE3;
390   (void)TEST_TYPE_IFACE1;
391
392   ADD_IFACE(1);
393
394   object_class = g_type_class_ref (TEST_TYPE_OBJECT);
395
396   ADD_IFACE(6);
397
398   /* All base and interface init functions should have been called
399    */
400   g_assert (base1 && base2 && base3 && base4 && base5 && base6);
401   g_assert (iface1 && iface2 && iface3 && iface4 && iface5 && iface6);
402   
403   object = g_object_new (TEST_TYPE_OBJECT, NULL);
404
405   iface = TEST_IFACE1_GET_CLASS (object);
406   g_assert (iface && iface->val == 0x10001 && iface->base_val == 0x110011);
407   iface = TEST_IFACE3_GET_CLASS (object);
408   g_assert (iface && iface->val == 0x30003 && iface->base_val == 0x330033);
409   iface = TEST_IFACE4_GET_CLASS (object);
410   g_assert (iface && iface->val == 0x40004 && iface->base_val == 0x440044);
411   iface = TEST_IFACE5_GET_CLASS (object);
412   g_assert (iface && iface->val == 0x50005 && iface->base_val == 0x550055);
413   iface = TEST_IFACE6_GET_CLASS (object);
414   g_assert (iface && iface->val == 0x60006 && iface->base_val == 0x660066);
415
416   return 0;
417 }