From 3a882f5b1de0dfd3a3a156518d1abf4b70c23924 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Mon, 7 May 2012 20:21:29 +0200 Subject: [PATCH] Port unit tests to contacts API of TpChannel https://bugs.freedesktop.org/show_bug.cgi?id=49371 --- tests/dbus/channel-introspect.c | 15 +++-- tests/dbus/cli-group.c | 34 +++++----- tests/dbus/group-mixin.c | 138 +++++++++++++++++++-------------------- tests/lib/util.c | 25 +++++++ tests/lib/util.h | 3 + 5 files changed, 123 insertions(+), 92 deletions(-) diff --git a/tests/dbus/channel-introspect.c b/tests/dbus/channel-introspect.c index 71281bb..a17fe6e 100644 --- a/tests/dbus/channel-introspect.c +++ b/tests/dbus/channel-introspect.c @@ -108,8 +108,9 @@ main (int argc, TpHandle handle; GHashTable *asv; GAsyncResult *prepare_result; - GQuark some_features[] = { TP_CHANNEL_FEATURE_CORE, + GQuark chat_states_features[] = { TP_CHANNEL_FEATURE_CORE, TP_CHANNEL_FEATURE_CHAT_STATES, 0 }; + GQuark contacts_features[] = { TP_CHANNEL_FEATURE_CONTACTS, 0 }; g_type_init (); tp_tests_abort_after (10); @@ -160,7 +161,7 @@ main (int argc, g_assert_no_error (error); prepare_result = NULL; - tp_proxy_prepare_async (chan, some_features, channel_prepared_cb, + tp_proxy_prepare_async (chan, chat_states_features, channel_prepared_cb, &prepare_result); g_assert_cmpint (tp_proxy_is_prepared (chan, TP_CHANNEL_FEATURE_CORE), ==, @@ -188,10 +189,10 @@ main (int argc, tp_handle_inspect (contact_repo, service_conn_as_base->self_handle)); /* no way to see what this is doing - just make sure it doesn't crash */ - tp_proxy_prepare_async (chan, some_features, NULL, NULL); + tp_proxy_prepare_async (chan, chat_states_features, NULL, NULL); prepare_result = NULL; - tp_proxy_prepare_async (chan, some_features, channel_prepared_cb, + tp_proxy_prepare_async (chan, chat_states_features, channel_prepared_cb, &prepare_result); if (prepare_result == NULL) @@ -277,7 +278,7 @@ main (int argc, g_hash_table_unref (asv); asv = NULL; - tp_tests_proxy_run_until_prepared (chan, NULL); + tp_tests_proxy_run_until_prepared (chan, contacts_features); g_assert_cmpuint (g_hash_table_size ( TP_TESTS_PROPS_TEXT_CHANNEL (service_props_group_chan) ->dbus_property_interfaces_retrieved), ==, 2); @@ -395,7 +396,7 @@ main (int argc, g_assert_no_error (error); prepare_result = NULL; - tp_proxy_prepare_async (chan, some_features, channel_prepared_cb, + tp_proxy_prepare_async (chan, chat_states_features, channel_prepared_cb, &prepare_result); g_assert (prepare_result == NULL); g_main_loop_run (mainloop); @@ -412,7 +413,7 @@ main (int argc, tp_tests_connection_assert_disconnect_succeeds (conn); prepare_result = NULL; - tp_proxy_prepare_async (chan, some_features, channel_prepared_cb, + tp_proxy_prepare_async (chan, chat_states_features, channel_prepared_cb, &prepare_result); /* is_prepared becomes FALSE because the channel broke */ diff --git a/tests/dbus/cli-group.c b/tests/dbus/cli-group.c index 38877e2..60e496f 100644 --- a/tests/dbus/cli-group.c +++ b/tests/dbus/cli-group.c @@ -42,14 +42,16 @@ gboolean expecting_invalidated = FALSE; static void group_members_changed_cb (TpChannel *chan_, - GArray *added, - GArray *removed, - GArray *local_pending, - GArray *remote_pending, + GPtrArray *added, + GPtrArray *removed, + GPtrArray *local_pending, + GPtrArray *remote_pending, + TpContact *actor, GHashTable *details, gpointer user_data) { guint reason = tp_asv_get_uint32 (details, "change-reason", NULL); + GMainLoop *loop = user_data; DEBUG ("%u, %u, %u, %u, %u details", added->len, removed->len, local_pending->len, remote_pending->len, g_hash_table_size (details)); @@ -58,6 +60,7 @@ group_members_changed_cb (TpChannel *chan_, g_assert_cmpuint (reason, ==, expected_reason); expecting_group_members_changed = FALSE; + g_main_loop_quit (loop); } @@ -67,11 +70,13 @@ test_channel_proxy (TpTestsTextChannelGroup *service_chan, { TpIntset *add, *rem, *expected_members; GHashTable *details; + GQuark features[] = { TP_CHANNEL_FEATURE_CONTACTS, 0 }; + GMainLoop *loop = g_main_loop_new (NULL, FALSE); - tp_tests_proxy_run_until_prepared (chan, NULL); + tp_tests_proxy_run_until_prepared (chan, features); - g_signal_connect (chan, "group-members-changed", - (GCallback) group_members_changed_cb, NULL); + g_signal_connect (chan, "group-contacts-changed", + (GCallback) group_members_changed_cb, loop); /* Add a couple of members. */ add = tp_intset_new (); @@ -93,14 +98,11 @@ test_channel_proxy (TpTestsTextChannelGroup *service_chan, tp_clear_pointer (&details, g_hash_table_unref); - /* Clear the queue to ensure that there aren't any more - * MembersChanged signals waiting for us. - */ - tp_tests_proxy_run_until_dbus_queue_processed (conn); + /* Run until we get "group-contacts-changed" signal */ + g_main_loop_run (loop); expected_members = add; - MYASSERT (tp_intset_is_equal (expected_members, - tp_channel_group_get_members (chan)), ""); + tp_tests_channel_assert_expect_members (chan, expected_members); /* Add one, remove one. Check that the cache is properly updated. */ add = tp_intset_new (); @@ -124,13 +126,13 @@ test_channel_proxy (TpTestsTextChannelGroup *service_chan, tp_intset_destroy (add); tp_intset_destroy (rem); - tp_tests_proxy_run_until_dbus_queue_processed (conn); + /* Run until we get "group-contacts-changed" signal */ + g_main_loop_run (loop); tp_intset_add (expected_members, h3); tp_intset_remove (expected_members, h1); - MYASSERT (tp_intset_is_equal (expected_members, - tp_channel_group_get_members (chan)), ""); + tp_tests_channel_assert_expect_members (chan, expected_members); tp_intset_destroy (expected_members); } diff --git a/tests/dbus/group-mixin.c b/tests/dbus/group-mixin.c index 3c0178c..702ee4d 100644 --- a/tests/dbus/group-mixin.c +++ b/tests/dbus/group-mixin.c @@ -35,8 +35,8 @@ TpChannel *chan = NULL; TpHandleRepoIface *contact_repo; TpHandle self_handle, camel, camel2; -typedef void (*diff_checker) (const GArray *added, const GArray *removed, - const GArray *local_pending, const GArray *remote_pending, +typedef void (*diff_checker) (const GPtrArray *added, const GPtrArray *removed, + const GPtrArray *local_pending, const GPtrArray *remote_pending, const GHashTable *details); static gboolean expecting_members_changed = FALSE; @@ -74,16 +74,15 @@ wait_for_outstanding_signals (void) static void on_members_changed (TpChannel *proxy, - const GArray *arg_Added, - const GArray *arg_Removed, - const GArray *arg_Local_Pending, - const GArray *arg_Remote_Pending, - GHashTable *arg_Details, - gpointer user_data, - GObject *weak_object) + GPtrArray *added, + GPtrArray *removed, + GPtrArray *local_pending, + GPtrArray *remote_pending, + TpContact *actor, + GHashTable *details, + gpointer user_data) { const gchar *message; - TpHandle actor; TpChannelGroupChangeReason reason; gboolean valid; @@ -91,26 +90,23 @@ on_members_changed (TpChannel *proxy, ": got unexpected MembersChanged"); expecting_members_changed = FALSE; - message = tp_asv_get_string (arg_Details, "message"); + message = tp_asv_get_string (details, "message"); if (message == NULL) message = ""; g_assert_cmpstr (message, ==, expected_message); - actor = tp_asv_get_uint32 (arg_Details, "actor", &valid); - if (valid) + if (actor != NULL) { - g_assert_cmpuint (actor, ==, expected_actor); + g_assert_cmpuint (tp_contact_get_handle (actor), ==, expected_actor); } else { g_assert_cmpuint (expected_actor, ==, 0); - MYASSERT (tp_asv_lookup (arg_Details, "actor") == NULL, - ": wanted an actor, not an imposter"); } - reason = tp_asv_get_uint32 (arg_Details, "change-reason", &valid); + reason = tp_asv_get_uint32 (details, "change-reason", &valid); if (valid) { g_assert_cmpuint (reason, ==, expected_reason); @@ -119,12 +115,12 @@ on_members_changed (TpChannel *proxy, { g_assert_cmpuint (expected_reason, ==, TP_CHANNEL_GROUP_CHANGE_REASON_NONE); - MYASSERT (tp_asv_lookup (arg_Details, "reason") == NULL, + MYASSERT (tp_asv_lookup (details, "reason") == NULL, ": utterly unreasonable"); } - expected_diffs (arg_Added, arg_Removed, arg_Local_Pending, - arg_Remote_Pending, arg_Details); + expected_diffs (added, removed, local_pending, + remote_pending, details); if (!outstanding_signals ()) g_main_loop_quit (mainloop); @@ -196,13 +192,13 @@ details_contains_ids_for (const GHashTable *details, } static void -self_added_to_lp (const GArray *added, - const GArray *removed, - const GArray *local_pending, - const GArray *remote_pending, +self_added_to_lp (const GPtrArray *added, + const GPtrArray *removed, + const GPtrArray *local_pending, + const GPtrArray *remote_pending, const GHashTable *details) { - TpHandle h; + TpContact *c; TpHandle hs[] = { self_handle, 0 }; MYASSERT (added->len == 0, ": no new added to members"); @@ -211,26 +207,26 @@ self_added_to_lp (const GArray *added, MYASSERT (local_pending->len == 1, ": one local pending..."); /* ...which is us */ - h = g_array_index (local_pending, TpHandle, 0); - g_assert_cmpuint (h, ==, self_handle); + c = g_ptr_array_index (local_pending, 0); + g_assert_cmpuint (tp_contact_get_handle (c), ==, self_handle); details_contains_ids_for (details, hs); } static void -self_added_to_members (const GArray *added, - const GArray *removed, - const GArray *local_pending, - const GArray *remote_pending, +self_added_to_members (const GPtrArray *added, + const GPtrArray *removed, + const GPtrArray *local_pending, + const GPtrArray *remote_pending, const GHashTable *details) { - TpHandle h; + TpContact *c; TpHandle hs[] = { self_handle, 0 }; MYASSERT (added->len == 1, ": one added"); - h = g_array_index (added, TpHandle, 0); - g_assert_cmpuint (h, ==, self_handle); + c = g_ptr_array_index (added, 0); + g_assert_cmpuint (tp_contact_get_handle (c), ==, self_handle); MYASSERT (removed->len == 0, ": no-one removed"); MYASSERT (local_pending->len == 0, ": no new local pending"); @@ -287,19 +283,19 @@ check_incoming_invitation (void) } static void -camel_added (const GArray *added, - const GArray *removed, - const GArray *local_pending, - const GArray *remote_pending, +camel_added (const GPtrArray *added, + const GPtrArray *removed, + const GPtrArray *local_pending, + const GPtrArray *remote_pending, const GHashTable *details) { - TpHandle h; + TpContact *c; TpHandle hs[] = { camel, 0 }; MYASSERT (added->len == 1, ": one added"); - h = g_array_index (added, TpHandle, 0); - g_assert_cmpuint (h, ==, camel); + c = g_ptr_array_index (added, 0); + g_assert_cmpuint (tp_contact_get_handle (c), ==, camel); details_contains_ids_for (details, hs); @@ -309,20 +305,20 @@ camel_added (const GArray *added, } static void -camel2_added (const GArray *added, - const GArray *removed, - const GArray *local_pending, - const GArray *remote_pending, +camel2_added (const GPtrArray *added, + const GPtrArray *removed, + const GPtrArray *local_pending, + const GPtrArray *remote_pending, const GHashTable *details) { - TpHandle h; + TpContact *c; /* camel is the actor */ TpHandle hs[] = { camel, camel2, 0 }; MYASSERT (added->len == 1, ": one added"); - h = g_array_index (added, TpHandle, 0); - g_assert_cmpuint (h, ==, camel2); + c = g_ptr_array_index (added, 0); + g_assert_cmpuint (tp_contact_get_handle (c), ==, camel2); details_contains_ids_for (details, hs); @@ -332,13 +328,13 @@ camel2_added (const GArray *added, } static void -camel_removed (const GArray *added, - const GArray *removed, - const GArray *local_pending, - const GArray *remote_pending, +camel_removed (const GPtrArray *added, + const GPtrArray *removed, + const GPtrArray *local_pending, + const GPtrArray *remote_pending, const GHashTable *details) { - TpHandle h; + TpContact *c; /* camel2 is the actor. camel shouldn't be in the ids, because they were * removed and the spec says that you can leave those out, and we want * tp-glib's automatic construction of contact-ids to work in the #ubuntu @@ -348,8 +344,8 @@ camel_removed (const GArray *added, MYASSERT (removed->len == 1, ": one removed"); - h = g_array_index (removed, TpHandle, 0); - g_assert_cmpuint (h, ==, camel); + c = g_ptr_array_index (removed, 0); + g_assert_cmpuint (tp_contact_get_handle (c), ==, camel); MYASSERT (added->len == 0, ": no-one added"); MYASSERT (local_pending->len == 0, ": no new local pending"); @@ -361,6 +357,11 @@ camel_removed (const GArray *added, static void in_the_desert (void) { + TpIntset *expected_members; + + expected_members = tp_intset_new (); + tp_intset_add (expected_members, self_handle); + camel = tp_handle_ensure (contact_repo, "camel", NULL, NULL); camel2 = tp_handle_ensure (contact_repo, "camel2", NULL, NULL); @@ -374,6 +375,7 @@ in_the_desert (void) NULL); tp_intset_add (add, camel); + tp_intset_add (expected_members, camel); expect_signals ("", camel, TP_CHANNEL_GROUP_CHANGE_REASON_NONE, camel_added); tp_group_mixin_change_members ((GObject *) service_chan, add, NULL, @@ -392,6 +394,7 @@ in_the_desert (void) NULL, (GDestroyNotify) tp_g_value_slice_free); tp_intset_add (add, camel2); + tp_intset_add (expected_members, camel2); g_hash_table_insert (details, "actor", tp_g_value_slice_new_uint (camel)); @@ -413,6 +416,7 @@ in_the_desert (void) NULL, (GDestroyNotify) tp_g_value_slice_free); tp_intset_add (del, camel); + tp_intset_remove (expected_members, camel); g_hash_table_insert (details, "actor", tp_g_value_slice_new_uint (camel2)); @@ -445,43 +449,39 @@ in_the_desert (void) /* We and the second camel should be left in the channel */ { - const TpIntset *members = tp_channel_group_get_members (chan); GArray *service_members; - TpHandle a, b; + TpIntset *service_members_intset; - g_assert_cmpuint (tp_intset_size (members), ==, 2); - MYASSERT (tp_intset_is_member (members, self_handle), ""); - MYASSERT (tp_intset_is_member (members, camel2), ": what a pity"); + tp_tests_channel_assert_expect_members (chan, expected_members); /* And let's check that the group mixin agrees, in case that's just the * client binding being wrong. */ tp_group_mixin_get_members ((GObject *) service_chan, &service_members, NULL); - g_assert_cmpuint (service_members->len, ==, 2); - a = g_array_index (service_members, TpHandle, 0); - b = g_array_index (service_members, TpHandle, 1); - MYASSERT (a != b, ""); - MYASSERT (a == self_handle || b == self_handle, ""); - MYASSERT (a == camel2 || b == camel2, ""); + service_members_intset = tp_intset_from_array (service_members); + g_assert (tp_intset_is_equal (service_members_intset, expected_members)); g_array_unref (service_members); + tp_intset_destroy (service_members_intset); } tp_handle_unref (contact_repo, camel); tp_handle_unref (contact_repo, camel2); + tp_intset_destroy (expected_members); } static void test_group_mixin (void) { - tp_tests_proxy_run_until_prepared (chan, NULL); + GQuark features[] = { TP_CHANNEL_FEATURE_CONTACTS, 0 }; + tp_tests_proxy_run_until_prepared (chan, features); MYASSERT (tp_proxy_has_interface (chan, TP_IFACE_CHANNEL_INTERFACE_GROUP), ""); - tp_cli_channel_interface_group_connect_to_members_changed (chan, - on_members_changed, NULL, NULL, NULL, NULL); + g_signal_connect (chan, "group-contacts-changed", + G_CALLBACK (on_members_changed), NULL); check_initial_properties (); diff --git a/tests/lib/util.c b/tests/lib/util.c index 3a0c876..d70134c 100644 --- a/tests/lib/util.c +++ b/tests/lib/util.c @@ -512,3 +512,28 @@ tp_tests_connection_run_until_contact_by_id (TpConnection *connection, return contact; } + +void +tp_tests_channel_assert_expect_members (TpChannel *channel, + TpIntset *expected_members) +{ + GPtrArray *contacts; + TpIntset *members; + guint i; + + members = tp_intset_new (); + contacts = tp_channel_group_dup_members_contacts (channel); + if (contacts != NULL) + { + for (i = 0; i < contacts->len; i++) + { + TpContact *contact = g_ptr_array_index (contacts, i); + tp_intset_add (members, tp_contact_get_handle (contact)); + } + } + + g_assert (tp_intset_is_equal (members, expected_members)); + + g_ptr_array_unref (contacts); + tp_intset_destroy (members); +} diff --git a/tests/lib/util.h b/tests/lib/util.h index 2253b49..745c08c 100644 --- a/tests/lib/util.h +++ b/tests/lib/util.h @@ -78,4 +78,7 @@ TpContact *tp_tests_connection_run_until_contact_by_id ( const gchar *id, const GQuark *features); +void tp_tests_channel_assert_expect_members (TpChannel *channel, + TpIntset *expected_members); + #endif /* #ifndef __TP_TESTS_LIB_UTIL_H__ */ -- 1.7.9.5