First public contribution.
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2001, 2003 Red Hat, Inc.
3 * Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
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 #include "mrt2_glib2_test.h"
34 /* What this test tests is the ability to add interfaces dynamically; in
35 * particular adding interfaces to a class while that class is being
38 * The test defines 5 interfaces:
40 * - TestIface1 is added before the class is initialized
41 * - TestIface2 is added in base_object_base_init()
42 * - TestIface3 is added in test_iface1_base_init()
43 * - TestIface4 is added in test_object_class_init()
44 * - TestIface5 is added in test_object_test_iface1_init()
45 * - TestIface6 is added after the class is initialized
48 /* All 6 interfaces actually share the same class structure, though
49 * we use separate typedefs
51 typedef struct _TestIfaceClass TestIfaceClass;
53 struct _TestIfaceClass
55 GTypeInterface base_iface;
61 #define TEST_TYPE_IFACE1 (test_iface1_get_type ())
62 #define TEST_IFACE1_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE1, TestIface1Class))
63 typedef struct _TestIface1 TestIface1;
64 typedef struct _TestIfaceClass TestIface1Class;
66 static void test_iface1_base_init (TestIface1Class *iface);
67 static void test_iface1_default_init (TestIface1Class *iface, gpointer class_data);
69 static DEFINE_IFACE(TestIface1, test_iface1, test_iface1_base_init, test_iface1_default_init)
71 #define TEST_TYPE_IFACE2 (test_iface2_get_type ())
72 #define TEST_IFACE2_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE2, TestIface2Class))
73 typedef struct _TestIface2 TestIface2;
74 typedef struct _TestIfaceClass TestIface2Class;
76 static void test_iface2_base_init (TestIface2Class *iface);
78 static DEFINE_IFACE(TestIface2, test_iface2, test_iface2_base_init, NULL)
80 #define TEST_TYPE_IFACE3 (test_iface3_get_type ())
81 #define TEST_IFACE3_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE3, TestIface3Class))
82 typedef struct _TestIface3 TestIface3;
83 typedef struct _TestIfaceClass TestIface3Class;
85 static void test_iface3_base_init (TestIface3Class *iface);
87 static DEFINE_IFACE(TestIface3, test_iface3, test_iface3_base_init, NULL)
89 #define TEST_TYPE_IFACE4 (test_iface4_get_type ())
90 #define TEST_IFACE4_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE4, TestIface4Class))
91 typedef struct _TestIface4 TestIface4;
92 typedef struct _TestIfaceClass TestIface4Class;
94 static void test_iface4_base_init (TestIface4Class *iface);
96 static DEFINE_IFACE(TestIface4, test_iface4, test_iface4_base_init, NULL)
98 #define TEST_TYPE_IFACE5 (test_iface5_get_type ())
99 #define TEST_IFACE5_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE5, TestIface5Class))
100 typedef struct _TestIface5 TestIface5;
101 typedef struct _TestIfaceClass TestIface5Class;
103 static void test_iface5_base_init (TestIface5Class *iface);
105 static DEFINE_IFACE(TestIface5, test_iface5, test_iface5_base_init, NULL)
107 #define TEST_TYPE_IFACE6 (test_iface6_get_type ())
108 #define TEST_IFACE6_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE6, TestIface6Class))
109 typedef struct _TestIface6 TestIface6;
110 typedef struct _TestIfaceClass TestIface6Class;
112 static void test_iface6_base_init (TestIface6Class *iface);
114 static DEFINE_IFACE(TestIface6, test_iface6, test_iface6_base_init, NULL)
117 * BaseObject, a parent class for TestObject
119 #define BASE_TYPE_OBJECT (base_object_get_type ())
120 typedef struct _BaseObject BaseObject;
121 typedef struct _BaseObjectClass BaseObjectClass;
125 GObject parent_instance;
127 struct _BaseObjectClass
129 GObjectClass parent_class;
133 * TestObject, a parent class for TestObject
135 #define TEST_TYPE_OBJECT (test_object_get_type ())
136 typedef struct _TestObject TestObject;
137 typedef struct _TestObjectClass TestObjectClass;
141 BaseObject parent_instance;
143 struct _TestObjectClass
145 BaseObjectClass parent_class;
148 #define TEST_CALLED_ONCE() G_STMT_START { \
149 static gboolean called = 0; \
150 g_assert (!called); \
154 #define CHECK_IFACE_TWICE(iface) G_STMT_START { \
155 static guint n_calls = 0; \
157 g_assert (n_calls <= 2); \
158 g_assert (G_TYPE_IS_INTERFACE (((GTypeInterface*) iface)->g_type)); \
160 g_assert (((GTypeInterface*) iface)->g_instance_type == 0); \
162 g_assert (G_TYPE_IS_OBJECT (((GTypeInterface*) iface)->g_instance_type)); \
165 #define ADD_IFACE(n) G_STMT_START { \
166 static GInterfaceInfo iface_info = { \
167 (GInterfaceInitFunc)test_object_test_iface##n##_init, \
170 g_type_add_interface_static (TEST_TYPE_OBJECT, \
171 test_iface##n##_get_type (), \
176 static gboolean base1, base2, base3, base4, base5, base6;
177 static gboolean iface1, iface2, iface3, iface4, iface5, iface6;
179 static void test_object_test_iface1_init (TestIface1Class *iface);
180 static void test_object_test_iface2_init (TestIface1Class *iface);
181 static void test_object_test_iface3_init (TestIface3Class *iface);
182 static void test_object_test_iface4_init (TestIface4Class *iface);
183 static void test_object_test_iface5_init (TestIface5Class *iface);
184 static void test_object_test_iface6_init (TestIface6Class *iface);
186 static GType test_object_get_type (void);
189 test_object_test_iface1_init (TestIface1Class *iface)
193 g_assert (iface->default_val == 0x111111);
195 iface->val = 0x10001;
203 test_object_test_iface2_init (TestIface2Class *iface)
207 iface->val = 0x20002;
213 test_object_test_iface3_init (TestIface3Class *iface)
217 iface->val = 0x30003;
223 test_object_test_iface4_init (TestIface4Class *iface)
227 iface->val = 0x40004;
233 test_object_test_iface5_init (TestIface5Class *iface)
237 iface->val = 0x50005;
243 test_object_test_iface6_init (TestIface6Class *iface)
247 iface->val = 0x60006;
253 test_iface1_default_init (TestIface1Class *iface,
257 g_assert (iface->base_iface.g_type == TEST_TYPE_IFACE1);
258 g_assert (iface->base_iface.g_instance_type == 0);
259 g_assert (iface->base_val == 0x110011);
260 g_assert (iface->val == 0);
261 g_assert (iface->default_val == 0);
262 iface->default_val = 0x111111;
266 test_iface1_base_init (TestIface1Class *iface)
268 static guint n_calls = 0;
270 g_assert (n_calls <= 2);
274 iface->base_val = 0x110011;
275 g_assert (iface->default_val == 0);
279 g_assert (iface->base_val == 0x110011);
280 g_assert (iface->default_val == 0x111111);
290 test_iface2_base_init (TestIface2Class *iface)
292 CHECK_IFACE_TWICE (iface);
294 iface->base_val = 0x220022;
300 test_iface3_base_init (TestIface3Class *iface)
302 CHECK_IFACE_TWICE (iface);
304 iface->base_val = 0x330033;
310 test_iface4_base_init (TestIface4Class *iface)
312 CHECK_IFACE_TWICE (iface);
314 iface->base_val = 0x440044;
320 test_iface5_base_init (TestIface5Class *iface)
322 CHECK_IFACE_TWICE (iface);
324 iface->base_val = 0x550055;
330 test_iface6_base_init (TestIface6Class *iface)
332 CHECK_IFACE_TWICE (iface);
334 iface->base_val = 0x660066;
340 base_object_base_init (BaseObjectClass *class)
342 static int n_called = 0;
345 /* The second time this is called is for TestObject */
350 /* No interface base init functions should have been called yet
352 g_assert (!base1 && !base2 && !base3 && !base4 && !base5 && !base6);
353 g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
358 test_object_class_init (TestObjectClass *class)
362 /* At this point, the base init functions for all interfaces that have
363 * been added should be called, but no interface init functions.
365 g_assert (base1 && base2 && base3 && base4 && !base5 && !base6);
366 g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
369 static DEFINE_TYPE(BaseObject, base_object,
370 NULL, base_object_base_init, NULL,
372 static DEFINE_TYPE(TestObject, test_object,
373 test_object_class_init, NULL, NULL,
383 TestObjectClass *object_class;
384 TestIfaceClass *iface;
386 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);
387 g_set_print_handler(mrtPrintHandler);
389 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
390 G_LOG_LEVEL_WARNING |
391 G_LOG_LEVEL_CRITICAL);
394 /* We force the interfaces to be registered in a different order
395 * than we add them, so our logic doesn't always deal with interfaces
398 (void)TEST_TYPE_IFACE4;
399 (void)TEST_TYPE_IFACE2;
400 (void)TEST_TYPE_IFACE6;
401 (void)TEST_TYPE_IFACE5;
402 (void)TEST_TYPE_IFACE3;
403 (void)TEST_TYPE_IFACE1;
407 object_class = g_type_class_ref (TEST_TYPE_OBJECT);
411 /* All base and interface init functions should have been called
413 g_assert (base1 && base2 && base3 && base4 && base5 && base6);
414 g_assert (iface1 && iface2 && iface3 && iface4 && iface5 && iface6);
416 object = g_object_new (TEST_TYPE_OBJECT, NULL);
418 iface = TEST_IFACE1_GET_CLASS (object);
419 g_assert (iface && iface->val == 0x10001 && iface->base_val == 0x110011);
420 iface = TEST_IFACE3_GET_CLASS (object);
421 g_assert (iface && iface->val == 0x30003 && iface->base_val == 0x330033);
422 iface = TEST_IFACE4_GET_CLASS (object);
423 g_assert (iface && iface->val == 0x40004 && iface->base_val == 0x440044);
424 iface = TEST_IFACE5_GET_CLASS (object);
425 g_assert (iface && iface->val == 0x50005 && iface->base_val == 0x550055);
426 iface = TEST_IFACE6_GET_CLASS (object);
427 g_assert (iface && iface->val == 0x60006 && iface->base_val == 0x660066);
429 testResultXml("ifaceinit");