From 679ccf8528516d5a4f80cb839adec05b24867700 Mon Sep 17 00:00:00 2001
From: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
Date: Wed, 25 Jan 2012 13:42:39 +0100
Subject: [PATCH] more tp_account_manager_get_most_available_presence() tests

https://bugs.freedesktop.org/show_bug.cgi?id=45120
---
 tests/dbus/account-manager.c |  159 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 159 insertions(+), 0 deletions(-)

diff --git a/tests/dbus/account-manager.c b/tests/dbus/account-manager.c
index b79f813..db7594b 100644
--- a/tests/dbus/account-manager.c
+++ b/tests/dbus/account-manager.c
@@ -38,6 +38,9 @@ typedef struct {
     TpTestsSimpleAccount *account1_service;
     TpTestsSimpleAccount *account2_service;
 
+    TpAccount *account1;
+    TpAccount *account2;
+
     GError *error /* initialized where needed */;
 } Test;
 
@@ -208,6 +211,9 @@ teardown_service (Test *test,
   tp_dbus_daemon_unregister_object (test->dbus, test->account2_service);
   g_object_unref (test->account2_service);
 
+  g_clear_object (&test->account1);
+  g_clear_object (&test->account2);
+
   test->service = NULL;
   teardown (test, data);
 }
@@ -546,6 +552,20 @@ test_ensure (Test *test,
 }
 
 /* tp_account_manager_get_most_available_presence() tests */
+static void
+create_tp_accounts (gpointer script_data,
+    gpointer user_data G_GNUC_UNUSED)
+{
+  Test *test = (Test *) script_data;
+
+  test->account1 = tp_account_manager_ensure_account (test->am, ACCOUNT1_PATH);
+  g_object_ref (test->account1);
+
+  test->account2 = tp_account_manager_ensure_account (test->am, ACCOUNT2_PATH);
+  g_object_ref (test->account2);
+
+  script_continue (test);
+}
 
 static void
 test_prepare_most_available (Test *test,
@@ -568,6 +588,7 @@ test_prepare_most_available (Test *test,
   test_prepare (test, data);
   script_append_action (test, manager_new_action, NULL);
   script_append_action (test, prepare_action, NULL);
+  script_append_action (test, create_tp_accounts, NULL);
 }
 
 typedef struct
@@ -622,6 +643,57 @@ check_presence_action (gpointer script_data,
 }
 
 static void
+account_presence_changed (TpAccount *account,
+    TpConnectionPresenceType presence,
+    const gchar *status,
+    const gchar *message,
+    Test *test)
+{
+  g_signal_handlers_disconnect_by_func (account,
+      account_presence_changed, test);
+
+  script_continue (test);
+}
+
+static void
+change_account_presence (Test *test,
+    TpTestsSimpleAccount *service,
+    TpAccount *account,
+    gpointer user_data)
+{
+  Presence *p = user_data;
+
+  tp_tests_simple_account_set_presence (service,
+      p->presence, p->status, p->message);
+
+  presence_free (p);
+
+  /* Wait for the presence change notification */
+  g_signal_connect (account, "presence-changed",
+      G_CALLBACK (account_presence_changed), test);
+}
+
+static void
+change_account1_presence (gpointer script_data,
+    gpointer user_data)
+{
+  Test *test = script_data;
+
+  change_account_presence (test, test->account1_service,
+      test->account1, user_data);
+}
+
+static void
+change_account2_presence (gpointer script_data,
+    gpointer user_data)
+{
+  Test *test = script_data;
+
+  change_account_presence (test, test->account2_service,
+      test->account2, user_data);
+}
+
+static void
 test_most_available_no_account (Test *test,
     gconstpointer data G_GNUC_UNUSED)
 {
@@ -631,6 +703,85 @@ test_most_available_no_account (Test *test,
       presence_new (TP_CONNECTION_PRESENCE_TYPE_OFFLINE, "offline", ""));
 }
 
+static void
+test_most_available_one_account (Test *test,
+    gconstpointer data G_GNUC_UNUSED)
+{
+  test_prepare_most_available (test, data, 1);
+
+  script_append_action (test, change_account1_presence,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, "available", ""));
+  script_append_action (test, check_presence_action,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, "available", ""));
+}
+
+static void
+test_most_available_two_account (Test *test,
+    gconstpointer data G_GNUC_UNUSED)
+{
+  test_prepare_most_available (test, data, 2);
+
+  script_append_action (test, change_account1_presence,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, "available", ""));
+  script_append_action (test, change_account2_presence,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_AWAY, "away", ""));
+
+  script_append_action (test, check_presence_action,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, "available", ""));
+
+  /* account1 disconnects */
+  script_append_action (test, change_account1_presence,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_OFFLINE, "offline", ""));
+
+  script_append_action (test, check_presence_action,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_AWAY, "away", ""));
+}
+
+static void
+test_most_available_one_unset (Test *test,
+    gconstpointer data G_GNUC_UNUSED)
+{
+  test_prepare_most_available (test, data, 1);
+
+  script_append_action (test, change_account1_presence,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_UNSET, "unset", ""));
+
+  /* Pretend that we are offline */
+  script_append_action (test, check_presence_action,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_OFFLINE, "offline", ""));
+}
+
+static void
+test_most_available_two_unset (Test *test,
+    gconstpointer data G_GNUC_UNUSED)
+{
+  test_prepare_most_available (test, data, 2);
+
+  script_append_action (test, change_account1_presence,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_UNSET, "unset", ""));
+  script_append_action (test, change_account2_presence,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_AWAY, "away", ""));
+
+  /* Use account2 away presence */
+  script_append_action (test, check_presence_action,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_AWAY, "away", ""));
+
+  /* account2 disconnects */
+  script_append_action (test, change_account2_presence,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_OFFLINE, "offline", ""));
+
+  /* Pretent that we are offline */
+  script_append_action (test, check_presence_action,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_OFFLINE, "offline", ""));
+
+  /* account2 reconnects with busy */
+  script_append_action (test, change_account2_presence,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_BUSY, "busy", ""));
+
+  script_append_action (test, check_presence_action,
+      presence_new (TP_CONNECTION_PRESENCE_TYPE_BUSY, "busy", ""));
+}
+
 int
 main (int argc,
     char **argv)
@@ -657,5 +808,13 @@ main (int argc,
 
   g_test_add ("/am/most-available/no-account", Test, NULL, setup_service,
               test_most_available_no_account, teardown_service);
+  g_test_add ("/am/most-available/one-account", Test, NULL, setup_service,
+              test_most_available_one_account, teardown_service);
+  g_test_add ("/am/most-available/two-account", Test, NULL, setup_service,
+              test_most_available_two_account, teardown_service);
+  g_test_add ("/am/most-available/one-unset", Test, NULL, setup_service,
+              test_most_available_one_unset, teardown_service);
+  g_test_add ("/am/most-available/two-unset", Test, NULL, setup_service,
+              test_most_available_two_unset, teardown_service);
   return g_test_run ();
 }
-- 
1.7.7.6