2 * Copyright (C) 2011 Collabora Ltd.
4 * This library is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 2.1 of the License, or
7 * (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library. If not, see <http://www.gnu.org/licenses/>.
17 * Authors: Raul Gutierrez Segales <raul.gutierrez.segales@collabora.co.uk>
26 public class MatchIMAddressesTests : Folks.TestCase
28 private GLib.MainLoop _main_loop;
29 private TrackerTest.Backend _tracker_backend;
30 private IndividualAggregator _aggregator;
31 private string _persona_fullname_1 = "aaa";
32 private string _persona_fullname_2 = "bbb";
33 private string _im_addr_1 = "some-address@jabber.example.org";
34 private string _im_addr_2 = "some-other-address@jabber.example.org";
35 private bool _added_personas = false;
36 private string _individual_id_1 = "";
37 private string _individual_id_2 = "";
38 private Folks.MatchResult _match;
39 private Trf.PersonaStore _pstore;
41 public MatchIMAddressesTests ()
43 base ("MatchIMAddressesTests");
45 this._tracker_backend = new TrackerTest.Backend ();
47 this.add_test ("test potential match by IM addresses ",
48 this.test_match_im_addresses);
51 public override void set_up ()
55 public override void tear_down ()
57 this._tracker_backend.tear_down ();
60 public void test_match_im_addresses ()
62 this._main_loop = new GLib.MainLoop (null, false);
64 this._test_match_im_addresses_async ();
66 Timeout.add_seconds (5, () =>
68 this._main_loop.quit ();
69 assert_not_reached ();
72 this._main_loop.run ();
73 assert (this._match >= Folks.MatchResult.HIGH);
76 private async void _test_match_im_addresses_async ()
78 var store = BackendStore.dup ();
79 yield store.prepare ();
80 this._aggregator = new IndividualAggregator ();
81 this._aggregator.individuals_changed.connect
82 (this._individuals_changed_cb);
85 yield this._aggregator.prepare ();
87 foreach (var backend in store.enabled_backends)
90 (Trf.PersonaStore) backend.persona_stores.lookup ("tracker");
91 if (this._pstore != null)
94 assert (this._pstore != null);
95 this._pstore.notify["is-prepared"].connect (this._notify_pstore_cb);
100 GLib.warning ("Error when calling prepare: %s\n", e.message);
104 private void _individuals_changed_cb
105 (GLib.List<Individual>? added,
106 GLib.List<Individual>? removed,
109 GroupDetails.ChangeReason reason)
111 foreach (unowned Individual i in added)
113 if (i.full_name == this._persona_fullname_1)
115 this._individual_id_1 = i.id;
117 else if (i.full_name == this._persona_fullname_2)
119 this._individual_id_2 = i.id;
123 if (this._individual_id_1 != "" &&
124 this._individual_id_2 != "")
126 this._try_potential_match ();
129 assert (removed == null);
132 private void _try_potential_match ()
134 var ind1 = this._aggregator.individuals.lookup (this._individual_id_1);
135 var ind2 = this._aggregator.individuals.lookup (this._individual_id_2);
137 Folks.PotentialMatch matchObj = new Folks.PotentialMatch ();
138 this._match = matchObj.potential_match (ind1, ind2);
140 this._main_loop.quit ();
143 private void _notify_pstore_cb (Object _pstore, ParamSpec ps)
148 private async void _try_to_add ()
150 lock (this._added_personas)
152 if (this._pstore.is_prepared &&
153 this._added_personas == false)
155 this._added_personas = true;
156 yield this._add_personas ();
161 private async void _add_personas ()
163 HashTable<string, Value?> details1 = new HashTable<string, Value?>
164 (str_hash, str_equal);
165 HashTable<string, Value?> details2 = new HashTable<string, Value?>
166 (str_hash, str_equal);
168 HashMultiMap<string, string> im_addrs;
170 val = Value (typeof (string));
171 val.set_string (this._persona_fullname_1);
172 details1.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
175 val = Value (typeof (MultiMap<string, string>));
176 im_addrs = new HashMultiMap<string, string> ();
177 im_addrs.set ("jabber", this._im_addr_1);
178 im_addrs.set ("yahoo", this._im_addr_2);
179 val.set_object (im_addrs);
181 Folks.PersonaStore.detail_key (PersonaDetail.IM_ADDRESSES),
184 val = Value (typeof (string));
185 val.set_string (this._persona_fullname_2);
186 details2.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
189 val = Value (typeof (MultiMap<string, string>));
190 im_addrs = new HashMultiMap<string, string> ();
191 im_addrs.set ("jabber", this._im_addr_1);
192 val.set_object (im_addrs);
194 Folks.PersonaStore.detail_key (PersonaDetail.IM_ADDRESSES),
199 yield this._aggregator.add_persona_from_details (null,
200 this._pstore, details1);
202 yield this._aggregator.add_persona_from_details (null,
203 this._pstore, details2);
205 catch (Folks.IndividualAggregatorError e)
207 GLib.warning ("[AddPersonaError] add_persona_from_details: %s\n",
213 public int main (string[] args)
215 Test.init (ref args);
217 TestSuite root = TestSuite.get_root ();
218 root.add_suite (new MatchIMAddressesTests ().get_suite ());