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 ();
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.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 (Set<Individual> added,
105 Set<Individual> removed,
108 GroupDetails.ChangeReason reason)
110 foreach (var i in added)
112 if (i.full_name == this._persona_fullname)
114 i.notify["email-addresses"].connect (this._notify_emails_cb);
116 var emails = new HashSet<FieldDetails> (
117 (GLib.HashFunc) FieldDetails.hash,
118 (GLib.EqualFunc) FieldDetails.equal);
119 var p1 = new FieldDetails (this._email_1);
121 var p2 = new FieldDetails (this._email_2);
124 foreach (var p in i.personas)
126 ((EmailDetails) p).email_addresses = emails;
131 assert (removed.size == 0);
134 private void _notify_emails_cb (Object individual_obj, ParamSpec ps)
136 Folks.Individual i = (Folks.Individual) individual_obj;
137 if (i.full_name == this._persona_fullname)
139 foreach (var p in i.email_addresses)
141 if (p.value == this._email_1)
142 this._email_1_found = true;
143 else if (p.value == this._email_2)
144 this._email_2_found = true;
148 if (this._email_1_found && this._email_2_found)
150 this._main_loop.quit ();
155 public int main (string[] args)
157 Test.init (ref args);
159 TestSuite root = TestSuite.get_root ();
160 root.add_suite (new SetEmailsTests ().get_suite ());