summaryrefslogtreecommitdiff
path: root/dom/base/test/test_bug444546.html
blob: 3209f9054970b0b6d46cb32722d0e226ea0d89e5 (plain)
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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=444546
-->
<head>
  <title>Test for Bug 444546</title>
  <script type="application/javascript" src="/MochiKit/MochiKit.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <style>
     .up {
       height: 14px;
       width: 1px;
       background: blue;
       font-size: 11px;
       color: white;
     }
     .down {
       height: 14px;
       width: 1px;
       background: blue;
       font-size: 11px;
       color: white;
     }
   </style>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=444546">Mozilla Bug 444546</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 444546 **/

  var xhrCount = 5;
  var xhrs = new Array();
  var uploads = new Array();
  var maxSize = 5000000;
  var hugeString = new Array(maxSize + 1).join('a');

  function updateProgress(evt) {
    ++evt.target.pcounter;
    var time = new Date().getTime();
    // 350 - 200 = 150ms
    if ((time - evt.target.prevTime) < 150) {
      evt.target.log.parentNode.style.background = "red";
    }
    var diff = (time - evt.target.prevTime);
    if (evt.target.min == -1 || evt.target.min > diff) {
      evt.target.min = diff;
    }
    if (evt.target.max == -1 || evt.target.max < diff) {
      evt.target.max = diff;
    }

    evt.target.log.textContent = diff + "ms";
    evt.target.prevTime = time;
    if (evt.lengthComputable) {
      var loaded = (evt.loaded / evt.total);
      if (loaded < 1) {
        evt.target.log.style.width = (loaded * 400) + "px";
      }
    }
  }

  function loaded(evt) {
    evt.target.log.style.width = "400px";
    evt.target.log.style.background = "green";
    if ("xhr" in evt.target) {
      evt.target.xhr.prevTime = new Date().getTime();
      evt.target.xhr.startTime = evt.target.xhr.prevTime;
    }
    var total = new Date().getTime() - evt.target.startTime;
    evt.target.log.textContent = "total:" + total + "ms";
    if (evt.target.pcounter) {
      evt.target.log.textContent += " ," + evt.target.pcounter + "pe, avg:" +
        parseInt((evt.target.prevTime - evt.target.startTime)/evt.target.pcounter) + "ms";
    }
    if (evt.target.min != -1) {
      ok(evt.target.min >= 150, "Events fired too fast!");
      evt.target.log.textContent += ", min:" + evt.target.min + "ms";
    }
    if (evt.target.max != -1) {
      // Disabled for now.
      //ok(evt.target.max <= 550, "Events didn't fire fast enough!");
      evt.target.log.textContent += ", max:" + evt.target.max + "ms";
    }
    if ("upload" in evt.target) {
      is(evt.total, maxSize * 10, "Wrong data!");
      --xhrCount;
      if (xhrCount == 0) {
        // This is a hack. To get more progress events, server sends the data
        // 10 times.
        SimpleTest.finish();
      } else {
        setTimeout(start, 10);
      }
    } else {
      is(evt.total, maxSize, "Wrong data!");
    }
  }

  function start() {
    var xhr = new XMLHttpRequest();
    xhrs.push(xhr);
    uploads.push(xhr.upload);
    var container = document.createElement("tr");
    var td1 = document.createElement("td");
    container.appendChild(td1);
    td1.textContent = xhrs.length + ".";
    var td2 = document.createElement("td");
    container.appendChild(td2);
    var td3 = document.createElement("td");
    container.appendChild(td3);
    var uploadElement = document.createElement("div");
    td2.appendChild(uploadElement);
    uploadElement.className = "up";
    var downloadElement = document.createElement("div");
    td3.appendChild(downloadElement);
    downloadElement.className = "down";
    document.getElementById('tbody').appendChild(container);
    xhr.log = downloadElement;
    xhr.upload.log = uploadElement;
    xhr.onprogress = updateProgress;
    xhr.upload.onprogress = updateProgress;
    xhr.onload = loaded;
    xhr.upload.onload = loaded;
    xhr.open("POST", "bug444546.sjs");
    xhr.upload.prevTime = new Date().getTime();
    xhr.upload.startTime = xhr.upload.prevTime;
    xhr.upload.xhr = xhr;
    xhr.pcounter = 0;
    xhr.upload.pcounter = 0;
    xhr.min = -1;
    xhr.upload.min = -1;
    xhr.max = -1;
    xhr.upload.max = -1;
    xhr.send(hugeString);
  }

  SimpleTest.waitForExplicitFinish();
  addLoadEvent(function() { setTimeout(start, 10); });

</script>
</pre>
  <table>
    <tbody id="tbody">
      <tr>
        <td>XHR</td>
        <td style="min-width: 410px;">upload</td>
        <td style="min-width: 410px;">download</td>
      </tr>
    </tbody>
  </table>
</body>
</html>