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