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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #define G_LOG_DOMAIN "TestIfaceInit"
22 #include <glib-object.h>
24 #include "testcommon.h"
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
30 * The test defines 5 interfaces:
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
40 /* All 6 interfaces actually share the same class structure, though
41 * we use separate typedefs
43 typedef struct _TestIfaceClass TestIfaceClass;
45 struct _TestIfaceClass
47 GTypeInterface base_iface;
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;
58 static void test_iface1_base_init (TestIface1Class *iface);
59 static void test_iface1_default_init (TestIface1Class *iface, gpointer class_data);
61 static DEFINE_IFACE(TestIface1, test_iface1, test_iface1_base_init, test_iface1_default_init)
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;
68 static void test_iface2_base_init (TestIface2Class *iface);
70 static DEFINE_IFACE(TestIface2, test_iface2, test_iface2_base_init, NULL)
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;
77 static void test_iface3_base_init (TestIface3Class *iface);
79 static DEFINE_IFACE(TestIface3, test_iface3, test_iface3_base_init, NULL)
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;
86 static void test_iface4_base_init (TestIface4Class *iface);
88 static DEFINE_IFACE(TestIface4, test_iface4, test_iface4_base_init, NULL)
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;
95 static void test_iface5_base_init (TestIface5Class *iface);
97 static DEFINE_IFACE(TestIface5, test_iface5, test_iface5_base_init, NULL)
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;
104 static void test_iface6_base_init (TestIface6Class *iface);
106 static DEFINE_IFACE(TestIface6, test_iface6, test_iface6_base_init, NULL)
109 * BaseObject, a parent class for TestObject
111 #define BASE_TYPE_OBJECT (base_object_get_type ())
112 typedef struct _BaseObject BaseObject;
113 typedef struct _BaseObjectClass BaseObjectClass;
117 GObject parent_instance;
119 struct _BaseObjectClass
121 GObjectClass parent_class;
125 * TestObject, a parent class for TestObject
127 #define TEST_TYPE_OBJECT (test_object_get_type ())
128 typedef struct _TestObject TestObject;
129 typedef struct _TestObjectClass TestObjectClass;
133 BaseObject parent_instance;
135 struct _TestObjectClass
137 BaseObjectClass parent_class;
140 #define TEST_CALLED_ONCE() G_STMT_START { \
141 static gboolean called = 0; \
142 g_assert (!called); \
146 #define CHECK_IFACE_TWICE(iface) G_STMT_START { \
147 static guint n_calls = 0; \
149 g_assert (n_calls <= 2); \
150 g_assert (G_TYPE_IS_INTERFACE (((GTypeInterface*) iface)->g_type)); \
152 g_assert (((GTypeInterface*) iface)->g_instance_type == 0); \
154 g_assert (G_TYPE_IS_OBJECT (((GTypeInterface*) iface)->g_instance_type)); \
157 #define ADD_IFACE(n) G_STMT_START { \
158 static GInterfaceInfo iface_info = { \
159 (GInterfaceInitFunc)test_object_test_iface##n##_init, \
162 g_type_add_interface_static (TEST_TYPE_OBJECT, \
163 test_iface##n##_get_type (), \
168 static gboolean base1, base2, base3, base4, base5, base6;
169 static gboolean iface1, iface2, iface3, iface4, iface5, iface6;
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);
178 static GType test_object_get_type (void);
181 test_object_test_iface1_init (TestIface1Class *iface)
185 g_assert (iface->default_val == 0x111111);
187 iface->val = 0x10001;
195 test_object_test_iface2_init (TestIface2Class *iface)
199 iface->val = 0x20002;
205 test_object_test_iface3_init (TestIface3Class *iface)
209 iface->val = 0x30003;
215 test_object_test_iface4_init (TestIface4Class *iface)
219 iface->val = 0x40004;
225 test_object_test_iface5_init (TestIface5Class *iface)
229 iface->val = 0x50005;
235 test_object_test_iface6_init (TestIface6Class *iface)
239 iface->val = 0x60006;
245 test_iface1_default_init (TestIface1Class *iface,
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;
258 test_iface1_base_init (TestIface1Class *iface)
260 static guint n_calls = 0;
262 g_assert (n_calls <= 2);
266 iface->base_val = 0x110011;
267 g_assert (iface->default_val == 0);
271 g_assert (iface->base_val == 0x110011);
272 g_assert (iface->default_val == 0x111111);
282 test_iface2_base_init (TestIface2Class *iface)
284 CHECK_IFACE_TWICE (iface);
286 iface->base_val = 0x220022;
292 test_iface3_base_init (TestIface3Class *iface)
294 CHECK_IFACE_TWICE (iface);
296 iface->base_val = 0x330033;
302 test_iface4_base_init (TestIface4Class *iface)
304 CHECK_IFACE_TWICE (iface);
306 iface->base_val = 0x440044;
312 test_iface5_base_init (TestIface5Class *iface)
314 CHECK_IFACE_TWICE (iface);
316 iface->base_val = 0x550055;
322 test_iface6_base_init (TestIface6Class *iface)
324 CHECK_IFACE_TWICE (iface);
326 iface->base_val = 0x660066;
332 base_object_base_init (BaseObjectClass *class)
334 static int n_called = 0;
337 /* The second time this is called is for TestObject */
342 /* No interface base init functions should have been called yet
344 g_assert (!base1 && !base2 && !base3 && !base4 && !base5 && !base6);
345 g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
350 test_object_class_init (TestObjectClass *class)
354 /* At this point, the base init functions for all interfaces that have
355 * been added should be called, but no interface init functions.
357 g_assert (base1 && base2 && base3 && base4 && !base5 && !base6);
358 g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
361 static DEFINE_TYPE(BaseObject, base_object,
362 NULL, base_object_base_init, NULL,
364 static DEFINE_TYPE(TestObject, test_object,
365 test_object_class_init, NULL, NULL,
373 TestObjectClass *object_class;
374 TestIfaceClass *iface;
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);
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
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;
394 object_class = g_type_class_ref (TEST_TYPE_OBJECT);
398 /* All base and interface init functions should have been called
400 g_assert (base1 && base2 && base3 && base4 && base5 && base6);
401 g_assert (iface1 && iface2 && iface3 && iface4 && iface5 && iface6);
403 object = g_object_new (TEST_TYPE_OBJECT, NULL);
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);