tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_multiple_register.html (3715B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 Bug 1038811: Push tests.
      5 
      6 Any copyright is dedicated to the Public Domain.
      7 http://creativecommons.org/licenses/publicdomain/
      8 
      9 -->
     10 <head>
     11  <title>Test for Bug 1038811</title>
     12  <script src="/tests/SimpleTest/SimpleTest.js"></script>
     13  <script type="text/javascript" src="/tests/dom/push/test/test_utils.js"></script>
     14  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     15  <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
     16 </head>
     17 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1038811">Mozilla Bug 1038811</a>
     18 <p id="display"></p>
     19 <div id="content" style="display: none">
     20 
     21 </div>
     22 <pre id="test">
     23 </pre>
     24 
     25 <script class="testbody" type="text/javascript">
     26  function debug() {
     27  //  console.log(str + "\n");
     28  }
     29 
     30  var registration;
     31 
     32  function start() {
     33    return navigator.serviceWorker.register("worker.js?caller=test_multiple_register.html", {scope: "."})
     34    .then((swr) => {
     35      registration = swr;
     36      return waitForActive(registration);
     37    });
     38  }
     39 
     40  function unregister() {
     41    return registration.unregister().then(function(result) {
     42      ok(result, "Unregister should return true.");
     43    }, function(e) {
     44      dump("Unregistering the SW failed with " + e + "\n");
     45    });
     46  }
     47 
     48  function setupPushNotification(swr) {
     49    var p = new Promise(function(res) {
     50      swr.pushManager.subscribe().then(
     51        function(pushSubscription) {
     52          ok(true, "successful registered for push notification");
     53          res({swr, pushSubscription});
     54        }, function() {
     55          ok(false, "could not register for push notification");
     56          res(null);
     57        }
     58        );
     59    });
     60    return p;
     61  }
     62 
     63  function setupSecondEndpoint(result) {
     64    var p = new Promise(function(res) {
     65      result.swr.pushManager.subscribe().then(
     66        function(pushSubscription) {
     67          ok(result.pushSubscription.endpoint == pushSubscription.endpoint, "setupSecondEndpoint - Got the same endpoint back.");
     68          res(result);
     69        }, function() {
     70          ok(false, "could not register for push notification");
     71          res(null);
     72        }
     73        );
     74    });
     75    return p;
     76  }
     77 
     78  function getEndpointExpectNull(swr) {
     79    var p = new Promise(function(res) {
     80      swr.pushManager.getSubscription().then(
     81        function(pushSubscription) {
     82          ok(pushSubscription == null, "getEndpoint should return null when app not subscribed.");
     83          res(swr);
     84        }, function() {
     85          ok(false, "could not register for push notification");
     86          res(null);
     87        }
     88        );
     89    });
     90    return p;
     91  }
     92 
     93  function getEndpoint(result) {
     94    var p = new Promise(function(res) {
     95      result.swr.pushManager.getSubscription().then(
     96        function(pushSubscription) {
     97          ok(result.pushSubscription.endpoint == pushSubscription.endpoint, "getEndpoint - Got the same endpoint back.");
     98 
     99          res(pushSubscription);
    100        }, function() {
    101          ok(false, "could not register for push notification");
    102          res(null);
    103        }
    104        );
    105    });
    106    return p;
    107  }
    108 
    109  function unregisterPushNotification(pushSubscription) {
    110    return pushSubscription.unsubscribe();
    111  }
    112 
    113  function runTest() {
    114    start()
    115    .then(getEndpointExpectNull)
    116    .then(setupPushNotification)
    117    .then(setupSecondEndpoint)
    118    .then(getEndpoint)
    119    .then(unregisterPushNotification)
    120    .then(unregister)
    121    .catch(function(e) {
    122      ok(false, "Some test failed with error " + e);
    123    }).then(SimpleTest.finish);
    124  }
    125 
    126  setupPrefsAndMockSocket(new MockWebSocket())
    127    .then(_ => setPushPermission(true))
    128    .then(_ => runTest());
    129  SimpleTest.waitForExplicitFinish();
    130 </script>
    131 </body>
    132 </html>