Update web-platform-tests to revision 474923949524b5c05a9e6f28ec082fdca87078de

This commit is contained in:
WPT Sync Bot 2019-10-25 10:23:30 +00:00
parent b7b1b903d3
commit 328d5a4231
91 changed files with 3190 additions and 185 deletions

View file

@ -0,0 +1,6 @@
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
// META: script=resources/test-helpers.js
// META: script=resources/native-fs-test-helpers.js
// META: script=resources/messaging-helpers.js
// META: script=resources/messaging-serialize-helpers.js
// META: script=script-tests/FileSystemBaseHandle-postMessage-BroadcastChannel.js

View file

@ -0,0 +1,8 @@
// META: script=/common/get-host-info.sub.js
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
// META: script=resources/test-helpers.js
// META: script=resources/native-fs-test-helpers.js
// META: script=resources/messaging-helpers.js
// META: script=resources/messaging-blob-helpers.js
// META: script=resources/messaging-serialize-helpers.js
// META: script=script-tests/FileSystemBaseHandle-postMessage-Error.js

View file

@ -0,0 +1,7 @@
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
// META: script=resources/test-helpers.js
// META: script=resources/native-fs-test-helpers.js
// META: script=resources/messaging-helpers.js
// META: script=resources/messaging-blob-helpers.js
// META: script=resources/messaging-serialize-helpers.js
// META: script=script-tests/FileSystemBaseHandle-postMessage-MessagePort.js

View file

@ -0,0 +1,7 @@
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
// META: script=resources/test-helpers.js
// META: script=resources/native-fs-test-helpers.js
// META: script=resources/messaging-helpers.js
// META: script=resources/messaging-blob-helpers.js
// META: script=resources/messaging-serialize-helpers.js
// META: script=script-tests/FileSystemBaseHandle-postMessage.js

View file

@ -0,0 +1,9 @@
'use strict';
importScripts(
'test-helpers.js',
'messaging-serialize-helpers.js',
'message-target.js'
);
add_message_event_handlers(/*receiver=*/self, /*target=*/self);

View file

@ -0,0 +1,9 @@
'use strict';
importScripts(
'test-helpers.js',
'messaging-serialize-helpers.js',
'message-target.js'
);
add_message_event_handlers(/*receiver=*/self);

View file

@ -0,0 +1,14 @@
'use strict';
importScripts(
'test-helpers.js',
'messaging-serialize-helpers.js',
'message-target.js'
);
self.addEventListener('connect', connect_event => {
const message_port = connect_event.ports[0];
add_message_event_handlers(
/*receiver=*/message_port, /*target=*/message_port);
message_port.start();
});

View file

@ -0,0 +1,22 @@
<!DOCTYPE html>
<script src='test-helpers.js'></script>
<script src='messaging-serialize-helpers.js'></script>
<script src='message-target.js'></script>
<script id="inline_script">
'use strict'
if (window.parent !== null) {
window.parent.postMessage('LOADED', { targetOrigin: '*' });
}
if (window.opener !== null) {
window.opener.postMessage('LOADED', { targetOrigin: '*' });
}
// Use an undefined message target to send responses to
// MessageEvent::source instead.
const target = undefined;
add_message_event_handlers(
/*receiver=*/self, target, /*target_origin=*/'*');
</script>

View file

@ -0,0 +1,145 @@
'use strict';
// This script depends on the following scripts:
// /native-file-system/resources/messaging-helpers.js
// /native-file-system/resources/test-helpers.js
// add_message_event_handlers() is the helper function used to setup all
// message targets, including iframes and workers.
//
// Adds a message event handler and a message error handler to |receiver|.
// The 'data' property from received MessageEvents must include a 'type'
// property. The 'type' selects the test logic to run. Most message type
// handlers use postMessage() to respond to the sender with test results.
// The sender then validates the test results after receiving the response.
//
// Both |target| and |target_origin| are optional. |target| is used
// to send message responses back to the sender. When omitted, the
// 'source' from received MessageEvents is used instead.
//
// For window messaging, |target_origin| specifies the origin to receive
// responses. Most window tests use '*' for the |target_origin|. Worker
// and message port tests must use undefined for |target_origin| to avoid
// exceptions.
function add_message_event_handlers(receiver, target, target_origin) {
receiver.addEventListener('message', async function (message_event) {
const message_data = message_event.data;
// Reply to the sender using the 'source' from the received MessageEvent.
let message_source = message_event.source;
if (message_source === null) {
// However, some message senders, like DedicatedWorkers, don't include
// a source. Fallback to the target when the source is null.
message_source = target;
}
try {
switch (message_data.type) {
case 'receive-message-port':
// Receive a MessagePort to use as a message target for testing.
add_message_event_handlers(
/*receiver=*/message_data.message_port,
/*target=*/message_data.message_port);
message_data.message_port.start();
break;
case 'create-broadcast-channel':
// Create a BroadcastChannel to use as a message target for testing.
const broadcast_channel =
new BroadcastChannel(message_data.broadcast_channel_name);
add_message_event_handlers(
/*receiver=*/broadcast_channel,
/*target=*/broadcast_channel);
message_source.postMessage(
{ type: 'broadcast-channel-created' },
{ targetOrigin: target_origin });
break;
case 'receive-file-system-handles':
// Receive a list of cloned FileSystemFileHandles. Access the
// properties of each FileSystemFileHandle by serializing the
// handle to a JavaScript object. Then respond with the serialized
// results, enabling the sender to verify that the cloned handle
// produced the expected property values from this execution context.
const serialized_handles = [];
const cloned_handles = message_data.cloned_handles;
for (let i = 0; i < cloned_handles.length; ++i) {
const serialized = await serialize_handle(cloned_handles[i]);
serialized_handles.push(serialized);
}
message_source.postMessage({
type: 'receive-serialized-file-system-handles',
serialized_handles,
// Respond with the cloned handles to create new clones for
// the sender to verify.
cloned_handles,
}, { targetOrigin: target_origin });
break;
case 'receive-serialized-file-system-handles':
// Do nothing. This message is meant for test runner validation.
// Other message targets may receive this message while testing
// broadcast channels.
break;
case 'create-file':
// Create a new file and then respond to the sender with it.
const directory =
await FileSystemDirectoryHandle.getSystemDirectory(
{ type: 'sandbox' });
const file_handle =
await directory.getFile('temp-file', { create: true });
message_source.postMessage(
{ type: 'receive-file', file_handle },
{ targetOrigin: target_origin });
break;
case 'create-directory':
// Create a new directory and then respond to the sender with it.
const parent_directory =
await FileSystemDirectoryHandle.getSystemDirectory(
{ type: 'sandbox' });
const directory_handle =
await parent_directory.getDirectory('temp-directory',
{ create: true });
message_source.postMessage(
{ type: 'receive-directory', directory_handle },
{ targetOrigin: target_origin });
break;
default:
throw `Unknown message type: '${message_data.type}'`;
}
} catch (error) {
// Respond with an error to trigger a failure in the sender's
// test runner.
message_source.postMessage(`ERROR: ${error}`,
{ targetOrigin: target_origin });
}
});
receiver.addEventListener('messageerror', async function (message_event) {
// Select the target for message responses (see comment in 'message' event
// listener above).
let message_source = message_event.source;
if (message_source === null) {
message_source = target;
}
try {
// Respond with the MessageEvent's property values, enabling the sender
// to verify results.
const serialized_message_error_event =
serialize_message_error_event(message_event);
message_source.postMessage({
type: 'serialized-message-error',
serialized_message_error_event
}, { targetOrigin: target_origin });
} catch (error) {
// Respond with an error to trigger a failure in the sender's
// test runner.
message_source.postMessage(`ERROR: ${error}`,
{ targetOrigin: target_origin });
}
});
}

