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