summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/test/unit/test_export.js
blob: 34874e2f691e57a2585c2e4ebda6462be758998c (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
/* 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/. */

"use strict";

var { AddrBookCard } = ChromeUtils.import(
  "resource:///modules/AddrBookCard.jsm"
);
var { AddrBookUtils } = ChromeUtils.import(
  "resource:///modules/AddrBookUtils.jsm"
);
var { AppConstants } = ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);
var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
var { VCardPropertyEntry } = ChromeUtils.import(
  "resource:///modules/VCardUtils.jsm"
);

async function subtest(cardConstructor) {
  let dirPrefId = MailServices.ab.newAddressBook(
    "new book",
    "",
    Ci.nsIAbManager.JS_DIRECTORY_TYPE
  );
  let book = MailServices.ab.getDirectoryFromId(dirPrefId);

  let contact1 = cardConstructor();
  contact1.UID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
  contact1.displayName = "contact number one";
  contact1.firstName = "contact";
  contact1.lastName = "one";
  contact1.primaryEmail = "contact1@invalid";
  contact1 = book.addCard(contact1);

  let contact2 = cardConstructor();
  contact2.UID = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy";
  contact2.displayName = "contact number two";
  contact2.firstName = "contact";
  contact2.lastName = "two";
  contact2.primaryEmail = "contact2@invalid";
  if (contact2.supportsVCard) {
    contact2.vCardProperties.addValue("title", `"worker"`);
    contact2.vCardProperties.addValue("note", "here's some unicode text…");
    contact2.vCardProperties.addEntry(
      new VCardPropertyEntry("x-custom1", {}, "text", "custom, 1")
    );
    contact2.vCardProperties.addEntry(
      new VCardPropertyEntry("x-custom2", {}, "text", "custom\t2")
    );
    contact2.vCardProperties.addEntry(
      new VCardPropertyEntry("x-custom3", {}, "text", "custom\r3")
    );
    contact2.vCardProperties.addEntry(
      new VCardPropertyEntry("x-custom4", {}, "text", "custom\n4")
    );
  } else {
    contact2.setProperty("JobTitle", `"worker"`);
    contact2.setProperty("Notes", "here's some unicode text…");
    contact2.setProperty("Custom1", "custom, 1");
    contact2.setProperty("Custom2", "custom\t2");
    contact2.setProperty("Custom3", "custom\r3");
    contact2.setProperty("Custom4", "custom\n4");
  }
  contact2 = book.addCard(contact2);

  let list = Cc["@mozilla.org/addressbook/directoryproperty;1"].createInstance(
    Ci.nsIAbDirectory
  );
  list.isMailList = true;
  list.dirName = "new list";
  list = book.addMailList(list);
  list.addCard(contact1);

  await compareAgainstFile(
    "export.csv",
    AddrBookUtils.exportDirectoryToDelimitedText(book, ",")
  );
  await compareAgainstFile(
    "export.txt",
    AddrBookUtils.exportDirectoryToDelimitedText(book, "\t")
  );
  await compareAgainstFile(
    "export.vcf",
    AddrBookUtils.exportDirectoryToVCard(book)
  );
  // modifytimestamp is always changing, replace it with a fixed value.
  await compareAgainstFile(
    "export.ldif",
    AddrBookUtils.exportDirectoryToLDIF(book).replace(
      /modifytimestamp: \d+/g,
      "modifytimestamp: 12345"
    )
  );
}

async function compareAgainstFile(fileName, actual) {
  info(`checking against ${fileName}`);

  // The test files are UTF-8 encoded and have Windows line endings. The
  // exportDirectoryTo* functions are platform-dependent, except for VCard
  // which always uses Windows line endings.

  let file = do_get_file(`data/${fileName}`);
  let expected = await IOUtils.readUTF8(file.path);

  if (AppConstants.platform != "win" && fileName != "export.vcf") {
    expected = expected.replace(/\r\n/g, "\n");
  }

  // From here on, \r is just another character. It will be the last character
  // on lines where Windows line endings exist.
  let expectedLines = expected.split("\n");
  let actualLines = actual.split("\n");
  info(actual);
  Assert.deepEqual(actualLines.sort(), expectedLines.sort());
  // equal(actualLines.length, expectedLines.length, "correct number of lines");

  // for (let l = 0; l < expectedLines.length; l++) {
  //   let expectedLine = expectedLines[l];
  //   let actualLine = actualLines[l];
  //   if (actualLine == expectedLine) {
  //     ok(true, `line ${l + 1} matches`);
  //   } else {
  //     for (let c = 0; c < expectedLine.length && c < actualLine.length; c++) {
  //       if (actualLine[c] != expectedLine[c]) {
  //         // This call to equal automatically prints some extra characters of
  //         // context. Hopefully that helps with debugging.
  //         equal(
  //           actualLine.substring(c - 10, c + 10),
  //           expectedLine.substring(c - 10, c + 10),
  //           `line ${l + 1} does not match at character ${c + 1}`
  //         );
  //       }
  //     }
  //     equal(
  //       expectedLine.length,
  //       actualLine.length,
  //       `line ${l + 1} lengths differ`
  //     );
  //   }
  // }
}

add_task(async function addrBookCard() {
  return subtest(() => new AddrBookCard());
});

add_task(async function cardProperty() {
  return subtest(() =>
    Cc["@mozilla.org/addressbook/cardproperty;1"].createInstance(Ci.nsIAbCard)
  );
});