glib/gwin32.c: Silence a Deprecation Warning
[glib.git] / tests / gobject / defaultiface.c
blobc44c18a79cb2fd8b42e4ca3071a9630ccaae25d0
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.
20 #undef G_LOG_DOMAIN
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
34 * interfaces.
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;
48 guint val;
51 GType test_static_iface_get_type (void);
52 #define TEST_TYPE_STATIC_IFACE (test_static_iface_get_type ())
54 static void
55 test_static_iface_default_init (TestStaticIfaceClass *iface)
57 iface->val = 42;
60 DEFINE_IFACE (TestStaticIface, test_static_iface,
61 NULL, test_static_iface_default_init)
63 static void
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);
72 /* Ref loads */
73 static_iface = g_type_default_interface_ref (TEST_TYPE_STATIC_IFACE);
74 g_assert (static_iface && static_iface->val == 42);
76 /* Peek then works */
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;
97 guint val;
100 static GType test_dynamic_iface_type;
101 static gboolean dynamic_iface_init = FALSE;
103 #define TEST_TYPE_DYNAMIC_IFACE (test_dynamic_iface_type)
105 static void
106 test_dynamic_iface_default_init (TestStaticIfaceClass *iface)
108 dynamic_iface_init = TRUE;
109 iface->val = 42;
112 static void
113 test_dynamic_iface_default_finalize (TestStaticIfaceClass *iface)
115 dynamic_iface_init = FALSE;
118 static void
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);
134 static void
135 module_register (GTypeModule *module)
137 test_dynamic_iface_register (module);
140 static void
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);
151 /* Ref loads */
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 #if 0
163 g_assert (!dynamic_iface_init);
164 #endif
166 /* Peek returns NULL */
167 dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
168 #if 0
169 g_assert (dynamic_iface == NULL);
170 #endif
172 /* Ref reloads */
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);
179 #if 0
180 g_assert (!dynamic_iface_init);
181 #endif
185 main (int argc,
186 char *argv[])
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 ();
195 return 0;