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.connect
87 (this._individuals_changed_cb);
90 yield this._aggregator.prepare ();
93 foreach (var backend in store.enabled_backends)
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<FieldDetails>));
135 var emails = new HashSet<FieldDetails> ();
136 var email_1 = new FieldDetails ("test-1@example.org");
137 emails.add (email_1);
138 var email_2 = new FieldDetails ("test-2@example.org");
139 emails.add (email_2);
140 v2.set_object (emails);
142 Folks.PersonaStore.detail_key (PersonaDetail.EMAIL_ADDRESSES),
147 yield this._aggregator.add_persona_from_details
148 (null, this._pstore, details);
150 catch (Folks.IndividualAggregatorError e)
152 GLib.warning ("[RemovePersonaError] add_persona_from_details: %s\n",
157 private void _individuals_changed_cb
158 (GLib.List<Individual>? added,
159 GLib.List<Individual>? removed,
162 GroupDetails.ChangeReason reason)
164 foreach (unowned Individual i in added)
166 if (i.full_name == this._persona_fullname)
168 this._individual_id = i.id;
169 this._persona_id = i.personas.nth_data (0).iid;
170 this._individual = i;
171 if (this._pstore.personas.lookup (this._persona_id) != null)
173 this._pstore.personas_changed.connect (this._personas_cb);
174 this._aggregator.remove_individual (this._individual);
179 foreach (unowned Individual i in removed)
181 if (i.id == this._individual_id)
183 this._individual_removed = true;
188 private void _personas_cb ()
190 if (this._pstore.personas.lookup (this._persona_id) == null)
192 this._persona_removed = true;
193 this._main_loop.quit ();
198 public int main (string[] args)
200 Test.init (ref args);
202 TestSuite root = TestSuite.get_root ();
203 root.add_suite (new RemovePersonaTests ().get_suite ());