summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/test/unit/test_getMsgTextFromStream.js
blob: b803619e21226a20e12820d45366b02fe6f247ab (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 *
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/licenses/publicdomain/
 *
 * ***** END LICENSE BLOCK ***** */

/**
 * Test suite for GetMsgTextFromStream.
 *
 * Currently tests: text/plain, text/html -- with tags stripped and without,
 * base64, multipart.
 * Does not currently test: quoted-printable, stripping quotes, UTF-8, small values of
 * bytesToRead.
 */
var kDataRoot = "../../../data/";

function create(fileName, bytes, compressQuotes, stripHTML, outContentType) {
  return {
    name: fileName,
    bytesToRead: bytes,
    compressQuotes,
    stripHTML,
    contentType: outContentType,
  };
}

var gTestFiles = [
  create("basic1", 1024, false, false, "text/plain"), // Simple plain text
  create("basic1", 1024, false, true, "text/plain"), // should be same as above
  create("basic2", 1024, false, false, "text/html"), // Simple HTML
  create("basic3", 1024, false, true, "text/html"), // HTML with tags stripped out
  create("basic4", 1024, false, false, "text/plain"), // No content type, should be assumed to be text/plain
  create("basic4", 1024, false, true, "text/plain"),
  create("basic5", 1024, false, false, "text/plain"), // HTML content in text/plain
  create("basic5", 1024, false, true, "text/plain"),
  create("base64-1", 1024, false, false, "text/plain"), // base64 text/plain
  create("base64-1", 1024, false, true, "text/plain"),
  create("base64-2", 1024, false, false, "text/html"), // base64 text/html
  create("base64-3", 1024, false, true, "text/html"), // strip out tags here
  create("multipart1", 1024, false, false, "text/plain"), // basic multipart message
  create("multipart1", 1024, false, true, "text/plain"),
  create("multipart2", 1024, false, false, "text/html"), // multipart HTML
  create("multipart3", 1024, false, true, "text/html"),
  create("multipart4", 1024, false, false, "text/plain"), // text with no headers
  create("multipart4", 1024, false, true, "text/plain"),
  create("multipart-base64-1", 1024, false, false, "text/plain"), // base64 encoded text
  create("multipart-base64-1", 1024, false, true, "text/plain"),
  create("multipart-base64-2", 1024, false, false, "text/html"),
  create("multipart-base64-3", 1024, false, true, "text/html"),
  create("multipart-complex1", 1024, false, true, "text/html"), // Things get more complex here
  create("multipart-complex2", 1024, false, false, "text/plain"),
  create("multipart-complex2", 1024, false, true, "text/plain"),
];

function run_test() {
  localAccountUtils.loadLocalMailAccount();
  var folder = localAccountUtils.incomingServer.rootMsgFolder;

  gTestFiles.forEach(function (test) {
    dump("Testing " + test.name + "\n");
    var inFile = do_get_file(kDataRoot + test.name);
    var inStream = Cc[
      "@mozilla.org/network/file-input-stream;1"
    ].createInstance(Ci.nsIFileInputStream);
    inStream.init(inFile, -1, -1, Ci.nsIFileInputStream.CLOSE_ON_EOF);

    // Now get the message body using getMsgTextFromStream
    var contentType = {};
    var body = folder.getMsgTextFromStream(
      inStream,
      "",
      test.bytesToRead,
      65536,
      test.compressQuotes,
      test.stripHTML,
      contentType
    );

    // Now we need to compare the output
    Assert.equal(test.contentType, contentType.value);

    var resultFile = do_get_file(kDataRoot + test.name + ".out");
    var actualBody = mailTestUtils.loadFileToString(resultFile, "UTF-8");
    Assert.equal(body, actualBody);
  });
}