Change Backend.persona_stores to be a Map<string, PersonaStore>
[platform/upstream/folks.git] / tests / tracker / match-name.vala
1 /*
2  * Copyright (C) 2011 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  * Authors: Raul Gutierrez Segales <raul.gutierrez.segales@collabora.co.uk>
18  *
19  */
20
21 using Tracker.Sparql;
22 using TrackerTest;
23 using Folks;
24 using Gee;
25
26 public class MatchNameTests : Folks.TestCase
27 {
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;
38
39   public MatchNameTests ()
40     {
41       base ("MatchNameTests");
42
43       this._tracker_backend = new TrackerTest.Backend ();
44
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);
51     }
52
53   public override void set_up ()
54     {
55     }
56
57   public override void tear_down ()
58     {
59       this._tracker_backend.tear_down ();
60     }
61
62   public void test_match_name_1 ()
63     {
64       this._main_loop = new GLib.MainLoop (null, false);
65
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 = "";
72
73       this._test_match_name_async ();
74
75       Timeout.add_seconds (5, () =>
76         {
77           this._main_loop.quit ();
78           assert_not_reached ();
79         });
80
81       this._main_loop.run ();
82
83       assert (this._match >= Folks.MatchResult.MEDIUM);
84     }
85
86   public void test_match_name_2 ()
87     {
88       this._main_loop = new GLib.MainLoop (null, false);
89
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 = "";
96
97       this._test_match_name_async ();
98
99       Timeout.add_seconds (5, () =>
100         {
101           this._main_loop.quit ();
102           assert_not_reached ();
103         });
104
105       this._main_loop.run ();
106
107       assert (this._match <= Folks.MatchResult.LOW);
108     }
109
110   public void test_match_name_3 ()
111     {
112       this._main_loop = new GLib.MainLoop (null, false);
113
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 = "";
120
121       this._test_match_name_async ();
122
123       Timeout.add_seconds (5, () =>
124         {
125           this._main_loop.quit ();
126           assert_not_reached ();
127         });
128
129       this._main_loop.run ();
130
131       assert (this._match >= Folks.MatchResult.MEDIUM);
132     }
133
134   private async void _test_match_name_async ()
135     {
136       var store = BackendStore.dup ();
137       yield store.prepare ();
138
139       if (this._aggregator == null)
140         {
141           this._aggregator = new IndividualAggregator ();
142           this._aggregator.individuals_changed.connect
143             (this._individuals_changed_cb);
144         }
145
146       try
147         {
148           yield this._aggregator.prepare ();
149           this._pstore = null;
150           foreach (var backend in store.enabled_backends)
151             {
152               this._pstore =
153                 (Trf.PersonaStore) backend.persona_stores.get ("tracker");
154               if (this._pstore != null)
155                 break;
156             }
157           assert (this._pstore != null);
158           this._pstore.notify["is-prepared"].connect (this._notify_pstore_cb);
159           this._try_to_add ();
160         }
161       catch (GLib.Error e)
162         {
163           GLib.warning ("Error when calling prepare: %s\n", e.message);
164         }
165     }
166
167   private void _individuals_changed_cb
168       (GLib.List<Individual>? added,
169        GLib.List<Individual>? removed,
170        string? message,
171        Persona? actor,
172        GroupDetails.ChangeReason reason)
173     {
174       foreach (unowned Individual i in added)
175         {
176           if (i.full_name == this._persona_fullname_1)
177             {
178               this._individual_id_1 = i.id;
179             }
180           else if (i.full_name == this._persona_fullname_2)
181             {
182               this._individual_id_2 = i.id;
183             }
184         }
185
186       if (this._individual_id_1 != "" &&
187           this._individual_id_2 != "")
188         {
189           this._try_potential_match ();
190         }
191
192       assert (removed == null);
193     }
194
195   private void _try_potential_match ()
196     {
197       var ind1 = this._aggregator.individuals.lookup (this._individual_id_1);
198       var ind2 = this._aggregator.individuals.lookup (this._individual_id_2);
199
200       Folks.PotentialMatch matchObj = new Folks.PotentialMatch ();
201       this._match = matchObj.potential_match (ind1, ind2);
202
203       this._main_loop.quit ();
204     }
205
206   private void _notify_pstore_cb (Object _pstore, ParamSpec ps)
207     {
208       this._try_to_add ();
209     }
210
211   private async void _try_to_add ()
212     {
213       lock (this._added_personas)
214         {
215           if (this._pstore.is_prepared &&
216               this._added_personas == false)
217             {
218               this._added_personas = true;
219               yield this._add_personas ();
220             }
221         }
222     }
223
224   private async void _add_personas ()
225     {
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);
230       Value? val;
231
232       val = Value (typeof (string));
233       val.set_string (this._persona_fullname_1);
234       details1.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
235           (owned) val);
236
237       val = Value (typeof (string));
238       val.set_string (this._persona_fullname_2);
239       details2.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
240           (owned) val);
241
242      try
243         {
244           yield this._aggregator.add_persona_from_details (null,
245               this._pstore, details1);
246
247           yield this._aggregator.add_persona_from_details (null,
248               this._pstore, details2);
249         }
250       catch (Folks.IndividualAggregatorError e)
251         {
252           GLib.warning ("[AddPersonaError] add_persona_from_details: %s\n",
253               e.message);
254         }
255     }
256 }
257
258 public int main (string[] args)
259 {
260   Test.init (ref args);
261
262   TestSuite root = TestSuite.get_root ();
263   root.add_suite (new MatchNameTests ().get_suite ());
264
265   Test.run ();
266
267   return 0;
268 }