View file

@ -0,0 +1,51 @@
'use strict';
// Creates a blob URL with the contents of 'message-target.html'. Use the
// blob as an iframe src or a window.open() URL, which creates a same origin
// message target.
async function create_message_target_blob_url(test) {
const html = await create_message_target_html_without_subresources(test);
const blob = new Blob([html], { type: 'text/html' });
return URL.createObjectURL(blob);
}
// Creates a data URI with the contents of 'message-target.html'. Use the
// data URI as an iframe src, which creates a cross origin message target.
async function create_message_target_data_uri(test) {
const iframe_html =
await create_message_target_html_without_subresources(test);
return `data:text/html,${encodeURIComponent(iframe_html)}`;
}
// Constructs a version of 'message-target.html' without any subresources.
// Enables the creation of blob URLs, data URIs and iframe srcdocs re-using
// the contents of 'message-target.html'.
async function create_message_target_html_without_subresources(test) {
const test_helpers_script = await fetch_text('resources/test-helpers.js');
const messaging_helpers_script =
await fetch_text('resources/messaging-helpers.js');
const messaging_serialize_helpers_script =
await fetch_text('resources/messaging-serialize-helpers.js');
const message_target_script =
await fetch_text('resources/message-target.js');
// Get the inline script code from 'message-target.html'.
const iframe = await add_iframe(test, { src: 'resources/message-target.html' });
const iframe_script =
iframe.contentWindow.document.getElementById('inline_script').outerHTML;
iframe.remove();
return '<!DOCTYPE html>' +
`<script>${test_helpers_script}</script>` +
`<script>${messaging_serialize_helpers_script}</script>` +
`<script>${message_target_script}</script>` +
`${iframe_script}`;
}
async function fetch_text(url) {
const response = await fetch(url);
return await response.text();
}

View file

