First public contribution.
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.
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.
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.
21 #define G_LOG_DOMAIN "TestReferences"
23 #undef G_DISABLE_ASSERT
24 #undef G_DISABLE_CHECKS
25 #undef G_DISABLE_CAST_CHECKS
27 #include <glib-object.h>
30 #include "mrt2_glib2_test.h"
31 #endif /*__SYMBIAN32__*/
32 /* This test tests weak and toggle references
35 static GObject *global_object;
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;
48 * TestObject, a parent class for TestObject
50 #define TEST_TYPE_OBJECT (test_object_get_type ())
51 typedef struct _TestObject TestObject;
52 typedef struct _TestObjectClass TestObjectClass;
56 GObject parent_instance;
58 struct _TestObjectClass
60 GObjectClass parent_class;
63 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT);
66 test_object_finalize (GObject *object)
68 object_destroyed = TRUE;
70 G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
74 test_object_class_init (TestObjectClass *class)
76 GObjectClass *object_class = G_OBJECT_CLASS (class);
78 object_class->finalize = test_object_finalize;
82 test_object_init (TestObject *test_object)
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;
101 weak_ref1 (gpointer data,
104 g_assert (object == global_object);
105 g_assert (data == GUINT_TO_POINTER (42));
107 weak_ref1_notified = TRUE;
111 weak_ref2 (gpointer data,
114 g_assert (object == global_object);
115 g_assert (data == GUINT_TO_POINTER (24));
117 weak_ref2_notified = TRUE;
121 toggle_ref1 (gpointer data,
123 gboolean is_last_ref)
125 g_assert (object == global_object);
126 g_assert (data == GUINT_TO_POINTER (42));
129 toggle_ref1_weakened = TRUE;
131 toggle_ref1_strengthened = TRUE;
135 toggle_ref2 (gpointer data,
137 gboolean is_last_ref)
139 g_assert (object == global_object);
140 g_assert (data == GUINT_TO_POINTER (24));
143 toggle_ref2_weakened = TRUE;
145 toggle_ref2_strengthened = TRUE;
149 toggle_ref3 (gpointer data,
151 gboolean is_last_ref)
153 g_assert (object == global_object);
154 g_assert (data == GUINT_TO_POINTER (34));
158 toggle_ref3_weakened = TRUE;
159 g_object_remove_toggle_ref (object, toggle_ref3, GUINT_TO_POINTER (34));
162 toggle_ref3_strengthened = TRUE;
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);
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
181 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
183 g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
186 g_object_unref (object);
187 g_assert (weak_ref1_notified == TRUE);
188 g_assert (object_destroyed == TRUE);
190 /* Test two weak references at once
192 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
194 g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
195 g_object_weak_ref (object, weak_ref2, GUINT_TO_POINTER (24));
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);
203 /* Test remove weak references
205 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
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));
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);
217 /* Test basic toggle reference operation
219 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
221 g_object_add_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
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);
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);
235 g_object_unref (object);
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);
243 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
245 /* Test two toggle references at once
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));
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);
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);
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);
274 /* Test a toggle reference that removes itself
276 global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
278 g_object_add_toggle_ref (object, toggle_ref3, GUINT_TO_POINTER (34));
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);
287 testResultXml("references");
288 #endif /*__SYMBIAN32__*/