Revision control

1
<!DOCTYPE html>
2
<script src='../resources/testharness.js'></script>
3
<script src='../resources/testharnessreport.js'></script>
4
<script>
5
test(function() {
6
const PromiseBackup = Promise;
7
8
try {
9
Promise = function() { assert_unreached("streams should not use this Promise object"); };
10
11
new ReadableStream();
12
new WritableStream();
13
} finally {
14
Promise = PromiseBackup;
15
}
16
}, 'Streams and promises: replace Promise constructor');
17
18
test(function() {
19
const PromiseResolveBackup = Promise.resolve;
20
21
try {
22
Promise.resolve = function() { assert_unreached("streams should not use this Promise.resolve method"); };
23
24
new ReadableStream();
25
new WritableStream();
26
} finally {
27
Promise.resolve = PromiseResolveBackup;
28
}
29
}, 'Streams and promises: replace Promise.resolve');
30
31
test(function() {
32
const PromiseRejectBackup = Promise.reject;
33
34
function handle() { }
35
36
try {
37
Promise.reject = function() { assert_unreached("streams should not use this Promise.reject method"); };
38
39
ReadableStream.prototype.cancel.call({}, "reason").then(handle, handle);
40
WritableStream.prototype.abort.call({}, "reason").then(handle, handle);
41
} finally {
42
Promise.reject = PromiseRejectBackup;
43
}
44
}, 'Streams and promises: replace Promise.reject');
45
46
test(function() {
47
function createMangledPromise() {
48
const promise = Promise.resolve();
49
Object.setPrototypeOf(promise, { constructor: Promise, then: function() { assert_unreached("streams should not use this promise then method"); } });
50
return promise;
51
}
52
new ReadableStream({ start: function() { return createMangledPromise(); } })
53
new WritableStream({ start: function() { return createMangledPromise(); } })
54
}, 'Streams and promises: replace prototype of a promise object');
55
56
test(function() {
57
const PromiseThenBackup = Promise.prototype.then;
58
59
try {
60
Promise.prototype.then = function() { assert_unreached("streams should not use this Promise.prototype.then method"); };
61
62
new ReadableStream();
63
new WritableStream();
64
} finally {
65
Promise.prototype.then = PromiseThenBackup;
66
}
67
}, 'Streams and promises: replace then method in Promise prototype');
68
69
test(function() {
70
const PromiseCatchBackup = Promise.prototype.catch;
71
const PromiseThenBackup = Promise.prototype.then;
72
73
try {
74
Promise.prototype.catch = function() { assert_unreached("streams should not use this Promise.prototype.catch method"); };
75
Promise.prototype.then = function() { assert_unreached("streams should not use this Promise.prototype.catch method"); };
76
77
const rs = new ReadableStream();
78
rs.tee();
79
} finally {
80
Promise.prototype.catch = PromiseCatchBackup;
81
Promise.prototype.then = PromiseThenBackup;
82
}
83
}, 'Streams and promises: replace catch method in Promise prototype');
84
85
test(function() {
86
function createMangledPromise() {
87
const promise = Promise.resolve();
88
promise.then = function() { assert_unreached("streams should not use this promise then method"); };
89
return promise;
90
}
91
new ReadableStream({ start: function() { return createMangledPromise(); } })
92
new WritableStream({ start: function() { return createMangledPromise(); } })
93
}, 'Streams and promises: replace then method in promise object');
94
95
test(function() {
96
const NumberBackup = Number;
97
const NumberIsNaNBackup = Number.isNaN;
98
const NumberIsFiniteBackup = Number.isFinite;
99
100
try {
101
Number.isNaN = function() { assert_unreached("streams should not use this Number.isNaN method"); };
102
Number.isFinite = function() { assert_unreached("streams should not use this Number.isFinite method"); };
103
Number = null;
104
105
new ReadableStream({
106
start: function(controller) {
107
controller.enqueue("small potato");
108
}
109
}, {
110
size: function(chunk) { return 2; },
111
highWaterMark: 1
112
});
113
114
} finally {
115
Number = NumberBackup;
116
Number.isNaN = NumberIsNaNBackup;
117
Number.isFinite = NumberIsFiniteBackup;
118
}
119
}, 'Streams should not directly use Number and related methods');
120
test(function() {
121
const ReadableStreamGetReaderBackup = ReadableStream.prototype.getReader;
122
123
try {
124
ReadableStream.prototype.getReader = function() { assert_unreached("streams should not use this ReadableStream.getReader method"); };
125
new ReadableStream().tee();
126
} finally {
127
ReadableStream.prototype.getReader = ReadableStreamGetReaderBackup;
128
}
129
}, 'Streams should not directly use ReadableStream public APIs');
130
131
promise_test(function() {
132
const ReadableStreamDefaultReader = new ReadableStream().getReader().constructor;
133
const ReadableStreamDefaultReaderReadBackup = ReadableStreamDefaultReader.prototype.read;
134
135
function cleanTest() {
136
ReadableStreamDefaultReader.prototype.read = ReadableStreamDefaultReaderReadBackup;
137
}
138
139
try {
140
ReadableStreamDefaultReader.prototype.read = function() { assert_unreached("streams should not use this ReadableStreamDefaultReader.read method"); };
141
142
[s1, s2] = new ReadableStream({
143
start: function(controller) {
144
controller.close();
145
}
146
}).tee();
147
return s1.getReader().closed.then(cleanTest, cleanTest);
148
149
} catch (error) {
150
cleanTest();
151
assert_unreached("test should not throw");
152
}
153
}, 'Streams should not directly use ReadableStreamDefaultReader read public API');
154
</script>