@ -0,0 +1,177 @@
'use strict';
// This script depends on the following script:
// /native-file-system/resources/test-helpers.js
// /service-workers/service-worker/resources/test-helpers.sub.js
// Define the URL constants used for each type of message target, including
// iframes and workers.
const kDocumentMessageTarget = 'resources/message-target.html';
const kSharedWorkerMessageTarget = 'resources/message-target-shared-worker.js';
const kServiceWorkerMessageTarget =
'resources/message-target-service-worker.js';
const kDedicatedWorkerMessageTarget =
'resources/message-target-dedicated-worker.js';
function create_dedicated_worker(test, url) {
const dedicated_worker = new Worker(url);
test.add_cleanup(() => {
dedicated_worker.terminate();
});
return dedicated_worker;
}
async function create_service_worker(test, script_url, scope) {
const registration = await service_worker_unregister_and_register(
test, script_url, scope);
test.add_cleanup(() => {
return registration.unregister();
});
return registration;
}
// Creates an iframe and waits to receive a message from the iframe.
// Valid |options| include src, srcdoc and sandbox, which mirror the
// corresponding iframe element properties.
async function add_iframe(test, options) {
const iframe = document.createElement('iframe');
if (options.sandbox !== undefined) {
iframe.sandbox = options.sandbox;
}
if (options.src !== undefined) {
iframe.src = options.src;
}
if (options.srcdoc !== undefined) {
iframe.srcdoc = options.srcdoc;
}
document.body.appendChild(iframe);
test.add_cleanup(() => {
iframe.remove();
});
await wait_for_loaded_message(self);
return iframe;
}
// Creates a child window using window.open() and waits to receive a message
// from the child window.
async function open_window(test, url) {
const child_window = window.open(url);
test.add_cleanup(() => {
child_window.close();
});
await wait_for_loaded_message(self);
return child_window;
}
// Wait until |receiver| gets a message event with the data set to 'LOADED'.
// The postMessage() tests use messaging instead of the loaded event because
// cross-origin child windows from window.open() do not dispatch the loaded
// event to the parent window.
async function wait_for_loaded_message(receiver) {
const message_promise = new Promise((resolve, reject) => {
receiver.addEventListener('message', message_event => {
if (message_event.data === 'LOADED') {
resolve();
} else {
reject('The message target must receive a "LOADED" message response.');
}
});
});
await message_promise;
}
// Sets up a new message channel. Sends one port to |target| and then returns
// the other port.
function create_message_channel(target, target_origin) {
const message_channel = new MessageChannel();
const message_data =
{ type: 'receive-message-port', message_port: message_channel.port2 };
target.postMessage(
message_data,
{
transfer: [message_channel.port2],
targetOrigin: target_origin
});
message_channel.port1.start();
return message_channel.port1;
}
// Creates a variety of different FileSystemFileHandles for testing.
async function create_file_system_handles(test, root) {
// Create some files to use with postMessage().
const empty_file = await createEmptyFile(test, 'empty-file', root);
const first_file = await createFileWithContents(
test, 'first-file-with-contents', 'first-text-content', root);
const second_file = await createFileWithContents(
test, 'second-file-with-contents', 'second-text-content', root);
// Create an empty directory to use with postMessage().
const empty_directory = await createDirectory(test, 'empty-directory', root);
// Create a directory containing both files and subdirectories to use
// with postMessage().
const directory_with_files =
await createDirectory(test, 'directory-with-files', root);
await createFileWithContents(test, 'first-file-in-directory',
'first-directory-text-content', directory_with_files);
await createFileWithContents(test, 'second-file-in-directory',
'second-directory-text-content', directory_with_files);
const subdirectory =
await createDirectory(test, 'subdirectory', directory_with_files);
await createFileWithContents(test, 'first-file-in-subdirectory',
'first-subdirectory-text-content', subdirectory);
return [
empty_file,
first_file,
second_file,
// Include the same FileSystemFileHandle twice.
second_file,
empty_directory,
// Include the Same FileSystemDirectoryHandle object twice.
empty_directory,
directory_with_files
];
}
// Tests sending an array of FileSystemHandles to |target| with postMessage().
// The array includes both FileSystemFileHandles and FileSystemDirectoryHandles.
// After receiving the message, |target| accesses all cloned handles by
// serializing the properties of each handle to a JavaScript object.
//
// |target| then responds with the resulting array of serialized handles. The
// response also includes the array of cloned handles, which creates more
// clones. After receiving the response, this test runner verifies that both
// the serialized handles and the cloned handles contain the expected properties.
async function do_post_message_test(
test, root_dir, receiver, target, target_origin) {
// Create and send the handles to |target|.
const handles =
await create_file_system_handles(test, root_dir, target, target_origin);
target.postMessage(
{ type: 'receive-file-system-handles', cloned_handles: handles },
{ targetOrigin: target_origin });
// Wait for |target| to respond with results.
const event_watcher = new EventWatcher(test, receiver, 'message');
const message_event = await event_watcher.wait_for('message');
const response = message_event.data;
assert_equals(response.type, 'receive-serialized-file-system-handles',
'The test runner must receive a "serialized-file-system-handles" ' +
`message response. Actual response: ${response}`);
// Verify the results.
const expected_serialized_handles = await serialize_handles(handles);
assert_equals_serialized_handles(
response.serialized_handles, expected_serialized_handles);
await assert_equals_cloned_handles(response.cloned_handles, handles);
}

View file

