[folks] tests: Simplify EDS details tests



commit 023a56394101738c57d9b3aa3698bc58f62b66a9
Author: Philip Withnall <philip withnall collabora co uk>
Date:   Wed Jun 17 17:11:16 2015 +0100

    tests: Simplify EDS details tests
    
    Drastically simplify the tests for retrieving persona details from
    Edsf.Personas, by using some of the newer test utility functions in
    libfolks. This has the useful side effect of fixing a common race
    condition where the test’s individuals-changed callback would be called
    during test teardown, and assertions in it would fail because it didn’t
    expect any personas to be removed.

 tests/eds/avatar-details.vala         |  111 ++++++---------------
 tests/eds/email-details.vala          |  157 ++++++++---------------------
 tests/eds/im-details.vala             |  124 ++++++-----------------
 tests/eds/link-personas.vala          |    6 -
 tests/eds/name-details.vala           |  178 ++++++++++++---------------------
 tests/eds/phone-details.vala          |  159 +++++++++--------------------
 tests/eds/postal-address-details.vala |  143 +++++++++------------------
 7 files changed, 264 insertions(+), 614 deletions(-)
---
diff --git a/tests/eds/avatar-details.vala b/tests/eds/avatar-details.vala
index d7f341f..a4ab1a4 100644
--- a/tests/eds/avatar-details.vala
+++ b/tests/eds/avatar-details.vala
@@ -25,12 +25,6 @@ using Gee;
 
 public class AvatarDetailsTests : EdsTest.TestCase
 {
-  private GLib.MainLoop _main_loop;
-  private IndividualAggregator _aggregator;
-  private Gee.HashMap<string, Value?> _c1;
-  private bool _avatars_are_equal;
-  private string _avatar_path;
-
   public AvatarDetailsTests ()
     {
       base ("AvatarDetails");
@@ -40,95 +34,52 @@ public class AvatarDetailsTests : EdsTest.TestCase
 
   public void test_avatar ()
     {
-      this._c1 = new Gee.HashMap<string, Value?> ();
-      this._main_loop = new GLib.MainLoop (null, false);
-      this._avatar_path = Folks.TestUtils.get_source_test_data (
-          "data/avatar-01.jpg");
-      this._avatars_are_equal = false;
+      /* Set up the backend. */
+      var c1 = new Gee.HashMap<string, Value?> ();
+      var avatar_path =
+          Folks.TestUtils.get_source_test_data ("data/avatar-01.jpg");
       Value? v;
 
-      this.eds_backend.reset ();
-
       v = Value (typeof (string));
       v.set_string ("bernie h. innocenti");
-      this._c1.set ("full_name", (owned) v);
+      c1.set ("full_name", (owned) v);
       v = Value (typeof (string));
-      v.set_string (this._avatar_path);
-      this._c1.set ("avatar",(owned) v);
-      this.eds_backend.add_contact (this._c1);
+      v.set_string (avatar_path);
+      c1.set ("avatar", (owned) v);
 
-      this._test_avatar_async.begin ();
+      this.eds_backend.add_contact (c1);
+      this.eds_backend.commit_contacts_to_addressbook_sync ();
 
-      TestUtils.loop_run_with_timeout (this._main_loop);
+      /* Set up the test variables. */
+      var avatars_are_equal = false;
 
-      assert (this._avatars_are_equal);
-   }
+      /* Set up the aggregator and wait until either the expected personas are
+       * seen, or the test times out and fails. */
+      var aggregator = IndividualAggregator.dup ();
+      TestUtils.aggregator_prepare_and_wait_for_individuals_sync_with_timeout (
+          aggregator, {"bernie h. innocenti"});
 
-  private async void _test_avatar_async ()
-    {
-
-      yield this.eds_backend.commit_contacts_to_addressbook ();
-
-      var store = BackendStore.dup ();
-      yield store.prepare ();
-      this._aggregator = IndividualAggregator.dup ();
-      this._aggregator.individuals_changed_detailed.connect
-          (this._individuals_changed_cb);
-      try
-        {
-          yield this._aggregator.prepare ();
-        }
-      catch (GLib.Error e)
+      /* Check the properties of our individual. */
+      var i = TestUtils.get_individual_by_name (aggregator,
+          "bernie h. innocenti");
+      if (i.avatar != null)
         {
-          GLib.warning ("Error when calling prepare: %s\n", e.message);
-        }
-    }
+          var b = new FileIcon (File.new_for_path (avatar_path));
 
-  private void _individuals_changed_cb (
-       MultiMap<Individual?, Individual?> changes)
-    {
-      var added = changes.get_values ();
-      var removed = changes.get_keys ();
-
-      assert (removed.size == 1);
+          var main_loop = new MainLoop ();
+          TestUtils.loadable_icons_content_equal.begin (i.avatar, b, -1,
+              (s, r) =>
+            {
+              avatars_are_equal =
+                  TestUtils.loadable_icons_content_equal.end (r);
+              main_loop.quit ();
+            });
 
-      foreach (var i in removed)
-        {
-          assert (i == null);
+          TestUtils.loop_run_with_timeout (main_loop);
         }
 
-      foreach (Individual i in added)
-        {
-          assert (i != null);
-
-          assert (i.personas.size == 1);
-
-          if (i.full_name == "bernie h. innocenti")
-            {
-              i.notify["avatar"].connect (this._notify_cb);
-              this._check_avatar.begin (i.avatar);
-            }
-        }
+      assert (avatars_are_equal);
    }
-
-  private void _notify_cb (Object individual_obj, ParamSpec ps)
-    {
-      Folks.Individual i = (Folks.Individual) individual_obj;
-      this._check_avatar.begin (i.avatar);
-    }
-
-  private async void _check_avatar (LoadableIcon? avatar)
-    {
-      if (avatar != null)
-        {
-          var b = new FileIcon (File.new_for_path (this._avatar_path));
-
-          this._avatars_are_equal =
-            yield TestUtils.loadable_icons_content_equal (
-                avatar, b, -1);
-          this._main_loop.quit ();
-        }
-    }
 }
 
 public int main (string[] args)
diff --git a/tests/eds/email-details.vala b/tests/eds/email-details.vala
index 17618b2..ee44ef5 100644
--- a/tests/eds/email-details.vala
+++ b/tests/eds/email-details.vala
@@ -25,14 +25,6 @@ using Gee;
 
 public class EmailDetailsTests : EdsTest.TestCase
 {
-  private GLib.MainLoop _main_loop;
-  private IndividualAggregator _aggregator;
-  private int _email_count;
-  private HashSet<string> _email_types;
-  private Gee.HashMap<string, Value?> _c1;
-  private Gee.HashMap<string, Value?> _c2;
-  private Gee.HashMap<string, Value?> _c3;
-
   public EmailDetailsTests ()
     {
       base ("EmailDetails");
@@ -42,149 +34,82 @@ public class EmailDetailsTests : EdsTest.TestCase
 
   public void test_email_details ()
     {
-      this._email_count = 0;
-      this._email_types = new HashSet<string> ();
-      this._c1 = new Gee.HashMap<string, Value?> ();
-      this._c2 = new Gee.HashMap<string, Value?> ();
-      this._c3 = new Gee.HashMap<string, Value?> ();
-      this._main_loop = new GLib.MainLoop (null, false);
+      /* Set up the backend. */
+      var c1 = new Gee.HashMap<string, Value?> ();
+      var c2 = new Gee.HashMap<string, Value?> ();
+      var c3 = new Gee.HashMap<string, Value?> ();
       Value? v;
 
-      this.eds_backend.reset ();
-
       v = Value (typeof (string));
       v.set_string ("bernie h. innocenti");
-      this._c1.set ("full_name", (owned) v);
+      c1.set ("full_name", (owned) v);
       v = Value (typeof (string));
       v.set_string ("bernie example org");
-      this._c1.set (Edsf.Persona.email_fields[0], (owned) v);
+      c1.set (Edsf.Persona.email_fields[0], (owned) v);
       v = Value (typeof (string));
       v.set_string ("bernie innocenti example org");
-      this._c1.set (Edsf.Persona.email_fields[1], (owned) v);
-      this.eds_backend.add_contact (this._c1);
+      c1.set (Edsf.Persona.email_fields[1], (owned) v);
+
+      this.eds_backend.add_contact (c1);
 
       v = Value (typeof (string));
       v.set_string ("richard m. stallman");
-      this._c2.set ("full_name", (owned) v);
+      c2.set ("full_name", (owned) v);
       v = Value (typeof (string));
       v.set_string ("rms example org");
-      this._c2.set (Edsf.Persona.email_fields[0], (owned) v);
+      c2.set (Edsf.Persona.email_fields[0], (owned) v);
       v = Value (typeof (string));
       v.set_string ("rms 1 example org");
-      this._c2.set (Edsf.Persona.email_fields[1], (owned) v);
-      this.eds_backend.add_contact (this._c2);
+      c2.set (Edsf.Persona.email_fields[1], (owned) v);
+
+      this.eds_backend.add_contact (c2);
 
       v = Value (typeof (string));
       v.set_string ("foo bar");
-      this._c3.set ("full_name", (owned) v);
+      c3.set ("full_name", (owned) v);
       v = Value (typeof (string));
       v.set_string ("foo example org");
-      this._c3.set (Edsf.Persona.email_fields[0], (owned) v);
+      c3.set (Edsf.Persona.email_fields[0], (owned) v);
       v = Value (typeof (string));
       v.set_string ("foo bar example org");
-      this._c3.set (Edsf.Persona.email_fields[1], (owned) v);
-      this.eds_backend.add_contact (this._c3);
+      c3.set (Edsf.Persona.email_fields[1], (owned) v);
 
-      this._test_email_details_async.begin ();
+      this.eds_backend.add_contact (c3);
 
-      TestUtils.loop_run_with_timeout (this._main_loop);
+      this.eds_backend.commit_contacts_to_addressbook_sync ();
 
-      assert (this._email_count == 6);
-      assert (this._email_types.size == 1);
-      assert (this._c1.size == 0);
-      assert (this._c2.size == 0);
-      assert (this._c3.size == 0);
-
-      foreach (var pt in this._email_types)
-        {
-          assert (pt == AbstractFieldDetails.PARAM_TYPE_OTHER);
-        }
-    }
+      /* Set up the aggregator and wait until either the expected personas are
+       * seen, or the test times out and fails. */
+      var aggregator = IndividualAggregator.dup ();
+      TestUtils.aggregator_prepare_and_wait_for_individuals_sync_with_timeout (
+          aggregator,
+          {"bernie h. innocenti", "richard m. stallman", "foo bar"});
 
-  private async void _test_email_details_async ()
-    {
+      /* Check the properties of our individuals. */
+      var i1 = TestUtils.get_individual_by_name (aggregator,
+          "bernie h. innocenti");
+      this._check_email_details (i1, 2);
 
-      yield this.eds_backend.commit_contacts_to_addressbook ();
+      var i2 = TestUtils.get_individual_by_name (aggregator,
+          "richard m. stallman");
+      this._check_email_details (i2, 2);
 
-      var store = BackendStore.dup ();
-      yield store.prepare ();
-      this._aggregator = IndividualAggregator.dup ();
-      this._aggregator.individuals_changed_detailed.connect
-          (this._individuals_changed_cb);
-      try
-        {
-          yield this._aggregator.prepare ();
-        }
-      catch (GLib.Error e)
-        {
-          GLib.warning ("Error when calling prepare: %s\n", e.message);
-        }
+      var i3 = TestUtils.get_individual_by_name (aggregator,
+          "foo bar");
+      this._check_email_details (i3, 2);
     }
 
-  private void _individuals_changed_cb (
-       MultiMap<Individual?, Individual?> changes)
+  private void _check_email_details (Individual i, uint n_expected)
     {
-      var added = changes.get_values ();
-      var removed = changes.get_keys ();
+      var email_owner = (Folks.EmailDetails) i;
 
-      foreach (Individual i in added)
+      foreach (var e in email_owner.email_addresses)
         {
-          assert (i != null);
-
-          unowned Gee.HashMap<string, Value?> contact = null;
-          assert (i.personas.size == 1);
-
-          if (i.full_name == "bernie h. innocenti")
-            {
-              contact = this._c1;
-            }
-          else if (i.full_name == "richard m. stallman")
-            {
-              contact = this._c2;
-            }
-          else if (i.full_name == "foo bar")
-            {
-              contact = this._c3;
-            }
-
-          if (contact == null)
-            {
-              continue;
-            }
-
-          contact.unset ("full_name");
-          var email_owner = (Folks.EmailDetails) i;
-          foreach (var e in email_owner.email_addresses)
-            {
-              this._email_count++;
-
-              bool found = false;
-              for (int j = 0; j < 2; j++) {
-                  var v = Edsf.Persona.email_fields[j];
-                  if (contact.get (v) != null &&
-                      contact.get (v).get_string () == e.value) {
-                      found = true;
-                      contact.unset (v);
-                  }
-              }
-              assert (found);
-              foreach (var v in e.get_parameter_values (AbstractFieldDetails.PARAM_TYPE))
-                {
-                  this._email_types.add (v);
-                }
-            }
-
-          /* Finished? */
-          if (this._email_count == 6)
-              this._main_loop.quit ();
+          foreach (var v in e.get_parameter_values (AbstractFieldDetails.PARAM_TYPE))
+              assert (v == AbstractFieldDetails.PARAM_TYPE_OTHER);
         }
 
-      assert (removed.size == 1);
-
-      foreach (var i in removed)
-        {
-          assert (i == null);
-        }
+      assert (email_owner.email_addresses.size == n_expected);
     }
 }
 
diff --git a/tests/eds/im-details.vala b/tests/eds/im-details.vala
index 230e8f2..d71b3bd 100644
--- a/tests/eds/im-details.vala
+++ b/tests/eds/im-details.vala
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Collabora Ltd.
+ * Copyright (C) 2011, 2015 Collabora Ltd.
  *
  * This library is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as published by
@@ -15,6 +15,7 @@
  * along with this library.  If not, see <http://www.gnu.org/licenses/>.
  *
  * Authors: Raul Gutierrez Segales <raul gutierrez segales collabora co uk>
+ *          Philip Withnall <philip withnall collabora co uk>
  *
  */
 
@@ -23,14 +24,6 @@ using Gee;
 
 public class ImDetailsTests : EdsTest.TestCase
 {
-  private GLib.MainLoop _main_loop;
-  private IndividualAggregator _aggregator;
-  private int _num_addrs;
-  private bool _found_addr_1;
-  private bool _found_addr_2;
-  private string _fullname;
-  private string _im_addrs;
-
   public ImDetailsTests ()
     {
       base ("ImDetailsTests");
@@ -41,107 +34,52 @@ public class ImDetailsTests : EdsTest.TestCase
 
   public void test_im_details_interface ()
     {
-      this._main_loop = new GLib.MainLoop (null, false);
-      Gee.HashMap<string, Value?> c1 = new Gee.HashMap<string, Value?> ();
-      this._fullname = "persona #1";
-      this._im_addrs = "im_jabber_home_1#test1 example org";
-      this._im_addrs += ",im_yahoo_home_1#test2 example org";
+      /* Set up the backend. */
+      var c1 = new Gee.HashMap<string, Value?> ();
+      var im_addrs = "im_jabber_home_1#test1 example org";
+      im_addrs += ",im_yahoo_home_1#test2 example org";
       Value? v;
 
-      this.eds_backend.reset ();
-
       v = Value (typeof (string));
-      v.set_string (this._fullname);
+      v.set_string ("persona #1");
       c1.set ("full_name", (owned) v);
       v = Value (typeof (string));
-      v.set_string (this._im_addrs);
+      v.set_string (im_addrs);
       c1.set ("im_addresses", (owned) v);
 
       this.eds_backend.add_contact (c1);
+      this.eds_backend.commit_contacts_to_addressbook_sync ();
 
-      this._num_addrs = 0;
-      this._found_addr_1 = false;
-      this._found_addr_2 = false;
-
-      this._test_im_details_interface_async.begin ();
-
-      TestUtils.loop_run_with_timeout (this._main_loop);
-
-      assert (this._num_addrs == 2);
-      assert (this._found_addr_1 == true);
-      assert (this._found_addr_2 == true);
-    }
-
-  private async void _test_im_details_interface_async ()
-    {
+      /* Set up the test variables. */
+      var found_addr_1 = false;
+      var found_addr_2 = false;
 
-      yield this.eds_backend.commit_contacts_to_addressbook ();
+      /* Set up the aggregator and wait until either the expected personas are
+       * seen, or the test times out and fails. */
+      var aggregator = IndividualAggregator.dup ();
+      TestUtils.aggregator_prepare_and_wait_for_individuals_sync_with_timeout (
+          aggregator, {"persona #1"});
 
-      var store = BackendStore.dup ();
-      yield store.prepare ();
-      this._aggregator = IndividualAggregator.dup ();
-      this._aggregator.individuals_changed_detailed.connect
-          (this._individuals_changed_cb);
-      try
-        {
-          yield this._aggregator.prepare ();
-        }
-      catch (GLib.Error e)
+      /* Check the properties of our individual. */
+      var i = TestUtils.get_individual_by_name (aggregator, "persona #1");
+      foreach (var proto in i.im_addresses.get_keys ())
         {
-          GLib.warning ("Error when calling prepare: %s\n", e.message);
-        }
-    }
+          var addrs = i.im_addresses.get (proto);
 
-  private void _individuals_changed_cb (
-       MultiMap<Individual?, Individual?> changes)
-    {
-      var added = changes.get_values ();
-      var removed = changes.get_keys ();
-
-      foreach (var i in added)
-        {
-          assert (i != null);
-
-          string full_name = i.full_name;
-          if (full_name == this._fullname)
+          if (proto == "jabber")
             {
-              foreach (var proto in i.im_addresses.get_keys ())
-                {
-                  var addrs = i.im_addresses.get (proto);
-
-                  if (proto == "jabber")
-                    {
-                      if (addrs.contains (
-                            new ImFieldDetails ("test1 example org")))
-                        {
-                          this._found_addr_1 = true;
-                          this._num_addrs++;
-                        }
-                    }
-                  else if (proto == "yahoo")
-                    {
-                      if (addrs.contains (
-                            new ImFieldDetails ("test2 example org")))
-                        {
-                          this._found_addr_2 = true;
-                          this._num_addrs++;
-                        }
-                    }
-                }
+              found_addr_1 =
+                  addrs.contains (new ImFieldDetails ("test1 example org"));
+            }
+          else if (proto == "yahoo")
+            {
+              found_addr_2 =
+                  addrs.contains (new ImFieldDetails ("test2 example org"));
             }
         }
 
-      assert (removed.size == 1);
-
-      foreach (var i in removed)
-        {
-          assert (i == null);
-        }
-
-      if (this._num_addrs == 2 &&
-          this._found_addr_1 == true &&
-          this._found_addr_2 == true)
-        this._main_loop.quit ();
+      assert (found_addr_1);
+      assert (found_addr_2);
     }
 }
 
diff --git a/tests/eds/link-personas.vala b/tests/eds/link-personas.vala
index a2501b5..288fd8c 100644
--- a/tests/eds/link-personas.vala
+++ b/tests/eds/link-personas.vala
@@ -271,12 +271,6 @@ public class LinkPersonasTests : EdsTest.TestCase
   private void _individuals_changed_cb (
        MultiMap<Individual?, Individual?> changes)
     {
-      this._individuals_changed_async (changes);
-    }
-
-  private void _individuals_changed_async (
-       MultiMap<Individual?, Individual?> changes)
-    {
       var added = changes.get_values ();
 
       foreach (var i in added)
diff --git a/tests/eds/name-details.vala b/tests/eds/name-details.vala
index 9f5cc28..6908bbf 100644
--- a/tests/eds/name-details.vala
+++ b/tests/eds/name-details.vala
@@ -25,12 +25,6 @@ using Gee;
 
 public class NameDetailsTests : EdsTest.TestCase
 {
-  private GLib.MainLoop _main_loop;
-  private IndividualAggregator _aggregator;
-  private int _names_count;
-  private Gee.HashMap<string, Value?> _c1;
-  private Gee.HashMap<string, Value?> _c2;
-
   public NameDetailsTests ()
     {
       base ("NameDetails");
@@ -40,158 +34,114 @@ public class NameDetailsTests : EdsTest.TestCase
 
   public void test_names ()
     {
-      this._c1 = new Gee.HashMap<string, Value?> ();
-      this._c2 = new Gee.HashMap<string, Value?> ();
-      this._names_count = 0;
-      this._main_loop = new GLib.MainLoop (null, false);
+      /* Set up the backend. */
+      var c1 = new Gee.HashMap<string, Value?> ();
+      var c2 = new Gee.HashMap<string, Value?> ();
       Value? v;
 
-      this.eds_backend.reset ();
-
       /* FIXME: passing the EContactName would be better */
       v = Value (typeof (string));
       v.set_string ("bernie h. innocenti");
-      this._c1.set ("full_name", (owned) v);
+      c1.set ("full_name", (owned) v);
       v = Value (typeof (string));
       v.set_string ("bernie");
-      this._c1.set ("nickname", (owned) v);
+      c1.set ("nickname", (owned) v);
       v = Value (typeof (string));
       v.set_string ("Innocenti");
-      this._c1.set ("contact_name_family", (owned) v);
+      c1.set ("contact_name_family", (owned) v);
       v = Value (typeof (string));
       v.set_string ("Bernardo");
-      this._c1.set ("contact_name_given", (owned) v);
+      c1.set ("contact_name_given", (owned) v);
       v = Value (typeof (string));
       v.set_string ("H.");
-      this._c1.set ("contact_name_additional", (owned) v);
+      c1.set ("contact_name_additional", (owned) v);
       v = Value (typeof (string));
       v.set_string ("Mr.");
-      this._c1.set ("contact_name_prefixes", (owned) v);
+      c1.set ("contact_name_prefixes", (owned) v);
       v = Value (typeof (string));
       v.set_string ("(sysadmin FSF)");
-      this._c1.set ("contact_name_suffixes", (owned) v);
-      this.eds_backend.add_contact (this._c1);
+      c1.set ("contact_name_suffixes", (owned) v);
+
+      this.eds_backend.add_contact (c1);
 
       v = Value (typeof (string));
       v.set_string ("richard m. stallman");
-      this._c2.set ("full_name", (owned) v);
+      c2.set ("full_name", (owned) v);
       v = Value (typeof (string));
       v.set_string ("Stallman");
-      this._c2.set ("contact_name_family", (owned) v);
+      c2.set ("contact_name_family", (owned) v);
       v = Value (typeof (string));
       v.set_string ("Richard M.");
-      this._c2.set ("contact_name_given", (owned) v);
-      this.eds_backend.add_contact (this._c2);
-
-      this._test_names_async.begin ();
-
-      TestUtils.loop_run_with_timeout (this._main_loop);
-
-      assert (this._names_count == 2);
-      assert (this._c1.size == 0);
-      assert (this._c2.size == 0);
-    }
-
-  private async void _test_names_async ()
-    {
+      c2.set ("contact_name_given", (owned) v);
 
-      yield this.eds_backend.commit_contacts_to_addressbook ();
-
-      var store = BackendStore.dup ();
-      yield store.prepare ();
-      this._aggregator = IndividualAggregator.dup ();
-      this._aggregator.individuals_changed_detailed.connect
-          (this._individuals_changed_cb);
-      try
-        {
-          yield this._aggregator.prepare ();
-        }
-      catch (GLib.Error e)
-        {
-          GLib.warning ("Error when calling prepare: %s\n", e.message);
-        }
-    }
-
-  private void _individuals_changed_cb (
-       MultiMap<Individual?, Individual?> changes)
-    {
-      var added = changes.get_values ();
-      var removed = changes.get_keys ();
+      this.eds_backend.add_contact (c2);
 
-      foreach (Individual i in added)
-        {
-          assert (i != null);
+      this.eds_backend.commit_contacts_to_addressbook_sync ();
 
-          string s;
+      /* Set up the aggregator and wait until either the expected personas are
+       * seen, or the test times out and fails. */
+      var aggregator = IndividualAggregator.dup ();
+      TestUtils.aggregator_prepare_and_wait_for_individuals_sync_with_timeout (
+          aggregator, {"bernie h. innocenti", "richard m. stallman"});
 
-          assert (i.personas.size == 1);
+      /* Check the properties of our individuals. */
+      string s;
 
-          var name = (Folks.NameDetails) i;
+      var i1 = TestUtils.get_individual_by_name (aggregator,
+          "bernie h. innocenti");
+      var name1 = (Folks.NameDetails) i1;
 
-          if (i.full_name == "bernie h. innocenti")
-            {
-              assert (name.structured_name.is_empty () == false);
+      assert (name1.structured_name.is_empty () == false);
 
-              s = this._c1.get ("full_name").get_string ();
-              assert (name.full_name == s);
-              this._c1.unset ("full_name");
+      s = c1.get ("full_name").get_string ();
+      assert (name1.full_name == s);
+      c1.unset ("full_name");
 
-              s = this._c1.get ("nickname").get_string ();
-              assert (name.nickname == s);
-              this._c1.unset ("nickname");
+      s = c1.get ("nickname").get_string ();
+      assert (name1.nickname == s);
+      c1.unset ("nickname");
 
-              s = this._c1.get ("contact_name_family").get_string ();
-              assert (name.structured_name.family_name == s);
-              this._c1.unset ("contact_name_family");
+      s = c1.get ("contact_name_family").get_string ();
+      assert (name1.structured_name.family_name == s);
+      c1.unset ("contact_name_family");
 
-              s = this._c1.get ("contact_name_given").get_string ();
-              assert (name.structured_name.given_name == s);
-              this._c1.unset ("contact_name_given");
+      s = c1.get ("contact_name_given").get_string ();
+      assert (name1.structured_name.given_name == s);
+      c1.unset ("contact_name_given");
 
-              s = this._c1.get ("contact_name_additional").get_string ();
-              assert (name.structured_name.additional_names == s);
-              this._c1.unset ("contact_name_additional");
+      s = c1.get ("contact_name_additional").get_string ();
+      assert (name1.structured_name.additional_names == s);
+      c1.unset ("contact_name_additional");
 
-              s = this._c1.get ("contact_name_prefixes").get_string ();
-              assert (name.structured_name.prefixes == s);
-              this._c1.unset ("contact_name_prefixes");
+      s = c1.get ("contact_name_prefixes").get_string ();
+      assert (name1.structured_name.prefixes == s);
+      c1.unset ("contact_name_prefixes");
 
-              s = this._c1.get ("contact_name_suffixes").get_string ();
-              assert (name.structured_name.suffixes == s);
-              this._c1.unset ("contact_name_suffixes");
-            }
-          else if (i.full_name == "richard m. stallman")
-            {
-              assert (name.structured_name.is_empty () == false);
+      s = c1.get ("contact_name_suffixes").get_string ();
+      assert (name1.structured_name.suffixes == s);
+      c1.unset ("contact_name_suffixes");
 
-              s = this._c2.get ("full_name").get_string ();
-              assert (name.full_name == s);
-              this._c2.unset ("full_name");
+      assert (c1.size == 0);
 
-              s = this._c2.get ("contact_name_family").get_string ();
-              assert (name.structured_name.family_name == s);
-              this._c2.unset ("contact_name_family");
+      var i2 = TestUtils.get_individual_by_name (aggregator,
+          "richard m. stallman");
+      var name2 = (Folks.NameDetails) i2;
 
-              s = this._c2.get ("contact_name_given").get_string ();
-              assert (name.structured_name.given_name == s);
-              this._c2.unset ("contact_name_given");
-            }
+      assert (name2.structured_name.is_empty () == false);
 
-          this._names_count++;
-        }
+      s = c2.get ("full_name").get_string ();
+      assert (name2.full_name == s);
+      c2.unset ("full_name");
 
-      /* Finished? */
-      if (this._names_count == 2)
-        {
-          this._main_loop.quit ();
-        }
+      s = c2.get ("contact_name_family").get_string ();
+      assert (name2.structured_name.family_name == s);
+      c2.unset ("contact_name_family");
 
-      assert (removed.size == 1);
+      s = c2.get ("contact_name_given").get_string ();
+      assert (name2.structured_name.given_name == s);
+      c2.unset ("contact_name_given");
 
-      foreach (var i in removed)
-        {
-          assert (i == null);
-        }
+      assert (c2.size == 0);
     }
 }
 
diff --git a/tests/eds/phone-details.vala b/tests/eds/phone-details.vala
index 2611098..63d6783 100644
--- a/tests/eds/phone-details.vala
+++ b/tests/eds/phone-details.vala
@@ -25,13 +25,6 @@ using Gee;
 
 public class PhoneDetailsTests : EdsTest.TestCase
 {
-  private GLib.MainLoop _main_loop;
-  private IndividualAggregator _aggregator;
-  private int _phones_count;
-  private HashSet<string> _phone_types;
-  private Gee.HashMap<string, Value?> _c1;
-  private Gee.HashMap<string, Value?> _c2;
-
   public PhoneDetailsTests ()
     {
       base ("PhoneDetails");
@@ -41,153 +34,101 @@ public class PhoneDetailsTests : EdsTest.TestCase
 
   public void test_phone_numbers ()
     {
-      this._phones_count = 0;
-      this._phone_types = new HashSet<string> ();
-      this._c1 = new Gee.HashMap<string, Value?> ();
-      this._c2 = new Gee.HashMap<string, Value?> ();
-      this._main_loop = new GLib.MainLoop (null, false);
+      /* Set up the backend. */
+      var c1 = new Gee.HashMap<string, Value?> ();
+      var c2 = new Gee.HashMap<string, Value?> ();
       Value? v;
-
-      this.eds_backend.reset ();
+      string[] names = {"bernie h. innocenti", "richard m. stallman"};
 
       v = Value (typeof (string));
       v.set_string ("bernie h. innocenti");
-      this._c1.set ("full_name", (owned) v);
+      c1.set ("full_name", (owned) v);
       v = Value (typeof (string));
       v.set_string ("123");
-      this._c1.set ("car_phone", (owned) v);
+      c1.set ("car_phone", (owned) v);
       v = Value (typeof (string));
       v.set_string ("1234");
-      this._c1.set ("company_phone", (owned) v);
+      c1.set ("company_phone", (owned) v);
       v = Value (typeof (string));
       v.set_string ("12345");
-      this._c1.set ("home_phone", (owned) v);
-      this.eds_backend.add_contact (this._c1);
+      c1.set ("home_phone", (owned) v);
+
+      this.eds_backend.add_contact (c1);
 
       v = Value (typeof (string));
       v.set_string ("richard m. stallman");
-      this._c2.set ("full_name", (owned) v);
+      c2.set ("full_name", (owned) v);
       v = Value (typeof (string));
       v.set_string ("54321");
-      this._c2.set ("car_phone", (owned) v);
+      c2.set ("car_phone", (owned) v);
       v = Value (typeof (string));
       v.set_string ("4321");
-      this._c2.set ("company_phone", (owned) v);
+      c2.set ("company_phone", (owned) v);
       v = Value (typeof (string));
       v.set_string ("321");
-      this._c2.set ("home_phone", (owned) v);
-      this.eds_backend.add_contact (this._c2);
-
-      this._test_phone_numbers_async.begin ();
-
-      TestUtils.loop_run_with_timeout (this._main_loop);
+      c2.set ("home_phone", (owned) v);
 
-      assert (this._phones_count == 6);
-      assert (this._phone_types.size == 3);
-      assert (this._c1.size == 0);
-      assert (this._c2.size == 0);
+      this.eds_backend.add_contact (c2);
 
-      foreach (var pt in this._phone_types)
-        {
-          assert(pt in Edsf.Persona.phone_fields);
-        }
-   }
+      this.eds_backend.commit_contacts_to_addressbook_sync ();
 
-  private async void _test_phone_numbers_async ()
-    {
+      /* Set up the test variables. */
+      var phones_count = 0;
+      var phone_types = new HashSet<string> ();
 
-      yield this.eds_backend.commit_contacts_to_addressbook ();
+      /* Set up the aggregator and wait until either the expected personas are
+       * seen, or the test times out and fails. */
+      var aggregator = IndividualAggregator.dup ();
+      TestUtils.aggregator_prepare_and_wait_for_individuals_sync_with_timeout (
+          aggregator, names);
 
-      var store = BackendStore.dup ();
-      yield store.prepare ();
-      this._aggregator = IndividualAggregator.dup ();
-      this._aggregator.individuals_changed_detailed.connect
-          (this._individuals_changed_cb);
-      try
+      /* Check the properties of our individual. */
+      foreach (var n in names)
         {
-          yield this._aggregator.prepare ();
-        }
-      catch (GLib.Error e)
-        {
-          GLib.warning ("Error when calling prepare: %s\n", e.message);
-        }
-    }
-
-  private void _individuals_changed_cb (
-       MultiMap<Individual?, Individual?> changes)
-    {
-      var added = changes.get_values ();
-      var removed = changes.get_keys ();
-
-      foreach (Individual i in added)
-        {
-          assert (i != null);
-
-          unowned Gee.HashMap<string, Value?> contact = null;
-          assert (i.personas.size == 1);
-
-          if (i.full_name == "bernie h. innocenti")
-            {
-              contact = this._c1;
-            }
-          else if (i.full_name == "richard m. stallman")
-            {
-              contact = this._c2;
-            }
-
-          if (contact == null)
-            {
-              continue;
-            }
+          var i = TestUtils.get_individual_by_name (aggregator, n);
+          var contact = (n == "bernie h. innocenti") ? c1 : c2;
 
           contact.unset ("full_name");
-          var phone_numbers = (Folks.PhoneDetails) i;
-          foreach (var phone_fd in phone_numbers.phone_numbers)
+
+          foreach (var phone_fd in i.phone_numbers)
             {
-              this._phones_count++;
+              phones_count++;
               foreach (var t in phone_fd.get_parameter_values (
                   AbstractFieldDetails.PARAM_TYPE))
                 {
-                  string? v = null;
+                  string? t_ = null;
 
                   if (t == "car")
-                    {
-                      v = "car_phone";
-                    }
+                      t_ = "car_phone";
                   else if (t == AbstractFieldDetails.PARAM_TYPE_HOME)
-                    {
-                      v = "home_phone";
-                    }
+                      t_ = "home_phone";
                   else if (t == "x-evolution-company")
+                      t_ = "company_phone";
+                  /* Expected aliases of the above: */
+                  else if (t != "voice")
                     {
-                      v = "company_phone";
+                      debug ("Unrecognised type: %s, %s", t, phone_fd.value);
+                      assert_not_reached ();
                     }
 
-                  if (v == null)
+                  if (t_ != null)
                     {
-                      continue;
+                      phone_types.add (t_);
+                      assert (contact.get (t_).get_string () == phone_fd.value);
+                      contact.unset (t_);
                     }
-
-                  this._phone_types.add (v);
-                  assert (contact.get (v).get_string () == phone_fd.value);
-                  contact.unset (v);
                 }
             }
         }
 
-        /* Finished? */
-        if (this._phones_count == 6)
-          {
-            this._main_loop.quit ();
-          }
-
-      assert (removed.size == 1);
+      assert (phones_count == 6);
+      assert (phone_types.size == 3);
+      assert (c1.size == 0);
+      assert (c2.size == 0);
 
-      foreach (var i in removed)
-        {
-          assert (i == null);
-        }
-    }
+      foreach (var pt in phone_types)
+          assert (pt in Edsf.Persona.phone_fields);
+   }
 }
 
 public int main (string[] args)
diff --git a/tests/eds/postal-address-details.vala b/tests/eds/postal-address-details.vala
index cfc8c71..dccea7b 100644
--- a/tests/eds/postal-address-details.vala
+++ b/tests/eds/postal-address-details.vala
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Collabora Ltd.
+ * Copyright (C) 2011, 2015 Collabora Ltd.
  *
  * This library is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as published by
@@ -15,6 +15,7 @@
  * along with this library.  If not, see <http://www.gnu.org/licenses/>.
  *
  * Authors: Raul Gutierrez Segales <raul gutierrez segales collabora co uk>
+ *          Philip Withnall <philip withnall collabora co uk>
  *
  */
 
@@ -23,19 +24,6 @@ using Gee;
 
 public class PostalAddressDetailsTests : EdsTest.TestCase
 {
-  private GLib.MainLoop _main_loop;
-  private IndividualAggregator _aggregator;
-  private string _pobox = "12345";
-  private string _locality = "example locality";
-  private string _postalcode = "example postalcode";
-  private string _street = "example street";
-  private string _extended = "example extended address";
-  private string _country = "example country";
-  private string _region = "example region";
-  private PostalAddressFieldDetails _postal_address;
-  private bool _found_postal_address;
-  private string _fullname;
-
   public PostalAddressDetailsTests ()
     {
       base ("PostalAddressDetailsTests");
@@ -46,35 +34,29 @@ public class PostalAddressDetailsTests : EdsTest.TestCase
 
   public void test_postal_address_details_interface ()
     {
-      this._main_loop = new GLib.MainLoop (null, false);
-      Gee.HashMap<string, Value?> c1 = new Gee.HashMap<string, Value?> ();
-      this._fullname = "persona #1";
+      /* Set up the backend. */
+      var c1 = new Gee.HashMap<string, Value?> ();
       Value? v;
 
-      var pa = new PostalAddress (
-           this._pobox,
-           this._extended,
-           this._street,
-           this._locality,
-           this._region,
-           this._postalcode,
-           this._country,
-           null, "eds_id");
-      this._postal_address = new PostalAddressFieldDetails (pa);
-      this._postal_address.add_parameter (AbstractFieldDetails.PARAM_TYPE,
-          AbstractFieldDetails.PARAM_TYPE_HOME);
+      var pobox = "12345";
+      var locality = "example locality";
+      var postalcode = "example postalcode";
+      var street = "example street";
+      var extended = "example extended address";
+      var country = "example country";
+      var region = "example region";
 
       v = Value (typeof (string));
-      v.set_string (this._fullname);
+      v.set_string ("persona #1");
       c1.set ("full_name", (owned) v);
       var pa_copy = new PostalAddress (
-           this._pobox,
-           this._extended,
-           this._street,
-           this._locality,
-           this._region,
-           this._postalcode,
-           this._country,
+           pobox,
+           extended,
+           street,
+           locality,
+           region,
+           postalcode,
+           country,
            null, "eds_id");
       var pa_fd_copy = new PostalAddressFieldDetails (pa_copy);
       pa_fd_copy.add_parameter (AbstractFieldDetails.PARAM_TYPE,
@@ -85,71 +67,40 @@ public class PostalAddressDetailsTests : EdsTest.TestCase
       c1.set (Edsf.Persona.address_fields[0], (owned) v);
 
       this.eds_backend.add_contact (c1);
+      this.eds_backend.commit_contacts_to_addressbook_sync ();
 
-      this._found_postal_address = false;
-
-      this._test_postal_address_details_interface_async.begin ();
-
-      TestUtils.loop_run_with_timeout (this._main_loop);
-
-      assert (this._found_postal_address == true);
-    }
-
-  private async void _test_postal_address_details_interface_async ()
-    {
-
-      yield this.eds_backend.commit_contacts_to_addressbook ();
-
-      var store = BackendStore.dup ();
-      yield store.prepare ();
-      this._aggregator = IndividualAggregator.dup ();
-      this._aggregator.individuals_changed_detailed.connect
-          (this._individuals_changed_cb);
-      try
-        {
-          yield this._aggregator.prepare ();
-        }
-      catch (GLib.Error e)
-        {
-          GLib.warning ("Error when calling prepare: %s\n", e.message);
-        }
-
-    }
+      /* Set up the aggregator and wait until either the expected personas are
+       * seen, or the test times out and fails. */
+      var aggregator = IndividualAggregator.dup ();
+      TestUtils.aggregator_prepare_and_wait_for_individuals_sync_with_timeout (
+          aggregator, {"persona #1"});
 
-  private void _individuals_changed_cb (
-       MultiMap<Individual?, Individual?> changes)
-    {
-      var added = changes.get_values ();
-      var removed = changes.get_keys ();
+      /* Check the properties of our individual. */
+      var i = TestUtils.get_individual_by_name (aggregator, "persona #1");
 
-      foreach (var i in added)
-        {
-          assert (i != null);
+      assert (i.postal_addresses.size == 1);
 
-          if (i.full_name == this._fullname)
-            {
-              foreach (var pa_fd in i.postal_addresses)
-              {
-                /* We copy the uid - we don't know it.
-                 * Although we could get it from the 1st
-                 * personas iid; there is no real need.
-                 */
-                this._postal_address.id = pa_fd.id;
-
-                if (pa_fd.equal (this._postal_address))
-                  {
-                    this._found_postal_address = true;
-                    this._main_loop.quit ();
-                  }
-              }
-            }
-        }
-
-      assert (removed.size == 1);
+      var pa = new PostalAddress (
+           pobox,
+           extended,
+           street,
+           locality,
+           region,
+           postalcode,
+           country,
+           null, "eds_id");
+      var postal_address = new PostalAddressFieldDetails (pa);
+      postal_address.add_parameter (AbstractFieldDetails.PARAM_TYPE,
+          AbstractFieldDetails.PARAM_TYPE_HOME);
 
-      foreach (var i in removed)
+      foreach (var pa_fd in i.postal_addresses)
         {
-          assert (i == null);
+          /* We copy the uid - we don't know it.
+           * Although we could get it from the 1st
+           * personas iid; there is no real need.
+           */
+          postal_address.id = pa_fd.id;
+          assert (pa_fd.equal (postal_address));
         }
     }
 }



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