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 "TestDefaultIface"
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"
30 #include "testmodule.h"
32 /* This test tests getting the default vtable for an interface
33 * and the initialization and finalization of such default
36 * We test this both for static and for dynamic interfaces.
39 /**********************************************************************
40 * Static interface tests
41 **********************************************************************/
43 typedef struct _TestStaticIfaceClass TestStaticIfaceClass;
45 struct _TestStaticIfaceClass
47 GTypeInterface base_iface;
51 GType test_static_iface_get_type (void);
52 #define TEST_TYPE_STATIC_IFACE (test_static_iface_get_type ())
55 test_static_iface_default_init (TestStaticIfaceClass *iface)
60 DEFINE_IFACE (TestStaticIface, test_static_iface,
61 NULL, test_static_iface_default_init)
64 test_static_iface (void)
66 TestStaticIfaceClass *static_iface;
68 /* Not loaded until we call ref for the first time */
69 static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
70 g_assert (static_iface == NULL);
73 static_iface = g_type_default_interface_ref (TEST_TYPE_STATIC_IFACE);
74 g_assert (static_iface && static_iface->val == 42);
77 static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
78 g_assert (static_iface && static_iface->val == 42);
80 /* Unref does nothing */
81 g_type_default_interface_unref (static_iface);
83 /* And peek still works */
84 static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
85 g_assert (static_iface && static_iface->val == 42);
88 /**********************************************************************
89 * Dynamic interface tests
90 **********************************************************************/
92 typedef struct _TestDynamicIfaceClass TestDynamicIfaceClass;
94 struct _TestDynamicIfaceClass
96 GTypeInterface base_iface;
100 static GType test_dynamic_iface_type;
101 static gboolean dynamic_iface_init = FALSE;
103 #define TEST_TYPE_DYNAMIC_IFACE (test_dynamic_iface_type)
106 test_dynamic_iface_default_init (TestStaticIfaceClass *iface)
108 dynamic_iface_init = TRUE;
113 test_dynamic_iface_default_finalize (TestStaticIfaceClass *iface)
115 dynamic_iface_init = FALSE;
119 test_dynamic_iface_register (GTypeModule *module)
121 const GTypeInfo iface_info =
123 sizeof (TestDynamicIfaceClass),
124 (GBaseInitFunc) NULL,
125 (GBaseFinalizeFunc) NULL,
126 (GClassInitFunc) test_dynamic_iface_default_init,
127 (GClassFinalizeFunc) test_dynamic_iface_default_finalize
130 test_dynamic_iface_type = g_type_module_register_type (module, G_TYPE_INTERFACE,
131 "TestDynamicIface", &iface_info, 0);
135 module_register (GTypeModule *module)
137 test_dynamic_iface_register (module);
141 test_dynamic_iface (void)
143 TestDynamicIfaceClass *dynamic_iface;
145 test_module_new (module_register);
147 /* Not loaded until we call ref for the first time */
148 dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
149 g_assert (dynamic_iface == NULL);
152 dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE);
153 g_assert (dynamic_iface_init);
154 g_assert (dynamic_iface && dynamic_iface->val == 42);
156 /* Peek then works */
157 dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
158 g_assert (dynamic_iface && dynamic_iface->val == 42);
160 /* Unref causes finalize */
161 g_type_default_interface_unref (dynamic_iface);
163 g_assert (!dynamic_iface_init);
166 /* Peek returns NULL */
167 dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
169 g_assert (dynamic_iface == NULL);
173 dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE);
174 g_assert (dynamic_iface_init);
175 g_assert (dynamic_iface && dynamic_iface->val == 42);
177 /* And Unref causes finalize once more*/
178 g_type_default_interface_unref (dynamic_iface);
180 g_assert (!dynamic_iface_init);
188 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
189 G_LOG_LEVEL_WARNING |
190 G_LOG_LEVEL_CRITICAL);
192 test_static_iface ();
193 test_dynamic_iface ();