1 /*
2 * Copyright © 2009 Ryan Lortie
3 *
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.
8 *
9 * See the included COPYING file for more information.
10 */
11
12 #include <glib/glib.h>
13 #include <gio/gio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
18
19 static GObject *got_source;
20 static GAsyncResult *got_result;
21 static gpointer got_user_data;
22
23 static void
ensure_destroyed(gpointer obj)24 ensure_destroyed (gpointer obj)
25 {
26 g_object_add_weak_pointer (obj, &obj);
27 g_object_unref (obj);
28 g_assert (obj == NULL);
29 }
30
31 static void
reset(void)32 reset (void)
33 {
34 got_source = NULL;
35
36 if (got_result)
37 ensure_destroyed (got_result);
38
39 got_result = NULL;
40 got_user_data = NULL;
41 }
42
43 static void
check(gpointer a,gpointer b,gpointer c)44 check (gpointer a, gpointer b, gpointer c)
45 {
46 g_assert (a == got_source);
47 g_assert (b == got_result);
48 g_assert (c == got_user_data);
49 }
50
51 static void
callback_func(GObject * source,GAsyncResult * result,gpointer user_data)52 callback_func (GObject *source,
53 GAsyncResult *result,
54 gpointer user_data)
55 {
56 got_source = source;
57 got_result = g_object_ref (result);
58 got_user_data = user_data;
59 }
60
61 static gboolean
test_simple_async_idle(gpointer user_data)62 test_simple_async_idle (gpointer user_data)
63 {
64 GSimpleAsyncResult *result;
65 GObject *a, *b, *c;
66 gboolean *ran = user_data;
67
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);
71
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);
76 check (a, result, b);
77 g_object_unref (result);
78
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));
84 reset ();
85 reset ();
86 reset ();
87
88 ensure_destroyed (a);
89 ensure_destroyed (b);
90 ensure_destroyed (c);
91
92 *ran = TRUE;
93 return G_SOURCE_REMOVE;
94 }
95
96 static void
test_simple_async(void)97 test_simple_async (void)
98 {
99 GSimpleAsyncResult *result;
100 GObject *a, *b;
101 gboolean ran_test_in_idle = FALSE;
102
103 g_idle_add (test_simple_async_idle, &ran_test_in_idle);
104 g_main_context_iteration (NULL, FALSE);
105
106 g_assert (ran_test_in_idle);
107
108 a = g_object_new (G_TYPE_OBJECT, NULL);
109 b = g_object_new (G_TYPE_OBJECT, NULL);
110
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);
118 reset ();
119
120 ensure_destroyed (a);
121 ensure_destroyed (b);
122 }
123
124 static void
test_valid(void)125 test_valid (void)
126 {
127 GAsyncResult *result;
128 GObject *a, *b;
129
130 a = g_object_new (G_TYPE_OBJECT, NULL);
131 b = g_object_new (G_TYPE_OBJECT, NULL);
132
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);
142
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);
152
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);
165
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);
178
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);
183
184 g_object_unref (a);
185 g_object_unref (b);
186 }
187
188 int
main(int argc,char ** argv)189 main (int argc, char **argv)
190 {
191 g_test_init (&argc, &argv, NULL);
192
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);
195
196 return g_test_run();
197 }
198
199 G_GNUC_END_IGNORE_DEPRECATIONS
200