Add XHR tests checking readyState transition when abort() is invoked in various states
[WebKit-https.git] / LayoutTests / http / tests / xmlhttprequest / readystatechange-and-abort.html
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <script src="../w3c/resources/testharness.js"></script>
5 <script src="../w3c/resources/testharnessreport.js"></script>
6 <script type="text/javascript">
7 test(function()
8 {
9     var xhr = new XMLHttpRequest;
10     xhr.onreadystatechange = this.step_func(function() {
11         assert_unreached("Received readystatechange event unexpectedly: readyState=" + xhr.readyState)
12     });
13     xhr.abort();
14     assert_equals(xhr.readyState, xhr.UNSENT, "xhr.readyState after abort() call");
15 }, "Test onreadystatechange invocation when abort()-ed in UNSENT state.");
16
17 test(function()
18 {
19     var xhr = new XMLHttpRequest;
20     var seenStates = [];
21     xhr.onreadystatechange = this.step_func(function() {
22         seenStates.push(xhr.readyState);
23
24         switch (xhr.readyState) {
25         case xhr.OPENED:
26             return;
27
28         case xhr.UNSENT:
29         case xhr.HEADERS_RECEIVED:
30         case xhr.LOADING:
31         case xhr.DONE:
32         default:
33             assert_unreached("Unexpected readyState: " + xhr.readyState);
34             return;
35         }
36     });
37     xhr.open("GET", "resources/test.ogv", true);
38     xhr.abort();
39     assert_equals(xhr.readyState, xhr.UNSENT, "xhr.readyState after abort() call");
40     assert_array_equals(seenStates, [xhr.OPENED]);
41 }, "Test onreadystatechange invocation when abort()-ed in OPENED state.");
42
43 test(function()
44 {
45     var xhr = new XMLHttpRequest;
46     var seenStates = [];
47     xhr.onreadystatechange = this.step_func(function() {
48         seenStates.push(xhr.readyState);
49
50         switch (xhr.readyState) {
51         case xhr.OPENED:
52         case xhr.DONE:
53             return;
54
55         case xhr.UNSENT:
56         case xhr.HEADERS_RECEIVED:
57         case xhr.LOADING:
58         default:
59             assert_unreached("Unexpected readyState: " + xhr.readyState);
60             return;
61         }
62     });
63     xhr.open("GET", "resources/test.ogv", true);
64     xhr.send();
65     xhr.abort();
66     assert_equals(xhr.readyState, xhr.UNSENT, "xhr.readyState after abort() call");
67     assert_array_equals(seenStates, [xhr.OPENED, xhr.DONE]);
68 }, "Test onreadystatechange invocation when abort()-ed right after calling send().");
69
70 var abortInHeadersReceivedTest = async_test("Test onreadystatechange invocation when abort()-ed in HEADERS_RECEIVED state.");
71 abortInHeadersReceivedTest.step(function()
72 {
73     var xhr = new XMLHttpRequest;
74     var seenStates = [];
75     xhr.onreadystatechange = abortInHeadersReceivedTest.step_func(function() {
76         seenStates.push(xhr.readyState);
77
78         switch (xhr.readyState) {
79         case xhr.OPENED:
80         case xhr.DONE:
81             return;
82
83         case xhr.HEADERS_RECEIVED:
84             xhr.abort();
85             assert_equals(xhr.readyState, xhr.UNSENT, "xhr.readyState after abort() call");
86             assert_array_equals(seenStates, [xhr.OPENED, xhr.HEADERS_RECEIVED, xhr.DONE]);
87             abortInHeadersReceivedTest.done();
88             return;
89
90         case xhr.UNSENT:
91         case xhr.LOADING:
92         default:
93             assert_unreached("Unexpected readyState: " + xhr.readyState)
94             return;
95         }
96     });
97     xhr.open("GET", "resources/test.ogv", true);
98     xhr.send();
99 });
100
101 var abortInLoadingTest = async_test("Test onreadystatechange invocation when abort()-ed in LOADING state.");
102 abortInLoadingTest.step(function()
103 {
104     var xhr = new XMLHttpRequest;
105     var seenStates = [];
106     xhr.onreadystatechange = abortInLoadingTest.step_func(function() {
107         seenStates.push(xhr.readyState);
108
109         switch (xhr.readyState) {
110         case xhr.OPENED:
111         case xhr.HEADERS_RECEIVED:
112         case xhr.DONE:
113             return;
114
115         case xhr.LOADING:
116             xhr.abort();
117             assert_equals(xhr.readyState, xhr.UNSENT, "xhr.readyState after abort() call");
118             assert_array_equals(seenStates, [xhr.OPENED, xhr.HEADERS_RECEIVED, xhr.LOADING, xhr.DONE]);
119             abortInLoadingTest.done();
120             return;
121
122         case xhr.UNSENT:
123         default:
124             assert_unreached("Unexpected readyState: " + xhr.readyState)
125             return;
126         }
127     });
128     xhr.open("GET", "resources/test.ogv", true);
129     xhr.send();
130 });
131
132 var abortInDoneTest = async_test("Test onreadystatechange invocation when abort()-ed in DONE state.");
133 abortInDoneTest.step(function()
134 {
135     var xhr = new XMLHttpRequest;
136     var seenStates = [];
137     xhr.onreadystatechange = abortInDoneTest.step_func(function() {
138         seenStates.push(xhr.readyState);
139
140         switch (xhr.readyState) {
141         case xhr.OPENED:
142         case xhr.HEADERS_RECEIVED:
143         case xhr.LOADING:
144             return;
145
146         case xhr.DONE:
147             xhr.abort();
148             assert_equals(xhr.readyState, xhr.UNSENT, "xhr.readyState after abort() call");
149             assert_array_equals(seenStates, [xhr.OPENED, xhr.HEADERS_RECEIVED, xhr.LOADING, xhr.DONE]);
150             abortInDoneTest.done();
151             return;
152
153         case xhr.UNSENT:
154         default:
155             assert_unreached("Unexpected readyState: " + xhr.readyState)
156             return;
157         }
158     });
159     xhr.open("GET", "resources/test.ogv", true);
160     xhr.send();
161 });
162 </script>
163 </body>
164 </html>