@ -0,0 +1,234 @@
'use strict';
// This script depends on the following script:
// /native-file-system/resources/test-helpers.js
// Serializes an array of FileSystemHandles where each element can be either a
// FileSystemFileHandle or FileSystemDirectoryHandle.
async function serialize_handles(handle_array) {
const serialized_handle_array = [];
for (let i = 0; i < handle_array.length; ++i) {
serialized_handle_array.push(await serialize_handle(handle_array[i]));
}
return serialized_handle_array;
}
// Serializes either a FileSystemFileHandle or FileSystemDirectoryHandle.
async function serialize_handle(handle) {
let serialized;
if (handle.isDirectory) {
serialized = await serialize_file_system_directory_handle(handle);
} else if (handle.isFile) {
serialized = await serialize_file_system_file_handle(handle);
} else {
throw 'Object is not a FileSystemFileHandle or ' +
`FileSystemDirectoryHandle ${handle}`;
}
return serialized;
}
// Creates a dictionary for a FileSystemHandle base, which contains
// serialized properties shared by both FileSystemFileHandle and
// FileSystemDirectoryHandle.
async function serialize_file_system_handle(handle) {
const read_permission =
await handle.queryPermission({ writable: false });
const write_permission =
await handle.queryPermission({ writable: true })
return {
is_file: handle.isFile,
is_directory: handle.isDirectory,
name: handle.name,
read_permission,
write_permission
};
}
// Create a dictionary with each property value in FileSystemFileHandle.
// Also, reads the contents of the file to include with the returned
// dictionary. Example output:
// {
// is_file: true,
// is_directory: false,
// name: "example-file-name"
// read_permission: "granted",
// write_permission: "granted",
// contents: "example-file-contents"
// }
async function serialize_file_system_file_handle(file_handle) {
const contents = await getFileContents(file_handle);
const serialized_file_system_handle =
await serialize_file_system_handle(file_handle);
return Object.assign(serialized_file_system_handle, { contents });
}
// Create a dictionary with each property value in FileSystemDirectoryHandle.
// Example output:
// {
// is_file: false,
// is_directory: true,
// name: "example-directory-name"
// read_permission: "granted",
// write_permission: "granted",
// files: [<first serialized file>, ...]
// directories: [<first serialized subdirectory>, ...]
// }
async function serialize_file_system_directory_handle(directory_handle) {
// Serialize the contents of the directory.
const serialized_files = [];
const serialized_directories = [];
for await (const child_handle of directory_handle.getEntries()) {
const serialized_child_handle = await serialize_handle(child_handle);
if (child_handle.isDirectory) {
serialized_directories.push(serialized_child_handle);
} else {
serialized_files.push(serialized_child_handle);
}
}
// Order the serialized contents of the directory by name.
serialized_files.sort((left, right) => {
return left.name.localeCompare(right.name);
});
serialized_directories.sort((left, right) => {
return left.name.localeCompare(right.name);
});
// Serialize the directory's common properties shared by all
// FileSystemHandles.
const serialized_file_system_handle =
await serialize_file_system_handle(directory_handle);
return Object.assign(
serialized_file_system_handle,
{ files: serialized_files, directories: serialized_directories });
}
// Verifies |left_array| is a clone of |right_array| where each element
// is a cloned FileSystemHandle with the same properties and contents.
async function assert_equals_cloned_handles(left_array, right_array) {
assert_equals(left_array.length, right_array.length,
'Each array of FileSystemHandles must have the same length');
for (let i = 0; i < left_array.length; ++i) {
assert_not_equals(left_array[i], right_array[i],
'Clones must create new FileSystemHandle instances.');
const left_serialized = await serialize_handle(left_array[i]);
const right_serialized = await serialize_handle(right_array[i]);
assert_equals_serialized_handle(left_serialized, right_serialized);
}
}
// Verifies |left_array| is the same as |right_array| where each element
// is a serialized FileSystemHandle with the same properties.
function assert_equals_serialized_handles(left_array, right_array) {
assert_equals(left_array.length, right_array.length,
'Each array of serialized handles must have the same length');
for (let i = 0; i < left_array.length; ++i) {
assert_equals_serialized_handle(left_array[i], right_array[i]);
}
}
// Verifies each property of a serialized FileSystemFileHandle or
// FileSystemDirectoryHandle.
function assert_equals_serialized_handle(left, right) {
if (left.is_directory) {
assert_equals_serialized_file_system_directory_handle(left, right);
} else if (left.is_file) {
assert_equals_serialized_file_system_file_handle(left, right);
} else {
throw 'Object is not a FileSystemFileHandle or ' +
`FileSystemDirectoryHandle ${handle}`;
}
}
// Compares the output of serialize_file_system_handle() for
// two FileSystemHandles.
function assert_equals_serialized_file_system_handle(left, right) {
assert_equals(left.is_file, right.is_file,
'Each FileSystemHandle instance must use the expected "isFile".');
assert_equals(left.is_directory, right.is_directory,
'Each FileSystemHandle instance must use the expected "isDirectory".');
assert_equals(left.name, right.name,
'Each FileSystemHandle instance must use the expected "name" ' +
' property.');
assert_equals(left.read_permission, right.read_permission,
'Each FileSystemHandle instance must have the expected read ' +
' permission.');
assert_equals(left.write_permission, right.write_permission,
'Each FileSystemHandle instance must have the expected write ' +
' permission.');
}
// Compares the output of serialize_file_system_file_handle()
// for two FileSystemFileHandle.
function assert_equals_serialized_file_system_file_handle(left, right) {
assert_equals_serialized_file_system_handle(left, right);
assert_equals(left.contents, right.contents,
'Each FileSystemFileHandle instance must have the same contents.');
}
// Compares the output of serialize_file_system_directory_handle()
// for two FileSystemDirectoryHandles.
function assert_equals_serialized_file_system_directory_handle(left, right) {
assert_equals_serialized_file_system_handle(left, right);
assert_equals(left.files.length, right.files.length,
'Each FileSystemDirectoryHandle must contain the same number of ' +
'file children');
for (let i = 0; i < left.files.length; ++i) {
assert_equals_serialized_file_system_file_handle(
left.files[i], right.files[i]);
}
assert_equals(left.directories.length, right.directories.length,
'Each FileSystemDirectoryHandle must contain the same number of ' +
'directory children');
for (let i = 0; i < left.directories.length; ++i) {
assert_equals_serialized_file_system_directory_handle(
left.directories[i], right.directories[i]);
}
}
// Creates a dictionary with interesting property values from MessageEvent.
function serialize_message_error_event(message_error_event) {
return {
data: message_error_event.data,
origin: message_error_event.origin,
last_event_id: message_error_event.lastEventId,
has_source: (message_error_event.source !== null),
ports_length: message_error_event.ports.length
};
}
// Compares the output of serialize_message_error_event() with an
// expected result.
function assert_equals_serialized_message_error_event(
serialized_event, expected_origin, expected_has_source) {
assert_equals(serialized_event.data, null,
'The message error event must set the "data" property to null.');
assert_equals(serialized_event.origin, expected_origin,
'The message error event must have the expected "origin" property.');
assert_equals(serialized_event.last_event_id, "",
'The message error event must set the "lastEventId" property to the empty string.');
assert_equals(serialized_event.has_source, expected_has_source,
'The message error event must have the expected "source" property.');
assert_equals(serialized_event.ports_length, 0,
'The message error event must not contain any message ports.');
}

View file

@ -0,0 +1,6 @@
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
// META: script=resources/test-helpers.js
// META: script=resources/sandboxed-fs-test-helpers.js
// META: script=resources/messaging-helpers.js
// META: script=resources/messaging-serialize-helpers.js
// META: script=script-tests/FileSystemBaseHandle-postMessage-BroadcastChannel.js

View file

@ -0,0 +1,8 @@
// META: script=/common/get-host-info.sub.js
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
// META: script=resources/test-helpers.js
// META: script=resources/sandboxed-fs-test-helpers.js
// META: script=resources/messaging-helpers.js
// META: script=resources/messaging-blob-helpers.js
// META: script=resources/messaging-serialize-helpers.js
// META: script=script-tests/FileSystemBaseHandle-postMessage-Error.js

