2003-04-18 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / test / test-utils.c
1 #include "test-utils.h"
2
3 typedef struct
4 {
5   DBusLoop *loop;
6   DBusConnection *connection;
7
8 } CData;
9
10 static dbus_bool_t
11 connection_watch_callback (DBusWatch     *watch,
12                            unsigned int   condition,
13                            void          *data)
14 {
15   return dbus_watch_handle (watch, condition);
16 }
17
18 static dbus_bool_t
19 add_watch (DBusWatch *watch,
20            void      *data)
21 {
22   CData *cd = data;
23
24   return _dbus_loop_add_watch (cd->loop,
25                                watch,
26                                connection_watch_callback,
27                                cd, NULL);
28 }
29
30 static void
31 remove_watch (DBusWatch *watch,
32               void      *data)
33 {
34   CData *cd = data;
35   
36   _dbus_loop_remove_watch (cd->loop,
37                            watch, connection_watch_callback, cd);  
38 }
39
40 static void
41 connection_timeout_callback (DBusTimeout   *timeout,
42                              void          *data)
43 {
44   CData *cd = data;
45
46   /* Can return FALSE on OOM but we just let it fire again later */
47   dbus_timeout_handle (timeout);
48 }
49
50 static dbus_bool_t
51 add_timeout (DBusTimeout *timeout,
52              void        *data)
53 {
54   CData *cd = data;
55
56   return _dbus_loop_add_timeout (cd->loop,
57                                  timeout, connection_timeout_callback, cd, NULL);
58 }
59
60 static void
61 remove_timeout (DBusTimeout *timeout,
62                 void        *data)
63 {
64   CData *cd = data;
65
66   _dbus_loop_remove_timeout (cd->loop,
67                              timeout, connection_timeout_callback, cd);
68 }
69
70 static void
71 dispatch_status_function (DBusConnection    *connection,
72                           DBusDispatchStatus new_status,
73                           void              *data)
74 {
75   DBusLoop *loop = data;
76   
77   if (new_status != DBUS_DISPATCH_COMPLETE)
78     {
79       while (!_dbus_loop_queue_dispatch (loop, connection))
80         _dbus_wait_for_memory ();
81     }
82 }
83
84 static void
85 cdata_free (void *data)
86 {
87   CData *cd = data;
88
89   dbus_connection_unref (cd->connection);
90   _dbus_loop_unref (cd->loop);
91   
92   dbus_free (cd);
93 }
94
95 static CData*
96 cdata_new (DBusLoop       *loop,
97            DBusConnection *connection)
98 {
99   CData *cd;
100
101   cd = dbus_new0 (CData, 1);
102   if (cd == NULL)
103     return NULL;
104
105   cd->loop = loop;
106   cd->connection = connection;
107
108   dbus_connection_ref (cd->connection);
109   _dbus_loop_ref (cd->loop);
110
111   return cd;
112 }
113
114 dbus_bool_t
115 test_connection_setup (DBusLoop       *loop,
116                        DBusConnection *connection)
117 {
118   CData *cd;
119
120   cd = NULL;
121   
122   dbus_connection_set_dispatch_status_function (connection, dispatch_status_function,
123                                                 loop, NULL);
124   
125   cd = cdata_new (loop, connection);
126   if (cd == NULL)
127     goto nomem;
128
129   /* Because dbus-mainloop.c checks dbus_timeout_get_enabled(),
130    * dbus_watch_get_enabled() directly, we don't have to provide
131    * "toggled" callbacks.
132    */
133   
134   if (!dbus_connection_set_watch_functions (connection,
135                                             add_watch,
136                                             remove_watch,
137                                             NULL,
138                                             cd, cdata_free))
139     goto nomem;
140
141
142   cd = cdata_new (loop, connection);
143   if (cd == NULL)
144     goto nomem;
145   
146   if (!dbus_connection_set_timeout_functions (connection,
147                                               add_timeout,
148                                               remove_timeout,
149                                               NULL,
150                                               cd, cdata_free))
151     goto nomem;
152
153   if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
154     {
155       if (!_dbus_loop_queue_dispatch (loop, connection))
156         goto nomem;
157     }
158   
159   return TRUE;
160   
161  nomem:
162   if (cd)
163     cdata_free (cd);
164   
165   dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
166   dbus_connection_set_watch_functions (connection, NULL, NULL, NULL, NULL, NULL);
167   dbus_connection_set_timeout_functions (connection, NULL, NULL, NULL, NULL, NULL);
168   
169   return FALSE;
170 }
171
172 void
173 test_connection_shutdown (DBusLoop       *loop,
174                           DBusConnection *connection)
175 {
176   if (!dbus_connection_set_watch_functions (connection,
177                                             NULL,
178                                             NULL,
179                                             NULL,
180                                             NULL, NULL))
181     _dbus_assert_not_reached ("setting watch functions to NULL failed");
182   
183   if (!dbus_connection_set_timeout_functions (connection,
184                                               NULL,
185                                               NULL,
186                                               NULL,
187                                               NULL, NULL))
188     _dbus_assert_not_reached ("setting timeout functions to NULL failed");
189
190   dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
191 }