From 698618bce525913fc4ac1bc9d652738b73106344 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 5 Mar 2012 17:48:37 +0000 Subject: [PATCH] Rewrite unsupported-interface to be suitable for the 'next' branch TpDBusDaemon doesn't have optionally-supported interfaces after the removal of Telepathy Properties, so we have to use TpConnection. I took the opportunity to modernize the test and use GTest. --- tests/dbus/unsupported-interface.c | 333 +++++++++++++++++++++++++----------- 1 files changed, 237 insertions(+), 96 deletions(-) diff --git a/tests/dbus/unsupported-interface.c b/tests/dbus/unsupported-interface.c index 6574684..56a2d30 100644 --- a/tests/dbus/unsupported-interface.c +++ b/tests/dbus/unsupported-interface.c @@ -1,129 +1,270 @@ +/* Regression test for unsupported interfaces on objects. + * + * Copyright © 2007-2012 Collabora Ltd. + * Copyright © 2007-2008 Nokia Corporation + * + * Copying and distribution of this file, with or without modification, + * are permitted in any medium without royalty provided the copyright + * notice and this notice are preserved. No warranty. + */ + #include "config.h" +#include #include #include +#include #include -#include "tests/lib/myassert.h" +#include "tests/lib/contacts-conn.h" #include "tests/lib/util.h" -static gboolean had_unsupported = FALSE; -static gboolean had_supported = FALSE; -static GMainLoop *mainloop = NULL; -static gboolean freed_user_data[] = { FALSE, FALSE, FALSE, FALSE }; +typedef struct { + TpDBusDaemon *dbus; + TpTestsSimpleConnection *service_conn; + TpBaseConnection *service_conn_as_base; + gchar *conn_name; + gchar *conn_path; + TpConnection *conn; + + guint wait; + gboolean reentrant; + gboolean freed; + GError *error /* initialized by GTest */; +} Fixture; + +static void +setup (Fixture *f, + gconstpointer data) +{ + g_type_init (); + tp_debug_set_flags ("all"); + f->dbus = tp_tests_dbus_daemon_dup_or_die (); + + f->service_conn = TP_TESTS_SIMPLE_CONNECTION ( + tp_tests_object_new_static_class ( + TP_TESTS_TYPE_CONTACTS_CONNECTION, + "account", "me@example.com", + "protocol", "simple-protocol", + NULL)); + f->service_conn_as_base = TP_BASE_CONNECTION (f->service_conn); + g_assert (f->service_conn != NULL); + g_assert (f->service_conn_as_base != NULL); + + g_assert (tp_base_connection_register (f->service_conn_as_base, "simple", + &f->conn_name, &f->conn_path, &f->error)); + g_assert_no_error (f->error); + + f->conn = tp_connection_new (f->dbus, f->conn_name, f->conn_path, + &f->error); + g_assert_no_error (f->error); +} + +static void +test_supported_run (Fixture *f, + gconstpointer nil G_GNUC_UNUSED) +{ + gboolean ok; + + ok = tp_cli_connection_run_connect (f->conn, -1, &f->error, NULL); + g_assert_no_error (f->error); + g_assert (ok); +} + +static void +test_unsupported_run (Fixture *f, + gconstpointer nil G_GNUC_UNUSED) +{ + gboolean ok; + + ok = tp_cli_connection_interface_mail_notification_run_request_inbox_url ( + f->conn, -1, NULL /* "out" arg */, &f->error, NULL); + g_assert_error (f->error, TP_DBUS_ERRORS, TP_DBUS_ERROR_NO_INTERFACE); + g_assert (!ok); +} + +static void +pretend_to_free (gpointer p) +{ + Fixture *f = p; + + g_assert (!f->freed); + f->freed = TRUE; +} + +static void +connect_cb (TpConnection *conn, + const GError *error, + gpointer user_data, + GObject *weak_object) +{ + Fixture *f = user_data; + + g_assert_no_error (f->error); + g_assert (!f->reentrant); + g_assert (!f->freed); + + if (error != NULL) + f->error = g_error_copy (error); + + f->wait--; +} + +static void +test_supported_async (Fixture *f, + gconstpointer nil G_GNUC_UNUSED) +{ + TpProxyPendingCall *call; + + f->reentrant = TRUE; + f->wait = 1; + call = tp_cli_connection_call_connect (f->conn, -1, connect_cb, + f, pretend_to_free, NULL); + f->reentrant = FALSE; + + g_assert (call != NULL); + g_assert (!f->freed); + + while (f->wait) + g_main_context_iteration (NULL, TRUE); + + g_assert_no_error (f->error); + g_assert (f->freed); +} static void -supported_cb (TpDBusDaemon *bus_daemon, - const gchar **names, - const GError *error, - gpointer user_data, - GObject *weak_object) +inbox_url_cb (TpConnection *conn, + const GValueArray *va, + const GError *error, + gpointer user_data, + GObject *weak_object) { - MYASSERT (user_data != NULL, ""); - MYASSERT (weak_object == NULL, ""); - MYASSERT (names != NULL, ""); - MYASSERT (bus_daemon != NULL, ""); - MYASSERT (error == NULL, ""); - MYASSERT (mainloop != NULL, ""); - - MYASSERT (!had_supported, ""); - had_supported = TRUE; - - if (had_unsupported && had_supported) - g_main_loop_quit (mainloop); + Fixture *f = user_data; + + g_assert_no_error (f->error); + /* Unsupported interfaces are signalled by a re-entrant callback in 0.x */ + g_assert (f->reentrant); + g_assert (!f->freed); + + if (error != NULL) + f->error = g_error_copy (error); + + f->wait--; } static void -unsupported_cb (TpProxy *proxy, - const GPtrArray *out0, - const GError *error, - gpointer user_data, - GObject *weak_object) +test_unsupported_async (Fixture *f, + gconstpointer nil G_GNUC_UNUSED) { - MYASSERT (weak_object == NULL, ""); - MYASSERT (user_data != NULL, ""); - MYASSERT (proxy != NULL, ""); - MYASSERT (out0 == NULL, ""); - MYASSERT (error != NULL, ""); - MYASSERT (mainloop != NULL, ""); - - MYASSERT (!had_unsupported, ""); - had_unsupported = TRUE; - - if (had_unsupported && had_supported) - g_main_loop_quit (mainloop); + TpProxyPendingCall *call; + + f->reentrant = TRUE; + f->wait = 1; + call = tp_cli_connection_interface_mail_notification_call_request_inbox_url ( + f->conn, -1, inbox_url_cb, f, pretend_to_free, NULL); + f->reentrant = FALSE; + + /* Unsupported interfaces are signalled by a re-entrant callback in 0.x */ + g_assert (call == NULL); + g_assert (f->freed); + + while (f->wait) + g_main_context_iteration (NULL, TRUE); + + g_assert_error (f->error, TP_DBUS_ERRORS, TP_DBUS_ERROR_NO_INTERFACE); } static void -do_nothing (void) +do_nothing (TpConnection *conn, + ...) { } static void -free_user_data (gpointer user_data) +test_supported_signal (Fixture *f, + gconstpointer nil G_GNUC_UNUSED) { - gboolean *ptr = user_data; + TpProxySignalConnection *sc; + + sc = tp_cli_connection_connect_to_status_changed (f->conn, + (void (*)(TpConnection *, guint, guint, gpointer, GObject *)) do_nothing, + f, pretend_to_free, NULL, &f->error); - *ptr = TRUE; + g_assert_no_error (f->error); + g_assert (sc != NULL); + g_assert (!f->freed); + + tp_proxy_signal_connection_disconnect (sc); + g_assert (f->freed); +} + +static void +test_unsupported_signal (Fixture *f, + gconstpointer nil G_GNUC_UNUSED) +{ + TpProxySignalConnection *sc; + + sc = tp_cli_connection_interface_mail_notification_connect_to_mails_received ( + f->conn, + (void (*)(TpConnection *, const GPtrArray *, gpointer, GObject *)) do_nothing, + f, pretend_to_free, NULL, &f->error); + + g_assert_error (f->error, TP_DBUS_ERRORS, TP_DBUS_ERROR_NO_INTERFACE); + g_assert (sc == NULL); + g_assert (f->freed); +} + +static void +teardown (Fixture *f, + gconstpointer data) +{ + TpConnection *conn; + + g_clear_error (&f->error); + + g_clear_object (&f->conn); + + /* disconnect the connection so we don't leak it */ + conn = tp_connection_new (f->dbus, f->conn_name, f->conn_path, + &f->error); + g_assert (conn != NULL); + g_assert_no_error (f->error); + + tp_tests_connection_assert_disconnect_succeeds (conn); + tp_tests_proxy_run_until_prepared_or_failed (conn, NULL, &f->error); + g_assert_error (f->error, TP_ERRORS, TP_ERROR_CANCELLED); + g_clear_error (&f->error); + + g_object_unref (conn); + + /* borrowed from service_conn */ + f->service_conn_as_base = NULL; + + g_clear_object (&f->service_conn); + g_free (f->conn_name); + g_free (f->conn_path); + + g_clear_object (&f->dbus); } int main (int argc, char **argv) { - TpDBusDaemon *bus_daemon; - GError *error = NULL; + tp_tests_init (&argc, &argv); - tp_tests_abort_after (10); - g_type_init (); - tp_debug_set_flags ("all"); + g_test_add ("/supported/run", Fixture, NULL, + setup, test_supported_run, teardown); + g_test_add ("/supported/async", Fixture, NULL, + setup, test_supported_async, teardown); + g_test_add ("/supported/signal", Fixture, NULL, + setup, test_supported_signal, teardown); + g_test_add ("/unsupported/run", Fixture, NULL, + setup, test_unsupported_run, teardown); + g_test_add ("/unsupported/async", Fixture, NULL, + setup, test_unsupported_async, teardown); + g_test_add ("/unsupported/signal", Fixture, NULL, + setup, test_unsupported_signal, teardown); - bus_daemon = tp_tests_dbus_daemon_dup_or_die (); - - /* this interface is automatically supported... */ - MYASSERT (tp_cli_dbus_daemon_run_list_names (bus_daemon, -1, NULL, - NULL, NULL), ""); - - /* ... but this one is not */ - MYASSERT (!tp_cli_properties_interface_run_list_properties (bus_daemon, -1, - NULL, &error, NULL), ""); - MYASSERT (error != NULL, ""); - g_error_free (error); - error = NULL; - - /* the same, but with async API */ - - mainloop = g_main_loop_new (NULL, FALSE); - - MYASSERT (tp_cli_dbus_daemon_call_list_names (bus_daemon, -1, supported_cb, - freed_user_data + 0, free_user_data, NULL) != NULL, ""); - MYASSERT (tp_cli_properties_interface_call_list_properties (bus_daemon, -1, - unsupported_cb, freed_user_data + 1, free_user_data, NULL) == NULL, - ""); - - /* the same, but with signals */ - MYASSERT (tp_cli_dbus_daemon_connect_to_name_acquired (bus_daemon, - (tp_cli_dbus_daemon_signal_callback_name_acquired) do_nothing, - freed_user_data + 2, free_user_data, NULL, NULL) != NULL, ""); - MYASSERT (tp_cli_properties_interface_connect_to_property_flags_changed - (bus_daemon, - (tp_cli_properties_interface_signal_callback_property_flags_changed) - do_nothing, - freed_user_data + 3, free_user_data, NULL, &error) == NULL, ""); - MYASSERT (error != NULL, ""); - g_error_free (error); - error = NULL; - - g_main_loop_run (mainloop); - g_main_loop_unref (mainloop); - mainloop = NULL; - - MYASSERT (freed_user_data[0], " (async call, supported)"); - MYASSERT (freed_user_data[1], " (async call, unsupported)"); - MYASSERT (!freed_user_data[2], " (signal connection, supported)"); - MYASSERT (freed_user_data[3], " (signal connection, unsupported)"); - - g_object_unref (bus_daemon); - - return 0; + return g_test_run (); } -- 1.7.9.1