Add subclasses for each category of TestCase
[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       Timeout.add_seconds (5, () =>
62         {
63           this._main_loop.quit ();
64           assert_not_reached ();
65         });
66
67       this._main_loop.run ();
68
69       assert (this._email_1_found);
70       assert (this._email_2_found);
71     }
72
73   private async void _test_set_emails_async ()
74     {
75       var store = BackendStore.dup ();
76       yield store.prepare ();
77       this._aggregator = new IndividualAggregator ();
78       this._aggregator.individuals_changed_detailed.connect
79           (this._individuals_changed_cb);
80       try
81         {
82           yield this._aggregator.prepare ();
83         }
84       catch (GLib.Error e)
85         {
86           GLib.warning ("Error when calling prepare: %s\n", e.message);
87         }
88     }
89
90   private void _individuals_changed_cb (
91        MultiMap<Individual?, Individual?> changes)
92     {
93       var added = changes.get_values ();
94       var removed = changes.get_keys ();
95
96       foreach (var i in added)
97         {
98           assert (i != null);
99
100           if (i.full_name == this._persona_fullname)
101             {
102               i.notify["email-addresses"].connect (this._notify_emails_cb);
103
104               var emails = new HashSet<EmailFieldDetails> (
105                   AbstractFieldDetails<string>.hash_static,
106                   AbstractFieldDetails<string>.equal_static);
107               var p1 = new EmailFieldDetails (this._email_1);
108               emails.add (p1);
109               var p2 = new EmailFieldDetails (this._email_2);
110               emails.add (p2);
111
112               foreach (var p in i.personas)
113                 {
114                   ((EmailDetails) p).email_addresses = emails;
115                 }
116             }
117         }
118
119       assert (removed.size == 1);
120
121       foreach (var i in removed)
122         {
123           assert (i == null);
124         }
125     }
126
127   private void _notify_emails_cb (Object individual_obj, ParamSpec ps)
128     {
129       Folks.Individual i = (Folks.Individual) individual_obj;
130       if (i.full_name == this._persona_fullname)
131         {
132           foreach (var p in i.email_addresses)
133             {
134               if (p.value == this._email_1)
135                 this._email_1_found = true;
136               else if (p.value == this._email_2)
137                 this._email_2_found = true;
138             }
139         }
140
141       if (this._email_1_found && this._email_2_found)
142         {
143           this._main_loop.quit ();
144         }
145     }
146 }
147
148 public int main (string[] args)
149 {
150   Test.init (ref args);
151
152   TestSuite root = TestSuite.get_root ();
153   root.add_suite (new SetEmailsTests ().get_suite ());
154
155   Test.run ();
156
157   return 0;
158 }