Bug 701787 - Part 2: Tests. r=smaug.

This commit is contained in:
Masatoshi Kimura 2011-12-07 13:30:19 +02:00
Родитель 6f84e891e6
Коммит ce34d53e94
5 изменённых файлов: 110 добавлений и 24 удалений

Просмотреть файл

@ -5,15 +5,20 @@
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<body onload="gen.next();">
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
<script class="testbody" type="application/javascript;version=1.7">
SimpleTest.waitForExplicitFinish();
var gen = runTests();
function continueTest() { gen.next(); }
function runTests() {
var path = "/tests/content/base/test/";
var passFiles = [['file_XHR_pass1.xml', 'GET'],
@ -63,7 +68,6 @@ for (i = 0; i < failFiles.length; ++i) {
}
}
// test response (responseType='document')
function checkResponseTextAccessThrows(xhr) {
var didthrow = false;
try { xhr.responseText } catch (e) { didthrow = true; }
@ -84,16 +88,37 @@ function checkSetResponseTypeThrows(xhr, type) {
try { xhr.responseType = type; } catch (e) { didthrow = true; }
ok(didthrow, "should have thrown when setting responseType");
}
function checkOpenThrows(xhr, method, url, async) {
var didthrow = false;
try { xhr.open(method, url, async); } catch (e) { didthrow = true; }
ok(didthrow, "should have thrown when open is called");
}
// test response (sync, responseType is not changeable)
xhr = new XMLHttpRequest();
xhr.open("GET", 'file_XHR_pass2.txt', false);
checkSetResponseTypeThrows(xhr, "");
checkSetResponseTypeThrows(xhr, "text");
checkSetResponseTypeThrows(xhr, "document");
xhr.open("GET", 'file_XHR_pass1.xml', false);
checkSetResponseTypeThrows(xhr, "arraybuffer");
checkSetResponseTypeThrows(xhr, "blob");
checkSetResponseTypeThrows(xhr, "moz-json");
checkSetResponseTypeThrows(xhr, "moz-chunked-text");
checkSetResponseTypeThrows(xhr, "moz-chunked-arraybuffer");
xhr.responseType = 'document';
xhr.send(null);
checkSetResponseTypeThrows(xhr, "document");
is(xhr.status, 200, "wrong status");
is(xhr.response, "hello pass\n", "wrong response");
// test response (responseType='document')
xhr = new XMLHttpRequest();
xhr.open("GET", 'file_XHR_pass1.xml');
xhr.responseType = 'document';
xhr.onloadend = continueTest;
xhr.send(null);
yield;
checkSetResponseTypeThrows(xhr, "document");
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
is((new XMLSerializer()).serializeToString(xhr.response.documentElement),
"<res>hello</res>",
@ -101,9 +126,11 @@ is((new XMLSerializer()).serializeToString(xhr.response.documentElement),
// test response (responseType='text')
xhr = new XMLHttpRequest();
xhr.open("GET", 'file_XHR_pass2.txt', false);
xhr.open("GET", 'file_XHR_pass2.txt');
xhr.responseType = 'text';
xhr.onloadend = continueTest;
xhr.send(null);
yield;
is(xhr.status, 200, "wrong status");
checkResponseXMLAccessThrows(xhr);
is(xhr.response, "hello pass\n", "wrong response");
@ -118,9 +145,11 @@ function arraybuffer_equals_to(ab, s) {
// with a simple text file
xhr = new XMLHttpRequest();
xhr.open("GET", 'file_XHR_pass2.txt', false);
xhr.open("GET", 'file_XHR_pass2.txt');
xhr.responseType = 'arraybuffer';
xhr.onloadend = continueTest;
xhr.send(null);
yield;
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
checkResponseXMLAccessThrows(xhr);
@ -129,9 +158,11 @@ ok(ab != null, "should have a non-null arraybuffer");
arraybuffer_equals_to(ab, "hello pass\n");
// test reusing the same XHR (Bug 680816)
xhr.open("GET", 'file_XHR_binary1.bin', false);
xhr.open("GET", 'file_XHR_binary1.bin');
xhr.responseType = 'arraybuffer';
xhr.onloadend = continueTest;
xhr.send(null);
yield;
is(xhr.status, 200, "wrong status");
ab2 = xhr.response;
ok(ab2 != null, "should have a non-null arraybuffer");
@ -141,9 +172,11 @@ arraybuffer_equals_to(ab2, "\xaa\xee\0\x03\xff\xff\xff\xff\xbb\xbb\xbb\xbb");
// with a binary file
xhr = new XMLHttpRequest();
xhr.open("GET", 'file_XHR_binary1.bin', false);
xhr.open("GET", 'file_XHR_binary1.bin');
xhr.responseType = 'arraybuffer';
xhr.send(null)
xhr.onloadend = continueTest;
xhr.send(null);
yield;
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
checkResponseXMLAccessThrows(xhr);
@ -154,10 +187,12 @@ is(xhr.response, xhr.response, "returns the same ArrayBuffer");
// test response (responseType='moz-json')
var xhr = new XMLHttpRequest();
xhr.open("POST", 'responseIdentical.sjs', false);
xhr.open("POST", 'responseIdentical.sjs');
xhr.responseType = 'moz-json';
jsonObjStr = JSON.stringify({title: "aBook", author: "john"});
xhr.onloadend = continueTest;
xhr.send(jsonObjStr);
yield;
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
checkResponseXMLAccessThrows(xhr);
@ -166,9 +201,11 @@ is(xhr.response, xhr.response, "returning the same object on each access");
// with invalid json
var xhr = new XMLHttpRequest();
xhr.open("POST", 'responseIdentical.sjs', false);
xhr.open("POST", 'responseIdentical.sjs');
xhr.responseType = 'moz-json';
xhr.onloadend = continueTest;
xhr.send("{");
yield;
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
checkResponseXMLAccessThrows(xhr);
@ -183,9 +220,11 @@ function checkOnloadCount() {
// with a simple text file
xhr = new XMLHttpRequest();
xhr.open("GET", 'file_XHR_pass2.txt', false);
xhr.open("GET", 'file_XHR_pass2.txt');
xhr.responseType = 'blob';
xhr.onloadend = continueTest;
xhr.send(null);
yield;
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
checkResponseXMLAccessThrows(xhr);
@ -281,6 +320,8 @@ client.open("GET", "file_XHR_pass1.xml", true);
client.send();
client.abort();
yield;
} /* runTests */
</script>
</pre>
</body>

Просмотреть файл

@ -8,7 +8,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=464848
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<body onload="gen.next();">
<a target="_blank"
href="https://bugzilla.mozilla.org/show_bug.cgi?id=464848">Mozilla Bug 464848</a>
<p id="display">
@ -19,6 +19,12 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=464848
</div>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.8">
SimpleTest.waitForExplicitFinish();
var gen = runTests();
function continueTest() { gen.next(); }
function runTests() {
xhr = new XMLHttpRequest();
xhr.open("GET", "file_XHRSendData_doc.xml", false);
@ -176,12 +182,16 @@ for (var i = 0; i < testDOMFiles.length; i++) {
try {
for each(test in tests) {
xhr = new XMLHttpRequest;
xhr.open("POST", "file_XHRSendData.sjs", false);
xhr.open("POST", "file_XHRSendData.sjs", !!test.resType);
if (test.contentType)
xhr.setRequestHeader("Content-Type", test.contentType);
if (test.resType)
if (test.resType) {
xhr.responseType = test.resType;
xhr.onloadend = continueTest;
}
xhr.send(test.body);
if (test.resType)
yield;
if (test.resContentType) {
is(xhr.getResponseHeader("Result-Content-Type"), test.resContentType,
@ -234,6 +244,10 @@ function is_identical_arraybuffer(ab1, ab2) {
String.fromCharCode.apply(String, u8v2), "arraybuffer values not equal");
}
SimpleTest.finish();
yield;
} /* runTests */
</script>
</pre>
</body>

Просмотреть файл

@ -86,14 +86,14 @@ function onWindowLoad() {
ok(document.iframeWasLoaded, "Loading resource via src-attribute");
for each (test in alltests) {
function runTest(test) {
var xhr = new XMLHttpRequest();
var method = "GET";
if (test.method != null) { method = test.method; }
xhr.open(method, test.url, false);
xhr.open(method, test.url);
xhr.withCredentials = test.withCredentials;
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
@ -104,11 +104,20 @@ function onWindowLoad() {
xhr.send();
} catch(e) {
}
var success = eval(xhr.status + test.status_check);
ok(success, test.error);
xhr.onloadend = function() {
var success = eval(xhr.status + test.status_check);
ok(success, test.error);
if (alltests.length == 0) {
SimpleTest.finish();
} else {
runTest(alltests.shift());
}
};
}
SimpleTest.finish();
runTest(alltests.shift());
}
SimpleTest.waitForExplicitFinish();

Просмотреть файл

@ -0,0 +1,21 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
// Test setting .responseType and .withCredentials is allowed
// in non-window non-Worker context
function run_test()
{
var xhr = Components.classes['@mozilla.org/xmlextras/xmlhttprequest;1'].
createInstance(Components.interfaces.nsIXMLHttpRequest);
xhr.open('GET', 'data:,', false);
var exceptionThrown = false;
try {
xhr.responseType = '';
xhr.withCredentials = false;
} catch (e) {
exceptionThrown = true;
}
do_check_eq(false, exceptionThrown);
}

Просмотреть файл

@ -7,4 +7,5 @@ tail =
[test_csputils.js]
[test_error_codes.js]
[test_thirdpartyutil.js]
[test_xhr_standalone.js]
[test_xmlserializer.js]