aboutsummaryrefslogtreecommitdiffstats
path: root/tests/wpt/web-platform-tests/native-file-system/FileSystemWriter.tentative.window.js
blob: cc8444237b6576f5425f74c0c3c5b12d0f644cd0 (plain) (blame)
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
// 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 writer = await handle.createWriter();

    await writer.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 writer = await handle.createWriter();

    await writer.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 writer = await handle.createWriter();

    await writer.write(0, new Blob(['1234567890']));
    await writer.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 writer = await handle.createWriter();

    await promise_rejects(t, 'InvalidStateError', writer.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, 'trunc_shrink');
    const writer = await handle.createWriter();

    await writer.write(0, new Blob(['1234567890']));
    await writer.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 writer = await handle.createWriter();

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

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

promise_test(async t => {
    const handle = await createEmptyFile(t, 'write_stream');
    const writer = await handle.createWriter();

    const stream = new Response('1234567890').body;
    await writer.write(0, stream);

    assert_equals(await getFileContents(handle), '1234567890');
    assert_equals(await getFileSize(handle), 10);
}, 'write() called with a ReadableStream');

promise_test(async t => {
    const handle = await createEmptyFile(t, 'write_stream');
    const handle_writer = await handle.createWriter();

    const { writable, readable } = new TransformStream();
    const write_result = handle_writer.write(0, readable);

    const stream_writer = writable.getWriter();
    stream_writer.write(new Uint8Array([0x73, 0x74, 0x72, 0x65, 0x61, 0x6D, 0x73, 0x21]));
    garbageCollect();
    stream_writer.write(new Uint8Array([0x21, 0x21]));
    stream_writer.close();

    await write_result;

    assert_equals(await getFileContents(handle), 'streams!!!');
    assert_equals(await getFileSize(handle), 10);
}, 'Using a WritableStream writer to write');