4696c5aa26713b66d42aeb15682c8f6a838775db
[platform/upstream/folks.git] / tests / tracker / 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 Tracker.Sparql;
22 using TrackerTest;
23 using Folks;
24 using Gee;
25
26 public class SetRolesTests : TrackerTest.TestCase
27 {
28   private GLib.MainLoop _main_loop;
29   private IndividualAggregator _aggregator;
30   private string _persona_fullname;
31   private bool _role_found;
32   private RoleFieldDetails _role_fd;
33
34   public SetRolesTests ()
35     {
36       base ("SetRolesTests");
37
38       this.add_test ("test setting roles ",
39           this.test_set_roles);
40     }
41
42   public void test_set_roles ()
43     {
44       this._main_loop = new GLib.MainLoop (null, false);
45       Gee.HashMap<string, string> c1 = new Gee.HashMap<string, string> ();
46       this._persona_fullname = "persona #1";
47
48       c1.set (Trf.OntologyDefs.NCO_FULLNAME, this._persona_fullname);
49       ((!) this.tracker_backend).add_contact (c1);
50
51       var role = new Role ("some title", "some organisation");
52       role.role = "some role";
53       this._role_fd = new RoleFieldDetails (role);
54
55       ((!) this.tracker_backend).set_up ();
56
57       this._role_found = false;
58
59       this._test_set_roles_async.begin ();
60
61       TestUtils.loop_run_with_timeout (this._main_loop);
62
63       assert (this._role_found);
64     }
65
66   private async void _test_set_roles_async ()
67     {
68       var store = BackendStore.dup ();
69       yield store.prepare ();
70       this._aggregator = new IndividualAggregator ();
71       this._aggregator.individuals_changed_detailed.connect
72           (this._individuals_changed_cb);
73       try
74         {
75           yield this._aggregator.prepare ();
76         }
77       catch (GLib.Error e)
78         {
79           GLib.warning ("Error when calling prepare: %s\n", e.message);
80         }
81     }
82
83   private void _individuals_changed_cb (
84        MultiMap<Individual?, Individual?> changes)
85     {
86       var added = changes.get_values ();
87       var removed = changes.get_keys ();
88
89       foreach (var i in added)
90         {
91           assert (i != null);
92
93           if (i.full_name == this._persona_fullname)
94             {
95               i.notify["roles"].connect (this._notify_roles_cb);
96
97               Gee.HashSet<RoleFieldDetails> role_fds =
98                 new HashSet<RoleFieldDetails> (
99                     AbstractFieldDetails<Role>.hash_static,
100                     AbstractFieldDetails<Role>.equal_static);
101               var role = new Role ("some title", "some organisation");
102               role.role = "some role";
103               var role_fd = new RoleFieldDetails (role);
104               role_fds.add ((owned) role_fd);
105
106               foreach (var p in i.personas)
107                 {
108                   ((RoleDetails) p).roles = role_fds;
109                 }
110             }
111         }
112
113       assert (removed.size == 1);
114
115       foreach (var i in removed)
116         {
117           assert (i == null);
118         }
119     }
120
121   private void _notify_roles_cb (Object individual_obj, ParamSpec ps)
122     {
123       Folks.Individual i = (Folks.Individual) individual_obj;
124       if (i.full_name == this._persona_fullname)
125         {
126           foreach (var role_fd in i.roles)
127             {
128               if (role_fd.equal (this._role_fd))
129                 {
130                   this._role_found = true;
131                   this._main_loop.quit ();
132                 }
133             }
134         }
135     }
136 }
137
138 public int main (string[] args)
139 {
140   Test.init (ref args);
141
142   var tests = new SetRolesTests ();
143   tests.register ();
144   Test.run ();
145   tests.final_tear_down ();
146
147   return 0;
148 }