test_bug444546.html (4665B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=444546 5 --> 6 <head> 7 <title>Test for Bug 444546</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 10 <style> 11 .up { 12 height: 14px; 13 width: 1px; 14 background: blue; 15 font-size: 11px; 16 color: white; 17 } 18 .down { 19 height: 14px; 20 width: 1px; 21 background: blue; 22 font-size: 11px; 23 color: white; 24 } 25 </style> 26 </head> 27 <body> 28 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=444546">Mozilla Bug 444546</a> 29 <p id="display"></p> 30 <div id="content" style="display: none"> 31 32 </div> 33 <pre id="test"> 34 <script type="application/javascript"> 35 36 /** Test for Bug 444546 */ 37 38 var xhrCount = 5; 39 var xhrs = new Array(); 40 var uploads = new Array(); 41 var maxSize = 5000000; 42 var hugeString = new Array(maxSize + 1).join('a'); 43 44 function updateProgress(evt) { 45 ++evt.target.pcounter; 46 var time = new Date().getTime(); 47 // 350 - 200 = 150ms 48 if ((time - evt.target.prevTime) < 150) { 49 evt.target.log.parentNode.style.background = "red"; 50 } 51 var diff = (time - evt.target.prevTime); 52 if (evt.target.min == -1 || evt.target.min > diff) { 53 evt.target.min = diff; 54 } 55 if (evt.target.max == -1 || evt.target.max < diff) { 56 evt.target.max = diff; 57 } 58 59 evt.target.log.textContent = diff + "ms"; 60 evt.target.prevTime = time; 61 if (evt.lengthComputable) { 62 var fractionLoaded = (evt.loaded / evt.total); 63 if (fractionLoaded < 1) { 64 evt.target.log.style.width = (fractionLoaded * 400) + "px"; 65 } 66 } 67 } 68 69 function loaded(evt) { 70 evt.target.log.style.width = "400px"; 71 evt.target.log.style.background = "green"; 72 if ("xhr" in evt.target) { 73 evt.target.xhr.prevTime = new Date().getTime(); 74 evt.target.xhr.startTime = evt.target.xhr.prevTime; 75 } 76 var total = new Date().getTime() - evt.target.startTime; 77 evt.target.log.textContent = "total:" + total + "ms"; 78 if (evt.target.pcounter) { 79 evt.target.log.textContent += " ," + evt.target.pcounter + "pe, avg:" + 80 parseInt((evt.target.prevTime - evt.target.startTime)/evt.target.pcounter) + "ms"; 81 } 82 if (evt.target.min != -1) { 83 ok(evt.target.min >= 150, "Events fired too fast!"); 84 evt.target.log.textContent += ", min:" + evt.target.min + "ms"; 85 } 86 if (evt.target.max != -1) { 87 // Disabled for now. 88 //ok(evt.target.max <= 550, "Events didn't fire fast enough!"); 89 evt.target.log.textContent += ", max:" + evt.target.max + "ms"; 90 } 91 if ("upload" in evt.target) { 92 is(evt.total, maxSize * 10, "Wrong data!"); 93 --xhrCount; 94 if (xhrCount == 0) { 95 // This is a hack. To get more progress events, server sends the data 96 // 10 times. 97 SimpleTest.finish(); 98 } else { 99 setTimeout(start, 10); 100 } 101 } else { 102 is(evt.total, maxSize, "Wrong data!"); 103 } 104 } 105 106 function start() { 107 var xhr = new XMLHttpRequest(); 108 xhrs.push(xhr); 109 uploads.push(xhr.upload); 110 var container = document.createElement("tr"); 111 var td1 = document.createElement("td"); 112 container.appendChild(td1); 113 td1.textContent = xhrs.length + "."; 114 var td2 = document.createElement("td"); 115 container.appendChild(td2); 116 var td3 = document.createElement("td"); 117 container.appendChild(td3); 118 var uploadElement = document.createElement("div"); 119 td2.appendChild(uploadElement); 120 uploadElement.className = "up"; 121 var downloadElement = document.createElement("div"); 122 td3.appendChild(downloadElement); 123 downloadElement.className = "down"; 124 document.getElementById('tbody').appendChild(container); 125 xhr.log = downloadElement; 126 xhr.upload.log = uploadElement; 127 xhr.onprogress = updateProgress; 128 xhr.upload.onprogress = updateProgress; 129 xhr.onload = loaded; 130 xhr.upload.onload = loaded; 131 xhr.open("POST", "bug444546.sjs"); 132 xhr.upload.prevTime = new Date().getTime(); 133 xhr.upload.startTime = xhr.upload.prevTime; 134 xhr.upload.xhr = xhr; 135 xhr.pcounter = 0; 136 xhr.upload.pcounter = 0; 137 xhr.min = -1; 138 xhr.upload.min = -1; 139 xhr.max = -1; 140 xhr.upload.max = -1; 141 xhr.send(hugeString); 142 } 143 144 SimpleTest.waitForExplicitFinish(); 145 addLoadEvent(function() { setTimeout(start, 10); }); 146 147 </script> 148 </pre> 149 <table> 150 <tbody id="tbody"> 151 <tr> 152 <td>XHR</td> 153 <td style="min-width: 410px;">upload</td> 154 <td style="min-width: 410px;">download</td> 155 </tr> 156 </tbody> 157 </table> 158 </body> 159 </html>