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