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

« back to all changes in this revision

Viewing changes to tests/tracker/remove-persona.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 RemovePersonaTests : 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 bool _persona_removed;
 
33
  private bool _individual_removed;
 
34
  private string _individual_id;
 
35
  private PersonaStore _pstore;
 
36
  private string _persona_id;
 
37
  private Individual _individual;
 
38
  private bool _added_persona = false;
 
39
 
 
40
  public RemovePersonaTests ()
 
41
    {
 
42
      base ("RemovePersonaTests");
 
43
 
 
44
      this._tracker_backend = new TrackerTest.Backend ();
 
45
 
 
46
      this.add_test ("test adding personas to Tracker ", this.test_remove_persona);
 
47
    }
 
48
 
 
49
  public override void set_up ()
 
50
    {
 
51
    }
 
52
 
 
53
  public override void tear_down ()
 
54
    {
 
55
    }
 
56
 
 
57
  public void test_remove_persona ()
 
58
    {
 
59
      this._main_loop = new GLib.MainLoop (null, false);
 
60
      this._persona_fullname = "persona #1";
 
61
 
 
62
      this._persona_removed = false;
 
63
      this._individual_removed = false;
 
64
 
 
65
      this._test_remove_persona_async ();
 
66
 
 
67
      Timeout.add_seconds (5, () =>
 
68
        {
 
69
          this._main_loop.quit ();
 
70
          assert_not_reached ();
 
71
        });
 
72
 
 
73
      this._main_loop.run ();
 
74
 
 
75
      assert (this._persona_removed == true);
 
76
      assert (this._individual_removed == true);
 
77
 
 
78
      this._tracker_backend.tear_down ();
 
79
    }
 
80
 
 
81
  private async void _test_remove_persona_async ()
 
82
    {
 
83
      var store = BackendStore.dup ();
 
84
      yield store.prepare ();
 
85
      this._aggregator = new IndividualAggregator ();
 
86
      this._aggregator.individuals_changed.connect
 
87
          (this._individuals_changed_cb);
 
88
      try
 
89
        {
 
90
          yield this._aggregator.prepare ();
 
91
 
 
92
          this._pstore = null;
 
93
          foreach (var backend in store.enabled_backends.values)
 
94
            {
 
95
              this._pstore = backend.persona_stores.get ("tracker");
 
96
              if (this._pstore != null)
 
97
                break;
 
98
            }
 
99
          assert (this._pstore != null);
 
100
 
 
101
          this._pstore.notify["is-prepared"].connect (this._notify_pstore_cb);
 
102
          this._try_to_add ();
 
103
        }
 
104
      catch (GLib.Error e)
 
105
        {
 
106
          GLib.warning ("Error when calling prepare: %s\n", e.message);
 
107
        }
 
108
    }
 
109
 
 
110
  private void _notify_pstore_cb (Object _pstore, ParamSpec ps)
 
111
    {
 
112
      this._try_to_add ();
 
113
    }
 
114
 
 
115
  private void _try_to_add ()
 
116
    {
 
117
      if (this._pstore.is_prepared &&
 
118
          this._added_persona == false)
 
119
        {
 
120
          this._added_persona = true;
 
121
          this._add_persona ();
 
122
        }
 
123
    }
 
124
 
 
125
  private async void _add_persona ()
 
126
    {
 
127
      HashTable<string, Value?> details = new HashTable<string, Value?>
 
128
          (str_hash, str_equal);
 
129
      Value? v1 = Value (typeof (string));
 
130
      v1.set_string (this._persona_fullname);
 
131
      details.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
 
132
          (owned) v1);
 
133
 
 
134
      Value? v2 = Value (typeof (Set<FieldDetails>));
 
135
      var emails = new HashSet<FieldDetails> ();
 
136
      var email_1 = new FieldDetails ("test-1@example.org");
 
137
      emails.add (email_1);
 
138
      var email_2 = new FieldDetails ("test-2@example.org");
 
139
      emails.add (email_2);
 
140
      v2.set_object (emails);
 
141
      details.insert (
 
142
          Folks.PersonaStore.detail_key (PersonaDetail.EMAIL_ADDRESSES),
 
143
          (owned) v2);
 
144
 
 
145
      try
 
146
        {
 
147
          yield this._aggregator.add_persona_from_details
 
148
              (null, this._pstore, details);
 
149
        }
 
150
      catch (Folks.IndividualAggregatorError e)
 
151
        {
 
152
          GLib.warning ("[RemovePersonaError] add_persona_from_details: %s\n",
 
153
              e.message);
 
154
        }
 
155
    }
 
156
 
 
157
  private void _individuals_changed_cb
 
158
      (Set<Individual> added,
 
159
       Set<Individual> removed,
 
160
       string? message,
 
161
       Persona? actor,
 
162
       GroupDetails.ChangeReason reason)
 
163
    {
 
164
      foreach (var i in added)
 
165
        {
 
166
          if (i.full_name == this._persona_fullname)
 
167
            {
 
168
              this._individual_id = i.id;
 
169
 
 
170
              /* Only examine the first persona */
 
171
              foreach (var p in i.personas)
 
172
                {
 
173
                  this._persona_id = p.iid;
 
174
                  break;
 
175
                }
 
176
 
 
177
              this._individual = i;
 
178
              if (this._pstore.personas.has_key (this._persona_id) == true)
 
179
                {
 
180
                  this._pstore.personas_changed.connect (this._personas_cb);
 
181
                  this._aggregator.remove_individual (this._individual);
 
182
                }
 
183
            }
 
184
        }
 
185
 
 
186
      foreach (var i in removed)
 
187
        {
 
188
          if (i.id == this._individual_id)
 
189
            {
 
190
              this._individual_removed = true;
 
191
            }
 
192
        }
 
193
    }
 
194
 
 
195
  private void _personas_cb ()
 
196
    {
 
197
      if (this._pstore.personas.has_key (this._persona_id) == false)
 
198
        {
 
199
          this._persona_removed = true;
 
200
          this._main_loop.quit ();
 
201
        }
 
202
    }
 
203
}
 
204
 
 
205
public int main (string[] args)
 
206
{
 
207
  Test.init (ref args);
 
208
 
 
209
  TestSuite root = TestSuite.get_root ();
 
210
  root.add_suite (new RemovePersonaTests ().get_suite ());
 
211
 
 
212
  Test.run ();
 
213
 
 
214
  return 0;
 
215
}