summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/compose/test/unit/test_detectAttachmentCharset.js
blob: 27e879d018d188b72be2a80e636b32f223b24df8 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * Test suite for auto-detecting attachment file charset.
 */

function checkAttachmentCharset(expectedCharset) {
  let msgData = mailTestUtils.loadMessageToString(
    gDraftFolder,
    mailTestUtils.firstMsgHdr(gDraftFolder)
  );
  let attachmentData = getAttachmentFromContent(msgData);

  Assert.equal(expectedCharset, getContentCharset(attachmentData));
}

function getContentCharset(aContent) {
  let found = aContent.match(/^Content-Type: text\/plain; charset=(.*?);/);
  if (found) {
    Assert.equal(found.length, 2);
    return found[1];
  }
  return null;
}

async function testUTF8() {
  await createMessage(do_get_file("data/test-UTF-8.txt"));
  checkAttachmentCharset("UTF-8");
}

async function testUTF16BE() {
  await createMessage(do_get_file("data/test-UTF-16BE.txt"));
  checkAttachmentCharset("UTF-16BE");
}

async function testUTF16LE() {
  await createMessage(do_get_file("data/test-UTF-16LE.txt"));
  checkAttachmentCharset("UTF-16LE");
}

async function testShiftJIS() {
  await createMessage(do_get_file("data/test-SHIFT_JIS.txt"));
  checkAttachmentCharset("Shift_JIS");
}

async function testISO2022JP() {
  await createMessage(do_get_file("data/test-ISO-2022-JP.txt"));
  checkAttachmentCharset("ISO-2022-JP");
}

async function testKOI8R() {
  // NOTE: KOI8-R is detected as KOI8-U which is a superset covering both
  // Russian and Ukrainian (a few box-drawing characters are repurposed).
  await createMessage(do_get_file("data/test-KOI8-R.txt"));
  checkAttachmentCharset("KOI8-U");
}

async function testWindows1252() {
  await createMessage(do_get_file("data/test-windows-1252.txt"));
  checkAttachmentCharset("windows-1252");
}

var tests = [
  testUTF8,
  testUTF16BE,
  testUTF16LE,
  testShiftJIS,
  testISO2022JP,
  testKOI8R,
  testWindows1252,
];

function run_test() {
  // Ensure we have at least one mail account
  localAccountUtils.loadLocalMailAccount();
  Services.prefs.setIntPref("mail.strictly_mime.parm_folding", 0);

  tests.forEach(x => add_task(x));
  run_next_test();
}