summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/test/unit/test_collection.js
blob: 720f28c2466d3b5907bf1e785856987bddf93f3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * Test suite for the Address Collector Service.
 *
 * This tests the main collection functions for adding new cards and modifying
 * existing ones.
 *
 * Tests against cards in different ABs are done in test_collection_2.js.
 */

// Source fields (emailHeader) and expected results for use for
// testing the addition of new addresses to the database.
//
// Note: these email addresses should be different to allow collecting an
// address to add a different card each time.
var addEmailChecks =
  // First 3 items aimed at basic collection and mail format.
  [
    {
      emailHeader: "test0@foo.invalid",
      primaryEmail: "test0@foo.invalid",
      displayName: "",
      firstName: "",
      lastName: "",
      screenName: "",
    },
    {
      emailHeader: "test1@foo.invalid",
      primaryEmail: "test1@foo.invalid",
      displayName: "",
      firstName: "",
      lastName: "",
      screenName: "",
    },
    {
      emailHeader: "test2@foo.invalid",
      primaryEmail: "test2@foo.invalid",
      displayName: "",
      firstName: "",
      lastName: "",
      screenName: "",
    },
    // UTF-8 based addresses (bug 407564)
    {
      emailHeader: "test0@\u00D0.invalid",
      primaryEmail: "test0@\u00D0.invalid",
      displayName: "",
      firstName: "",
      lastName: "",
      screenName: "",
    },
    {
      emailHeader: "test0\u00D0@foo.invalid",
      primaryEmail: "test0\u00D0@foo.invalid",
      displayName: "",
      firstName: "",
      lastName: "",
      screenName: "",
    },
    // Screen names
    {
      emailHeader: "invalid\u00D00@aol.com",
      primaryEmail: "invalid\u00D00@aol.com",
      displayName: "",
      firstName: "",
      lastName: "",
      screenName: "invalid\u00D00",
    },
    {
      emailHeader: "invalid1\u00D00@cs.com",
      primaryEmail: "invalid1\u00D00@cs.com",
      displayName: "",
      firstName: "",
      lastName: "",
      screenName: "invalid1\u00D00",
    },
    {
      emailHeader: "invalid2\u00D00@netscape.net",
      primaryEmail: "invalid2\u00D00@netscape.net",
      displayName: "",
      firstName: "",
      lastName: "",
      screenName: "invalid2\u00D00",
    },
    // Collection of names
    {
      emailHeader: "Test User <test3@foo.invalid>",
      primaryEmail: "test3@foo.invalid",
      displayName: "Test User",
      firstName: "Test",
      lastName: "User",
      screenName: "",
    },
    {
      emailHeader: "Test <test4@foo.invalid>",
      primaryEmail: "test4@foo.invalid",
      displayName: "Test",
      firstName: "",
      lastName: "",
      screenName: "",
    },
    // Collection of names with UTF-8 specific items
    {
      emailHeader: "Test\u00D0 User <test5@foo.invalid>",
      primaryEmail: "test5@foo.invalid",
      displayName: "Test\u00D0 User",
      firstName: "Test\u00D0",
      lastName: "User",
      screenName: "",
    },
    {
      emailHeader: "Test\u00D0 <test6@foo.invalid>",
      primaryEmail: "test6@foo.invalid",
      displayName: "Test\u00D0",
      firstName: "",
      lastName: "",
      screenName: "",
    },
  ];

