~ubuntu-branches/ubuntu/precise/folks/precise

« back to all changes in this revision

Viewing changes to tests/tracker/avatar-updates.vala

  • Committer: Bazaar Package Importer
  • Author(s): Ken VanDine
  • Date: 2011-06-10 11:28:11 UTC
  • mfrom: (1.2.11 upstream) (4.2.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20110610112811-whyeodbo9mjezxfp
Tags: 0.5.2-1ubuntu1
* Merge with Debian experimental, remaining Ubuntu changes:
  - debian/control:
    + Add Vcs-Bzr link

Show diffs side-by-side

added added

removed removed

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