|
1 /* GObject - GLib Type, Object, Parameter and Signal Library |
|
2 * Copyright (C) 2001, 2003 Red Hat, Inc. |
|
3 * Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved. |
|
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 #ifdef __SYMBIAN32__ |
|
33 #include "mrt2_glib2_test.h" |
|
34 #endif /*__SYMBIAN32__*/ |
|
35 |
|
36 /* This test tests g_type_add_interface_check_func(), which allows |
|
37 * installing a post-initialization check function. |
|
38 */ |
|
39 |
|
40 #define TEST_TYPE_IFACE (test_iface_get_type ()) |
|
41 #define TEST_IFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass)) |
|
42 typedef struct _TestIfaceClass TestIfaceClass; |
|
43 |
|
44 struct _TestIfaceClass |
|
45 { |
|
46 GTypeInterface base_iface; |
|
47 GString *history; |
|
48 }; |
|
49 |
|
50 static void |
|
51 test_iface_base_init (TestIfaceClass *iface) |
|
52 { |
|
53 iface->history = g_string_new (iface->history ? iface->history->str : NULL); |
|
54 } |
|
55 |
|
56 static DEFINE_IFACE(TestIface, test_iface, test_iface_base_init, NULL) |
|
57 |
|
58 /* |
|
59 * TestObject1 |
|
60 */ |
|
61 #define TEST_TYPE_OBJECT1 (test_object1_get_type ()) |
|
62 typedef struct _GObject TestObject1; |
|
63 typedef struct _GObjectClass TestObject1Class; |
|
64 |
|
65 static DEFINE_TYPE_FULL (TestObject1, test_object1, |
|
66 NULL, NULL, NULL, |
|
67 G_TYPE_OBJECT, |
|
68 INTERFACE (NULL, TEST_TYPE_IFACE)) |
|
69 |
|
70 /* |
|
71 * TestObject2 |
|
72 */ |
|
73 #define TEST_TYPE_OBJECT2 (test_object2_get_type ()) |
|
74 typedef struct _GObject TestObject2; |
|
75 typedef struct _GObjectClass TestObject2Class; |
|
76 |
|
77 static DEFINE_TYPE_FULL (TestObject2, test_object2, |
|
78 NULL, NULL, NULL, |
|
79 G_TYPE_OBJECT, |
|
80 INTERFACE (NULL, TEST_TYPE_IFACE)) |
|
81 |
|
82 /* |
|
83 * TestObject3 |
|
84 */ |
|
85 #define TEST_TYPE_OBJECT3 (test_object3_get_type ()) |
|
86 typedef struct _GObject TestObject3; |
|
87 typedef struct _GObjectClass TestObject3Class; |
|
88 |
|
89 static DEFINE_TYPE_FULL (TestObject3, test_object3, |
|
90 NULL, NULL, NULL, |
|
91 G_TYPE_OBJECT, |
|
92 INTERFACE (NULL, TEST_TYPE_IFACE)) |
|
93 |
|
94 /* |
|
95 * TestObject4 |
|
96 */ |
|
97 #define TEST_TYPE_OBJECT4 (test_object4_get_type ()) |
|
98 typedef struct _GObject TestObject4; |
|
99 typedef struct _GObjectClass TestObject4Class; |
|
100 |
|
101 |
|
102 static DEFINE_TYPE_FULL (TestObject4, test_object4, |
|
103 NULL, NULL, NULL, |
|
104 G_TYPE_OBJECT, {}) |
|
105 |
|
106 static void |
|
107 check_func (gpointer check_data, |
|
108 gpointer g_iface) |
|
109 { |
|
110 TestIfaceClass *iface = g_iface; |
|
111 |
|
112 g_string_append (iface->history, check_data); |
|
113 } |
|
114 |
|
115 int |
|
116 main (int argc, |
|
117 char *argv[]) |
|
118 { |
|
119 TestIfaceClass *iface; |
|
120 GObject *object; |
|
121 char *string1 = "A"; |
|
122 char *string2 = "B"; |
|
123 #ifdef __SYMBIAN32__ |
|
124 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); |
|
125 g_set_print_handler(mrtPrintHandler); |
|
126 #endif /*__SYMBIAN32__*/ |
|
127 g_type_init (); |
|
128 |
|
129 /* Basic check of interfaces added before class_init time |
|
130 */ |
|
131 g_type_add_interface_check (string1, check_func); |
|
132 |
|
133 object = g_object_new (TEST_TYPE_OBJECT1, NULL); |
|
134 iface = TEST_IFACE_GET_CLASS (object); |
|
135 g_assert (strcmp (iface->history->str, "A") == 0); |
|
136 g_object_unref (object); |
|
137 |
|
138 /* Add a second check function |
|
139 */ |
|
140 g_type_add_interface_check (string2, check_func); |
|
141 |
|
142 object = g_object_new (TEST_TYPE_OBJECT2, NULL); |
|
143 iface = TEST_IFACE_GET_CLASS (object); |
|
144 g_assert (strcmp (iface->history->str, "AB") == 0); |
|
145 g_object_unref (object); |
|
146 |
|
147 /* Remove the first check function |
|
148 */ |
|
149 g_type_remove_interface_check (string1, check_func); |
|
150 |
|
151 object = g_object_new (TEST_TYPE_OBJECT3, NULL); |
|
152 iface = TEST_IFACE_GET_CLASS (object); |
|
153 g_assert (strcmp (iface->history->str, "B") == 0); |
|
154 g_object_unref (object); |
|
155 |
|
156 /* Test interfaces added after class_init time |
|
157 */ |
|
158 g_type_class_ref (TEST_TYPE_OBJECT4); |
|
159 { |
|
160 static GInterfaceInfo const iface = { |
|
161 NULL, NULL, NULL |
|
162 }; |
|
163 |
|
164 g_type_add_interface_static (TEST_TYPE_OBJECT4, TEST_TYPE_IFACE, &iface); |
|
165 } |
|
166 |
|
167 object = g_object_new (TEST_TYPE_OBJECT4, NULL); |
|
168 iface = TEST_IFACE_GET_CLASS (object); |
|
169 g_assert (strcmp (iface->history->str, "B") == 0); |
|
170 g_object_unref (object); |
|
171 #ifdef __SYMBIAN32__ |
|
172 testResultXml("ifacecheck"); |
|
173 #endif /*__SYMBIAN32__*/ |
|
174 return 0; |
|
175 } |