• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <glib-object.h>
2 
3 static void
test_registration_serial(void)4 test_registration_serial (void)
5 {
6   gint serial1, serial2, serial3;
7 
8   serial1 = g_type_get_type_registration_serial ();
9   g_pointer_type_register_static ("my+pointer");
10   serial2 = g_type_get_type_registration_serial ();
11   g_assert (serial1 != serial2);
12   serial3 = g_type_get_type_registration_serial ();
13   g_assert (serial2 == serial3);
14 }
15 
16 typedef struct {
17   GTypeInterface g_iface;
18 } BarInterface;
19 
20 GType bar_get_type (void);
21 
G_DEFINE_INTERFACE(Bar,bar,G_TYPE_OBJECT)22 G_DEFINE_INTERFACE (Bar, bar, G_TYPE_OBJECT)
23 
24 static void
25 bar_default_init (BarInterface *iface)
26 {
27 }
28 
29 typedef struct {
30   GTypeInterface g_iface;
31 } FooInterface;
32 
33 GType foo_get_type (void);
34 
G_DEFINE_INTERFACE_WITH_CODE(Foo,foo,G_TYPE_OBJECT,g_type_interface_add_prerequisite (g_define_type_id,bar_get_type ()))35 G_DEFINE_INTERFACE_WITH_CODE (Foo, foo, G_TYPE_OBJECT,
36                               g_type_interface_add_prerequisite (g_define_type_id, bar_get_type ()))
37 
38 static void
39 foo_default_init (FooInterface *iface)
40 {
41 }
42 
43 typedef struct {
44   GTypeInterface g_iface;
45 } BaaInterface;
46 
47 GType baa_get_type (void);
48 
G_DEFINE_INTERFACE(Baa,baa,G_TYPE_INVALID)49 G_DEFINE_INTERFACE (Baa, baa, G_TYPE_INVALID)
50 
51 static void
52 baa_default_init (BaaInterface *iface)
53 {
54 }
55 
56 typedef struct {
57   GTypeInterface g_iface;
58 } BooInterface;
59 
60 GType boo_get_type (void);
61 
G_DEFINE_INTERFACE_WITH_CODE(Boo,boo,G_TYPE_INVALID,g_type_interface_add_prerequisite (g_define_type_id,baa_get_type ()))62 G_DEFINE_INTERFACE_WITH_CODE (Boo, boo, G_TYPE_INVALID,
63                               g_type_interface_add_prerequisite (g_define_type_id, baa_get_type ()))
64 
65 static void
66 boo_default_init (BooInterface *iface)
67 {
68 }
69 
70 typedef struct {
71   GTypeInterface g_iface;
72 } BibiInterface;
73 
74 GType bibi_get_type (void);
75 
G_DEFINE_INTERFACE(Bibi,bibi,G_TYPE_INITIALLY_UNOWNED)76 G_DEFINE_INTERFACE (Bibi, bibi, G_TYPE_INITIALLY_UNOWNED)
77 
78 static void
79 bibi_default_init (BibiInterface *iface)
80 {
81 }
82 
83 typedef struct {
84   GTypeInterface g_iface;
85 } BozoInterface;
86 
87 GType bozo_get_type (void);
88 
G_DEFINE_INTERFACE_WITH_CODE(Bozo,bozo,G_TYPE_INVALID,g_type_interface_add_prerequisite (g_define_type_id,foo_get_type ());g_type_interface_add_prerequisite (g_define_type_id,bibi_get_type ()))89 G_DEFINE_INTERFACE_WITH_CODE (Bozo, bozo, G_TYPE_INVALID,
90                               g_type_interface_add_prerequisite (g_define_type_id, foo_get_type ());
91                               g_type_interface_add_prerequisite (g_define_type_id, bibi_get_type ()))
92 
93 static void
94 bozo_default_init (BozoInterface *iface)
95 {
96 }
97 
98 
99 
100 static void
test_interface_prerequisite(void)101 test_interface_prerequisite (void)
102 {
103   GType *prereqs;
104   guint n_prereqs;
105   gpointer iface;
106   gpointer parent;
107 
108   prereqs = g_type_interface_prerequisites (foo_get_type (), &n_prereqs);
109   g_assert_cmpint (n_prereqs, ==, 2);
110   g_assert (prereqs[0] == bar_get_type ());
111   g_assert (prereqs[1] == G_TYPE_OBJECT);
112   g_assert (g_type_interface_instantiatable_prerequisite (foo_get_type ()) == G_TYPE_OBJECT);
113 
114   iface = g_type_default_interface_ref (foo_get_type ());
115   parent = g_type_interface_peek_parent (iface);
116   g_assert (parent == NULL);
117   g_type_default_interface_unref (iface);
118 
119   g_free (prereqs);
120 
121   g_assert_cmpint (g_type_interface_instantiatable_prerequisite (baa_get_type ()), ==, G_TYPE_INVALID);
122   g_assert_cmpint (g_type_interface_instantiatable_prerequisite (boo_get_type ()), ==, G_TYPE_INVALID);
123 
124   g_assert_cmpint (g_type_interface_instantiatable_prerequisite (bozo_get_type ()), ==, G_TYPE_INITIALLY_UNOWNED);
125 }
126 
127 typedef struct {
128   GTypeInterface g_iface;
129 } BazInterface;
130 
131 GType baz_get_type (void);
132 
G_DEFINE_INTERFACE(Baz,baz,G_TYPE_OBJECT)133 G_DEFINE_INTERFACE (Baz, baz, G_TYPE_OBJECT)
134 
135 static void
136 baz_default_init (BazInterface *iface)
137 {
138 }
139 
140 typedef struct {
141   GObject parent;
142 } Bazo;
143 
144 typedef struct {
145   GObjectClass parent_class;
146 } BazoClass;
147 
148 GType bazo_get_type (void);
149 static void bazo_iface_init (BazInterface *i);
150 
G_DEFINE_TYPE_WITH_CODE(Bazo,bazo,G_TYPE_INITIALLY_UNOWNED,G_IMPLEMENT_INTERFACE (baz_get_type (),bazo_iface_init);)151 G_DEFINE_TYPE_WITH_CODE (Bazo, bazo, G_TYPE_INITIALLY_UNOWNED,
152                          G_IMPLEMENT_INTERFACE (baz_get_type (),
153                                                 bazo_iface_init);)
154 
155 static void
156 bazo_init (Bazo *b)
157 {
158 }
159 
160 static void
bazo_class_init(BazoClass * c)161 bazo_class_init (BazoClass *c)
162 {
163 }
164 
165 static void
bazo_iface_init(BazInterface * i)166 bazo_iface_init (BazInterface *i)
167 {
168 }
169 
170 static gint check_called;
171 
172 static void
check_func(gpointer check_data,gpointer g_iface)173 check_func (gpointer check_data,
174             gpointer g_iface)
175 {
176   g_assert (check_data == &check_called);
177 
178   check_called++;
179 }
180 
181 static void
test_interface_check(void)182 test_interface_check (void)
183 {
184   GObject *o;
185 
186   check_called = 0;
187   g_type_add_interface_check (&check_called, check_func);
188   o = g_object_new (bazo_get_type (), NULL);
189   g_object_unref (o);
190   g_assert_cmpint (check_called, ==, 1);
191   g_type_remove_interface_check (&check_called, check_func);
192 }
193 
194 static void
test_next_base(void)195 test_next_base (void)
196 {
197   GType type;
198 
199   type = g_type_next_base (bazo_get_type (), G_TYPE_OBJECT);
200 
201   g_assert (type == G_TYPE_INITIALLY_UNOWNED);
202 }
203 
204 int
main(int argc,char * argv[])205 main (int argc, char *argv[])
206 {
207   g_test_init (&argc, &argv, NULL);
208 
209   g_test_add_func ("/type/registration-serial", test_registration_serial);
210   g_test_add_func ("/type/interface-prerequisite", test_interface_prerequisite);
211   g_test_add_func ("/type/interface-check", test_interface_check);
212   g_test_add_func ("/type/next-base", test_next_base);
213 
214   return g_test_run ();
215 }
216