29ed2965b2ad383088947974cbb1a9f07f4d4822
[platform/upstream/folks.git] / tests / eds / set-urls.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 SetUrlsTests : EdsTest.TestCase
26 {
27   private IndividualAggregator _aggregator;
28   private GLib.MainLoop _main_loop;
29   private bool _found_before_update;
30   private bool _found_url_extra_1;
31   private bool _found_url_extra_2;
32   private bool _found_url_home;
33   private bool _found_url_blog;
34   private string _url_extra_1 = "http://example.org";
35   private string _url_extra_2 = "http://extra.example.org";
36   private string _url_home = "http://home.example.org";
37   private string _url_blog = "http://blog.example.org";
38
39
40   public SetUrlsTests ()
41     {
42       base ("SetUrls");
43
44       this.add_test ("setting urls on e-d-s persona",
45           this.test_set_urls);
46     }
47
48   void test_set_urls ()
49     {
50       Gee.HashMap<string, Value?> c1 = new Gee.HashMap<string, Value?> ();
51       this._main_loop = new GLib.MainLoop (null, false);
52       Value? v;
53
54       this._found_before_update = false;
55       this._found_url_extra_1 = false;
56       this._found_url_extra_2 = false;
57       this._found_url_home = false;
58       this._found_url_blog = false;
59
60       this.eds_backend.reset ();
61
62       v = Value (typeof (string));
63       v.set_string ("Albus Percival Wulfric Brian Dumbledore");
64       c1.set ("full_name", (owned) v);
65       this.eds_backend.add_contact (c1);
66
67       this._test_set_urls_async.begin ();
68
69       Timeout.add_seconds (5, () => {
70             this._main_loop.quit ();
71             assert_not_reached ();
72           });
73
74       this._main_loop.run ();
75
76       assert (this._found_before_update);
77       assert (this._found_url_extra_1);
78       assert (this._found_url_extra_2);
79       assert (this._found_url_home);
80       assert (this._found_url_blog);
81     }
82
83   private async void _test_set_urls_async ()
84     {
85       yield this.eds_backend.commit_contacts_to_addressbook ();
86
87       var store = BackendStore.dup ();
88       yield store.prepare ();
89       this._aggregator = new IndividualAggregator ();
90       this._aggregator.individuals_changed_detailed.connect
91           (this._individuals_changed_cb);
92       try
93         {
94           yield this._aggregator.prepare ();
95         }
96       catch (GLib.Error e)
97         {
98           GLib.warning ("Error when calling prepare: %s\n", e.message);
99         }
100     }
101
102   private void _individuals_changed_cb (
103        MultiMap<Individual?, Individual?> changes)
104     {
105       var added = changes.get_values ();
106       var removed = changes.get_keys ();
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 == "Albus Percival Wulfric Brian Dumbledore")
115             {
116               i.notify["urls"].connect (this._notify_urls_cb);
117               this._found_before_update = true;
118
119               foreach (var p in i.personas)
120                 {
121                   var urls = new HashSet<UrlFieldDetails> ();
122
123                   var p1 = new UrlFieldDetails (this._url_extra_1);
124                   urls.add (p1);
125                   var p2 = new UrlFieldDetails (this._url_extra_2);
126                   urls.add (p2);
127                   var p3 = new UrlFieldDetails (this._url_home);
128                   p3.set_parameter(AbstractFieldDetails.PARAM_TYPE,
129                       UrlFieldDetails.PARAM_TYPE_HOME_PAGE);
130                   urls.add (p3);
131                   var p4 = new UrlFieldDetails (this._url_blog);
132                   p4.set_parameter(AbstractFieldDetails.PARAM_TYPE,
133                       UrlFieldDetails.PARAM_TYPE_BLOG);
134                   urls.add (p4);
135
136                   ((UrlDetails) p).urls = urls;
137                 }
138             }
139         }
140
141       assert (removed.size == 1);
142
143       foreach (var i in removed)
144         {
145           assert (i == null);
146         }
147     }
148
149   private void _notify_urls_cb (Object individual_obj, ParamSpec ps)
150     {
151       Folks.Individual i = (Folks.Individual) individual_obj;
152       foreach (var url in i.urls)
153         {
154           if (url.value == this._url_extra_1)
155             {
156               this._found_url_extra_1 = true;
157             }
158           else if (url.value == this._url_extra_2)
159             {
160               this._found_url_extra_2 = true;
161             }
162           else if (url.value == this._url_home)
163             {
164               this._found_url_home = true;
165             }
166           else if (url.value == this._url_blog)
167             {
168               this._found_url_blog = true;
169             }
170         }
171
172       if (this._found_url_extra_1 &&
173           this._found_url_extra_2 &&
174           this._found_url_home &&
175           this._found_url_blog)
176         {
177           this._main_loop.quit ();
178         }
179     }
180 }
181
182 public int main (string[] args)
183 {
184   Test.init (ref args);
185
186   var tests = new SetUrlsTests ();
187   tests.register ();
188   Test.run ();
189   tests.final_tear_down ();
190
191   return 0;
192 }