summaryrefslogtreecommitdiffstats
path: root/browser/components/downloads/test/browser/browser_download_starts_in_tmp.js
blob: 1301e8fa1be77ba02fd7fbea22ebe86824c4ede4 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const UCT_URI = "chrome://mozapps/content/downloads/unknownContentType.xhtml";
// Need to start the server before `httpUrl` works.
startServer();
const DOWNLOAD_URL = httpUrl("interruptible.txt");

let gDownloadDir;

let gExternalHelperAppService = Cc[
  "@mozilla.org/uriloader/external-helper-app-service;1"
].getService(Ci.nsIExternalHelperAppService);
gExternalHelperAppService.QueryInterface(Ci.nsIObserver);

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.download.start_downloads_in_tmp_dir", true],
      ["browser.helperApps.deleteTempFileOnExit", true],
    ],
  });
  registerCleanupFunction(task_resetState);
  gDownloadDir = new FileUtils.File(await setDownloadDir());
  registerCleanupFunction(() => {
    Services.prefs.clearUserPref("browser.download.dir");
    Services.prefs.clearUserPref("browser.download.folderList");
  });
});

add_task(async function test_download_asking_starts_in_tmp() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.download.always_ask_before_handling_new_types", true]],
  });
  let list = await Downloads.getList(Downloads.PUBLIC);
  let downloadStarted = new Promise(resolve => {
    let view = {
      onDownloadAdded(download) {
        list.removeView(view);
        resolve(download);
      },
    };
    list.addView(view);
  });
  // Wait for the download prompting dialog
  let dialogPromise = BrowserTestUtils.domWindowOpenedAndLoaded(
    null,
    win => win.document.documentURI == UCT_URI
  );
  serveInterruptibleAsDownload();
  mustInterruptResponses();
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: DOWNLOAD_URL,
      waitForLoad: false,
      waitForStop: true,
    },
    async function () {
      let dialogWin = await dialogPromise;
      let tempFile = dialogWin.dialog.mLauncher.targetFile;
      ok(
        !tempFile.parent.equals(gDownloadDir),
        "Should not have put temp file in the downloads dir."
      );

      let dialogEl = dialogWin.document.querySelector("dialog");
      dialogEl.getButton("accept").disabled = false;
      dialogEl.acceptDialog();
      let download = await downloadStarted;
      is(
        PathUtils.parent(download.target.path),
        gDownloadDir.path,
        "Should have put final file in the downloads dir."
      );
      continueResponses();
      await download.whenSucceeded();
      await IOUtils.remove(download.target.path);
    }
  );
  await list.removeFinished();
});

