Bug 650414 — Need better APIs to handle image data
[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 ();
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.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       (Set<Individual> added,
117        Set<Individual> removed,
118        string? message,
119        Persona? actor,
120        GroupDetails.ChangeReason reason)
121     {
122       foreach (var i in added)
123         {
124           if (i.full_name == this._initial_fullname)
125             {
126               i.notify["avatar"].connect (this._notify_avatar_cb);
127               this._individual_id = i.id;
128
129               var initial_avatar =
130                   new FileIcon (File.new_for_uri (this._initial_avatar_uri));
131
132               if (i.avatar != null && i.avatar.equal (initial_avatar) == true)
133                 {
134                   this._initial_avatar_found = true;
135
136                   this._tracker_backend.remove_triplet (this._contact_urn,
137                       Trf.OntologyDefs.NCO_PHOTO, this._photo_urn);
138
139                   string photo_urn_2 = "<" + this._updated_avatar_uri;
140                   photo_urn_2 += ">";
141                   this._tracker_backend.insert_triplet (photo_urn_2,
142                       "a", "nfo:Image, nie:DataObject",
143                       Trf.OntologyDefs.NIE_URL,
144                       this._updated_avatar_uri);
145
146                   this._tracker_backend.insert_triplet
147                       (this._contact_urn,
148                       Trf.OntologyDefs.NCO_PHOTO, photo_urn_2);
149
150                 }
151             }
152
153           assert (removed.size == 0);
154         }
155     }
156
157   private void _notify_avatar_cb ()
158     {
159       var i = this._aggregator.individuals.get (this._individual_id);
160       if (i == null)
161         return;
162
163       if (i.avatar != null &&
164           i.avatar.equal (this._updated_avatar))
165         {
166           this._main_loop.quit ();
167           this._updated_avatar_found = true;
168         }
169     }
170 }
171
172 public int main (string[] args)
173 {
174   Test.init (ref args);
175
176   TestSuite root = TestSuite.get_root ();
177   root.add_suite (new AvatarUpdatesTests ().get_suite ());
178
179   Test.run ();
180
181   return 0;
182 }