/** *Getamessagedescribingthistest. * *@returns{string}Thetestdescription.
*/
getMessage() { var rv = this.id + ", "; if (this.mustReset) {
rv += "original timeout at " + this.timeLimit + ", ";
rv += "reset at " + this.resetAfter + " to " + this.resetTo;
} else {
rv += "timeout scheduled at " + this.timeLimit;
} return rv;
},
/** *Checktheeventreceived,andifit'stheright(andonly)oneweget. * *@param{DOMProgressEvent}evtAneventoftype"load"or"timeout".
*/
handleEvent(evt) { if (this.hasFired) {
ok(false, "Only one event should fire: " + this.getMessage()); return;
} this.hasFired = true;
var type = evt.type,
expectedType; // The XHR responds after 2500 milliseconds with a load event. var timeLimit = this.mustReset && this.resetAfter < Math.min(2500, this.timeLimit)
? this.resetTo
: this.timeLimit; if (timeLimit == 0 || timeLimit >= 2500) {
expectedType = "load";
} else {
expectedType = "timeout";
}
is(type, expectedType, this.getMessage());
TestCounter.testComplete();
},
};
/** *GenerateandtrackXMLHttpRequestswhichwillhaveabort()calledon. * *@paramshouldAbort{Boolean}Trueifweshouldcallabortatall. *@paramabortDelay{Number}Thetimeinmstowaitbeforecallingabort().
*/ function AbortedRequest(shouldAbort, abortDelay) { this.shouldAbort = shouldAbort; this.abortDelay = abortDelay; this.hasFired = false;
}
AbortedRequest.prototype = { /** *StarttheXMLHttpRequest!
*/
startXHR() { var req = new XMLHttpRequest(); this.request = req;
req.open("GET", "file_XHR_timeout.sjs"); var me = this; function handleEvent(e) { return me.handleEvent(e);
}
req.onerror = handleEvent;
req.onload = handleEvent;
req.onabort = handleEvent;
req.ontimeout = handleEvent;
req.timeout = 2000; var _this = this;
function abortReq() {
req.abort();
}
if (!this.shouldAbort) {
self.setTimeout(function () { try {
_this.noEventsFired();
} catch (e) {
ok(false, "Unexpected error: " + e);
TestCounter.testComplete();
}
}, 5000);
} else { // Abort events can only be triggered on sent requests.
req.send(); if (this.abortDelay == -1) {
abortReq();
} else {
self.setTimeout(abortReq, this.abortDelay);
}
}
},
/** *Ensurethatnoeventsfiredatall,especiallynotourtimeoutevent.
*/
noEventsFired() {
ok(
!this.hasFired, "No events should fire for an unsent, unaborted request"
); // We're done; if timeout hasn't fired by now, it never will.
TestCounter.testComplete();
},
/** *Getamessagedescribingthistest. * *@returns{string}Thetestdescription.
*/
getMessage() { return"time to abort is " + this.abortDelay + ", timeout set at 2000";
},
var SyncRequestSettingTimeoutAfterOpen = {
startXHR() { var pass = false; var req = new XMLHttpRequest();
req.open("GET", "file_XHR_timeout.sjs", false); try {
req.timeout = 1000;
} catch (e) {
pass = true;
}
ok(pass, "Synchronous XHR must not allow a timeout to be set");
TestCounter.testComplete();
},
};
var SyncRequestSettingTimeoutBeforeOpen = {
startXHR() { var pass = false; var req = new XMLHttpRequest();
req.timeout = 1000; try {
req.open("GET", "file_XHR_timeout.sjs", false);
} catch (e) {
pass = true;
}
ok(pass, "Synchronous XHR must not allow a timeout to be set");
TestCounter.testComplete();
},
};
var TestRequests = [ // Simple timeouts. new RequestTracker(true, "no time out scheduled, load fires normally", 0), new RequestTracker(true, "load fires normally", 5000), new RequestTracker(true, "timeout hit before load", 1500),
// Timeouts reset after a certain delay. new RequestTracker( true, "load fires normally with no timeout set, twice", 0, 2000, 0
), new RequestTracker( true, "load fires normally with same timeout set twice", 5000, 2000, 5000
), new RequestTracker( true, "timeout fires normally with same timeout set twice", 1500, 750, 1500
),
new RequestTracker( true, "timeout disabled after initially set", 5000, 2000, 0
), new RequestTracker( true, "timeout overrides load after a delay", 5000, 1000, 1500
), new RequestTracker( true, "timeout enabled after initially disabled", 0, 2000, 5000
),
new RequestTracker( true, "timeout set to expiring value after load fires", 5000, 4000, 1000
), new RequestTracker( true, "timeout set to expired value before load fires", 5000, 2000, 1000
), new RequestTracker( true, "timeout set to non-expiring value after timeout fires", 1000, 2000, 5000
),
// Aborted requests. new AbortedRequest(false), new AbortedRequest(true, -1), new AbortedRequest(true, 5000),
];
var MainThreadTestRequests = [ new AbortedRequest(true, 0), new AbortedRequest(true, 1000),
var WorkerThreadTestRequests = [ // Simple timeouts. new RequestTracker(false, "no time out scheduled, load fires normally", 0), new RequestTracker(false, "load fires normally", 5000), new RequestTracker(false, "timeout hit before load", 1500),
// Reset timeouts don't make much sense with a sync request ...
];
// This code controls moving from one test to another. var TestCounter = {
testComplete() { // Allow for the possibility there are other events coming.
self.setTimeout(function () {
TestCounter.next();
}, 5000);
},
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.