content/base/test/test_xhr_abort_after_load.html
author Gregory Szorc <gps@mozilla.com>
Wed, 28 Jan 2015 13:37:00 -0800
branchMOBILE130_2012050817_RELBRANCH
changeset 120184 c9527b7c0b9a28a3f1209c9488fb16965ed051cd
parent 82226 c531673c8d2f063b72c10bcae1f591a256c3f7e5
child 110253 0a1f4d81635adf13c29c71aa80d4e44198125119
permissions -rw-r--r--
Close old release branch MOBILE130_2012050817_RELBRANCH

<!DOCTYPE HTML>
<html>
<head>
  <title>Test bug 482935</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="	/tests/SimpleTest/test.css" />
</head>
<body onload="onWindowLoad()">
<script class="testbody" type="text/javascript">"use strict";
SimpleTest.waitForExplicitFinish();

var url = "file_XHR_pass1.xml";

function onWindowLoad() {
  runTest();
}

function runTest() {
  var testFunctions = [
    startTest1,
    startTest2,
    startTest3,
  ];

  function nextTest() {
    if (testFunctions.length == 0) {
      SimpleTest.finish();
    }
    (testFunctions.shift())();
  }

  nextTest();

  var xhr;
  function startTest1() {
    xhr = new XMLHttpRequest();
    xhr.onload = onLoad1;
    xhr.open("GET", url);
    xhr.send();
  }

  function onLoad1() {
    is(xhr.readyState, xhr.DONE, "readyState should be DONE");
    xhr.onabort = onAbort1;
    xhr.abort();

    function onAbort1(e) {
      ok(false, e.type + " event should not be fired!");
    }

    is(xhr.readyState, xhr.UNSENT, "readyState should be UNSENT");
    nextTest();
  }

  function startTest2() {
    xhr = new XMLHttpRequest();
    xhr.onloadstart = onAfterSend;
    xhr.open("GET", url);
    xhr.send();
  }

  function startTest3() {
    xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.send();
    onAfterSend();
  }

  function onAfterSend() {
    is(xhr.readyState, xhr.OPENED, "readyState should be OPENED");
    var sent = false;
    try {
      xhr.send();
    } catch (e) {
      sent = true;
    }
    ok(sent, "send() flag should be set");
    var aborted = false;
    xhr.onabort = onAbort2;
    xhr.abort();

    function onAbort2() {
      is(xhr.readyState, xhr.DONE, "readyState should be DONE");
      aborted = true;
    }

    ok(aborted, "abort event should be fired");
    is(xhr.readyState, xhr.UNSENT, "readyState should be UNSENT");
    nextTest();
  }
}

</script>
</body>
</html>