Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / xmlhttprequest / readystatechange-and-abort.html
blobcab8bb864a81257013bc0c715b822b9707e9875b
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <script src="../resources/testharness.js"></script>
5 <script src="../resources/testharnessreport.js"></script>
6 <script type="text/javascript">
8 // Interval prepared to wait until deciding if 'abort' has been delivered (or not.)
9 var TIME_DELAY_ABORT_DELIVERY = 200;
11 test(function()
13 var xhr = new XMLHttpRequest;
14 xhr.onreadystatechange = this.step_func(function() {
15 assert_unreached("Received readystatechange event unexpectedly: readyState=" + xhr.readyState)
16 });
17 xhr.onabort = this.step_func(function () {
18 assert_unreached("Unexpected 'abort' event in state UNSENT.");
19 });
20 xhr.abort();
21 assert_equals(xhr.readyState, xhr.UNSENT, "xhr.readyState after abort() call");
22 }, "Test onreadystatechange + onabort invocation when abort()-ed in UNSENT state.");
24 test(function()
26 var xhr = new XMLHttpRequest;
27 var seenStates = [];
28 xhr.onreadystatechange = this.step_func(function() {
29 seenStates.push(xhr.readyState);
31 switch (xhr.readyState) {
32 case xhr.OPENED:
33 return;
35 case xhr.UNSENT:
36 case xhr.HEADERS_RECEIVED:
37 case xhr.LOADING:
38 case xhr.DONE:
39 default:
40 assert_unreached("Unexpected readyState: " + xhr.readyState);
41 return;
43 });
44 xhr.onabort = this.step_func(function () {
45 assert_unreached("Unexpected 'abort' event in state OPENED.");
46 });
47 xhr.open("GET", "resources/test.ogv", true);
48 xhr.abort();
49 assert_equals(xhr.readyState, xhr.UNSENT, "xhr.readyState after abort() call");
50 assert_array_equals(seenStates, [xhr.OPENED]);
51 }, "Test onreadystatechange + onabort invocation when abort()-ed in OPENED state.");
53 var abortAfterSendTest = async_test("Test onreadystatechange + onabort invocation when abort()-ed right after calling send().");
54 abortAfterSendTest.step(function()
56 var xhr = new XMLHttpRequest;
57 var seenStates = [];
58 xhr.onreadystatechange = this.step_func(function() {
59 seenStates.push(xhr.readyState);
61 switch (xhr.readyState) {
62 case xhr.OPENED:
63 case xhr.DONE:
64 return;
66 case xhr.UNSENT:
67 case xhr.HEADERS_RECEIVED:
68 case xhr.LOADING:
69 default:
70 assert_unreached("Unexpected readyState: " + xhr.readyState);
71 return;
73 });
74 var abortFired = false;
75 xhr.onabort = function () {
76 abortFired = true;
78 xhr.open("GET", "resources/test.ogv", true);
79 xhr.send();
80 xhr.abort();
81 assert_equals(xhr.readyState, xhr.UNSENT, "xhr.readyState after abort() call");
82 assert_array_equals(seenStates, [xhr.OPENED, xhr.DONE]);
83 setTimeout(abortAfterSendTest.step_func(function () {
84 assert_true(abortFired, "Expected 'abort' event to have fired in sent request.");
85 abortAfterSendTest.done();
86 }), TIME_DELAY_ABORT_DELIVERY);
87 });
89 var abortInHeadersReceivedTest = async_test("Test onreadystatechange + onabort invocation when abort()-ed in HEADERS_RECEIVED state.");
90 abortInHeadersReceivedTest.step(function()
92 var xhr = new XMLHttpRequest;
93 var seenStates = [];
94 xhr.onreadystatechange = abortInHeadersReceivedTest.step_func(function() {
95 seenStates.push(xhr.readyState);
97 switch (xhr.readyState) {
98 case xhr.OPENED:
99 case xhr.DONE:
100 return;
102 case xhr.HEADERS_RECEIVED:
103 xhr.abort();
104 assert_equals(xhr.readyState, xhr.UNSENT, "xhr.readyState after abort() call");
105 assert_array_equals(seenStates, [xhr.OPENED, xhr.HEADERS_RECEIVED, xhr.DONE]);
106 setTimeout(abortInHeadersReceivedTest.step_func(function() {
107 assert_true(abortFired, "Expected 'abort' event to have fired in HEADERS_RECEIVED state.");
108 abortInHeadersReceivedTest.done();
109 }), TIME_DELAY_ABORT_DELIVERY);
110 return;
112 case xhr.UNSENT:
113 case xhr.LOADING:
114 default:
115 assert_unreached("Unexpected readyState: " + xhr.readyState)
116 return;
119 var abortFired = false;
120 xhr.onabort = function () {
121 abortFired = true;
123 xhr.open("GET", "resources/test.ogv", true);
124 xhr.send();
127 var abortInLoadingTest = async_test("Test onreadystatechange + onabort invocation when abort()-ed in LOADING state.");
128 abortInLoadingTest.step(function()
130 var xhr = new XMLHttpRequest;
131 var seenStates = [];
132 xhr.onreadystatechange = abortInLoadingTest.step_func(function() {
133 seenStates.push(xhr.readyState);
135 switch (xhr.readyState) {
136 case xhr.OPENED:
137 case xhr.HEADERS_RECEIVED:
138 case xhr.DONE:
139 return;
141 case xhr.LOADING:
142 xhr.abort();
143 assert_equals(xhr.readyState, xhr.UNSENT, "xhr.readyState after abort() call");
144 assert_array_equals(seenStates, [xhr.OPENED, xhr.HEADERS_RECEIVED, xhr.LOADING, xhr.DONE]);
145 setTimeout(abortInLoadingTest.step_func(function() {
146 assert_true(abortFired, "Expected 'abort' event to have fired in LOADING state.");
147 abortInLoadingTest.done();
148 }), TIME_DELAY_ABORT_DELIVERY);
149 return;
151 case xhr.UNSENT:
152 default:
153 assert_unreached("Unexpected readyState: " + xhr.readyState)
154 return;
157 var abortFired = false;
158 xhr.onabort = function () {
159 abortFired = true;
161 xhr.open("GET", "resources/test.ogv", true);
162 xhr.send();
165 var abortInDoneTest = async_test("Test onreadystatechange + onabort invocation when abort()-ed in DONE state.");
166 abortInDoneTest.step(function()
168 var xhr = new XMLHttpRequest;
169 var seenStates = [];
170 xhr.onreadystatechange = abortInDoneTest.step_func(function() {
171 seenStates.push(xhr.readyState);
173 switch (xhr.readyState) {
174 case xhr.OPENED:
175 case xhr.HEADERS_RECEIVED:
176 case xhr.LOADING:
177 return;
179 case xhr.DONE:
180 xhr.abort();
181 assert_equals(xhr.readyState, xhr.UNSENT, "xhr.readyState after abort() call");
182 assert_array_equals(seenStates, [xhr.OPENED, xhr.HEADERS_RECEIVED, xhr.LOADING, xhr.DONE]);
184 setTimeout(abortInDoneTest.step_func(function() {
185 abortInDoneTest.done();
186 }), TIME_DELAY_ABORT_DELIVERY);
187 return;
189 case xhr.UNSENT:
190 default:
191 assert_unreached("Unexpected readyState: " + xhr.readyState)
192 return;
195 xhr.onabort = abortInDoneTest.step_func(function () {
196 assert_unreached("abort() should not cause the abort event to fire");
198 xhr.open("GET", "resources/test.ogv", true);
199 xhr.send();
201 </script>
202 </body>
203 </html>