[daemon-fix] Registering starters: unwanted release_kdbus_name when no error was...
[platform/upstream/dbus.git] / test / test-utils.c
index 4fd84fe..9a4f358 100644 (file)
@@ -1,6 +1,13 @@
 #include <config.h>
 #include "test-utils.h"
 
+#ifndef DBUS_TEST_USE_INTERNAL
+# include <dbus/dbus.h>
+# include <dbus/dbus-glib-lowlevel.h>
+#endif
+
+#ifdef DBUS_TEST_USE_INTERNAL
+
 typedef struct
 {
   DBusLoop *loop;
@@ -26,6 +33,15 @@ remove_watch (DBusWatch *watch,
   _dbus_loop_remove_watch (cd->loop, watch);
 }
 
+static void
+toggle_watch (DBusWatch  *watch,
+              void       *data)
+{
+  CData *cd = data;
+
+  _dbus_loop_toggle_watch (cd->loop, watch);
+}
+
 static dbus_bool_t
 add_timeout (DBusTimeout *timeout,
             void        *data)
@@ -88,10 +104,14 @@ cdata_new (DBusLoop       *loop,
   return cd;
 }
 
+#endif /* DBUS_TEST_USE_INTERNAL */
+
 dbus_bool_t
-test_connection_setup (DBusLoop       *loop,
+test_connection_setup (TestMainContext *ctx,
                        DBusConnection *connection)
 {
+#ifdef DBUS_TEST_USE_INTERNAL
+  DBusLoop *loop = ctx;
   CData *cd;
 
   cd = NULL;
@@ -103,15 +123,10 @@ test_connection_setup (DBusLoop       *loop,
   if (cd == NULL)
     goto nomem;
 
-  /* Because dbus-mainloop.c checks dbus_timeout_get_enabled(),
-   * dbus_watch_get_enabled() directly, we don't have to provide
-   * "toggled" callbacks.
-   */
-  
   if (!dbus_connection_set_watch_functions (connection,
                                             add_watch,
                                             remove_watch,
-                                            NULL,
+                                            toggle_watch,
                                             cd, cdata_free))
     goto nomem;
 
@@ -144,10 +159,23 @@ test_connection_setup (DBusLoop       *loop,
   dbus_connection_set_timeout_functions (connection, NULL, NULL, NULL, NULL, NULL);
   
   return FALSE;
+#else /* !DBUS_TEST_USE_INTERNAL */
+
+  dbus_connection_setup_with_g_main (connection, ctx);
+  return TRUE;
+
+#endif /* !DBUS_TEST_USE_INTERNAL */
+}
+
+static void
+die (const char *message)
+{
+  fprintf (stderr, "*** %s", message);
+  exit (1);
 }
 
 void
-test_connection_shutdown (DBusLoop       *loop,
+test_connection_shutdown (TestMainContext *ctx,
                           DBusConnection *connection)
 {
   if (!dbus_connection_set_watch_functions (connection,
@@ -155,18 +183,20 @@ test_connection_shutdown (DBusLoop       *loop,
                                             NULL,
                                             NULL,
                                             NULL, NULL))
-    _dbus_assert_not_reached ("setting watch functions to NULL failed");
+    die ("setting watch functions to NULL failed");
   
   if (!dbus_connection_set_timeout_functions (connection,
                                               NULL,
                                               NULL,
                                               NULL,
                                               NULL, NULL))
-    _dbus_assert_not_reached ("setting timeout functions to NULL failed");
+    die ("setting timeout functions to NULL failed");
 
   dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
 }
 
+#ifdef DBUS_TEST_USE_INTERNAL
+
 typedef struct
 {
   DBusLoop *loop;
@@ -213,6 +243,15 @@ add_server_watch (DBusWatch  *watch,
 }
 
 static void
+toggle_server_watch (DBusWatch  *watch,
+                     void       *data)
+{
+  ServerData *context = data;
+
+  _dbus_loop_toggle_watch (context->loop, watch);
+}
+
+static void
 remove_server_watch (DBusWatch  *watch,
                      void       *data)
 {
@@ -239,10 +278,14 @@ remove_server_timeout (DBusTimeout *timeout,
   _dbus_loop_remove_timeout (context->loop, timeout);
 }
 
+#endif /* DBUS_TEST_USE_INTERNAL */
+
 dbus_bool_t
-test_server_setup (DBusLoop      *loop,
+test_server_setup (TestMainContext *ctx,
                    DBusServer    *server)
 {
+#ifdef DBUS_TEST_USE_INTERNAL
+  DBusLoop *loop = ctx;
   ServerData *sd;
 
   sd = serverdata_new (loop, server);
@@ -252,7 +295,7 @@ test_server_setup (DBusLoop      *loop,
   if (!dbus_server_set_watch_functions (server,
                                         add_server_watch,
                                         remove_server_watch,
-                                        NULL,
+                                        toggle_server_watch,
                                         sd,
                                         serverdata_free))
     {
@@ -280,10 +323,17 @@ test_server_setup (DBusLoop      *loop,
   test_server_shutdown (loop, server);
   
   return FALSE;
+
+#else /* !DBUS_TEST_USE_INTERNAL */
+
+  dbus_server_setup_with_g_main (server, ctx);
+  return TRUE;
+
+#endif /* !DBUS_TEST_USE_INTERNAL */
 }
 
 void
-test_server_shutdown (DBusLoop         *loop,
+test_server_shutdown (TestMainContext  *ctx,
                       DBusServer       *server)
 {
   dbus_server_disconnect (server);
@@ -292,11 +342,51 @@ test_server_shutdown (DBusLoop         *loop,
                                         NULL, NULL, NULL,
                                         NULL,
                                         NULL))
-    _dbus_assert_not_reached ("setting watch functions to NULL failed");
+    die ("setting watch functions to NULL failed");
   
   if (!dbus_server_set_timeout_functions (server,
                                           NULL, NULL, NULL,
                                           NULL,
                                           NULL))
-    _dbus_assert_not_reached ("setting timeout functions to NULL failed");  
+    die ("setting timeout functions to NULL failed");
+}
+
+TestMainContext *
+test_main_context_get (void)
+{
+#ifdef DBUS_TEST_USE_INTERNAL
+  return _dbus_loop_new ();
+#else
+  /* I suspect dbus-glib relies the default main context in some places */
+  return g_main_context_ref (g_main_context_default ());
+#endif
+}
+
+TestMainContext *
+test_main_context_ref (TestMainContext *ctx)
+{
+#ifdef DBUS_TEST_USE_INTERNAL
+  return _dbus_loop_ref (ctx);
+#else
+  return g_main_context_ref (ctx);
+#endif
+}
+
+void test_main_context_unref (TestMainContext *ctx)
+{
+#ifdef DBUS_TEST_USE_INTERNAL
+  _dbus_loop_unref (ctx);
+#else
+  g_main_context_unref (ctx);
+#endif
+}
+
+void test_main_context_iterate (TestMainContext *ctx,
+                                dbus_bool_t      may_block)
+{
+#ifdef DBUS_TEST_USE_INTERNAL
+  _dbus_loop_iterate (ctx, may_block);
+#else
+  g_main_context_iteration (ctx, may_block);
+#endif
 }