1// META: global=window,worker
2'use strict';
3
4class ThrowingOptions {
5  constructor(whatShouldThrow) {
6    this.whatShouldThrow = whatShouldThrow;
7    this.touched = [];
8  }
9
10  get preventClose() {
11    this.maybeThrow('preventClose');
12    return false;
13  }
14
15  get preventAbort() {
16    this.maybeThrow('preventAbort');
17    return false;
18  }
19
20  get preventCancel() {
21    this.maybeThrow('preventCancel');
22    return false;
23  }
24
25  get signal() {
26    this.maybeThrow('signal');
27    return undefined;
28  }
29
30  maybeThrow(forWhat) {
31    this.touched.push(forWhat);
32    if (this.whatShouldThrow === forWhat) {
33      throw new Error(this.whatShouldThrow);
34    }
35  }
36}
37
38const checkOrder = ['preventAbort', 'preventCancel', 'preventClose', 'signal'];
39
40for (let i = 0; i < checkOrder.length; ++i) {
41  const whatShouldThrow = checkOrder[i];
42  const whatShouldBeTouched = checkOrder.slice(0, i + 1);
43
44  promise_test(t => {
45    const options = new ThrowingOptions(whatShouldThrow);
46    return promise_rejects_js(
47               t, Error,
48               new ReadableStream().pipeTo(new WritableStream(), options),
49               'pipeTo should reject')
50        .then(() => assert_array_equals(
51            options.touched, whatShouldBeTouched,
52            'options should be touched in the right order'));
53  }, `pipeTo should stop after getting ${whatShouldThrow} throws`);
54
55  test(() => {
56    const options = new ThrowingOptions(whatShouldThrow);
57    assert_throws_js(
58        Error,
59        () => new ReadableStream().pipeThrough(new TransformStream(), options),
60        'pipeThrough should throw');
61    assert_array_equals(
62        options.touched, whatShouldBeTouched,
63        'options should be touched in the right order');
64  }, `pipeThrough should stop after getting ${whatShouldThrow} throws`);
65}
66