1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/workers/test/xhrAbort_worker.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,92 @@ 1.4 +/** 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.7 + */ 1.8 +function runTest() { 1.9 + var xhr = new XMLHttpRequest(); 1.10 + 1.11 + var events = []; 1.12 + function pushEvent(event) { 1.13 + var readyState, responseText, status, statusText; 1.14 + 1.15 + try { 1.16 + readyState = xhr.readyState; 1.17 + } 1.18 + catch (e) { 1.19 + readyState = "[exception]"; 1.20 + } 1.21 + 1.22 + try { 1.23 + responseText = xhr.responseText; 1.24 + } 1.25 + catch (e) { 1.26 + responseText = "[exception]"; 1.27 + } 1.28 + 1.29 + try { 1.30 + status = xhr.status; 1.31 + } 1.32 + catch (e) { 1.33 + status = "[exception]"; 1.34 + } 1.35 + 1.36 + try { 1.37 + statusText = xhr.statusText; 1.38 + } 1.39 + catch (e) { 1.40 + statusText = "[exception]"; 1.41 + } 1.42 + 1.43 + var str = event.type + "(" + readyState + ", '" + responseText + "', " + 1.44 + status + ", '" + statusText + "'"; 1.45 + if ((("ProgressEvent" in this) && event instanceof ProgressEvent) || 1.46 + (("WorkerProgressEvent" in this) && event instanceof WorkerProgressEvent)) { 1.47 + str += ", progressEvent"; 1.48 + } 1.49 + str += ")"; 1.50 + 1.51 + events.push(str); 1.52 + } 1.53 + 1.54 + xhr.onerror = function(event) { 1.55 + throw new Error("Error: " + xhr.statusText); 1.56 + } 1.57 + 1.58 + xhr.onload = function(event) { 1.59 + throw new Error("Shouldn't have gotten load event!"); 1.60 + }; 1.61 + 1.62 + var seenAbort; 1.63 + xhr.onabort = function(event) { 1.64 + if (seenAbort) { 1.65 + throw new Error("Already seen the abort event!"); 1.66 + } 1.67 + seenAbort = true; 1.68 + 1.69 + pushEvent(event); 1.70 + postMessage(events); 1.71 + }; 1.72 + 1.73 + xhr.onreadystatechange = function(event) { 1.74 + pushEvent(event); 1.75 + if (xhr.readyState == xhr.HEADERS_RECEIVED) { 1.76 + xhr.abort(); 1.77 + } 1.78 + }; 1.79 + 1.80 + xhr.open("GET", "testXHR.txt"); 1.81 + xhr.overrideMimeType("text/plain"); 1.82 + xhr.send(null); 1.83 +} 1.84 + 1.85 +function messageListener(event) { 1.86 + switch (event.data) { 1.87 + case "start": 1.88 + runTest(); 1.89 + break; 1.90 + default: 1.91 + throw new Error("Bad message!"); 1.92 + } 1.93 +} 1.94 + 1.95 +addEventListener("message", messageListener, false);