View file

@ -0,0 +1,7 @@
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
// META: script=resources/test-helpers.js
// META: script=resources/sandboxed-fs-test-helpers.js
// META: script=resources/messaging-helpers.js
// META: script=resources/messaging-blob-helpers.js
// META: script=resources/messaging-serialize-helpers.js
// META: script=script-tests/FileSystemBaseHandle-postMessage-MessagePort.js

View file

@ -0,0 +1,7 @@
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
// META: script=resources/test-helpers.js
// META: script=resources/sandboxed-fs-test-helpers.js
// META: script=resources/messaging-helpers.js
// META: script=resources/messaging-blob-helpers.js
// META: script=resources/messaging-serialize-helpers.js
// META: script=script-tests/FileSystemBaseHandle-postMessage.js

View file

@ -0,0 +1,82 @@
'use strict';
// This script depends on the following scripts:
// /native-file-system/resources/messaging-helpers.js
// /native-file-system/resources/messaging-serialize-helpers.js
// /native-file-system/resources/test-helpers.js
// /service-workers/service-worker/resources/test-helpers.sub.js
// Sets up a new broadcast channel in |target|. Posts a message instructing
// |target| to open the broadcast channel using |broadcast_channel_name|.
async function create_broadcast_channel(
test, broadcast_channel_name, receiver, target, target_origin) {
target.postMessage(
{ type: 'create-broadcast-channel', broadcast_channel_name },
{ targetOrigin: target_origin });
const event_watcher = new EventWatcher(test, receiver, 'message');
// Wait until |target| is listening to the broad cast channel.
const message_event = await event_watcher.wait_for('message');
assert_equals(message_event.data.type, 'broadcast-channel-created',
'The message target must receive a "broadcast-channel-created" message ' +
'response.');
}
// This test is very similar to 'FileSystemBaseHandle-postMessage.js'. It
// starts by creating three message targets for the broadcast channel:
// an iframe, dedicated worker and a service worker. After setup, an array
// of FileSystemHandles is sent across the broadcast channel. The test
// expects three responses -- one from each message target.
directory_test(async (t, root) => {
const broadcast_channel_name = 'file-system-file-handle-channel';
const broadcast_channel = new BroadcastChannel(broadcast_channel_name);
const broadcast_channel_event_watcher =
new EventWatcher(t, broadcast_channel, 'message');
const iframe = await add_iframe(t, { src: kDocumentMessageTarget });
await create_broadcast_channel(
t, broadcast_channel_name, self, iframe.contentWindow, '*');
const scope = `${kServiceWorkerMessageTarget}` +
'?post-message-to-broadcast-channel-with-file-handle';
const registration = await create_service_worker(
t, kServiceWorkerMessageTarget, scope);
await create_broadcast_channel(
t, broadcast_channel_name,
navigator.serviceWorker, registration.installing);
const dedicated_worker =
create_dedicated_worker(t, kDedicatedWorkerMessageTarget);
await create_broadcast_channel(
t, broadcast_channel_name, dedicated_worker, dedicated_worker);
const handles = await create_file_system_handles(t, root);
broadcast_channel.postMessage(
{ type: 'receive-file-system-handles', cloned_handles: handles });
const expected_response_count = 3;
const responses = [];
for (let i = 0; i < expected_response_count; ++i) {
const message_event =
await broadcast_channel_event_watcher.wait_for('message');
responses.push(message_event.data);
}
const expected_serialized_handles = await serialize_handles(handles);
for (let i = 0; i < responses.length; ++i) {
assert_equals(responses[i].type, 'receive-serialized-file-system-handles',
'The test runner must receive a "serialized-file-system-handles" ' +
`message response. Actual response: ${responses[i]}`);
assert_equals_serialized_handles(
responses[i].serialized_handles, expected_serialized_handles);
await assert_equals_cloned_handles(responses[i].cloned_handles, handles);
}
}, 'Send and receive messages using a broadcast channel in an iframe, ' +
'dedicated worker and service worker.');

View file

