eds test: don't assume Individuals remain after changing a linkable property
[platform/upstream/folks.git] / tests / eds / 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 EdsTest;
22 using Folks;
23 using Gee;
24
25 public class SetEmailsTests : Folks.TestCase
26 {
27   private EdsTest.Backend _eds_backend;
28   private IndividualAggregator _aggregator;
29   private GLib.MainLoop _main_loop;
30   private bool _found_before_update;
31   private bool _found_after_update;
32
33   public SetEmailsTests ()
34     {
35       base ("SetEmails");
36
37       this.add_test ("setting emails on e-d-s persona", this.test_set_emails);
38     }
39
40   public override void set_up ()
41     {
42       this._eds_backend = new EdsTest.Backend ();
43       this._eds_backend.set_up ();
44
45       /* We configure eds as the primary store */
46       var config_val = "eds:%s".printf (this._eds_backend.address_book_uid);
47       Environment.set_variable ("FOLKS_PRIMARY_STORE", config_val, true);
48     }
49
50   public override void tear_down ()
51     {
52       this._eds_backend.tear_down ();
53     }
54
55   void test_set_emails ()
56     {
57       Gee.HashMap<string, Value?> c1 = new Gee.HashMap<string, Value?> ();
58       this._main_loop = new GLib.MainLoop (null, false);
59       Value? v;
60
61       this._found_before_update = false;
62       this._found_after_update = false;
63
64       this._eds_backend.reset ();
65
66       v = Value (typeof (string));
67       v.set_string ("bernie h. innocenti");
68       c1.set ("full_name", (owned) v);
69       this._eds_backend.add_contact (c1);
70
71       this._test_set_emails_async.begin ();
72
73       Timeout.add_seconds (5, () => {
74             this._main_loop.quit ();
75             assert_not_reached ();
76           });
77
78       this._main_loop.run ();
79
80       assert (this._found_before_update);
81       assert (this._found_after_update);
82     }
83
84   private async void _test_set_emails_async ()
85     {
86       yield this._eds_backend.commit_contacts_to_addressbook ();
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
108       foreach (Individual i in added)
109         {
110           assert (i != null);
111
112           var name = (Folks.NameDetails) i;
113
114           if (name.full_name != "bernie h. innocenti")
115             continue;
116
117           /* Because we update a linkable property, we'll get a new individual
118            * because re-linking has to happen.
119            */
120           if (!this._found_before_update)
121             {
122               this._found_before_update = true;
123
124               foreach (var p in i.personas)
125                 {
126                   var emails = new HashSet<EmailFieldDetails> (
127                       (GLib.HashFunc) EmailFieldDetails.hash,
128                       (GLib.EqualFunc) EmailFieldDetails.equal);
129                   var email_1 = new EmailFieldDetails ("bernie@example.org");
130                   email_1.set_parameter (AbstractFieldDetails.PARAM_TYPE,
131                       AbstractFieldDetails.PARAM_TYPE_OTHER);
132                   emails.add (email_1);
133                   ((EmailDetails) p).email_addresses = emails;
134                 }
135             }
136           else
137             {
138               foreach (var e in i.email_addresses)
139                 {
140                   if (e.value == "bernie@example.org")
141                     {
142                       this._found_after_update = true;
143                       this._main_loop.quit ();
144                     }
145                 }
146             }
147         }
148     }
149 }
150
151 public int main (string[] args)
152 {
153   Test.init (ref args);
154
155   TestSuite root = TestSuite.get_root ();
156   root.add_suite (new SetEmailsTests ().get_suite ());
157
158   Test.run ();
159
160   return 0;
161 }