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

« back to all changes in this revision

Viewing changes to tests/tracker/match-phone-number.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 MatchPhoneNumberTests : Folks.TestCase
 
27
{
 
28
  private GLib.MainLoop _main_loop;
 
29
  private TrackerTest.Backend _tracker_backend;
 
30
  private IndividualAggregator _aggregator;
 
31
  private string _persona_fullname_1 = "aaa";
 
32
  private string _persona_fullname_2 = "bbb";
 
33
  private string _phone_1 = "+1-800-123-4567";
 
34
  private string _phone_2 = "123-4567";
 
35
  private bool _added_personas = false;
 
36
  private string _individual_id_1 = "";
 
37
  private string _individual_id_2 = "";
 
38
  private Folks.MatchResult _match;
 
39
  private Trf.PersonaStore _pstore;
 
40
 
 
41
  public MatchPhoneNumberTests ()
 
42
    {
 
43
      base ("MatchPhoneNumberTests");
 
44
 
 
45
      this._tracker_backend = new TrackerTest.Backend ();
 
46
 
 
47
      this.add_test ("test potential match with phone numbers ",
 
48
          this.test_match_phone_number);
 
49
    }
 
50
 
 
51
  public override void set_up ()
 
52
    {
 
53
    }
 
54
 
 
55
  public override void tear_down ()
 
56
    {
 
57
      this._tracker_backend.tear_down ();
 
58
    }
 
59
 
 
60
  public void test_match_phone_number ()
 
61
    {
 
62
      this._main_loop = new GLib.MainLoop (null, false);
 
63
 
 
64
      this._test_match_phone_number_async ();
 
65
 
 
66
      Timeout.add_seconds (5, () =>
 
67
        {
 
68
          this._main_loop.quit ();
 
69
          assert_not_reached ();
 
70
        });
 
71
 
 
72
      this._main_loop.run ();
 
73
 
 
74
      /* Phone number match is very decisive */
 
75
      assert (this._match == Folks.MatchResult.HIGH);
 
76
    }
 
77
 
 
78
  private async void _test_match_phone_number_async ()
 
79
    {
 
80
      var store = BackendStore.dup ();
 
81
      yield store.prepare ();
 
82
      this._aggregator = new IndividualAggregator ();
 
83
      this._aggregator.individuals_changed.connect
 
84
          (this._individuals_changed_cb);
 
85
      try
 
86
        {
 
87
          yield this._aggregator.prepare ();
 
88
          this._pstore = null;
 
89
          foreach (var backend in store.enabled_backends.values)
 
90
            {
 
91
              this._pstore =
 
92
                (Trf.PersonaStore) backend.persona_stores.get ("tracker");
 
93
              if (this._pstore != null)
 
94
                break;
 
95
            }
 
96
          assert (this._pstore != null);
 
97
          this._pstore.notify["is-prepared"].connect (this._notify_pstore_cb);
 
98
          this._try_to_add ();
 
99
        }
 
100
      catch (GLib.Error e)
 
101
        {
 
102
          GLib.warning ("Error when calling prepare: %s\n", e.message);
 
103
        }
 
104
    }
 
105
 
 
106
  private void _individuals_changed_cb
 
107
      (Set<Individual> added,
 
108
       Set<Individual> removed,
 
109
       string? message,
 
110
       Persona? actor,
 
111
       GroupDetails.ChangeReason reason)
 
112
    {
 
113
      foreach (var i in added)
 
114
        {
 
115
          if (i.full_name == this._persona_fullname_1)
 
116
            {
 
117
              this._individual_id_1 = i.id;
 
118
            }
 
119
          else if (i.full_name == this._persona_fullname_2)
 
120
            {
 
121
              this._individual_id_2 = i.id;
 
122
            }
 
123
        }
 
124
 
 
125
      if (this._individual_id_1 != "" &&
 
126
          this._individual_id_2 != "")
 
127
        {
 
128
          this._try_potential_match ();
 
129
        }
 
130
 
 
131
      assert (removed.size == 0);
 
132
    }
 
133
 
 
134
  private void _try_potential_match ()
 
135
    {
 
136
      var ind1 = this._aggregator.individuals.get (this._individual_id_1);
 
137
      var ind2 = this._aggregator.individuals.get (this._individual_id_2);
 
138
 
 
139
      Folks.PotentialMatch matchObj = new Folks.PotentialMatch ();
 
140
      this._match = matchObj.potential_match (ind1, ind2);
 
141
 
 
142
      this._main_loop.quit ();
 
143
    }
 
144
 
 
145
  private void _notify_pstore_cb (Object _pstore, ParamSpec ps)
 
146
    {
 
147
      this._try_to_add ();
 
148
    }
 
149
 
 
150
  private async void _try_to_add ()
 
151
    {
 
152
      lock (this._added_personas)
 
153
        {
 
154
          if (this._pstore.is_prepared &&
 
155
              this._added_personas == false)
 
156
            {
 
157
              this._added_personas = true;
 
158
              yield this._add_personas ();
 
159
            }
 
160
        }
 
161
    }
 
162
 
 
163
  private async void _add_personas ()
 
164
    {
 
165
      HashTable<string, Value?> details1 = new HashTable<string, Value?>
 
166
          (str_hash, str_equal);
 
167
      HashTable<string, Value?> details2 = new HashTable<string, Value?>
 
168
          (str_hash, str_equal);
 
169
      Value? val;
 
170
 
 
171
      val = Value (typeof (string));
 
172
      val.set_string (this._persona_fullname_1);
 
173
      details1.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
 
174
          (owned) val);
 
175
 
 
176
      val = Value (typeof (Set<FieldDetails>));
 
177
      var phone_numbers1 = new HashSet<FieldDetails> ();
 
178
      var phone_number_1 = new FieldDetails (this._phone_1);
 
179
      phone_numbers1.add (phone_number_1);
 
180
      val.set_object (phone_numbers1);
 
181
      details1.insert (
 
182
          Folks.PersonaStore.detail_key (PersonaDetail.PHONE_NUMBERS),
 
183
          (owned) val);
 
184
 
 
185
      val = Value (typeof (string));
 
186
      val.set_string (this._persona_fullname_2);
 
187
      details2.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
 
188
          (owned) val);
 
189
 
 
190
      val = Value (typeof (Set<FieldDetails>));
 
191
      var phone_numbers2 = new HashSet<FieldDetails> ();
 
192
      var phone_number_2 = new FieldDetails (this._phone_2);
 
193
      phone_numbers2.add (phone_number_2);
 
194
      val.set_object (phone_numbers2);
 
195
      details2.insert (
 
196
          Folks.PersonaStore.detail_key (PersonaDetail.PHONE_NUMBERS),
 
197
          (owned) val);
 
198
 
 
199
     try
 
200
        {
 
201
          yield this._aggregator.add_persona_from_details (null,
 
202
              this._pstore, details1);
 
203
 
 
204
          yield this._aggregator.add_persona_from_details (null,
 
205
              this._pstore, details2);
 
206
        }
 
207
      catch (Folks.IndividualAggregatorError e)
 
208
        {
 
209
          GLib.warning ("[AddPersonaError] add_persona_from_details: %s\n",
 
210
              e.message);
 
211
        }
 
212
    }
 
213
}
 
214
 
 
215
public int main (string[] args)
 
216
{
 
217
  Test.init (ref args);
 
218
 
 
219
  TestSuite root = TestSuite.get_root ();
 
220
  root.add_suite (new MatchPhoneNumberTests ().get_suite ());
 
221
 
 
222
  Test.run ();
 
223
 
 
224
  return 0;
 
225
}