[Telepathy-commits] [telepathy-glib/master] Add a test for respawning text channels in the Echo example CM

Simon McVittie simon.mcvittie at collabora.co.uk
Thu Oct 30 07:19:39 PDT 2008


---
 .gitignore                |    1 +
 tests/dbus/Makefile.am    |    7 +
 tests/dbus/text-respawn.c |  291 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 299 insertions(+), 0 deletions(-)
 create mode 100644 tests/dbus/text-respawn.c

diff --git a/.gitignore b/.gitignore
index 39cfeb9..12a996d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -77,6 +77,7 @@ tests/dbus/test-handle-set
 tests/dbus/test-invalidated-while-invoking-signals
 tests/dbus/test-properties
 tests/dbus/test-text-mixin
+tests/dbus/test-text-respawn
 tests/dbus/test-unsupported-interface
 tests/test-asv
 tests/test-availability-cmp
diff --git a/tests/dbus/Makefile.am b/tests/dbus/Makefile.am
index 7d6460d..8467947 100644
--- a/tests/dbus/Makefile.am
+++ b/tests/dbus/Makefile.am
@@ -14,6 +14,7 @@ noinst_PROGRAMS = \
     test-invalidated-while-invoking-signals \
     test-properties \
     test-text-mixin \
+    test-text-respawn \
     test-unsupported-interface
 
 TESTS = $(noinst_PROGRAMS)
@@ -86,6 +87,12 @@ test_text_mixin_LDADD = \
     $(top_builddir)/examples/cm/echo/libexample-cm-echo.la
 test_text_mixin_SOURCES = text-mixin.c
 
+test_text_respawn_LDADD = \
+    $(TP_GLIB_LIBS) \
+    $(top_builddir)/tests/lib/libtp-glib-tests.la \
+    $(top_builddir)/examples/cm/echo/libexample-cm-echo.la
+test_text_respawn_SOURCES = text-respawn.c
+
 test_unsupported_interface_LDADD = $(TP_GLIB_LIBS)
 test_unsupported_interface_SOURCES = unsupported-interface.c
 
