summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/downloads/tests/browser/browser_save_wrongextension.js
blob: ba1b6cde5cc4c296c0ac928f0e28b16bfe79e432 (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
/* 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/. */

"use strict";

let url =
  "data:text/html,<a id='link' href='http://localhost:8000/thefile.js'>Link</a>";

let MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(window);

let httpServer = null;

add_task(async function test() {
  const { HttpServer } = ChromeUtils.import(
    "resource://testing-common/httpd.js"
  );

  httpServer = new HttpServer();
  httpServer.start(8000);

  function handleRequest(aRequest, aResponse) {
    aResponse.setStatusLine(aRequest.httpVersion, 200);
    aResponse.setHeader("Content-Type", "text/plain");
    aResponse.write("Some Text");
  }
  httpServer.registerPathHandler("/thefile.js", handleRequest);

  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, url);

  let popupShownPromise = BrowserTestUtils.waitForEvent(document, "popupshown");

  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#link",
    { type: "contextmenu", button: 2 },
    gBrowser.selectedBrowser
  );
  await popupShownPromise;

  let tempDir = createTemporarySaveDirectory();
  let destFile;

  MockFilePicker.displayDirectory = tempDir;
  MockFilePicker.showCallback = fp => {
    let fileName = fp.defaultString;
    destFile = tempDir.clone();
    destFile.append(fileName);

    MockFilePicker.setFiles([destFile]);
    MockFilePicker.filterIndex = 1; // kSaveAsType_URL
  };

  let transferCompletePromise = new Promise(resolve => {
    mockTransferCallback = resolve;
    mockTransferRegisterer.register();
  });

  registerCleanupFunction(function () {
    mockTransferRegisterer.unregister();
    tempDir.remove(true);
  });

  document.getElementById("context-savelink").doCommand();

  let contextMenu = document.getElementById("contentAreaContextMenu");
  let popupHiddenPromise = BrowserTestUtils.waitForEvent(
    document,
    "popuphidden"
  );
  contextMenu.hidePopup();
  await popupHiddenPromise;

  await transferCompletePromise;

  is(destFile.leafName, "thefile.js", "filename extension is not modified");

  BrowserTestUtils.removeTab(tab);
});

add_task(async () => {
  MockFilePicker.cleanup();
  await new Promise(resolve => httpServer.stop(resolve));
});

Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js",
  this
);

function createTemporarySaveDirectory() {
  let saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
  saveDir.append("testsavedir");
  if (!saveDir.exists()) {
    saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
  }
  return saveDir;
}