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 MatchPhoneNumberTests : Folks.TestCase
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 _phone_1 = "+1-800-123-4567";
34
private string _phone_2 = "123-4567";
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;
41
public MatchPhoneNumberTests ()
43
base ("MatchPhoneNumberTests");
45
this._tracker_backend = new TrackerTest.Backend ();
47
this.add_test ("test potential match with phone numbers ",
48
this.test_match_phone_number);
51
public override void set_up ()
55
public override void tear_down ()
57
this._tracker_backend.tear_down ();
60
public void test_match_phone_number ()
62
this._main_loop = new GLib.MainLoop (null, false);
64
this._test_match_phone_number_async ();
66
Timeout.add_seconds (5, () =>
68
this._main_loop.quit ();
69
assert_not_reached ();
72
this._main_loop.run ();
74
/* Phone number match is very decisive */
75
assert (this._match == Folks.MatchResult.HIGH);
78
private async void _test_match_phone_number_async ()
80
var store = BackendStore.dup ();
81
yield store.prepare ();
82
this._aggregator = new IndividualAggregator ();
83
this._aggregator.individuals_changed.connect
84
(this._individuals_changed_cb);
87
yield this._aggregator.prepare ();
89
foreach (var backend in store.enabled_backends.values)
92
(Trf.PersonaStore) backend.persona_stores.get ("tracker");
93
if (this._pstore != null)
96
assert (this._pstore != null);
97
this._pstore.notify["is-prepared"].connect (this._notify_pstore_cb);
102
GLib.warning ("Error when calling prepare: %s\n", e.message);
106
private void _individuals_changed_cb
107
(Set<Individual> added,
108
Set<Individual> removed,
111
GroupDetails.ChangeReason reason)
113
foreach (var i in added)
115
if (i.full_name == this._persona_fullname_1)
117
this._individual_id_1 = i.id;
119
else if (i.full_name == this._persona_fullname_2)
121
this._individual_id_2 = i.id;
125
if (this._individual_id_1 != "" &&
126
this._individual_id_2 != "")
128
this._try_potential_match ();
131
assert (removed.size == 0);
134
private void _try_potential_match ()
136
var ind1 = this._aggregator.individuals.get (this._individual_id_1);
137
var ind2 = this._aggregator.individuals.get (this._individual_id_2);
139
Folks.PotentialMatch matchObj = new Folks.PotentialMatch ();
140
this._match = matchObj.potential_match (ind1, ind2);
142
this._main_loop.quit ();
145
private void _notify_pstore_cb (Object _pstore, ParamSpec ps)
150
private async void _try_to_add ()
152
lock (this._added_personas)
154
if (this._pstore.is_prepared &&
155
this._added_personas == false)
157
this._added_personas = true;
158
yield this._add_personas ();
163
private async void _add_personas ()
165
HashTable<string, Value?> details1 = new HashTable<string, Value?>
166
(str_hash, str_equal);
167
HashTable<string, Value?> details2 = new HashTable<string, Value?>
168
(str_hash, str_equal);
171
val = Value (typeof (string));
172
val.set_string (this._persona_fullname_1);
173
details1.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
176
val = Value (typeof (Set<FieldDetails>));
177
var phone_numbers1 = new HashSet<FieldDetails> ();
178
var phone_number_1 = new FieldDetails (this._phone_1);
179
phone_numbers1.add (phone_number_1);
180
val.set_object (phone_numbers1);
182
Folks.PersonaStore.detail_key (PersonaDetail.PHONE_NUMBERS),
185
val = Value (typeof (string));
186
val.set_string (this._persona_fullname_2);
187
details2.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
190
val = Value (typeof (Set<FieldDetails>));
191
var phone_numbers2 = new HashSet<FieldDetails> ();
192
var phone_number_2 = new FieldDetails (this._phone_2);
193
phone_numbers2.add (phone_number_2);
194
val.set_object (phone_numbers2);
196
Folks.PersonaStore.detail_key (PersonaDetail.PHONE_NUMBERS),
201
yield this._aggregator.add_persona_from_details (null,
202
this._pstore, details1);
204
yield this._aggregator.add_persona_from_details (null,
205
this._pstore, details2);
207
catch (Folks.IndividualAggregatorError e)
209
GLib.warning ("[AddPersonaError] add_persona_from_details: %s\n",
215
public int main (string[] args)
217
Test.init (ref args);
219
TestSuite root = TestSuite.get_root ();
220
root.add_suite (new MatchPhoneNumberTests ().get_suite ());