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

"use strict";

const mimeSvc = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
const handlerSvc = Cc["@mozilla.org/uriloader/handler-service;1"].getService(
  Ci.nsIHandlerService
);
let mimeInfo = mimeSvc.getFromTypeAndExtension("application/pdf", "pdf");

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({ set: [["pdfjs.disabled", true]] });

  let oldAsk = mimeInfo.alwaysAskBeforeHandling;
  let oldPreferredAction = mimeInfo.preferredAction;
  let oldPreferredApp = mimeInfo.preferredApplicationHandler;
  registerCleanupFunction(() => {
    mimeInfo.preferredApplicationHandler = oldPreferredApp;
    mimeInfo.preferredAction = oldPreferredAction;
    mimeInfo.alwaysAskBeforeHandling = oldAsk;
    handlerSvc.store(mimeInfo);
  });

  if (!mimeInfo.preferredApplicationHandler) {
    let handlerApp = Cc[
      "@mozilla.org/uriloader/local-handler-app;1"
    ].createInstance(Ci.nsILocalHandlerApp);
    handlerApp.executable = Services.dirsvc.get("TmpD", Ci.nsIFile);
    handlerApp.executable.append("foopydoo.exe");
    mimeInfo.possibleApplicationHandlers.appendElement(handlerApp);
    mimeInfo.preferredApplicationHandler = handlerApp;
  }
});

add_task(async function open_from_dialog() {
  // Force PDFs to prompt:
  mimeInfo.preferredAction = mimeInfo.useHelperApp;
  mimeInfo.alwaysAskBeforeHandling = true;
  handlerSvc.store(mimeInfo);

  let openingPromise = TestUtils.topicObserved(
    "test-only-opening-downloaded-file",
    (subject, data) => {
      subject.QueryInterface(Ci.nsISupportsPRBool);
      // Block opening the file:
      subject.data = false;
      return true;
    }
  );

  let dialogWindowPromise = BrowserTestUtils.domWindowOpenedAndLoaded();
  let openedFile = getChromeDir(getResolvedURI(gTestPath));
  openedFile.append("file_pdf_binary_octet_stream.pdf");
  let expectedPath = openedFile.isSymlink()
    ? openedFile.target
    : openedFile.path;
  let loadingTab = BrowserTestUtils.addTab(gBrowser, expectedPath);

  let dialogWindow = await dialogWindowPromise;
  is(
    dialogWindow.location.href,
    "chrome://mozapps/content/downloads/unknownContentType.xhtml",
    "Should have seen the unknown content dialogWindow."
  );

  let doc = dialogWindow.document;

  // Select the 'open' entry.
  doc.querySelector("#open").click();
  let dialog = doc.querySelector("#unknownContentType");
  dialog.getButton("accept").removeAttribute("disabled");
  dialog.acceptDialog();
  let [, openedPath] = await openingPromise;
  is(
    openedPath,
    expectedPath,
    "Should have opened file directly (not created a copy)."
  );
  if (openedPath != expectedPath) {
    await IOUtils.setPermissions(openedPath, 0o666);
    await IOUtils.remove(openedPath);
  }
  BrowserTestUtils.removeTab(loadingTab);
});

add_task(async function open_directly() {
  // Force PDFs to open immediately:
  mimeInfo.preferredAction = mimeInfo.useHelperApp;
  mimeInfo.alwaysAskBeforeHandling = false;
  handlerSvc.store(mimeInfo);

  let openingPromise = TestUtils.topicObserved(
    "test-only-opening-downloaded-file",
    (subject, data) => {
      subject.QueryInterface(Ci.nsISupportsPRBool);
      // Block opening the file:
      subject.data = false;
      return true;
    }
  );

  let openedFile = getChromeDir(getResolvedURI(gTestPath));
  openedFile.append("file_pdf_binary_octet_stream.pdf");
  let expectedPath = openedFile.isSymlink()
    ? openedFile.target
    : openedFile.path;
  let loadingTab = BrowserTestUtils.addTab(gBrowser, expectedPath);

  let [, openedPath] = await openingPromise;
  is(
    openedPath,
    expectedPath,
    "Should have opened file directly (not created a copy)."
  );
  if (openedPath != expectedPath) {
    await IOUtils.setPermissions(openedPath, 0o666);
    await IOUtils.remove(openedPath);
  }
  BrowserTestUtils.removeTab(loadingTab);
});