Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / xmlhttprequest / response-stream.html
1 <!DOCTYPE html>
2 <script src="../resources/testharness.js"></script>
3 <script src="../resources/testharnessreport.js"></script>
4 <script type="text/javascript">
5 var test = async_test('Test response of XMLHttpRequest with responseType set to "stream" for various readyState.');
6
7 test.step(function()
8 {
9     var xhr = new XMLHttpRequest;
10
11     xhr.responseType = 'stream';
12     assert_equals(xhr.responseType, 'stream', 'xhr.responseType');
13
14     assert_equals(xhr.readyState, xhr.UNSENT, 'xhr.readyState');
15     assert_equals(xhr.response, null, 'xhr.response during UNSENT');
16
17     var seenStates = [];
18
19     function readStream(stream) {
20         var chunks = [];
21         function rec(resolve, reject) {
22             while (stream.state === 'readable') {
23                 chunks.push(stream.read());
24             }
25             if (stream.state === 'closed') {
26                 resolve(chunks);
27                 return;
28             }
29             stream.wait().then(function() {
30                 rec(resolve, reject);
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.length - 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_RECEIVED');
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');
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>