11cb0ef41Sopenharmony_ci// META: global=window,worker
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci'use strict';
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// The browser is assumed to use the same implementation as for TextDecoder, so
61cb0ef41Sopenharmony_ci// this file don't replicate the exhaustive checks it has. It is just a smoke
71cb0ef41Sopenharmony_ci// test that non-UTF-8 encodings work at all.
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst encodings = [
101cb0ef41Sopenharmony_ci  {
111cb0ef41Sopenharmony_ci    name: 'UTF-16BE',
121cb0ef41Sopenharmony_ci    value: [108, 52],
131cb0ef41Sopenharmony_ci    expected: "\u{6c34}",
141cb0ef41Sopenharmony_ci    invalid: [0xD8, 0x00]
151cb0ef41Sopenharmony_ci  },
161cb0ef41Sopenharmony_ci  {
171cb0ef41Sopenharmony_ci    name: 'UTF-16LE',
181cb0ef41Sopenharmony_ci    value: [52, 108],
191cb0ef41Sopenharmony_ci    expected: "\u{6c34}",
201cb0ef41Sopenharmony_ci    invalid: [0x00, 0xD8]
211cb0ef41Sopenharmony_ci  },
221cb0ef41Sopenharmony_ci  {
231cb0ef41Sopenharmony_ci    name: 'Shift_JIS',
241cb0ef41Sopenharmony_ci    value: [144, 133],
251cb0ef41Sopenharmony_ci    expected: "\u{6c34}",
261cb0ef41Sopenharmony_ci    invalid: [255]
271cb0ef41Sopenharmony_ci  },
281cb0ef41Sopenharmony_ci  {
291cb0ef41Sopenharmony_ci    name: 'ISO-2022-JP',
301cb0ef41Sopenharmony_ci    value: [65, 66, 67, 0x1B, 65, 66, 67],
311cb0ef41Sopenharmony_ci    expected: "ABC\u{fffd}ABC",
321cb0ef41Sopenharmony_ci    invalid: [0x0E]
331cb0ef41Sopenharmony_ci  },
341cb0ef41Sopenharmony_ci  {
351cb0ef41Sopenharmony_ci    name: 'ISO-8859-14',
361cb0ef41Sopenharmony_ci    value: [100, 240, 114],
371cb0ef41Sopenharmony_ci    expected: "d\u{0175}r",
381cb0ef41Sopenharmony_ci    invalid: undefined  // all bytes are treated as valid
391cb0ef41Sopenharmony_ci  }
401cb0ef41Sopenharmony_ci];
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_cifor (const encoding of encodings) {
431cb0ef41Sopenharmony_ci  promise_test(async () => {
441cb0ef41Sopenharmony_ci    const stream = new TextDecoderStream(encoding.name);
451cb0ef41Sopenharmony_ci    const reader = stream.readable.getReader();
461cb0ef41Sopenharmony_ci    const writer = stream.writable.getWriter();
471cb0ef41Sopenharmony_ci    const writePromise = writer.write(new Uint8Array(encoding.value));
481cb0ef41Sopenharmony_ci    const {value, done} = await reader.read();
491cb0ef41Sopenharmony_ci    assert_false(done, 'readable should not be closed');
501cb0ef41Sopenharmony_ci    assert_equals(value, encoding.expected, 'chunk should match expected');
511cb0ef41Sopenharmony_ci    await writePromise;
521cb0ef41Sopenharmony_ci  }, `TextDecoderStream should be able to decode ${encoding.name}`);
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  if (!encoding.invalid)
551cb0ef41Sopenharmony_ci    continue;
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  promise_test(async t => {
581cb0ef41Sopenharmony_ci    const stream = new TextDecoderStream(encoding.name);
591cb0ef41Sopenharmony_ci    const reader = stream.readable.getReader();
601cb0ef41Sopenharmony_ci    const writer = stream.writable.getWriter();
611cb0ef41Sopenharmony_ci    const writePromise = writer.write(new Uint8Array(encoding.invalid));
621cb0ef41Sopenharmony_ci    const closePromise = writer.close();
631cb0ef41Sopenharmony_ci    const {value, done} = await reader.read();
641cb0ef41Sopenharmony_ci    assert_false(done, 'readable should not be closed');
651cb0ef41Sopenharmony_ci    assert_equals(value, '\u{FFFD}', 'output should be replacement character');
661cb0ef41Sopenharmony_ci    await Promise.all([writePromise, closePromise]);
671cb0ef41Sopenharmony_ci  }, `TextDecoderStream should be able to decode invalid sequences in ` +
681cb0ef41Sopenharmony_ci     `${encoding.name}`);
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  promise_test(async t => {
711cb0ef41Sopenharmony_ci    const stream = new TextDecoderStream(encoding.name, {fatal: true});
721cb0ef41Sopenharmony_ci    const reader = stream.readable.getReader();
731cb0ef41Sopenharmony_ci    const writer = stream.writable.getWriter();
741cb0ef41Sopenharmony_ci    const writePromise = writer.write(new Uint8Array(encoding.invalid));
751cb0ef41Sopenharmony_ci    const closePromise = writer.close();
761cb0ef41Sopenharmony_ci    await promise_rejects_js(t, TypeError, reader.read(),
771cb0ef41Sopenharmony_ci                          'readable should be errored');
781cb0ef41Sopenharmony_ci    await promise_rejects_js(t, TypeError,
791cb0ef41Sopenharmony_ci                          Promise.all([writePromise, closePromise]),
801cb0ef41Sopenharmony_ci                          'writable should be errored');
811cb0ef41Sopenharmony_ci  }, `TextDecoderStream should be able to reject invalid sequences in ` +
821cb0ef41Sopenharmony_ci     `${encoding.name}`);
831cb0ef41Sopenharmony_ci}
84