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

« back to all changes in this revision

Viewing changes to tests/tracker/birthday-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 BirthdayUpdatesTests : Folks.TestCase
 
27
{
 
28
  private TrackerTest.Backend _tracker_backend;
 
29
  private IndividualAggregator _aggregator;
 
30
  private string _initial_birthday;
 
31
  private string _updated_birthday;
 
32
  private string _individual_id;
 
33
  private bool _initial_birthday_found;
 
34
  private bool _updated_birthday_found;
 
35
  private string _contact_urn;
 
36
  private DateTime _initial_bday_obj;
 
37
  private DateTime _updated_bday_obj;
 
38
  private string _initial_fullname;
 
39
  private GLib.MainLoop _main_loop;
 
40
 
 
41
  public BirthdayUpdatesTests ()
 
42
    {
 
43
      base ("BirthdayUpdates");
 
44
 
 
45
      this._tracker_backend = new TrackerTest.Backend ();
 
46
      this._tracker_backend.debug = false;
 
47
 
 
48
      this.add_test ("birthday updates", this.test_birthday_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_birthday_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_birthday = "2001-10-26T20:32:52Z";
 
65
      this._updated_birthday = "1991-10-26T20:32:52Z";
 
66
      this._contact_urn = "<urn:contact001>";
 
67
 
 
68
      TimeVal t1 = TimeVal ();
 
69
      t1.from_iso8601 (this._initial_birthday);
 
70
      this._initial_bday_obj = new  DateTime.from_timeval_utc (t1);
 
71
 
 
72
      TimeVal t2 = TimeVal ();
 
73
      t2.from_iso8601 (this._updated_birthday);
 
74
      this._updated_bday_obj = new  DateTime.from_timeval_utc (t2);
 
75
 
 
76
      c1.set (TrackerTest.Backend.URN, this._contact_urn);
 
77
      c1.set (Trf.OntologyDefs.NCO_FULLNAME, this._initial_fullname);
 
78
      c1.set (Trf.OntologyDefs.NCO_BIRTHDAY, this._initial_birthday);
 
79
      this._tracker_backend.add_contact (c1);
 
80
 
 
81
      this._tracker_backend.set_up ();
 
82
 
 
83
      this._initial_birthday_found = false;
 
84
      this._updated_birthday_found = false;
 
85
      this._individual_id = "";
 
86
 
 
87
      test_birthday_updates_async ();
 
88
 
 
89
 
 
90
      Timeout.add_seconds (5, () =>
 
91
        {
 
92
          this._main_loop.quit ();
 
93
          assert_not_reached ();
 
94
        });
 
95
 
 
96
      this._main_loop.run ();
 
97
 
 
98
      assert (this._initial_birthday_found == true);
 
99
      assert (this._updated_birthday_found == true);
 
100
 
 
101
      this._tracker_backend.tear_down ();
 
102
    }
 
103
 
 
104
  private async void test_birthday_updates_async ()
 
105
    {
 
106
      var store = BackendStore.dup ();
 
107
      yield store.prepare ();
 
108
      /* Set up the aggregator */
 
109
      this._aggregator = new IndividualAggregator ();
 
110
      this._aggregator.individuals_changed.connect
 
111
          (this._individuals_changed_cb);
 
112
      try
 
113
        {
 
114
          yield this._aggregator.prepare ();
 
115
        }
 
116
      catch (GLib.Error e)
 
117
        {
 
118
          GLib.warning ("Error when calling prepare: %s\n", e.message);
 
119
        }
 
120
    }
 
121
 
 
122
  private void _individuals_changed_cb
 
123
      (Set<Individual> added,
 
124
       Set<Individual> removed,
 
125
       string? message,
 
126
       Persona? actor,
 
127
       GroupDetails.ChangeReason reason)
 
128
    {
 
129
      foreach (var i in added)
 
130
        {
 
131
          if (i.full_name == this._initial_fullname)
 
132
            {
 
133
              i.notify["birthday"].connect (this._notify_birthday_cb);
 
134
              if (i.birthday != null &&
 
135
                  i.birthday.compare (this._initial_bday_obj) == 0)
 
136
                {
 
137
                  this._individual_id = i.id;
 
138
                  this._initial_birthday_found = true;
 
139
                  this._tracker_backend.update_contact (this._contact_urn,
 
140
                      Trf.OntologyDefs.NCO_BIRTHDAY, this._updated_birthday);
 
141
                }
 
142
            }
 
143
        }
 
144
        assert (removed.size == 0);
 
145
    }
 
146
 
 
147
  void _notify_birthday_cb (Object individual_obj, ParamSpec ps)
 
148
    {
 
149
      Folks.Individual i = (Folks.Individual) individual_obj;
 
150
 
 
151
      if (i.birthday == null)
 
152
        {
 
153
          return;
 
154
        }
 
155
 
 
156
      if (i.birthday.compare (this._initial_bday_obj) == 0)
 
157
        {
 
158
          this._individual_id = i.id;
 
159
          this._initial_birthday_found = true;
 
160
          this._tracker_backend.update_contact (this._contact_urn,
 
161
              Trf.OntologyDefs.NCO_BIRTHDAY, this._updated_birthday);
 
162
        }
 
163
      else if (i.birthday.compare (this._updated_bday_obj) == 0)
 
164
        {
 
165
          this._updated_birthday_found = true;
 
166
          this._main_loop.quit ();
 
167
        }
 
168
    }
 
169
}
 
170
 
 
171
public int main (string[] args)
 
172
{
 
173
  Test.init (ref args);
 
174
 
 
175
  TestSuite root = TestSuite.get_root ();
 
176
  root.add_suite (new BirthdayUpdatesTests ().get_suite ());
 
177
 
 
178
  Test.run ();
 
179
 
 
180
  return 0;
 
181
}