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 Timeout.add_seconds (5, () =>
63 this._main_loop.quit ();
64 assert_not_reached ();
67 this._main_loop.run ();
69 assert (this._email_1_found);
70 assert (this._email_2_found);
73 private async void _test_set_emails_async ()
75 var store = BackendStore.dup ();
76 yield store.prepare ();
77 this._aggregator = new IndividualAggregator ();
78 this._aggregator.individuals_changed_detailed.connect
79 (this._individuals_changed_cb);
82 yield this._aggregator.prepare ();
86 GLib.warning ("Error when calling prepare: %s\n", e.message);
90 private void _individuals_changed_cb (
91 MultiMap<Individual?, Individual?> changes)
93 var added = changes.get_values ();
94 var removed = changes.get_keys ();
96 foreach (var i in added)
100 if (i.full_name == this._persona_fullname)
102 i.notify["email-addresses"].connect (this._notify_emails_cb);
104 var emails = new HashSet<EmailFieldDetails> (
105 AbstractFieldDetails<string>.hash_static,
106 AbstractFieldDetails<string>.equal_static);
107 var p1 = new EmailFieldDetails (this._email_1);
109 var p2 = new EmailFieldDetails (this._email_2);
112 foreach (var p in i.personas)
114 ((EmailDetails) p).email_addresses = emails;
119 assert (removed.size == 1);
121 foreach (var i in removed)
127 private void _notify_emails_cb (Object individual_obj, ParamSpec ps)
129 Folks.Individual i = (Folks.Individual) individual_obj;
130 if (i.full_name == this._persona_fullname)
132 foreach (var p in i.email_addresses)
134 if (p.value == this._email_1)
135 this._email_1_found = true;
136 else if (p.value == this._email_2)
137 this._email_2_found = true;
141 if (this._email_1_found && this._email_2_found)
143 this._main_loop.quit ();
148 public int main (string[] args)
150 Test.init (ref args);
152 TestSuite root = TestSuite.get_root ();
153 root.add_suite (new SetEmailsTests ().get_suite ());