eds test: add basic anti-linking test
authorTravis Reitter <travis.reitter@collabora.co.uk>
Thu, 3 Jan 2013 17:42:57 +0000 (09:42 -0800)
committerTravis Reitter <travis.reitter@collabora.co.uk>
Sun, 6 Jan 2013 01:29:12 +0000 (17:29 -0800)
tests/eds/Makefile.am
tests/eds/anti-linking.vala [new file with mode: 0644]

index 7585fb4..655aeca 100644 (file)
@@ -77,6 +77,7 @@ noinst_PROGRAMS = \
        set-roles \
        linkable-properties \
        link-personas-diff-stores \
+       anti-linking \
        enable-disable-stores \
        create-remove-stores \
        set-is-favourite \
@@ -102,6 +103,10 @@ TESTS = $(noinst_PROGRAMS)
 #      store-removed.vala \
 #      $(NULL)
 
+anti_linking_SOURCES = \
+       anti-linking.vala \
+       $(NULL)
+
 persona_store_tests_SOURCES = \
        persona-store-tests.vala \
        $(NULL)
diff --git a/tests/eds/anti-linking.vala b/tests/eds/anti-linking.vala
new file mode 100644 (file)
index 0000000..74a46fe
--- /dev/null
@@ -0,0 +1,202 @@
+/*
+ * Copyright (C) 2012 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
+ * the Free Software Foundation, either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Raul Gutierrez Segales <raul.gutierrez.segales@collabora.co.uk>
+ *          Travis Reitter <travis.reitter@collabora.co.uk>
+ */
+
+using EdsTest;
+using Folks;
+using Gee;
+
+public class AntiLinkingTests : Folks.TestCase
+{
+  private EdsTest.Backend _eds_backend;
+  private IndividualAggregator _aggregator;
+  private GLib.MainLoop _main_loop;
+  private bool _found_before_update;
+  private bool _found_after_update;
+
+  /* NOTE: each full name should remain unique */
+  private const string _full_name_1 = "bernie h. innocenti";
+  private const string _email_1 = "bernie@example.org";
+  private const string _full_name_2 = "Clyde McPoyle";
+
+  public AntiLinkingTests ()
+    {
+      base ("AntiLinking");
+
+      this.add_test ("basic anti-linking", this.test_anti_linking_basic);
+    }
+
+  public override void set_up ()
+    {
+      this._found_before_update = false;
+      this._found_after_update = false;
+      this._eds_backend = new EdsTest.Backend ();
+      this._eds_backend.set_up ();
+
+      /* We configure eds as the primary store */
+      var config_val = "eds:%s".printf (this._eds_backend.address_book_uid);
+      Environment.set_variable ("FOLKS_PRIMARY_STORE", config_val, true);
+    }
+
+  public override void tear_down ()
+    {
+      this._eds_backend.tear_down ();
+    }
+
+  /* Confirm that basic anti-linking works for two Personas who have a common
+   * linkable property value.
+   *
+   * FIXME: this test should be moved to tests/folks and rebased upon the Dummy
+   * backend once bgo#648811 is fixed.
+   */
+  void test_anti_linking_basic ()
+    {
+      Gee.HashMap<string, Value?> c;
+      this._main_loop = new GLib.MainLoop (null, false);
+      Value? v;
+
+      this._found_before_update = false;
+      this._found_after_update = false;
+
+      this._eds_backend.reset ();
+
+      c = new Gee.HashMap<string, Value?> ();
+      v = Value (typeof (string));
+      v.set_string (_full_name_1);
+      c.set ("full_name", (owned) v);
+      v = Value (typeof (string));
+      v.set_string (_email_1);
+      c.set ("email_1", (owned) v);
+      this._eds_backend.add_contact (c);
+
+      c = new Gee.HashMap<string, Value?> ();
+      v = Value (typeof (string));
+      v.set_string (_full_name_2);
+      c.set ("full_name", (owned) v);
+      v = Value (typeof (string));
+      /* Intentionally set the same email address so these will be linked */
+      v.set_string (_email_1);
+      c.set ("email_1", (owned) v);
+      this._eds_backend.add_contact (c);
+
+      this._test_anti_linking_basic_async.begin ();
+
+      Timeout.add_seconds (5, () =>
+        {
+          this._main_loop.quit ();
+          stderr.printf ("Personas failed to be anti-linked\n");
+          assert_not_reached ();
+        });
+
+      this._main_loop.run ();
+
+      assert (this._found_before_update);
+      assert (this._found_after_update);
+    }
+
+  private async void _test_anti_linking_basic_async ()
+    {
+      yield this._eds_backend.commit_contacts_to_addressbook ();
+
+      var store = BackendStore.dup ();
+      yield store.prepare ();
+      this._aggregator = new IndividualAggregator ();
+      this._aggregator.individuals_changed_detailed.connect
+          (this._individuals_changed_aggregate_after_change_cb);
+      try
+        {
+          yield this._aggregator.prepare ();
+        }
+      catch (GLib.Error e)
+        {
+          GLib.warning ("Error when calling prepare: %s\n", e.message);
+        }
+    }
+
+  private void _individuals_changed_aggregate_after_change_cb (
+       MultiMap<Individual?, Individual?> changes)
+    {
+      var added = changes.get_values ();
+      var removed = changes.get_values ();
+
+      if (!this._found_before_update)
+        {
+          assert (changes.size == 1);
+          assert (added.size == 1);
+          assert (removed.size == 1);
+
+          foreach (Individual i in added)
+            {
+              assert (i != null);
+              assert (i.personas.size == 2);
+
+              this._found_before_update = true;
+
+              var iter = i.personas.iterator ();
+              iter.next ();
+              var al_1 = iter.get () as AntiLinkable;
+              iter.next ();
+              var al_2 = iter.get () as AntiLinkable;
+
+              var anti_links = new HashSet<Persona> ();
+              anti_links.add (al_2);
+              al_1.add_anti_links.begin (anti_links);
+            }
+        }
+      else
+        {
+          /* the first Individual should have been split in two new ones */
+          assert (changes.size == 2);
+          assert (added.size == 2);
+
+          var found_1 = false;
+          var found_2 = false;
+
+          foreach (var i in added)
+            {
+              if (i.full_name == _full_name_1)
+                {
+                  found_1 = true;
+                }
+              if (i.full_name == _full_name_2)
+                {
+                  found_2 = true;
+                }
+            }
+
+          if (found_1 && found_2)
+            {
+              this._found_after_update = true;
+              this._main_loop.quit ();
+            }
+        }
+    }
+}
+
+public int main (string[] args)
+{
+  Test.init (ref args);
+
+  TestSuite root = TestSuite.get_root ();
+  root.add_suite (new AntiLinkingTests ().get_suite ());
+
+  Test.run ();
+
+  return 0;
+}