summaryrefslogtreecommitdiff
path: root/test/test-utils.c
diff options
context:
space:
mode:
authorSimon McVittie <simon.mcvittie@collabora.co.uk>2013-09-02 16:32:31 +0100
committerSimon McVittie <simon.mcvittie@collabora.co.uk>2013-09-03 12:00:51 +0100
commit30e7a81302a60adc4ec497deee3c0dfe8ec1123e (patch)
tree62c090f2ad018868dd0115d7bbbe1d544d72143c /test/test-utils.c
parentf17fd1cc4e8d69f92986b6f452618c6c1b9819dc (diff)
downloaddbus-30e7a81302a60adc4ec497deee3c0dfe8ec1123e.tar.gz
Tests: allow dbus-glib to be replaced with use of libdbus-internal
We only use dbus-glib for its main loop; within dbus, DBusLoop is available as an alternative, although it isn't thread-safe and isn't public API. For tests that otherwise only use libdbus public API, it's desirable to be able to avoid DBusLoop, so we can run them against an installed libdbus as an integration test. However, if we don't have dbus-glib, we're going to have to use an in-tree main loop, which might as well be DBusLoop. The major disadvantage of using dbus-glib is that it isn't safe to link both dbus-1 and dbus-internal at the same time. This is awkward for a future test case that wants to use _dbus_getsid() in dbus-daemon.c, but only on Windows (fd.o #54445). If we use the same API wrapper around both dbus-glib and DBusLoop, we can compile that test against dbus-glib or against DBusLoop, depending on the platform. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68852 Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
Diffstat (limited to 'test/test-utils.c')
-rw-r--r--test/test-utils.c93
1 files changed, 85 insertions, 8 deletions
diff --git a/test/test-utils.c b/test/test-utils.c
index c3c3ed34..9a4f3584 100644
--- a/test/test-utils.c
+++ b/test/test-utils.c
@@ -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;
@@ -97,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;
@@ -148,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,
@@ -159,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;
@@ -252,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);
@@ -293,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);
@@ -305,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
}