2 * Copyright © 2009 Ryan Lortie
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2 of the licence or (at
7 * your option) any later version.
9 * See the included COPYING file for more information.
12 #include <glib/glib.h>
17 static GObject *got_source;
18 static GAsyncResult *got_result;
19 static gpointer got_user_data;
22 ensure_destroyed (gpointer obj)
24 g_object_add_weak_pointer (obj, &obj);
26 g_assert (obj == NULL);
35 ensure_destroyed (got_result);
42 check (gpointer a, gpointer b, gpointer c)
44 g_assert (a == got_source);
45 g_assert (b == got_result);
46 g_assert (c == got_user_data);
50 callback_func (GObject *source,
55 got_result = g_object_ref (result);
56 got_user_data = user_data;
60 test_simple_async_idle (gpointer user_data)
62 GSimpleAsyncResult *result;
64 gboolean *ran = user_data;
66 a = g_object_new (G_TYPE_OBJECT, NULL);
67 b = g_object_new (G_TYPE_OBJECT, NULL);
68 c = g_object_new (G_TYPE_OBJECT, NULL);
70 result = g_simple_async_result_new (a, callback_func, b, test_simple_async_idle);
71 g_assert (g_async_result_get_user_data (G_ASYNC_RESULT (result)) == b);
72 check (NULL, NULL, NULL);
73 g_simple_async_result_complete (result);
75 g_object_unref (result);
77 g_assert (g_simple_async_result_is_valid (got_result, a, test_simple_async_idle));
78 g_assert (!g_simple_async_result_is_valid (got_result, b, test_simple_async_idle));
79 g_assert (!g_simple_async_result_is_valid (got_result, c, test_simple_async_idle));
80 g_assert (!g_simple_async_result_is_valid (got_result, b, callback_func));
81 g_assert (!g_simple_async_result_is_valid ((gpointer) a, NULL, NULL));
91 return G_SOURCE_REMOVE;
95 test_simple_async (void)
97 GSimpleAsyncResult *result;
99 gboolean ran_test_in_idle = FALSE;
101 g_idle_add (test_simple_async_idle, &ran_test_in_idle);
102 g_main_context_iteration (NULL, FALSE);
104 g_assert (ran_test_in_idle);
106 a = g_object_new (G_TYPE_OBJECT, NULL);
107 b = g_object_new (G_TYPE_OBJECT, NULL);
109 result = g_simple_async_result_new (a, callback_func, b, test_simple_async);
110 check (NULL, NULL, NULL);
111 g_simple_async_result_complete_in_idle (result);
112 g_object_unref (result);
113 check (NULL, NULL, NULL);
114 g_main_context_iteration (NULL, FALSE);
115 check (a, result, b);
118 ensure_destroyed (a);
119 ensure_destroyed (b);
125 GAsyncResult *result;
128 a = g_object_new (G_TYPE_OBJECT, NULL);
129 b = g_object_new (G_TYPE_OBJECT, NULL);
131 /* Without source or tag */
132 result = (GAsyncResult *) g_simple_async_result_new (NULL, NULL, NULL, NULL);
133 g_assert_true (g_simple_async_result_is_valid (result, NULL, NULL));
134 g_assert_true (g_simple_async_result_is_valid (result, NULL, test_valid));
135 g_assert_true (g_simple_async_result_is_valid (result, NULL, test_simple_async));
136 g_assert_false (g_simple_async_result_is_valid (result, a, NULL));
137 g_assert_false (g_simple_async_result_is_valid (result, a, test_valid));
138 g_assert_false (g_simple_async_result_is_valid (result, a, test_simple_async));
139 g_object_unref (result);
141 /* Without source, with tag */
142 result = (GAsyncResult *) g_simple_async_result_new (NULL, NULL, NULL, test_valid);
143 g_assert_true (g_simple_async_result_is_valid (result, NULL, NULL));
144 g_assert_true (g_simple_async_result_is_valid (result, NULL, test_valid));
145 g_assert_false (g_simple_async_result_is_valid (result, NULL, test_simple_async));
146 g_assert_false (g_simple_async_result_is_valid (result, a, NULL));
147 g_assert_false (g_simple_async_result_is_valid (result, a, test_valid));
148 g_assert_false (g_simple_async_result_is_valid (result, a, test_simple_async));
149 g_object_unref (result);
151 /* With source, without tag */
152 result = (GAsyncResult *) g_simple_async_result_new (a, NULL, NULL, NULL);
153 g_assert_true (g_simple_async_result_is_valid (result, a, NULL));
154 g_assert_true (g_simple_async_result_is_valid (result, a, test_valid));
155 g_assert_true (g_simple_async_result_is_valid (result, a, test_simple_async));
156 g_assert_false (g_simple_async_result_is_valid (result, NULL, NULL));
157 g_assert_false (g_simple_async_result_is_valid (result, NULL, test_valid));
158 g_assert_false (g_simple_async_result_is_valid (result, NULL, test_simple_async));
159 g_assert_false (g_simple_async_result_is_valid (result, b, NULL));
160 g_assert_false (g_simple_async_result_is_valid (result, b, test_valid));
161 g_assert_false (g_simple_async_result_is_valid (result, b, test_simple_async));
162 g_object_unref (result);
164 /* With source and tag */
165 result = (GAsyncResult *) g_simple_async_result_new (a, NULL, NULL, test_valid);
166 g_assert_true (g_simple_async_result_is_valid (result, a, test_valid));
167 g_assert_true (g_simple_async_result_is_valid (result, a, NULL));
168 g_assert_false (g_simple_async_result_is_valid (result, a, test_simple_async));
169 g_assert_false (g_simple_async_result_is_valid (result, NULL, NULL));
170 g_assert_false (g_simple_async_result_is_valid (result, NULL, test_valid));
171 g_assert_false (g_simple_async_result_is_valid (result, NULL, test_simple_async));
172 g_assert_false (g_simple_async_result_is_valid (result, b, NULL));
173 g_assert_false (g_simple_async_result_is_valid (result, b, test_valid));
174 g_assert_false (g_simple_async_result_is_valid (result, b, test_simple_async));
175 g_object_unref (result);
177 /* Non-GSimpleAsyncResult */
178 result = (GAsyncResult *) g_task_new (NULL, NULL, NULL, NULL);
179 g_assert_false (g_simple_async_result_is_valid (result, NULL, NULL));
180 g_object_unref (result);
187 main (int argc, char **argv)
189 g_test_init (&argc, &argv, NULL);
191 g_test_add_func ("/gio/simple-async-result/test", test_simple_async);
192 g_test_add_func ("/gio/simple-async-result/valid", test_valid);