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"
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"
31 /* What this test tests is the ability to add interfaces dynamically; in
32 * particular adding interfaces to a class while that class is being
35 * The test defines 5 interfaces:
37 * - TestIface1 is added before the class is initialized
38 * - TestIface2 is added in base_object_base_init()
39 * - TestIface3 is added in test_iface1_base_init()
40 * - TestIface4 is added in test_object_class_init()
41 * - TestIface5 is added in test_object_test_iface1_init()
42 * - TestIface6 is added after the class is initialized
45 /* All 6 interfaces actually share the same class structure, though
46 * we use separate typedefs
48 typedef struct _TestIfaceClass TestIfaceClass;
50 struct _TestIfaceClass
52 GTypeInterface base_iface;
58 #define TEST_TYPE_IFACE1 (test_iface1_get_type ())
59 #define TEST_IFACE1_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE1, TestIface1Class))
60 typedef struct _TestIface1 TestIface1;
61 typedef struct _TestIfaceClass TestIface1Class;
63 static void test_iface1_base_init (TestIface1Class *iface);
64 static void test_iface1_default_init (TestIface1Class *iface, gpointer class_data);
66 static DEFINE_IFACE(TestIface1, test_iface1, test_iface1_base_init, test_iface1_default_init)
68 #define TEST_TYPE_IFACE2 (test_iface2_get_type ())
69 #define TEST_IFACE2_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE2, TestIface2Class))
70 typedef struct _TestIface2 TestIface2;
71 typedef struct _TestIfaceClass TestIface2Class;
73 static void test_iface2_base_init (TestIface2Class *iface);
75 static DEFINE_IFACE(TestIface2, test_iface2, test_iface2_base_init, NULL)
77 #define TEST_TYPE_IFACE3 (test_iface3_get_type ())
78 #define TEST_IFACE3_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE3, TestIface3Class))
79 typedef struct _TestIface3 TestIface3;
80 typedef struct _TestIfaceClass TestIface3Class;
82 static void test_iface3_base_init (TestIface3Class *iface);
84 static DEFINE_IFACE(TestIface3, test_iface3, test_iface3_base_init, NULL)
86 #define TEST_TYPE_IFACE4 (test_iface4_get_type ())
87 #define TEST_IFACE4_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE4, TestIface4Class))
88 typedef struct _TestIface4 TestIface4;
89 typedef struct _TestIfaceClass TestIface4Class;
91 static void test_iface4_base_init (TestIface4Class *iface);
93 static DEFINE_IFACE(TestIface4, test_iface4, test_iface4_base_init, NULL)
95 #define TEST_TYPE_IFACE5 (test_iface5_get_type ())
96 #define TEST_IFACE5_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE5, TestIface5Class))
97 typedef struct _TestIface5 TestIface5;
98 typedef struct _TestIfaceClass TestIface5Class;
100 static void test_iface5_base_init (TestIface5Class *iface);
102 static DEFINE_IFACE(TestIface5, test_iface5, test_iface5_base_init, NULL)
104 #define TEST_TYPE_IFACE6 (test_iface6_get_type ())
105 #define TEST_IFACE6_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE6, TestIface6Class))
106 typedef struct _TestIface6 TestIface6;
107 typedef struct _TestIfaceClass TestIface6Class;
109 static void test_iface6_base_init (TestIface6Class *iface);
111 static DEFINE_IFACE(TestIface6, test_iface6, test_iface6_base_init, NULL)
114 * BaseObject, a parent class for TestObject
116 #define BASE_TYPE_OBJECT (base_object_get_type ())
117 typedef struct _BaseObject BaseObject;
118 typedef struct _BaseObjectClass BaseObjectClass;
122 GObject parent_instance;
124 struct _BaseObjectClass
126 GObjectClass parent_class;
130 * TestObject, a parent class for TestObject
132 #define TEST_TYPE_OBJECT (test_object_get_type ())
133 typedef struct _TestObject TestObject;
134 typedef struct _TestObjectClass TestObjectClass;
138 BaseObject parent_instance;
140 struct _TestObjectClass
142 BaseObjectClass parent_class;
145 #define TEST_CALLED_ONCE() G_STMT_START { \
146 static gboolean called = 0; \
147 g_assert (!called); \
151 #define CHECK_IFACE_TWICE(iface) G_STMT_START { \
152 static guint n_calls = 0; \
154 g_assert (n_calls <= 2); \
155 g_assert (G_TYPE_IS_INTERFACE (((GTypeInterface*) iface)->g_type)); \
157 g_assert (((GTypeInterface*) iface)->g_instance_type == 0); \
159 g_assert (G_TYPE_IS_OBJECT (((GTypeInterface*) iface)->g_instance_type)); \
162 #define ADD_IFACE(n) G_STMT_START { \
163 GInterfaceInfo iface_info = { \
164 (GInterfaceInitFunc)test_object_test_iface##n##_init, \
167 g_type_add_interface_static (TEST_TYPE_OBJECT, \
168 test_iface##n##_get_type (), \
173 static gboolean base1, base2, base3, base4, base5, base6;
174 static gboolean iface1, iface2, iface3, iface4, iface5, iface6;
176 static void test_object_test_iface1_init (TestIface1Class *iface);
177 static void test_object_test_iface2_init (TestIface1Class *iface);
178 static void test_object_test_iface3_init (TestIface3Class *iface);
179 static void test_object_test_iface4_init (TestIface4Class *iface);
180 static void test_object_test_iface5_init (TestIface5Class *iface);
181 static void test_object_test_iface6_init (TestIface6Class *iface);
183 static GType test_object_get_type (void);
186 test_object_test_iface1_init (TestIface1Class *iface)
190 g_assert (iface->default_val == 0x111111);
192 iface->val = 0x10001;
200 test_object_test_iface2_init (TestIface2Class *iface)
204 iface->val = 0x20002;
210 test_object_test_iface3_init (TestIface3Class *iface)
214 iface->val = 0x30003;
220 test_object_test_iface4_init (TestIface4Class *iface)
224 iface->val = 0x40004;
230 test_object_test_iface5_init (TestIface5Class *iface)
234 iface->val = 0x50005;
240 test_object_test_iface6_init (TestIface6Class *iface)
244 iface->val = 0x60006;
250 test_iface1_default_init (TestIface1Class *iface,
254 g_assert (iface->base_iface.g_type == TEST_TYPE_IFACE1);
255 g_assert (iface->base_iface.g_instance_type == 0);
256 g_assert (iface->base_val == 0x110011);
257 g_assert (iface->val == 0);
258 g_assert (iface->default_val == 0);
259 iface->default_val = 0x111111;
263 test_iface1_base_init (TestIface1Class *iface)
265 static guint n_calls = 0;
267 g_assert (n_calls <= 2);
271 iface->base_val = 0x110011;
272 g_assert (iface->default_val == 0);
276 g_assert (iface->base_val == 0x110011);
277 g_assert (iface->default_val == 0x111111);
287 test_iface2_base_init (TestIface2Class *iface)
289 CHECK_IFACE_TWICE (iface);
291 iface->base_val = 0x220022;
297 test_iface3_base_init (TestIface3Class *iface)
299 CHECK_IFACE_TWICE (iface);
301 iface->base_val = 0x330033;
307 test_iface4_base_init (TestIface4Class *iface)
309 CHECK_IFACE_TWICE (iface);
311 iface->base_val = 0x440044;
317 test_iface5_base_init (TestIface5Class *iface)
319 CHECK_IFACE_TWICE (iface);
321 iface->base_val = 0x550055;
327 test_iface6_base_init (TestIface6Class *iface)
329 CHECK_IFACE_TWICE (iface);
331 iface->base_val = 0x660066;
337 base_object_base_init (BaseObjectClass *class)
339 static int n_called = 0;
342 /* The second time this is called is for TestObject */
347 /* No interface base init functions should have been called yet
349 g_assert (!base1 && !base2 && !base3 && !base4 && !base5 && !base6);
350 g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
355 test_object_class_init (TestObjectClass *class)
359 /* At this point, the base init functions for all interfaces that have
360 * been added should be called, but no interface init functions.
362 g_assert (base1 && base2 && base3 && base4 && !base5 && !base6);
363 g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
366 static DEFINE_TYPE(BaseObject, base_object,
367 NULL, base_object_base_init, NULL,
369 static DEFINE_TYPE(TestObject, test_object,
370 test_object_class_init, NULL, NULL,
378 TestObjectClass *object_class;
379 TestIfaceClass *iface;
381 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
382 G_LOG_LEVEL_WARNING |
383 G_LOG_LEVEL_CRITICAL);
385 /* We force the interfaces to be registered in a different order
386 * than we add them, so our logic doesn't always deal with interfaces
389 (void)TEST_TYPE_IFACE4;
390 (void)TEST_TYPE_IFACE2;
391 (void)TEST_TYPE_IFACE6;
392 (void)TEST_TYPE_IFACE5;
393 (void)TEST_TYPE_IFACE3;
394 (void)TEST_TYPE_IFACE1;
398 object_class = g_type_class_ref (TEST_TYPE_OBJECT);
402 /* All base and interface init functions should have been called
404 g_assert (base1 && base2 && base3 && base4 && base5 && base6);
405 g_assert (iface1 && iface2 && iface3 && iface4 && iface5 && iface6);
407 object = g_object_new (TEST_TYPE_OBJECT, NULL);
409 iface = TEST_IFACE1_GET_CLASS (object);
410 g_assert (iface && iface->val == 0x10001 && iface->base_val == 0x110011);
411 iface = TEST_IFACE3_GET_CLASS (object);
412 g_assert (iface && iface->val == 0x30003 && iface->base_val == 0x330033);
413 iface = TEST_IFACE4_GET_CLASS (object);
414 g_assert (iface && iface->val == 0x40004 && iface->base_val == 0x440044);
415 iface = TEST_IFACE5_GET_CLASS (object);
416 g_assert (iface && iface->val == 0x50005 && iface->base_val == 0x550055);
417 iface = TEST_IFACE6_GET_CLASS (object);
418 g_assert (iface && iface->val == 0x60006 && iface->base_val == 0x660066);
420 g_type_class_unref (object_class);