Update ReadMe.md
[qtwebkit.git] / LayoutTests / streams / shadowing-Promise.html
blob55ea10ca1568aa177354430ed581c71ef250ee7d
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;
8 try {
9 Promise = function() { assert_unreached("streams should not use this Promise object"); };
11 new ReadableStream();
12 new WritableStream();
13 } finally {
14 Promise = PromiseBackup;
16 }, 'Streams and promises: replace Promise constructor');
18 test(function() {
19 const PromiseResolveBackup = Promise.resolve;
21 try {
22 Promise.resolve = function() { assert_unreached("streams should not use this Promise.resolve method"); };
24 new ReadableStream();
25 new WritableStream();
26 } finally {
27 Promise.resolve = PromiseResolveBackup;
29 }, 'Streams and promises: replace Promise.resolve');
31 test(function() {
32 const PromiseRejectBackup = Promise.reject;
34 function handle() { }
36 try {
37 Promise.reject = function() { assert_unreached("streams should not use this Promise.reject method"); };
39 ReadableStream.prototype.cancel.call({}, "reason").then(handle, handle);
40 WritableStream.prototype.abort.call({}, "reason").then(handle, handle);
41 } finally {
42 Promise.reject = PromiseRejectBackup;
44 }, 'Streams and promises: replace Promise.reject');
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;
52 new ReadableStream({ start: function() { return createMangledPromise(); } })
53 new WritableStream({ start: function() { return createMangledPromise(); } })
54 }, 'Streams and promises: replace prototype of a promise object');
56 test(function() {
57 const PromiseThenBackup = Promise.prototype.then;
59 try {
60 Promise.prototype.then = function() { assert_unreached("streams should not use this Promise.prototype.then method"); };
62 new ReadableStream();
63 new WritableStream();
64 } finally {
65 Promise.prototype.then = PromiseThenBackup;
67 }, 'Streams and promises: replace then method in Promise prototype');
69 test(function() {
70 const PromiseCatchBackup = Promise.prototype.catch;
71 const PromiseThenBackup = Promise.prototype.then;
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"); };
77 const rs = new ReadableStream();
78 rs.tee();
79 } finally {
80 Promise.prototype.catch = PromiseCatchBackup;
81 Promise.prototype.then = PromiseThenBackup;
83 }, 'Streams and promises: replace catch method in Promise prototype');
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;
91 new ReadableStream({ start: function() { return createMangledPromise(); } })
92 new WritableStream({ start: function() { return createMangledPromise(); } })
93 }, 'Streams and promises: replace then method in promise object');
95 test(function() {
96 const NumberBackup = Number;
97 const NumberIsNaNBackup = Number.isNaN;
98 const NumberIsFiniteBackup = Number.isFinite;
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;
105 new ReadableStream({
106 start: function(controller) {
107 controller.enqueue("small potato");
109 }, {
110 size: function(chunk) { return 2; },
111 highWaterMark: 1
114 } finally {
115 Number = NumberBackup;
116 Number.isNaN = NumberIsNaNBackup;
117 Number.isFinite = NumberIsFiniteBackup;
119 }, 'Streams should not directly use Number and related methods');
120 test(function() {
121 const ReadableStreamGetReaderBackup = ReadableStream.prototype.getReader;
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;
129 }, 'Streams should not directly use ReadableStream public APIs');
131 promise_test(function() {
132 const ReadableStreamDefaultReader = new ReadableStream().getReader().constructor;
133 const ReadableStreamDefaultReaderReadBackup = ReadableStreamDefaultReader.prototype.read;
135 function cleanTest() {
136 ReadableStreamDefaultReader.prototype.read = ReadableStreamDefaultReaderReadBackup;
139 try {
140 ReadableStreamDefaultReader.prototype.read = function() { assert_unreached("streams should not use this ReadableStreamDefaultReader.read method"); };
142 [s1, s2] = new ReadableStream({
143 start: function(controller) {
144 controller.close();
146 }).tee();
147 return s1.getReader().closed.then(cleanTest, cleanTest);
149 } catch (error) {
150 cleanTest();
151 assert_unreached("test should not throw");
153 }, 'Streams should not directly use ReadableStreamDefaultReader read public API');
154 </script>