• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// META: global=worker
2
3'use strict';
4
5// The browser is assumed to use the same implementation as for TextDecoder, so
6// this file don't replicate the exhaustive checks it has. It is just a smoke
7// test that non-UTF-8 encodings work at all.
8
9const encodings = [
10  {
11    name: 'UTF-16BE',
12    value: [108, 52],
13    expected: "\u{6c34}",
14    invalid: [0xD8, 0x00]
15  },
16  {
17    name: 'UTF-16LE',
18    value: [52, 108],
19    expected: "\u{6c34}",
20    invalid: [0x00, 0xD8]
21  },
22  {
23    name: 'Shift_JIS',
24    value: [144, 133],
25    expected: "\u{6c34}",
26    invalid: [255]
27  },
28  {
29    name: 'ISO-8859-14',
30    value: [100, 240, 114],
31    expected: "d\u{0175}r",
32    invalid: undefined  // all bytes are treated as valid
33  }
34];
35
36for (const encoding of encodings) {
37  promise_test(async () => {
38    const stream = new TextDecoderStream(encoding.name);
39    const reader = stream.readable.getReader();
40    const writer = stream.writable.getWriter();
41    const writePromise = writer.write(new Uint8Array(encoding.value));
42    const {value, done} = await reader.read();
43    assert_false(done, 'readable should not be closed');
44    assert_equals(value, encoding.expected, 'chunk should match expected');
45    await writePromise;
46  }, `TextDecoderStream should be able to decode ${encoding.name}`);
47
48  if (!encoding.invalid)
49    continue;
50
51  promise_test(async t => {
52    const stream = new TextDecoderStream(encoding.name);
53    const reader = stream.readable.getReader();
54    const writer = stream.writable.getWriter();
55    const writePromise = writer.write(new Uint8Array(encoding.invalid));
56    const closePromise = writer.close();
57    const {value, done} = await reader.read();
58    assert_false(done, 'readable should not be closed');
59    assert_equals(value, '\u{FFFD}', 'output should be replacement character');
60    await Promise.all([writePromise, closePromise]);
61  }, `TextDecoderStream should be able to decode invalid sequences in ` +
62     `${encoding.name}`);
63
64  promise_test(async t => {
65    const stream = new TextDecoderStream(encoding.name, {fatal: true});
66    const reader = stream.readable.getReader();
67    const writer = stream.writable.getWriter();
68    const writePromise = writer.write(new Uint8Array(encoding.invalid));
69    const closePromise = writer.close();
70    await promise_rejects(t, new TypeError(), reader.read(),
71                          'readable should be errored');
72    await promise_rejects(t, new TypeError(),
73                          Promise.all([writePromise, closePromise]),
74                          'writable should be errored');
75  }, `TextDecoderStream should be able to reject invalid sequences in ` +
76     `${encoding.name}`);
77}
78