1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
<!DOCTYPE HTML>
<html>
<head>
<title>Test for XMLHttpRequest Progress Events</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="gen.next();">
<pre id=l></pre>
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();
var gen = runTests();
function log(s) {
// Uncomment these to get debugging information
/*
document.getElementById("l").textContent += s + "\n";
dump(s + "\n");
*/
}
function getEvent(e) {
log("got event: " + e.type + " (" + e.target.readyState + ")");
gen.next(e);
}
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.encoded) {
if (data.blob) {
is(e.loaded, response.size, "event.loaded matches response size" + test);
}
else {
is(e.loaded, response.length, "event.loaded matches response size" + test);
}
}
ok(e.loaded > data.receivedBytes, "event.loaded increased" + test);
ok(e.loaded - data.receivedBytes <= data.pendingBytes,
"event.loaded didn't increase too much" + 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);
}
if (!data.nodata && !data.blob) {
data.pendingResult = data.pendingResult.substr(newData.length);
}
data.pendingBytes -= e.loaded - data.receivedBytes;
data.receivedResult = response;
data.receivedBytes = e.loaded;
}
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;
var responseTypes = [{ type: "text", text: true },
{ type: "arraybuffer", text: false, nodata: true },
{ type: "blob", text: false, nodata: true, blob: true },
{ type: "document", text: true, nodata: true },
{ type: "json", text: true, nodata: true },
{ type: "", text: true },
];
var responseType;
var fileExpectedResult = "";
for (let i = 0; i < 65536; i++) {
fileExpectedResult += String.fromCharCode(i & 255);
}
while ((responseType = responseTypes.shift())) {
let tests = [{ open: "Content-Type=text/plain", name: "simple test" },
{ data: "hello world" },
{ data: "\u0000\u0001\u0002\u0003" },
{ data: longString },
{ data: "x" },
{ close: true },
{ open: "Content-Type=text/plain&Content-Length=20", name: "with length", total: 20 },
// 5 bytes from the "ready" in the open step
{ data: "abcde" },
{ data: "0123456789" },
{ close: true },
{ open: "Content-Type=application/xml", name: "without length, as xml" },
{ data: "<out>" },
{ data: "text" },
{ data: "</foo>invalid" },
{ close: true },
{ open: "Content-Type=text/plain;charset%3dutf-8", name: "utf8 data", encoded: true },
{ data: utf8encode("räksmörgås"), utf16: "räksmörgås" },
{ data: utf8encode("Å").substr(0,1), utf16: "" },
{ data: utf8encode("Å").substr(1), utf16: "Å" },
{ data: utf8encode("aöb").substr(0,2), utf16: "a" },
{ data: utf8encode("aöb").substr(2), utf16: "öb" },
{ data: utf8encode("a\u867Eb").substr(0,3), utf16: "a" },
{ data: utf8encode("a\u867Eb").substr(3,1), utf16: "\u867E" },
{ data: utf8encode("a\u867Eb").substr(4), utf16: "b" },
{ close: true },
];
if (responseType.blob) {
tests.push({ file: "file_XHR_binary2.bin", name: "cacheable data", total: 65536 },
{ close: true },
{ file: "file_XHR_binary2.bin", name: "cached data", total: 65536 },
{ close: true });
}
let testState = { index: 0 };
for (let i = 0; i < tests.length; ++i) {
let test = tests[i];
testState.index++;
if ("open" in test || "file" in test) {
log("opening " + testState.name);
testState = { name: test.name + " for " + responseType.type,
index: 0,
pendingResult: "ready",
pendingBytes: 5,
receivedResult: "",
receivedBytes: 0,
total: test.total,
encoded: test.encoded,
nodata: responseType.nodata,
text: responseType.text,
blob: responseType.blob,
file: test.file };
xhr.onreadystatechange = null;
if (testState.file)
xhr.open("GET", test.file);
else
xhr.open("POST", "progressserver.sjs?open&" + test.open);
xhr.responseType = responseType.type;
xhr.send("ready");
xhr.onreadystatechange = 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;
updateProgress(e, testState, "data for " + testState.name + "[" + testState.index + "]");
}
if (!testState.nodata && !testState.blob) {
is(testState.pendingResult, "",
"should have consumed the expected result");
}
log("done with this test");
}
is(testState.name, "", "forgot to close last test");
}
SimpleTest.finish();
}
</script>
</body>
</html>
|