Bug 1338637 - Updated webkitdirectory test to handle confirm prompts. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D97319
This commit is contained in:
pbz 2020-11-19 09:45:04 +00:00
Родитель 4cf818dd4f
Коммит 97d9bda2ed
3 изменённых файлов: 82 добавлений и 24 удалений

Просмотреть файл

@ -6,5 +6,7 @@ support-files =
[test_basic.html]
[test_webkitdirectory.html]
skip-if = os == "android" # Bug 1674428
support-files = script_promptHandler.js
[test_worker_basic.html]
[test_bug1319088.html]

Просмотреть файл

@ -0,0 +1,20 @@
/* eslint-env mozilla/frame-script */
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
let dialogObserverTopic = "common-dialog-loaded";
function dialogObserver(subj, topic, data) {
subj.document.querySelector("dialog").acceptDialog();
sendAsyncMessage("promptAccepted");
}
addMessageListener("init", message => {
Services.obs.addObserver(dialogObserver, dialogObserverTopic);
sendAsyncMessage("initDone");
});
addMessageListener("cleanup", message => {
Services.obs.removeObserver(dialogObserver, dialogObserverTopic);
sendAsyncMessage("cleanupDone");
});

Просмотреть файл

@ -14,25 +14,44 @@
<script type="application/javascript">
const { AppConstants } = SpecialPowers.Cu.import("resource://gre/modules/AppConstants.jsm", {});
let promptHandler;
function waitForEvent(element, eventName) {
return new Promise(function(resolve) {
element.addEventListener(eventName, e => resolve(e.detail), { once: true });
});
}
function waitForPromptHandled() {
return new Promise(resolve => promptHandler.addMessageListener("promptAccepted", resolve));
}
function populateInputFile(aInputFile) {
var url = SimpleTest.getTestFileURL("script_fileList.js");
var script = SpecialPowers.loadChromeScript(url);
var MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(window, "A Mock File Picker", SpecialPowers.Ci.nsIFilePicker.modeOpen);
MockFilePicker.init(window, "A Mock File Picker", SpecialPowers.Ci.nsIFilePicker.modeGetFolder);
function onOpened(message) {
async function onOpened(message) {
MockFilePicker.useDirectory(message.dir);
var input = document.getElementById(aInputFile);
let input = document.getElementById(aInputFile);
input.setAttribute("data-name", message.name);
input.addEventListener("change", function() {
MockFilePicker.cleanup();
script.destroy();
next();
}, {once: true});
let promptHandled = waitForPromptHandled();
let changeEvent = waitForEvent(input, "change");
input.click();
await promptHandled;
await changeEvent;
MockFilePicker.cleanup();
script.destroy();
next();
}
script.addMessageListener("dir.opened", onOpened);
@ -91,7 +110,8 @@ function test_changeDataWhileWorking() {
var script = SpecialPowers.loadChromeScript(url);
var MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(window, "A Mock File Picker", SpecialPowers.Ci.nsIFilePicker.modeOpen);
MockFilePicker.init(window, "A Mock File Picker", SpecialPowers.Ci.nsIFilePicker.modeGetFolder);
let promptHandled;
// Let's start retrieving the root nsIFile object
new Promise(function(resolve) {
@ -105,11 +125,13 @@ function test_changeDataWhileWorking() {
})
// input.click() pointing to the root dir
.then(function(aDir) {
.then(async function(aDir) {
MockFilePicker.cleanup();
MockFilePicker.init(window, "A Mock File Picker", SpecialPowers.Ci.nsIFilePicker.modeOpen);
MockFilePicker.init(window, "A Mock File Picker", SpecialPowers.Ci.nsIFilePicker.modeGetFolder);
MockFilePicker.useDirectory(aDir);
var input = document.getElementById("inputFileDirectoryChange");
promptHandled = waitForPromptHandled();
input.click();
})
@ -128,33 +150,46 @@ function test_changeDataWhileWorking() {
})
// Now let's click again and wait for onchange.
.then(function(aDir) {
return new Promise(function(resolve) {
.then(async function(aDir) {
MockFilePicker.cleanup();
MockFilePicker.init(window, "A Mock File Picker", SpecialPowers.Ci.nsIFilePicker.modeOpen);
MockFilePicker.init(window, "A Mock File Picker", SpecialPowers.Ci.nsIFilePicker.modeGetFolder);
MockFilePicker.useDirectory(aDir);
var input = document.getElementById("inputFileDirectoryChange");
input.addEventListener("change", function() {
MockFilePicker.cleanup();
resolve();
});
let input = document.getElementById("inputFileDirectoryChange");
let changeEvent = waitForEvent(input, "change");
input.click();
});
})
await promptHandled;
await changeEvent;
MockFilePicker.cleanup();
})
.then(function() {
test_fileList("inputFileWebkitDirectory", testDirData);
});
}
function test_setup() {
async function test_setup() {
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.input.dirpicker", true],
["dom.filesystem.pathcheck.disabled", true],
["dom.webkitBlink.dirPicker.enabled", true]]}, next);
}
async function test_cleanup() {
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" }];
@ -174,14 +209,15 @@ var tests = [
test_changeDataWhileWorking,
];
function next() {
async function next() {
if (!tests.length) {
await test_cleanup();
SimpleTest.finish();
return;
}
var test = tests.shift();
test();
await test();
}
SimpleTest.waitForExplicitFinish();