aboutsummaryrefslogtreecommitdiffstats
path: root/tests/wpt/web-platform-tests/FileAPI/blob/Blob-stream.any.js
blob: 894f09f8ece47cab1301b8b474285204a4b7c4f1 (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
// META: title=Blob Stream
// META: script=../support/Blob.js
'use strict';

// Takes in a ReadableStream and reads from it until it is done, returning
// an array that contains the results of each read operation
async function read_all_chunks(stream) {
  assert_true(stream instanceof ReadableStream);
  assert_true('getReader' in stream);
  const reader = stream.getReader();

  assert_true('read' in reader);
  let read_value = await reader.read();

  let out = [];
  let i = 0;
  while (!read_value.done) {
    for (let val of read_value.value) {
      out[i++] = val;
    }
    read_value = await reader.read();
  }
  return out;
}

promise_test(async () => {
  const blob = new Blob(["PASS"]);
  const stream = await blob.stream()
  const chunks = await read_all_chunks(stream);
  for (let [index, value] of chunks.entries()) {
    assert_equals(value, "PASS".charCodeAt(index));
  }
}, "Blob.stream()")

promise_test(async () => {
  const blob = new Blob();
  const stream = await blob.stream()
  const chunks = await read_all_chunks(stream);

  assert_array_equals(chunks, []);
}, "Blob.stream() empty Blob")

promise_test(async () => {
  const input_arr = [8, 241, 48, 123, 151];
  const typed_arr = new Uint8Array(input_arr);
  const blob = new Blob([typed_arr]);
  const stream = await blob.stream()
  const chunks = await read_all_chunks(stream);
  assert_array_equals(chunks, input_arr)
}, "Blob.stream() non-unicode input")