1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/test/test_bug435425.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,401 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=435425 1.8 +--> 1.9 +<head> 1.10 + <title>Test for Bug 435425</title> 1.11 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.12 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.13 +</head> 1.14 +<body> 1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=435425">Mozilla Bug 435425</a> 1.16 +<p id="display"></p> 1.17 +<div id="content" style="display: none"> 1.18 + 1.19 +</div> 1.20 +<pre id="test"> 1.21 +<script class="testbody" type="text/javascript"> 1.22 + 1.23 +/** Test for Bug 435425 **/ 1.24 + 1.25 +var xhr = null; 1.26 +var upload = null; 1.27 +var currentEvents = null; 1.28 +var expectedResponseText = null; 1.29 +var uploadTotal = 0; 1.30 + 1.31 +function logEvent(evt) { 1.32 + var i = 0; 1.33 + while ((currentEvents.length != i) && 1.34 + currentEvents[i].optional && 1.35 + ((currentEvents[i].type != evt.type) || 1.36 + !(evt.target instanceof currentEvents[i].target))) { 1.37 + ++i; 1.38 + } 1.39 + if (evt.target instanceof XMLHttpRequestUpload) { 1.40 + if (evt.type == "loadstart") { 1.41 + uploadTotal = evt.total 1.42 + } else { 1.43 + if (evt.type == "progress") { 1.44 + ok(evt.lengthComputable, "event(" + evt.type + ").lengthComputable should be true."); 1.45 + } 1.46 + is(evt.total, uploadTotal, "event(" + evt.type + ").total should not change during upload."); 1.47 + } 1.48 + } 1.49 + ok(i != currentEvents.length, "Extra or wrong event?"); 1.50 + is(evt.type, currentEvents[i].type, "Wrong event!") 1.51 + ok(evt.target instanceof currentEvents[i].target, 1.52 + "Wrong event target [" + evt.target + "," + evt.type + "]!"); 1.53 + // If we handled non-optional event, remove all optional events before the 1.54 + // handled event and then the non-optional event from the list. 1.55 + if (!currentEvents[i].optional) { 1.56 + for (;i != -1; --i) { 1.57 + currentEvents.shift(); 1.58 + } 1.59 + } 1.60 +} 1.61 + 1.62 +function hasPendingNonOptionalEvent(ev) { 1.63 + var i = 0; 1.64 + while (i < currentEvents.length) { 1.65 + if (!currentEvents[i].optional && currentEvents[i].type == ev) 1.66 + return true; 1.67 + ++i; 1.68 + } 1.69 + return false; 1.70 +} 1.71 + 1.72 +function maybeStop(evt) { 1.73 + logEvent(evt); 1.74 + if (!hasPendingNonOptionalEvent("loadend")) 1.75 + nextTest(); 1.76 +} 1.77 + 1.78 +function openXHR(xhr, method, url, privileged) { 1.79 + if (privileged) 1.80 + SpecialPowers.wrap(xhr).open(method, url); 1.81 + else 1.82 + xhr.open(method, url); 1.83 +} 1.84 + 1.85 +function start(obj) { 1.86 + xhr = new XMLHttpRequest(); 1.87 + upload = xhr.upload; 1.88 + currentEvents = obj.expectedEvents; 1.89 + expectedResponseText = obj.withUpload; 1.90 + xhr.onload = 1.91 + function(evt) { 1.92 + if (expectedResponseText) { 1.93 + is(evt.target.responseText, expectedResponseText, "Wrong responseText"); 1.94 + } 1.95 + logEvent(evt); 1.96 + } 1.97 + xhr.onerror = 1.98 + function(evt) { 1.99 + logEvent(evt); 1.100 + } 1.101 + xhr.onabort = 1.102 + function(evt) { 1.103 + logEvent(evt); 1.104 + } 1.105 + xhr.onloadend = 1.106 + function (evt) { 1.107 + maybeStop(evt); 1.108 + } 1.109 + xhr.onloadstart = 1.110 + function (evt) { 1.111 + logEvent(evt); 1.112 + } 1.113 + xhr.onprogress = 1.114 + function (evt) { 1.115 + logEvent(evt); 1.116 + } 1.117 + 1.118 + if ("upload" in xhr) { 1.119 + xhr.upload.onloadstart = 1.120 + function (evt) { 1.121 + logEvent(evt); 1.122 + } 1.123 + xhr.upload.onprogress = 1.124 + function (evt) { 1.125 + logEvent(evt); 1.126 + } 1.127 + xhr.upload.onload = 1.128 + function (evt) { 1.129 + logEvent(evt); 1.130 + } 1.131 + xhr.upload.onerror = 1.132 + function (evt) { 1.133 + logEvent(evt); 1.134 + } 1.135 + xhr.upload.onabort = 1.136 + function (evt) { 1.137 + logEvent(evt); 1.138 + } 1.139 + xhr.upload.onloadend = 1.140 + function (evt) { 1.141 + maybeStop(evt); 1.142 + } 1.143 + } 1.144 + 1.145 + try { 1.146 + var methodIsGet = (obj.method == "GET"); 1.147 + var url; 1.148 + var privileged = false; 1.149 + if (obj.testRedirectError) { 1.150 + url = "bug435425_redirect.sjs"; 1.151 + } else if (obj.testNetworkError) { 1.152 + url = "http://nosuchdomain.localhost"; 1.153 + privileged = true; 1.154 + } else { 1.155 + url = "bug435425.sjs"; 1.156 + if (obj.withUpload && methodIsGet) { 1.157 + url += "?" + obj.withUpload; 1.158 + } 1.159 + } 1.160 + openXHR(xhr, obj.method, url, privileged); 1.161 + xhr.send(!methodIsGet ? obj.withUpload : null); 1.162 + if (obj.testAbort) { 1.163 + xhr.abort(); 1.164 + } 1.165 + } catch (ex) { 1.166 + ok(false, ex); 1.167 + } 1.168 +} 1.169 + 1.170 +var none = null; 1.171 +var small = ""; 1.172 +var mid = ""; 1.173 +var large = ""; 1.174 + 1.175 + 1.176 +for (var smallLength = 0; smallLength < (0x00000000 + 2); ++smallLength) { 1.177 + small += "A"; 1.178 +} 1.179 + 1.180 +for (var midLength = 0; midLength < (0x00000FFF + 2); ++midLength) { 1.181 + mid += "A"; 1.182 +} 1.183 + 1.184 +for (var largeLength = 0; largeLength < (0x0000FFFF + 2); ++largeLength) { 1.185 + large += "A"; 1.186 +} 1.187 + 1.188 +const XHR = XMLHttpRequest; 1.189 +const UPLOAD = XMLHttpRequestUpload; 1.190 + 1.191 +var tests = 1.192 + [ 1.193 + { method: "GET", withUpload: none, testAbort: false, testRedirectError: false, testNetworkError: false, 1.194 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.195 + {target: XHR, type: "progress", optional: true}, 1.196 + {target: XHR, type: "load", optional: false}, 1.197 + {target: XHR, type: "loadend", optional: false}]}, 1.198 + { method: "GET", withUpload: none, testAbort: true, testRedirectError: false, testNetworkError: false, 1.199 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.200 + {target: XHR, type: "abort", optional: false}, 1.201 + {target: XHR, type: "loadend", optional: false}]}, 1.202 + { method: "GET", withUpload: none, testAbort: false, testRedirectError: true, testNetworkError: false, 1.203 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.204 + {target: XHR, type: "error", optional: false}, 1.205 + {target: XHR, type: "loadend", optional: false}]}, 1.206 + { method: "GET", withUpload: none, testAbort: false, testRedirectError: false, testNetworkError: true, 1.207 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.208 + {target: XHR, type: "error", optional: false}, 1.209 + {target: XHR, type: "loadend", optional: false}]}, 1.210 + 1.211 + { method: "GET", withUpload: small, testAbort: false, testRedirectError: false, testNetworkError: false, 1.212 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.213 + {target: XHR, type: "progress", optional: true}, 1.214 + {target: XHR, type: "load", optional: false}, 1.215 + {target: XHR, type: "loadend", optional: false}]}, 1.216 + { method: "GET", withUpload: small, testAbort: true, testRedirectError: false, testNetworkError: false, 1.217 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.218 + {target: XHR, type: "abort", optional: false}, 1.219 + {target: XHR, type: "loadend", optional: false}]}, 1.220 + { method: "GET", withUpload: small, testAbort: false, testRedirectError: true, testNetworkError: false, 1.221 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.222 + {target: XHR, type: "error", optional: false}, 1.223 + {target: XHR, type: "loadend", optional: false}]}, 1.224 + { method: "GET", withUpload: small, testAbort: false, testRedirectError: false, testNetworkError: true, 1.225 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.226 + {target: XHR, type: "error", optional: false}, 1.227 + {target: XHR, type: "loadend", optional: false}]}, 1.228 + 1.229 + { method: "GET", withUpload: mid, testAbort: false, testRedirectError: false, testNetworkError: false, 1.230 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.231 + {target: XHR, type: "progress", optional: true}, 1.232 + {target: XHR, type: "load", optional: false}, 1.233 + {target: XHR, type: "loadend", optional: false}]}, 1.234 + { method: "GET", withUpload: mid, testAbort: true, testRedirectError: false, testNetworkError: false, 1.235 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.236 + {target: XHR, type: "abort", optional: false}, 1.237 + {target: XHR, type: "loadend", optional: false}]}, 1.238 + { method: "GET", withUpload: mid, testAbort: false, testRedirectError: true, testNetworkError: false, 1.239 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.240 + {target: XHR, type: "error", optional: false}, 1.241 + {target: XHR, type: "loadend", optional: false}]}, 1.242 + { method: "GET", withUpload: mid, testAbort: false, testRedirectError: false, testNetworkError: true, 1.243 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.244 + {target: XHR, type: "error", optional: false}, 1.245 + {target: XHR, type: "loadend", optional: false}]}, 1.246 + 1.247 + { method: "GET", withUpload: large, testAbort: false, testRedirectError: false, testNetworkError: false, 1.248 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.249 + {target: XHR, type: "progress", optional: true}, 1.250 + {target: XHR, type: "load", optional: false}, 1.251 + {target: XHR, type: "loadend", optional: false}]}, 1.252 + { method: "GET", withUpload: large, testAbort: true, testRedirectError: false, testNetworkError: false, 1.253 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.254 + {target: XHR, type: "abort", optional: false}, 1.255 + {target: XHR, type: "loadend", optional: false}]}, 1.256 + { method: "GET", withUpload: large, testAbort: false, testRedirectError: true, testNetworkError: false, 1.257 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.258 + {target: XHR, type: "error", optional: false}, 1.259 + {target: XHR, type: "loadend", optional: false}]}, 1.260 + { method: "GET", withUpload: large, testAbort: false, testRedirectError: false, testNetworkError: true, 1.261 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.262 + {target: XHR, type: "error", optional: false}, 1.263 + {target: XHR, type: "loadend", optional: false}]}, 1.264 + 1.265 + { method: "POST", withUpload: none, testAbort: false, testRedirectError: false, testNetworkError: false, 1.266 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.267 + {target: XHR, type: "progress", optional: true}, 1.268 + {target: XHR, type: "load", optional: false}, 1.269 + {target: XHR, type: "loadend", optional: false}]}, 1.270 + { method: "POST", withUpload: none, testAbort: true, testRedirectError: false, testNetworkError: false, 1.271 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.272 + {target: XHR, type: "abort", optional: false}, 1.273 + {target: XHR, type: "loadend", optional: false}]}, 1.274 + { method: "POST", withUpload: none, testAbort: false, testRedirectError: true, testNetworkError: false, 1.275 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.276 + {target: XHR, type: "error", optional: false}, 1.277 + {target: XHR, type: "loadend", optional: false}]}, 1.278 + { method: "POST", withUpload: none, testAbort: false, testRedirectError: false, testNetworkError: true, 1.279 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.280 + {target: XHR, type: "error", optional: false}, 1.281 + {target: XHR, type: "loadend", optional: false}]}, 1.282 + 1.283 + { method: "POST", withUpload: small, testAbort: false, testRedirectError: false, testNetworkError: false, 1.284 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.285 + {target: UPLOAD, type: "loadstart", optional: false}, 1.286 + {target: UPLOAD, type: "progress", optional: true}, 1.287 + {target: UPLOAD, type: "load", optional: false}, 1.288 + {target: UPLOAD, type: "loadend", optional: false}, 1.289 + {target: XHR, type: "progress", optional: true}, 1.290 + {target: XHR, type: "load", optional: false}, 1.291 + {target: XHR, type: "loadend", optional: false}]}, 1.292 + { method: "POST", withUpload: small, testAbort: true, testRedirectError: false, testNetworkError: false, 1.293 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.294 + {target: UPLOAD, type: "loadstart", optional: false}, 1.295 + {target: XHR, type: "abort", optional: false}, 1.296 + {target: XHR, type: "loadend", optional: false}, 1.297 + {target: UPLOAD, type: "abort", optional: false}, 1.298 + {target: UPLOAD, type: "loadend", optional: false}]}, 1.299 + { method: "POST", withUpload: small, testAbort: false, testRedirectError: true, testNetworkError: false, 1.300 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.301 + {target: UPLOAD, type: "loadstart", optional: false}, 1.302 + {target: UPLOAD, type: "progress", optional: true}, 1.303 + {target: XHR, type: "error", optional: false}, 1.304 + {target: XHR, type: "loadend", optional: false}, 1.305 + {target: UPLOAD, type: "error", optional: false}, 1.306 + {target: UPLOAD, type: "loadend", optional: false}]}, 1.307 + { method: "POST", withUpload: small, testAbort: false, testRedirectError: false, testNetworkError: true, 1.308 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.309 + {target: UPLOAD, type: "loadstart", optional: false}, 1.310 + {target: UPLOAD, type: "progress", optional: true}, 1.311 + {target: XHR, type: "error", optional: false}, 1.312 + {target: XHR, type: "loadend", optional: false}, 1.313 + {target: UPLOAD, type: "error", optional: false}, 1.314 + {target: UPLOAD, type: "loadend", optional: false}]}, 1.315 + 1.316 + { method: "POST", withUpload: mid, testAbort: false, testRedirectError: false, testNetworkError: false, 1.317 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.318 + {target: UPLOAD, type: "loadstart", optional: false}, 1.319 + {target: UPLOAD, type: "progress", optional: true}, 1.320 + {target: UPLOAD, type: "load", optional: false}, 1.321 + {target: UPLOAD, type: "loadend", optional: false}, 1.322 + {target: XHR, type: "progress", optional: true}, 1.323 + {target: XHR, type: "load", optional: false}, 1.324 + {target: XHR, type: "loadend", optional: false}]}, 1.325 + { method: "POST", withUpload: mid, testAbort: true, testRedirectError: false, testNetworkError: false, 1.326 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.327 + {target: UPLOAD, type: "loadstart", optional: false}, 1.328 + {target: XHR, type: "abort", optional: false}, 1.329 + {target: XHR, type: "loadend", optional: false}, 1.330 + {target: UPLOAD, type: "abort", optional: false}, 1.331 + {target: UPLOAD, type: "loadend", optional: false}]}, 1.332 + { method: "POST", withUpload: mid, testAbort: false, testRedirectError: true, testNetworkError: false, 1.333 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.334 + {target: UPLOAD, type: "loadstart", optional: false}, 1.335 + {target: UPLOAD, type: "progress", optional: true}, 1.336 + {target: XHR, type: "error", optional: false}, 1.337 + {target: XHR, type: "loadend", optional: false}, 1.338 + {target: UPLOAD, type: "error", optional: false}, 1.339 + {target: UPLOAD, type: "loadend", optional: false}]}, 1.340 + { method: "POST", withUpload: mid, testAbort: false, testRedirectError: false, testNetworkError: true, 1.341 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.342 + {target: UPLOAD, type: "loadstart", optional: false}, 1.343 + {target: UPLOAD, type: "progress", optional: true}, 1.344 + {target: XHR, type: "error", optional: false}, 1.345 + {target: XHR, type: "loadend", optional: false}, 1.346 + {target: UPLOAD, type: "error", optional: false}, 1.347 + {target: UPLOAD, type: "loadend", optional: false}]}, 1.348 + 1.349 + { method: "POST", withUpload: large, testAbort: false, testRedirectError: false, testNetworkError: false, 1.350 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.351 + {target: UPLOAD, type: "loadstart", optional: false}, 1.352 + {target: UPLOAD, type: "progress", optional: true}, 1.353 + {target: UPLOAD, type: "load", optional: false}, 1.354 + {target: UPLOAD, type: "loadend", optional: false}, 1.355 + {target: XHR, type: "progress", optional: true}, 1.356 + {target: XHR, type: "load", optional: false}, 1.357 + {target: XHR, type: "loadend", optional: false}]}, 1.358 + { method: "POST", withUpload: large, testAbort: true, testRedirectError: false, testNetworkError: false, 1.359 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.360 + {target: UPLOAD, type: "loadstart", optional: false}, 1.361 + {target: XHR, type: "abort", optional: false}, 1.362 + {target: XHR, type: "loadend", optional: false}, 1.363 + {target: UPLOAD, type: "abort", optional: false}, 1.364 + {target: UPLOAD, type: "loadend", optional: false}]}, 1.365 + { method: "POST", withUpload: large, testAbort: false, testRedirectError: true, testNetworkError: false, 1.366 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.367 + {target: UPLOAD, type: "loadstart", optional: false}, 1.368 + {target: UPLOAD, type: "progress", optional: true}, 1.369 + {target: XHR, type: "error", optional: false}, 1.370 + {target: XHR, type: "loadend", optional: false}, 1.371 + {target: UPLOAD, type: "error", optional: false}, 1.372 + {target: UPLOAD, type: "loadend", optional: false}]}, 1.373 + { method: "POST", withUpload: large, testAbort: false, testRedirectError: false, testNetworkError: true, 1.374 + expectedEvents: [{target: XHR, type: "loadstart", optional: false}, 1.375 + {target: UPLOAD, type: "loadstart", optional: false}, 1.376 + {target: UPLOAD, type: "progress", optional: true}, 1.377 + {target: XHR, type: "error", optional: false}, 1.378 + {target: XHR, type: "loadend", optional: false}, 1.379 + {target: UPLOAD, type: "error", optional: false}, 1.380 + {target: UPLOAD, type: "loadend", optional: false}]}, 1.381 +]; 1.382 + 1.383 +function runTest() { 1.384 + var test = tests.shift(); 1.385 + start(test); 1.386 +} 1.387 + 1.388 +function nextTest() { 1.389 + if (tests.length > 1) { 1.390 + setTimeout("runTest()", 0); 1.391 + } else { 1.392 + SimpleTest.finish(); 1.393 + } 1.394 +} 1.395 + 1.396 +ok("upload" in (new XMLHttpRequest()), "XMLHttpRequest.upload isn't supported!"); 1.397 +SimpleTest.waitForExplicitFinish(); 1.398 +addLoadEvent(runTest); 1.399 + 1.400 +</script> 1.401 +</pre> 1.402 +</body> 1.403 +</html> 1.404 +