2 * Copyright © 2009 Ryan Lortie
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.1 of the License, or (at your option) any later version.
9 * See the included COPYING file for more information.
12 #include <glib/glib.h>
17 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
19 static GObject
*got_source
;
20 static GAsyncResult
*got_result
;
21 static gpointer got_user_data
;
24 ensure_destroyed (gpointer obj
)
26 g_object_add_weak_pointer (obj
, &obj
);
28 g_assert (obj
== NULL
);
37 ensure_destroyed (got_result
);
44 check (gpointer a
, gpointer b
, gpointer c
)
46 g_assert (a
== got_source
);
47 g_assert (b
== got_result
);
48 g_assert (c
== got_user_data
);
52 callback_func (GObject
*source
,
57 got_result
= g_object_ref (result
);
58 got_user_data
= user_data
;
62 test_simple_async_idle (gpointer user_data
)
64 GSimpleAsyncResult
*result
;
66 gboolean
*ran
= user_data
;
68 a
= g_object_new (G_TYPE_OBJECT
, NULL
);
69 b
= g_object_new (G_TYPE_OBJECT
, NULL
);
70 c
= g_object_new (G_TYPE_OBJECT
, NULL
);
72 result
= g_simple_async_result_new (a
, callback_func
, b
, test_simple_async_idle
);
73 g_assert (g_async_result_get_user_data (G_ASYNC_RESULT (result
)) == b
);
74 check (NULL
, NULL
, NULL
);
75 g_simple_async_result_complete (result
);
77 g_object_unref (result
);
79 g_assert (g_simple_async_result_is_valid (got_result
, a
, test_simple_async_idle
));
80 g_assert (!g_simple_async_result_is_valid (got_result
, b
, test_simple_async_idle
));
81 g_assert (!g_simple_async_result_is_valid (got_result
, c
, test_simple_async_idle
));
82 g_assert (!g_simple_async_result_is_valid (got_result
, b
, callback_func
));
83 g_assert (!g_simple_async_result_is_valid ((gpointer
) a
, NULL
, NULL
));
93 return G_SOURCE_REMOVE
;
97 test_simple_async (void)
99 GSimpleAsyncResult
*result
;
101 gboolean ran_test_in_idle
= FALSE
;
103 g_idle_add (test_simple_async_idle
, &ran_test_in_idle
);
104 g_main_context_iteration (NULL
, FALSE
);
106 g_assert (ran_test_in_idle
);
108 a
= g_object_new (G_TYPE_OBJECT
, NULL
);
109 b
= g_object_new (G_TYPE_OBJECT
, NULL
);
111 result
= g_simple_async_result_new (a
, callback_func
, b
, test_simple_async
);
112 check (NULL
, NULL
, NULL
);
113 g_simple_async_result_complete_in_idle (result
);
114 g_object_unref (result
);
115 check (NULL
, NULL
, NULL
);
116 g_main_context_iteration (NULL
, FALSE
);
117 check (a
, result
, b
);
120 ensure_destroyed (a
);
121 ensure_destroyed (b
);
127 GAsyncResult
*result
;
130 a
= g_object_new (G_TYPE_OBJECT
, NULL
);
131 b
= g_object_new (G_TYPE_OBJECT
, NULL
);
133 /* Without source or tag */
134 result
= (GAsyncResult
*) g_simple_async_result_new (NULL
, NULL
, NULL
, NULL
);
135 g_assert_true (g_simple_async_result_is_valid (result
, NULL
, NULL
));
136 g_assert_true (g_simple_async_result_is_valid (result
, NULL
, test_valid
));
137 g_assert_true (g_simple_async_result_is_valid (result
, NULL
, test_simple_async
));
138 g_assert_false (g_simple_async_result_is_valid (result
, a
, NULL
));
139 g_assert_false (g_simple_async_result_is_valid (result
, a
, test_valid
));
140 g_assert_false (g_simple_async_result_is_valid (result
, a
, test_simple_async
));
141 g_object_unref (result
);
143 /* Without source, with tag */
144 result
= (GAsyncResult
*) g_simple_async_result_new (NULL
, NULL
, NULL
, test_valid
);
145 g_assert_true (g_simple_async_result_is_valid (result
, NULL
, NULL
));
146 g_assert_true (g_simple_async_result_is_valid (result
, NULL
, test_valid
));
147 g_assert_false (g_simple_async_result_is_valid (result
, NULL
, test_simple_async
));
148 g_assert_false (g_simple_async_result_is_valid (result
, a
, NULL
));
149 g_assert_false (g_simple_async_result_is_valid (result
, a
, test_valid
));
150 g_assert_false (g_simple_async_result_is_valid (result
, a
, test_simple_async
));
151 g_object_unref (result
);
153 /* With source, without tag */
154 result
= (GAsyncResult
*) g_simple_async_result_new (a
, NULL
, NULL
, NULL
);
155 g_assert_true (g_simple_async_result_is_valid (result
, a
, NULL
));
156 g_assert_true (g_simple_async_result_is_valid (result
, a
, test_valid
));
157 g_assert_true (g_simple_async_result_is_valid (result
, a
, test_simple_async
));
158 g_assert_false (g_simple_async_result_is_valid (result
, NULL
, NULL
));
159 g_assert_false (g_simple_async_result_is_valid (result
, NULL
, test_valid
));
160 g_assert_false (g_simple_async_result_is_valid (result
, NULL
, test_simple_async
));
161 g_assert_false (g_simple_async_result_is_valid (result
, b
, NULL
));
162 g_assert_false (g_simple_async_result_is_valid (result
, b
, test_valid
));
163 g_assert_false (g_simple_async_result_is_valid (result
, b
, test_simple_async
));
164 g_object_unref (result
);
166 /* With source and tag */
167 result
= (GAsyncResult
*) g_simple_async_result_new (a
, NULL
, NULL
, test_valid
);
168 g_assert_true (g_simple_async_result_is_valid (result
, a
, test_valid
));
169 g_assert_true (g_simple_async_result_is_valid (result
, a
, NULL
));
170 g_assert_false (g_simple_async_result_is_valid (result
, a
, test_simple_async
));
171 g_assert_false (g_simple_async_result_is_valid (result
, NULL
, NULL
));
172 g_assert_false (g_simple_async_result_is_valid (result
, NULL
, test_valid
));
173 g_assert_false (g_simple_async_result_is_valid (result
, NULL
, test_simple_async
));
174 g_assert_false (g_simple_async_result_is_valid (result
, b
, NULL
));
175 g_assert_false (g_simple_async_result_is_valid (result
, b
, test_valid
));
176 g_assert_false (g_simple_async_result_is_valid (result
, b
, test_simple_async
));
177 g_object_unref (result
);
179 /* Non-GSimpleAsyncResult */
180 result
= (GAsyncResult
*) g_task_new (NULL
, NULL
, NULL
, NULL
);
181 g_assert_false (g_simple_async_result_is_valid (result
, NULL
, NULL
));
182 g_object_unref (result
);
189 main (int argc
, char **argv
)
191 g_test_init (&argc
, &argv
, NULL
);
193 g_test_add_func ("/gio/simple-async-result/test", test_simple_async
);
194 g_test_add_func ("/gio/simple-async-result/valid", test_valid
);
199 G_GNUC_END_IGNORE_DEPRECATIONS