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