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
|
<!doctype html>
<html>
<head>
<title>Test downloads.download() saveAs option</title>
<script src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script src="chrome_head.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";
ChromeUtils.import("resource://gre/modules/FileUtils.jsm");
const PROMPTLESS_DOWNLOAD_PREF = "browser.download.useDownloadDir";
// We need to be able to distinguish files downloaded by the file picker from
// files downloaded without it.
let pickerDir;
let defaultDir;
add_task(async function setup() {
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}`);
defaultDir = downloadDir.clone();
defaultDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
info(`Using default download directory ${defaultDir.path}`);
isnot(pickerDir.path, defaultDir.path,
"Should be able to distinguish between files saved with or without the file picker");
await SpecialPowers.pushPrefEnv({"set": [
["browser.download.folderList", 2],
["browser.download.dir", defaultDir.path],
]});
SimpleTest.registerCleanupFunction(async () => {
await SpecialPowers.popPrefEnv();
pickerDir.remove(true);
defaultDir.remove(true);
});
});
add_task(async function test_downloads_saveAs() {
const pickerFile = pickerDir.clone();
pickerFile.append("file_download.txt");
const defaultFile = defaultDir.clone();
defaultFile.append("file_download.txt");
const {MockFilePicker} = SpecialPowers;
MockFilePicker.init(window);
MockFilePicker.showCallback = fp => {
// On picker 'show' event, choose the filename that was set as the default
// and append it to the picker's download directory
let file = pickerDir.clone();
file.append(fp.defaultString);
MockFilePicker.setFiles([file]);
};
function background() {
const url = URL.createObjectURL(new Blob(["file content"]));
browser.test.onMessage.addListener(async (filename, saveAs) => {
try {
let options = {
url,
filename,
};
// 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, manifest: {permissions: ["downloads"]}};
const extension = ExtensionTestUtils.loadExtension(manifest);
await extension.startup();
await extension.awaitMessage("ready");
async function testExpectFilePicker(saveAs) {
ok(!pickerFile.exists(), "the file should have been cleaned up properly previously");
MockFilePicker.returnValue = MockFilePicker.returnOK;
extension.sendMessage("file_download.txt", saveAs);
let result = await extension.awaitMessage("done");
ok(result.ok, `downloads.download() works with saveAs=${saveAs}`);
ok(pickerFile.exists(), "the file exists.");
is(pickerFile.fileSize, 12, "downloaded file is the correct size");
pickerFile.remove(false);
// Test the user canceling the save dialog.
MockFilePicker.returnValue = MockFilePicker.returnCancel;
extension.sendMessage("file_download.txt", saveAs);
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(!pickerFile.exists(), "file was not downloaded");
}
async function testNoFilePicker(saveAs) {
ok(!defaultFile.exists(), "the file should have been cleaned up properly previously");
extension.sendMessage("file_download.txt", saveAs);
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 saveAs = true;
await testExpectFilePicker(saveAs);
info("Testing that saveAs=false does not use the file picker");
saveAs = false;
await testNoFilePicker(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`);
saveAs = undefined;
await SpecialPowers.pushPrefEnv({"set": [
[PROMPTLESS_DOWNLOAD_PREF, false],
]});
await testExpectFilePicker(saveAs);
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(saveAs);
await extension.unload();
MockFilePicker.cleanup();
});
</script>
</body>
</html>
|