summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/composition/browser_inlineImage.js
blob: 5b26b05ac5594cee9786e39ea30d9f72345ef79c (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
/* 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 sending message with inline image.
 */

var { get_msg_source, open_compose_new_mail, setup_msg_contents } =
  ChromeUtils.import("resource://testing-common/mozmill/ComposeHelpers.jsm");
var {
  be_in_folder,
  get_special_folder,
  get_about_message,
  press_delete,
  select_click_row,
} = ChromeUtils.import(
  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
);
var { plan_for_window_close, wait_for_window_close } = ChromeUtils.import(
  "resource://testing-common/mozmill/WindowHelpers.jsm"
);

var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

var gOutboxFolder;

var kBoxId = "compose-notification-bottom";
var kNotificationId = "blockedContent";

function typedArrayToString(buffer) {
  var string = "";
  for (let i = 0; i < buffer.length; i += 100) {
    string += String.fromCharCode.apply(undefined, buffer.subarray(i, i + 100));
  }
  return string;
}

function putHTMLOnClipboard(html) {
  let trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
    Ci.nsITransferable
  );

  // Register supported data flavors
  trans.init(null);
  trans.addDataFlavor("text/html");

  let wapper = Cc["@mozilla.org/supports-string;1"].createInstance(
    Ci.nsISupportsString
  );
  wapper.data = html;
  trans.setTransferData("text/html", wapper);

  Services.clipboard.setData(trans, null, Ci.nsIClipboard.kGlobalClipboard);
}

add_setup(async function () {
  gOutboxFolder = await get_special_folder(Ci.nsMsgFolderFlags.Queue);
});

/**
 * Tests that sending message with inline image works, and we pick a file name
 * for data uri if needed.
 */
add_task(async function test_send_inline_image() {
  let cwc = open_compose_new_mail();
  setup_msg_contents(
    cwc,
    "someone@example.com",
    "Test sending inline image",
    "The image doesn't display because we changed the data URI\n"
  );

  let fileBuf = await IOUtils.read(getTestFilePath("data/nest.png"));
  let fileContent = btoa(typedArrayToString(fileBuf));
  let dataURI = `data:image/png;base64,${fileContent}`;

  putHTMLOnClipboard(`<img id="inline-img" src="${dataURI}">`);
  cwc.window.document.getElementById("messageEditor").focus();
  // Ctrl+V = Paste
  EventUtils.synthesizeKey(
    "v",
    { shiftKey: false, accelKey: true },
    cwc.window
  );

  plan_for_window_close(cwc);
  cwc.window.goDoCommand("cmd_sendLater");
  wait_for_window_close();

  await be_in_folder(gOutboxFolder);
  let msgLoaded = BrowserTestUtils.waitForEvent(
    get_about_message(),
    "MsgLoaded"
  );
  let outMsg = select_click_row(0);
  await msgLoaded;
  let outMsgContent = await get_msg_source(outMsg);

  ok(
    outMsgContent.includes('id="inline-img" src="cid:'),
    "inline-img should be cid after send"
  );
  ok(
    /Content-Type: image\/png;\s* name="\w{16}.png"/.test(outMsgContent),
    `file name should have 16 characters: ${outMsgContent}`
  );

  press_delete(); // Delete the msg from Outbox.
});