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.
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, see <http://www.gnu.org/licenses/>.
19 #define G_LOG_DOMAIN "TestIfaceInit"
21 #undef G_DISABLE_ASSERT
22 #undef G_DISABLE_CHECKS
23 #undef G_DISABLE_CAST_CHECKS
25 #include <glib-object.h>
27 #include "testcommon.h"
29 /* What this test tests is the ability to add interfaces dynamically; in
30 * particular adding interfaces to a class while that class is being
33 * The test defines 5 interfaces:
35 * - TestIface1 is added before the class is initialized
36 * - TestIface2 is added in base_object_base_init()
37 * - TestIface3 is added in test_iface1_base_init()
38 * - TestIface4 is added in test_object_class_init()
39 * - TestIface5 is added in test_object_test_iface1_init()
40 * - TestIface6 is added after the class is initialized
43 /* All 6 interfaces actually share the same class structure, though
44 * we use separate typedefs
46 typedef struct _TestIfaceClass TestIfaceClass
;
48 struct _TestIfaceClass
50 GTypeInterface base_iface
;
56 #define TEST_TYPE_IFACE1 (test_iface1_get_type ())
57 #define TEST_IFACE1_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE1, TestIface1Class))
58 typedef struct _TestIface1 TestIface1
;
59 typedef struct _TestIfaceClass TestIface1Class
;
61 static void test_iface1_base_init (TestIface1Class
*iface
);
62 static void test_iface1_default_init (TestIface1Class
*iface
, gpointer class_data
);
64 static DEFINE_IFACE(TestIface1
, test_iface1
, test_iface1_base_init
, test_iface1_default_init
)
66 #define TEST_TYPE_IFACE2 (test_iface2_get_type ())
67 #define TEST_IFACE2_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE2, TestIface2Class))
68 typedef struct _TestIface2 TestIface2
;
69 typedef struct _TestIfaceClass TestIface2Class
;
71 static void test_iface2_base_init (TestIface2Class
*iface
);
73 static DEFINE_IFACE(TestIface2
, test_iface2
, test_iface2_base_init
, NULL
)
75 #define TEST_TYPE_IFACE3 (test_iface3_get_type ())
76 #define TEST_IFACE3_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE3, TestIface3Class))
77 typedef struct _TestIface3 TestIface3
;
78 typedef struct _TestIfaceClass TestIface3Class
;
80 static void test_iface3_base_init (TestIface3Class
*iface
);
82 static DEFINE_IFACE(TestIface3
, test_iface3
, test_iface3_base_init
, NULL
)
84 #define TEST_TYPE_IFACE4 (test_iface4_get_type ())
85 #define TEST_IFACE4_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE4, TestIface4Class))
86 typedef struct _TestIface4 TestIface4
;
87 typedef struct _TestIfaceClass TestIface4Class
;
89 static void test_iface4_base_init (TestIface4Class
*iface
);
91 static DEFINE_IFACE(TestIface4
, test_iface4
, test_iface4_base_init
, NULL
)
93 #define TEST_TYPE_IFACE5 (test_iface5_get_type ())
94 #define TEST_IFACE5_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE5, TestIface5Class))
95 typedef struct _TestIface5 TestIface5
;
96 typedef struct _TestIfaceClass TestIface5Class
;
98 static void test_iface5_base_init (TestIface5Class
*iface
);
100 static DEFINE_IFACE(TestIface5
, test_iface5
, test_iface5_base_init
, NULL
)
102 #define TEST_TYPE_IFACE6 (test_iface6_get_type ())
103 #define TEST_IFACE6_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE6, TestIface6Class))
104 typedef struct _TestIface6 TestIface6
;
105 typedef struct _TestIfaceClass TestIface6Class
;
107 static void test_iface6_base_init (TestIface6Class
*iface
);
109 static DEFINE_IFACE(TestIface6
, test_iface6
, test_iface6_base_init
, NULL
)
112 * BaseObject, a parent class for TestObject
114 #define BASE_TYPE_OBJECT (base_object_get_type ())
115 typedef struct _BaseObject BaseObject
;
116 typedef struct _BaseObjectClass BaseObjectClass
;
120 GObject parent_instance
;
122 struct _BaseObjectClass
124 GObjectClass parent_class
;
128 * TestObject, a parent class for TestObject
130 #define TEST_TYPE_OBJECT (test_object_get_type ())
131 typedef struct _TestObject TestObject
;
132 typedef struct _TestObjectClass TestObjectClass
;
136 BaseObject parent_instance
;
138 struct _TestObjectClass
140 BaseObjectClass parent_class
;
143 #define TEST_CALLED_ONCE() G_STMT_START { \
144 static gboolean called = 0; \
145 g_assert (!called); \
149 #define CHECK_IFACE_TWICE(iface) G_STMT_START { \
150 static guint n_calls = 0; \
152 g_assert (n_calls <= 2); \
153 g_assert (G_TYPE_IS_INTERFACE (((GTypeInterface*) iface)->g_type)); \
155 g_assert (((GTypeInterface*) iface)->g_instance_type == 0); \
157 g_assert (G_TYPE_IS_OBJECT (((GTypeInterface*) iface)->g_instance_type)); \
160 #define ADD_IFACE(n) G_STMT_START { \
161 GInterfaceInfo iface_info = { \
162 (GInterfaceInitFunc)test_object_test_iface##n##_init, \
165 g_type_add_interface_static (TEST_TYPE_OBJECT, \
166 test_iface##n##_get_type (), \
171 static gboolean base1
, base2
, base3
, base4
, base5
, base6
;
172 static gboolean iface1
, iface2
, iface3
, iface4
, iface5
, iface6
;
174 static void test_object_test_iface1_init (TestIface1Class
*iface
);
175 static void test_object_test_iface2_init (TestIface1Class
*iface
);
176 static void test_object_test_iface3_init (TestIface3Class
*iface
);
177 static void test_object_test_iface4_init (TestIface4Class
*iface
);
178 static void test_object_test_iface5_init (TestIface5Class
*iface
);
179 static void test_object_test_iface6_init (TestIface6Class
*iface
);
181 static GType
test_object_get_type (void);
184 test_object_test_iface1_init (TestIface1Class
*iface
)
188 g_assert (iface
->default_val
== 0x111111);
190 iface
->val
= 0x10001;
198 test_object_test_iface2_init (TestIface2Class
*iface
)
202 iface
->val
= 0x20002;
208 test_object_test_iface3_init (TestIface3Class
*iface
)
212 iface
->val
= 0x30003;
218 test_object_test_iface4_init (TestIface4Class
*iface
)
222 iface
->val
= 0x40004;
228 test_object_test_iface5_init (TestIface5Class
*iface
)
232 iface
->val
= 0x50005;
238 test_object_test_iface6_init (TestIface6Class
*iface
)
242 iface
->val
= 0x60006;
248 test_iface1_default_init (TestIface1Class
*iface
,
252 g_assert (iface
->base_iface
.g_type
== TEST_TYPE_IFACE1
);
253 g_assert (iface
->base_iface
.g_instance_type
== 0);
254 g_assert (iface
->base_val
== 0x110011);
255 g_assert (iface
->val
== 0);
256 g_assert (iface
->default_val
== 0);
257 iface
->default_val
= 0x111111;
261 test_iface1_base_init (TestIface1Class
*iface
)
263 static guint n_calls
= 0;
265 g_assert (n_calls
<= 2);
269 iface
->base_val
= 0x110011;
270 g_assert (iface
->default_val
== 0);
274 g_assert (iface
->base_val
== 0x110011);
275 g_assert (iface
->default_val
== 0x111111);
285 test_iface2_base_init (TestIface2Class
*iface
)
287 CHECK_IFACE_TWICE (iface
);
289 iface
->base_val
= 0x220022;
295 test_iface3_base_init (TestIface3Class
*iface
)
297 CHECK_IFACE_TWICE (iface
);
299 iface
->base_val
= 0x330033;
305 test_iface4_base_init (TestIface4Class
*iface
)
307 CHECK_IFACE_TWICE (iface
);
309 iface
->base_val
= 0x440044;
315 test_iface5_base_init (TestIface5Class
*iface
)
317 CHECK_IFACE_TWICE (iface
);
319 iface
->base_val
= 0x550055;
325 test_iface6_base_init (TestIface6Class
*iface
)
327 CHECK_IFACE_TWICE (iface
);
329 iface
->base_val
= 0x660066;
335 base_object_base_init (BaseObjectClass
*class)
337 static int n_called
= 0;
340 /* The second time this is called is for TestObject */
345 /* No interface base init functions should have been called yet
347 g_assert (!base1
&& !base2
&& !base3
&& !base4
&& !base5
&& !base6
);
348 g_assert (!iface1
&& !iface2
&& !iface3
&& !iface4
&& !iface5
&& !iface6
);
353 test_object_class_init (TestObjectClass
*class)
357 /* At this point, the base init functions for all interfaces that have
358 * been added should be called, but no interface init functions.
360 g_assert (base1
&& base2
&& base3
&& base4
&& !base5
&& !base6
);
361 g_assert (!iface1
&& !iface2
&& !iface3
&& !iface4
&& !iface5
&& !iface6
);
364 static DEFINE_TYPE(BaseObject
, base_object
,
365 NULL
, base_object_base_init
, NULL
,
367 static DEFINE_TYPE(TestObject
, test_object
,
368 test_object_class_init
, NULL
, NULL
,
376 TestObjectClass
*object_class
;
377 TestIfaceClass
*iface
;
379 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK
) |
380 G_LOG_LEVEL_WARNING
|
381 G_LOG_LEVEL_CRITICAL
);
383 /* We force the interfaces to be registered in a different order
384 * than we add them, so our logic doesn't always deal with interfaces
387 (void)TEST_TYPE_IFACE4
;
388 (void)TEST_TYPE_IFACE2
;
389 (void)TEST_TYPE_IFACE6
;
390 (void)TEST_TYPE_IFACE5
;
391 (void)TEST_TYPE_IFACE3
;
392 (void)TEST_TYPE_IFACE1
;
396 object_class
= g_type_class_ref (TEST_TYPE_OBJECT
);
400 /* All base and interface init functions should have been called
402 g_assert (base1
&& base2
&& base3
&& base4
&& base5
&& base6
);
403 g_assert (iface1
&& iface2
&& iface3
&& iface4
&& iface5
&& iface6
);
405 object
= g_object_new (TEST_TYPE_OBJECT
, NULL
);
407 iface
= TEST_IFACE1_GET_CLASS (object
);
408 g_assert (iface
&& iface
->val
== 0x10001 && iface
->base_val
== 0x110011);
409 iface
= TEST_IFACE3_GET_CLASS (object
);
410 g_assert (iface
&& iface
->val
== 0x30003 && iface
->base_val
== 0x330033);
411 iface
= TEST_IFACE4_GET_CLASS (object
);
412 g_assert (iface
&& iface
->val
== 0x40004 && iface
->base_val
== 0x440044);
413 iface
= TEST_IFACE5_GET_CLASS (object
);
414 g_assert (iface
&& iface
->val
== 0x50005 && iface
->base_val
== 0x550055);
415 iface
= TEST_IFACE6_GET_CLASS (object
);
416 g_assert (iface
&& iface
->val
== 0x60006 && iface
->base_val
== 0x660066);
418 g_type_class_unref (object_class
);