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
17G_GNUC_BEGIN_IGNORE_DEPRECATIONS
18
19static GObject *got_source;
20static GAsyncResult *got_result;
21static gpointer got_user_data;
22
23static void
24ensure_destroyed (gpointer obj)
25{
26 g_object_add_weak_pointer (object: obj, weak_pointer_location: &obj);
27 g_object_unref (object: obj);
28 g_assert (obj == NULL);
29}
30
31static void
32reset (void)
33{
34 got_source = NULL;
35
36 if (got_result)
37 ensure_destroyed (obj: got_result);
38
39 got_result = NULL;
40 got_user_data = NULL;
41}
42
43static void
44check (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
51static void
52callback_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
61static gboolean
62test_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 (source_object: a, callback: callback_func, user_data: b, source_tag: 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 (simple: result);
76 check (a, b: result, c: b);
77 g_object_unref (object: 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 (obj: a);
89 ensure_destroyed (obj: b);
90 ensure_destroyed (obj: c);
91
92 *ran = TRUE;
93 return G_SOURCE_REMOVE;
94}
95
96static void
97test_simple_async (void)
98{
99 GSimpleAsyncResult *result;
100 GObject *a, *b;
101 gboolean ran_test_in_idle = FALSE;
102
103 g_idle_add (function: test_simple_async_idle, data: &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 (source_object: a, callback: callback_func, user_data: b, source_tag: test_simple_async);
112 check (NULL, NULL, NULL);
113 g_simple_async_result_complete_in_idle (simple: result);
114 g_object_unref (object: result);
115 check (NULL, NULL, NULL);
116 g_main_context_iteration (NULL, FALSE);
117 check (a, b: result, c: b);
118 reset ();
119
120 ensure_destroyed (obj: a);
121 ensure_destroyed (obj: b);
122}
123
124static void
125test_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 (object: result);
142
143 /* Without source, with tag */
144 result = (GAsyncResult *) g_simple_async_result_new (NULL, NULL, NULL, source_tag: 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 (object: result);
152
153 /* With source, without tag */
154 result = (GAsyncResult *) g_simple_async_result_new (source_object: 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 (object: result);
165
166 /* With source and tag */
167 result = (GAsyncResult *) g_simple_async_result_new (source_object: a, NULL, NULL, source_tag: 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 (object: 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 (object: result);
183
184 g_object_unref (object: a);
185 g_object_unref (object: b);
186}
187
188int
189main (int argc, char **argv)
190{
191 g_test_init (argc: &argc, argv: &argv, NULL);
192
193 g_test_add_func (testpath: "/gio/simple-async-result/test", test_func: test_simple_async);
194 g_test_add_func (testpath: "/gio/simple-async-result/valid", test_func: test_valid);
195
196 return g_test_run();
197}
198
199G_GNUC_END_IGNORE_DEPRECATIONS
200

source code of gtk/subprojects/glib/gio/tests/simple-async-result.c