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>
25 public class SetRolesTests : Folks.TestCase
27 private EdsTest.Backend _eds_backend;
28 private IndividualAggregator _aggregator;
29 private GLib.MainLoop _main_loop;
30 private bool _found_before_update;
31 private bool _found_after_update;
34 public SetRolesTests ()
38 this.add_test ("setting roles on e-d-s persona",
42 public override void set_up ()
44 this._eds_backend = new EdsTest.Backend ();
45 this._eds_backend.set_up ();
47 /* We configure eds as the primary store */
48 var config_val = "eds:%s".printf (this._eds_backend.address_book_uid);
49 Environment.set_variable ("FOLKS_PRIMARY_STORE", config_val, true);
52 public override void tear_down ()
54 this._eds_backend.tear_down ();
57 void test_set_roles ()
59 Gee.HashMap<string, Value?> c1 = new Gee.HashMap<string, Value?> ();
60 this._main_loop = new GLib.MainLoop (null, false);
63 this._found_before_update = false;
64 this._found_after_update = false;
66 this._eds_backend.reset ();
68 v = Value (typeof (string));
69 v.set_string ("The Guard");
70 c1.set ("full_name", (owned) v);
71 this._eds_backend.add_contact (c1);
73 this._test_set_roles_async ();
75 Timeout.add_seconds (5, () => {
76 this._main_loop.quit ();
77 assert_not_reached ();
80 this._main_loop.run ();
82 assert (this._found_before_update);
83 assert (this._found_after_update);
86 private async void _test_set_roles_async ()
88 yield this._eds_backend.commit_contacts_to_addressbook ();
90 var store = BackendStore.dup ();
91 yield store.prepare ();
92 this._aggregator = new IndividualAggregator ();
93 this._aggregator.individuals_changed_detailed.connect
94 (this._individuals_changed_cb);
97 yield this._aggregator.prepare ();
101 GLib.warning ("Error when calling prepare: %s\n", e.message);
105 private void _individuals_changed_cb (
106 MultiMap<Individual?, Individual?> changes)
108 var added = changes.get_values ();
110 foreach (Individual i in added)
112 var name = (Folks.NameDetails) i;
114 if (name.full_name == "The Guard")
116 i.notify["roles"].connect (this._notify_roles_cb);
117 this._found_before_update = true;
119 foreach (var p in i.personas)
121 var role_fds = new HashSet<RoleFieldDetails> (
122 AbstractFieldDetails<Role>.hash_static,
123 AbstractFieldDetails<Role>.equal_static);
124 var r1 = new Role ("Dr.", "The Nut House Ltd");
125 r1.role = "The Manager";
126 var role_fd1 = new RoleFieldDetails (r1);
127 role_fds.add (role_fd1);
128 ((RoleDetails) p).roles = role_fds;
134 private void _notify_roles_cb (Object individual_obj, ParamSpec ps)
136 Folks.Individual i = (Folks.Individual) individual_obj;
137 foreach (var role_fd in i.roles)
139 var r1 = new Role ("Dr.", "The Nut House Ltd");
140 r1.role = "The Manager";
141 var role_fd_expected = new RoleFieldDetails (r1);
142 if (role_fd.equal (role_fd_expected))
144 this._found_after_update = true;
145 this._main_loop.quit ();
151 public int main (string[] args)
153 Test.init (ref args);
155 TestSuite root = TestSuite.get_root ();
156 root.add_suite (new SetRolesTests ().get_suite ());