Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / tests / simple-async-result.c
1 /*
2  * Copyright © 2009 Ryan Lortie
3  *
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.
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 static GObject      *got_source;
18 static GAsyncResult *got_result;
19 static gpointer      got_user_data;
20
21 static void
22 ensure_destroyed (gpointer obj)
23 {
24   g_object_add_weak_pointer (obj, &obj);
25   g_object_unref (obj);
26   g_assert (obj == NULL);
27 }
28
29 static void
30 reset (void)
31 {
32   got_source = NULL;
33
34   if (got_result)
35     ensure_destroyed (got_result);
36
37   got_result = NULL;
38   got_user_data = NULL;
39 }
40
41 static void
42 check (gpointer a, gpointer b, gpointer c)
43 {
44   g_assert (a == got_source);
45   g_assert (b == got_result);
46   g_assert (c == got_user_data);
47 }
48
49 static void
50 callback_func (GObject      *source,
51                GAsyncResult *result,
52                gpointer      user_data)
53 {
54   got_source = source;
55   got_result = g_object_ref (result);
56   got_user_data = user_data;
57 }
58
59 static gboolean
60 test_simple_async_idle (gpointer user_data)
61 {
62   GSimpleAsyncResult *result;
63   GObject *a, *b, *c;
64   gboolean *ran = user_data;
65
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);
69
70   result = g_simple_async_result_new (a, callback_func, b, test_simple_async_idle);
71   check (NULL, NULL, NULL);
72   g_simple_async_result_complete (result);
73   check (a, result, b);
74   g_object_unref (result);
75
76   g_assert (g_simple_async_result_is_valid (got_result, a, test_simple_async_idle));
77   g_assert (!g_simple_async_result_is_valid (got_result, b, test_simple_async_idle));
78   g_assert (!g_simple_async_result_is_valid (got_result, c, test_simple_async_idle));
79   g_assert (!g_simple_async_result_is_valid (got_result, b, callback_func));
80   g_assert (!g_simple_async_result_is_valid ((gpointer) a, NULL, NULL));
81   reset ();
82   reset ();
83   reset ();
84
85   ensure_destroyed (a);
86   ensure_destroyed (b);
87   ensure_destroyed (c);
88
89   *ran = TRUE;
90   return FALSE;
91 }
92
93 static void
94 test_simple_async (void)
95 {
96   GSimpleAsyncResult *result;
97   GObject *a, *b;
98   gboolean ran_test_in_idle = FALSE;
99
100   g_idle_add (test_simple_async_idle, &ran_test_in_idle);
101   g_main_context_iteration (NULL, FALSE);
102
103   g_assert (ran_test_in_idle);
104
105   a = g_object_new (G_TYPE_OBJECT, NULL);
106   b = g_object_new (G_TYPE_OBJECT, NULL);
107
108   result = g_simple_async_result_new (a, callback_func, b, test_simple_async);
109   check (NULL, NULL, NULL);
110   g_simple_async_result_complete_in_idle (result);
111   g_object_unref (result);
112   check (NULL, NULL, NULL);
113   g_main_context_iteration (NULL, FALSE);
114   check (a, result, b);
115   reset ();
116
117   ensure_destroyed (a);
118   ensure_destroyed (b);
119 }
120
121 int
122 main (int argc, char **argv)
123 {
124   g_type_init ();
125   g_test_init (&argc, &argv, NULL);
126
127   g_test_add_func ("/gio/simple-async-result/test", test_simple_async);
128
129   return g_test_run();
130 }