Change EmailDetails.email_addresses to be a Set<FieldDetails>
[platform/upstream/folks.git] / tests / tracker / match-known-emails.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 MatchKnownEmailsTests : Folks.TestCase
27 {
28   private GLib.MainLoop _main_loop;
29   private TrackerTest.Backend _tracker_backend;
30   private IndividualAggregator _aggregator;
31
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";
36
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;
43
44   public MatchKnownEmailsTests ()
45     {
46       base ("MatchKnownEmailsTests");
47
48       this._tracker_backend = new TrackerTest.Backend ();
49
50       this.add_test ("test potential match with same email addresses ",
51           this.test_match_email_addresses);
52     }
53
54   public override void set_up ()
55     {
56     }
57
58   public override void tear_down ()
59     {
60       this._tracker_backend.tear_down ();
61     }
62
63   public void test_match_email_addresses ()
64     {
65       this._main_loop = new GLib.MainLoop (null, false);
66
67       this._test_match_email_addresses_async ();
68
69       Timeout.add_seconds (5, () =>
70         {
71           this._main_loop.quit ();
72           assert_not_reached ();
73         });
74
75       this._main_loop.run ();
76
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);
82     }
83
84   private async void _test_match_email_addresses_async ()
85     {
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);
91       try
92         {
93           yield this._aggregator.prepare ();
94           this._pstore = null;
95           foreach (var backend in store.enabled_backends)
96             {
97               this._pstore =
98                 (Trf.PersonaStore) backend.persona_stores.get ("tracker");
99               if (this._pstore != null)
100                 break;
101             }
102           assert (this._pstore != null);
103           this._pstore.notify["is-prepared"].connect (this._notify_pstore_cb);
104           this._try_to_add ();
105         }
106       catch (GLib.Error e)
107         {
108           GLib.warning ("Error when calling prepare: %s\n", e.message);
109         }
110     }
111
112   private void _individuals_changed_cb
113       (GLib.List<Individual>? added,
114        GLib.List<Individual>? removed,
115        string? message,
116        Persona? actor,
117        GroupDetails.ChangeReason reason)
118     {
119       foreach (unowned Individual i in added)
120         {
121           if (i.full_name == this._persona_fullname_1)
122             {
123               this._individual_id_1 = i.id;
124             }
125           else if (i.full_name == this._persona_fullname_2)
126             {
127               this._individual_id_2 = i.id;
128             }
129         }
130
131       if (this._individual_id_1 != "" &&
132           this._individual_id_2 != "")
133         {
134           this._try_potential_match ();
135         }
136
137       assert (removed == null);
138     }
139
140   private void _try_potential_match ()
141     {
142       var ind1 = this._aggregator.individuals.lookup (this._individual_id_1);
143       var ind2 = this._aggregator.individuals.lookup (this._individual_id_2);
144
145       Folks.PotentialMatch matchObj = new Folks.PotentialMatch ();
146       this._match = matchObj.potential_match (ind1, ind2);
147
148       this._main_loop.quit ();
149     }
150
151   private void _notify_pstore_cb (Object _pstore, ParamSpec ps)
152     {
153       this._try_to_add ();
154     }
155
156   private async void _try_to_add ()
157     {
158       lock (this._added_personas)
159         {
160           if (this._pstore.is_prepared &&
161               this._added_personas == false)
162             {
163               this._added_personas = true;
164               yield this._add_personas ();
165             }
166         }
167     }
168
169   private async void _add_personas ()
170     {
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);
175       Value? val;
176
177       val = Value (typeof (string));
178       val.set_string (this._persona_fullname_1);
179       details1.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
180           (owned) val);
181
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);
187       details1.insert (
188           Folks.PersonaStore.detail_key (PersonaDetail.EMAIL_ADDRESSES),
189           (owned) val);
190
191       val = Value (typeof (string));
192       val.set_string (this._persona_fullname_2);
193       details2.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
194           (owned) val);
195
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);
201       details2.insert (
202           Folks.PersonaStore.detail_key (PersonaDetail.EMAIL_ADDRESSES),
203           (owned) val);
204
205      try
206         {
207           yield this._aggregator.add_persona_from_details (null,
208               this._pstore, details1);
209
210           yield this._aggregator.add_persona_from_details (null,
211               this._pstore, details2);
212         }
213       catch (Folks.IndividualAggregatorError e)
214         {
215           GLib.warning ("[AddPersonaError] add_persona_from_details: %s\n",
216               e.message);
217         }
218     }
219 }
220
221 public int main (string[] args)
222 {
223   Test.init (ref args);
224
225   TestSuite root = TestSuite.get_root ();
226   root.add_suite (new MatchKnownEmailsTests ().get_suite ());
227
228   Test.run ();
229
230   return 0;
231 }