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 #define TEST_TYPE_STATIC_IFACE (test_static_iface_get_type ())
54 test_static_iface_default_init (TestStaticIfaceClass *iface)
59 DEFINE_IFACE (TestStaticIface, test_static_iface,
60 NULL, test_static_iface_default_init)
63 test_static_iface (void)
65 TestStaticIfaceClass *static_iface;
67 /* Not loaded until we call ref for the first time */
68 static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
69 g_assert (static_iface == NULL);
72 static_iface = g_type_default_interface_ref (TEST_TYPE_STATIC_IFACE);
73 g_assert (static_iface && static_iface->val == 42);
76 static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
77 g_assert (static_iface && static_iface->val == 42);
79 /* Unref does nothing */
80 g_type_default_interface_unref (static_iface);
82 /* And peek still works */
83 static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
84 g_assert (static_iface && static_iface->val == 42);
87 /**********************************************************************
88 * Dynamic interface tests
89 **********************************************************************/
91 typedef struct _TestDynamicIfaceClass TestDynamicIfaceClass;
93 struct _TestDynamicIfaceClass
95 GTypeInterface base_iface;
99 static GType test_dynamic_iface_type;
100 static gboolean dynamic_iface_init = FALSE;
102 #define TEST_TYPE_DYNAMIC_IFACE (test_dynamic_iface_type)
105 test_dynamic_iface_default_init (TestStaticIfaceClass *iface)
107 dynamic_iface_init = TRUE;
112 test_dynamic_iface_default_finalize (TestStaticIfaceClass *iface)
114 dynamic_iface_init = FALSE;
118 test_dynamic_iface_register (GTypeModule *module)
120 static const GTypeInfo iface_info =
122 sizeof (TestDynamicIfaceClass),
123 (GBaseInitFunc) NULL,
124 (GBaseFinalizeFunc) NULL,
125 (GClassInitFunc) test_dynamic_iface_default_init,
126 (GClassFinalizeFunc) test_dynamic_iface_default_finalize
129 test_dynamic_iface_type = g_type_module_register_type (module, G_TYPE_INTERFACE,
130 "TestDynamicIface", &iface_info, 0);
134 module_register (GTypeModule *module)
136 test_dynamic_iface_register (module);
140 test_dynamic_iface (void)
143 TestDynamicIfaceClass *dynamic_iface;
145 module = 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);
162 g_assert (!dynamic_iface_init);
164 /* Peek returns NULL */
165 dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
166 g_assert (dynamic_iface == NULL);
169 dynamic_iface = g_type_default_interface_ref (TEST_TYPE_DYNAMIC_IFACE);
170 g_assert (dynamic_iface_init);
171 g_assert (dynamic_iface && dynamic_iface->val == 42);
173 /* And Unref causes finalize once more*/
174 g_type_default_interface_unref (dynamic_iface);
175 g_assert (!dynamic_iface_init);
182 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
183 G_LOG_LEVEL_WARNING |
184 G_LOG_LEVEL_CRITICAL);
187 test_static_iface ();
188 test_dynamic_iface ();