Bug 1173320 - patch 7/8 - Tests for FileList and Directories, r=smaug

This commit is contained in:
Andrea Marchesini 2016-03-19 22:29:47 +01:00
Родитель 7aba2fa5e8
Коммит 6f10afaf24
11 изменённых файлов: 216 добавлений и 9 удалений

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

@ -1,15 +1,25 @@
var { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.importGlobalProperties(["File"]);
var testFile = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIDirectoryService)
.QueryInterface(Ci.nsIProperties)
.get("ProfD", Ci.nsIFile);
testFile.append("prefs.js");
addMessageListener("file.open", function () {
var testFile = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIDirectoryService)
.QueryInterface(Ci.nsIProperties)
.get("ProfD", Ci.nsIFile);
testFile.append("prefs.js");
sendAsyncMessage("file.opened", {
file: new File(testFile)
});
});
addMessageListener("dir.open", function () {
var testFile = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIDirectoryService)
.QueryInterface(Ci.nsIProperties)
.get("ProfD", Ci.nsIFile);
sendAsyncMessage("dir.opened", {
dir: testFile.path
});
});

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

@ -72,7 +72,7 @@ var clonableObjects = [
{ crossThreads: true, data: new ImageData(2, 2) },
];
function create_fileList() {
function create_fileList_forFile() {
var url = SimpleTest.getTestFileURL("script_postmessages_fileList.js");
var script = SpecialPowers.loadChromeScript(url);
@ -93,6 +93,27 @@ function create_fileList() {
script.sendAsyncMessage("file.open");
}
function create_fileList_forDir() {
var url = SimpleTest.getTestFileURL("script_postmessages_fileList.js");
var script = SpecialPowers.loadChromeScript(url);
function onOpened(message) {
var fileList = document.getElementById('fileList');
SpecialPowers.wrap(fileList).mozSetDirectory(message.dir);
// Just a simple test
is(fileList.files.length, 1, "Filelist has 1 element");
ok(fileList.files[0] instanceof Directory, "We have a directory.");
clonableObjects.push({ crossThreads: false, data: fileList.files });
script.destroy();
next();
}
script.addMessageListener("dir.opened", onOpened);
script.sendAsyncMessage("dir.open");
}
function runTests(obj) {
ok(('clonableObjects' in obj) &&
('transferableObjects' in obj) &&
@ -513,7 +534,8 @@ function test_messagePort_inWorkers() {
}
var tests = [
create_fileList,
create_fileList_forFile,
create_fileList_forDir,
test_windowToWindow,
test_windowToIframe,

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

@ -327,7 +327,7 @@ GetDirectoryListingTask::HandlerCallback()
Sequence<OwningFileOrDirectory> listing;
if (!listing.SetLength(count, mozilla::fallible_t())) {
mPromise->MaybeReject(NS_ERROR_FAILURE);
mPromise->MaybeReject(NS_ERROR_OUT_OF_MEMORY);
mPromise = nullptr;
return;
}

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

@ -4,6 +4,8 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
TEST_DIRS += ['tests']
EXPORTS.mozilla.dom += [
'DeviceStorageFileSystem.h',
'Directory.h',

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

@ -0,0 +1,5 @@
[DEFAULT]
support-files =
script_fileList.js
[test_basic.html]

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

@ -0,0 +1,7 @@
# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
MOCHITEST_MANIFESTS += ['mochitest.ini']

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

@ -0,0 +1,25 @@
var { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.importGlobalProperties(["File"]);
addMessageListener("file.open", function () {
var testFile = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIDirectoryService)
.QueryInterface(Ci.nsIProperties)
.get("ProfD", Ci.nsIFile);
testFile.append("prefs.js");
sendAsyncMessage("file.opened", {
file: new File(testFile)
});
});
addMessageListener("dir.open", function () {
var testFile = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIDirectoryService)
.QueryInterface(Ci.nsIProperties)
.get("ProfD", Ci.nsIFile);
sendAsyncMessage("dir.opened", {
dir: testFile.path
});
});

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

@ -0,0 +1,103 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for Directory API</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<input id="fileList" type="file"></input>
<script type="application/javascript;version=1.7">
var directory;
function create_fileList() {
var url = SimpleTest.getTestFileURL("script_fileList.js");
var script = SpecialPowers.loadChromeScript(url);
function onOpened(message) {
var fileList = document.getElementById('fileList');
SpecialPowers.wrap(fileList).mozSetDirectory(message.dir);
// Just a simple test
is(fileList.files.length, 1, "Filelist has 1 element");
ok(fileList.files[0] instanceof Directory, "We have a directory.");
directory = fileList.files[0];
script.destroy();
next();
}
script.addMessageListener("dir.opened", onOpened);
script.sendAsyncMessage("dir.open");
}
function test_basic() {
ok(directory, "Directory exists.");
ok(directory instanceof Directory, "We have a directory.");
is(directory.name, '/', "directory.name must be '/'");
is(directory.path, '/', "directory.path must be '/'");
next();
}
function checkSubDir(dir) {
dir.getFilesAndDirectories().then(
function(data) {
for (var i = 0; i < data.length; ++i) {
ok (data[i] instanceof File || data[i] instanceof Directory, "Just Files or Directories");
if (data[i] instanceof Directory) {
isnot(data[i].name, '/', "Subdirectory should be called with the leafname");
isnot(data[i].path, '/', "Subdirectory path should be called with the leafname");
}
}
}
);
}
function getFilesAndDirectories() {
directory.getFilesAndDirectories().then(
function(data) {
ok(data.length, "We should have some data.");
var promises = [];
for (var i = 0; i < data.length; ++i) {
ok (data[i] instanceof File || data[i] instanceof Directory, "Just Files or Directories");
if (data[i] instanceof Directory) {
isnot(data[i].name, '/', "Subdirectory should be called with the leafname");
isnot(data[i].path, '/', "Subdirectory path should be called with the leafname");
promises.push(checkSubDir(data[i]));
}
}
return Promise.all(promises);
},
function() {
ok(false, "Something when wrong");
}
).then(next);
}
var tests = [
create_fileList,
test_basic,
getFilesAndDirectories,
];
function next() {
if (!tests.length) {
SimpleTest.finish();
return;
}
var test = tests.shift();
test();
}
SimpleTest.waitForExplicitFinish();
next();
</script>
</body>
</html>

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

@ -2284,6 +2284,34 @@ HTMLInputElement::MozSetFileNameArray(const char16_t** aFileNames,
return rv.StealNSResult();
}
void
HTMLInputElement::MozSetDirectory(const nsAString& aDirectoryPath,
ErrorResult& aRv)
{
nsCOMPtr<nsIFile> file;
NS_ConvertUTF16toUTF8 path(aDirectoryPath);
aRv = NS_NewNativeLocalFile(path, true, getter_AddRefs(file));
if (NS_WARN_IF(aRv.Failed())) {
return;
}
nsPIDOMWindowInner* window = OwnerDoc()->GetInnerWindow();
if (NS_WARN_IF(!window)) {
aRv.Throw(NS_ERROR_FAILURE);
return;
}
RefPtr<Directory> directory = Directory::Create(window, file,
Directory::eDOMRootDirectory);
MOZ_ASSERT(directory);
nsTArray<OwningFileOrDirectory> array;
OwningFileOrDirectory* element = array.AppendElement();
element->SetAsDirectory() = directory;
SetFilesOrDirectories(array, true);
}
bool
HTMLInputElement::MozIsTextField(bool aExcludePassword)
{

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

@ -724,6 +724,7 @@ public:
void MozSetFileNameArray(const Sequence< nsString >& aFileNames, ErrorResult& aRv);
void MozSetFileArray(const Sequence<OwningNonNull<File>>& aFiles);
void MozSetDirectory(const nsAString& aDirectoryPath, ErrorResult& aRv);
HTMLInputElement* GetOwnerNumberControl();

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

@ -155,6 +155,10 @@ partial interface HTMLInputElement {
[ChromeOnly]
void mozSetFileArray(sequence<File> files);
// This method is meant to use for testing only.
[ChromeOnly, Throws]
void mozSetDirectory(DOMString directoryPath);
// Number controls (<input type=number>) have an anonymous text control
// (<input type=text>) in the anonymous shadow tree that they contain. On
// such an anonymous text control this property provides access to the