Test case for g_type_add_interface_check().
[platform/upstream/glib.git] / tests / gobject / ifacecheck.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 "TestIfaceCheck"
22
23 #undef G_DISABLE_ASSERT
24 #undef G_DISABLE_CHECKS
25 #undef G_DISABLE_CAST_CHECKS
26
27 #include <string.h>
28
29 #include <glib-object.h>
30
31 #include "testcommon.h"
32
33 /* This test tests g_type_add_interface_check_func(), which allows
34  * installing a post-initialization check function.
35  */
36
37 #define TEST_TYPE_IFACE           (test_iface_get_type ())
38 #define TEST_IFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass))
39 typedef struct _TestIfaceClass  TestIfaceClass;
40
41 struct _TestIfaceClass
42 {
43   GTypeInterface base_iface;
44   GString *history;
45 };
46
47 static void
48 test_iface_base_init (TestIfaceClass *iface)
49 {
50   if (iface->history)
51     g_string_free (iface->history, TRUE);
52   iface->history = g_string_new (NULL);
53 }
54
55 static DEFINE_IFACE(TestIface, test_iface, test_iface_base_init, NULL)
56
57 /*
58  * TestObject1
59  */
60 #define TEST_TYPE_OBJECT1         (test_object1_get_type ())
61 typedef struct _GObject           TestObject1;
62 typedef struct _GObjectClass      TestObject1Class;
63
64 static DEFINE_TYPE_FULL (TestObject1, test_object1,
65                          NULL, NULL, NULL,
66                          G_TYPE_OBJECT,
67                          INTERFACE (NULL, TEST_TYPE_IFACE))
68      
69 /*
70  * TestObject2
71  */
72 #define TEST_TYPE_OBJECT2         (test_object2_get_type ())
73 typedef struct _GObject           TestObject2;
74 typedef struct _GObjectClass      TestObject2Class;
75
76 static DEFINE_TYPE_FULL (TestObject2, test_object2,
77                          NULL, NULL, NULL,
78                          G_TYPE_OBJECT,
79                          INTERFACE (NULL, TEST_TYPE_IFACE))
80      
81 /*
82  * TestObject3
83  */
84 #define TEST_TYPE_OBJECT3         (test_object3_get_type ())
85 typedef struct _GObject           TestObject3;
86 typedef struct _GObjectClass      TestObject3Class;
87
88 static DEFINE_TYPE_FULL (TestObject3, test_object3,
89                          NULL, NULL, NULL,
90                          G_TYPE_OBJECT,
91                          INTERFACE (NULL, TEST_TYPE_IFACE))
92      
93 /*
94  * TestObject4
95  */
96 #define TEST_TYPE_OBJECT4         (test_object4_get_type ())
97 typedef struct _GObject           TestObject4;
98 typedef struct _GObjectClass      TestObject4Class;
99
100
101 static DEFINE_TYPE_FULL (TestObject4, test_object4,
102                          NULL, NULL, NULL,
103                          G_TYPE_OBJECT, {})
104
105 static void
106 check_func (gpointer check_data,
107             gpointer g_iface)
108 {
109   TestIfaceClass *iface = g_iface;
110
111   g_string_append (iface->history, check_data);
112 }
113
114 int
115 main (int   argc,
116       char *argv[])
117 {
118   TestIfaceClass *iface;
119   GObject *object;
120   char *string1 = "A";
121   char *string2 = "B";
122
123   g_type_init ();
124   
125   /* Basic check of interfaces added before class_init time
126    */
127   g_type_add_interface_check (string1, check_func);
128
129   object = g_object_new (TEST_TYPE_OBJECT1, NULL);
130   iface = TEST_IFACE_GET_CLASS (object);
131     g_assert (strcmp (iface->history->str, "A") == 0);
132   g_object_unref (object);
133
134   /* Add a second check function
135    */
136   g_type_add_interface_check (string2, check_func);
137
138   object = g_object_new (TEST_TYPE_OBJECT2, NULL);
139   iface = TEST_IFACE_GET_CLASS (object);
140   g_assert (strcmp (iface->history->str, "AB") == 0);
141   g_object_unref (object);
142
143   /* Remove the first check function
144    */
145   g_type_remove_interface_check (string1, check_func);
146
147   object = g_object_new (TEST_TYPE_OBJECT3, NULL);
148   iface = TEST_IFACE_GET_CLASS (object);
149   g_assert (strcmp (iface->history->str, "B") == 0);
150   g_object_unref (object);
151
152   /* Test interfaces added after class_init time
153    */
154   g_type_class_ref (TEST_TYPE_OBJECT4);
155   {
156     static GInterfaceInfo const iface = {
157       NULL, NULL, NULL
158     };
159     
160     g_type_add_interface_static (TEST_TYPE_OBJECT4, TEST_TYPE_IFACE, &iface);
161   }
162   
163   object = g_object_new (TEST_TYPE_OBJECT4, NULL);
164   iface = TEST_IFACE_GET_CLASS (object);
165   g_assert (strcmp (iface->history->str, "B") == 0);
166   g_object_unref (object);
167     
168   return 0;
169 }