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 SetEmailsTests : TrackerTest.TestCase
28 private GLib.MainLoop _main_loop;
29 private IndividualAggregator _aggregator;
30 private string _persona_fullname;
31 private string _email_1;
32 private string _email_2;
33 private bool _email_1_found;
34 private bool _email_2_found;
36 public SetEmailsTests ()
38 base ("SetEmailsTests");
40 this.add_test ("test setting emails ", this.test_set_emails);
43 public void test_set_emails ()
45 this._main_loop = new GLib.MainLoop (null, false);
46 Gee.HashMap<string, string> c1 = new Gee.HashMap<string, string> ();
47 this._persona_fullname = "persona #1";
48 this._email_1 = "email-1@example.org";
49 this._email_2 = "email-2@example.org";
51 c1.set (Trf.OntologyDefs.NCO_FULLNAME, this._persona_fullname);
52 ((!) this.tracker_backend).add_contact (c1);
54 ((!) this.tracker_backend).set_up ();
56 this._email_1_found = false;
57 this._email_2_found = false;
59 this._test_set_emails_async.begin ();
61 TestUtils.loop_run_with_timeout (this._main_loop);
63 assert (this._email_1_found);
64 assert (this._email_2_found);
67 private async void _test_set_emails_async ()
69 var store = BackendStore.dup ();
70 yield store.prepare ();
71 this._aggregator = new IndividualAggregator ();
72 this._aggregator.individuals_changed_detailed.connect
73 (this._individuals_changed_cb);
76 yield this._aggregator.prepare ();
80 GLib.warning ("Error when calling prepare: %s\n", e.message);
84 private void _individuals_changed_cb (
85 MultiMap<Individual?, Individual?> changes)
87 var added = changes.get_values ();
88 var removed = changes.get_keys ();
90 foreach (var i in added)
94 if (i.full_name == this._persona_fullname)
96 i.notify["email-addresses"].connect (this._notify_emails_cb);
98 var emails = new HashSet<EmailFieldDetails> (
99 AbstractFieldDetails<string>.hash_static,
100 AbstractFieldDetails<string>.equal_static);
101 var p1 = new EmailFieldDetails (this._email_1);
103 var p2 = new EmailFieldDetails (this._email_2);
106 foreach (var p in i.personas)
108 ((EmailDetails) p).email_addresses = emails;
113 assert (removed.size == 1);
115 foreach (var i in removed)
121 private void _notify_emails_cb (Object individual_obj, ParamSpec ps)
123 Folks.Individual i = (Folks.Individual) individual_obj;
124 if (i.full_name == this._persona_fullname)
126 foreach (var p in i.email_addresses)
128 if (p.value == this._email_1)
129 this._email_1_found = true;
130 else if (p.value == this._email_2)
131 this._email_2_found = true;
135 if (this._email_1_found && this._email_2_found)
137 this._main_loop.quit ();
142 public int main (string[] args)
144 Test.init (ref args);
146 var tests = new SetEmailsTests ();
149 tests.final_tear_down ();