| 12
 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
 
 | <!doctype html>
<html>
<head>
  <title>Test downloads.download() saveAs option</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script src="head.js"></script>
  <link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="text/javascript">
"use strict";
const {FileUtils} = ChromeUtils.importESModule(
  "resource://gre/modules/FileUtils.sys.mjs"
);
const PROMPTLESS_DOWNLOAD_PREF = "browser.download.useDownloadDir";
const DOWNLOAD_FILENAME = "file_download.nonext.txt";
const DEFAULT_SUBDIR = "subdir";
// We need to be able to distinguish files downloaded by the file picker from
// files downloaded without it.
let pickerDir;
let pbPickerDir; // for incognito downloads
let defaultDir;
add_task(async function setup() {
  // Reset DownloadLastDir preferences in case other tests set them.
  SpecialPowers.Services.obs.notifyObservers(
    null,
    "browser:purge-session-history"
  );
  // Set up temporary directories.
  let downloadDir = FileUtils.getDir("TmpD", ["downloads"]);
  pickerDir = downloadDir.clone();
  pickerDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
  info(`Using file picker download directory ${pickerDir.path}`);
  pbPickerDir = downloadDir.clone();
  pbPickerDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
  info(`Using private browsing file picker download directory ${pbPickerDir.path}`);
  defaultDir = downloadDir.clone();
  defaultDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
  info(`Using default download directory ${defaultDir.path}`);
  let subDir = defaultDir.clone();
  subDir.append(DEFAULT_SUBDIR);
  subDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
  isnot(pickerDir.path, defaultDir.path,
        "Should be able to distinguish between files saved with or without the file picker");
  isnot(pickerDir.path, pbPickerDir.path,
        "Should be able to distinguish between files saved in and out of private browsing mode");
  await SpecialPowers.pushPrefEnv({"set": [
    ["browser.download.folderList", 2],
    ["browser.download.dir", defaultDir.path],
  ]});
  SimpleTest.registerCleanupFunction(async () => {
    await SpecialPowers.popPrefEnv();
    pickerDir.remove(true);
    pbPickerDir.remove(true);
    defaultDir.remove(true); // This also removes DEFAULT_SUBDIR.
  });
});
add_task(async function test_downloads_saveAs() {
  const pickerFile = pickerDir.clone();
  pickerFile.append(DOWNLOAD_FILENAME);
  const pbPickerFile = pbPickerDir.clone();
  pbPickerFile.append(DOWNLOAD_FILENAME);
  const defaultFile = defaultDir.clone();
  defaultFile.append(DOWNLOAD_FILENAME);
  const {MockFilePicker} = SpecialPowers;
  MockFilePicker.init(SpecialPowers.wrap(window).browsingContext);
  function mockFilePickerCallback(expectedStartingDir, pickedFile) {
    return fp => {
      // Assert that the downloads API correctly sets the starting directory.
      ok(fp.displayDirectory.equals(expectedStartingDir), "Got the expected FilePicker displayDirectory");
      // Assert that the downloads API configures both default properties.
      is(fp.defaultString, DOWNLOAD_FILENAME, "Got the expected FilePicker defaultString");
      is(fp.defaultExtension, "txt", "Got the expected FilePicker defaultExtension");
      MockFilePicker.setFiles([pickedFile]);
    };
  }
  function background() {
    const url = URL.createObjectURL(new Blob(["file content"]));
    browser.test.onMessage.addListener(async (filename, saveAs, isPrivate) => {
      try {
        let options = {
          url,
          filename,
          incognito: isPrivate,
        };
        // Only define the saveAs option if the argument was actually set
        if (saveAs !== undefined) {
          options.saveAs = saveAs;
        }
        let id = await browser.downloads.download(options);
        browser.downloads.onChanged.addListener(delta => {
          if (delta.id == id && delta.state.current === "complete") {
            browser.test.sendMessage("done", {ok: true, id});
          }
        });
      } catch ({message}) {
        browser.test.sendMessage("done", {ok: false, message});
      }
    });
    browser.test.sendMessage("ready");
  }
  const manifest = {
    background,
    incognitoOverride: "spanning",
    manifest: {permissions: ["downloads"]},
  };
  const extension = ExtensionTestUtils.loadExtension(manifest);
  await extension.startup();
  await extension.awaitMessage("ready");
  // options should have the following properties:
  //   saveAs (Boolean or undefined)
  //   isPrivate (Boolean)
  //   fileName (string)
  //   expectedStartingDir (nsIFile)
  //   destinationFile (nsIFile)
  async function testExpectFilePicker(options) {
    ok(!options.destinationFile.exists(), "the file should have been cleaned up properly previously");
    MockFilePicker.showCallback = mockFilePickerCallback(
      options.expectedStartingDir,
      options.destinationFile
    );
    MockFilePicker.returnValue = MockFilePicker.returnOK;
    extension.sendMessage(options.fileName, options.saveAs, options.isPrivate);
    let result = await extension.awaitMessage("done");
    ok(result.ok, `downloads.download() works with saveAs=${options.saveAs}`);
    ok(options.destinationFile.exists(), "the file exists.");
    is(options.destinationFile.fileSize, 12, "downloaded file is the correct size");
    options.destinationFile.remove(false);
    MockFilePicker.reset();
    // Test the user canceling the save dialog.
    MockFilePicker.returnValue = MockFilePicker.returnCancel;
    extension.sendMessage(options.fileName, options.saveAs, options.isPrivate);
    result = await extension.awaitMessage("done");
    ok(!result.ok, "download rejected if the user cancels the dialog");
    is(result.message, "Download canceled by the user", "with the correct message");
    ok(!options.destinationFile.exists(), "file was not downloaded");
    MockFilePicker.reset();
  }
  async function testNoFilePicker(saveAs) {
    ok(!defaultFile.exists(), "the file should have been cleaned up properly previously");
    extension.sendMessage(DOWNLOAD_FILENAME, saveAs, false);
    let result = await extension.awaitMessage("done");
    ok(result.ok, `downloads.download() works with saveAs=${saveAs}`);
    ok(defaultFile.exists(), "the file exists.");
    is(defaultFile.fileSize, 12, "downloaded file is the correct size");
    defaultFile.remove(false);
  }
  info("Testing that saveAs=true uses the file picker as expected");
  let expectedStartingDir = defaultDir;
  let fpOptions = {
    saveAs: true,
    isPrivate: false,
    fileName: DOWNLOAD_FILENAME,
    expectedStartingDir: expectedStartingDir,
    destinationFile: pickerFile,
  };
  await testExpectFilePicker(fpOptions);
  info("Testing that saveas=true reuses last file picker directory");
  fpOptions.expectedStartingDir = pickerDir;
  await testExpectFilePicker(fpOptions);
  info("Testing that saveAs=true in PB reuses last directory");
  let nonPBStartingDir = fpOptions.expectedStartingDir;
  fpOptions.isPrivate = true;
  fpOptions.destinationFile = pbPickerFile;
  await testExpectFilePicker(fpOptions);
  info("Testing that saveAs=true in PB uses a separate last directory");
  fpOptions.expectedStartingDir = pbPickerDir;
  await testExpectFilePicker(fpOptions);
  info("Testing that saveAs=true in Permanent PB mode ignores the incognito option");
  await SpecialPowers.pushPrefEnv({
    set: [["browser.privatebrowsing.autostart", true]],
  });
  fpOptions.isPrivate = false;
  fpOptions.expectedStartingDir = pbPickerDir;
  await testExpectFilePicker(fpOptions);
  info("Testing that saveas=true reuses the non-PB last directory after private download");
  await SpecialPowers.popPrefEnv();
  fpOptions.isPrivate = false;
  fpOptions.expectedStartingDir = nonPBStartingDir;
  fpOptions.destinationFile = pickerFile;
  await testExpectFilePicker(fpOptions);
  info("Testing that saveAs=true does not reuse last directory when filename contains a path separator");
  fpOptions.fileName = DEFAULT_SUBDIR + "/" + DOWNLOAD_FILENAME;
  let destinationFile = defaultDir.clone();
  destinationFile.append(DEFAULT_SUBDIR);
  fpOptions.expectedStartingDir = destinationFile.clone();
  destinationFile.append(DOWNLOAD_FILENAME);
  fpOptions.destinationFile = destinationFile;
  await testExpectFilePicker(fpOptions);
  info("Testing that saveAs=false does not use the file picker");
  fpOptions.saveAs = false;
  await testNoFilePicker(fpOptions.saveAs);
  // When saveAs is not set, the behavior should be determined by the Firefox
  // pref that normally determines whether the "Save As" prompt should be
  // displayed.
  info(`Testing that the file picker is used when saveAs is not specified ` +
       `but ${PROMPTLESS_DOWNLOAD_PREF} is disabled`);
  fpOptions.saveAs = undefined;
  await SpecialPowers.pushPrefEnv({"set": [
    [PROMPTLESS_DOWNLOAD_PREF, false],
  ]});
  await testExpectFilePicker(fpOptions);
  info(`Testing that the file picker is NOT used when saveAs is not ` +
       `specified but ${PROMPTLESS_DOWNLOAD_PREF} is enabled`);
  await SpecialPowers.popPrefEnv();
  await SpecialPowers.pushPrefEnv({"set": [
    [PROMPTLESS_DOWNLOAD_PREF, true],
  ]});
  await testNoFilePicker(fpOptions.saveAs);
  await extension.unload();
  MockFilePicker.cleanup();
});
</script>
</body>
</html>
 |