Revert "Port folks to libgee 0.8."
[platform/upstream/folks.git] / tests / tracker / remove-persona.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 RemovePersonaTests : 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 _persona_removed;
33   private bool _individual_removed;
34   private string _individual_id;
35   private PersonaStore _pstore;
36   private string _persona_id;
37   private Individual _individual;
38   private bool _added_persona = false;
39
40   public RemovePersonaTests ()
41     {
42       base ("RemovePersonaTests");
43
44       this._tracker_backend = new TrackerTest.Backend ();
45
46       this.add_test ("test adding personas to Tracker ", this.test_remove_persona);
47     }
48
49   public override void set_up ()
50     {
51     }
52
53   public override void tear_down ()
54     {
55     }
56
57   public void test_remove_persona ()
58     {
59       this._main_loop = new GLib.MainLoop (null, false);
60       this._persona_fullname = "persona #1";
61
62       this._persona_removed = false;
63       this._individual_removed = false;
64
65       this._test_remove_persona_async ();
66
67       Timeout.add_seconds (5, () =>
68         {
69           this._main_loop.quit ();
70           assert_not_reached ();
71         });
72
73       this._main_loop.run ();
74
75       assert (this._persona_removed == true);
76       assert (this._individual_removed == true);
77
78       this._tracker_backend.tear_down ();
79     }
80
81   private async void _test_remove_persona_async ()
82     {
83       var store = BackendStore.dup ();
84       yield store.prepare ();
85       this._aggregator = new IndividualAggregator ();
86       this._aggregator.individuals_changed_detailed.connect
87           (this._individuals_changed_cb);
88       try
89         {
90           yield this._aggregator.prepare ();
91
92           this._pstore = null;
93           foreach (var backend in store.enabled_backends.values)
94             {
95               this._pstore = backend.persona_stores.get ("tracker");
96               if (this._pstore != null)
97                 break;
98             }
99           assert (this._pstore != null);
100
101           this._pstore.notify["is-prepared"].connect (this._notify_pstore_cb);
102           this._try_to_add ();
103         }
104       catch (GLib.Error e)
105         {
106           GLib.warning ("Error when calling prepare: %s\n", e.message);
107         }
108     }
109
110   private void _notify_pstore_cb (Object _pstore, ParamSpec ps)
111     {
112       this._try_to_add ();
113     }
114
115   private void _try_to_add ()
116     {
117       if (this._pstore.is_prepared &&
118           this._added_persona == false)
119         {
120           this._added_persona = true;
121           this._add_persona ();
122         }
123     }
124
125   private async void _add_persona ()
126     {
127       HashTable<string, Value?> details = new HashTable<string, Value?>
128           (str_hash, str_equal);
129       Value? v1 = Value (typeof (string));
130       v1.set_string (this._persona_fullname);
131       details.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
132           (owned) v1);
133
134       Value? v2 = Value (typeof (Set));
135       var emails = new HashSet<EmailFieldDetails> (
136           (GLib.HashFunc) EmailFieldDetails.hash,
137           (GLib.EqualFunc) EmailFieldDetails.equal);
138       var email_1 = new EmailFieldDetails ("test-1@example.org");
139       emails.add (email_1);
140       var email_2 = new EmailFieldDetails ("test-2@example.org");
141       emails.add (email_2);
142       v2.set_object (emails);
143       details.insert (
144           Folks.PersonaStore.detail_key (PersonaDetail.EMAIL_ADDRESSES),
145           (owned) v2);
146
147       try
148         {
149           yield this._aggregator.add_persona_from_details
150               (null, this._pstore, details);
151         }
152       catch (Folks.IndividualAggregatorError e)
153         {
154           GLib.warning ("[RemovePersonaError] add_persona_from_details: %s\n",
155               e.message);
156         }
157     }
158
159   private void _individuals_changed_cb (
160        MultiMap<Individual?, Individual?> changes)
161     {
162       var added = changes.get_values ();
163       var removed = changes.get_keys ();
164
165       foreach (var i in added)
166         {
167           if (i == null)
168             {
169               continue;
170             }
171
172           if (i.full_name == this._persona_fullname)
173             {
174               this._individual_id = i.id;
175
176               /* Only examine the first persona */
177               foreach (var p in i.personas)
178                 {
179                   this._persona_id = p.iid;
180                   break;
181                 }
182
183               this._individual = i;
184               if (this._pstore.personas.has_key (this._persona_id) == true)
185                 {
186                   this._pstore.personas_changed.connect (this._personas_cb);
187                   this._aggregator.remove_individual (this._individual);
188                 }
189             }
190         }
191
192       foreach (var i in removed)
193         {
194           if (i == null)
195             {
196               continue;
197             }
198
199           if (i.id == this._individual_id)
200             {
201               this._individual_removed = true;
202             }
203         }
204     }
205
206   private void _personas_cb ()
207     {
208       if (this._pstore.personas.has_key (this._persona_id) == false)
209         {
210           this._persona_removed = true;
211           this._main_loop.quit ();
212         }
213     }
214 }
215
216 public int main (string[] args)
217 {
218   Test.init (ref args);
219
220   TestSuite root = TestSuite.get_root ();
221   root.add_suite (new RemovePersonaTests ().get_suite ());
222
223   Test.run ();
224
225   return 0;
226 }