Port IndividualAggregator.individuals_changed to use Set<Individual>
[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.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       (Set<Individual> added,
105        Set<Individual> removed,
106        string? message,
107        Persona? actor,
108        GroupDetails.ChangeReason reason)
109     {
110       foreach (var i in added)
111         {
112           if (i.full_name == this._persona_fullname)
113             {
114               i.notify["email-addresses"].connect (this._notify_emails_cb);
115
116               var emails = new HashSet<FieldDetails> ();
117               var p1 = new FieldDetails (this._email_1);
118               emails.add (p1);
119               var p2 = new FieldDetails (this._email_2);
120               emails.add (p2);
121
122               foreach (var p in i.personas)
123                 {
124                   ((EmailDetails) p).email_addresses = emails;
125                 }
126             }
127         }
128
129       assert (removed.size == 0);
130     }
131
132   private void _notify_emails_cb (Object individual_obj, ParamSpec ps)
133     {
134       Folks.Individual i = (Folks.Individual) individual_obj;
135       if (i.full_name == this._persona_fullname)
136         {
137           foreach (var p in i.email_addresses)
138             {
139               if (p.value == this._email_1)
140                 this._email_1_found = true;
141               else if (p.value == this._email_2)
142                 this._email_2_found = true;
143             }
144         }
145
146       if (this._email_1_found && this._email_2_found)
147         {
148           this._main_loop.quit ();
149         }
150     }
151 }
152
153 public int main (string[] args)
154 {
155   Test.init (ref args);
156
157   TestSuite root = TestSuite.get_root ();
158   root.add_suite (new SetEmailsTests ().get_suite ());
159
160   Test.run ();
161
162   return 0;
163 }