summaryrefslogtreecommitdiffstats
path: root/browser/components/enterprisepolicies/tests/browser/browser_policy_downloads.js
blob: 163745aaaf0a05fd94e60488c9d0ac722e0381d8 (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
/* 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";

add_task(async function test_defaultdownload() {
  await setupPolicyEngineWithJson({
    policies: {
      DefaultDownloadDirectory: "${home}/Downloads",
      PromptForDownloadLocation: false,
    },
  });

  await BrowserTestUtils.withNewTab("about:preferences", async browser => {
    is(
      browser.contentDocument.getElementById("alwaysAsk").disabled,
      true,
      "alwaysAsk should be disabled."
    );
    let home = Services.dirsvc.get("Home", Ci.nsIFile).path;
    is(
      Services.prefs.getStringPref("browser.download.dir"),
      home + "/Downloads",
      "browser.download.dir should be ${home}/Downloads."
    );
    is(
      Services.prefs.getBoolPref("browser.download.useDownloadDir"),
      true,
      "browser.download.useDownloadDir should be true."
    );
    is(
      Services.prefs.prefIsLocked("browser.download.useDownloadDir"),
      true,
      "browser.download.useDownloadDir should be locked."
    );
  });
});

add_task(async function test_download() {
  await setupPolicyEngineWithJson({
    policies: {
      DownloadDirectory: "${home}/Documents",
    },
  });

  await BrowserTestUtils.withNewTab("about:preferences", async browser => {
    is(
      browser.contentDocument.getElementById("alwaysAsk").disabled,
      true,
      "alwaysAsk should be disabled."
    );
    is(
      browser.contentDocument.getElementById("downloadFolder").disabled,
      true,
      "downloadFolder should be disabled."
    );
    is(
      browser.contentDocument.getElementById("chooseFolder").disabled,
      true,
      "chooseFolder should be disabled."
    );
    let home = Services.dirsvc.get("Home", Ci.nsIFile).path;
    is(
      Services.prefs.getStringPref("browser.download.dir"),
      home + "/Documents",
      "browser.download.dir should be ${home}/Documents."
    );
    is(
      Services.prefs.getBoolPref("browser.download.useDownloadDir"),
      true,
      "browser.download.useDownloadDir should be true."
    );
    is(
      Services.prefs.prefIsLocked("browser.download.useDownloadDir"),
      true,
      "browser.download.useDownloadDir should be locked."
    );
  });
});

async function setDownloadDir() {
  let tmpDir = PathUtils.join(
    PathUtils.tempDir,
    "testsavedir" + Math.floor(Math.random() * 2 ** 32)
  );
  // Create this dir if it doesn't exist (ignores existing dirs)
  await IOUtils.makeDirectory(tmpDir);
  registerCleanupFunction(async function () {
    try {
      await IOUtils.remove(tmpDir, { recursive: true });
    } catch (e) {
      console.error(e);
    }
  });
  Services.prefs.setIntPref("browser.download.folderList", 2);
  Services.prefs.setCharPref("browser.download.dir", tmpDir);
  return tmpDir;
}

add_task(async function test_tmpdir_download() {
  await setupPolicyEngineWithJson({
    policies: {
      StartDownloadsInTempDirectory: true,
    },
  });

  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.download.always_ask_before_handling_new_types", true],
      ["browser.helperApps.deleteTempFileOnExit", true],
    ],
  });

  let dlDir = new FileUtils.File(await setDownloadDir());
  registerCleanupFunction(() => {
    Services.prefs.clearUserPref("browser.download.dir");
    Services.prefs.clearUserPref("browser.download.folderList");
  });

  // Wait for the download prompting dialog
  let dialogPromise = BrowserTestUtils.domWindowOpenedAndLoaded(
    null,
    win => win.document.documentURI == UCT_URI
  );

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "https://example.com/browser/browser/components/downloads/test/browser/foo.txt",
      waitForLoad: false,
      waitForStop: true,
    },
    async function () {
      let dialogWin = await dialogPromise;
      let tempFile = dialogWin.dialog.mLauncher.targetFile;
      isnot(
        tempFile.parent.path,
        dlDir.path,
        "Should not have put temp file in the downloads dir."
      );

      dialogWin.close();
    }
  );
});