// Source fields (emailHeader) and expected results for use for
// testing the modification of cards in the database.
//
// Note: these sets re-use some of the ones for ease of definition.
var modifyEmailChecks =
  // No display name/other details. Add details and modify mail format.
  [
    {
      emailHeader: "Modify User\u00D0 <test0@\u00D0.invalid>",
      primaryEmail: "test0@\u00D0.invalid",
      displayName: "Modify User\u00D0",
      firstName: "Modify",
      lastName: "User\u00D0",
      screenName: "",
    },
    {
      emailHeader: "Modify <test0\u00D0@foo.invalid>",
      primaryEmail: "test0\u00D0@foo.invalid",
      displayName: "Modify",
      firstName: "",
      lastName: "",
      screenName: "",
    },
    // No modification of existing cards with display names
    {
      emailHeader: "Modify2 User\u00D02 <test0@\u00D0.invalid>",
      primaryEmail: "test0@\u00D0.invalid",
      displayName: "Modify User\u00D0",
      firstName: "Modify",
      lastName: "User\u00D0",
      screenName: "",
    },
    {
      emailHeader: "Modify3 <test0\u00D0@foo.invalid>",
      primaryEmail: "test0\u00D0@foo.invalid",
      displayName: "Modify",
      firstName: "",
      lastName: "",
      screenName: "",
    },
    // Check no modification of cards for mail format where format is not
    // "unknown".
    {
      emailHeader: "Modify User\u00D0 <test0@\u00D0.invalid>",
      primaryEmail: "test0@\u00D0.invalid",
      displayName: "Modify User\u00D0",
      firstName: "Modify",
      lastName: "User\u00D0",
      screenName: "",
    },
    {
      emailHeader: "Modify <test0\u00D0@foo.invalid>",
      primaryEmail: "test0\u00D0@foo.invalid",
      displayName: "Modify",
      firstName: "",
      lastName: "",
      screenName: "",
    },
    // No modification of cards with email in second email address.
    {
      emailHeader: "Modify Secondary <usersec\u00D0@foo.invalid>",
      primaryEmail: "userprim\u00D0@foo.invalid",
      secondEmail: "usersec\u00D0@foo.invalid",
      displayName: "",
      firstName: "",
      lastName: "",
      screenName: "",
    },
    {
      emailHeader: "Modify <usersec\u00D0@foo.invalid>",
      primaryEmail: "userprim\u00D0@foo.invalid",
      secondEmail: "usersec\u00D0@foo.invalid",
      displayName: "",
      firstName: "",
      lastName: "",
      screenName: "",
    },
  ];

var collectChecker = {
  addressCollect: null,
  AB: null,
  part: 0,

  checkAddress(aDetails) {
    info("checkAddress: " + aDetails.emailHeader);
    try {
      this.addressCollect.collectAddress(aDetails.emailHeader, true);

      this.checkCardResult(aDetails, false);
    } catch (e) {
      throw new Error(
        "FAILED in checkAddress emailHeader: " +
          aDetails.emailHeader +
          " part: " +
          this.part +
          " : " +
          e
      );
    }
    ++this.part;
  },

  checkAll(aDetailsArray) {
    try {
      // Formulate the string to add.
      var emailHeader = "";
      var i;

      for (i = 0; i < aDetailsArray.length - 1; ++i) {
        emailHeader += aDetailsArray[i].emailHeader + ", ";
      }

      emailHeader += aDetailsArray[aDetailsArray.length - 1].emailHeader;

      // Now add it. In this case we just set the Mail format Type to unknown.
      this.addressCollect.collectAddress(emailHeader, true);

      for (i = 0; i < aDetailsArray.length; ++i) {
        this.checkCardResult(aDetailsArray[i], true);
      }
    } catch (e) {
      throw new Error("FAILED in checkAll item: " + i + " : " + e);
    }
  },

  checkCardResult(aDetails) {
    info("checkCardResult: " + aDetails.emailHeader);
    try {
      var card = this.AB.cardForEmailAddress(aDetails.primaryEmail);

      Assert.ok(card != null);

      if ("secondEmail" in aDetails) {
        Assert.equal(card.emailAddresses[1], aDetails.secondEmail);
      }

      Assert.equal(card.displayName, aDetails.displayName);
      Assert.equal(card.firstName, aDetails.firstName);
      Assert.equal(card.lastName, aDetails.lastName);
      Assert.equal(card.getProperty("_AimScreenName", ""), aDetails.screenName);
    } catch (e) {
      throw new Error(
        "FAILED in checkCardResult emailHeader: " +
          aDetails.emailHeader +
          " : " +
          e
      );
    }
  },
};

