77c92f5d1becb9a3b1a85d61221c4cc6e8ff166f
[platform/upstream/folks.git] / tests / tracker / avatar-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 AvatarDetailsInterfaceTests : Folks.TestCase
27 {
28   private TrackerTest.Backend _tracker_backend;
29   private string _avatar_uri;
30   private bool _avatars_are_equal;
31   private GLib.MainLoop _main_loop;
32   IndividualAggregator _aggregator;
33
34   public AvatarDetailsInterfaceTests ()
35     {
36       base ("AvatarDetailsInterfaceTests");
37
38       this._tracker_backend = new TrackerTest.Backend ();
39
40       this.add_test ("test avatar details interface",
41           this.test_avatar_details_interface);
42     }
43
44   public override void set_up ()
45     {
46     }
47
48   public override void tear_down ()
49     {
50     }
51
52   public void test_avatar_details_interface ()
53     {
54       this._main_loop = new GLib.MainLoop (null, false);
55       Gee.HashMap<string, string> c1 = new Gee.HashMap<string, string> ();
56       string avatar_path = Environment.get_variable ("AVATAR_FILE_PATH");
57       var temp_file = GLib.File.new_for_path (avatar_path);
58       var full_avatar_path = temp_file.get_path ();
59       this._avatar_uri = "file://" + full_avatar_path;
60       this._avatars_are_equal = false;
61
62       c1.set (Trf.OntologyDefs.NCO_FULLNAME, "persona #1");
63       c1.set (Trf.OntologyDefs.NCO_PHOTO, this._avatar_uri);
64       this._tracker_backend.add_contact (c1);
65       this._tracker_backend.set_up ();
66
67       test_avatar_details_interface_async ();
68
69       Timeout.add_seconds (5, () =>
70         {
71           this._main_loop.quit ();
72           assert_not_reached ();
73         });
74
75       this._main_loop.run ();
76       assert (this._avatars_are_equal);
77       this._tracker_backend.tear_down ();
78     }
79
80   private async void test_avatar_details_interface_async ()
81     {
82       var store = BackendStore.dup ();
83       yield store.prepare ();
84
85       /* Set up the aggregator */
86       this._aggregator = new IndividualAggregator ();
87       this._aggregator.individuals_changed.connect
88           (this._individuals_changed_cb);
89       try
90         {
91           yield this._aggregator.prepare ();
92         }
93       catch (GLib.Error e)
94         {
95           GLib.warning ("Error when calling prepare: %s\n", e.message);
96         }
97     }
98
99   private void _individuals_changed_cb
100       (Set<Individual> added,
101        Set<Individual> removed,
102        string? message,
103        Persona? actor,
104        GroupDetails.ChangeReason reason)
105     {
106       foreach (var i in added)
107         {
108           string full_name = ((Folks.NameDetails) i).full_name;
109           if (full_name != null)
110             {
111               i.notify["avatar"].connect (this._notify_avatar_cb);
112               if (i.avatar != null)
113                 {
114                   var src_avatar = File.new_for_uri (this._avatar_uri);
115                   this._avatars_are_equal =
116                       this._compare_files (src_avatar, i.avatar);
117                   this._main_loop.quit ();
118                 }
119             }
120         }
121     }
122
123   private void _notify_avatar_cb (Object individual_obj, ParamSpec ps)
124     {
125       Folks.Individual individual = (Folks.Individual) individual_obj;
126       var src_avatar = File.new_for_uri (this._avatar_uri);
127       this._avatars_are_equal = this._compare_files (src_avatar,
128           individual.avatar);
129       this._main_loop.quit ();
130     }
131
132   private bool _compare_files (File a, File b)
133     {
134       uint8 *content_a;
135       uint8 *content_b;
136
137       try
138         {
139           a.load_contents (null, out content_a);
140         }
141       catch (GLib.Error e)
142         {
143           GLib.warning ("couldn't load file a");
144         }
145
146       try
147         {
148           b.load_contents (null, out content_b);
149         }
150       catch (GLib.Error e)
151         {
152           GLib.warning ("couldn't load file b");
153         }
154
155       return ((string) content_a) == ((string) content_b);
156     }
157 }
158
159 public int main (string[] args)
160 {
161   Test.init (ref args);
162
163   TestSuite root = TestSuite.get_root ();
164   root.add_suite (new AvatarDetailsInterfaceTests ().get_suite ());
165
166   Test.run ();
167
168   return 0;
169 }