• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define TEST_TYPE_STATIC_IFACE (test_static_iface_get_type ())
52 
53 static void
test_static_iface_default_init(TestStaticIfaceClass * iface)54 test_static_iface_default_init (TestStaticIfaceClass *iface)
55 {
56   iface->val = 42;
57 }
58 
DEFINE_IFACE(TestStaticIface,test_static_iface,NULL,test_static_iface_default_init)59 DEFINE_IFACE (TestStaticIface, test_static_iface,
60 	      NULL, test_static_iface_default_init)
61 
62 static void
63 test_static_iface (void)
64 {
65   TestStaticIfaceClass *static_iface;
66 
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);
70 
71   /* Ref loads */
72   static_iface = g_type_default_interface_ref (TEST_TYPE_STATIC_IFACE);
73   g_assert (static_iface && static_iface->val == 42);
74 
75   /* Peek then works */
76   static_iface = g_type_default_interface_peek (TEST_TYPE_STATIC_IFACE);
77   g_assert (static_iface && static_iface->val == 42);
78 
79   /* Unref does nothing */
80   g_type_default_interface_unref (static_iface);
81 
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);
85 }
86 
87 /**********************************************************************
88  * Dynamic interface tests
89  **********************************************************************/
90 
91 typedef struct _TestDynamicIfaceClass TestDynamicIfaceClass;
92 
93 struct _TestDynamicIfaceClass
94 {
95   GTypeInterface base_iface;
96   guint val;
97 };
98 
99 static GType test_dynamic_iface_type;
100 static gboolean dynamic_iface_init = FALSE;
101 
102 #define TEST_TYPE_DYNAMIC_IFACE (test_dynamic_iface_type)
103 
104 static void
test_dynamic_iface_default_init(TestStaticIfaceClass * iface)105 test_dynamic_iface_default_init (TestStaticIfaceClass *iface)
106 {
107   dynamic_iface_init = TRUE;
108   iface->val = 42;
109 }
110 
111 static void
test_dynamic_iface_default_finalize(TestStaticIfaceClass * iface)112 test_dynamic_iface_default_finalize (TestStaticIfaceClass *iface)
113 {
114   dynamic_iface_init = FALSE;
115 }
116 
117 static void
test_dynamic_iface_register(GTypeModule * module)118 test_dynamic_iface_register (GTypeModule *module)
119 {
120   static const GTypeInfo iface_info =
121     {
122       sizeof (TestDynamicIfaceClass),
123       (GBaseInitFunc)	   NULL,
124       (GBaseFinalizeFunc)  NULL,
125       (GClassInitFunc)     test_dynamic_iface_default_init,
126       (GClassFinalizeFunc) test_dynamic_iface_default_finalize
127     };
128 
129   test_dynamic_iface_type = g_type_module_register_type (module, G_TYPE_INTERFACE,
130 							 "TestDynamicIface", &iface_info, 0);
131 }
132 
133 static void
module_register(GTypeModule * module)134 module_register (GTypeModule *module)
135 {
136   test_dynamic_iface_register (module);
137 }
138 
139 static void
test_dynamic_iface(void)140 test_dynamic_iface (void)
141 {
142   GTypeModule *module;
143   TestDynamicIfaceClass *dynamic_iface;
144 
145   module = 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   g_assert (!dynamic_iface_init);
163 
164   /* Peek returns NULL */
165   dynamic_iface = g_type_default_interface_peek (TEST_TYPE_DYNAMIC_IFACE);
166   g_assert (dynamic_iface == NULL);
167 
168   /* Ref reloads */
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);
172 
173   /* And Unref causes finalize once more*/
174   g_type_default_interface_unref (dynamic_iface);
175   g_assert (!dynamic_iface_init);
176 }
177 
178 int
main(int argc,char * argv[])179 main (int   argc,
180       char *argv[])
181 {
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);
185   g_type_init ();
186 
187   test_static_iface ();
188   test_dynamic_iface ();
189 
190   return 0;
191 }
192