dom/requestsync/tests/test_promise_app.html
author Andrea Marchesini <amarchesini@mozilla.com>
Sun, 04 Jan 2015 10:37:24 +0100
changeset 247785 2ef1c26d77d3f892e385b828f0694a41a3740006
parent 247783 8c01c134e40f0c73b8841e2210b17333944e4804
permissions -rw-r--r--
Bug 1018320 - RequestSync API - patch 7 - reject promise when an exception is thrown, r=fabrice

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for requestSync - promise</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="common_basic.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<div id="container"></div>
  <script type="application/javascript;version=1.7">

  var foobarCounter = 0;
  var pendingCounter = 0;
  function setMessageHandler() {
    navigator.mozSetMessageHandler('request-sync', function(e) {

      if (e.task == 'foobar') {
        ok(true, "foobar message received:" + ++foobarCounter);

        if (foobarCounter == 1) {
          // The first time we wait 2 seconds.
          info("Setting a promise object.");
          navigator.mozSetMessageHandlerPromise(new Promise(function(a, b) {
            SimpleTest.requestFlakyTimeout("Just testing to make sure things work.");
            setTimeout(a, 2000);
          }));
        } else if (foobarCounter == 2) {
          // The second time we don't reply at all.
          info("Setting a promise object without resolving it.");
          navigator.mozSetMessageHandlerPromise(new Promise(function(a, b) {}));
        } else if (foobarCounter == 3) {
          info("Throwing an exception.");
          // Now we throw an exception
          SimpleTest.expectUncaughtException();
          throw "Booom!";
        } else {
          info("Setting a promise object and reject it.");
          navigator.mozSetMessageHandlerPromise(new Promise(function(a, b) {
            setTimeout(b, 0);
          }));
        }
      }

      else if (e.task  == 'pending') {
        ok(true, "pending message received: " + ++pendingCounter);
        if (pendingCounter == 5) {
          runTests();
        }
      }

      else {
      ok(false, "Unknown message");
      }
    });

    runTests();
  }

  function test_register_foobar() {
    navigator.sync.register('foobar', { minInterval: 2,
                                        oneShot: false,
                                        data: 42,
                                        wifiOnly: false,
                                        wakeUpPage: location.href }).then(
    function() {
      ok(true, "navigator.sync.register() foobar done");
      runTests();
    }, genericError);
  }

  function test_register_pending() {
    navigator.sync.register('pending', { minInterval: 2,
                                         oneShot: false,
                                         data: 'hello world!',
                                         wifiOnly: false,
                                         wakeUpPage: location.href }).then(
    function() {
      ok(true, "navigator.sync.register() pending done");
      runTests();
    }, genericError);
  }

  function test_unregister_foobar() {
    navigator.sync.unregister('foobar').then(
    function() {
      ok(true, "navigator.sync.unregister() foobar done");
      runTests();
    }, genericError);
  }

  function test_unregister_pending() {
    navigator.sync.unregister('pending').then(
    function() {
      ok(true, "navigator.sync.unregister() pending done");
      runTests();
    }, genericError);
  }

  function test_wait() {
    // nothing to do here.
  }

  var tests = [
    function() {
      SpecialPowers.pushPrefEnv({"set": [["dom.requestSync.enabled", true],
                                         ["dom.requestSync.minInterval", 1],
                                         ["dom.requestSync.maxTaskTimeout", 5000 /* 5 seconds */],
                                         ["dom.ignore_webidl_scope_checks", true]]}, runTests);
    },

    function() {
      SpecialPowers.pushPermissions(
        [{ "type": "requestsync-manager", "allow": 1, "context": document } ], runTests);
    },

    function() {
      if (SpecialPowers.isMainProcess()) {
        SpecialPowers.Cu.import("resource://gre/modules/RequestSyncService.jsm");
      }
      runTests();
    },

    setMessageHandler,

    test_register_foobar,
    test_register_pending,

    test_wait,

    test_unregister_foobar,
    test_unregister_pending,
  ];

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

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

  SimpleTest.waitForExplicitFinish();
  runTests();
  </script>
</body>
</html>