use IndividualAggregator.dup () in tests and tools
[platform/upstream/folks.git] / tests / tracker / email-details-interface.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 EmailDetailsInterfaceTests : TrackerTest.TestCase
27 {
28   private GLib.MainLoop _main_loop;
29   private IndividualAggregator _aggregator;
30   private int _num_emails;
31   private bool _found_email_1;
32   private bool _found_email_2;
33
34   public EmailDetailsInterfaceTests ()
35     {
36       base ("EmailDetailsInterfaceTests");
37
38       this.add_test ("test email details interface",
39           this.test_email_details_interface);
40     }
41
42   public void test_email_details_interface ()
43     {
44       this._main_loop = new GLib.MainLoop (null, false);
45       Gee.HashMap<string, string> c1 = new Gee.HashMap<string, string> ();
46
47       c1.set (Trf.OntologyDefs.NCO_FULLNAME, "persona #1");
48       c1.set (Trf.OntologyDefs.NCO_EMAIL_PROP,
49           "test1@example.org,test2@example.org");
50       ((!) this.tracker_backend).add_contact (c1);
51
52       ((!) this.tracker_backend).set_up ();
53
54       this._num_emails = 0;
55       this._found_email_1 = false;
56       this._found_email_2 = false;
57
58       this._test_email_details_interface_async.begin ();
59
60       TestUtils.loop_run_with_timeout (this._main_loop);
61
62       assert (this._num_emails == 2);
63       assert (this._found_email_1 == true);
64       assert (this._found_email_2 == true);
65     }
66
67   private async void _test_email_details_interface_async ()
68     {
69       var store = BackendStore.dup ();
70       yield store.prepare ();
71       this._aggregator = IndividualAggregator.dup ();
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           string full_name = i.full_name;
95           if (full_name != null)
96             {
97               foreach (var email in i.email_addresses)
98                 {
99                   if (email.value == "test1@example.org")
100                     {
101                       this._found_email_1 = true;
102                       this._num_emails++;
103                     }
104                   else if (email.value == "test2@example.org")
105                     {
106                       this._found_email_2 = true;
107                       this._num_emails++;
108                     }
109                 }
110             }
111         }
112
113         if (this._found_email_1 &&
114             this._found_email_2 &&
115             this._num_emails == 2)
116           this._main_loop.quit ();
117
118       assert (removed.size == 1);
119
120       foreach (var i in removed)
121         {
122           assert (i == null);
123         }
124     }
125 }
126
127 public int main (string[] args)
128 {
129   Test.init (ref args);
130
131   var tests = new EmailDetailsInterfaceTests ();
132   tests.register ();
133   Test.run ();
134   tests.final_tear_down ();
135
136   return 0;
137 }