Skip to content

Commit

Permalink
[Native File System] Writable File Stream API
Browse files Browse the repository at this point in the history
This change adds a writable stream output to the `FileSystemFileHandle`.

This follows some
[discussion](WICG/file-system-access#19 (comment)) on github.

While this writable stream does not yet feature stream-like behavior, it
carries over some of the interface from `NativeFileSystemWriter`, namely
`truncate` and `write`.

Bug: 955189
Change-Id: I8b8c9ff1ef36adfb30501251563699c6c7973889
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1623534
Reviewed-by: Marijn Kruisselbrink <mek@chromium.org>
Reviewed-by: Jeremy Roman <jbroman@chromium.org>
Commit-Queue: Jeremy Roman <jbroman@chromium.org>
Auto-Submit: Olivier Yiptong <oyiptong@chromium.org>
Cr-Commit-Position: refs/heads/master@{#662620}
  • Loading branch information
oyiptong authored and chromium-wpt-export-bot committed May 23, 2019
1 parent 137ac6d commit b2dd1d9
Showing 1 changed file with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// META: script=resources/test-helpers.js
promise_test(async t => cleanupSandboxedFileSystem(),
'Cleanup to setup test environment');

promise_test(async t => {
const handle = await createEmptyFile(t, 'empty_blob');
const stream = await handle.createWritable();

await stream.write(0, new Blob([]));

assert_equals(await getFileContents(handle), '');
assert_equals(await getFileSize(handle), 0);
}, 'write() with an empty blob to an empty file');

promise_test(async t => {
const handle = await createEmptyFile(t, 'valid_blob');
const stream = await handle.createWritable();

await stream.write(0, new Blob(['1234567890']));

assert_equals(await getFileContents(handle), '1234567890');
assert_equals(await getFileSize(handle), 10);
}, 'write() a blob to an empty file');

promise_test(async t => {
const handle = await createEmptyFile(t, 'blob_with_offset');
const stream = await handle.createWritable();

await stream.write(0, new Blob(['1234567890']));
await stream.write(4, new Blob(['abc']));

assert_equals(await getFileContents(handle), '1234abc890');
assert_equals(await getFileSize(handle), 10);
}, 'write() called with a blob and a valid offset');

promise_test(async t => {
const handle = await createEmptyFile(t, 'bad_offset');
const stream = await handle.createWritable();

await promise_rejects(t, 'InvalidStateError', stream.write(4, new Blob(['abc'])));

assert_equals(await getFileContents(handle), '');
assert_equals(await getFileSize(handle), 0);
}, 'write() called with an invalid offset');

promise_test(async t => {
const handle = await createEmptyFile(t, 'empty_string');
const stream = await handle.createWritable();

await stream.write(0, '');
assert_equals(await getFileContents(handle), '');
assert_equals(await getFileSize(handle), 0);
}, 'write() with an empty string to an empty file');

promise_test(async t => {
const handle = await createEmptyFile(t, 'valid_utf8_string');
const stream = await handle.createWritable();

await stream.write(0, 'foo🤘');
assert_equals(await getFileContents(handle), 'foo🤘');
assert_equals(await getFileSize(handle), 7);
}, 'write() with a valid utf-8 string');

promise_test(async t => {
const handle = await createEmptyFile(t, 'string_with_unix_line_ending');
const stream = await handle.createWritable();

await stream.write(0, 'foo\n');
assert_equals(await getFileContents(handle), 'foo\n');
assert_equals(await getFileSize(handle), 4);
}, 'write() with a string with unix line ending preserved');

promise_test(async t => {
const handle = await createEmptyFile(t, 'string_with_windows_line_ending');
const stream = await handle.createWritable();

await stream.write(0, 'foo\r\n');
assert_equals(await getFileContents(handle), 'foo\r\n');
assert_equals(await getFileSize(handle), 5);
}, 'write() with a string with windows line ending preserved');

promise_test(async t => {
const handle = await createEmptyFile(t, 'empty_array_buffer');
const stream = await handle.createWritable();

let buf = new ArrayBuffer(0);
await stream.write(0, buf);
assert_equals(await getFileContents(handle), '');
assert_equals(await getFileSize(handle), 0);
}, 'write() with an empty array buffer to an empty file');

promise_test(async t => {
const handle = await createEmptyFile(t, 'valid_string_typed_byte_array');
const stream = await handle.createWritable();

let buf = new ArrayBuffer(3);
let intView = new Uint8Array(buf);
intView[0] = 0x66;
intView[1] = 0x6f;
intView[2] = 0x6f;
await stream.write(0, buf);
assert_equals(await getFileContents(handle), 'foo');
assert_equals(await getFileSize(handle), 3);
}, 'write() with a valid typed array buffer');

promise_test(async t => {
const handle = await createEmptyFile(t, 'trunc_shrink');
const stream = await handle.createWritable();

await stream.write(0, new Blob(['1234567890']));
await stream.truncate(5);

assert_equals(await getFileContents(handle), '12345');
assert_equals(await getFileSize(handle), 5);
}, 'truncate() to shrink a file');

promise_test(async t => {
const handle = await createEmptyFile(t, 'trunc_grow');
const stream = await handle.createWritable();

await stream.write(0, new Blob(['abc']));
await stream.truncate(5);

assert_equals(await getFileContents(handle), 'abc\0\0');
assert_equals(await getFileSize(handle), 5);
}, 'truncate() to grow a file');

0 comments on commit b2dd1d9

Please sign in to comment.