summaryrefslogtreecommitdiffstats
path: root/uriloader/exthandler/tests/mochitest/browser_download_urlescape.js
blob: e153b696bf21a1a8ef87037d5de887e390e1cfbd (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_PATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "https://example.com"
);

var MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(window.browsingContext);
registerCleanupFunction(() => MockFilePicker.cleanup());

/**
 * Check downloading files URL-escapes content-disposition
 * information when necessary.
 */
add_task(async function test_check_filename_urlescape() {
  let pendingPromise;
  let pendingTest = "";
  let expectedFileName = "";
  MockFilePicker.showCallback = function (fp) {
    info(`${pendingTest} - Filepicker shown, checking filename`);
    is(
      fp.defaultString,
      expectedFileName,
      `${pendingTest} - Should have escaped filename`
    );
    ok(
      pendingPromise,
      `${pendingTest} - Should have expected this picker open.`
    );
    if (pendingPromise) {
      pendingPromise.resolve();
    }
    return Ci.nsIFilePicker.returnCancel;
  };
  function runTestFor(fileName, selector) {
    return BrowserTestUtils.withNewTab(TEST_PATH + fileName, async browser => {
      expectedFileName = fileName;
      let tabLabel = gBrowser.getTabForBrowser(browser).getAttribute("label");
      ok(
        tabLabel.startsWith(fileName),
        `"${tabLabel}" should have been escaped.`
      );

      pendingTest = "save browser";
      pendingPromise = Promise.withResolvers();
      // First try to save the browser
      saveBrowser(browser);
      await pendingPromise.promise;

      // Next, try the context menu:
      pendingTest = "save from context menu";
      pendingPromise = Promise.withResolvers();
      let menu = document.getElementById("contentAreaContextMenu");
      let menuShown = BrowserTestUtils.waitForEvent(menu, "popupshown");
      BrowserTestUtils.synthesizeMouse(
        selector,
        5,
        5,
        { type: "contextmenu", button: 2 },
        browser
      );
      await menuShown;
      gContextMenu.saveMedia();
      menu.hidePopup();
      await pendingPromise.promise;
      pendingPromise = null;
    });
  }
  await runTestFor("file_with@@funny_name.png", "img");
  await runTestFor("file_with[funny_name.webm", "video");
});