use IndividualAggregator.dup () in tests and tools
[platform/upstream/folks.git] / tests / tracker / url-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 UrlDetailsInterfaceTests : TrackerTest.TestCase
27 {
28   private GLib.MainLoop _main_loop;
29   private IndividualAggregator _aggregator;
30   private string _blog_url;
31   private string _website_url;
32   private string _urls;
33   private bool _found_blog;
34   private bool _found_website;
35
36   public UrlDetailsInterfaceTests ()
37     {
38       base ("UrlDetailsInterfaceTests");
39
40       ((!) this.tracker_backend).debug = false;
41
42       this.add_test ("test url details interface",
43           this.test_url_details_interface);
44     }
45
46   public void test_url_details_interface ()
47     {
48       this._main_loop = new GLib.MainLoop (null, false);
49       Gee.HashMap<string, string> c1 = new Gee.HashMap<string, string> ();
50       this._blog_url = "http://blog.example.org";
51       this._website_url = "http://www.example.org";
52       this._urls = "%s,%s".printf (this._blog_url, this._website_url);
53
54       c1.set (Trf.OntologyDefs.NCO_FULLNAME, "persona #1");
55       c1.set (TrackerTest.Backend.URLS, this._urls);
56       ((!) this.tracker_backend).add_contact (c1);
57
58       ((!) this.tracker_backend).set_up ();
59
60       this._found_blog = false;
61       this._found_website = false;
62
63       this._test_url_details_interface_async.begin ();
64
65       TestUtils.loop_run_with_timeout (this._main_loop);
66
67       assert (this._found_blog == true);
68       assert (this._found_website == true);
69     }
70
71   private async void _test_url_details_interface_async ()
72     {
73       var store = BackendStore.dup ();
74       yield store.prepare ();
75       this._aggregator = IndividualAggregator.dup ();
76       this._aggregator.individuals_changed_detailed.connect
77           (this._individuals_changed_cb);
78       try
79         {
80           yield this._aggregator.prepare ();
81         }
82       catch (GLib.Error e)
83         {
84           GLib.warning ("Error when calling prepare: %s\n", e.message);
85         }
86     }
87
88   private void _individuals_changed_cb (
89        MultiMap<Individual?, Individual?> changes)
90     {
91       var added = changes.get_values ();
92       var removed = changes.get_keys ();
93
94       foreach (var i in added)
95         {
96           assert (i != null);
97
98           string full_name = i.full_name;
99           if (full_name != null)
100             {
101               foreach (var url in i.urls)
102                 {
103                   if (url.value == this._blog_url)
104                     {
105                       this._found_blog = true;
106                     }
107                   else if (url.value == this._website_url)
108                     {
109                       this._found_website = true;
110                     }
111                 }
112             }
113         }
114
115       if (this._found_blog &&
116           this._found_website)
117         this._main_loop.quit ();
118
119       assert (removed.size == 1);
120
121       foreach (var i in removed)
122         {
123           assert (i == null);
124         }
125     }
126 }
127
128 public int main (string[] args)
129 {
130   Test.init (ref args);
131
132   var tests = new UrlDetailsInterfaceTests ();
133   tests.register ();
134   Test.run ();
135   tests.final_tear_down ();
136
137   return 0;
138 }