Bug 696586 - Part 3: Tests; r=jonas

This commit is contained in:
Masatoshi Kimura 2012-01-30 11:34:15 +01:00
Родитель ccfe102961
Коммит 9ed67ddc9b
2 изменённых файлов: 27 добавлений и 13 удалений

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

@ -212,13 +212,16 @@ is(xhr.response, null, "Bad JSON should result in null response even 2nd time.")
// test response (responseType='blob') // test response (responseType='blob')
var onloadCount = 0; var onloadCount = 0;
function checkOnloadCount() { function checkOnloadCount() {
if (++onloadCount >= 3) SimpleTest.finish(); if (++onloadCount >= 6) SimpleTest.finish();
}; };
var responseTypes = ['blob', 'moz-blob'];
for (var i = 0; i < responseTypes.length; i++) {
var t = responseTypes[i];
// with a simple text file // with a simple text file
xhr = new XMLHttpRequest(); xhr = new XMLHttpRequest();
xhr.open("GET", 'file_XHR_pass2.txt'); xhr.open("GET", 'file_XHR_pass2.txt');
xhr.responseType = 'blob'; xhr.responseType = t;
xhr.onloadend = continueTest; xhr.onloadend = continueTest;
xhr.send(null); xhr.send(null);
yield; yield;
@ -245,7 +248,7 @@ xhr.onreadystatechange = function() {
switch (xhr.readyState) { switch (xhr.readyState) {
case 2: case 2:
is(xhr.status, 200, "wrong status"); is(xhr.status, 200, "wrong status");
xhr.responseType = 'blob'; xhr.responseType = t;
break; break;
case 4: case 4:
b = xhr.response; b = xhr.response;
@ -293,9 +296,10 @@ xhr.onreadystatechange = function() {
} }
}; };
xhr.open("GET", 'file_XHR_binary2.bin', true); xhr.open("GET", 'file_XHR_binary2.bin', true);
xhr.responseType = 'blob'; xhr.responseType = t;
xhr.send(null); xhr.send(null);
})(); })();
}
var client = new XMLHttpRequest(); var client = new XMLHttpRequest();
client.onreadystatechange = function() { client.onreadystatechange = function() {

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

@ -39,13 +39,21 @@ function updateProgress(e, data, testName) {
is(typeof e.target.response, "string", "response should be a string" + test); is(typeof e.target.response, "string", "response should be a string" + test);
response = e.target.response; response = e.target.response;
} }
else if (data.blob) {
ok(e.target.response instanceof Blob, "response should be a Blob" + test);
response = e.target.response;
}
else { else {
ok(e.target.response instanceof ArrayBuffer, "response should be a ArrayBuffer" + test); ok(e.target.response instanceof ArrayBuffer, "response should be an ArrayBuffer" + test);
response = bufferToString(e.target.response); response = bufferToString(e.target.response);
} }
is(e.target.response, e.target.response, "reflexivity should hold" + test);
if (!data.nodata && !data.encoded) { if (!data.nodata && !data.encoded) {
if (!data.chunked) { if (data.blob) {
is(e.loaded, response.size, "event.loaded matches response size" + test);
}
else if (!data.chunked) {
is(e.loaded, response.length, "event.loaded matches response size" + test); is(e.loaded, response.length, "event.loaded matches response size" + test);
} }
else { else {
@ -57,7 +65,7 @@ function updateProgress(e, data, testName) {
ok(e.loaded - data.receivedBytes <= data.pendingBytes, ok(e.loaded - data.receivedBytes <= data.pendingBytes,
"event.loaded didn't increase too much" + test); "event.loaded didn't increase too much" + test);
if (!data.nodata) { if (!data.nodata && !data.blob) {
var newData; var newData;
ok(startsWith(response, data.receivedResult), ok(startsWith(response, data.receivedResult),
"response strictly grew" + test); "response strictly grew" + test);
@ -74,7 +82,7 @@ function updateProgress(e, data, testName) {
is(e.total, data.total, "total" + test); is(e.total, data.total, "total" + test);
} }
if (!data.nodata) { if (!data.nodata && !data.blob) {
data.pendingResult = data.pendingResult.substr(newData.length); data.pendingResult = data.pendingResult.substr(newData.length);
} }
data.pendingBytes -= e.loaded - data.receivedBytes; data.pendingBytes -= e.loaded - data.receivedBytes;
@ -113,7 +121,8 @@ function runTests() {
var responseTypes = [{ type: "text", text: true }, var responseTypes = [{ type: "text", text: true },
{ type: "arraybuffer", text: false, nodata: true }, { type: "arraybuffer", text: false, nodata: true },
{ type: "blob", text: false, nodata: true }, { type: "blob", text: false, nodata: true, blob: true },
{ type: "moz-blob", text: false, nodata: false, blob: true },
{ type: "document", text: true, nodata: true }, { type: "document", text: true, nodata: true },
{ type: "json", text: true, nodata: true }, { type: "json", text: true, nodata: true },
{ type: "", text: true }, { type: "", text: true },
@ -153,7 +162,7 @@ function runTests() {
{ data: utf8encode("a\u867Eb").substr(4), utf16: "b" }, { data: utf8encode("a\u867Eb").substr(4), utf16: "b" },
{ close: true }, { close: true },
]; ];
if (responseType.type === "blob") { if (responseType.blob) {
tests.push({ file: "file_XHR_binary2.bin", name: "cacheable data", total: 65536 }, tests.push({ file: "file_XHR_binary2.bin", name: "cacheable data", total: 65536 },
{ close: true }, { close: true },
{ file: "file_XHR_binary2.bin", name: "cached data", total: 65536 }, { file: "file_XHR_binary2.bin", name: "cached data", total: 65536 },
@ -177,6 +186,7 @@ function runTests() {
nodata: responseType.nodata, nodata: responseType.nodata,
chunked: responseType.chunked, chunked: responseType.chunked,
text: responseType.text, text: responseType.text,
blob: responseType.blob,
file: test.file }; file: test.file };
xhr.onreadystatechange = null; xhr.onreadystatechange = null;
@ -235,7 +245,7 @@ function runTests() {
is(xhr.response, null, "chunked data has null response for " + testState.name); is(xhr.response, null, "chunked data has null response for " + testState.name);
} }
if (!testState.nodata || responseType.chunked) { if (!testState.nodata && !responseType.blob || responseType.chunked) {
// This branch intentionally left blank // This branch intentionally left blank
// Under these conditions we check the response during updateProgress // Under these conditions we check the response during updateProgress
} }
@ -243,7 +253,7 @@ function runTests() {
is(bufferToString(xhr.response), testState.pendingResult, is(bufferToString(xhr.response), testState.pendingResult,
"full response for " + testState.name); "full response for " + testState.name);
} }
else if (responseType.type === "blob") { else if (responseType.blob) {
let reader = new FileReader; let reader = new FileReader;
reader.readAsBinaryString(xhr.response); reader.readAsBinaryString(xhr.response);
reader.onloadend = getEvent; reader.onloadend = getEvent;
@ -280,7 +290,7 @@ function runTests() {
} }
} }
if (!testState.nodata) { if (!testState.nodata && !testState.blob) {
is(testState.pendingResult, "", is(testState.pendingResult, "",
"should have consumed the expected result"); "should have consumed the expected result");
} }