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 : 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 string _email_1;
33 private string _email_2;
34 private bool _email_1_found;
35 private bool _email_2_found;
37 public SetEmailsTests ()
39 base ("SetEmailsTests");
41 this._tracker_backend = new TrackerTest.Backend ();
43 this.add_test ("test setting emails ", this.test_set_emails);
46 public override void set_up ()
50 public override void tear_down ()
54 public void test_set_emails ()
56 this._main_loop = new GLib.MainLoop (null, false);
57 Gee.HashMap<string, string> c1 = new Gee.HashMap<string, string> ();
58 this._persona_fullname = "persona #1";
59 this._email_1 = "email-1@example.org";
60 this._email_2 = "email-2@example.org";
62 c1.set (Trf.OntologyDefs.NCO_FULLNAME, this._persona_fullname);
63 this._tracker_backend.add_contact (c1);
65 this._tracker_backend.set_up ();
67 this._email_1_found = false;
68 this._email_2_found = false;
70 this._test_set_emails_async.begin ();
72 Timeout.add_seconds (5, () =>
74 this._main_loop.quit ();
75 assert_not_reached ();
78 this._main_loop.run ();
80 assert (this._email_1_found);
81 assert (this._email_2_found);
83 this._tracker_backend.tear_down ();
86 private async void _test_set_emails_async ()
88 var store = BackendStore.dup ();
89 yield store.prepare ();
90 this._aggregator = new IndividualAggregator ();
91 this._aggregator.individuals_changed_detailed.connect
92 (this._individuals_changed_cb);
95 yield this._aggregator.prepare ();
99 GLib.warning ("Error when calling prepare: %s\n", e.message);
103 private void _individuals_changed_cb (
104 MultiMap<Individual?, Individual?> changes)
106 var added = changes.get_values ();
107 var removed = changes.get_keys ();
109 foreach (var i in added)
113 if (i.full_name == this._persona_fullname)
115 i.notify["email-addresses"].connect (this._notify_emails_cb);
117 var emails = new HashSet<EmailFieldDetails> (
118 AbstractFieldDetails<string>.hash_static,
119 AbstractFieldDetails<string>.equal_static);
120 var p1 = new EmailFieldDetails (this._email_1);
122 var p2 = new EmailFieldDetails (this._email_2);
125 foreach (var p in i.personas)
127 ((EmailDetails) p).email_addresses = emails;
132 assert (removed.size == 1);
134 foreach (var i in removed)
140 private void _notify_emails_cb (Object individual_obj, ParamSpec ps)
142 Folks.Individual i = (Folks.Individual) individual_obj;
143 if (i.full_name == this._persona_fullname)
145 foreach (var p in i.email_addresses)
147 if (p.value == this._email_1)
148 this._email_1_found = true;
149 else if (p.value == this._email_2)
150 this._email_2_found = true;
154 if (this._email_1_found && this._email_2_found)
156 this._main_loop.quit ();
161 public int main (string[] args)
163 Test.init (ref args);
165 TestSuite root = TestSuite.get_root ();
166 root.add_suite (new SetEmailsTests ().get_suite ());