78dfcea1dc70f6707e8c3f9b41ae8f7d5559f3f1
[platform/upstream/folks.git] / tests / eds / set-roles.vala
1 /*
2  * Copyright (C) 2011 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  * Authors: Raul Gutierrez Segales <raul.gutierrez.segales@collabora.co.uk>
18  *
19  */
20
21 using EdsTest;
22 using Folks;
23 using Gee;
24
25 public class SetRolesTests : Folks.TestCase
26 {
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;
32
33
34   public SetRolesTests ()
35     {
36       base ("SetRoles");
37
38       this.add_test ("setting roles on e-d-s persona",
39           this.test_set_roles);
40     }
41
42   public override void set_up ()
43     {
44       this._eds_backend = new EdsTest.Backend ();
45       this._eds_backend.set_up ();
46
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);
50     }
51
52   public override void tear_down ()
53     {
54       this._eds_backend.tear_down ();
55     }
56
57   void test_set_roles ()
58     {
59       Gee.HashMap<string, Value?> c1 = new Gee.HashMap<string, Value?> ();
60       this._main_loop = new GLib.MainLoop (null, false);
61       Value? v;
62
63       this._found_before_update = false;
64       this._found_after_update = false;
65
66       this._eds_backend.reset ();
67
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);
72
73       this._test_set_roles_async ();
74
75       Timeout.add_seconds (5, () => {
76             this._main_loop.quit ();
77             assert_not_reached ();
78           });
79
80       this._main_loop.run ();
81
82       assert (this._found_before_update);
83       assert (this._found_after_update);
84     }
85
86   private async void _test_set_roles_async ()
87     {
88       yield this._eds_backend.commit_contacts_to_addressbook ();
89
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);
95       try
96         {
97           yield this._aggregator.prepare ();
98         }
99       catch (GLib.Error e)
100         {
101           GLib.warning ("Error when calling prepare: %s\n", e.message);
102         }
103     }
104
105   private void _individuals_changed_cb (
106        MultiMap<Individual?, Individual?> changes)
107     {
108       var added = changes.get_values ();
109
110       foreach (Individual i in added)
111         {
112           var name = (Folks.NameDetails) i;
113
114           if (name.full_name == "The Guard")
115             {
116               i.notify["roles"].connect (this._notify_roles_cb);
117               this._found_before_update = true;
118
119               foreach (var p in i.personas)
120                 {
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;
129                 }
130             }
131         }
132     }
133
134   private void _notify_roles_cb (Object individual_obj, ParamSpec ps)
135     {
136       Folks.Individual i = (Folks.Individual) individual_obj;
137       foreach (var role_fd in i.roles)
138         {
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))
143             {
144               this._found_after_update = true;
145               this._main_loop.quit ();
146             }
147         }
148     }
149 }
150
151 public int main (string[] args)
152 {
153   Test.init (ref args);
154
155   TestSuite root = TestSuite.get_root ();
156   root.add_suite (new SetRolesTests ().get_suite ());
157
158   Test.run ();
159
160   return 0;
161 }