[folks] Allow multiple test accounts to be connected simultaneously



commit b81e26d48507e7fd61b26c9732fb67b150593e20
Author: Philip Withnall <philip withnall collabora co uk>
Date:   Sun Jan 9 21:52:54 2011 +0000

    Allow multiple test accounts to be connected simultaneously
    
    This doesn't affect any existing tests, but will be useful for testing that
    aggregation across different persona stores works correctly.

 tests/lib/telepathy/contactlist/account-manager.c |   50 ++++++-
 tests/lib/telepathy/contactlist/account-manager.h |    5 +
 tests/lib/telepathy/contactlist/backend.c         |  152 +++++++++++++++------
 tests/lib/telepathy/contactlist/backend.h         |    8 +
 tests/telepathy/individual-properties.vala        |    4 +
 tests/telepathy/individual-retrieval.vala         |    4 +
 tests/telepathy/persona-store-capabilities.vala   |    4 +
 7 files changed, 178 insertions(+), 49 deletions(-)
---
diff --git a/tests/lib/telepathy/contactlist/account-manager.c b/tests/lib/telepathy/contactlist/account-manager.c
index 984eee9..26a1a3f 100644
--- a/tests/lib/telepathy/contactlist/account-manager.c
+++ b/tests/lib/telepathy/contactlist/account-manager.c
@@ -33,10 +33,6 @@ G_DEFINE_TYPE_WITH_CODE (TpTestAccountManager,
 /* TP_IFACE_ACCOUNT_MANAGER is implied */
 static const char *ACCOUNT_MANAGER_INTERFACES[] = { NULL };
 
-static const gchar *VALID_ACCOUNTS[] = {
-  "/org/freedesktop/Telepathy/Account/cm/protocol/account",
-  NULL };
-
 static const gchar *INVALID_ACCOUNTS[] = {
   "/org/freedesktop/Telepathy/Account/fakecm/fakeproto/invalidaccount",
   NULL };
@@ -51,7 +47,7 @@ enum
 
 struct _TpTestAccountManagerPrivate
 {
-  int dummy;
+  GPtrArray *valid_accounts;
 };
 
 static void
@@ -84,6 +80,17 @@ tp_test_account_manager_init (TpTestAccountManager *self)
 {
   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
       TP_TEST_TYPE_ACCOUNT_MANAGER, TpTestAccountManagerPrivate);
+
+  self->priv->valid_accounts =
+      g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
+}
+
+static void
+tp_test_account_manager_finalize (GObject *obj)
+{
+  g_ptr_array_free (TP_TEST_ACCOUNT_MANAGER (obj)->priv->valid_accounts, TRUE);
+
+  G_OBJECT_CLASS (tp_test_account_manager_parent_class)->finalize (obj);
 }
 
 static void
