4 var ranProgressHandler
= false;
5 var completedUpload
= false;
8 var lastSeenProgress
= 0;
11 function sendResults(failures
) {
12 var resultString
= failures
.length
? failures
.join('\n') : "success";
13 window
.domAutomationController
.send(resultString
);
16 function progressListener(e
) {
17 var progress
= e
.loaded
;
20 // The |progress| event should not be called after the |load| event.
21 // e.loaded should never hold the same value twice.
23 failureList
.push('Progress event occurred after load event.');
24 if (progress
<= lastSeenProgress
)
25 failureList
.push('No forward upload progress between events.');
26 if (e
.total
!= data
.length
)
27 failureList
.push('Upload total does not match payload size.');
28 if (progress
> e
.total
)
29 failureList
.push('Upload progress exceeds payload size.');
31 if (failureList
.length
)
32 sendResults(failureList
);
34 lastSeenProgress
= progress
;
35 ranProgressHandler
= true;
38 function completedUpload(e
) {
39 completedUpload
= true;
42 function onFinished(e
) {
44 if (!ranProgressHandler
)
45 failureList
.push('Finished upload without firing a progress event.');
46 if (lastSeenProgress
!= data
.length
)
47 failureList
.push('Final progress event before data transfer completed.');
48 if (this.responseText
!= 'hello') {
50 'Receieved responseText: \'' + this.responseText
+'\'. Expected: \'hello\'.');
52 sendResults(failureList
);
56 sendResults(['Received an XHR error event.']);
59 function WaitForAsyncXHR(url
, payloadSize
) {
60 // Build a long string, fast.
61 // Note: payloadSize will be of the form 2*3^x.
63 while(data
.length
!== payloadSize
) {
64 data
= data
+ data
+ data
;
66 asyncXHR
= new XMLHttpRequest();
67 asyncXHR
.addEventListener('load', onFinished
);
68 asyncXHR
.addEventListener('error', onError
);
70 asyncXHR
.upload
.addEventListener('progress', progressListener
);
71 asyncXHR
.upload
.addEventListener('load', completedUpload
);
73 asyncXHR
.open('POST', url
, true);
75 asyncXHR
.setRequestHeader('Content-Type', 'text/plain');
81 This page sends an asynchronous XMLHttpRequest on calling WaitForAsyncXHR(url).