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 RemovePersonaTests : Folks.TestCase
28 private GLib.MainLoop _main_loop;
29 private TrackerTest.Backend _tracker_backend;
30 private IndividualAggregator _aggregator;
31 private string _persona_fullname;
32 private bool _persona_removed;
33 private bool _individual_removed;
34 private string _individual_id;
35 private PersonaStore _pstore;
36 private string _persona_id;
37 private Individual _individual;
38 private bool _added_persona = false;
40 public RemovePersonaTests ()
42 base ("RemovePersonaTests");
44 this._tracker_backend = new TrackerTest.Backend ();
46 this.add_test ("test adding personas to Tracker ", this.test_remove_persona);
49 public override void set_up ()
53 public override void tear_down ()
57 public void test_remove_persona ()
59 this._main_loop = new GLib.MainLoop (null, false);
60 this._persona_fullname = "persona #1";
62 this._persona_removed = false;
63 this._individual_removed = false;
65 this._test_remove_persona_async ();
67 Timeout.add_seconds (5, () =>
69 this._main_loop.quit ();
70 assert_not_reached ();
73 this._main_loop.run ();
75 assert (this._persona_removed == true);
76 assert (this._individual_removed == true);
78 this._tracker_backend.tear_down ();
81 private async void _test_remove_persona_async ()
83 var store = BackendStore.dup ();
84 yield store.prepare ();
85 this._aggregator = new IndividualAggregator ();
86 this._aggregator.individuals_changed_detailed.connect
87 (this._individuals_changed_cb);
90 yield this._aggregator.prepare ();
93 foreach (var backend in store.enabled_backends.values)
95 this._pstore = backend.persona_stores.get ("tracker");
96 if (this._pstore != null)
99 assert (this._pstore != null);
101 this._pstore.notify["is-prepared"].connect (this._notify_pstore_cb);
106 GLib.warning ("Error when calling prepare: %s\n", e.message);
110 private void _notify_pstore_cb (Object _pstore, ParamSpec ps)
115 private void _try_to_add ()
117 if (this._pstore.is_prepared &&
118 this._added_persona == false)
120 this._added_persona = true;
121 this._add_persona ();
125 private async void _add_persona ()
127 HashTable<string, Value?> details = new HashTable<string, Value?>
128 (str_hash, str_equal);
129 Value? v1 = Value (typeof (string));
130 v1.set_string (this._persona_fullname);
131 details.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
134 Value? v2 = Value (typeof (Set));
135 var emails = new HashSet<EmailFieldDetails> (
136 (GLib.HashFunc) EmailFieldDetails.hash,
137 (GLib.EqualFunc) EmailFieldDetails.equal);
138 var email_1 = new EmailFieldDetails ("test-1@example.org");
139 emails.add (email_1);
140 var email_2 = new EmailFieldDetails ("test-2@example.org");
141 emails.add (email_2);
142 v2.set_object (emails);
144 Folks.PersonaStore.detail_key (PersonaDetail.EMAIL_ADDRESSES),
149 yield this._aggregator.add_persona_from_details
150 (null, this._pstore, details);
152 catch (Folks.IndividualAggregatorError e)
154 GLib.warning ("[RemovePersonaError] add_persona_from_details: %s\n",
159 private void _individuals_changed_cb (
160 MultiMap<Individual?, Individual?> changes)
162 var added = changes.get_values ();
163 var removed = changes.get_keys ();
165 foreach (var i in added)
172 if (i.full_name == this._persona_fullname)
174 this._individual_id = i.id;
176 /* Only examine the first persona */
177 foreach (var p in i.personas)
179 this._persona_id = p.iid;
183 this._individual = i;
184 if (this._pstore.personas.has_key (this._persona_id) == true)
186 this._pstore.personas_changed.connect (this._personas_cb);
187 this._aggregator.remove_individual (this._individual);
192 foreach (var i in removed)
199 if (i.id == this._individual_id)
201 this._individual_removed = true;
206 private void _personas_cb ()
208 if (this._pstore.personas.has_key (this._persona_id) == false)
210 this._persona_removed = true;
211 this._main_loop.quit ();
216 public int main (string[] args)
218 Test.init (ref args);
220 TestSuite root = TestSuite.get_root ();
221 root.add_suite (new RemovePersonaTests ().get_suite ());