c44c18a79cb2fd8b42e4ca3071a9630ccaae25d0
[platform/upstream/glib.git] / tests / gobject / defaultiface.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2001, 2003 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #undef  G_LOG_DOMAIN
21 #define G_LOG_DOMAIN "TestDefaultIface"
22
23 #undef G_DISABLE_ASSERT
24 #undef G_DISABLE_CHECKS
25 #undef G_DISABLE_CAST_CHECKS
26
27 #include <glib-object.h>
28
29 #include "testcommon.h"
30 #include "testmodule.h"
31
32 /* This test tests getting the default vtable for an interface
33  * and the initialization and finalization of such default
34  * interfaces.
35  *
36  * We test this both for static and for dynamic interfaces.
37  */
38
39 /**********************************************************************
40  * Static interface tests
41  **********************************************************************/
42
43 typedef struct _TestStaticIfaceClass TestStaticIfaceClass;
44
45 struct _TestStaticIfaceClass
46 {
47   GTypeInterface base_iface;
48   guint val;
49 };
50
51 GType test_static_iface_get_type (void);
52 #define TEST_TYPE_STATIC_IFACE (test_static_iface_get_type ())
53
54 static void
55 test_static_iface_default_init (TestStaticIfaceClass *iface)
56 {
57   iface->val = 42;
58 }
59
60 DEFINE_IFACE (TestStaticIface, test_static_iface,
61               NULL, test_static_iface_default_init)
62
63 static void
64 test_static_iface (void)
65 {
66   TestStaticIfaceClass *static_iface;
67
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);
71
72   /* Ref loads */
73   static_iface = g_type_default_interface_ref (TEST_TYPE_STATIC_IFACE);
74   g_assert (static_iface && static_iface->val == 42);
75
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);
79   
80   /* Unref does nothing */
81   g_type_default_interface_unref (static_iface);
82   
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);
86 }
87
88 /**********************************************************************
89  * Dynamic interface tests
90  **********************************************************************/
91
92 typedef struct _TestDynamicIfaceClass TestDynamicIfaceClass;
93
94 struct _TestDynamicIfaceClass
95 {
96   GTypeInterface base_iface;
97   guint val;
98 };
99
100 static GType test_dynamic_iface_type;
101 static gboolean dynamic_iface_init = FALSE;
102
103 #define TEST_TYPE_DYNAMIC_IFACE (test_dynamic_iface_type)
104
105 static void
106 test_dynamic_iface_default_init (TestStaticIfaceClass *iface)
107 {
108   dynamic_iface_init = TRUE;
109   iface->val = 42;
110 }
111
112 static void
113 test_dynamic_iface_default_finalize (TestStaticIfaceClass *iface)
114 {
115   dynamic_iface_init = FALSE;
116 }
117
118 static void
119 test_dynamic_iface_register (GTypeModule *module)
120 {
121   const GTypeInfo iface_info =                  
122     {                                                           
123       sizeof (TestDynamicIfaceClass),
124       (GBaseInitFunc)      NULL,
125       (GBaseFinalizeFunc)  NULL,                                
126       (GClassInitFunc)     test_dynamic_iface_default_init,
127       (GClassFinalizeFunc) test_dynamic_iface_default_finalize
128     };                                                  
129
130   test_dynamic_iface_type = g_type_module_register_type (module, G_TYPE_INTERFACE,
131                                                          "TestDynamicIface", &iface_info, 0);
132 }
133
134 static void
135 module_register (GTypeModule *module)
136 {
137   test_dynamic_iface_register (module);
138 }
139
140 static void
141 test_dynamic_iface (void)
142 {
143   TestDynamicIfaceClass *dynamic_iface;
144
145   test_module_new (module_register);
146
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);
150
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);
155
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);
159   
160   /* Unref causes finalize */
161   g_type_default_interface_unref (dynamic_iface);
162 #if 0
163   g_assert (!dynamic_iface_init);
164 #endif
165
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
171   
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);
176
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
182 }
183
184 int
185 main (int   argc,
186       char *argv[])
187 {
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);
191
192   test_static_iface ();
193   test_dynamic_iface ();
194   
195   return 0;
196 }