e-d-s: update set-im-addresses test
[platform/upstream/folks.git] / tests / eds / set-im-addresses.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 SetIMAddressesTests : 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 SetIMAddressesTests ()
34     {
35       base ("SetIMAddresses");
36
37       this._eds_backend = new EdsTest.Backend ();
38
39       this.add_test ("setting im addresses on e-d-s persona",
40           this.test_set_im_addresses);
41     }
42
43   public override void set_up ()
44     {
45       this._eds_backend.set_up ();
46     }
47
48   public override void tear_down ()
49     {
50       this._eds_backend.tear_down ();
51     }
52
53   void test_set_im_addresses ()
54     {
55       Gee.HashMap<string, Value?> c1 = new Gee.HashMap<string, Value?> ();
56       this._main_loop = new GLib.MainLoop (null, false);
57       Value? v;
58
59       this._found_before_update = false;
60       this._found_after_update = false;
61
62       this._eds_backend.reset ();
63
64       v = Value (typeof (string));
65       v.set_string ("bernie h. innocenti");
66       c1.set ("full_name", (owned) v);
67       this._eds_backend.add_contact (c1);
68
69       this._test_set_im_addresses_async ();
70
71       Timeout.add_seconds (5, () => {
72             this._main_loop.quit ();
73             assert_not_reached ();
74           });
75
76       this._main_loop.run ();
77
78       assert (this._found_before_update);
79       assert (this._found_after_update);
80     }
81
82   private async void _test_set_im_addresses_async ()
83     {
84       yield this._eds_backend.commit_contacts_to_addressbook ();
85
86       var store = BackendStore.dup ();
87       yield store.prepare ();
88       this._aggregator = new IndividualAggregator ();
89       this._aggregator.individuals_changed_detailed.connect
90           (this._individuals_changed_cb);
91       try
92         {
93           yield this._aggregator.prepare ();
94         }
95       catch (GLib.Error e)
96         {
97           GLib.warning ("Error when calling prepare: %s\n", e.message);
98         }
99     }
100
101   private void _individuals_changed_cb (
102        MultiMap<Individual?, Individual?> changes)
103     {
104       var added = changes.get_values ();
105
106       foreach (Individual i in added)
107         {
108           assert (i != null);
109
110           var name = (Folks.NameDetails) i;
111
112           if (name.full_name != "bernie h. innocenti")
113             continue;
114
115           /* Because we update a linkable property, we'll
116              get a new individual because re-linking has to
117              happen.
118           */
119           if (!this._found_before_update)
120             {
121               this._found_before_update = true;
122
123               foreach (var p in i.personas)
124                 {
125                   var im_addrs = new HashMultiMap<string, ImFieldDetails> (
126                       null, null,
127                       (GLib.HashFunc) ImFieldDetails.hash,
128                       (GLib.EqualFunc) ImFieldDetails.equal);
129                   im_addrs.set ("jabber",
130                       new ImFieldDetails ("bernie@example.org"));
131                   ((ImDetails) p).im_addresses = im_addrs;
132                 }
133             }
134           else
135             {
136               foreach (var proto in i.im_addresses.get_keys ())
137                 {
138                   foreach (var im_fd in i.im_addresses.get (proto))
139                     {
140                       if (im_fd.equal (
141                               new ImFieldDetails ("bernie@example.org")))
142                         {
143                           this._found_after_update = true;
144                           this._main_loop.quit ();
145                         }
146                     }
147                 }
148             }
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 SetIMAddressesTests ().get_suite ());
159
160   Test.run ();
161
162   return 0;
163 }