@ -0,0 +1,272 @@
'use strict';
// This script depends on the following scripts:
// /native-file-system/resources/messaging-helpers.js
// /native-file-system/resources/messaging-blob-helpers.js
// /native-file-system/resources/messaging-serialize-helpers.js
// /native-file-system/resources/test-helpers.js
// /common/get-host-info.sub.js
// /service-workers/service-worker/resources/test-helpers.sub.js
// Define URL constants for cross origin windows.
const kRemoteOrigin = get_host_info().HTTPS_REMOTE_ORIGIN;
const kRemoteOriginDocumentMessageTarget = `${kRemoteOrigin}${base_path()}` +
kDocumentMessageTarget;
// Sending a FileSystemHandle to a cross origin |target| through postMessage()
// must dispatch the 'messageerror' event.
//
// This test sends a FileSystemHandle to |target|. |target| responds with a
// serialized MessageEvent from the 'messageerror' event, allowing the test
// runner to verify MessageEvent properties.
async function do_send_message_error_test(
test,
root_dir,
receiver,
target,
target_origin,
// False when the MessageEvent's source is null.
expected_has_source,
// The origin of MessageEvents received by |target|.
expected_origin) {
const message_watcher = new EventWatcher(test, receiver, 'message');
// Send a file to |target|.
const file = await createFileWithContents(
test, 'test-error-file', 'test-error-file-contents', root_dir);
target.postMessage(
{ type: 'receive-file-system-handles', cloned_file_system_handles: [file] },
{ targetOrigin: target_origin });
// Wait for |target| to respond with results.
let message_event = await message_watcher.wait_for('message');
const first_response = message_event.data;
assert_equals(first_response.type, 'serialized-message-error',
'The test runner must receive a "serialized-message-error" message ' +
'in response to a FileSystemFileHandle message.');
// Verify the results.
assert_equals_serialized_message_error_event(
first_response.serialized_message_error_event,
expected_origin, expected_has_source);
// Send a directory to |target|.
const directory = await createDirectory(
test, 'test-error-directory', root_dir);
target.postMessage(
{
type: 'receive-file-system-handles',
cloned_file_system_handles: [directory]
}, { targetOrigin: target_origin });
// Wait for |target| to respond with results.
message_event = await message_watcher.wait_for('message');
const second_response = message_event.data;
assert_equals(second_response.type, 'serialized-message-error',
'The test runner must receive a "serialized-message-error" message ' +
'response to a FileSystemDirectoryHandle message.');
// Verify the results.
assert_equals_serialized_message_error_event(
second_response.serialized_message_error_event,
expected_origin, expected_has_source);
}
// This test receives a FileSystemHandle from |target|. This test runner
// must dispatch the 'messageerror' event after receiving a handle from target.
async function do_receive_message_error_test(
test,
receiver,
target,
target_origin,
// False when the MessageEvent's source is null.
expected_has_source,
// The origin of MessageEvents received by this test runner.
expected_origin) {
const error_watcher = new EventWatcher(test, receiver, 'messageerror');
// Receive a file from |target|.
target.postMessage(
{ type: 'create-file' }, { targetOrigin: target_origin });
const first_error = await error_watcher.wait_for('messageerror');
const serialized_first_error = serialize_message_error_event(first_error);
assert_equals_serialized_message_error_event(
serialized_first_error, expected_origin, expected_has_source);
// Receive a directory from |target|.
target.postMessage(
{ type: 'create-directory' }, { targetOrigin: target_origin });
const second_error = await error_watcher.wait_for('messageerror');
const serialized_second_error = serialize_message_error_event(second_error);
assert_equals_serialized_message_error_event(
serialized_second_error, expected_origin, expected_has_source);
}
// Performs the send message error test followed by the receive message error
// test.
async function do_send_and_receive_message_error_test(
test,
root_dir,
receiver,
target,
target_origin,
// False when the MessageEvent's source is null.
expected_has_source,
// The origin of MessageEvents received by |target|.
expected_origin,
// The origin of MessageEvents received by this test runner.
expected_remote_origin) {
await do_send_message_error_test(
test, root_dir, receiver, target, target_origin, expected_has_source,
expected_origin);
await do_receive_message_error_test(
test, receiver, target, target_origin, expected_has_source,
expected_remote_origin);
}
// Runs the same test as do_send_message_error_test(), but uses a MessagePort.
// This test starts by establishing a message channel between the test runner
// and |target|.
async function do_send_message_port_error_test(
test, root_dir, target, target_origin) {
const message_port = create_message_channel(target, target_origin);
await do_send_message_error_test(
test, root_dir, /*receiver=*/message_port, /*target=*/message_port,
/*target_origin=*/undefined, /*expected_has_source=*/false,
/*expected_origin=*/'', /*expected_remote_origin=*/'');
}
// Runs the same test as do_receive_message_error_test(), but uses a MessagePort.
async function do_receive_message_port_error_test(
test, target, target_origin) {
const message_port = create_message_channel(target, target_origin);
await do_receive_message_error_test(
test, /*receiver=*/message_port, /*target=*/message_port,
/*target_origin=*/undefined, /*expected_has_source=*/false,
/*expected_origin=*/'');
}
// Runs the same test as do_send_and_receive_message_error_test(), but uses a
// MessagePort.
async function do_send_and_receive_message_port_error_test(
test, root_dir, target, target_origin) {
await do_send_message_port_error_test(
test, root_dir, target, target_origin);
await do_receive_message_port_error_test(
test, target, target_origin);
}
directory_test(async (t, root_dir) => {
const iframe = await add_iframe(
t, { src: kRemoteOriginDocumentMessageTarget });
await do_send_and_receive_message_error_test(
t, root_dir, /*receiver=*/self, /*target=*/iframe.contentWindow,
/*target_origin=*/'*', /*expected_has_source=*/true,
/*expected_origin=*/location.origin,
/*expected_remote_origin=*/kRemoteOrigin);
}, 'Fail to send and receive messages using a cross origin iframe.');
directory_test(async (t, root_dir) => {
const iframe = await add_iframe(t, { src: kRemoteOriginDocumentMessageTarget });
await do_send_and_receive_message_port_error_test(
t, root_dir, /*target=*/iframe.contentWindow, /*target_origin=*/'*');
}, 'Fail to send and receive messages using a cross origin message port in ' +
'an iframe.');
directory_test(async (t, root_dir) => {
const iframe = await add_iframe(
t, { src: kDocumentMessageTarget, sandbox: 'allow-scripts' });
await do_send_message_error_test(
t, root_dir, /*receiver=*/self, /*target=*/iframe.contentWindow,
/*target_origin=*/'*', /*expected_has_source*/true,
/*expected_origin=*/location.origin);
// https://crbug.com/1014248 Should sandboxed iframes expose the
// NativeFileSystem?
//
// await do_receive_message_error_test(
// t, /*receiver=*/self, /*target=*/iframe.contentWindow,
// /*target_origin=*/'*', /*expected_has_source=*/true,
// /*expected_origin=*/kRemoteOrigin);
}, 'Fail to send and receive messages using a sandboxed iframe.');
directory_test(async (t, root_dir) => {
const iframe = await add_iframe(
t, { src: kDocumentMessageTarget, sandbox: 'allow-scripts' });
await do_send_message_port_error_test(
t, root_dir, /*target=*/iframe.contentWindow, /*target_origin=*/'*');
// https://crbug.com/1014248 Should sandboxed iframes expose the
// NativeFileSystem?
//
// await do_receive_message_port_error_test(
// t, /*target=*/iframe.contentWindow, /*target_origin=*/'*');
}, 'Fail to send and receive messages using a message port in a sandboxed ' +
'iframe.');
directory_test(async (t, root_dir) => {
const iframe_data_uri = await create_message_target_data_uri(t);
const iframe = await add_iframe(t, { src: iframe_data_uri });
await do_send_message_error_test(t, root_dir, /*receiver=*/self,
/*target=*/iframe.contentWindow, /*target_origin=*/'*',
/*expected_has_source*/true, /*expected_origin=*/location.origin);
// Do not test receiving FileSystemHandles from the data URI iframe.
// Data URI iframes are insecure and do not expose the NativeFileSystem APIs.
}, 'Fail to send messages to a data URI iframe.');
directory_test(async (t, root_dir) => {
const iframe_data_uri = await create_message_target_data_uri(t);
const iframe = await add_iframe(t, { src: iframe_data_uri });
await do_send_message_port_error_test(
t, root_dir, /*target=*/iframe.contentWindow, /*target_origin=*/'*');
}, 'Fail to send messages using a message port in a data URI iframe.');
directory_test(async (t, root_dir) => {
const child_window = await open_window(t, kRemoteOriginDocumentMessageTarget);
await do_send_and_receive_message_error_test(
t, root_dir, /*receiver=*/self, /*target=*/child_window, /*target_origin=*/'*',
/*expected_has_source=*/true, /*expected_origin=*/location.origin,
/*expected_remote_origin=*/kRemoteOrigin);
}, 'Fail to send and receive messages using a cross origin window.');
directory_test(async (t, root_dir) => {
const child_window = await open_window(t, kRemoteOriginDocumentMessageTarget);
await do_send_message_port_error_test(
t, root_dir, /*target=*/child_window, /*target_origin=*/'*');
}, 'Fail to send and receive messages using a cross origin message port in ' +
'a window.');
directory_test(async (t, root_dir) => {
const url = `${kDocumentMessageTarget}?pipe=header(Content-Security-Policy` +
', sandbox allow-scripts)';
const child_window = await open_window(t, url);
await do_send_message_error_test(
t, root_dir, /*receiver=*/self, /*target=*/child_window,
/*target_origin=*/'*', /*expected_has_source*/true,
/*expected_origin=*/location.origin);
// https://crbug.com/1014248 Should sandboxed windows expose the
// NativeFileSystem?
//
// await do_receive_message_error_test(
// t, /*receiver=*/self, /*target=*/child_window,
// /*target_origin=*/'*', /*expected_has_source=*/true,
// /*expected_origin=*/kRemoteOrigin);
}, 'Fail to send and receive messages using a sandboxed window.');
directory_test(async (t, root_dir) => {
const url = `${kDocumentMessageTarget}?pipe=header(Content-Security-Policy` +
', sandbox allow-scripts)';
const child_window = await open_window(t, url);
await do_send_message_port_error_test(
t, root_dir, /*target=*/child_window, /*target_origin=*/'*');
// https://crbug.com/1014248 Should sandboxed windows expose the
// NativeFileSystem?
//
// await do_receive_message_port_error_test(
// t, /*target=*/child_window, /*target_origin=*/'*');
}, 'Fail to send and receive messages using a message port in a sandboxed ' +
'window.');

