tor-browser

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

test_multiple_register_during_service_activation.html (3310B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 Bug 1150812: If service is in activating or no connection state it can not send
      5 request immediately, but the requests are queued. This test test the case of
      6 multiple subscription for the same scope during activation.
      7 
      8 Any copyright is dedicated to the Public Domain.
      9 http://creativecommons.org/licenses/publicdomain/
     10 
     11 -->
     12 <head>
     13  <title>Test for Bug 1150812</title>
     14  <script src="/tests/SimpleTest/SimpleTest.js"></script>
     15  <script type="text/javascript" src="/tests/dom/push/test/test_utils.js"></script>
     16  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     17  <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
     18 </head>
     19 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1150812">Mozilla Bug 1150812</a>
     20 <p id="display"></p>
     21 <div id="content" style="display: none">
     22 
     23 </div>
     24 <pre id="test">
     25 </pre>
     26 
     27 <script class="testbody" type="text/javascript">
     28  function debug() {
     29  //  console.log(str + "\n");
     30  }
     31 
     32  function registerServiceWorker() {
     33    return navigator.serviceWorker.register("worker.js?caller=test_multiple_register_during_service_activation.html", {scope: "."});
     34  }
     35 
     36  function unregister(swr) {
     37    return swr.unregister()
     38      .then(result => {
     39        ok(result, "Unregister should return true.");
     40      }, err => {
     41        dump("Unregistering the SW failed with " + err + "\n");
     42        throw err;
     43      });
     44  }
     45 
     46  function subscribe(swr) {
     47    return swr.pushManager.subscribe()
     48      .then(sub => {
     49        ok(true, "successful registered for push notification");
     50        return sub;
     51      }, err => {
     52        ok(false, "could not register for push notification");
     53        throw err;
     54      });
     55  }
     56 
     57  function setupMultipleSubscriptions(swr) {
     58    // We need to do this to restart service so that a queue will be formed.
     59    let promiseTeardown = teardownMockPushSocket();
     60    setupMockPushSocket(new MockWebSocket());
     61 
     62    var pushSubscription;
     63    return Promise.all([
     64      subscribe(swr),
     65      subscribe(swr),
     66    ]).then(a => {
     67      ok(a[0].endpoint == a[1].endpoint, "setupMultipleSubscriptions - Got the same endpoint back.");
     68      pushSubscription = a[0];
     69      return promiseTeardown;
     70    }).then(_ => {
     71      return pushSubscription;
     72    }, err => {
     73      ok(false, "could not register for push notification");
     74      throw err;
     75    });
     76  }
     77 
     78  function getEndpointExpectNull(swr) {
     79    return swr.pushManager.getSubscription()
     80      .then(pushSubscription => {
     81        ok(pushSubscription == null, "getEndpoint should return null when app not subscribed.");
     82      }, err => {
     83        ok(false, "could not register for push notification");
     84        throw err;
     85      });
     86  }
     87 
     88  function unsubscribe(sub) {
     89    return sub.unsubscribe();
     90  }
     91 
     92  function runTest() {
     93    registerServiceWorker()
     94    .then(swr =>
     95      getEndpointExpectNull(swr)
     96        .then(_ => setupMultipleSubscriptions(swr))
     97        .then(sub => unsubscribe(sub))
     98        .then(_ => unregister(swr))
     99    )
    100    .catch(err => {
    101      ok(false, "Some test failed with error " + err);
    102    }).then(SimpleTest.finish);
    103  }
    104 
    105  setupPrefsAndMockSocket(new MockWebSocket()).then(_ => runTest());
    106  SpecialPowers.addPermission("desktop-notification", true, document);
    107  SimpleTest.waitForExplicitFinish();
    108 </script>
    109 </body>
    110 </html>