|
1 /* GObject - GLib Type, Object, Parameter and Signal Library |
|
2 * Copyright (C) 2005 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 "TestReferences" |
|
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 #ifdef __SYMBIAN32__ |
|
30 #include "mrt2_glib2_test.h" |
|
31 #endif /*__SYMBIAN32__*/ |
|
32 /* This test tests weak and toggle references |
|
33 */ |
|
34 |
|
35 static GObject *global_object; |
|
36 |
|
37 static gboolean object_destroyed; |
|
38 static gboolean weak_ref1_notified; |
|
39 static gboolean weak_ref2_notified; |
|
40 static gboolean toggle_ref1_weakened; |
|
41 static gboolean toggle_ref1_strengthened; |
|
42 static gboolean toggle_ref2_weakened; |
|
43 static gboolean toggle_ref2_strengthened; |
|
44 static gboolean toggle_ref3_weakened; |
|
45 static gboolean toggle_ref3_strengthened; |
|
46 |
|
47 /* |
|
48 * TestObject, a parent class for TestObject |
|
49 */ |
|
50 #define TEST_TYPE_OBJECT (test_object_get_type ()) |
|
51 typedef struct _TestObject TestObject; |
|
52 typedef struct _TestObjectClass TestObjectClass; |
|
53 |
|
54 struct _TestObject |
|
55 { |
|
56 GObject parent_instance; |
|
57 }; |
|
58 struct _TestObjectClass |
|
59 { |
|
60 GObjectClass parent_class; |
|
61 }; |
|
62 |
|
63 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT); |
|
64 |
|
65 static void |
|
66 test_object_finalize (GObject *object) |
|
67 { |
|
68 object_destroyed = TRUE; |
|
69 |
|
70 G_OBJECT_CLASS (test_object_parent_class)->finalize (object); |
|
71 } |
|
72 |
|
73 static void |
|
74 test_object_class_init (TestObjectClass *class) |
|
75 { |
|
76 GObjectClass *object_class = G_OBJECT_CLASS (class); |
|
77 |
|
78 object_class->finalize = test_object_finalize; |
|
79 } |
|
80 |
|
81 static void |
|
82 test_object_init (TestObject *test_object) |
|
83 { |
|
84 } |
|
85 |
|
86 static void |
|
87 clear_flags (void) |
|
88 { |
|
89 object_destroyed = FALSE; |
|
90 weak_ref1_notified = FALSE; |
|
91 weak_ref2_notified = FALSE; |
|
92 toggle_ref1_weakened = FALSE; |
|
93 toggle_ref1_strengthened = FALSE; |
|
94 toggle_ref2_weakened = FALSE; |
|
95 toggle_ref2_strengthened = FALSE; |
|
96 toggle_ref3_weakened = FALSE; |
|
97 toggle_ref3_strengthened = FALSE; |
|
98 } |
|
99 |
|
100 static void |
|
101 weak_ref1 (gpointer data, |
|
102 GObject *object) |
|
103 { |
|
104 g_assert (object == global_object); |
|
105 g_assert (data == GUINT_TO_POINTER (42)); |
|
106 |
|
107 weak_ref1_notified = TRUE; |
|
108 } |
|
109 |
|
110 static void |
|
111 weak_ref2 (gpointer data, |
|
112 GObject *object) |
|
113 { |
|
114 g_assert (object == global_object); |
|
115 g_assert (data == GUINT_TO_POINTER (24)); |
|
116 |
|
117 weak_ref2_notified = TRUE; |
|
118 } |
|
119 |
|
120 static void |
|
121 toggle_ref1 (gpointer data, |
|
122 GObject *object, |
|
123 gboolean is_last_ref) |
|
124 { |
|
125 g_assert (object == global_object); |
|
126 g_assert (data == GUINT_TO_POINTER (42)); |
|
127 |
|
128 if (is_last_ref) |
|
129 toggle_ref1_weakened = TRUE; |
|
130 else |
|
131 toggle_ref1_strengthened = TRUE; |
|
132 } |
|
133 |
|
134 static void |
|
135 toggle_ref2 (gpointer data, |
|
136 GObject *object, |
|
137 gboolean is_last_ref) |
|
138 { |
|
139 g_assert (object == global_object); |
|
140 g_assert (data == GUINT_TO_POINTER (24)); |
|
141 |
|
142 if (is_last_ref) |
|
143 toggle_ref2_weakened = TRUE; |
|
144 else |
|
145 toggle_ref2_strengthened = TRUE; |
|
146 } |
|
147 |
|
148 static void |
|
149 toggle_ref3 (gpointer data, |
|
150 GObject *object, |
|
151 gboolean is_last_ref) |
|
152 { |
|
153 g_assert (object == global_object); |
|
154 g_assert (data == GUINT_TO_POINTER (34)); |
|
155 |
|
156 if (is_last_ref) |
|
157 { |
|
158 toggle_ref3_weakened = TRUE; |
|
159 g_object_remove_toggle_ref (object, toggle_ref3, GUINT_TO_POINTER (34)); |
|
160 } |
|
161 else |
|
162 toggle_ref3_strengthened = TRUE; |
|
163 } |
|
164 |
|
165 int |
|
166 main (int argc, |
|
167 char *argv[]) |
|
168 { |
|
169 GObject *object; |
|
170 |
|
171 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) | |
|
172 G_LOG_LEVEL_WARNING | |
|
173 G_LOG_LEVEL_CRITICAL); |
|
174 g_type_init (); |
|
175 |
|
176 #ifdef __SYMBIAN32__ |
|
177 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); |
|
178 #endif /*__SYMBIAN32__*/ |
|
179 /* Test basic weak reference operation |
|
180 */ |
|
181 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL); |
|
182 |
|
183 g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42)); |
|
184 |
|
185 clear_flags (); |
|
186 g_object_unref (object); |
|
187 g_assert (weak_ref1_notified == TRUE); |
|
188 g_assert (object_destroyed == TRUE); |
|
189 |
|
190 /* Test two weak references at once |
|
191 */ |
|
192 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL); |
|
193 |
|
194 g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42)); |
|
195 g_object_weak_ref (object, weak_ref2, GUINT_TO_POINTER (24)); |
|
196 |
|
197 clear_flags (); |
|
198 g_object_unref (object); |
|
199 g_assert (weak_ref1_notified == TRUE); |
|
200 g_assert (weak_ref2_notified == TRUE); |
|
201 g_assert (object_destroyed == TRUE); |
|
202 |
|
203 /* Test remove weak references |
|
204 */ |
|
205 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL); |
|
206 |
|
207 g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42)); |
|
208 g_object_weak_ref (object, weak_ref2, GUINT_TO_POINTER (24)); |
|
209 g_object_weak_unref (object, weak_ref1, GUINT_TO_POINTER (42)); |
|
210 |
|
211 clear_flags (); |
|
212 g_object_unref (object); |
|
213 g_assert (weak_ref1_notified == FALSE); |
|
214 g_assert (weak_ref2_notified == TRUE); |
|
215 g_assert (object_destroyed == TRUE); |
|
216 |
|
217 /* Test basic toggle reference operation |
|
218 */ |
|
219 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL); |
|
220 |
|
221 g_object_add_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42)); |
|
222 |
|
223 clear_flags (); |
|
224 g_object_unref (object); |
|
225 g_assert (toggle_ref1_weakened == TRUE); |
|
226 g_assert (toggle_ref1_strengthened == FALSE); |
|
227 g_assert (object_destroyed == FALSE); |
|
228 |
|
229 clear_flags (); |
|
230 g_object_ref (object); |
|
231 g_assert (toggle_ref1_weakened == FALSE); |
|
232 g_assert (toggle_ref1_strengthened == TRUE); |
|
233 g_assert (object_destroyed == FALSE); |
|
234 |
|
235 g_object_unref (object); |
|
236 |
|
237 clear_flags (); |
|
238 g_object_remove_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42)); |
|
239 g_assert (toggle_ref1_weakened == FALSE); |
|
240 g_assert (toggle_ref1_strengthened == FALSE); |
|
241 g_assert (object_destroyed == TRUE); |
|
242 |
|
243 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL); |
|
244 |
|
245 /* Test two toggle references at once |
|
246 */ |
|
247 g_object_add_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42)); |
|
248 g_object_add_toggle_ref (object, toggle_ref2, GUINT_TO_POINTER (24)); |
|
249 |
|
250 clear_flags (); |
|
251 g_object_unref (object); |
|
252 g_assert (toggle_ref1_weakened == FALSE); |
|
253 g_assert (toggle_ref1_strengthened == FALSE); |
|
254 g_assert (toggle_ref2_weakened == FALSE); |
|
255 g_assert (toggle_ref2_strengthened == FALSE); |
|
256 g_assert (object_destroyed == FALSE); |
|
257 |
|
258 clear_flags (); |
|
259 g_object_remove_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42)); |
|
260 g_assert (toggle_ref1_weakened == FALSE); |
|
261 g_assert (toggle_ref1_strengthened == FALSE); |
|
262 g_assert (toggle_ref2_weakened == TRUE); |
|
263 g_assert (toggle_ref2_strengthened == FALSE); |
|
264 g_assert (object_destroyed == FALSE); |
|
265 |
|
266 clear_flags (); |
|
267 g_object_remove_toggle_ref (object, toggle_ref2, GUINT_TO_POINTER (24)); |
|
268 g_assert (toggle_ref1_weakened == FALSE); |
|
269 g_assert (toggle_ref1_strengthened == FALSE); |
|
270 g_assert (toggle_ref2_weakened == FALSE); |
|
271 g_assert (toggle_ref2_strengthened == FALSE); |
|
272 g_assert (object_destroyed == TRUE); |
|
273 |
|
274 /* Test a toggle reference that removes itself |
|
275 */ |
|
276 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL); |
|
277 |
|
278 g_object_add_toggle_ref (object, toggle_ref3, GUINT_TO_POINTER (34)); |
|
279 |
|
280 clear_flags (); |
|
281 g_object_unref (object); |
|
282 g_assert (toggle_ref3_weakened == TRUE); |
|
283 g_assert (toggle_ref3_strengthened == FALSE); |
|
284 g_assert (object_destroyed == TRUE); |
|
285 |
|
286 #ifdef __SYMBIAN32__ |
|
287 testResultXml("references"); |
|
288 #endif /*__SYMBIAN32__*/ |
|
289 return 0; |
|
290 } |