diff options
author | Raul Gutierrez Segales <raul.gutierrez.segales@collabora.co.uk> | 2011-04-08 23:55:19 +0100 |
---|---|---|
committer | Raul Gutierrez Segales <raul.gutierrez.segales@collabora.co.uk> | 2011-04-09 01:43:26 +0100 |
commit | 319643ded3369aa640d3232fad6bf36077608cff (patch) | |
tree | 68ec2f6eae81c6b3227ac669adfb2ed485d0447e /tests | |
parent | 62695d90ce0b63656e1adb454966495cbba86648 (diff) |
Add test to match via IM Addresses
Diffstat (limited to 'tests')
-rw-r--r-- | tests/tracker/Makefile.am | 6 | ||||
-rw-r--r-- | tests/tracker/match-im-addresses.vala | 230 |
2 files changed, 236 insertions, 0 deletions
diff --git a/tests/tracker/Makefile.am b/tests/tracker/Makefile.am index fbe2a30..5bae88f 100644 --- a/tests/tracker/Makefile.am +++ b/tests/tracker/Makefile.am @@ -93,6 +93,7 @@ noinst_PROGRAMS = \ duplicated-emails \ duplicated-phones \ link-personas-via-local-ids \ + match-im-addresses \ $(NULL) backend_store_key_file=$(srcdir)/data/backend-tracker-only.ini @@ -307,6 +308,10 @@ link_personas_via_local_ids_SOURCES = \ link-personas-via-local-ids.vala \ $(NULL) +match_im_addresses_SOURCES = \ + match-im-addresses.vala \ + $(NULL) + CLEANFILES = \ *.pid \ *.address \ @@ -365,6 +370,7 @@ MAINTAINERCLEANFILES = \ duplicated_emails_vala.stamp \ duplicated_phones_vala.stamp \ link_personas_via_local_ids_vala.stamp \ + match_im_addresses.stamp \ $(NULL) EXTRA_DIST = \ diff --git a/tests/tracker/match-im-addresses.vala b/tests/tracker/match-im-addresses.vala new file mode 100644 index 0000000..7772c47 --- /dev/null +++ b/tests/tracker/match-im-addresses.vala @@ -0,0 +1,230 @@ +/* + * 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 MatchIMAddressesTests : Folks.TestCase +{ + private GLib.MainLoop _main_loop; + private TrackerTest.Backend _tracker_backend; + private IndividualAggregator _aggregator; + private string _persona_fullname_1 = "aaa"; + private string _persona_fullname_2 = "bbb"; + private string _im_addr_1 = "some-address@jabber.example.org"; + private string _im_addr_2 = "some-other-address@jabber.example.org"; + private bool _added_personas = false; + private string _individual_id_1 = ""; + private string _individual_id_2 = ""; + private Folks.MatchResult _match; + private Trf.PersonaStore _pstore; + + public MatchIMAddressesTests () + { + base ("MatchIMAddressesTests"); + + this._tracker_backend = new TrackerTest.Backend (); + + this.add_test ("test potential match by IM addresses ", + this.test_match_im_addresses); + } + + public override void set_up () + { + } + + public override void tear_down () + { + this._tracker_backend.tear_down (); + } + + public void test_match_im_addresses () + { + this._main_loop = new GLib.MainLoop (null, false); + + this._test_match_im_addresses_async (); + + Timeout.add_seconds (5, () => + { + this._main_loop.quit (); + assert_not_reached (); + }); + + this._main_loop.run (); + assert (this._match >= Folks.MatchResult.HIGH); + } + + private async void _test_match_im_addresses_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._individual_id_1 = i.id; + } + else if (i.full_name == this._persona_fullname_2) + { + this._individual_id_2 = i.id; + } + } + + if (this._individual_id_1 != "" && + this._individual_id_2 != "") + { + this._try_potential_match (); + } + + assert (removed == null); + } + + private void _try_potential_match () + { + var ind1 = this._aggregator.individuals.lookup (this._individual_id_1); + var ind2 = this._aggregator.individuals.lookup (this._individual_id_2); + + Folks.PotentialMatch matchObj = new Folks.PotentialMatch (); + this._match = matchObj.potential_match (ind1, ind2); + + this._main_loop.quit (); + } + + 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 (); + } + } + } + + private async void _add_personas () + { + HashTable<string, Value?> details1 = new HashTable<string, Value?> + (str_hash, str_equal); + HashTable<string, Value?> details2 = new HashTable<string, Value?> + (str_hash, str_equal); + Value? val; + HashTable<string, LinkedHashSet<string>> im_addrs; + LinkedHashSet<string> proto; + + 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 (HashTable<string, LinkedHashSet<string>>)); + im_addrs = new HashTable<string, LinkedHashSet<string>> (null, null); + proto = new LinkedHashSet<string> (); + proto.add (this._im_addr_1); + im_addrs.insert ("jabber", (owned) proto); + proto = new LinkedHashSet<string> (); + proto.add (this._im_addr_2); + im_addrs.insert ("yahoo", (owned) proto); + val.set_boxed ((owned) im_addrs); + details1.insert ( + Folks.PersonaStore.detail_key (PersonaDetail.IM_ADDRESSES), + (owned) val); + + val = Value (typeof (string)); + val.set_string (this._persona_fullname_2); + details2.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME), + (owned) val); + + val = Value (typeof (HashTable<string, LinkedHashSet<string>>)); + im_addrs = new HashTable<string, LinkedHashSet<string>> (null, null); + proto = new LinkedHashSet<string> (); + proto.add (this._im_addr_1); + im_addrs.insert ("jabber", (owned) proto); + val.set_boxed ((owned) im_addrs); + details2.insert ( + Folks.PersonaStore.detail_key (PersonaDetail.IM_ADDRESSES), + (owned) val); + + try + { + yield this._aggregator.add_persona_from_details (null, + this._pstore, details1); + + yield this._aggregator.add_persona_from_details (null, + this._pstore, details2); + } + 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 MatchIMAddressesTests ().get_suite ()); + + Test.run (); + + return 0; +} |