Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 var global = this; | 1 var global = this; |
| 2 if (global.importScripts) { | 2 if (global.importScripts) { |
| 3 // Worker case | 3 // Worker case |
| 4 importScripts('/resources/testharness.js'); | 4 importScripts('/resources/testharness.js'); |
| 5 } | 5 } |
| 6 | 6 |
| 7 var test = async_test('Test response of XMLHttpRequest with responseType set to "stream" for various readyState.'); | 7 var test = async_test('Test response of XMLHttpRequest with responseType set to "stream" for various readyState.'); |
| 8 | 8 |
| 9 test.step(function() { | 9 test.step(function() { |
| 10 var xhr = new XMLHttpRequest; | 10 var xhr = new XMLHttpRequest; |
| 11 | 11 |
| 12 xhr.responseType = 'stream'; | 12 xhr.responseType = 'stream'; |
| 13 assert_equals(xhr.responseType, 'stream', 'xhr.responseType'); | 13 assert_equals(xhr.responseType, 'stream', 'xhr.responseType'); |
| 14 | 14 |
| 15 assert_equals(xhr.readyState, xhr.UNSENT, 'xhr.readyState'); | 15 assert_equals(xhr.readyState, xhr.UNSENT, 'xhr.readyState'); |
| 16 assert_equals(xhr.response, null, 'xhr.response during UNSENT'); | 16 assert_equals(xhr.response, null, 'xhr.response during UNSENT'); |
| 17 | 17 |
| 18 var seenStates = []; | 18 var seenStates = []; |
| 19 | 19 |
| 20 function readStream(stream) { | 20 function readStream(reader) { |
| 21 var chunks = []; | 21 var chunks = []; |
| 22 function rec(resolve, reject) { | 22 function rec(resolve, reject) { |
| 23 while (stream.state === 'readable') { | 23 while (reader.state === 'readable') { |
| 24 chunks.push(stream.read()); | 24 chunks.push(reader.read()); |
| 25 } | 25 } |
| 26 if (stream.state === 'closed') { | 26 if (reader.state === 'closed') { |
| 27 resolve(chunks); | 27 resolve(chunks); |
| 28 return; | 28 return; |
| 29 } | 29 } |
| 30 stream.ready.then(function() { | 30 reader.ready.then(function() { |
| 31 rec(resolve, reject); | 31 rec(resolve, reject); |
| 32 }).catch(reject); | 32 }).catch(reject); |
| 33 } | 33 } |
| 34 return new Promise(rec); | 34 return new Promise(rec); |
| 35 } | 35 } |
| 36 var streamPromise = undefined; | 36 var streamPromise = undefined; |
| 37 | 37 |
| 38 xhr.onreadystatechange = test.step_func(function() { | 38 xhr.onreadystatechange = test.step_func(function() { |
| 39 // onreadystatechange can be invoked multiple times in LOADING state. | 39 // onreadystatechange can be invoked multiple times in LOADING state. |
| 40 if (seenStates.length == 0 || xhr.readyState != seenStates[seenStates.le ngth - 1]) | 40 if (seenStates.length == 0 || xhr.readyState != seenStates[seenStates.le ngth - 1]) |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 51 | 51 |
| 52 case xhr.HEADERS_RECEIVED: | 52 case xhr.HEADERS_RECEIVED: |
| 53 assert_equals(xhr.response, null, 'xhr.response during HEADERS_RECEI VED'); | 53 assert_equals(xhr.response, null, 'xhr.response during HEADERS_RECEI VED'); |
| 54 return; | 54 return; |
| 55 | 55 |
| 56 case xhr.LOADING: | 56 case xhr.LOADING: |
| 57 assert_not_equals(xhr.response, null, 'xhr.response during LOADING') ; | 57 assert_not_equals(xhr.response, null, 'xhr.response during LOADING') ; |
| 58 assert_true(xhr.response instanceof ReadableStream, | 58 assert_true(xhr.response instanceof ReadableStream, |
| 59 'xhr.response should be ReadableStream during LOADING'); | 59 'xhr.response should be ReadableStream during LOADING'); |
| 60 if (streamPromise === undefined) { | 60 if (streamPromise === undefined) { |
| 61 streamPromise = readStream(xhr.response); | 61 streamPromise = readStream(xhr.response.getReader()); |
| 62 } | 62 } |
| 63 streamPromise.then(test.step_func(function(chunks) { | 63 streamPromise.then(function(chunks) { |
|
tyoshino (SeeGerritForStatus)
2015/03/17 05:14:39
add
test.step_func(
yhirano
2015/03/17 05:24:15
We don't need it because we catch the exception af
tyoshino (SeeGerritForStatus)
2015/03/17 07:09:37
True but it's not 'failed to read the response str
yhirano
2015/03/17 07:21:38
Fixed.
| |
| 64 assert_equals(xhr.status, 200, 'xhr.status'); | 64 assert_equals(xhr.status, 200, 'xhr.status'); |
| 65 | 65 |
| 66 // Check that we saw all states. | 66 // Check that we saw all states. |
| 67 assert_array_equals(seenStates, | 67 assert_array_equals(seenStates, |
| 68 [xhr.OPENED, xhr.HEADERS_RECEIVED, xhr.LOADING, xhr.DONE]); | 68 [xhr.OPENED, xhr.HEADERS_RECEIVED, xhr.LOADING, xhr.DONE]); |
| 69 assert_equals(xhr.response.state, 'closed', 'stream status'); | |
| 70 var size = 0; | 69 var size = 0; |
| 71 for (var i = 0; i < chunks.length; ++i) { | 70 for (var i = 0; i < chunks.length; ++i) { |
| 72 size += chunks[i].byteLength; | 71 size += chunks[i].byteLength; |
| 73 } | 72 } |
| 74 assert_equals(size, 103746, 'response size'); | 73 assert_equals(size, 103746, 'response size'); |
| 74 return xhr.response.closed; | |
| 75 }).then(function() { | |
| 75 test.done(); | 76 test.done(); |
| 76 }), test.step_func(function(e) { | 77 }).catch(test.step_func(function(e) { |
| 77 assert_unreached('failed to read the response stream: ' + e); | 78 assert_unreached('failed to read the response stream: ' + e); |
| 78 })); | 79 })); |
| 79 return; | 80 return; |
| 80 | 81 |
| 81 case xhr.DONE: | 82 case xhr.DONE: |
| 82 return; | 83 return; |
| 83 | 84 |
| 84 default: | 85 default: |
| 85 assert_unreached('Unexpected readyState: ' + xhr.readyState) | 86 assert_unreached('Unexpected readyState: ' + xhr.readyState) |
| 86 return; | 87 return; |
| 87 } | 88 } |
| 88 }); | 89 }); |
| 89 | 90 |
| 90 xhr.open('GET', '/resources/test.ogv', true); | 91 xhr.open('GET', '/resources/test.ogv', true); |
| 91 xhr.send(); | 92 xhr.send(); |
| 92 }); | 93 }); |
| 93 | 94 |
| 94 if (global.importScripts) { | 95 if (global.importScripts) { |
| 95 // Worker case | 96 // Worker case |
| 96 done(); | 97 done(); |
| 97 } | 98 } |
| OLD | NEW |