View file

@ -0,0 +1,97 @@
'use strict';
// This script depends on the following scripts:
// /native-file-system/resources/messaging-helpers.js
// /native-file-system/resources/messaging-blob-helpers.js
// /native-file-system/resources/messaging-serialize-helpers.js
// /native-file-system/resources/test-helpers.js
// /service-workers/service-worker/resources/test-helpers.sub.js
// Runs the same test as do_post_message_test(), but uses a MessagePort.
// This test starts by establishing a message channel between the test runner
// and |target|. Afterwards, the test sends FileSystemHandles through the
// message port channel.
async function do_message_port_test(test, root_dir, target, target_origin) {
const message_port = create_message_channel(target, target_origin);
await do_post_message_test(
test, root_dir, /*receiver=*/message_port, /*target=*/message_port);
}
directory_test(async (t, root_dir) => {
const iframe = await add_iframe(t, { src: kDocumentMessageTarget });
await do_message_port_test(
t, root_dir, /*target=*/iframe.contentWindow, /*target_origin=*/'*');
}, 'Send and receive messages using a message port in a same origin ' +
'iframe.');
directory_test(async (t, root_dir) => {
const iframe = await add_iframe(t, {
src: kDocumentMessageTarget,
sandbox: 'allow-scripts allow-same-origin'
});
await do_message_port_test(
t, root_dir, /*target=*/iframe.contentWindow, /*target_origin=*/'*');
}, 'Send and receive messages using a message port in a sandboxed same ' +
'origin iframe.');
directory_test(async (t, root_dir) => {
const blob_url = await create_message_target_blob_url(t);
const iframe = await add_iframe(t, { src: blob_url });
await do_message_port_test(
t, root_dir, /*target=*/iframe.contentWindow, /*target_origin=*/'*');
}, 'Send and receive messages using a message port in a blob iframe.');
directory_test(async (t, root_dir) => {
const iframe_html = await create_message_target_html_without_subresources(t);
const iframe = await add_iframe(t, { srcdoc: iframe_html });
await do_message_port_test(
t, root_dir, /*target=*/iframe.contentWindow, /*target_origin=*/'*');
}, 'Send and receive messages using a message port in an iframe srcdoc.');
directory_test(async (t, root_dir) => {
const child_window = await open_window(t, kDocumentMessageTarget);
await do_message_port_test(
t, root_dir, /*target=*/child_window, /*target_origin=*/'*');
}, 'Send and receive messages using a message port in a same origin ' +
'window.');
directory_test(async (t, root_dir) => {
const blob_url = await create_message_target_blob_url(t);
const child_window = await open_window(t, blob_url);
await do_message_port_test(
t, root_dir, /*target=*/child_window, /*target_origin=*/'*');
}, 'Send and receive messages using a message port in a blob window.');
directory_test(async (t, root_dir) => {
const url = `${kDocumentMessageTarget}?pipe=header(Content-Security-Policy` +
', sandbox allow-scripts allow-same-origin)';
const child_window = await open_window(t, url);
await do_message_port_test(
t, root_dir, /*target=*/child_window, /*target_origin=*/'*');
}, 'Send and receive messages using a message port in a sandboxed same ' +
'origin window.');
directory_test(async (t, root_dir) => {
const dedicated_worker =
create_dedicated_worker(t, kDedicatedWorkerMessageTarget);
await do_message_port_test(t, root_dir, /*target=*/dedicated_worker);
}, 'Send and receive messages using a message port in a dedicated ' +
'worker.');
directory_test(async (t, root_dir) => {
const scope = `${kServiceWorkerMessageTarget}` +
'?post-message-to-message-port-with-file-handle';
const registration = await create_service_worker(
t, kServiceWorkerMessageTarget, scope);
await do_message_port_test(t, root_dir, /*target=*/registration.installing);
}, 'Send and receive messages using a message port in a service ' +
'worker.');
if (self.SharedWorker !== undefined) {
directory_test(async (t, root_dir) => {
const shared_worker = new SharedWorker(kSharedWorkerMessageTarget);
shared_worker.port.start();
await do_message_port_test(t, root_dir, /*target=*/shared_worker.port);
}, 'Send and receive messages using a message port in a shared ' +
' worker.');
}

