summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/compose/test/unit/test_nsIMsgCompFields.js
blob: 69c1b753b6ee781c59346cf01de18e46ff9332f1 (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
/* 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 that nsIMsgCompFields works properly

var nsMsgCompFields = Components.Constructor(
  "@mozilla.org/messengercompose/composefields;1",
  Ci.nsIMsgCompFields
);

function check_headers(enumerator, container) {
  let checkValues = new Set(container.map(header => header.toLowerCase()));
  for (let value of enumerator) {
    value = value.toLowerCase();
    Assert.ok(checkValues.has(value));
    checkValues.delete(value);
  }
  Assert.equal(checkValues.size, 0);
}

function run_test() {
  let fields = new nsMsgCompFields();
  Assert.ok(fields instanceof Ci.nsIMsgCompFields);
  Assert.ok(fields instanceof Ci.msgIStructuredHeaders);
  Assert.ok(fields instanceof Ci.msgIWritableStructuredHeaders);
  check_headers(fields.headerNames, []);
  Assert.ok(!fields.hasRecipients);

  // Try some basic headers
  fields.setHeader("From", [{ name: "", email: "a@test.invalid" }]);
  let from = fields.getHeader("from");
  Assert.equal(from.length, 1);
  Assert.equal(from[0].email, "a@test.invalid");
  check_headers(fields.headerNames, ["From"]);
  Assert.ok(!fields.hasRecipients);

  // Add a To header
  fields.setHeader("To", [{ name: "", email: "b@test.invalid" }]);
  check_headers(fields.headerNames, ["From", "To"]);
  Assert.ok(fields.hasRecipients);

  // Delete a header...
  fields.deleteHeader("from");
  Assert.equal(fields.getHeader("From"), undefined);
  check_headers(fields.headerNames, ["To"]);

  // Subject should work and not convert to RFC 2047.
  fields.subject = "\u79c1\u306f\u4ef6\u540d\u5348\u524d";
  Assert.equal(fields.subject, "\u79c1\u306f\u4ef6\u540d\u5348\u524d");
  Assert.equal(
    fields.getHeader("Subject"),
    "\u79c1\u306f\u4ef6\u540d\u5348\u524d"
  );

  // Check header synchronization.
  fields.from = "a@test.invalid";
  Assert.equal(fields.from, "a@test.invalid");
  Assert.equal(fields.getHeader("From")[0].email, "a@test.invalid");
  fields.from = null;
  Assert.equal(fields.getHeader("From"), undefined);
}