tor-browser

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

test_multiple_register_different_scope.html (3582B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 Bug 1150812: Test registering for two different scopes.
      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 1150812</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=1150812">Mozilla Bug 1150812</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  var scopeA = "./a/";
     27  var scopeB = "./b/";
     28 
     29  function debug() {
     30  //  console.log(str + "\n");
     31  }
     32 
     33  function registerServiceWorker(scope) {
     34    return navigator.serviceWorker.register("worker.js?caller=test_multiple_register_different_scope.html", {scope})
     35      .then(swr => waitForActive(swr));
     36  }
     37 
     38  function unregister(swr) {
     39    return swr.unregister()
     40      .then(result => {
     41        ok(result, "Unregister should return true.");
     42      }, err => {
     43        ok(false, "Unregistering the SW failed with " + err + "\n");
     44        throw err;
     45      });
     46  }
     47 
     48  function subscribe(swr) {
     49    return swr.pushManager.subscribe()
     50      .then(sub => {
     51        ok(true, "successful registered for push notification");
     52        return sub;
     53      }, err => {
     54        ok(false, "could not register for push notification");
     55        throw err;
     56      });
     57  }
     58 
     59 
     60  function setupMultipleSubscriptions(swr1, swr2) {
     61    return Promise.all([
     62      subscribe(swr1),
     63      subscribe(swr2),
     64    ]).then(a => {
     65      ok(a[0].endpoint != a[1].endpoint, "setupMultipleSubscriptions - Got different endpoints.");
     66      return a;
     67    });
     68  }
     69 
     70  function getEndpointExpectNull(swr) {
     71    return swr.pushManager.getSubscription()
     72      .then(pushSubscription => {
     73        ok(pushSubscription == null, "getEndpoint should return null when app not subscribed.");
     74      }, err => {
     75        ok(false, "could not register for push notification");
     76        throw err;
     77      });
     78  }
     79 
     80  function getEndpoint(swr, results) {
     81    return swr.pushManager.getSubscription()
     82      .then(sub => {
     83        ok((results[0].endpoint == sub.endpoint) ||
     84           (results[1].endpoint == sub.endpoint), "getEndpoint - Got the same endpoint back.");
     85        return results;
     86      }, err => {
     87          ok(false, "could not register for push notification");
     88          throw err;
     89      });
     90  }
     91 
     92  function unsubscribe(result) {
     93    return result[0].unsubscribe()
     94      .then(_ => result[1].unsubscribe());
     95  }
     96 
     97  function runTest() {
     98    registerServiceWorker(scopeA)
     99    .then(swrA =>
    100      registerServiceWorker(scopeB)
    101        .then(swrB =>
    102          getEndpointExpectNull(swrA)
    103            .then(_ => getEndpointExpectNull(swrB))
    104            .then(_ => setupMultipleSubscriptions(swrA, swrB))
    105            .then(results => getEndpoint(swrA, results))
    106            .then(results => getEndpoint(swrB, results))
    107            .then(results => unsubscribe(results))
    108            .then(_ => unregister(swrA))
    109            .then(_ => unregister(swrB))
    110        )
    111    )
    112    .catch(err => {
    113      ok(false, "Some test failed with error " + err);
    114    }).then(SimpleTest.finish);
    115  }
    116 
    117  setupPrefsAndMockSocket(new MockWebSocket())
    118    .then(_ => setPushPermission(true))
    119    .then(_ => runTest());
    120  SimpleTest.waitForExplicitFinish();
    121 </script>
    122 </body>
    123 </html>