533c2cafb607e24680c30df143260173b7642c04
[platform/upstream/folks.git] / tests / tracker / set-avatar.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 SetAvatarTests : Folks.TestCase
27 {
28   private GLib.MainLoop _main_loop;
29   private TrackerTest.Backend _tracker_backend;
30   private IndividualAggregator _aggregator;
31   private string _persona_fullname;
32   private string _avatar_uri;
33   private File _avatar;
34   private bool _avatar_found;
35
36   public SetAvatarTests ()
37     {
38       base ("SetAvatarTests");
39
40       this._tracker_backend = new TrackerTest.Backend ();
41
42       this.add_test ("test setting avatar ", this.test_set_avatar);
43     }
44
45   public override void set_up ()
46     {
47     }
48
49   public override void tear_down ()
50     {
51     }
52
53   public void test_set_avatar ()
54     {
55       this._main_loop = new GLib.MainLoop (null, false);
56       Gee.HashMap<string, string> c1 = new Gee.HashMap<string, string> ();
57       this._persona_fullname = "persona #1";
58       this._avatar_uri = "file:///tmp/some-avatar.jpg";
59       this._avatar = File.new_for_uri (this._avatar_uri);
60
61       c1.set (Trf.OntologyDefs.NCO_FULLNAME, this._persona_fullname);
62       this._tracker_backend.add_contact (c1);
63
64       this._tracker_backend.set_up ();
65
66       this._avatar_found = false;
67
68       this._test_set_avatar_async ();
69
70       Timeout.add_seconds (5, () =>
71         {
72           this._main_loop.quit ();
73           assert_not_reached ();
74         });
75
76       this._main_loop.run ();
77
78       assert (this._avatar_found);
79
80      this._tracker_backend.tear_down ();
81     }
82
83   private async void _test_set_avatar_async ()
84     {
85       var store = BackendStore.dup ();
86       yield store.prepare ();
87       this._aggregator = new IndividualAggregator ();
88       this._aggregator.individuals_changed.connect
89           (this._individuals_changed_cb);
90       try
91         {
92           yield this._aggregator.prepare ();
93         }
94       catch (GLib.Error e)
95         {
96           GLib.warning ("Error when calling prepare: %s\n", e.message);
97         }
98     }
99
100   private void _individuals_changed_cb
101       (Set<Individual> added,
102        Set<Individual> removed,
103        string? message,
104        Persona? actor,
105        GroupDetails.ChangeReason reason)
106     {
107       foreach (var i in added)
108         {
109           if (i.full_name == this._persona_fullname)
110             {
111               i.notify["avatar"].connect (this._notify_avatar_cb);
112
113               foreach (var p in i.personas)
114                 {
115                   ((AvatarDetails) p).avatar = this._avatar;
116                 }
117             }
118         }
119
120       assert (removed.size == 0);
121     }
122
123   private void _notify_avatar_cb (Object individual_obj, ParamSpec ps)
124     {
125       Folks.Individual i = (Folks.Individual) individual_obj;
126       if (i.full_name == this._persona_fullname)
127         {
128           if (i.avatar.get_uri () == this._avatar_uri)
129             {
130               this._avatar_found = true;
131               this._main_loop.quit ();
132             }
133         }
134    }
135 }
136
137 public int main (string[] args)
138 {
139   Test.init (ref args);
140
141   TestSuite root = TestSuite.get_root ();
142   root.add_suite (new SetAvatarTests ().get_suite ());
143
144   Test.run ();
145
146   return 0;
147 }