content/base/test/test_bug218236.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/test_bug218236.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,139 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=218236
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for Bug 218236</title>
    1.11 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
    1.12 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.13 +</head>
    1.14 +<body>
    1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=218236">Mozilla Bug 218236</a>
    1.16 +<p id="display"></p>
    1.17 +<div id="content" style="display: none">
    1.18 +  
    1.19 +</div>
    1.20 +<pre id="test">
    1.21 +<script class="testbody" type="text/javascript">
    1.22 +
    1.23 +/** Test for Bug 218236 **/
    1.24 +
    1.25 +SimpleTest.waitForExplicitFinish();
    1.26 +
    1.27 +/* Test data */
    1.28 +
    1.29 +var url_200 = window.location.href;
    1.30 +var url_404 = url_200.replace(/[^/]+$/, "this_file_is_not_going_to_be_there.dummy");
    1.31 +var url_connection_error = url_200.replace(/^(\w+:\/\/[^/]+?)(:\d+)?\//, "$1:9546/");
    1.32 +
    1.33 +// List of tests: name of the test, URL to be requested, expected sequence
    1.34 +// of events and optionally a function to be called from readystatechange handler.
    1.35 +// Numbers in the list of events are values of XMLHttpRequest.readyState
    1.36 +// when readystatechange event is triggered.
    1.37 +var tests = [
    1.38 +  ["200 OK",                            url_200,                [1, 2, 3, 4, "load"], null],
    1.39 +  ["404 Not Found",                     url_404,                [1, 2, 3, 4, "load"], null],
    1.40 +  ["connection error",                  url_connection_error,   [1, 2, 4, "error"],   null],
    1.41 +  ["abort() call on readyState = 1",    url_200,                [1, 4],               null, doAbort1],
    1.42 +  ["abort() call on readyState = 2",    url_200,                [1, 2, 4],            doAbort2],
    1.43 +];
    1.44 +
    1.45 +var testName = null;
    1.46 +var currentState = 0;
    1.47 +var currentSequence = null;
    1.48 +var expectedSequence = null;
    1.49 +var currentCallback = null;
    1.50 +var finalizeTimeoutID = null;
    1.51 +
    1.52 +var request = null;
    1.53 +
    1.54 +runNextTest();
    1.55 +
    1.56 +function doAbort1() {
    1.57 +  if (request.readyState == 1)
    1.58 +    request.abort();
    1.59 +}
    1.60 +function doAbort2() {
    1.61 +  if (request.readyState == 2)
    1.62 +    request.abort();
    1.63 +}
    1.64 +
    1.65 +/* Utility functions */
    1.66 +
    1.67 +function runNextTest() {
    1.68 +  if (tests.length > 0) {
    1.69 +    var test = tests.shift();
    1.70 +
    1.71 +    // Initialize state variables
    1.72 +    testName = test[0]
    1.73 +    currentState = 0;
    1.74 +    currentSequence = [];
    1.75 +    expectedSequence = test[2];
    1.76 +    currentCallback = test[3];
    1.77 +    postSendCallback = test[4];
    1.78 +
    1.79 +    // Prepare request object
    1.80 +    request = new XMLHttpRequest();
    1.81 +    request.onreadystatechange = onReadyStateChange;
    1.82 +    request.open("GET", test[1]);
    1.83 +    request.onload = onLoad;
    1.84 +    request.onerror = onError;
    1.85 +
    1.86 +    // Start request
    1.87 +    request.send(null);
    1.88 +    if (postSendCallback)
    1.89 +      postSendCallback();
    1.90 +  }
    1.91 +  else
    1.92 +    SimpleTest.finish();
    1.93 +}
    1.94 +
    1.95 +function finalizeTest() {
    1.96 +  finalizeTimeoutID = null;
    1.97 +  ok(compareArrays(expectedSequence, currentSequence), "event sequence for '" + testName + "' was " + currentSequence.join(", "));
    1.98 +
    1.99 +  runNextTest();
   1.100 +}
   1.101 +
   1.102 +function onReadyStateChange() {
   1.103 +  clearTimeout(finalizeTimeoutID);
   1.104 +  finalizeTimeoutID = null;
   1.105 +
   1.106 +  currentState = request.readyState;
   1.107 +  currentSequence.push(currentState);
   1.108 +
   1.109 +  if (currentState == 4) {
   1.110 +    // Allow remaining event to fire but then we are finished with this test
   1.111 +    // unless we get another onReadyStateChange in which case we'll cancel
   1.112 +    // this timeout
   1.113 +    finalizeTimeoutID = setTimeout(finalizeTest, 0);
   1.114 +  }
   1.115 +
   1.116 +  if (currentCallback)
   1.117 +    currentCallback();
   1.118 +}
   1.119 +
   1.120 +function onLoad() {
   1.121 +  currentSequence.push("load");
   1.122 +}
   1.123 +
   1.124 +function onError() {
   1.125 +  currentSequence.push("error");
   1.126 +}
   1.127 +
   1.128 +function compareArrays(array1, array2) {
   1.129 +  if (array1.length != array2.length)
   1.130 +    return false;
   1.131 +
   1.132 +  for (var i = 0; i < array1.length; i++)
   1.133 +    if (array1[i] != array2[i])
   1.134 +      return false;
   1.135 +
   1.136 +  return true;
   1.137 +}
   1.138 +</script>
   1.139 +</pre>
   1.140 +</body>
   1.141 +</html>
   1.142 +

mercurial