add_task(async function test_download_asking_and_opening_opens_from_tmp() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.download.always_ask_before_handling_new_types", true]],
  });
  let list = await Downloads.getList(Downloads.PUBLIC);
  let downloadStarted = new Promise(resolve => {
    let view = {
      onDownloadAdded(download) {
        list.removeView(view);
        resolve(download);
      },
    };
    list.addView(view);
  });
  // Wait for the download prompting dialog
  let dialogPromise = BrowserTestUtils.domWindowOpenedAndLoaded(
    null,
    win => win.document.documentURI == UCT_URI
  );

  let oldLaunchFile = DownloadIntegration.launchFile;
  let promiseLaunchFileCalled = new Promise(resolve => {
    DownloadIntegration.launchFile = file => {
      ok(true, "The file should be launched with an external application");
      resolve(file);
      DownloadIntegration.launchFile = oldLaunchFile;
    };
  });
  registerCleanupFunction(() => {
    DownloadIntegration.launchFile = oldLaunchFile;
  });

  serveInterruptibleAsDownload();
  mustInterruptResponses();
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: DOWNLOAD_URL,
      waitForLoad: false,
      waitForStop: true,
    },
    async function () {
      let dialogWin = await dialogPromise;
      let tempFile = dialogWin.dialog.mLauncher.targetFile;
      ok(
        !tempFile.parent.equals(gDownloadDir),
        "Should not have put temp file in the downloads dir."
      );

      dialogWin.document.getElementById("open").click();
      let dialogEl = dialogWin.document.querySelector("dialog");
      dialogEl.getButton("accept").disabled = false;
      dialogEl.acceptDialog();
      let download = await downloadStarted;
      isnot(
        PathUtils.parent(download.target.path),
        gDownloadDir.path,
        "Should not have put final file in the downloads dir when it's supposed to be automatically opened."
      );
      continueResponses();
      await download.whenSucceeded();
      await download.refresh();
      isnot(
        PathUtils.parent(download.target.path),
        gDownloadDir.path,
        "Once finished the download should not be in the downloads dir when it's supposed to be automatically opened."
      );
      let file = await promiseLaunchFileCalled;
      ok(
        !file.parent.equals(gDownloadDir),
        "Should not have put opened file in the downloads dir."
      );

      // Pretend that we've quit so we wipe all the files:
      gExternalHelperAppService.observe(null, "profile-before-change", "");
      // Now the file should go away, but that's async...

      let f = new FileUtils.File(download.target.path);
      await TestUtils.waitForCondition(
        () => !f.exists(),
        "Temp file should be removed",
        500
      ).catch(err => ok(false, err));
      ok(!f.exists(), "Temp file should be removed.");

      await IOUtils.remove(download.target.path);
    }
  );
  await list.removeFinished();
});

// Check that if we open the file automatically, it opens from the temp dir.
add_task(async function test_download_automatically_opened_from_tmp() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.download.always_ask_before_handling_new_types", false]],
  });
  serveInterruptibleAsDownload();
  mustInterruptResponses();

  let list = await Downloads.getList(Downloads.PUBLIC);
  let downloadStarted = new Promise(resolve => {
    let view = {
      onDownloadAdded(download) {
        list.removeView(view);
        resolve(download);
      },
    };
    list.addView(view);
  });

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

  let txtHandlerInfo = mimeSvc.getFromTypeAndExtension("text/plain", "txt");
  txtHandlerInfo.preferredAction = Ci.nsIHandlerInfo.useSystemDefault;
  txtHandlerInfo.alwaysAskBeforeHandling = false;
  handlerSvc.store(txtHandlerInfo);
  registerCleanupFunction(() => handlerSvc.remove(txtHandlerInfo));

  let oldLaunchFile = DownloadIntegration.launchFile;
  let promiseLaunchFileCalled = new Promise(resolve => {
    DownloadIntegration.launchFile = file => {
      ok(true, "The file should be launched with an external application");
      resolve(file);
      DownloadIntegration.launchFile = oldLaunchFile;
    };
  });
  registerCleanupFunction(() => {
    DownloadIntegration.launchFile = oldLaunchFile;
  });

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: DOWNLOAD_URL,
      waitForLoad: false,
      waitForStop: true,
    },
    async function () {
      let download = await downloadStarted;
      isnot(
        PathUtils.parent(download.target.partFilePath),
        gDownloadDir.path,
        "Should not start the download in the downloads dir."
      );
      continueResponses();
      await download.whenSucceeded();
      isnot(
        PathUtils.parent(download.target.path),
        gDownloadDir.path,
        "Should not have put final file in the downloads dir."
      );
      let file = await promiseLaunchFileCalled;
      ok(
        !file.parent.equals(gDownloadDir),
        "Should not have put opened file in the downloads dir."
      );

      // Pretend that we've quit so we wipe all the files:
      gExternalHelperAppService.observe(null, "profile-before-change", "");
      // Now the file should go away, but that's async...

      let f = new FileUtils.File(download.target.path);
      await TestUtils.waitForCondition(
        () => !f.exists(),
        "Temp file should be removed",
        500
      ).catch(err => ok(false, err));
      ok(!f.exists(), "Temp file should be removed.");

      await IOUtils.remove(download.target.path);
    }
  );

  handlerSvc.remove(txtHandlerInfo);
  await list.removeFinished();
});