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