|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <!-- |
|
4 https://bugzilla.mozilla.org/show_bug.cgi?id=218236 |
|
5 --> |
|
6 <head> |
|
7 <title>Test for Bug 218236</title> |
|
8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
|
10 </head> |
|
11 <body> |
|
12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=218236">Mozilla Bug 218236</a> |
|
13 <p id="display"></p> |
|
14 <div id="content" style="display: none"> |
|
15 |
|
16 </div> |
|
17 <pre id="test"> |
|
18 <script class="testbody" type="text/javascript"> |
|
19 |
|
20 /** Test for Bug 218236 **/ |
|
21 |
|
22 SimpleTest.waitForExplicitFinish(); |
|
23 |
|
24 /* Test data */ |
|
25 |
|
26 var url_200 = window.location.href; |
|
27 var url_404 = url_200.replace(/[^/]+$/, "this_file_is_not_going_to_be_there.dummy"); |
|
28 var url_connection_error = url_200.replace(/^(\w+:\/\/[^/]+?)(:\d+)?\//, "$1:9546/"); |
|
29 |
|
30 // List of tests: name of the test, URL to be requested, expected sequence |
|
31 // of events and optionally a function to be called from readystatechange handler. |
|
32 // Numbers in the list of events are values of XMLHttpRequest.readyState |
|
33 // when readystatechange event is triggered. |
|
34 var tests = [ |
|
35 ["200 OK", url_200, [1, 2, 3, 4, "load"], null], |
|
36 ["404 Not Found", url_404, [1, 2, 3, 4, "load"], null], |
|
37 ["connection error", url_connection_error, [1, 2, 4, "error"], null], |
|
38 ["abort() call on readyState = 1", url_200, [1, 4], null, doAbort1], |
|
39 ["abort() call on readyState = 2", url_200, [1, 2, 4], doAbort2], |
|
40 ]; |
|
41 |
|
42 var testName = null; |
|
43 var currentState = 0; |
|
44 var currentSequence = null; |
|
45 var expectedSequence = null; |
|
46 var currentCallback = null; |
|
47 var finalizeTimeoutID = null; |
|
48 |
|
49 var request = null; |
|
50 |
|
51 runNextTest(); |
|
52 |
|
53 function doAbort1() { |
|
54 if (request.readyState == 1) |
|
55 request.abort(); |
|
56 } |
|
57 function doAbort2() { |
|
58 if (request.readyState == 2) |
|
59 request.abort(); |
|
60 } |
|
61 |
|
62 /* Utility functions */ |
|
63 |
|
64 function runNextTest() { |
|
65 if (tests.length > 0) { |
|
66 var test = tests.shift(); |
|
67 |
|
68 // Initialize state variables |
|
69 testName = test[0] |
|
70 currentState = 0; |
|
71 currentSequence = []; |
|
72 expectedSequence = test[2]; |
|
73 currentCallback = test[3]; |
|
74 postSendCallback = test[4]; |
|
75 |
|
76 // Prepare request object |
|
77 request = new XMLHttpRequest(); |
|
78 request.onreadystatechange = onReadyStateChange; |
|
79 request.open("GET", test[1]); |
|
80 request.onload = onLoad; |
|
81 request.onerror = onError; |
|
82 |
|
83 // Start request |
|
84 request.send(null); |
|
85 if (postSendCallback) |
|
86 postSendCallback(); |
|
87 } |
|
88 else |
|
89 SimpleTest.finish(); |
|
90 } |
|
91 |
|
92 function finalizeTest() { |
|
93 finalizeTimeoutID = null; |
|
94 ok(compareArrays(expectedSequence, currentSequence), "event sequence for '" + testName + "' was " + currentSequence.join(", ")); |
|
95 |
|
96 runNextTest(); |
|
97 } |
|
98 |
|
99 function onReadyStateChange() { |
|
100 clearTimeout(finalizeTimeoutID); |
|
101 finalizeTimeoutID = null; |
|
102 |
|
103 currentState = request.readyState; |
|
104 currentSequence.push(currentState); |
|
105 |
|
106 if (currentState == 4) { |
|
107 // Allow remaining event to fire but then we are finished with this test |
|
108 // unless we get another onReadyStateChange in which case we'll cancel |
|
109 // this timeout |
|
110 finalizeTimeoutID = setTimeout(finalizeTest, 0); |
|
111 } |
|
112 |
|
113 if (currentCallback) |
|
114 currentCallback(); |
|
115 } |
|
116 |
|
117 function onLoad() { |
|
118 currentSequence.push("load"); |
|
119 } |
|
120 |
|
121 function onError() { |
|
122 currentSequence.push("error"); |
|
123 } |
|
124 |
|
125 function compareArrays(array1, array2) { |
|
126 if (array1.length != array2.length) |
|
127 return false; |
|
128 |
|
129 for (var i = 0; i < array1.length; i++) |
|
130 if (array1[i] != array2[i]) |
|
131 return false; |
|
132 |
|
133 return true; |
|
134 } |
|
135 </script> |
|
136 </pre> |
|
137 </body> |
|
138 </html> |
|
139 |