06734a9f1cb16a59591c5abcf4427cdc038e69b9
[platform/upstream/folks.git] / tests / tracker / match-im-addresses.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 MatchIMAddressesTests : Folks.TestCase
27 {
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;
40
41   public MatchIMAddressesTests ()
42     {
43       base ("MatchIMAddressesTests");
44
45       this._tracker_backend = new TrackerTest.Backend ();
46
47       this.add_test ("test potential match by IM addresses ",
48           this.test_match_im_addresses);
49     }
50
51   public override void set_up ()
52     {
53     }
54
55   public override void tear_down ()
56     {
57       this._tracker_backend.tear_down ();
58     }
59
60   public void test_match_im_addresses ()
61     {
62       this._main_loop = new GLib.MainLoop (null, false);
63
64       this._test_match_im_addresses_async ();
65
66       Timeout.add_seconds (5, () =>
67         {
68           this._main_loop.quit ();
69           assert_not_reached ();
70         });
71
72       this._main_loop.run ();
73       assert (this._match >= Folks.MatchResult.HIGH);
74     }
75
76   private async void _test_match_im_addresses_async ()
77     {
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);
83       try
84         {
85           yield this._aggregator.prepare ();
86           this._pstore = null;
87           foreach (var backend in store.enabled_backends)
88             {
89               this._pstore =
90                 (Trf.PersonaStore) backend.persona_stores.lookup ("tracker");
91               if (this._pstore != null)
92                 break;
93             }
94           assert (this._pstore != null);
95           this._pstore.notify["is-prepared"].connect (this._notify_pstore_cb);
96           this._try_to_add ();
97         }
98       catch (GLib.Error e)
99         {
100           GLib.warning ("Error when calling prepare: %s\n", e.message);
101         }
102     }
103
104   private void _individuals_changed_cb
105       (GLib.List<Individual>? added,
106        GLib.List<Individual>? removed,
107        string? message,
108        Persona? actor,
109        GroupDetails.ChangeReason reason)
110     {
111       foreach (unowned Individual i in added)
112         {
113           if (i.full_name == this._persona_fullname_1)
114             {
115               this._individual_id_1 = i.id;
116             }
117           else if (i.full_name == this._persona_fullname_2)
118             {
119               this._individual_id_2 = i.id;
120             }
121         }
122
123       if (this._individual_id_1 != "" &&
124           this._individual_id_2 != "")
125         {
126           this._try_potential_match ();
127         }
128
129       assert (removed == null);
130     }
131
132   private void _try_potential_match ()
133     {
134       var ind1 = this._aggregator.individuals.lookup (this._individual_id_1);
135       var ind2 = this._aggregator.individuals.lookup (this._individual_id_2);
136
137       Folks.PotentialMatch matchObj = new Folks.PotentialMatch ();
138       this._match = matchObj.potential_match (ind1, ind2);
139
140       this._main_loop.quit ();
141     }
142
143   private void _notify_pstore_cb (Object _pstore, ParamSpec ps)
144     {
145       this._try_to_add ();
146     }
147
148   private async void _try_to_add ()
149     {
150       lock (this._added_personas)
151         {
152           if (this._pstore.is_prepared &&
153               this._added_personas == false)
154             {
155               this._added_personas = true;
156               yield this._add_personas ();
157             }
158         }
159     }
160
161   private async void _add_personas ()
162     {
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);
167       Value? val;
168       HashMultiMap<string, string> im_addrs;
169
170       val = Value (typeof (string));
171       val.set_string (this._persona_fullname_1);
172       details1.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
173           (owned) val);
174
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);
180       details1.insert (
181           Folks.PersonaStore.detail_key (PersonaDetail.IM_ADDRESSES),
182           (owned) val);
183
184       val = Value (typeof (string));
185       val.set_string (this._persona_fullname_2);
186       details2.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
187           (owned) val);
188
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);
193       details2.insert (
194           Folks.PersonaStore.detail_key (PersonaDetail.IM_ADDRESSES),
195           (owned) val);
196
197      try
198         {
199           yield this._aggregator.add_persona_from_details (null,
200               this._pstore, details1);
201
202           yield this._aggregator.add_persona_from_details (null,
203               this._pstore, details2);
204         }
205       catch (Folks.IndividualAggregatorError e)
206         {
207           GLib.warning ("[AddPersonaError] add_persona_from_details: %s\n",
208               e.message);
209         }
210     }
211 }
212
213 public int main (string[] args)
214 {
215   Test.init (ref args);
216
217   TestSuite root = TestSuite.get_root ();
218   root.add_suite (new MatchIMAddressesTests ().get_suite ());
219
220   Test.run ();
221
222   return 0;
223 }