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

"use strict";

let { FileTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/FileTestUtils.sys.mjs"
);

const ROOT = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "https://example.com"
);
const TEST_FILE = ROOT + "download.bin";

add_task(async function test_check_download_dir() {
  // Force XDG dir to somewhere that has no config files, causing lookups of the
  // system download dir to fail:
  let newXDGRoot = FileTestUtils.getTempFile("xdgstuff");
  newXDGRoot.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
  let oldXDG = Services.env.exists("XDG_CONFIG_HOME")
    ? Services.env.get("XDG_CONFIG_HOME")
    : "";
  registerCleanupFunction(() => Services.env.set("XDG_CONFIG_HOME", oldXDG));
  Services.env.set("XDG_CONFIG_HOME", newXDGRoot.path + "/");

  let propBundle = Services.strings.createBundle(
    "chrome://mozapps/locale/downloads/downloads.properties"
  );
  let dlRoot = PathUtils.join(
    Services.dirsvc.get("Home", Ci.nsIFile).path,
    propBundle.GetStringFromName("downloadsFolder")
  );

  // Check lookups fail:
  Assert.throws(
    () => Services.dirsvc.get("DfltDwnld", Ci.nsIFile),
    /NS_ERROR_FAILURE/,
    "Should throw when asking for downloads dir."
  );

  await SpecialPowers.pushPrefEnv({
    set: [
      // Avoid opening dialogs
      ["browser.download.always_ask_before_handling_new_types", false],
      // Switch back to default OS downloads dir (changed in head.js):
      ["browser.download.folderList", 1],
    ],
  });

  let publicList = await Downloads.getList(Downloads.PUBLIC);
  let downloadFinished = promiseDownloadFinished(publicList);
  let tab = BrowserTestUtils.addTab(gBrowser, TEST_FILE);
  let dl = await downloadFinished;
  ok(dl.succeeded, "Download should succeed.");
  Assert.stringContains(
    dl.target.path,
    dlRoot,
    "Should store download under DL folder root."
  );
  let dlKids = await IOUtils.getChildren(dlRoot);
  ok(
    dlKids.includes(dl.target.path),
    "Download should be a direct child of the DL folder."
  );
  await IOUtils.remove(dl.target.path);

  BrowserTestUtils.removeTab(tab);

  // Download a second file to make sure we're not continuously adding filenames
  // onto the download folder path.
  downloadFinished = promiseDownloadFinished(publicList);
  tab = BrowserTestUtils.addTab(gBrowser, TEST_FILE);
  dl = await downloadFinished;
  Assert.stringContains(
    dl.target.path,
    dlRoot,
    "Second download should store download under new DL folder root."
  );
  dlKids = await IOUtils.getChildren(dlRoot);
  ok(
    dlKids.includes(dl.target.path),
    "Second download should be a direct child of the new DL folder."
  );
  BrowserTestUtils.removeTab(tab);
  await IOUtils.remove(dl.target.path);

  await publicList.removeFinished();
  await IOUtils.remove(newXDGRoot.path);
});