dom/base/test/test_domrequest.html
author Nicholas Nethercote <nnethercote@mozilla.com>
Mon, 05 Oct 2015 16:59:32 -0700
changeset 301715 3897da20e42dec0bcf73987af1eaac4f8ce950b8
parent 236780 a14b8bf72b1655f2dd70d1a7bc022abb6f9bf96d
child 393926 a072d7934ff7c6fd2ad974d7abc0b08d11041aca
permissions -rw-r--r--
Bug 1211324 (part 3) - Remove GraphicsFilter::FILTER_FAST and replace it with FILTER_BEST. r=mattwoodrow. This may sound like an odd change but it's what the current code effectively already does due to the way ToFilter() and ThebesFilter() are defined.

<!DOCTYPE HTML>
<html>
<head>
  <title>Test for DOMRequest</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">
"use strict";

var reqserv = SpecialPowers.getDOMRequestService();
ok("createRequest" in reqserv, "appears to be a service");

function testBasics() {
  // create a request
  var req = reqserv.createRequest(window);
  ok("result" in req, "request has result");
  ok("error" in req, "request has error");
  ok("onsuccess" in req, "request has onsuccess");
  ok("onerror" in req, "request has onerror");
  ok("readyState" in req, "request has readyState");
  ok("then" in req, "request has then");

  is(req.readyState, "pending", "readyState is pending");
  is(req.result, undefined, "result is undefined");
  is(req.onsuccess, null, "onsuccess is null");
  is(req.onerror, null, "onerror is null");

  runTest();
}

function testSuccess() {
  // fire success
  var req = reqserv.createRequest(window);
  var ev = null;
  req.onsuccess = function(e) {
    ev = e;
  }
  var result = null;
  var promise = req.then(function(r) {
    is(r, "my result", "correct result when resolving the promise");
    result = r;
    runTest();
  }, function(e) {
    ok(false, "promise should not be rejected");
    runTest();
  });
  ok(promise instanceof Promise, "then() should return a Promise");
  reqserv.fireSuccess(req, "my result");
  ok(ev, "got success event");
  is(ev.type, "success", "correct type during success");
  is(ev.target, req, "correct target during success");
  is(req.readyState, "done", "correct readyState after success");
  is(req.error, null, "correct error after success");
  is(req.result, "my result", "correct result after success");
  is(result, null, "Promise should not be resolved synchronously");
}

function testError() {
  // fire error
  var req = reqserv.createRequest(window);
  var ev = null;
  req.onerror = function(e) {
    ev = e;
  }
  var error = null;
  var promise = req.then(function(r) {
    ok(false, "promise should not be resolved");
    runTest();
  }, function(e) {
    ok(e instanceof DOMError, "got error rejection");
    ok(e === req.error, "got correct error when rejecting the promise");
    error = e;
    runTest();
  });
  ok(promise instanceof Promise, "then() should return a Promise");
  reqserv.fireError(req, "OhMyError");
  ok(ev, "got error event");
  is(ev.type, "error", "correct type during error");
  is(ev.target, req, "correct target during error");
  is(req.readyState, "done", "correct readyState after error");
  is(req.error.name, "OhMyError", "correct error after error");
  is(req.result, undefined, "correct result after error");
  is(error, null, "Promise should not be rejected synchronously");
}

function testDetailedError() {
  // fire detailed error
  var req = reqserv.createRequest(window);
  var ev = null;
  req.onerror = function(e) {
    ev = e;
  };
  var error = null;
  var promise = req.then(function(r) {
    ok(false, "promise should not be resolved");
    runTest();
  }, function(e) {
    ok(e instanceof DOMError, "got error rejection");
    ok(e === req.error, "got correct error when rejecting the promise");
    error = e;
    runTest();
  });
  ok(promise instanceof Promise, "then() should return a Promise");
  reqserv.fireDetailedError(req, new DOMError("detailedError"));
  ok(ev, "got error event");
  is(ev.type, "error", "correct type during error");
  is(ev.target, req, "correct target during error");
  is(req.readyState, "done", "correct readyState after error");
  is(req.error.name, "detailedError", "correct error after error");
  is(req.result, undefined, "correct result after error");
  is(error, null, "Promise should not be rejected synchronously");
}

function testThenAfterSuccess() {
  // fire success
  var req = reqserv.createRequest(window);
  var ev = null;
  req.onsuccess = function(e) {
    ev = e;
  }
  reqserv.fireSuccess(req, "my result");
  ok(ev, "got success event");
  is(ev.type, "success", "correct type during success");
  is(ev.target, req, "correct target during success");
  is(req.readyState, "done", "correct readyState after success");
  is(req.error, null, "correct error after success");
  is(req.result, "my result", "correct result after success");
  var result = null;
  var promise = req.then(function(r) {
    is(r, "my result", "correct result when resolving the promise");
    result = r;
    runTest();
  }, function(e) {
    ok(false, "promise should not be rejected");
    runTest();
  });
  ok(promise instanceof Promise, "then() should return a Promise");
  is(result, null, "Promise should not be resolved synchronously");
}

function testThenAfterError() {
  // fire error
  var req = reqserv.createRequest(window);
  var ev = null;
  req.onerror = function(e) {
    ev = e;
  }
  reqserv.fireError(req, "OhMyError");
  ok(ev, "got error event");
  is(ev.type, "error", "correct type during error");
  is(ev.target, req, "correct target during error");
  is(req.readyState, "done", "correct readyState after error");
  is(req.error.name, "OhMyError", "correct error after error");
  is(req.result, undefined, "correct result after error");
  var error = null;
  var promise = req.then(function(r) {
    ok(false, "promise should not be resolved");
    runTest();
  }, function(e) {
    ok(e instanceof DOMError, "got error rejection");
    ok(e === req.error, "got correct error when rejecting the promise");
    error = e;
    runTest();
  });
  ok(promise instanceof Promise, "then() should return a Promise");
  is(error, null, "Promise should not be rejected synchronously");
}

function testDetailedError() {
  // fire detailed error
  var req = reqserv.createRequest(window);
  var ev = null;
  req.onerror = function(e) {
    ev = e;
  };
  var error = null;
  var promise = req.then(function(r) {
    ok(false, "promise should not be resolved");
    runTest();
  }, function(e) {
    ok(e instanceof DOMError, "got error rejection");
    ok(e === req.error, "got correct error when rejecting the promise");
    error = e;
    runTest();
  });
  ok(promise instanceof Promise, "then() should return a Promise");
  reqserv.fireDetailedError(req, new DOMError("detailedError"));
  ok(ev, "got error event");
  is(ev.type, "error", "correct type during error");
  is(ev.target, req, "correct target during error");
  is(req.readyState, "done", "correct readyState after error");
  is(req.error.name, "detailedError", "correct error after error");
  is(req.result, undefined, "correct result after error");
  is(error, null, "Promise should not be rejected synchronously");
}

var tests = [
  testBasics,
  testSuccess,
  testError,
  testDetailedError,
  testThenAfterSuccess,
  testThenAfterError,
];

function runTest() {
  if (!tests.length) {
    SimpleTest.finish();
    return;
  }

  var test = tests.shift();
  test();
}

SimpleTest.waitForExplicitFinish();
runTest();

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