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 MatchKnownEmailsTests : Folks.TestCase
28 private GLib.MainLoop _main_loop;
29 private TrackerTest.Backend _tracker_backend;
30 private IndividualAggregator _aggregator;
32 /* Make sure the names are completely different so
33 * we don't end up accidentally matching by names */
34 private string _persona_fullname_1 = "aaa";
35 private string _persona_fullname_2 = "bbb";
37 private string _email_1 = "admin@jabber.example.org";
38 private bool _added_personas = false;
39 private string _individual_id_1 = "";
40 private string _individual_id_2 = "";
41 private Folks.MatchResult _match;
42 private Trf.PersonaStore _pstore;
44 public MatchKnownEmailsTests ()
46 base ("MatchKnownEmailsTests");
48 this._tracker_backend = new TrackerTest.Backend ();
50 this.add_test ("test potential match with same email addresses ",
51 this.test_match_email_addresses);
54 public override void set_up ()
58 public override void tear_down ()
60 this._tracker_backend.tear_down ();
63 public void test_match_email_addresses ()
65 this._main_loop = new GLib.MainLoop (null, false);
67 this._test_match_email_addresses_async ();
69 Timeout.add_seconds (5, () =>
71 this._main_loop.quit ();
72 assert_not_reached ();
75 this._main_loop.run ();
77 /* Some e-mail addresses (namely admin, webmaster, etc.)
78 * are considered common alias so they shouldn't
79 * be used to match individuals.
80 * Hence, we should get a LOW.*/
81 assert (this._match <= Folks.MatchResult.LOW);
84 private async void _test_match_email_addresses_async ()
86 var store = BackendStore.dup ();
87 yield store.prepare ();
88 this._aggregator = new IndividualAggregator ();
89 this._aggregator.individuals_changed.connect
90 (this._individuals_changed_cb);
93 yield this._aggregator.prepare ();
95 foreach (var backend in store.enabled_backends)
98 (Trf.PersonaStore) backend.persona_stores.get ("tracker");
99 if (this._pstore != null)
102 assert (this._pstore != null);
103 this._pstore.notify["is-prepared"].connect (this._notify_pstore_cb);
108 GLib.warning ("Error when calling prepare: %s\n", e.message);
112 private void _individuals_changed_cb
113 (GLib.List<Individual>? added,
114 GLib.List<Individual>? removed,
117 GroupDetails.ChangeReason reason)
119 foreach (unowned Individual i in added)
121 if (i.full_name == this._persona_fullname_1)
123 this._individual_id_1 = i.id;
125 else if (i.full_name == this._persona_fullname_2)
127 this._individual_id_2 = i.id;
131 if (this._individual_id_1 != "" &&
132 this._individual_id_2 != "")
134 this._try_potential_match ();
137 assert (removed == null);
140 private void _try_potential_match ()
142 var ind1 = this._aggregator.individuals.lookup (this._individual_id_1);
143 var ind2 = this._aggregator.individuals.lookup (this._individual_id_2);
145 Folks.PotentialMatch matchObj = new Folks.PotentialMatch ();
146 this._match = matchObj.potential_match (ind1, ind2);
148 this._main_loop.quit ();
151 private void _notify_pstore_cb (Object _pstore, ParamSpec ps)
156 private async void _try_to_add ()
158 lock (this._added_personas)
160 if (this._pstore.is_prepared &&
161 this._added_personas == false)
163 this._added_personas = true;
164 yield this._add_personas ();
169 private async void _add_personas ()
171 HashTable<string, Value?> details1 = new HashTable<string, Value?>
172 (str_hash, str_equal);
173 HashTable<string, Value?> details2 = new HashTable<string, Value?>
174 (str_hash, str_equal);
177 val = Value (typeof (string));
178 val.set_string (this._persona_fullname_1);
179 details1.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
182 val = Value (typeof (Set<FieldDetails>));
183 var emails1 = new HashSet<FieldDetails> ();
184 var email_1 = new FieldDetails (this._email_1);
185 emails1.add (email_1);
186 val.set_object (emails1);
188 Folks.PersonaStore.detail_key (PersonaDetail.EMAIL_ADDRESSES),
191 val = Value (typeof (string));
192 val.set_string (this._persona_fullname_2);
193 details2.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
196 val = Value (typeof (Set<FieldDetails>));
197 var emails2 = new HashSet<FieldDetails> ();
198 var email_2 = new FieldDetails (this._email_1);
199 emails2.add (email_2);
200 val.set_object (emails2);
202 Folks.PersonaStore.detail_key (PersonaDetail.EMAIL_ADDRESSES),
207 yield this._aggregator.add_persona_from_details (null,
208 this._pstore, details1);
210 yield this._aggregator.add_persona_from_details (null,
211 this._pstore, details2);
213 catch (Folks.IndividualAggregatorError e)
215 GLib.warning ("[AddPersonaError] add_persona_from_details: %s\n",
221 public int main (string[] args)
223 Test.init (ref args);
225 TestSuite root = TestSuite.get_root ();
226 root.add_suite (new MatchKnownEmailsTests ().get_suite ());