0ecdceb1a8fb88140c1f900d57c114f23d4a1173
[platform/upstream/folks.git] / tests / key-file / individual-retrieval.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: Travis Reitter <travis.reitter@collabora.co.uk>
18  */
19
20 using Gee;
21 using Folks;
22 using KfTest;
23
24 public class IndividualRetrievalTests : Folks.TestCase
25 {
26   private KfTest.Backend kf_backend;
27   private int _test_timeout = 3;
28
29   public IndividualRetrievalTests ()
30     {
31       base ("IndividualRetrieval");
32
33       this.kf_backend = new KfTest.Backend ();
34
35       this.add_test ("singleton individuals", this.test_singleton_individuals);
36       this.add_test ("aliases", this.test_aliases);
37
38       if (Environment.get_variable ("FOLKS_TEST_VALGRIND") != null)
39           this._test_timeout = 10;
40     }
41
42   public override void set_up ()
43     {
44     }
45
46   public override void tear_down ()
47     {
48     }
49
50   public void test_singleton_individuals ()
51     {
52       var main_loop = new GLib.MainLoop (null, false);
53       this.kf_backend.set_up (
54           "[0]\n" +
55           "msn=foo@hotmail.com\n" +
56           "[1]\n" +
57           "__alias=Bar McBadgerson\n" +
58           "jabber=bar@jabber.org\n");
59
60       /* Create a set of the individuals we expect to see */
61       HashSet<string> expected_individuals = new HashSet<string> ();
62
63       expected_individuals.add ("0");
64       expected_individuals.add ("1");
65
66       /* Set up the aggregator */
67       var aggregator = new IndividualAggregator ();
68       aggregator.individuals_changed_detailed.connect ((changes) =>
69         {
70           var added = changes.get_values ();
71           var removed = changes.get_keys ();
72
73           foreach (Individual i in added)
74             {
75               assert (i != null);
76               assert (i.personas.size == 1);
77
78               /* Using the display ID is a little hacky, since we strictly
79                * shouldn't assume anything about…but for the key-file backend,
80                * we know it's equal to the group name. */
81               foreach (var persona in i.personas)
82                 {
83                   expected_individuals.remove (persona.display_id);
84                 }
85             }
86
87           assert (removed.size == 1);
88
89           foreach (var i in removed)
90             {
91               assert (i == null);
92             }
93         });
94       aggregator.prepare ();
95
96       /* Kill the main loop after a few seconds. If there are still individuals
97        * in the set of expected individuals, the aggregator has either failed
98        * or been too slow (which we can consider to be failure). */
99       Timeout.add_seconds (this._test_timeout, () =>
100         {
101           main_loop.quit ();
102           return false;
103         });
104
105       main_loop.run ();
106
107       /* We should have enumerated exactly the individuals in the set */
108       assert (expected_individuals.size == 0);
109
110       this.kf_backend.tear_down ();
111     }
112
113   public void test_aliases ()
114     {
115       var main_loop = new GLib.MainLoop (null, false);
116       this.kf_backend.set_up (
117           "[0]\n" +
118           "__alias=Brian Briansson\n" +
119           "msn=foo@hotmail.com\n");
120
121       /* Set up the aggregator */
122       var aggregator = new IndividualAggregator ();
123       uint individuals_changed_count = 0;
124       aggregator.individuals_changed_detailed.connect ((changes) =>
125         {
126           var added = changes.get_values ();
127           var removed = changes.get_keys ();
128
129           individuals_changed_count++;
130
131           assert (added.size == 1);
132           assert (removed.size == 1);
133
134           /* Check properties */
135           foreach (var i in added)
136             {
137               assert (i.alias == "Brian Briansson");
138             }
139
140           foreach (var i in removed)
141             {
142               assert (i == null);
143             }
144         });
145       aggregator.prepare ();
146
147       /* Kill the main loop after a few seconds. If there are still individuals
148        * in the set of expected individuals, the aggregator has either failed
149        * or been too slow (which we can consider to be failure). */
150       Timeout.add_seconds (this._test_timeout, () =>
151         {
152           main_loop.quit ();
153           return false;
154         });
155
156       main_loop.run ();
157
158       /* We should have enumerated exactly one individual */
159       assert (individuals_changed_count == 1);
160
161       this.kf_backend.tear_down ();
162     }
163 }
164
165 public int main (string[] args)
166 {
167   Test.init (ref args);
168
169   TestSuite root = TestSuite.get_root ();
170   root.add_suite (new IndividualRetrievalTests ().get_suite ());
171
172   Test.run ();
173
174   return 0;
175 }