View file

@ -0,0 +1,91 @@
'use strict';
// This script depends on the following scripts:
// /native-file-system/resources/messaging-helpers.js
// /native-file-system/resources/messaging-blob-helpers.js
// /native-file-system/resources/messaging-serialize-helpers.js
// /native-file-system/resources/test-helpers.js
// /service-workers/service-worker/resources/test-helpers.sub.js
directory_test(async (t, root_dir) => {
const iframe = await add_iframe(t, { src: kDocumentMessageTarget });
await do_post_message_test(
t, root_dir, /*receiver=*/self, /*target=*/iframe.contentWindow,
/*target_origin=*/'*');
}, 'Send and receive messages using a same origin iframe.');
directory_test(async (t, root_dir) => {
const iframe = await add_iframe(t, {
src: kDocumentMessageTarget,
sandbox: 'allow-scripts allow-same-origin'
});
await do_post_message_test(
t, root_dir, /*receiver=*/self, /*target=*/iframe.contentWindow,
/*target_origin=*/'*');
}, 'Send and receive messages using a sandboxed same origin iframe.');
directory_test(async (t, root_dir) => {
const blob_url = await create_message_target_blob_url(t);
const iframe = await add_iframe(t, { src: blob_url });
await do_post_message_test(
t, root_dir, /*receiver=*/self, /*target=*/iframe.contentWindow,
/*target_origin=*/'*');
}, 'Send and receive messages using a blob iframe.');
directory_test(async (t, root_dir) => {
const iframe_html = await create_message_target_html_without_subresources(t);
const iframe = await add_iframe(t, { srcdoc: iframe_html });
await do_post_message_test(
t, root_dir, /*receiver=*/self, /*target=*/iframe.contentWindow,
/*target_origin=*/'*');
}, 'Send and receive messages using an iframe srcdoc.');
directory_test(async (t, root_dir) => {
const child_window = await open_window(t, kDocumentMessageTarget);
await do_post_message_test(
t, root_dir, /*receiver=*/self, /*target=*/child_window,
/*target_origin=*/'*');
}, 'Send and receive messages using a same origin window.');
directory_test(async (t, root_dir) => {
const blob_url = await create_message_target_blob_url(t);
const child_window = await open_window(t, blob_url);
await do_post_message_test(
t, root_dir, /*receiver=*/self, /*target=*/child_window,
/*target_origin=*/'*');
}, 'Send and receive messages using a blob window.');
directory_test(async (t, root_dir) => {
const url = `${kDocumentMessageTarget}?pipe=header(Content-Security-Policy` +
', sandbox allow-scripts allow-same-origin)';
const child_window = await open_window(t, url);
await do_post_message_test(
t, root_dir, /*receiver=*/self, /*target=*/child_window,
/*target_origin=*/'*');
}, 'Send and receive messages using a sandboxed same origin window.');
directory_test(async (t, root_dir) => {
const dedicated_worker =
create_dedicated_worker(t, kDedicatedWorkerMessageTarget);
await do_post_message_test(
t, root_dir, /*receiver=*/dedicated_worker, /*target=*/dedicated_worker);
}, 'Send and receive messages using a dedicated worker.');
directory_test(async (t, root_dir) => {
const scope = `${kServiceWorkerMessageTarget}?post-message-with-file-handle`;
const registration = await create_service_worker(
t, kServiceWorkerMessageTarget, scope);
await do_post_message_test(
t, root_dir, /*receiver=*/navigator.serviceWorker,
/*target=*/registration.installing);
}, 'Send and receive messages using a service worker.');
if (self.SharedWorker !== undefined) {
directory_test(async (t, root_dir) => {
const shared_worker = new SharedWorker(kSharedWorkerMessageTarget);
shared_worker.port.start();
await do_post_message_test(
t, root_dir, /*receiver=*/shared_worker.port,
/*target=*/shared_worker.port);
}, 'Send and receive messages using a shared worker.');
}