Always append to AM_VALAFLAGS, don't replace
[platform/upstream/folks.git] / tests / tracker / avatar-updates.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 AvatarUpdatesTests : Folks.TestCase
27 {
28   private TrackerTest.Backend _tracker_backend;
29   private IndividualAggregator _aggregator;
30   private bool _updated_avatar_found;
31   private string _updated_avatar_uri;
32   private LoadableIcon _updated_avatar;
33   private string _individual_id;
34   private GLib.MainLoop _main_loop;
35   private bool _initial_avatar_found;
36   private string _initial_fullname;
37   private string _initial_avatar_uri;
38   private string _contact_urn;
39   private string _photo_urn;
40
41   public AvatarUpdatesTests ()
42     {
43       base ("AvatarUpdates");
44
45       this._tracker_backend = new TrackerTest.Backend ();
46       this._tracker_backend.debug = false;
47
48       this.add_test ("avatar updates", this.test_avatar_updates);
49     }
50
51   public override void set_up ()
52     {
53     }
54
55   public override void tear_down ()
56     {
57     }
58
59   public void test_avatar_updates ()
60     {
61       this._main_loop = new GLib.MainLoop (null, false);
62       Gee.HashMap<string, string> c1 = new Gee.HashMap<string, string> ();
63       this._initial_fullname = "persona #1";
64       this._initial_avatar_uri = "file:///tmp/avatar-01";
65       this._contact_urn = "<urn:contact001>";
66       this._photo_urn = "<" + this._initial_avatar_uri + ">";
67       this._updated_avatar_uri = "file:///tmp/avatar-02";
68       this._updated_avatar =
69           new FileIcon (File.new_for_uri (this._updated_avatar_uri));
70
71       c1.set (TrackerTest.Backend.URN, this._contact_urn);
72       c1.set (Trf.OntologyDefs.NCO_FULLNAME, this._initial_fullname);
73       c1.set (Trf.OntologyDefs.NCO_PHOTO, this._initial_avatar_uri);
74       this._tracker_backend.add_contact (c1);
75
76       this._tracker_backend.set_up ();
77
78       this._initial_avatar_found = false;
79       this._updated_avatar_found = false;
80       this._individual_id = "";
81
82       test_avatar_updates_async.begin ();
83
84       Timeout.add_seconds (5, () =>
85         {
86           this._main_loop.quit ();
87           assert_not_reached ();
88         });
89
90       this._main_loop.run ();
91
92       assert (this._initial_avatar_found == true);
93       assert (this._updated_avatar_found == true);
94
95       this._tracker_backend.tear_down ();
96     }
97
98   private async void test_avatar_updates_async ()
99     {
100       var store = BackendStore.dup ();
101       yield store.prepare ();
102       this._aggregator = new IndividualAggregator ();
103       this._aggregator.individuals_changed_detailed.connect
104           (this._individuals_changed_cb);
105       try
106         {
107           yield this._aggregator.prepare ();
108         }
109       catch (GLib.Error e)
110         {
111           GLib.warning ("Error when calling prepare: %s\n", e.message);
112         }
113     }
114
115   private void _individuals_changed_cb (
116        MultiMap<Individual?, Individual?> changes)
117     {
118       var added = changes.get_values ();
119       var removed = changes.get_keys ();
120
121       foreach (var i in added)
122         {
123           assert (i != null);
124
125           if (i.full_name == this._initial_fullname)
126             {
127               i.notify["avatar"].connect (this._notify_avatar_cb);
128               this._individual_id = i.id;
129
130               var initial_avatar =
131                   new FileIcon (File.new_for_uri (this._initial_avatar_uri));
132
133               if (i.avatar != null && i.avatar.equal (initial_avatar) == true)
134                 {
135                   this._initial_avatar_found = true;
136
137                   this._tracker_backend.remove_triplet (this._contact_urn,
138                       Trf.OntologyDefs.NCO_PHOTO, this._photo_urn);
139
140                   string photo_urn_2 = "<" + this._updated_avatar_uri;
141                   photo_urn_2 += ">";
142                   this._tracker_backend.insert_triplet (photo_urn_2,
143                       "a", "nfo:Image, nie:DataObject",
144                       Trf.OntologyDefs.NIE_URL,
145                       this._updated_avatar_uri);
146
147                   this._tracker_backend.insert_triplet
148                       (this._contact_urn,
149                       Trf.OntologyDefs.NCO_PHOTO, photo_urn_2);
150
151                 }
152             }
153         }
154
155       assert (removed.size == 1);
156
157       foreach (var i in removed)
158         {
159           assert (i == null);
160         }
161     }
162
163   private void _notify_avatar_cb ()
164     {
165       var i = this._aggregator.individuals.get (this._individual_id);
166       if (i == null)
167         return;
168
169       if (i.avatar != null &&
170           i.avatar.equal (this._updated_avatar))
171         {
172           this._main_loop.quit ();
173           this._updated_avatar_found = true;
174         }
175     }
176 }
177
178 public int main (string[] args)
179 {
180   Test.init (ref args);
181
182   TestSuite root = TestSuite.get_root ();
183   root.add_suite (new AvatarUpdatesTests ().get_suite ());
184
185   Test.run ();
186
187   return 0;
188 }