summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/composition/browser_blockedContent.js
blob: 997c760af07529a94971f91004910275229549c0 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* 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/. */

/**
 * Tests that we do the right thing wrt. blocked resources during composition.
 */

"use strict";

var utils = ChromeUtils.import("resource://testing-common/mozmill/utils.jsm");
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, press_delete, select_click_row } =
  ChromeUtils.import(
    "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
  );
var { wait_for_notification_to_show } = ChromeUtils.import(
  "resource://testing-common/mozmill/NotificationBoxHelpers.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";

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

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);
}

/**
 * Test that accessing file: URLs will block when appropriate, and load
 * the content when appropriate.
 */
add_task(async function test_paste_file_urls() {
  let cwc = open_compose_new_mail();
  setup_msg_contents(
    cwc,
    "someone@example.com",
    "testing html paste",
    "See these images- one broken one not\n"
  );

  const fname = "data/tb-logo.png";
  let file = new FileUtils.File(getTestFilePath(fname));
  let fileHandler = Services.io
    .getProtocolHandler("file")
    .QueryInterface(Ci.nsIFileProtocolHandler);

  let dest = PathUtils.join(
    Services.dirsvc.get("TmpD", Ci.nsIFile).path,
    file.leafName
  );
  let tmpFile;
  let tmpFileURL;
  IOUtils.remove(dest, { ignoreAbsent: true })
    .then(function () {
      return IOUtils.copy(file.path, dest);
    })
    .then(function () {
      return IOUtils.setModificationTime(dest);
    })
    .then(function () {
      tmpFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
      tmpFile.initWithPath(dest);
      Assert.ok(tmpFile.exists(), "tmpFile's not there at " + dest);

      tmpFileURL = fileHandler.getURLSpecFromActualFile(tmpFile);
      putHTMLOnClipboard(
        "<img id='bad-img' src='file://foo/non-existent' alt='bad' /> and " +
          "<img id='tmp-img' src='" +
          tmpFileURL +
          "' alt='tmp' />"
      );

      cwc.window.document.getElementById("messageEditor").focus();
      // Ctrl+V = Paste
      EventUtils.synthesizeKey(
        "v",
        { shiftKey: false, accelKey: true },
        cwc.window
      );
    })
    .catch(function (err) {
      throw new Error("Setting up img file FAILED: " + err);
    });

  // Now wait for the paste, and for the file: based image to get converted
  // to data:.
  utils.waitFor(function () {
    let img = cwc.window.document
      .getElementById("messageEditor")
      .contentDocument.getElementById("tmp-img");
    return img && img.naturalHeight == 84 && img.src.startsWith("data:");
  }, "Timeout waiting for pasted tmp image to be loaded ok");

  // For the non-existent (non-accessible!) image we should get a notification.
  wait_for_notification_to_show(cwc.window, kBoxId, kNotificationId);

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

  await be_in_folder(gOutboxFolder);
  let outMsg = select_click_row(0);
  let outMsgContent = await get_msg_source(outMsg);

  Assert.ok(
    outMsgContent.includes("file://foo/non-existent"),
    "non-existent file not in content=" + outMsgContent
  );

  Assert.ok(
    !outMsgContent.includes(tmpFileURL),
    "tmp file url still in content=" + outMsgContent
  );

  Assert.ok(
    outMsgContent.includes('id="tmp-img" src="cid:'),
    "tmp-img should be cid after send; content=" + outMsgContent
  );

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