[Telepathy-commits] [telepathy-qt4/master] tests/lib: build a simple CM and Connection, taken from telepathy-glib
Simon McVittie
simon.mcvittie at collabora.co.uk
Fri Jan 9 07:06:58 PST 2009
---
configure.ac | 1 +
tests/Makefile.am | 2 +
tests/lib/Makefile.am | 19 ++++
tests/lib/simple-conn.c | 226 ++++++++++++++++++++++++++++++++++++++++++++
tests/lib/simple-conn.h | 59 ++++++++++++
tests/lib/simple-manager.c | 95 ++++++++++++++++++
tests/lib/simple-manager.h | 55 +++++++++++
7 files changed, 457 insertions(+), 0 deletions(-)
create mode 100644 tests/lib/Makefile.am
create mode 100644 tests/lib/simple-conn.c
create mode 100644 tests/lib/simple-conn.h
create mode 100644 tests/lib/simple-manager.c
create mode 100644 tests/lib/simple-manager.h
diff --git a/configure.ac b/configure.ac
index 3b6870c..3f54d42 100644
--- a/configure.ac
+++ b/configure.ac
@@ -252,6 +252,7 @@ AC_OUTPUT([
tests/Makefile
tests/dbus/Makefile
tests/dbus-1/services/account-manager.service
+ tests/lib/Makefile
tests/pinocchio/Makefile
tests/prototype/Makefile
tools/Makefile
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 2f769c5..8e4c278 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,4 +1,6 @@
+# compile lib/ first, so it can be used by tests in the other directories
SUBDIRS = \
+ lib \
dbus \
pinocchio \
prototype
diff --git a/tests/lib/Makefile.am b/tests/lib/Makefile.am
new file mode 100644
index 0000000..b8817c1
--- /dev/null
+++ b/tests/lib/Makefile.am
@@ -0,0 +1,19 @@
+AM_CFLAGS = \
+ $(ERROR_CFLAGS) \
+ $(TP_GLIB_CFLAGS)
+
+noinst_LTLIBRARIES =
+
+if ENABLE_TP_GLIB_TESTS
+
+noinst_LTLIBRARIES += libtp-glib-tests.la
+
+libtp_glib_tests_la_SOURCES = \
+ simple-conn.c \
+ simple-conn.h \
+ simple-manager.c \
+ simple-manager.h
+libtp_glib_tests_la_LIBADD = \
+ $(TP_GLIB_LIBS)
+
+endif
diff --git a/tests/lib/simple-conn.c b/tests/lib/simple-conn.c
new file mode 100644
index 0000000..8dba8a5
--- /dev/null
+++ b/tests/lib/simple-conn.c
@@ -0,0 +1,226 @@
+/*
+ * simple-conn.c - a simple connection
+ *
+ * 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 "simple-conn.h"
+
+#include <string.h>
+
+#include <dbus/dbus-glib.h>
+
+#include <telepathy-glib/dbus.h>
+#include <telepathy-glib/errors.h>
+#include <telepathy-glib/gtypes.h>
+#include <telepathy-glib/handle-repo-dynamic.h>
+#include <telepathy-glib/util.h>
+
+G_DEFINE_TYPE_WITH_CODE (SimpleConnection,
+ simple_connection,
+ TP_TYPE_BASE_CONNECTION,
+ G_STMT_START { } G_STMT_END)
+
+/* type definition stuff */
+
+enum
+{
+ PROP_ACCOUNT = 1,
+ N_PROPS
+};
+
+struct _SimpleConnectionPrivate
+{
+ gchar *account;
+};
+
+static void
+simple_connection_init (SimpleConnection *self)
+{
+ self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, SIMPLE_TYPE_CONNECTION,
+ SimpleConnectionPrivate);
+}
+
+static void
+get_property (GObject *object,
+ guint property_id,
+ GValue *value,
+ GParamSpec *spec)
+{
+ SimpleConnection *self = SIMPLE_CONNECTION (object);
+
+ switch (property_id) {
+ case PROP_ACCOUNT:
+ g_value_set_string (value, self->priv->account);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, spec);
+ }
+}
+
+static void
+set_property (GObject *object,
+ guint property_id,
+ const GValue *value,
+ GParamSpec *spec)
+{
+ SimpleConnection *self = SIMPLE_CONNECTION (object);
+
+ switch (property_id) {
+ case PROP_ACCOUNT:
+ g_free (self->priv->account);
+ self->priv->account = g_utf8_strdown (g_value_get_string (value), -1);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, spec);
+ }
+}
+
+static void
+finalize (GObject *object)
+{
+ SimpleConnection *self = SIMPLE_CONNECTION (object);
+
+ g_free (self->priv->account);
+
+ G_OBJECT_CLASS (simple_connection_parent_class)->finalize (object);
+}
+
+static gchar *
+get_unique_connection_name (TpBaseConnection *conn)
+{
+ SimpleConnection *self = SIMPLE_CONNECTION (conn);
+
+ return g_strdup (self->priv->account);
+}
+
+static gchar *
+simple_normalize_contact (TpHandleRepoIface *repo,
+ const gchar *id,
+ gpointer context,
+ GError **error)
+{
+ if (id[0] == '\0')
+ {
+ g_set_error (error, TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
+ "ID must not be empty");
+ return NULL;
+ }
+
+ if (strchr (id, ' ') != NULL)
+ {
+ g_set_error (error, TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
+ "ID must not contain spaces");
+ return NULL;
+ }
+
+ return g_utf8_strdown (id, -1);
+}
+
+static void
+create_handle_repos (TpBaseConnection *conn,
+ TpHandleRepoIface *repos[NUM_TP_HANDLE_TYPES])
+{
+ repos[TP_HANDLE_TYPE_CONTACT] = tp_dynamic_handle_repo_new
+ (TP_HANDLE_TYPE_CONTACT, simple_normalize_contact, NULL);
+}
+
+static GPtrArray *
+create_channel_factories (TpBaseConnection *conn)
+{
+ return g_ptr_array_sized_new (0);
+}
+
+void
+simple_connection_inject_disconnect (SimpleConnection *self)
+{
+ tp_base_connection_change_status ((TpBaseConnection *) self,
+ TP_CONNECTION_STATUS_DISCONNECTED,
+ TP_CONNECTION_STATUS_REASON_REQUESTED);
+}
+
+static gboolean
+pretend_connected (gpointer data)
+{
+ SimpleConnection *self = SIMPLE_CONNECTION (data);
+ TpBaseConnection *conn = (TpBaseConnection *) self;
+ TpHandleRepoIface *contact_repo = tp_base_connection_get_handles (conn,
+ TP_HANDLE_TYPE_CONTACT);
+
+ conn->self_handle = tp_handle_ensure (contact_repo, self->priv->account,
+ NULL, NULL);
+
+ tp_base_connection_change_status (conn, TP_CONNECTION_STATUS_CONNECTED,
+ TP_CONNECTION_STATUS_REASON_REQUESTED);
+
+ return FALSE;
+}
+
+static gboolean
+start_connecting (TpBaseConnection *conn,
+ GError **error)
+{
+ SimpleConnection *self = SIMPLE_CONNECTION (conn);
+
+ tp_base_connection_change_status (conn, TP_CONNECTION_STATUS_CONNECTING,
+ TP_CONNECTION_STATUS_REASON_REQUESTED);
+
+ /* In a real connection manager we'd ask the underlying implementation to
+ * start connecting, then go to state CONNECTED when finished. Here there
+ * isn't actually a connection, so we'll fake a connection process that
+ * takes half a second. */
+ g_timeout_add (500, pretend_connected, self);
+
+ return TRUE;
+}
+
+static gboolean
+pretend_disconnected (gpointer data)
+{
+ tp_base_connection_finish_shutdown (TP_BASE_CONNECTION (data));
+ return FALSE;
+}
+
+static void
+shut_down (TpBaseConnection *conn)
+{
+ /* In a real connection manager we'd ask the underlying implementation to
+ * start shutting down, then call this function when finished. Here there
+ * isn't actually a connection, so we'll fake a disconnection process that
+ * takes half a second. */
+ g_timeout_add (500, pretend_disconnected, conn);
+}
+
+static void
+simple_connection_class_init (SimpleConnectionClass *klass)
+{
+ TpBaseConnectionClass *base_class =
+ (TpBaseConnectionClass *) klass;
+ GObjectClass *object_class = (GObjectClass *) klass;
+ GParamSpec *param_spec;
+ static const gchar *interfaces_always_present[] = { NULL };
+
+ object_class->get_property = get_property;
+ object_class->set_property = set_property;
+ object_class->finalize = finalize;
+ g_type_class_add_private (klass, sizeof (SimpleConnectionPrivate));
+
+ base_class->create_handle_repos = create_handle_repos;
+ base_class->get_unique_connection_name = get_unique_connection_name;
+ base_class->create_channel_factories = create_channel_factories;
+ base_class->start_connecting = start_connecting;
+ base_class->shut_down = shut_down;
+
+ base_class->interfaces_always_present = interfaces_always_present;
+
+ param_spec = g_param_spec_string ("account", "Account name",
+ "The username of this user", NULL,
+ G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
+ G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB);
+ g_object_class_install_property (object_class, PROP_ACCOUNT, param_spec);
+}
diff --git a/tests/lib/simple-conn.h b/tests/lib/simple-conn.h
new file mode 100644
index 0000000..93cd7ef
--- /dev/null
+++ b/tests/lib/simple-conn.h
@@ -0,0 +1,59 @@
+/*
+ * simple-conn.h - header for a simple connection
+ *
+ * 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.
+ */
+
+#ifndef __SIMPLE_CONN_H__
+#define __SIMPLE_CONN_H__
+
+#include <glib-object.h>
+#include <telepathy-glib/base-connection.h>
+
+G_BEGIN_DECLS
+
+typedef struct _SimpleConnection SimpleConnection;
+typedef struct _SimpleConnectionClass SimpleConnectionClass;
+typedef struct _SimpleConnectionPrivate SimpleConnectionPrivate;
+
+struct _SimpleConnectionClass {
+ TpBaseConnectionClass parent_class;
+};
+
+struct _SimpleConnection {
+ TpBaseConnection parent;
+
+ SimpleConnectionPrivate *priv;
+};
+
+GType simple_connection_get_type (void);
+
+/* TYPE MACROS */
+#define SIMPLE_TYPE_CONNECTION \
+ (simple_connection_get_type ())
+#define SIMPLE_CONNECTION(obj) \
+ (G_TYPE_CHECK_INSTANCE_CAST((obj), SIMPLE_TYPE_CONNECTION, \
+ SimpleConnection))
+#define SIMPLE_CONNECTION_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_CAST((klass), SIMPLE_TYPE_CONNECTION, \
+ SimpleConnectionClass))
+#define SIMPLE_IS_CONNECTION(obj) \
+ (G_TYPE_CHECK_INSTANCE_TYPE((obj), SIMPLE_TYPE_CONNECTION))
+#define SIMPLE_IS_CONNECTION_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_TYPE((klass), SIMPLE_TYPE_CONNECTION))
+#define SIMPLE_CONNECTION_GET_CLASS(obj) \
+ (G_TYPE_INSTANCE_GET_CLASS ((obj), SIMPLE_TYPE_CONNECTION, \
+ SimpleConnectionClass))
+
+/* Cause "network events", for debugging/testing */
+
+void simple_connection_inject_disconnect (SimpleConnection *self);
+
+G_END_DECLS
+
+#endif /* #ifndef __SIMPLE_CONN_H__ */
diff --git a/tests/lib/simple-manager.c b/tests/lib/simple-manager.c
new file mode 100644
index 0000000..9cff3f2
--- /dev/null
+++ b/tests/lib/simple-manager.c
@@ -0,0 +1,95 @@
+/*
+ * simple-manager.c - an simple connection manager
+ *
+ * Copyright (C) 2007-2008 Collabora Ltd.
+ * 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 "simple-manager.h"
+
+#include <dbus/dbus-protocol.h>
+#include <dbus/dbus-glib.h>
+
+#include <telepathy-glib/dbus.h>
+#include <telepathy-glib/errors.h>
+
+#include "simple-conn.h"
+
+G_DEFINE_TYPE (SimpleConnectionManager,
+ simple_connection_manager,
+ TP_TYPE_BASE_CONNECTION_MANAGER)
+
+/* type definition stuff */
+
+static void
+simple_connection_manager_init (SimpleConnectionManager *self)
+{
+}
+
+/* private data */
+
+typedef struct {
+ gchar *account;
+} SimpleParams;
+
+static const TpCMParamSpec simple_params[] = {
+ { "account", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING,
+ TP_CONN_MGR_PARAM_FLAG_REQUIRED | TP_CONN_MGR_PARAM_FLAG_REGISTER, NULL,
+ G_STRUCT_OFFSET (SimpleParams, account),
+ tp_cm_param_filter_string_nonempty, NULL },
+
+ { NULL }
+};
+
+static gpointer
+alloc_params (void)
+{
+ return g_slice_new0 (SimpleParams);
+}
+
+static void
+free_params (gpointer p)
+{
+ SimpleParams *params = p;
+
+ g_free (params->account);
+
+ g_slice_free (SimpleParams, params);
+}
+
+static const TpCMProtocolSpec simple_protocols[] = {
+ { "simple", simple_params, alloc_params, free_params },
+ { NULL, NULL }
+};
+
+static TpBaseConnection *
+new_connection (TpBaseConnectionManager *self,
+ const gchar *proto,
+ TpIntSet *params_present,
+ gpointer parsed_params,
+ GError **error)
+{
+ SimpleParams *params = parsed_params;
+ SimpleConnection *conn = SIMPLE_CONNECTION
+ (g_object_new (SIMPLE_TYPE_CONNECTION,
+ "account", params->account,
+ "protocol", proto,
+ NULL));
+
+ return (TpBaseConnection *) conn;
+}
+
+static void
+simple_connection_manager_class_init (SimpleConnectionManagerClass *klass)
+{
+ TpBaseConnectionManagerClass *base_class =
+ (TpBaseConnectionManagerClass *) klass;
+
+ base_class->new_connection = new_connection;
+ base_class->cm_dbus_name = "simple";
+ base_class->protocol_params = simple_protocols;
+}
diff --git a/tests/lib/simple-manager.h b/tests/lib/simple-manager.h
new file mode 100644
index 0000000..748f43c
--- /dev/null
+++ b/tests/lib/simple-manager.h
@@ -0,0 +1,55 @@
+/*
+ * simple-manager.h - header for a simple connection manager
+ * Copyright (C) 2007-2008 Collabora Ltd.
+ * 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.
+ */
+
+#ifndef __SIMPLE_CONNECTION_MANAGER_H__
+#define __SIMPLE_CONNECTION_MANAGER_H__
+
+#include <glib-object.h>
+#include <telepathy-glib/base-connection-manager.h>
+
+G_BEGIN_DECLS
+
+typedef struct _SimpleConnectionManager SimpleConnectionManager;
+typedef struct _SimpleConnectionManagerClass SimpleConnectionManagerClass;
+
+struct _SimpleConnectionManagerClass {
+ TpBaseConnectionManagerClass parent_class;
+
+ gpointer priv;
+};
+
+struct _SimpleConnectionManager {
+ TpBaseConnectionManager parent;
+
+ gpointer priv;
+};
+
+GType simple_connection_manager_get_type (void);
+
+/* TYPE MACROS */
+#define SIMPLE_TYPE_CONNECTION_MANAGER \
+ (simple_connection_manager_get_type ())
+#define SIMPLE_CONNECTION_MANAGER(obj) \
+ (G_TYPE_CHECK_INSTANCE_CAST((obj), SIMPLE_TYPE_CONNECTION_MANAGER, \
+ simpleConnectionManager))
+#define SIMPLE_CONNECTION_MANAGER_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_CAST((klass), SIMPLE_TYPE_CONNECTION_MANAGER, \
+ SimpleConnectionManagerClass))
+#define SIMPLE_IS_CONNECTION_MANAGER(obj) \
+ (G_TYPE_CHECK_INSTANCE_TYPE((obj), SIMPLE_TYPE_CONNECTION_MANAGER))
+#define SIMPLE_IS_CONNECTION_MANAGER_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_TYPE((klass), SIMPLE_TYPE_CONNECTION_MANAGER))
+#define SIMPLE_CONNECTION_MANAGER_GET_CLASS(obj) \
+ (G_TYPE_INSTANCE_GET_CLASS ((obj), SIMPLE_TYPE_CONNECTION_MANAGER, \
+ SimpleConnectionManagerClass))
+
+G_END_DECLS
+
+#endif /* #ifndef __SIMPLE_CONNECTION_MANAGER_H__*/
--
1.5.6.5
More information about the Telepathy-commits
mailing list