Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <!DOCTYPE html> | |
| 2 <html> | |
| 3 <body> | |
|
tyoshino (SeeGerritForStatus)
2014/08/14 04:39:09
feel free to omit <html> and <body>
yhirano
2014/08/14 06:48:53
Done.
| |
| 4 <script src="../resources/testharness.js"></script> | |
| 5 <script src="../resources/testharnessreport.js"></script> | |
| 6 <script type="text/javascript"> | |
| 7 var test = async_test('Test response of XMLHttpRequest with responseType set to "stream" for various readyState.'); | |
| 8 | |
| 9 test.step(function() | |
| 10 { | |
| 11 var xhr = new XMLHttpRequest; | |
| 12 | |
| 13 xhr.responseType = 'stream'; | |
| 14 assert_equals(xhr.responseType, 'stream', 'xhr.responseType'); | |
| 15 | |
| 16 assert_equals(xhr.readyState, xhr.UNSENT, 'xhr.readyState'); | |
| 17 assert_equals(xhr.response, null, 'xhr.response during UNSENT'); | |
| 18 | |
| 19 var seenStates = []; | |
| 20 | |
| 21 function readStream(stream) { | |
| 22 var chunks = []; | |
| 23 function rec(resolve, reject) { | |
| 24 stream.wait().then(function() { | |
| 25 if (stream.state === 'closed') { | |
| 26 resolve(chunks); | |
| 27 return; | |
| 28 } | |
| 29 chunks.push(stream.read()); | |
| 30 rec(resolve, reject); | |
|
tyoshino (SeeGerritForStatus)
2014/08/14 04:39:09
this works, but to follow the design philosophy of
yhirano
2014/08/14 06:48:53
Done.
I'd leave the first stream.wait to avoid pla
yhirano
2014/08/14 06:49:39
Sorry, this comment was wrong.
| |
| 31 }).catch(reject); | |
| 32 } | |
| 33 return new Promise(rec); | |
| 34 } | |
| 35 var streamPromise = undefined; | |
| 36 | |
| 37 xhr.onreadystatechange = test.step_func(function() { | |
| 38 // onreadystatechange can be invoked multiple times in LOADING state. | |
| 39 if (seenStates.length == 0 || xhr.readyState != seenStates[seenStates.le ngth - 1]) | |
| 40 seenStates.push(xhr.readyState); | |
| 41 | |
| 42 switch (xhr.readyState) { | |
| 43 case xhr.UNSENT: | |
| 44 assert_unreached('Unexpected readyState: UNSENT'); | |
| 45 return; | |
| 46 | |
| 47 case xhr.OPENED: | |
| 48 assert_equals(xhr.response, null, 'xhr.response during OPENED'); | |
| 49 return; | |
| 50 | |
| 51 case xhr.HEADERS_RECEIVED: | |
| 52 assert_equals(xhr.response, null, 'xhr.response during HEADERS_RECEI VED'); | |
| 53 return; | |
| 54 | |
| 55 case xhr.LOADING: | |
| 56 assert_not_equals(xhr.response, null, 'xhr.response during LOADING') ; | |
| 57 assert_true(xhr.response instanceof ReadableStream, | |
| 58 'xhr.response should be ReadableStream during LOADING'); | |
|
tyoshino (SeeGerritForStatus)
2014/08/14 04:39:09
be consistent about how you indent wrapped lines (
yhirano
2014/08/14 06:48:53
Done.
| |
| 59 if (streamPromise === undefined) { | |
| 60 streamPromise = readStream(xhr.response); | |
| 61 } | |
| 62 return; | |
| 63 | |
| 64 case xhr.DONE: | |
| 65 assert_equals(xhr.status, 200, 'xhr.status'); | |
| 66 | |
| 67 // Check that we saw all states. | |
| 68 assert_array_equals(seenStates, | |
| 69 [xhr.OPENED, xhr.HEADERS_RECEIVED, xhr.LOADING, xhr.DONE]); | |
| 70 | |
| 71 assert_not_equals(streamPromise, undefined, 'streamPromise'); | |
| 72 streamPromise.then(test.step_func(function(chunks) { | |
| 73 assert_equals(xhr.response.state, 'closed', 'stream status'); | |
| 74 var size = 0; | |
| 75 for (var i = 0; i < chunks.length; ++i) { | |
| 76 size += chunks[i].byteLength; | |
| 77 } | |
| 78 assert_equals(size, 103746, 'response size'); | |
| 79 test.done(); | |
| 80 }), test.step_func(function(e) { | |
| 81 assert_unreached('failed to read the response stream: ' + e); | |
| 82 })); | |
| 83 return; | |
| 84 | |
| 85 default: | |
| 86 assert_unreached('Unexpected readyState: ' + xhr.readyState) | |
| 87 return; | |
| 88 } | |
| 89 }); | |
| 90 | |
| 91 xhr.open('GET', '../resources/test.ogv', true); | |
| 92 xhr.send(); | |
| 93 }); | |
| 94 </script> | |
| 95 </body> | |
| 96 </html> | |
| OLD | NEW |