Port folks to libgee 0.8.
[platform/upstream/folks.git] / tests / tracker / set-emails.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 SetEmailsTests : 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 string _email_1;
33   private string _email_2;
34   private bool _email_1_found;
35   private bool _email_2_found;
36
37   public SetEmailsTests ()
38     {
39       base ("SetEmailsTests");
40
41       this._tracker_backend = new TrackerTest.Backend ();
42
43       this.add_test ("test setting emails ", this.test_set_emails);
44     }
45
46   public override void set_up ()
47     {
48     }
49
50   public override void tear_down ()
51     {
52     }
53
54   public void test_set_emails ()
55     {
56       this._main_loop = new GLib.MainLoop (null, false);
57       Gee.HashMap<string, string> c1 = new Gee.HashMap<string, string> ();
58       this._persona_fullname = "persona #1";
59       this._email_1 = "email-1@example.org";
60       this._email_2 = "email-2@example.org";
61
62       c1.set (Trf.OntologyDefs.NCO_FULLNAME, this._persona_fullname);
63       this._tracker_backend.add_contact (c1);
64
65       this._tracker_backend.set_up ();
66
67       this._email_1_found = false;
68       this._email_2_found = false;
69
70       this._test_set_emails_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._email_1_found);
81       assert (this._email_2_found);
82
83      this._tracker_backend.tear_down ();
84     }
85
86   private async void _test_set_emails_async ()
87     {
88       var store = BackendStore.dup ();
89       yield store.prepare ();
90       this._aggregator = new IndividualAggregator ();
91       this._aggregator.individuals_changed_detailed.connect
92           (this._individuals_changed_cb);
93       try
94         {
95           yield this._aggregator.prepare ();
96         }
97       catch (GLib.Error e)
98         {
99           GLib.warning ("Error when calling prepare: %s\n", e.message);
100         }
101     }
102
103   private void _individuals_changed_cb (
104        MultiMap<Individual?, Individual?> changes)
105     {
106       var added = changes.get_values ();
107       var removed = changes.get_keys ();
108
109       foreach (var i in added)
110         {
111           assert (i != null);
112
113           if (i.full_name == this._persona_fullname)
114             {
115               i.notify["email-addresses"].connect (this._notify_emails_cb);
116
117               var emails = new HashSet<EmailFieldDetails> (
118                    AbstractFieldDetails<string>.hash_static,
119                    AbstractFieldDetails<string>.equal_static);
120               var p1 = new EmailFieldDetails (this._email_1);
121               emails.add (p1);
122               var p2 = new EmailFieldDetails (this._email_2);
123               emails.add (p2);
124
125               foreach (var p in i.personas)
126                 {
127                   ((EmailDetails) p).email_addresses = emails;
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_emails_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 p in i.email_addresses)
146             {
147               if (p.value == this._email_1)
148                 this._email_1_found = true;
149               else if (p.value == this._email_2)
150                 this._email_2_found = true;
151             }
152         }
153
154       if (this._email_1_found && this._email_2_found)
155         {
156           this._main_loop.quit ();
157         }
158     }
159 }
160
161 public int main (string[] args)
162 {
163   Test.init (ref args);
164
165   TestSuite root = TestSuite.get_root ();
166   root.add_suite (new SetEmailsTests ().get_suite ());
167
168   Test.run ();
169
170   return 0;
171 }