Simplify tests that can time out
[platform/upstream/folks.git] / tests / tracker / 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 Tracker.Sparql;
22 using TrackerTest;
23 using Folks;
24 using Gee;
25
26 public class SetEmailsTests : TrackerTest.TestCase
27 {
28   private GLib.MainLoop _main_loop;
29   private IndividualAggregator _aggregator;
30   private string _persona_fullname;
31   private string _email_1;
32   private string _email_2;
33   private bool _email_1_found;
34   private bool _email_2_found;
35
36   public SetEmailsTests ()
37     {
38       base ("SetEmailsTests");
39
40       this.add_test ("test setting emails ", this.test_set_emails);
41     }
42
43   public void test_set_emails ()
44     {
45       this._main_loop = new GLib.MainLoop (null, false);
46       Gee.HashMap<string, string> c1 = new Gee.HashMap<string, string> ();
47       this._persona_fullname = "persona #1";
48       this._email_1 = "email-1@example.org";
49       this._email_2 = "email-2@example.org";
50
51       c1.set (Trf.OntologyDefs.NCO_FULLNAME, this._persona_fullname);
52       ((!) this.tracker_backend).add_contact (c1);
53
54       ((!) this.tracker_backend).set_up ();
55
56       this._email_1_found = false;
57       this._email_2_found = false;
58
59       this._test_set_emails_async.begin ();
60
61       TestUtils.loop_run_with_timeout (this._main_loop);
62
63       assert (this._email_1_found);
64       assert (this._email_2_found);
65     }
66
67   private async void _test_set_emails_async ()
68     {
69       var store = BackendStore.dup ();
70       yield store.prepare ();
71       this._aggregator = new IndividualAggregator ();
72       this._aggregator.individuals_changed_detailed.connect
73           (this._individuals_changed_cb);
74       try
75         {
76           yield this._aggregator.prepare ();
77         }
78       catch (GLib.Error e)
79         {
80           GLib.warning ("Error when calling prepare: %s\n", e.message);
81         }
82     }
83
84   private void _individuals_changed_cb (
85        MultiMap<Individual?, Individual?> changes)
86     {
87       var added = changes.get_values ();
88       var removed = changes.get_keys ();
89
90       foreach (var i in added)
91         {
92           assert (i != null);
93
94           if (i.full_name == this._persona_fullname)
95             {
96               i.notify["email-addresses"].connect (this._notify_emails_cb);
97
98               var emails = new HashSet<EmailFieldDetails> (
99                   AbstractFieldDetails<string>.hash_static,
100                   AbstractFieldDetails<string>.equal_static);
101               var p1 = new EmailFieldDetails (this._email_1);
102               emails.add (p1);
103               var p2 = new EmailFieldDetails (this._email_2);
104               emails.add (p2);
105
106               foreach (var p in i.personas)
107                 {
108                   ((EmailDetails) p).email_addresses = emails;
109                 }
110             }
111         }
112
113       assert (removed.size == 1);
114
115       foreach (var i in removed)
116         {
117           assert (i == null);
118         }
119     }
120
121   private void _notify_emails_cb (Object individual_obj, ParamSpec ps)
122     {
123       Folks.Individual i = (Folks.Individual) individual_obj;
124       if (i.full_name == this._persona_fullname)
125         {
126           foreach (var p in i.email_addresses)
127             {
128               if (p.value == this._email_1)
129                 this._email_1_found = true;
130               else if (p.value == this._email_2)
131                 this._email_2_found = true;
132             }
133         }
134
135       if (this._email_1_found && this._email_2_found)
136         {
137           this._main_loop.quit ();
138         }
139     }
140 }
141
142 public int main (string[] args)
143 {
144   Test.init (ref args);
145
146   var tests = new SetEmailsTests ();
147   tests.register ();
148   Test.run ();
149   tests.final_tear_down ();
150
151   return 0;
152 }