Skip to content

Commit

Permalink
Bug 1014466 - Part 3: Test case for async XHR reuse. r=khuey
Browse files Browse the repository at this point in the history
  • Loading branch information
moztpe committed Jun 6, 2014
1 parent 90fa6d3 commit 88865fe
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions dom/workers/test/bug1014466_data1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1234567890
1 change: 1 addition & 0 deletions dom/workers/test/bug1014466_data2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ABCDEFGH
64 changes: 64 additions & 0 deletions dom/workers/test/bug1014466_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/

function ok(a, msg) {
postMessage({type: "status", status: !!a, msg: msg });
}

onmessage = function(event) {

function getResponse(url) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.send();
return xhr.responseText;
}

const testFile1 = "bug1014466_data1.txt";
const testFile2 = "bug1014466_data2.txt";
const testData1 = getResponse(testFile1);
const testData2 = getResponse(testFile2);

var response_count = 0;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == xhr.DONE && xhr.status == 200) {
response_count++;
switch (response_count) {
case 1:
ok(xhr.responseText == testData1, "Check data 1");
test_data2();
break;
case 2:
ok(xhr.responseText == testData2, "Check data 2");
postMessage({type: "finish" });
break;
default:
ok(false, "Unexpected response received");
postMessage({type: "finish" });
break;
}
}
}
xhr.onerror = function(event) {
ok(false, "Got an error event: " + event);
postMessage({type: "finish" });
}

function test_data1() {
xhr.open("GET", testFile1, true);
xhr.responseType = "text";
xhr.send();
}

function test_data2() {
xhr.abort();
xhr.open("GET", testFile2, true);
xhr.responseType = "text";
xhr.send();
}

test_data1();
}
4 changes: 4 additions & 0 deletions dom/workers/test/mochitest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
support-files =
WorkerTest_badworker.js
atob_worker.js
bug1014466_data1.txt
bug1014466_data2.txt
bug1014466_worker.js
bug1020226_worker.js
bug1020226_frame.html
clearTimeouts_worker.js
Expand Down Expand Up @@ -80,6 +83,7 @@ support-files =
[test_blobWorkers.html]
[test_bug949946.html]
[test_bug1010784.html]
[test_bug1014466.html]
[test_bug1020226.html]
[test_chromeWorker.html]
[test_clearTimeouts.html]
Expand Down
42 changes: 42 additions & 0 deletions dom/workers/test/test_bug1014466.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!--
2 Any copyright is dedicated to the Public Domain.
3 http://creativecommons.org/publicdomain/zero/1.0/
4 -->
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1014466
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 1014466</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1014466">Mozilla Bug 1014466</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script type="application/javascript">

var worker = new Worker("bug1014466_worker.js");

worker.onmessage = function(event) {
if (event.data.type == 'finish') {
SimpleTest.finish();
} else if (event.data.type == 'status') {
ok(event.data.status, event.data.msg);
}
};

worker.postMessage(true);

SimpleTest.waitForExplicitFinish();

</script>
</pre>
</body>
</html>

0 comments on commit 88865fe

Please sign in to comment.