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 MatchNameTests : Folks.TestCase
28 private GLib.MainLoop _main_loop;
29 private TrackerTest.Backend _tracker_backend;
30 private IndividualAggregator _aggregator = null;
31 private string _persona_fullname_1 = "Bernie Innocenti";
32 private string _persona_fullname_2 = "Bernardo H. Innocenti";
33 private bool _added_personas;
34 private string _individual_id_1 = "";
35 private string _individual_id_2 = "";
36 private Folks.MatchResult _match;
37 private Trf.PersonaStore _pstore;
39 public MatchNameTests ()
41 base ("MatchNameTests");
43 this._tracker_backend = new TrackerTest.Backend ();
45 this.add_test ("test potential match by name #1 ",
46 this.test_match_name_1);
47 this.add_test ("test potential match by name #2 ",
48 this.test_match_name_2);
49 this.add_test ("test potential match by name #3 ",
50 this.test_match_name_3);
53 public override void set_up ()
57 public override void tear_down ()
59 this._tracker_backend.tear_down ();
62 public void test_match_name_1 ()
64 this._main_loop = new GLib.MainLoop (null, false);
66 this._match = Folks.MatchResult.MIN;
67 this._added_personas = false;
68 this._persona_fullname_1 = "Bernie Innocenti";
69 this._persona_fullname_2 = "Bernardo H. Innocenti";
70 this._individual_id_1 = "";
71 this._individual_id_2 = "";
73 this._test_match_name_async ();
75 Timeout.add_seconds (5, () =>
77 this._main_loop.quit ();
78 assert_not_reached ();
81 this._main_loop.run ();
83 assert (this._match >= Folks.MatchResult.MEDIUM);
86 public void test_match_name_2 ()
88 this._main_loop = new GLib.MainLoop (null, false);
90 this._match = Folks.MatchResult.MIN;
91 this._added_personas = false;
92 this._persona_fullname_1 = "AAAA BBBBB";
93 this._persona_fullname_2 = "CCCCC DDDDD";
94 this._individual_id_1 = "";
95 this._individual_id_2 = "";
97 this._test_match_name_async ();
99 Timeout.add_seconds (5, () =>
101 this._main_loop.quit ();
102 assert_not_reached ();
105 this._main_loop.run ();
107 assert (this._match <= Folks.MatchResult.LOW);
110 public void test_match_name_3 ()
112 this._main_loop = new GLib.MainLoop (null, false);
114 this._match = Folks.MatchResult.MIN;
115 this._added_personas = false;
116 this._persona_fullname_1 = "Travis Reitter";
117 this._persona_fullname_2 = "Travis R.";
118 this._individual_id_1 = "";
119 this._individual_id_2 = "";
121 this._test_match_name_async ();
123 Timeout.add_seconds (5, () =>
125 this._main_loop.quit ();
126 assert_not_reached ();
129 this._main_loop.run ();
131 assert (this._match >= Folks.MatchResult.MEDIUM);
134 private async void _test_match_name_async ()
136 var store = BackendStore.dup ();
137 yield store.prepare ();
139 if (this._aggregator == null)
141 this._aggregator = new IndividualAggregator ();
142 this._aggregator.individuals_changed.connect
143 (this._individuals_changed_cb);
148 yield this._aggregator.prepare ();
150 foreach (var backend in store.enabled_backends)
153 (Trf.PersonaStore) backend.persona_stores.get ("tracker");
154 if (this._pstore != null)
157 assert (this._pstore != null);
158 this._pstore.notify["is-prepared"].connect (this._notify_pstore_cb);
163 GLib.warning ("Error when calling prepare: %s\n", e.message);
167 private void _individuals_changed_cb
168 (GLib.List<Individual>? added,
169 GLib.List<Individual>? removed,
172 GroupDetails.ChangeReason reason)
174 foreach (unowned Individual i in added)
176 if (i.full_name == this._persona_fullname_1)
178 this._individual_id_1 = i.id;
180 else if (i.full_name == this._persona_fullname_2)
182 this._individual_id_2 = i.id;
186 if (this._individual_id_1 != "" &&
187 this._individual_id_2 != "")
189 this._try_potential_match ();
192 assert (removed == null);
195 private void _try_potential_match ()
197 var ind1 = this._aggregator.individuals.lookup (this._individual_id_1);
198 var ind2 = this._aggregator.individuals.lookup (this._individual_id_2);
200 Folks.PotentialMatch matchObj = new Folks.PotentialMatch ();
201 this._match = matchObj.potential_match (ind1, ind2);
203 this._main_loop.quit ();
206 private void _notify_pstore_cb (Object _pstore, ParamSpec ps)
211 private async void _try_to_add ()
213 lock (this._added_personas)
215 if (this._pstore.is_prepared &&
216 this._added_personas == false)
218 this._added_personas = true;
219 yield this._add_personas ();
224 private async void _add_personas ()
226 HashTable<string, Value?> details1 = new HashTable<string, Value?>
227 (str_hash, str_equal);
228 HashTable<string, Value?> details2 = new HashTable<string, Value?>
229 (str_hash, str_equal);
232 val = Value (typeof (string));
233 val.set_string (this._persona_fullname_1);
234 details1.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
237 val = Value (typeof (string));
238 val.set_string (this._persona_fullname_2);
239 details2.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
244 yield this._aggregator.add_persona_from_details (null,
245 this._pstore, details1);
247 yield this._aggregator.add_persona_from_details (null,
248 this._pstore, details2);
250 catch (Folks.IndividualAggregatorError e)
252 GLib.warning ("[AddPersonaError] add_persona_from_details: %s\n",
258 public int main (string[] args)
260 Test.init (ref args);
262 TestSuite root = TestSuite.get_root ();
263 root.add_suite (new MatchNameTests ().get_suite ());