build fix: Only depend on Gee 0.8.3, since 0.8.4 isn't released yet
[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.add_test ("setting im addresses on e-d-s persona",
38           this.test_set_im_addresses);
39     }
40
41   public override void set_up ()
42     {
43       this._eds_backend = new EdsTest.Backend ();
44       this._eds_backend.set_up ();
45
46       /* We configure eds as the primary store */
47       var config_val = "eds:%s".printf (this._eds_backend.address_book_uid);
48       Environment.set_variable ("FOLKS_PRIMARY_STORE", config_val, true);
49     }
50
51   public override void tear_down ()
52     {
53       this._eds_backend.tear_down ();
54     }
55
56   void test_set_im_addresses ()
57     {
58       Gee.HashMap<string, Value?> c1 = new Gee.HashMap<string, Value?> ();
59       this._main_loop = new GLib.MainLoop (null, false);
60       Value? v;
61
62       this._found_before_update = false;
63       this._found_after_update = false;
64
65       this._eds_backend.reset ();
66
67       v = Value (typeof (string));
68       v.set_string ("bernie h. innocenti");
69       c1.set ("full_name", (owned) v);
70       this._eds_backend.add_contact (c1);
71
72       this._test_set_im_addresses_async.begin ();
73
74       Timeout.add_seconds (5, () => {
75             this._main_loop.quit ();
76             assert_not_reached ();
77           });
78
79       this._main_loop.run ();
80
81       assert (this._found_before_update);
82       assert (this._found_after_update);
83     }
84
85   private async void _test_set_im_addresses_async ()
86     {
87       yield this._eds_backend.commit_contacts_to_addressbook ();
88
89       var store = BackendStore.dup ();
90       yield store.prepare ();
91       this._aggregator = new IndividualAggregator ();
92       this._aggregator.individuals_changed_detailed.connect
93           (this._individuals_changed_cb);
94       try
95         {
96           yield this._aggregator.prepare ();
97         }
98       catch (GLib.Error e)
99         {
100           GLib.warning ("Error when calling prepare: %s\n", e.message);
101         }
102     }
103
104   private void _individuals_changed_cb (
105        MultiMap<Individual?, Individual?> changes)
106     {
107       var added = changes.get_values ();
108
109       foreach (Individual i in added)
110         {
111           assert (i != null);
112
113           var name = (Folks.NameDetails) i;
114
115           if (name.full_name != "bernie h. innocenti")
116             continue;
117
118           /* Because we update a linkable property, we'll
119              get a new individual because re-linking has to
120              happen.
121           */
122           if (!this._found_before_update)
123             {
124               this._found_before_update = true;
125
126               foreach (var p in i.personas)
127                 {
128                   var im_addrs = new HashMultiMap<string, ImFieldDetails> (
129                       null, null,
130                       (Gee.HashDataFunc)
131                           AbstractFieldDetails<string>.hash_static,
132                       (Gee.EqualDataFunc)
133                           AbstractFieldDetails<string>.equal_static);
134                   im_addrs.set ("jabber",
135                       new ImFieldDetails ("bernie@example.org"));
136                   ((ImDetails) p).im_addresses = im_addrs;
137                 }
138             }
139           else
140             {
141               foreach (var proto in i.im_addresses.get_keys ())
142                 {
143                   foreach (var im_fd in i.im_addresses.get (proto))
144                     {
145                       if (im_fd.equal (
146                               new ImFieldDetails ("bernie@example.org")))
147                         {
148                           this._found_after_update = true;
149                           this._main_loop.quit ();
150                         }
151                     }
152                 }
153             }
154         }
155     }
156 }
157
158 public int main (string[] args)
159 {
160   Test.init (ref args);
161
162   TestSuite root = TestSuite.get_root ();
163   root.add_suite (new SetIMAddressesTests ().get_suite ());
164
165   Test.run ();
166
167   return 0;
168 }