summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/imap/test/unit/test_bug460636.js
blob: 6cb8b600560d2e929cf0b7f4fcfb0ad9ddb42002 (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
/*
 * Test bug 460636 - nsMsgSaveAsListener sometimes inserts extra LF characters
 */

var { PromiseTestUtils } = ChromeUtils.import(
  "resource://testing-common/mailnews/PromiseTestUtils.jsm"
);

var gSavedMsgFile;

var gIMAPService = Cc[
  "@mozilla.org/messenger/messageservice;1?type=imap"
].getService(Ci.nsIMsgMessageService);

var gFileName = "bug460636";
var gMsgFile = do_get_file("../../../data/" + gFileName);

add_task(async function run_the_test() {
  await setup();
  await checkSavedMessage();
  teardown();
});

async function setup() {
  setupIMAPPump();

  // Ok, prelude done. Read the original message from disk
  // (through a file URI), and add it to the Inbox.
  var msgfileuri = Services.io
    .newFileURI(gMsgFile)
    .QueryInterface(Ci.nsIFileURL);

  IMAPPump.mailbox.addMessage(
    new ImapMessage(msgfileuri.spec, IMAPPump.mailbox.uidnext++, [])
  );
  let promiseUrlListener = new PromiseTestUtils.PromiseUrlListener();
  IMAPPump.inbox.updateFolderWithListener(null, promiseUrlListener);
  await promiseUrlListener.promise;

  // Save the message to a local file. IMapMD corresponds to
  // <profile_dir>/mailtest/ImapMail (where fakeserver puts the IMAP mailbox
  // files). If we pass the test, we'll remove the file afterwards
  // (cf. UrlListener), otherwise it's kept in IMapMD.
  gSavedMsgFile = Services.dirsvc.get("IMapMD", Ci.nsIFile);
  gSavedMsgFile.append(gFileName + ".eml");

  // From nsIMsgMessageService.idl:
  // void SaveMessageToDisk(in string aMessageURI, in nsIFile aFile,
  //                        in boolean aGenerateDummyEnvelope,
  //                        in nsIUrlListener aUrlListener, out nsIURI aURL,
  //                        in boolean canonicalLineEnding,
  //                        in nsIMsgWindow aMsgWindow);
  // Enforcing canonicalLineEnding (i.e., CRLF) makes sure that the
  let promiseUrlListener2 = new PromiseTestUtils.PromiseUrlListener();
  gIMAPService.SaveMessageToDisk(
    "imap-message://user@localhost/INBOX#" + (IMAPPump.mailbox.uidnext - 1),
    gSavedMsgFile,
    false,
    promiseUrlListener2,
    {},
    true,
    null
  );
  await promiseUrlListener2.promise;
}

async function checkSavedMessage() {
  Assert.equal(
    await IOUtils.readUTF8(gMsgFile.path),
    await IOUtils.readUTF8(gSavedMsgFile.path)
  );
}

function teardown() {
  try {
    gSavedMsgFile.remove(false);
  } catch (ex) {
    dump(ex);
    do_throw(ex);
  }
  teardownIMAPPump();
}