sl@0
|
1 |
/* GObject - GLib Type, Object, Parameter and Signal Library
|
sl@0
|
2 |
* Copyright (C) 2005 Red Hat, Inc.
|
sl@0
|
3 |
* Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved.
|
sl@0
|
4 |
* This library is free software; you can redistribute it and/or
|
sl@0
|
5 |
* modify it under the terms of the GNU Lesser General Public
|
sl@0
|
6 |
* License as published by the Free Software Foundation; either
|
sl@0
|
7 |
* version 2 of the License, or (at your option) any later version.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is distributed in the hope that it will be useful,
|
sl@0
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
sl@0
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
sl@0
|
12 |
* Lesser General Public License for more details.
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* You should have received a copy of the GNU Lesser General
|
sl@0
|
15 |
* Public License along with this library; if not, write to the
|
sl@0
|
16 |
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
sl@0
|
17 |
* Boston, MA 02111-1307, USA.
|
sl@0
|
18 |
*/
|
sl@0
|
19 |
|
sl@0
|
20 |
#undef G_LOG_DOMAIN
|
sl@0
|
21 |
#define G_LOG_DOMAIN "TestReferences"
|
sl@0
|
22 |
|
sl@0
|
23 |
#undef G_DISABLE_ASSERT
|
sl@0
|
24 |
#undef G_DISABLE_CHECKS
|
sl@0
|
25 |
#undef G_DISABLE_CAST_CHECKS
|
sl@0
|
26 |
|
sl@0
|
27 |
#include <glib-object.h>
|
sl@0
|
28 |
|
sl@0
|
29 |
#ifdef __SYMBIAN32__
|
sl@0
|
30 |
#include "mrt2_glib2_test.h"
|
sl@0
|
31 |
#endif /*__SYMBIAN32__*/
|
sl@0
|
32 |
/* This test tests weak and toggle references
|
sl@0
|
33 |
*/
|
sl@0
|
34 |
|
sl@0
|
35 |
static GObject *global_object;
|
sl@0
|
36 |
|
sl@0
|
37 |
static gboolean object_destroyed;
|
sl@0
|
38 |
static gboolean weak_ref1_notified;
|
sl@0
|
39 |
static gboolean weak_ref2_notified;
|
sl@0
|
40 |
static gboolean toggle_ref1_weakened;
|
sl@0
|
41 |
static gboolean toggle_ref1_strengthened;
|
sl@0
|
42 |
static gboolean toggle_ref2_weakened;
|
sl@0
|
43 |
static gboolean toggle_ref2_strengthened;
|
sl@0
|
44 |
static gboolean toggle_ref3_weakened;
|
sl@0
|
45 |
static gboolean toggle_ref3_strengthened;
|
sl@0
|
46 |
|
sl@0
|
47 |
/*
|
sl@0
|
48 |
* TestObject, a parent class for TestObject
|
sl@0
|
49 |
*/
|
sl@0
|
50 |
#define TEST_TYPE_OBJECT (test_object_get_type ())
|
sl@0
|
51 |
typedef struct _TestObject TestObject;
|
sl@0
|
52 |
typedef struct _TestObjectClass TestObjectClass;
|
sl@0
|
53 |
|
sl@0
|
54 |
struct _TestObject
|
sl@0
|
55 |
{
|
sl@0
|
56 |
GObject parent_instance;
|
sl@0
|
57 |
};
|
sl@0
|
58 |
struct _TestObjectClass
|
sl@0
|
59 |
{
|
sl@0
|
60 |
GObjectClass parent_class;
|
sl@0
|
61 |
};
|
sl@0
|
62 |
|
sl@0
|
63 |
G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT);
|
sl@0
|
64 |
|
sl@0
|
65 |
static void
|
sl@0
|
66 |
test_object_finalize (GObject *object)
|
sl@0
|
67 |
{
|
sl@0
|
68 |
object_destroyed = TRUE;
|
sl@0
|
69 |
|
sl@0
|
70 |
G_OBJECT_CLASS (test_object_parent_class)->finalize (object);
|
sl@0
|
71 |
}
|
sl@0
|
72 |
|
sl@0
|
73 |
static void
|
sl@0
|
74 |
test_object_class_init (TestObjectClass *class)
|
sl@0
|
75 |
{
|
sl@0
|
76 |
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
sl@0
|
77 |
|
sl@0
|
78 |
object_class->finalize = test_object_finalize;
|
sl@0
|
79 |
}
|
sl@0
|
80 |
|
sl@0
|
81 |
static void
|
sl@0
|
82 |
test_object_init (TestObject *test_object)
|
sl@0
|
83 |
{
|
sl@0
|
84 |
}
|
sl@0
|
85 |
|
sl@0
|
86 |
static void
|
sl@0
|
87 |
clear_flags (void)
|
sl@0
|
88 |
{
|
sl@0
|
89 |
object_destroyed = FALSE;
|
sl@0
|
90 |
weak_ref1_notified = FALSE;
|
sl@0
|
91 |
weak_ref2_notified = FALSE;
|
sl@0
|
92 |
toggle_ref1_weakened = FALSE;
|
sl@0
|
93 |
toggle_ref1_strengthened = FALSE;
|
sl@0
|
94 |
toggle_ref2_weakened = FALSE;
|
sl@0
|
95 |
toggle_ref2_strengthened = FALSE;
|
sl@0
|
96 |
toggle_ref3_weakened = FALSE;
|
sl@0
|
97 |
toggle_ref3_strengthened = FALSE;
|
sl@0
|
98 |
}
|
sl@0
|
99 |
|
sl@0
|
100 |
static void
|
sl@0
|
101 |
weak_ref1 (gpointer data,
|
sl@0
|
102 |
GObject *object)
|
sl@0
|
103 |
{
|
sl@0
|
104 |
g_assert (object == global_object);
|
sl@0
|
105 |
g_assert (data == GUINT_TO_POINTER (42));
|
sl@0
|
106 |
|
sl@0
|
107 |
weak_ref1_notified = TRUE;
|
sl@0
|
108 |
}
|
sl@0
|
109 |
|
sl@0
|
110 |
static void
|
sl@0
|
111 |
weak_ref2 (gpointer data,
|
sl@0
|
112 |
GObject *object)
|
sl@0
|
113 |
{
|
sl@0
|
114 |
g_assert (object == global_object);
|
sl@0
|
115 |
g_assert (data == GUINT_TO_POINTER (24));
|
sl@0
|
116 |
|
sl@0
|
117 |
weak_ref2_notified = TRUE;
|
sl@0
|
118 |
}
|
sl@0
|
119 |
|
sl@0
|
120 |
static void
|
sl@0
|
121 |
toggle_ref1 (gpointer data,
|
sl@0
|
122 |
GObject *object,
|
sl@0
|
123 |
gboolean is_last_ref)
|
sl@0
|
124 |
{
|
sl@0
|
125 |
g_assert (object == global_object);
|
sl@0
|
126 |
g_assert (data == GUINT_TO_POINTER (42));
|
sl@0
|
127 |
|
sl@0
|
128 |
if (is_last_ref)
|
sl@0
|
129 |
toggle_ref1_weakened = TRUE;
|
sl@0
|
130 |
else
|
sl@0
|
131 |
toggle_ref1_strengthened = TRUE;
|
sl@0
|
132 |
}
|
sl@0
|
133 |
|
sl@0
|
134 |
static void
|
sl@0
|
135 |
toggle_ref2 (gpointer data,
|
sl@0
|
136 |
GObject *object,
|
sl@0
|
137 |
gboolean is_last_ref)
|
sl@0
|
138 |
{
|
sl@0
|
139 |
g_assert (object == global_object);
|
sl@0
|
140 |
g_assert (data == GUINT_TO_POINTER (24));
|
sl@0
|
141 |
|
sl@0
|
142 |
if (is_last_ref)
|
sl@0
|
143 |
toggle_ref2_weakened = TRUE;
|
sl@0
|
144 |
else
|
sl@0
|
145 |
toggle_ref2_strengthened = TRUE;
|
sl@0
|
146 |
}
|
sl@0
|
147 |
|
sl@0
|
148 |
static void
|
sl@0
|
149 |
toggle_ref3 (gpointer data,
|
sl@0
|
150 |
GObject *object,
|
sl@0
|
151 |
gboolean is_last_ref)
|
sl@0
|
152 |
{
|
sl@0
|
153 |
g_assert (object == global_object);
|
sl@0
|
154 |
g_assert (data == GUINT_TO_POINTER (34));
|
sl@0
|
155 |
|
sl@0
|
156 |
if (is_last_ref)
|
sl@0
|
157 |
{
|
sl@0
|
158 |
toggle_ref3_weakened = TRUE;
|
sl@0
|
159 |
g_object_remove_toggle_ref (object, toggle_ref3, GUINT_TO_POINTER (34));
|
sl@0
|
160 |
}
|
sl@0
|
161 |
else
|
sl@0
|
162 |
toggle_ref3_strengthened = TRUE;
|
sl@0
|
163 |
}
|
sl@0
|
164 |
|
sl@0
|
165 |
int
|
sl@0
|
166 |
main (int argc,
|
sl@0
|
167 |
char *argv[])
|
sl@0
|
168 |
{
|
sl@0
|
169 |
GObject *object;
|
sl@0
|
170 |
|
sl@0
|
171 |
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
|
sl@0
|
172 |
G_LOG_LEVEL_WARNING |
|
sl@0
|
173 |
G_LOG_LEVEL_CRITICAL);
|
sl@0
|
174 |
g_type_init ();
|
sl@0
|
175 |
|
sl@0
|
176 |
#ifdef __SYMBIAN32__
|
sl@0
|
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);
|
sl@0
|
178 |
#endif /*__SYMBIAN32__*/
|
sl@0
|
179 |
/* Test basic weak reference operation
|
sl@0
|
180 |
*/
|
sl@0
|
181 |
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
|
sl@0
|
182 |
|
sl@0
|
183 |
g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
|
sl@0
|
184 |
|
sl@0
|
185 |
clear_flags ();
|
sl@0
|
186 |
g_object_unref (object);
|
sl@0
|
187 |
g_assert (weak_ref1_notified == TRUE);
|
sl@0
|
188 |
g_assert (object_destroyed == TRUE);
|
sl@0
|
189 |
|
sl@0
|
190 |
/* Test two weak references at once
|
sl@0
|
191 |
*/
|
sl@0
|
192 |
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
|
sl@0
|
193 |
|
sl@0
|
194 |
g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
|
sl@0
|
195 |
g_object_weak_ref (object, weak_ref2, GUINT_TO_POINTER (24));
|
sl@0
|
196 |
|
sl@0
|
197 |
clear_flags ();
|
sl@0
|
198 |
g_object_unref (object);
|
sl@0
|
199 |
g_assert (weak_ref1_notified == TRUE);
|
sl@0
|
200 |
g_assert (weak_ref2_notified == TRUE);
|
sl@0
|
201 |
g_assert (object_destroyed == TRUE);
|
sl@0
|
202 |
|
sl@0
|
203 |
/* Test remove weak references
|
sl@0
|
204 |
*/
|
sl@0
|
205 |
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
|
sl@0
|
206 |
|
sl@0
|
207 |
g_object_weak_ref (object, weak_ref1, GUINT_TO_POINTER (42));
|
sl@0
|
208 |
g_object_weak_ref (object, weak_ref2, GUINT_TO_POINTER (24));
|
sl@0
|
209 |
g_object_weak_unref (object, weak_ref1, GUINT_TO_POINTER (42));
|
sl@0
|
210 |
|
sl@0
|
211 |
clear_flags ();
|
sl@0
|
212 |
g_object_unref (object);
|
sl@0
|
213 |
g_assert (weak_ref1_notified == FALSE);
|
sl@0
|
214 |
g_assert (weak_ref2_notified == TRUE);
|
sl@0
|
215 |
g_assert (object_destroyed == TRUE);
|
sl@0
|
216 |
|
sl@0
|
217 |
/* Test basic toggle reference operation
|
sl@0
|
218 |
*/
|
sl@0
|
219 |
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
|
sl@0
|
220 |
|
sl@0
|
221 |
g_object_add_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
|
sl@0
|
222 |
|
sl@0
|
223 |
clear_flags ();
|
sl@0
|
224 |
g_object_unref (object);
|
sl@0
|
225 |
g_assert (toggle_ref1_weakened == TRUE);
|
sl@0
|
226 |
g_assert (toggle_ref1_strengthened == FALSE);
|
sl@0
|
227 |
g_assert (object_destroyed == FALSE);
|
sl@0
|
228 |
|
sl@0
|
229 |
clear_flags ();
|
sl@0
|
230 |
g_object_ref (object);
|
sl@0
|
231 |
g_assert (toggle_ref1_weakened == FALSE);
|
sl@0
|
232 |
g_assert (toggle_ref1_strengthened == TRUE);
|
sl@0
|
233 |
g_assert (object_destroyed == FALSE);
|
sl@0
|
234 |
|
sl@0
|
235 |
g_object_unref (object);
|
sl@0
|
236 |
|
sl@0
|
237 |
clear_flags ();
|
sl@0
|
238 |
g_object_remove_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
|
sl@0
|
239 |
g_assert (toggle_ref1_weakened == FALSE);
|
sl@0
|
240 |
g_assert (toggle_ref1_strengthened == FALSE);
|
sl@0
|
241 |
g_assert (object_destroyed == TRUE);
|
sl@0
|
242 |
|
sl@0
|
243 |
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
|
sl@0
|
244 |
|
sl@0
|
245 |
/* Test two toggle references at once
|
sl@0
|
246 |
*/
|
sl@0
|
247 |
g_object_add_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
|
sl@0
|
248 |
g_object_add_toggle_ref (object, toggle_ref2, GUINT_TO_POINTER (24));
|
sl@0
|
249 |
|
sl@0
|
250 |
clear_flags ();
|
sl@0
|
251 |
g_object_unref (object);
|
sl@0
|
252 |
g_assert (toggle_ref1_weakened == FALSE);
|
sl@0
|
253 |
g_assert (toggle_ref1_strengthened == FALSE);
|
sl@0
|
254 |
g_assert (toggle_ref2_weakened == FALSE);
|
sl@0
|
255 |
g_assert (toggle_ref2_strengthened == FALSE);
|
sl@0
|
256 |
g_assert (object_destroyed == FALSE);
|
sl@0
|
257 |
|
sl@0
|
258 |
clear_flags ();
|
sl@0
|
259 |
g_object_remove_toggle_ref (object, toggle_ref1, GUINT_TO_POINTER (42));
|
sl@0
|
260 |
g_assert (toggle_ref1_weakened == FALSE);
|
sl@0
|
261 |
g_assert (toggle_ref1_strengthened == FALSE);
|
sl@0
|
262 |
g_assert (toggle_ref2_weakened == TRUE);
|
sl@0
|
263 |
g_assert (toggle_ref2_strengthened == FALSE);
|
sl@0
|
264 |
g_assert (object_destroyed == FALSE);
|
sl@0
|
265 |
|
sl@0
|
266 |
clear_flags ();
|
sl@0
|
267 |
g_object_remove_toggle_ref (object, toggle_ref2, GUINT_TO_POINTER (24));
|
sl@0
|
268 |
g_assert (toggle_ref1_weakened == FALSE);
|
sl@0
|
269 |
g_assert (toggle_ref1_strengthened == FALSE);
|
sl@0
|
270 |
g_assert (toggle_ref2_weakened == FALSE);
|
sl@0
|
271 |
g_assert (toggle_ref2_strengthened == FALSE);
|
sl@0
|
272 |
g_assert (object_destroyed == TRUE);
|
sl@0
|
273 |
|
sl@0
|
274 |
/* Test a toggle reference that removes itself
|
sl@0
|
275 |
*/
|
sl@0
|
276 |
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
|
sl@0
|
277 |
|
sl@0
|
278 |
g_object_add_toggle_ref (object, toggle_ref3, GUINT_TO_POINTER (34));
|
sl@0
|
279 |
|
sl@0
|
280 |
clear_flags ();
|
sl@0
|
281 |
g_object_unref (object);
|
sl@0
|
282 |
g_assert (toggle_ref3_weakened == TRUE);
|
sl@0
|
283 |
g_assert (toggle_ref3_strengthened == FALSE);
|
sl@0
|
284 |
g_assert (object_destroyed == TRUE);
|
sl@0
|
285 |
|
sl@0
|
286 |
#ifdef __SYMBIAN32__
|
sl@0
|
287 |
testResultXml("references");
|
sl@0
|
288 |
#endif /*__SYMBIAN32__*/
|
sl@0
|
289 |
return 0;
|
sl@0
|
290 |
}
|