@@ -92,6 +99,7 @@ tp_test_account_manager_get_property (GObject *object,
               GValue *value,
               GParamSpec *spec)
 {
+  TpTestAccountManagerPrivate *priv = TP_TEST_ACCOUNT_MANAGER (object)->priv;
   GPtrArray *accounts;
   guint i = 0;
 
@@ -103,8 +111,8 @@ tp_test_account_manager_get_property (GObject *object,
     case PROP_VALID_ACCOUNTS:
       accounts = g_ptr_array_new ();
 
-      for (i=0; VALID_ACCOUNTS[i] != NULL; i++)
-        g_ptr_array_add (accounts, g_strdup (VALID_ACCOUNTS[i]));
+      for (i = 0; i < priv->valid_accounts->len; i++)
+        g_ptr_array_add (accounts, g_strdup (priv->valid_accounts->pdata[i]));
 
       g_value_take_boxed (value, accounts);
       break;
@@ -159,6 +167,7 @@ tp_test_account_manager_class_init (
 
   g_type_class_add_private (klass, sizeof (TpTestAccountManagerPrivate));
   object_class->get_property = tp_test_account_manager_get_property;
+  object_class->finalize = tp_test_account_manager_finalize;
 
   param_spec = g_param_spec_boxed ("interfaces", "Extra D-Bus interfaces",
       "In this case we only implement AccountManager, so none.",
@@ -186,3 +195,30 @@ tp_test_account_manager_new (void)
 {
   return g_object_new (TP_TEST_TYPE_ACCOUNT_MANAGER, NULL);
 }
+
+void
+tp_test_account_manager_add_account (TpTestAccountManager *self,
+    const gchar *account_path)
+{
+  g_ptr_array_add (self->priv->valid_accounts, g_strdup (account_path));
+  g_object_notify (G_OBJECT (self), "valid-accounts");
+}
+
+void
+tp_test_account_manager_remove_account (TpTestAccountManager *self,
+    const gchar *account_path)
+{
+  TpTestAccountManagerPrivate *priv = self->priv;
+  guint i;
+
+  for (i = 0; i < priv->valid_accounts->len; i++)
+    {
+      if (g_strcmp0 (account_path, priv->valid_accounts->pdata[0]) == 0)
+        {
+          g_ptr_array_remove_index_fast (priv->valid_accounts, i);
+          break;
+        }
+    }
+
+  g_object_notify (G_OBJECT (self), "valid-accounts");
+}
diff --git a/tests/lib/telepathy/contactlist/account-manager.h b/tests/lib/telepathy/contactlist/account-manager.h
index c35edb5..a780f97 100644
--- a/tests/lib/telepathy/contactlist/account-manager.h
+++ b/tests/lib/telepathy/contactlist/account-manager.h
@@ -56,6 +56,11 @@ GType tp_test_account_manager_get_type (void);
 
 TpTestAccountManager *tp_test_account_manager_new (void);
 
+void tp_test_account_manager_add_account (TpTestAccountManager *self,
+    const gchar *account_path);
+void tp_test_account_manager_remove_account (TpTestAccountManager *self,
+    const gchar *account_path);
+
 G_END_DECLS
 
 #endif /* #ifndef __TP_TEST_ACCOUNT_MANAGER_H__ */
diff --git a/tests/lib/telepathy/contactlist/backend.c b/tests/lib/telepathy/contactlist/backend.c
index c656cbf..445bbf1 100644
--- a/tests/lib/telepathy/contactlist/backend.c
+++ b/tests/lib/telepathy/contactlist/backend.c
@@ -33,12 +33,17 @@
 struct _TpTestBackendPrivate
 {
   TpDBusDaemon *daemon;
-  TpTestAccount *account;
   TpTestAccountManager *account_manager;
+  GList *accounts;
+};
+
+typedef struct
+{
+  TpTestAccount *account;
   TpBaseConnection *conn;
   gchar *bus_name;
   gchar *object_path;
-};
+} AccountData;
 
 G_DEFINE_TYPE (TpTestBackend, tp_test_backend, G_TYPE_OBJECT)
 
@@ -58,6 +63,14 @@ tp_test_backend_init (TpTestBackend *self)
 static void
 tp_test_backend_finalize (GObject *object)
 {
+  TpTestBackendPrivate *priv = TP_TEST_BACKEND (object)->priv;
+  GList *l;
+
+  for (l = priv->accounts; l != NULL; l = l->next)
+    {
+      tp_test_backend_remove_account (TP_TEST_BACKEND (object), l->data);
+    }
+
   tp_test_backend_tear_down (TP_TEST_BACKEND (object));
   G_OBJECT_CLASS (tp_test_backend_parent_class)->finalize (object);
 }
@@ -73,7 +86,7 @@ tp_test_backend_get_property (GObject *object,
   switch (property_id)
     {
     case PROP_CONNECTION:
-      g_value_set_object (value, self->priv->conn);
+      g_value_set_object (value, tp_test_backend_get_connection (self));
       break;
 
     default:
@@ -141,9 +154,6 @@ void
 tp_test_backend_set_up (TpTestBackend *self)
 {
   TpTestBackendPrivate *priv = self->priv;
-  TpHandleRepoIface *handle_repo;
-  TpHandle self_handle;
-  gchar *object_path;
   GError *error = NULL;
 
   /* Override the handler set in the general Folks.TestCase class */
@@ -154,51 +164,112 @@ tp_test_backend_set_up (TpTestBackend *self)
   if (error != NULL)
     g_error ("Couldn't get D-Bus daemon: %s", error->message);
 
+  /* Create an account manager */
+  tp_dbus_daemon_request_name (priv->daemon, TP_ACCOUNT_MANAGER_BUS_NAME, FALSE,
+      &error);
+  if (error != NULL)
+    {
+      g_error ("Couldn't request account manager bus name '%s': %s",
+          TP_ACCOUNT_MANAGER_BUS_NAME, error->message);
+    }
+
+  priv->account_manager = tp_test_account_manager_new ();
+  tp_dbus_daemon_register_object (priv->daemon, TP_ACCOUNT_MANAGER_OBJECT_PATH,
+      priv->account_manager);
+}
+
+/**
+ * tp_test_backend_add_account:
+ * @self:
+ * @protocol_name:
+ * @user_id:
+ * @connection_manager_name:
+ * @account_name:
+ *
+ * Return value: (transfer none):
+ */
+gpointer
+tp_test_backend_add_account (TpTestBackend *self,
+    const gchar *protocol_name,
+    const gchar *user_id,
+    const gchar *connection_manager_name,
+    const gchar *account_name)
+{
+  TpTestBackendPrivate *priv = self->priv;
+  TpHandleRepoIface *handle_repo;
+  TpHandle self_handle;
+  gchar *object_path;
+  AccountData *data;
+  GError *error = NULL;
+
+  data = g_slice_new (AccountData);
+
   /* Set up a contact list connection */
-  priv->conn =
-      TP_BASE_CONNECTION (tp_test_contact_list_connection_new ("me example com",
-          "protocol", 0, 0));
+  data->conn =
+      TP_BASE_CONNECTION (tp_test_contact_list_connection_new (user_id,
+          protocol_name, 0, 0));
 
-  tp_base_connection_register (priv->conn, "cm", &priv->bus_name,
-      &priv->object_path, &error);
+  tp_base_connection_register (data->conn, connection_manager_name,
+      &data->bus_name, &data->object_path, &error);
   if (error != NULL)
     {
-      g_error ("Failed to register connection %p: %s", priv->conn,
+      g_error ("Failed to register connection %p: %s", data->conn,
           error->message);
     }
 
-  handle_repo = tp_base_connection_get_handles (priv->conn,
+  handle_repo = tp_base_connection_get_handles (data->conn,
       TP_HANDLE_TYPE_CONTACT);
-  self_handle = tp_handle_ensure (handle_repo, "me example com", NULL, &error);
+  self_handle = tp_handle_ensure (handle_repo, user_id, NULL, &error);
   if (error != NULL)
     {
-      g_error ("Couldn't ensure self handle '%s': %s", "me example com",
-              error->message);
+      g_error ("Couldn't ensure self handle '%s': %s", user_id, error->message);
     }
 
-  tp_base_connection_set_self_handle (priv->conn, self_handle);
-  tp_base_connection_change_status (priv->conn,
+  tp_base_connection_set_self_handle (data->conn, self_handle);
+  tp_base_connection_change_status (data->conn,
       TP_CONNECTION_STATUS_CONNECTED, TP_CONNECTION_STATUS_REASON_REQUESTED);
 
   /* Create an account */
-  priv->account = tp_test_account_new (priv->object_path);
+  data->account = tp_test_account_new (data->object_path);
   object_path =
-      g_strdup_printf ("%scm/protocol/account", TP_ACCOUNT_OBJECT_PATH_BASE);
-  tp_dbus_daemon_register_object (priv->daemon, object_path, priv->account);
+      g_strdup_printf ("%s%s/%s/%s", TP_ACCOUNT_OBJECT_PATH_BASE,
+          connection_manager_name, protocol_name, account_name);
+  tp_dbus_daemon_register_object (priv->daemon, object_path, data->account);
   g_free (object_path);
 
-  /* Create an account manager */
-  tp_dbus_daemon_request_name (priv->daemon, TP_ACCOUNT_MANAGER_BUS_NAME, FALSE,
-      &error);
-  if (error != NULL)
+  /* Add the account to the list of accounts and return a handle to it */
+  priv->accounts = g_list_prepend (priv->accounts, data);
+
+  return data;
+}
+
+void
+tp_test_backend_remove_account (TpTestBackend *self,
+    gpointer handle)
+{
+  TpTestBackendPrivate *priv = self->priv;
+  AccountData *data;
+
+  if (g_list_find (priv->accounts, handle) == NULL)
     {
-      g_error ("Couldn't request account manager bus name '%s': %s",
-          TP_ACCOUNT_MANAGER_BUS_NAME, error->message);
+      return;
     }
 
-  priv->account_manager = tp_test_account_manager_new ();
-  tp_dbus_daemon_register_object (priv->daemon, TP_ACCOUNT_MANAGER_OBJECT_PATH,
-      priv->account_manager);
+  /* Remove the account from the list of accounts */
+  priv->accounts = g_list_remove (priv->accounts, handle);
+  data = (AccountData *) handle;
+
+  /* Disconnect it */
+  tp_base_connection_change_status (data->conn,
+      TP_CONNECTION_STATUS_DISCONNECTED, TP_CONNECTION_STATUS_REASON_REQUESTED);
+
+  tp_dbus_daemon_unregister_object (priv->daemon, data->account);
+  tp_clear_object (&data->account);
+
+  tp_clear_object (&data->conn);
+
+  g_free (data->bus_name);
+  g_free (data->object_path);
 }
 
 void
@@ -207,9 +278,6 @@ tp_test_backend_tear_down (TpTestBackend *self)
   TpTestBackendPrivate *priv = self->priv;
   GError *error = NULL;
 
-  tp_base_connection_change_status (priv->conn,
-      TP_CONNECTION_STATUS_DISCONNECTED, TP_CONNECTION_STATUS_REASON_REQUESTED);
-
   tp_dbus_daemon_unregister_object (priv->daemon, priv->account_manager);
   tp_clear_object (&priv->account_manager);
 
@@ -221,15 +289,7 @@ tp_test_backend_tear_down (TpTestBackend *self)
           TP_ACCOUNT_MANAGER_BUS_NAME, error->message);
     }
 
-  tp_dbus_daemon_unregister_object (priv->daemon, priv->account);
-  tp_clear_object (&priv->account);
-
-  tp_clear_object (&priv->conn);
   tp_clear_object (&priv->daemon);
-  g_free (priv->bus_name);
-  priv->bus_name = NULL;
-  g_free (priv->object_path);
-  priv->object_path = NULL;
 }
 
 /**
@@ -241,7 +301,15 @@ tp_test_backend_tear_down (TpTestBackend *self)
 TpTestContactListConnection *
 tp_test_backend_get_connection (TpTestBackend *self)
 {
+  AccountData *data;
+
   g_return_val_if_fail (TP_TEST_IS_BACKEND (self), NULL);
 
-  return TP_TEST_CONTACT_LIST_CONNECTION (self->priv->conn);
+  if (self->priv->accounts == NULL)
+    {
+      return NULL;
+    }
+
+  data = (AccountData *) self->priv->accounts->data;
+  return TP_TEST_CONTACT_LIST_CONNECTION (data->conn);
 }
diff --git a/tests/lib/telepathy/contactlist/backend.h b/tests/lib/telepathy/contactlist/backend.h
index 40181df..4e8f01d 100644
--- a/tests/lib/telepathy/contactlist/backend.h
+++ b/tests/lib/telepathy/contactlist/backend.h
@@ -64,6 +64,14 @@ void tp_test_backend_tear_down (TpTestBackend *self);
 TpTestContactListConnection *tp_test_backend_get_connection (
     TpTestBackend *self);
 
+gpointer tp_test_backend_add_account (TpTestBackend *self,
+    const gchar *protocol_name,
+    const gchar *user_id,
+    const gchar *connection_manager_name,
+    const gchar *account_name);
+void tp_test_backend_remove_account (TpTestBackend *self,
+    gpointer handle);
+
 G_END_DECLS
 
 #endif /* !TP_TEST_BACKEND_H */
diff --git a/tests/telepathy/individual-properties.vala b/tests/telepathy/individual-properties.vala
index 296e16c..3b7c661 100644
--- a/tests/telepathy/individual-properties.vala
+++ b/tests/telepathy/individual-properties.vala
@@ -8,6 +8,7 @@ using Gee;
 public class IndividualPropertiesTests : Folks.TestCase
 {
   private TpTest.Backend tp_backend;
+  private void* _account_handle;
   private string individual_id_prefix = "telepathy:protocol:";
 
   public IndividualPropertiesTests ()
@@ -27,10 +28,13 @@ public class IndividualPropertiesTests : Folks.TestCase
   public override void set_up ()
     {
       this.tp_backend.set_up ();
+      this._account_handle = this.tp_backend.add_account ("protocol",
+          "me example com", "cm", "account");
     }
 
   public override void tear_down ()
     {
+      this.tp_backend.remove_account (this._account_handle);
       this.tp_backend.tear_down ();
     }
 
diff --git a/tests/telepathy/individual-retrieval.vala b/tests/telepathy/individual-retrieval.vala
index 8851384..d39ccb2 100644
--- a/tests/telepathy/individual-retrieval.vala
+++ b/tests/telepathy/individual-retrieval.vala
@@ -8,6 +8,7 @@ using Gee;
 public class IndividualRetrievalTests : Folks.TestCase
 {
   private TpTest.Backend tp_backend;
+  private void* _account_handle;
   private HashSet<string> default_individuals;
   private string individual_id_prefix = "telepathy:protocol:";
 
@@ -37,10 +38,13 @@ public class IndividualRetrievalTests : Folks.TestCase
   public override void set_up ()
     {
       this.tp_backend.set_up ();
+      this._account_handle = this.tp_backend.add_account ("protocol",
+          "me example com", "cm", "account");
     }
 
   public override void tear_down ()
     {
+      this.tp_backend.remove_account (this._account_handle);
       this.tp_backend.tear_down ();
     }
 
diff --git a/tests/telepathy/persona-store-capabilities.vala b/tests/telepathy/persona-store-capabilities.vala
index f031f26..4f6f392 100644
--- a/tests/telepathy/persona-store-capabilities.vala
+++ b/tests/telepathy/persona-store-capabilities.vala
@@ -8,6 +8,7 @@ using Gee;
 public class PersonaStoreCapabilitiesTests : Folks.TestCase
 {
   private TpTest.Backend tp_backend;
+  private void* _account_handle;
   private HashSet<string> group_flags_received;
 
   public PersonaStoreCapabilitiesTests ()
@@ -25,10 +26,13 @@ public class PersonaStoreCapabilitiesTests : Folks.TestCase
       this.group_flags_received = new HashSet<string> (str_hash, str_equal);
 
       this.tp_backend.set_up ();
+      this._account_handle = this.tp_backend.add_account ("protocol",
+          "me example com", "cm", "account");
     }
 
   public override void tear_down ()
     {
+      this.tp_backend.remove_account (this._account_handle);
       this.tp_backend.tear_down ();
     }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]