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