function run_test() {
  // Test - Get the address collecter

  // XXX Getting all directories ensures we create all ABs because the
  // address collecter can't currently create ABs itself (bug 314448).
  MailServices.ab.directories;

  // Get the actual AB for the collector so we can check cards have been
  // added.
  collectChecker.AB = MailServices.ab.getDirectory(
    Services.prefs.getCharPref("mail.collect_addressbook")
  );

  // Get the actual collecter
  collectChecker.addressCollect = Cc[
    "@mozilla.org/addressbook/services/addressCollector;1"
  ].getService(Ci.nsIAbAddressCollector);

  // Test - Addition of header without email address.

  collectChecker.addressCollect.collectAddress("MyTest <>", true);

  // Address book should have no cards present.
  Assert.equal(collectChecker.AB.childCards.length, 0);

  // Test - Email doesn't exist, but don't add it.

  // As we've just set everything up, we know we haven't got anything in the
  // AB, so just try and collect without adding.
  collectChecker.addressCollect.collectAddress(
    addEmailChecks[0].emailHeader,
    false
  );

  var card = collectChecker.AB.cardForEmailAddress(
    addEmailChecks[0].emailHeader
  );

  Assert.ok(card == null);

  // Test - Try and collect various emails and formats.

  collectChecker.part = 0;

  addEmailChecks.forEach(collectChecker.checkAddress, collectChecker);

  // Test - Do all emails at the same time.

  // First delete all existing cards
  collectChecker.AB.deleteCards(collectChecker.AB.childCards);

  // Address book should have no cards present.
  Assert.equal(collectChecker.AB.childCards.length, 0);

  Assert.equal(
    collectChecker.AB.cardForEmailAddress(addEmailChecks[0].emailHeader),
    null
  );

  // Now do all emails at the same time.
  collectChecker.checkAll(addEmailChecks);

  // Test - Try and modify various emails and formats.

  // Add a basic card with just primary and second email to allow testing
  // of the case where we don't modify when second email is matching.
  card = Cc["@mozilla.org/addressbook/cardproperty;1"].createInstance(
    Ci.nsIAbCard
  );

  card.primaryEmail = "userprim\u00D0@foo.invalid";
  card.setProperty("SecondEmail", "usersec\u00D0@foo.invalid");

  collectChecker.AB.addCard(card);

  collectChecker.part = 0;

  modifyEmailChecks.forEach(collectChecker.checkAddress, collectChecker);

  // Test collectSingleAddress - Note: because the above tests test
  // collectAddress which we know calls collectSingleAddress, we only need to
  // test the case where aSkipCheckExisting is true.

  // Add an email that is already there and check we get two instances of it in
  // the AB.

  const kSingleAddress =
    modifyEmailChecks[modifyEmailChecks.length - 1].primaryEmail;
  const kSingleDisplayName = "Test Single";

  collectChecker.addressCollect.collectSingleAddress(
    kSingleAddress,
    kSingleDisplayName,
    true,
    true
  );

  // Try collecting the same address in another case. This shouldn't create any
  // new card.
  collectChecker.addressCollect.collectSingleAddress(
    kSingleAddress.toUpperCase(),
    kSingleDisplayName,
    true,
    true
  );

  var foundCards = [];

  for (card of collectChecker.AB.childCards) {
    if (card.primaryEmail == kSingleAddress) {
      foundCards.push(card);
    }
  }

  Assert.equal(foundCards.length, 2);

  if (
    foundCards[0].displayName != kSingleDisplayName &&
    foundCards[1].displayName != kSingleDisplayName
  ) {
    do_throw("Error, collectSingleCard didn't create a new card");
  }

  if (foundCards[0].displayName != "" && foundCards[1].displayName != "") {
    do_throw(
      "Error, collectSingleCard created ok, but other card does not exist"
    );
  }
}