Add some more cases to the app-id unit tests
[glib.git] / gio / tests / simple-async-result.c
blobe9396dce1a5f84b695572d4af99c8f60aa872d44
1 /*
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>
13 #include <gio/gio.h>
14 #include <stdlib.h>
15 #include <string.h>
17 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
19 static GObject *got_source;
20 static GAsyncResult *got_result;
21 static gpointer got_user_data;
23 static void
24 ensure_destroyed (gpointer obj)
26 g_object_add_weak_pointer (obj, &obj);
27 g_object_unref (obj);
28 g_assert (obj == NULL);
31 static void
32 reset (void)
34 got_source = NULL;
36 if (got_result)
37 ensure_destroyed (got_result);
39 got_result = NULL;
40 got_user_data = NULL;
43 static void
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);
51 static void
52 callback_func (GObject *source,
53 GAsyncResult *result,
54 gpointer user_data)
56 got_source = source;
57 got_result = g_object_ref (result);
58 got_user_data = user_data;
61 static gboolean
62 test_simple_async_idle (gpointer user_data)
64 GSimpleAsyncResult *result;
65 GObject *a, *b, *c;
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);
76 check (a, result, b);
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));
84 reset ();
85 reset ();
86 reset ();
88 ensure_destroyed (a);
89 ensure_destroyed (b);
90 ensure_destroyed (c);
92 *ran = TRUE;
93 return G_SOURCE_REMOVE;
96 static void
97 test_simple_async (void)
99 GSimpleAsyncResult *result;
100 GObject *a, *b;
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);
118 reset ();
120 ensure_destroyed (a);
121 ensure_destroyed (b);
124 static void
125 test_valid (void)
127 GAsyncResult *result;
128 GObject *a, *b;
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);
184 g_object_unref (a);
185 g_object_unref (b);
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);
196 return g_test_run();
199 G_GNUC_END_IGNORE_DEPRECATIONS