diff --git a/tests/dbus/text-respawn.c b/tests/dbus/text-respawn.c
new file mode 100644
index 0000000..d2ce13c
--- /dev/null
+++ b/tests/dbus/text-respawn.c
@@ -0,0 +1,291 @@
+/* Basic test for the text mixin and the echo example CM.
+ *
+ * Copyright (C) 2007-2008 Collabora Ltd. <http://www.collabora.co.uk/>
+ * Copyright (C) 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.
+ */
+
+#include <telepathy-glib/channel.h>
+#include <telepathy-glib/connection.h>
+#include <telepathy-glib/dbus.h>
+#include <telepathy-glib/debug.h>
+#include <telepathy-glib/gtypes.h>
+#include <telepathy-glib/interfaces.h>
+
+#include "examples/cm/echo/chan.h"
+#include "examples/cm/echo/conn.h"
+#include "tests/lib/myassert.h"
+
+static int fail = 0;
+
+static void
+myassert_failed (void)
+{
+  fail = 1;
+}
+
+static guint received_count = 0;
+static guint last_received_id = 0;
+static guint last_received_time = 0;
+static guint last_received_sender = 0;
+static guint last_received_type = 0;
+static guint last_received_flags = 0;
+static gchar *last_received_text = NULL;
+
+static guint sent_count = 0;
+static guint last_sent_type = 0;
+static gchar *last_sent_text = NULL;
+
+static void
+on_sent (TpChannel *chan,
+         guint timestamp,
+         guint type,
+         const gchar *text,
+         gpointer data,
+         GObject *object)
+{
+  g_message ("%p: Sent: time %u, type %u, text '%s'",
+      chan, timestamp, type, text);
+
+  sent_count++;
+  last_sent_type = type;
+  g_free (last_sent_text);
+  last_sent_text = g_strdup (text);
+}
+
+static void
+on_received (TpChannel *chan,
+             guint id,
+             guint timestamp,
+             guint sender,
+             guint type,
+             guint flags,
+             const gchar *text,
+             gpointer data,
+             GObject *object)
+{
+  TpHandleRepoIface *contact_repo = data;
+
+  g_message ("%p: Received #%u: time %u, sender %u '%s', type %u, flags %u, "
+      "text '%s'", chan, id, timestamp, sender,
+      tp_handle_inspect (contact_repo, sender), type, flags, text);
+
+  received_count++;
+  last_received_id = id;
+  last_received_time = timestamp;
+  last_received_sender = sender;
+  last_received_type = type;
+  last_received_flags = flags;
+  g_free (last_received_text);
+  last_received_text = g_strdup (text);
+}
+
+int
+main (int argc,
+      char **argv)
+{
+  ExampleEchoConnection *service_conn;
+  TpBaseConnection *service_conn_as_base;
+  TpHandleRepoIface *contact_repo;
+  ExampleEchoChannel *service_chan;
+  TpDBusDaemon *dbus;
+  TpConnection *conn;
+  TpChannel *chan;
+  GError *error = NULL;
+  gchar *name;
+  gchar *conn_path;
+  gchar *chan_path;
+  TpHandle handle;
+
+  g_type_init ();
+  /* tp_debug_set_flags ("all"); */
+
+  service_conn = EXAMPLE_ECHO_CONNECTION (g_object_new (
+        EXAMPLE_TYPE_ECHO_CONNECTION,
+        "account", "me at example.com",
+        "protocol", "example",
+        NULL));
+  service_conn_as_base = TP_BASE_CONNECTION (service_conn);
+  MYASSERT (service_conn != NULL, "");
+  MYASSERT (service_conn_as_base != NULL, "");
+
+  MYASSERT (tp_base_connection_register (service_conn_as_base, "example",
+        &name, &conn_path, &error), "");
+  MYASSERT_NO_ERROR (error);
+
+  dbus = tp_dbus_daemon_new (tp_get_bus ());
+  conn = tp_connection_new (dbus, name, conn_path, &error);
+  MYASSERT (conn != NULL, "");
+  MYASSERT_NO_ERROR (error);
+
+  MYASSERT (tp_connection_run_until_ready (conn, TRUE, &error, NULL),
+      "");
+  MYASSERT_NO_ERROR (error);
+
+  contact_repo = tp_base_connection_get_handles (service_conn_as_base,
+      TP_HANDLE_TYPE_CONTACT);
+  MYASSERT (contact_repo != NULL, "");
+
+  handle = tp_handle_ensure (contact_repo, "them at example.org", NULL, &error);
+  MYASSERT_NO_ERROR (error);
+
+  /* FIXME: exercise RequestChannel rather than just pasting on a channel */
+
+  chan_path = g_strdup_printf ("%s/Channel", conn_path);
+
+  service_chan = EXAMPLE_ECHO_CHANNEL (g_object_new (
+        EXAMPLE_TYPE_ECHO_CHANNEL,
+        "connection", service_conn,
+        "object-path", chan_path,
+        "handle", handle,
+        NULL));
+
+  chan = tp_channel_new (conn, chan_path, TP_IFACE_CHANNEL_TYPE_TEXT,
+      TP_HANDLE_TYPE_CONTACT, handle, &error);
+  MYASSERT_NO_ERROR (error);
+
+  tp_channel_run_until_ready (chan, &error, NULL);
+  MYASSERT_NO_ERROR (error);
+
+  MYASSERT (tp_cli_channel_type_text_connect_to_received (chan, on_received,
+      g_object_ref (contact_repo), g_object_unref, NULL, NULL) != NULL, "");
+  MYASSERT (tp_cli_channel_type_text_connect_to_sent (chan, on_sent,
+      NULL, NULL, NULL, NULL) != NULL, "");
+
+  sent_count = 0;
+  received_count = 0;
+  tp_cli_channel_type_text_run_send (chan, -1,
+      TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, "Hello, world!",
+      &error, NULL);
+  MYASSERT_NO_ERROR (error);
+  MYASSERT (sent_count == 1, ": %u != 1", sent_count);
+  MYASSERT (received_count == 1, ": %u != 1", received_count);
+  MYASSERT (last_sent_type == TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
+      ": %u != NORMAL", last_sent_type);
+  MYASSERT (!tp_strdiff (last_sent_text, "Hello, world!"),
+      "'%s' != '%s'", last_sent_text, "Hello, world!");
+  MYASSERT (last_received_type == TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
+      ": %u != NORMAL", last_received_type);
+  MYASSERT (last_received_flags == 0, ": %u != 0", last_received_flags);
+  MYASSERT (last_received_sender == handle,
+      ": %u != %u", last_received_sender, handle);
+  MYASSERT (!tp_strdiff (last_received_text, "You said: Hello, world!"),
+      "'%s'", last_received_text);
+
+  g_print ("\n\n==== Closing channel (it will respawn) ====\n");
+
+    {
+      gboolean dead;
+      TpHandle new_initiator;
+
+      MYASSERT (tp_cli_channel_run_close (chan, -1, &error, NULL), "");
+      MYASSERT_NO_ERROR (error);
+      MYASSERT (tp_proxy_get_invalidated (chan) != NULL, "");
+
+      g_object_get (service_chan,
+          "channel-destroyed", &dead,
+          "initiator-handle", &new_initiator,
+          NULL);
+
+      MYASSERT (!dead, "");
+      MYASSERT_SAME_UINT (new_initiator, handle);
+    }
+
+  g_print ("\n\n==== Re-creating TpChannel ====\n");
+
+  g_object_unref (chan);
+
+  chan = tp_channel_new (conn, chan_path, TP_IFACE_CHANNEL_TYPE_TEXT,
+      TP_HANDLE_TYPE_CONTACT, handle, &error);
+  MYASSERT_NO_ERROR (error);
+
+  tp_channel_run_until_ready (chan, &error, NULL);
+  MYASSERT_NO_ERROR (error);
+
+  g_print ("\n\n==== Listing messages ====\n");
+
+/* FIXME: when we merge spec 0.17.14, use the proper constant */
+#define TP_CHANNEL_TEXT_MESSAGE_FLAG_RESCUED ((TpChannelTextMessageFlags) 8)
+
+    {
+      GPtrArray *messages;
+      GValueArray *structure;
+
+      tp_cli_channel_type_text_run_list_pending_messages (chan, -1,
+          FALSE, &messages, &error, NULL);
+      MYASSERT_NO_ERROR (error);
+
+      MYASSERT_SAME_UINT (messages->len, 1);
+      structure = g_ptr_array_index (messages, 0);
+      MYASSERT_SAME_UINT (g_value_get_uint (structure->values + 0),
+          last_received_id);
+      MYASSERT_SAME_UINT (g_value_get_uint (structure->values + 1),
+          last_received_time);
+      MYASSERT_SAME_UINT (g_value_get_uint (structure->values + 2),
+          handle);
+      MYASSERT_SAME_UINT (g_value_get_uint (structure->values + 3),
+          TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL);
+      MYASSERT_SAME_UINT (g_value_get_uint (structure->values + 4),
+          TP_CHANNEL_TEXT_MESSAGE_FLAG_RESCUED);
+      MYASSERT_SAME_STRING (g_value_get_string (structure->values + 5),
+          "You said: Hello, world!");
+
+      g_print ("Freeing\n");
+      g_boxed_free (TP_ARRAY_TYPE_PENDING_TEXT_MESSAGE_LIST, messages);
+    }
+
+  g_print ("\n\n==== Acknowledging the message ====\n");
+
+    {
+      GArray *ids = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
+
+      g_array_append_val (ids, last_received_id);
+
+      tp_cli_channel_type_text_run_acknowledge_pending_messages (chan, -1,
+          ids, &error, NULL);
+      MYASSERT_NO_ERROR (error);
+
+      g_array_free (ids, TRUE);
+    }
+
+  g_print ("\n\n==== Closing channel again ====\n");
+
+    {
+      gboolean dead;
+
+      MYASSERT (tp_cli_channel_run_close (chan, -1, &error, NULL), "");
+      MYASSERT_NO_ERROR (error);
+      MYASSERT (tp_proxy_get_invalidated (chan) != NULL, "");
+
+      g_object_get (service_chan,
+          "channel-destroyed", &dead,
+          NULL);
+
+      MYASSERT (dead, "");
+    }
+
+  g_print ("\n\n==== End of tests ====\n");
+
+  MYASSERT (tp_cli_connection_run_disconnect (conn, -1, &error, NULL), "");
+  MYASSERT_NO_ERROR (error);
+
+  tp_handle_unref (contact_repo, handle);
+  g_object_unref (chan);
+  g_object_unref (conn);
+  g_object_unref (service_chan);
+
+  service_conn_as_base = NULL;
+  g_object_unref (service_conn);
+  g_object_unref (dbus);
+  g_free (name);
+  g_free (conn_path);
+  g_free (chan_path);
+
+  g_free (last_sent_text);
+  g_free (last_received_text);
+
+  return fail;
+}
-- 
1.5.6.5



More information about the Telepathy-commits mailing list