[folks] Add test for setting an existing email an a Trf.Persona
- From: Raul Gutierrez Segales <raulgs src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [folks] Add test for setting an existing email an a Trf.Persona
- Date: Mon, 11 Apr 2011 15:25:45 +0000 (UTC)
commit 6064e680ea0154579d499dd3d9140f2a9fbcef54
Author: Raul Gutierrez Segales <raul gutierrez segales collabora co uk>
Date: Sun Apr 10 15:18:02 2011 +0100
Add test for setting an existing email an a Trf.Persona
tests/tracker/Makefile.am | 6 +
tests/tracker/set-duplicate-email.vala | 226 ++++++++++++++++++++++++++++++++
2 files changed, 232 insertions(+), 0 deletions(-)
---
diff --git a/tests/tracker/Makefile.am b/tests/tracker/Makefile.am
index 81eb36f..9855671 100644
--- a/tests/tracker/Makefile.am
+++ b/tests/tracker/Makefile.am
@@ -99,6 +99,7 @@ noinst_PROGRAMS = \
match-phone-number \
match-name \
match-all \
+ set-duplicate-email \
$(NULL)
backend_store_key_file=$(srcdir)/data/backend-tracker-only.ini
@@ -337,6 +338,10 @@ match_all_SOURCES = \
match-all.vala \
$(NULL)
+set_duplicate_email_SOURCES = \
+ set-duplicate-email.vala \
+ $(NULL)
+
CLEANFILES = \
*.pid \
*.address \
@@ -401,6 +406,7 @@ MAINTAINERCLEANFILES = \
match_phone_number_vala.stamp \
match_name_vala.stamp \
match_all_vala.stamp \
+ set_duplicate_email_vala.stamp \
$(NULL)
EXTRA_DIST = \
diff --git a/tests/tracker/set-duplicate-email.vala b/tests/tracker/set-duplicate-email.vala
new file mode 100644
index 0000000..0b9d633
--- /dev/null
+++ b/tests/tracker/set-duplicate-email.vala
@@ -0,0 +1,226 @@
+/*
+ * Copyright (C) 2011 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>
+ *
+ */
+
+using Tracker.Sparql;
+using TrackerTest;
+using Folks;
+using Gee;
+
+public class SetDuplicateEmailTests : Folks.TestCase
+{
+ private GLib.MainLoop _main_loop;
+ private TrackerTest.Backend _tracker_backend;
+ private IndividualAggregator _aggregator;
+ private string _persona_fullname_1 = "persona #1";
+ private string _email_1 = "some-address example org";
+ private bool _added_personas = false;
+ private Trf.PersonaStore _pstore;
+ private bool _email_found;
+
+ public SetDuplicateEmailTests ()
+ {
+ base ("SetDuplicateEmailTests");
+
+ this._tracker_backend = new TrackerTest.Backend ();
+
+ this.add_test ("test re-setting an existing e-mail address",
+ this.test_set_duplicate_email);
+ }
+
+ public override void set_up ()
+ {
+ }
+
+ public override void tear_down ()
+ {
+ this._tracker_backend.tear_down ();
+ }
+
+ public void test_set_duplicate_email ()
+ {
+ this._main_loop = new GLib.MainLoop (null, false);
+
+ this._email_found = false;
+ this._test_set_duplicate_email_async ();
+
+ Timeout.add_seconds (5, () =>
+ {
+ this._main_loop.quit ();
+ assert_not_reached ();
+ });
+
+ this._main_loop.run ();
+ /* we should see the e-mail address twice:
+ * 1) when we create the Persona
+ * 2) when we re-set the address */
+ assert (this._email_found == true);
+ }
+
+ private async void _test_set_duplicate_email_async ()
+ {
+ var store = BackendStore.dup ();
+ yield store.prepare ();
+ this._aggregator = new IndividualAggregator ();
+ this._aggregator.individuals_changed.connect
+ (this._individuals_changed_cb);
+ try
+ {
+ yield this._aggregator.prepare ();
+ this._pstore = null;
+ foreach (var backend in store.enabled_backends)
+ {
+ this._pstore =
+ (Trf.PersonaStore) backend.persona_stores.lookup ("tracker");
+ if (this._pstore != null)
+ break;
+ }
+ assert (this._pstore != null);
+ this._pstore.notify["is-prepared"].connect (this._notify_pstore_cb);
+ this._try_to_add ();
+ }
+ catch (GLib.Error e)
+ {
+ GLib.warning ("Error when calling prepare: %s\n", e.message);
+ }
+ }
+
+ private void _individuals_changed_cb
+ (GLib.List<Individual>? added,
+ GLib.List<Individual>? removed,
+ string? message,
+ Persona? actor,
+ GroupDetails.ChangeReason reason)
+ {
+ foreach (unowned Individual i in added)
+ {
+ if (i.full_name == this._persona_fullname_1)
+ {
+ this._reset_email_address (i);
+ }
+ }
+ assert (removed == null);
+ }
+
+ private void _reset_email_address (Individual i)
+ {
+ Trf.Persona p = (Trf.Persona) i.personas.nth_data (0);
+ if (this._has_email (p, this._email_1))
+ {
+ var emails1 = new GLib.List<FieldDetails> ();
+ var email_1 = new FieldDetails (this._email_1);
+ emails1.prepend ((owned) email_1);
+ p.email_addresses = (emails1);
+ p.notify["email-addresses"].connect (this._email_addresses_cb);
+ }
+ }
+
+ private void _email_addresses_cb (Object p, ParamSpec ps)
+ {
+ var persona = (Trf.Persona) p;
+ if (this._has_email (persona, this._email_1))
+ {
+ this._email_found = true;
+ this._main_loop.quit ();
+ }
+ }
+
+ private bool _has_email (Trf.Persona persona, string email)
+ {
+ if (persona.email_addresses != null)
+ {
+ foreach (var fd in persona.email_addresses)
+ {
+ if (fd.value == email)
+ {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ private void _notify_pstore_cb (Object _pstore, ParamSpec ps)
+ {
+ this._try_to_add ();
+ }
+
+ private async void _try_to_add ()
+ {
+ lock (this._added_personas)
+ {
+ if (this._pstore.is_prepared &&
+ this._added_personas == false)
+ {
+ this._added_personas = true;
+ yield this._add_personas ();
+ }
+ }
+ }
+
+ /**
+ * Add 1 persona and once we've seen it try to re-set it's
+ * e-mail address (the Tracker backend should figure it already
+ * exist so we don't bump into a constraint error).
+ * See https://bugzilla.gnome.org/show_bug.cgi?id=647331 */
+ private async void _add_personas ()
+ {
+ HashTable<string, Value?> details1 = new HashTable<string, Value?>
+ (str_hash, str_equal);
+ Value? val;
+
+ val = Value (typeof (string));
+ val.set_string (this._persona_fullname_1);
+ details1.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
+ (owned) val);
+
+ val = Value (typeof (GLib.List<FieldDetails>));
+ var emails1 = new GLib.List<FieldDetails> ();
+ var email_1 = new FieldDetails (this._email_1);
+ emails1.prepend ((owned) email_1);
+ val.set_pointer (emails1);
+ details1.insert (
+ Folks.PersonaStore.detail_key (PersonaDetail.EMAIL_ADDRESSES),
+ (owned) val);
+
+ try
+ {
+ yield this._aggregator.add_persona_from_details (null,
+ this._pstore, details1);
+ }
+ catch (Folks.IndividualAggregatorError e)
+ {
+ GLib.warning ("[AddPersonaError] add_persona_from_details: %s\n",
+ e.message);
+ }
+ }
+}
+
+public int main (string[] args)
+{
+ Test.init (ref args);
+
+ TestSuite root = TestSuite.get_root ();
+ root.add_suite (new SetDuplicateEmailTests ().get_suite ());
+
+ Test.run ();
+
+ return 0;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]