content/base/test/test_bug435425.html

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     1 <!DOCTYPE HTML>
     2 <html>
     3 <!--
     4 https://bugzilla.mozilla.org/show_bug.cgi?id=435425
     5 -->
     6 <head>
     7   <title>Test for Bug 435425</title>
     8   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     9   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    10 </head>
    11 <body>
    12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=435425">Mozilla Bug 435425</a>
    13 <p id="display"></p>
    14 <div id="content" style="display: none">
    16 </div>
    17 <pre id="test">
    18 <script class="testbody" type="text/javascript">
    20 /** Test for Bug 435425 **/
    22 var xhr = null;
    23 var upload = null;
    24 var currentEvents = null;
    25 var expectedResponseText = null;
    26 var uploadTotal = 0;
    28 function logEvent(evt) {
    29   var i = 0;
    30   while ((currentEvents.length != i) &&
    31          currentEvents[i].optional &&
    32          ((currentEvents[i].type != evt.type) ||
    33           !(evt.target instanceof currentEvents[i].target))) {
    34     ++i;
    35   }
    36   if (evt.target instanceof XMLHttpRequestUpload) {
    37     if (evt.type == "loadstart") {
    38       uploadTotal = evt.total
    39     } else {
    40       if (evt.type == "progress") {
    41         ok(evt.lengthComputable, "event(" + evt.type +  ").lengthComputable should be true.");
    42       }
    43       is(evt.total, uploadTotal, "event(" + evt.type +  ").total should not change during upload.");
    44     }
    45   }
    46   ok(i != currentEvents.length, "Extra or wrong event?");
    47   is(evt.type, currentEvents[i].type, "Wrong event!")
    48   ok(evt.target instanceof currentEvents[i].target,
    49      "Wrong event target [" + evt.target + "," + evt.type + "]!");
    50   // If we handled non-optional event, remove all optional events before the 
    51   // handled event and then the non-optional event from the list.
    52   if (!currentEvents[i].optional) {
    53     for (;i != -1; --i) {
    54       currentEvents.shift();
    55     }
    56   }
    57 }
    59 function hasPendingNonOptionalEvent(ev) {
    60   var i = 0;
    61   while (i < currentEvents.length) {
    62     if (!currentEvents[i].optional && currentEvents[i].type == ev)
    63       return true;
    64     ++i;
    65   }
    66   return false;
    67 }
    69 function maybeStop(evt) {
    70   logEvent(evt);
    71   if (!hasPendingNonOptionalEvent("loadend"))
    72     nextTest();
    73 }
    75 function openXHR(xhr, method, url, privileged) {
    76   if (privileged)
    77     SpecialPowers.wrap(xhr).open(method, url);
    78   else
    79     xhr.open(method, url);
    80 }
    82 function start(obj) {
    83   xhr = new XMLHttpRequest();
    84   upload = xhr.upload;
    85   currentEvents = obj.expectedEvents;
    86   expectedResponseText = obj.withUpload;
    87   xhr.onload =
    88     function(evt) {
    89       if (expectedResponseText) {
    90         is(evt.target.responseText, expectedResponseText, "Wrong responseText");
    91       }
    92       logEvent(evt);
    93     }
    94   xhr.onerror =
    95     function(evt) {
    96       logEvent(evt);
    97     }
    98   xhr.onabort =
    99     function(evt) {
   100       logEvent(evt);
   101     }
   102   xhr.onloadend =
   103     function (evt) {
   104       maybeStop(evt);
   105     }
   106   xhr.onloadstart =
   107     function (evt) {
   108       logEvent(evt);
   109     }
   110   xhr.onprogress =
   111     function (evt) {
   112       logEvent(evt);
   113     }
   115   if ("upload" in xhr) {
   116     xhr.upload.onloadstart =
   117       function (evt) {
   118         logEvent(evt);
   119       }
   120     xhr.upload.onprogress =
   121       function (evt) {
   122         logEvent(evt);
   123       }
   124     xhr.upload.onload =
   125       function (evt) {
   126         logEvent(evt);
   127       }
   128     xhr.upload.onerror =
   129       function (evt) {
   130         logEvent(evt);
   131       }
   132     xhr.upload.onabort =
   133       function (evt) {
   134         logEvent(evt);
   135       }
   136     xhr.upload.onloadend =
   137       function (evt) {
   138         maybeStop(evt);
   139       }
   140   }
   142   try {
   143     var methodIsGet = (obj.method == "GET");
   144     var url;
   145     var privileged = false;
   146     if (obj.testRedirectError) {
   147       url = "bug435425_redirect.sjs";
   148     } else if (obj.testNetworkError) {
   149       url = "http://nosuchdomain.localhost";
   150       privileged = true;
   151     } else {
   152       url = "bug435425.sjs";
   153       if (obj.withUpload && methodIsGet) {
   154         url += "?" + obj.withUpload;
   155       }
   156     }
   157     openXHR(xhr, obj.method, url, privileged);
   158     xhr.send(!methodIsGet ? obj.withUpload : null);
   159     if (obj.testAbort) {
   160       xhr.abort();
   161     }
   162   } catch (ex) {
   163     ok(false, ex);
   164   }
   165 }
   167 var none = null;
   168 var small = "";
   169 var mid = "";
   170 var large = "";
   173 for (var smallLength = 0; smallLength < (0x00000000 + 2); ++smallLength) {
   174   small += "A";
   175 }
   177 for (var midLength = 0; midLength < (0x00000FFF + 2); ++midLength) {
   178   mid += "A";
   179 }
   181 for (var largeLength = 0; largeLength < (0x0000FFFF + 2); ++largeLength) {
   182   large += "A";
   183 }
   185 const XHR = XMLHttpRequest;
   186 const UPLOAD = XMLHttpRequestUpload;
   188 var tests = 
   189   [
   190     { method: "GET", withUpload: none, testAbort: false, testRedirectError: false, testNetworkError: false,
   191       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   192                        {target: XHR, type: "progress", optional: true},
   193                        {target: XHR, type: "load", optional: false},
   194                        {target: XHR, type: "loadend", optional: false}]},
   195     { method: "GET", withUpload: none, testAbort: true, testRedirectError: false, testNetworkError: false,
   196       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   197                        {target: XHR, type: "abort", optional: false},
   198                        {target: XHR, type: "loadend", optional: false}]},
   199     { method: "GET", withUpload: none, testAbort: false, testRedirectError: true, testNetworkError: false,
   200       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   201                        {target: XHR, type: "error", optional: false},
   202                        {target: XHR, type: "loadend", optional: false}]},
   203     { method: "GET", withUpload: none, testAbort: false, testRedirectError: false, testNetworkError: true,
   204       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   205                        {target: XHR, type: "error", optional: false},
   206                        {target: XHR, type: "loadend", optional: false}]},
   208     { method: "GET", withUpload: small, testAbort: false, testRedirectError: false, testNetworkError: false,
   209       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   210                        {target: XHR, type: "progress", optional: true},
   211                        {target: XHR, type: "load", optional: false},
   212                        {target: XHR, type: "loadend", optional: false}]},
   213     { method: "GET", withUpload: small, testAbort: true, testRedirectError: false, testNetworkError: false,
   214       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   215                        {target: XHR, type: "abort", optional: false},
   216                        {target: XHR, type: "loadend", optional: false}]},
   217     { method: "GET", withUpload: small, testAbort: false, testRedirectError: true, testNetworkError: false,
   218       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   219                        {target: XHR, type: "error", optional: false},
   220                        {target: XHR, type: "loadend", optional: false}]},
   221     { method: "GET", withUpload: small, testAbort: false, testRedirectError: false, testNetworkError: true,
   222       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   223                        {target: XHR, type: "error", optional: false},
   224                        {target: XHR, type: "loadend", optional: false}]},
   226     { method: "GET", withUpload: mid, testAbort: false, testRedirectError: false, testNetworkError: false,
   227       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   228                        {target: XHR, type: "progress", optional: true},
   229                        {target: XHR, type: "load", optional: false},
   230                        {target: XHR, type: "loadend", optional: false}]},
   231     { method: "GET", withUpload: mid, testAbort: true, testRedirectError: false, testNetworkError: false,
   232       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   233                        {target: XHR, type: "abort", optional: false},
   234                        {target: XHR, type: "loadend", optional: false}]},
   235     { method: "GET", withUpload: mid, testAbort: false, testRedirectError: true, testNetworkError: false,
   236       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   237                        {target: XHR, type: "error", optional: false},
   238                        {target: XHR, type: "loadend", optional: false}]},
   239     { method: "GET", withUpload: mid, testAbort: false, testRedirectError: false, testNetworkError: true,
   240       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   241                        {target: XHR, type: "error", optional: false},
   242                        {target: XHR, type: "loadend", optional: false}]},
   244     { method: "GET", withUpload: large, testAbort: false, testRedirectError: false, testNetworkError: false,
   245       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   246                        {target: XHR, type: "progress", optional: true},
   247                        {target: XHR, type: "load", optional: false},
   248                        {target: XHR, type: "loadend", optional: false}]},
   249     { method: "GET", withUpload: large, testAbort: true, testRedirectError: false, testNetworkError: false,
   250       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   251                        {target: XHR, type: "abort", optional: false},
   252                        {target: XHR, type: "loadend", optional: false}]},
   253     { method: "GET", withUpload: large, testAbort: false, testRedirectError: true, testNetworkError: false,
   254       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   255                        {target: XHR, type: "error", optional: false},
   256                        {target: XHR, type: "loadend", optional: false}]},
   257     { method: "GET", withUpload: large, testAbort: false, testRedirectError: false, testNetworkError: true,
   258       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   259                        {target: XHR, type: "error", optional: false},
   260                        {target: XHR, type: "loadend", optional: false}]},
   262     { method: "POST", withUpload: none, testAbort: false, testRedirectError: false, testNetworkError: false,
   263       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   264                        {target: XHR, type: "progress", optional: true},
   265                        {target: XHR, type: "load", optional: false},
   266                        {target: XHR, type: "loadend", optional: false}]},
   267     { method: "POST", withUpload: none, testAbort: true, testRedirectError: false, testNetworkError: false,
   268       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   269                        {target: XHR, type: "abort", optional: false},
   270                        {target: XHR, type: "loadend", optional: false}]},
   271     { method: "POST", withUpload: none, testAbort: false, testRedirectError: true, testNetworkError: false,
   272       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   273                        {target: XHR, type: "error", optional: false},
   274                        {target: XHR, type: "loadend", optional: false}]},
   275     { method: "POST", withUpload: none, testAbort: false, testRedirectError: false, testNetworkError: true,
   276       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   277                        {target: XHR, type: "error", optional: false},
   278                        {target: XHR, type: "loadend", optional: false}]},
   280     { method: "POST", withUpload: small, testAbort: false, testRedirectError: false, testNetworkError: false,
   281       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   282                        {target: UPLOAD, type: "loadstart", optional: false},
   283                        {target: UPLOAD, type: "progress", optional: true},
   284                        {target: UPLOAD, type: "load", optional: false},
   285                        {target: UPLOAD, type: "loadend", optional: false},
   286                        {target: XHR, type: "progress", optional: true},
   287                        {target: XHR, type: "load", optional: false},
   288                        {target: XHR, type: "loadend", optional: false}]},
   289     { method: "POST", withUpload: small, testAbort: true, testRedirectError: false, testNetworkError: false,
   290       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   291                        {target: UPLOAD, type: "loadstart", optional: false},
   292                        {target: XHR, type: "abort", optional: false},
   293                        {target: XHR, type: "loadend", optional: false},
   294                        {target: UPLOAD, type: "abort", optional: false},
   295                        {target: UPLOAD, type: "loadend", optional: false}]},
   296     { method: "POST", withUpload: small, testAbort: false, testRedirectError: true, testNetworkError: false,
   297       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   298                        {target: UPLOAD, type: "loadstart", optional: false},
   299                        {target: UPLOAD, type: "progress", optional: true},
   300                        {target: XHR, type: "error", optional: false},
   301                        {target: XHR, type: "loadend", optional: false},
   302                        {target: UPLOAD, type: "error", optional: false},
   303                        {target: UPLOAD, type: "loadend", optional: false}]},
   304     { method: "POST", withUpload: small, testAbort: false, testRedirectError: false, testNetworkError: true,
   305       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   306                        {target: UPLOAD, type: "loadstart", optional: false},
   307                        {target: UPLOAD, type: "progress", optional: true},
   308                        {target: XHR, type: "error", optional: false},
   309                        {target: XHR, type: "loadend", optional: false},
   310                        {target: UPLOAD, type: "error", optional: false},
   311                        {target: UPLOAD, type: "loadend", optional: false}]},
   313     { method: "POST", withUpload: mid, testAbort: false, testRedirectError: false, testNetworkError: false,
   314       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   315                        {target: UPLOAD, type: "loadstart", optional: false},
   316                        {target: UPLOAD, type: "progress", optional: true},
   317                        {target: UPLOAD, type: "load", optional: false},
   318                        {target: UPLOAD, type: "loadend", optional: false},
   319                        {target: XHR, type: "progress", optional: true},
   320                        {target: XHR, type: "load", optional: false},
   321                        {target: XHR, type: "loadend", optional: false}]},
   322     { method: "POST", withUpload: mid, testAbort: true, testRedirectError: false, testNetworkError: false,
   323       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   324                        {target: UPLOAD, type: "loadstart", optional: false},
   325                        {target: XHR, type: "abort", optional: false},
   326                        {target: XHR, type: "loadend", optional: false},
   327                        {target: UPLOAD, type: "abort", optional: false},
   328                        {target: UPLOAD, type: "loadend", optional: false}]},
   329     { method: "POST", withUpload: mid, testAbort: false, testRedirectError: true, testNetworkError: false,
   330       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   331                        {target: UPLOAD, type: "loadstart", optional: false},
   332                        {target: UPLOAD, type: "progress", optional: true},
   333                        {target: XHR, type: "error", optional: false},
   334                        {target: XHR, type: "loadend", optional: false},
   335                        {target: UPLOAD, type: "error", optional: false},
   336                        {target: UPLOAD, type: "loadend", optional: false}]},
   337     { method: "POST", withUpload: mid, testAbort: false, testRedirectError: false, testNetworkError: true,
   338       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   339                        {target: UPLOAD, type: "loadstart", optional: false},
   340                        {target: UPLOAD, type: "progress", optional: true},
   341                        {target: XHR, type: "error", optional: false},
   342                        {target: XHR, type: "loadend", optional: false},
   343                        {target: UPLOAD, type: "error", optional: false},
   344                        {target: UPLOAD, type: "loadend", optional: false}]},
   346     { method: "POST", withUpload: large, testAbort: false, testRedirectError: false, testNetworkError: false,
   347       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   348                        {target: UPLOAD, type: "loadstart", optional: false},
   349                        {target: UPLOAD, type: "progress", optional: true},
   350                        {target: UPLOAD, type: "load", optional: false},
   351                        {target: UPLOAD, type: "loadend", optional: false},
   352                        {target: XHR, type: "progress", optional: true},
   353                        {target: XHR, type: "load", optional: false},
   354                        {target: XHR, type: "loadend", optional: false}]},
   355     { method: "POST", withUpload: large, testAbort: true, testRedirectError: false, testNetworkError: false,
   356       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   357                        {target: UPLOAD, type: "loadstart", optional: false},
   358                        {target: XHR, type: "abort", optional: false},
   359                        {target: XHR, type: "loadend", optional: false},
   360                        {target: UPLOAD, type: "abort", optional: false},
   361                        {target: UPLOAD, type: "loadend", optional: false}]},
   362     { method: "POST", withUpload: large, testAbort: false, testRedirectError: true, testNetworkError: false,
   363       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   364                        {target: UPLOAD, type: "loadstart", optional: false},
   365                        {target: UPLOAD, type: "progress", optional: true},
   366                        {target: XHR, type: "error", optional: false},
   367                        {target: XHR, type: "loadend", optional: false},
   368                        {target: UPLOAD, type: "error", optional: false},
   369                        {target: UPLOAD, type: "loadend", optional: false}]},
   370     { method: "POST", withUpload: large, testAbort: false, testRedirectError: false, testNetworkError: true,
   371       expectedEvents: [{target: XHR, type: "loadstart", optional: false},
   372                        {target: UPLOAD, type: "loadstart", optional: false},
   373                        {target: UPLOAD, type: "progress", optional: true},
   374                        {target: XHR, type: "error", optional: false},
   375                        {target: XHR, type: "loadend", optional: false},
   376                        {target: UPLOAD, type: "error", optional: false},
   377                        {target: UPLOAD, type: "loadend", optional: false}]},
   378 ];
   380 function runTest() {
   381   var test = tests.shift();
   382   start(test);
   383 }
   385 function nextTest() {
   386   if (tests.length > 1) {
   387     setTimeout("runTest()", 0);
   388   } else {
   389     SimpleTest.finish();
   390   }
   391 }
   393 ok("upload" in (new XMLHttpRequest()), "XMLHttpRequest.upload isn't supported!");
   394 SimpleTest.waitForExplicitFinish();
   395 addLoadEvent(runTest);
   397 </script>
   398 </pre>
   399 </body>
   400 </html>

mercurial