summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/test/unit/test_nsAbManager2.js
blob: 37238c51e86f5e8f21c26327f6663f5e8b758fb9 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * Test suite for nsAbManager functions relating to add/delete directories and
 * getting the list of directories..
 */

function checkDirs(aDirs, aDirArray) {
  // Don't modify the passed in array.
  var dirArray = aDirArray.concat();

  for (let dir of aDirs) {
    var loc = dirArray.indexOf(dir.URI);

    Assert.equal(MailServices.ab.getDirectory(dir.URI), dir);

    if (loc == -1) {
      do_throw(
        "Unexpected directory " + dir.URI + " found in address book list"
      );
    } else {
      dirArray[loc] = null;
    }
  }

  dirArray.forEach(function (value) {
    Assert.equal(value, null);
  });
}

function addDirectory(dirName) {
  // Add the directory
  let dirPrefId = MailServices.ab.newAddressBook(dirName, "", kPABData.dirType);
  return MailServices.ab.getDirectoryFromId(dirPrefId);
}

async function run_test() {
  var expectedABs = [kPABData.URI, kCABData.URI];

  // Test - Check initial directories

  checkDirs(MailServices.ab.directories, expectedABs);

  // Test - Add a directory

  var newDirectory1 = addDirectory("testAb1");

  // Test - Check new directory list
  expectedABs.push(newDirectory1.URI);

  checkDirs(MailServices.ab.directories, expectedABs);

  // Test - Repeat for a second directory

  var newDirectory2 = addDirectory("testAb2");

  // Test - Check new directory list
  expectedABs.push(newDirectory2.URI);

  checkDirs(MailServices.ab.directories, expectedABs);

  // Test - Remove a directory

  var pos = expectedABs.indexOf(newDirectory1.URI);

  expectedABs.splice(pos, 1);

  await promiseDirectoryRemoved(newDirectory1.URI);
  newDirectory1 = null;

  // Test - Check new directory list

  checkDirs(MailServices.ab.directories, expectedABs);

  // Test - Repeat the removal

  await promiseDirectoryRemoved(newDirectory2.URI);
  newDirectory2 = null;

  expectedABs.pop();

  // Test - Check new directory list
  checkDirs(MailServices.ab.directories, expectedABs);
}