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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
<!DOCTYPE HTML>
<html>
<head>
<title>Test for webkitdirectory and webkitRelativePath</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<input id="inputFileWebkitDirectory" type="file" webkitdirectory></input>
<input id="inputFileWebkitFile" type="file"></input>
<input id="inputFileDirectoryChange" type="file" webkitdirectory></input>
<script type="application/javascript">
const { AppConstants } = SpecialPowers.ChromeUtils.importESModule(
"resource://gre/modules/AppConstants.sys.mjs"
);
let promptHandler;
function waitForEvent(element, eventName) {
return new Promise(function(resolve) {
element.addEventListener(eventName, e => resolve(e.detail), { once: true });
});
}
function waitForPromptHandled() {
if (AppConstants.platform === "android") {
return new Promise(resolve => {
let MockPromptCollection = SpecialPowers.MockPromptCollection;
MockPromptCollection.showConfirmFolderUploadCallback = function() {
resolve();
return true;
};
});
}
return new Promise(resolve => promptHandler.addMessageListener("promptAccepted", resolve));
}
// Populate the given input type=file `aInputFile`'s `files` attribute by:
// - loading `script_fileList.js` in the parent process
// - telling it to generate the "test" template directory pattern which will
// create "foo.txt", "subdir/bar.txt", and if symlinks are available on the
// platform, "symlink.txt" which will be a symlink to "foo.txt". (Note that
// we explicitly expect the symlink to be filtered out if generated, and
// during the enhancement of the test we verified the file was created on
// linux by running the test before fixing the GetFilesHelper logic to filter
// the symlink out and verifying the subsequent `test_fileList` check failed.)
// - Triggering the mock file picker with the base directory of the "test"
// template directory.
//
// It's expected that `test_fileList` will be used after this step completes in
// order to validate the results.
function populateInputFile(aInputFile) {
var url = SimpleTest.getTestFileURL("script_fileList.js");
var script = SpecialPowers.loadChromeScript(url);
var MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(SpecialPowers.wrap(window).browsingContext);
async function onOpened(message) {
MockFilePicker.useDirectory(message.dir);
let input = document.getElementById(aInputFile);
input.setAttribute("data-name", message.name);
let promptHandled = waitForPromptHandled();
let changeEvent = waitForEvent(input, "change");
SpecialPowers.wrap(document).notifyUserGestureActivation();
input.click();
await promptHandled;
await changeEvent;
MockFilePicker.cleanup();
script.destroy();
next();
}
script.addMessageListener("dir.opened", onOpened);
script.sendAsyncMessage("dir.open", { path: "test" });
}
function checkFile(file, fileList, dirName) {
for (var i = 0; i < fileList.length; ++i) {
ok(fileList[i] instanceof File, "We want just files.");
if (fileList[i].name == file.name) {
is(fileList[i].webkitRelativePath, dirName + file.path, "Path matches");
return;
}
}
ok(false, "File not found.");
}
// Validate the contents of the given input type=file `aInputFile`'s' `files`
// property against the expected list of files `aWhat`.
function test_fileList(aInputFile, aWhat) {
var input = document.getElementById(aInputFile);
var fileList = input.files;
if (aWhat == null) {
is(fileList, null, "We want a null fileList for " + aInputFile);
next();
return;
}
is(fileList.length, aWhat.length, "We want just " + aWhat.length + " elements for " + aInputFile);
for (var i = 0; i < aWhat.length; ++i) {
checkFile(aWhat[i], fileList, input.dataset.name);
}
next();
}
// Verify that we can explicitly select a symlink and it will not be filtered
// out. This is really a verification that GetFileHelper's file-handling logic
// https://searchfox.org/mozilla-central/rev/065102493dfc49234120c37fc6a334a5b1d86d9e/dom/filesystem/GetFilesHelper.cpp#81-86
// does not proactively take an action to filter out a selected symlink.
//
// This is a glass box test that is not entirely realistic for our actual system
// file pickers but does reflect what will happen in the drag-and-drop case
// for `HTMLInputElement::MozSetDndFilesAndDirectories` and this helps ensure
// that future implementation changes will behave as expected. Specifically,
// the presence of webkitdirectory will result in the file picker using
// `modeGetFolder` which will only allow selection of a directory and forbid
// file selection.
//
// This test explicitly does not validate HTMLInputElement's non-webkitdirectory
// file selection mechanism because it does not involve GetFileHelper.
async function test_individualSymlink(aInputFile) {
const input = document.getElementById(aInputFile);
// -- Create the symlink and get a `File` instance pointing at it.
const url = SimpleTest.getTestFileURL("script_fileList.js");
const script = SpecialPowers.loadChromeScript(url);
let opened = new Promise(resolve => script.addMessageListener("symlink.opened", resolve));
script.sendAsyncMessage("symlink.open", {});
let { dir, file: symlinkFile } = await opened;
info(`symlink.open provided dir: ${dir}`)
// -- Have the picker pick it
var MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(SpecialPowers.wrap(window).browsingContext);
MockFilePicker.displayDirectory = dir;
let pickerShown = new Promise(resolve => {
MockFilePicker.showCallback = function() {
// This is where we are diverging from a realistic scenario in order to get
// the expected coverage.
MockFilePicker.setFiles([symlinkFile]);
resolve();
}
});
MockFilePicker.returnValue = MockFilePicker.returnOK;
let changeEvent = waitForEvent(input, "change");
SpecialPowers.wrap(document).notifyUserGestureActivation();
input.click();
await pickerShown;
await changeEvent;
MockFilePicker.cleanup();
script.destroy();
// -- Verify that we see the symlink.
let fileList = input.files;
is(fileList.length, 1, "There should be 1 file.");
is(fileList[0].name, "symlink.txt", "The file should be the symlink.");
next();
}
function test_webkitdirectory_attribute() {
var a = document.createElement("input");
a.setAttribute("type", "file");
ok("webkitdirectory" in a, "HTMLInputElement.webkitdirectory exists");
ok(!a.hasAttribute("webkitdirectory"), "No webkitdirectory DOM attribute by default");
ok(!a.webkitdirectory, "No webkitdirectory attribute by default");
a.webkitdirectory = true;
ok(a.hasAttribute("webkitdirectory"), "Webkitdirectory DOM attribute is set");
ok(a.webkitdirectory, "Webkitdirectory attribute is set");
next();
}
function test_changeDataWhileWorking() {
var url = SimpleTest.getTestFileURL("script_fileList.js");
var script = SpecialPowers.loadChromeScript(url);
var MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(SpecialPowers.wrap(window).browsingContext);
let promptHandled;
// Let's start retrieving the root nsIFile object
new Promise(function(resolve) {
function onOpened(message) {
script.removeMessageListener("dir.opened", onOpened);
resolve(message.dir);
}
script.addMessageListener("dir.opened", onOpened);
script.sendAsyncMessage("dir.open", { path: "root" });
})
// input.click() pointing to the root dir
.then(async function(aDir) {
MockFilePicker.cleanup();
MockFilePicker.init(SpecialPowers.wrap(window).browsingContext);
MockFilePicker.useDirectory(aDir);
var input = document.getElementById("inputFileDirectoryChange");
promptHandled = waitForPromptHandled();
SpecialPowers.wrap(document).notifyUserGestureActivation();
input.click();
})
// Before onchange, let's take the 'test' directory
.then(function() {
return new Promise(function(resolve) {
function onOpened(message) {
script.removeMessageListener("dir.opened", onOpened);
script.destroy();
resolve(message.dir);
}
script.addMessageListener("dir.opened", onOpened);
script.sendAsyncMessage("dir.open", { path: "test" });
});
})
// Now let's click again and wait for onchange.
.then(async function(aDir) {
MockFilePicker.cleanup();
MockFilePicker.init(SpecialPowers.wrap(window).browsingContext);
MockFilePicker.useDirectory(aDir);
let input = document.getElementById("inputFileDirectoryChange");
let changeEvent = waitForEvent(input, "change");
SpecialPowers.wrap(document).notifyUserGestureActivation();
input.click();
await promptHandled;
await changeEvent;
MockFilePicker.cleanup();
})
.then(function() {
test_fileList("inputFileWebkitDirectory", testDirData);
});
}
async function test_setup() {
if (AppConstants.platform === "android") {
let MockPromptCollection = SpecialPowers.MockPromptCollection;
MockPromptCollection.init(SpecialPowers.wrap(window).browsingContext);
} else {
let promptHandlerUrl = SimpleTest.getTestFileURL("script_promptHandler.js")
promptHandler = SpecialPowers.loadChromeScript(promptHandlerUrl);
let promptHandlerReady = new Promise(resolve => promptHandler.addMessageListener("initDone", resolve));
promptHandler.sendAsyncMessage("init");
await promptHandlerReady;
}
SpecialPowers.pushPrefEnv({"set": [["dom.filesystem.pathcheck.disabled", true],
["dom.webkitBlink.dirPicker.enabled", true]]}, next);
}
async function test_cleanup() {
if (AppConstants.platform === "android") {
return;
}
let promptHandlerDone = new Promise(resolve => promptHandler.addMessageListener("cleanupDone", resolve));
promptHandler.sendAsyncMessage("cleanup");
await promptHandlerDone;
promptHandler.destroy();
}
var testDirData = [ { name: "foo.txt", path: "/foo.txt" },
{ name: "bar.txt", path: "/subdir/bar.txt" }];
var tests = [
test_setup,
function() { populateInputFile("inputFileWebkitDirectory"); },
function() { test_fileList("inputFileWebkitDirectory", testDirData); },
function() {
// Symlinks are not available on Windows and so will not be created.
if (AppConstants.platform === "win" || AppConstants.platform === "android") {
info("Skipping individual symlink check on Windows and Android.");
next();
return;
}
test_individualSymlink("inputFileWebkitFile").catch(err => ok(false, `Problem in symlink case: ${err}`));
},
test_webkitdirectory_attribute,
test_changeDataWhileWorking,
];
async function next() {
if (!tests.length) {
await test_cleanup();
SimpleTest.finish();
return;
}
var test = tests.shift();
await test();
}
SimpleTest.waitForExplicitFinish();
next();
</script>
</body>
</html>
|