function startsWith(a, b) {
return a.substr(0, b.length) === b;
}
function updateProgress(e, data, testName) { var test = " while running " + testName;
is(e.type, "progress", "event type" + test);
let response;
if (data.nodata) {
is(e.target.response, null, "response should be null" + test);
response = null;
}
else if (data.text) {
is(typeof e.target.response, "string", "response should be a string" + test);
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 {
ok(e.target.response instanceof ArrayBuffer, "response should be an ArrayBuffer" + test);
response = bufferToString(e.target.response);
}
is(e.target.response, e.target.response, "reflexivity should hold" + test);
if (!data.nodata && !data.blob) { var newData;
ok(startsWith(response, data.receivedResult), "response strictly grew" + test);
newData = response.substr(data.receivedResult.length);
if (!data.encoded) {
ok(newData.length, "sanity check for progress" + test);
}
ok(startsWith(data.pendingResult, newData), "new data matches expected" + test);
}
is(e.lengthComputable, "total" in data, "lengthComputable" + test);
if ("total" in data) {
is(e.total, data.total, "total" + test);
}
function sendData(s) { var xhr = new XMLHttpRequest();
xhr.open("POST", "progressserver.sjs?send");
// The Blob constructor encodes String elements as UTF-8;
// for straight bytes, manually convert to ArrayBuffer first var buffer = new Uint8Array(s.length);
for (var i = 0; i < s.length; ++i) {
buffer[i] = s.charCodeAt(i) & 0xff;
};
xhr.send(new Blob([buffer]));
}
function closeConn() {
log("in closeConn"); var xhr = new XMLHttpRequest();
xhr.open("POST", "progressserver.sjs?close");
xhr.send();
return xhr;
}
var longString = "long";
while(longString.length < 65536)
longString += longString;
function utf8encode(s) {
return unescape(encodeURIComponent(s));
}
function bufferToString(buffer) {
return String.fromCharCode.apply(String, new Uint8Array(buffer));
}
function* runTests() { var xhr = new XMLHttpRequest();
xhr.onprogress = xhr.onload = xhr.onerror = xhr.onreadystatechange = xhr.onloadend = getEvent;
let e = yield undefined;
is(e.type, "readystatechange", "should readystate to headers-received starting " + testState.name);
is(xhr.readyState, xhr.HEADERS_RECEIVED, "should be in state HEADERS_RECEIVED starting " + testState.name);
e = yield undefined;
is(e.type, "readystatechange", "should readystate to loading starting " + testState.name);
is(xhr.readyState, xhr.LOADING, "should be in state LOADING starting " + testState.name);
if (typeof testState.total == "undefined")
delete testState.total;
}
if ("file" in test) {
testState.pendingBytes = testState.total;
testState.pendingResult = fileExpectedResult;
}
if ("close" in test) {
log("closing");
let xhrClose;
if (!testState.file)
xhrClose = closeConn();
let e = yield undefined;
is(e.type, "readystatechange", "should readystate to done closing " + testState.name);
is(xhr.readyState, xhr.DONE, "should be in state DONE closing " + testState.name);
log("readystate to 4");
e = yield undefined;
is(e.type, "load", "should fire load closing " + testState.name);
is(e.lengthComputable, e.total != 0, "length should " + (e.total == 0 ? "not " : "") + "be computable during load closing " + testState.name);
log("got load");
e = yield undefined;
is(e.type, "loadend", "should fire loadend closing " + testState.name);
is(e.lengthComputable, e.total != 0, "length should " + (e.total == 0 ? "not " : "") + "be computable during loadend closing " + testState.name);
log("got loadend");
// if we closed the connection using an explicit request, make sure that goes through before
// running the next test in order to avoid reordered requests from closing the wrong
// connection.
if (xhrClose && xhrClose.readyState != xhrClose.DONE) {
log("wait for closeConn to finish");
xhrClose.onloadend = getEvent;
yield undefined;
is(xhrClose.readyState, xhrClose.DONE, "closeConn finished");
}
if (!testState.nodata && !responseType.blob) {
// This branch intentionally left blank
// Under these conditions we check the response during updateProgress
}
else if (responseType.type === "arraybuffer") {
is(bufferToString(xhr.response), testState.pendingResult, "full response for " + testState.name);
}
else if (responseType.blob) {
let reader = new FileReader;
reader.readAsBinaryString(xhr.response);
reader.onloadend = getEvent;
yield undefined;
is(reader.result, testState.pendingResult, "full response in blob for " + testState.name);
}
testState.name = "";
}
if ("data" in test) {
log("sending");
if (responseType.text) {
testState.pendingResult += "utf16" in test ? test.utf16 : test.data;
}
else {
testState.pendingResult += test.data;
}
testState.pendingBytes = test.data.length;
sendData(test.data);
}
while(testState.pendingBytes) {
log("waiting for more bytes: " + testState.pendingBytes);
let e = yield undefined;
// Readystate can fire several times between each progress event.
if (e.type === "readystatechange")
continue;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.