tor-browser

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

test_try_registering_offline_disabled.html (9783B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 Bug 1150812: Try to register when serviced if offline or connection is disabled.
      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  function debug() {
     27  //  console.log(str + "\n");
     28  }
     29 
     30  function registerServiceWorker() {
     31    return navigator.serviceWorker.register("worker.js?caller=test_try_registering_offline_disabled.html", {scope: "."})
     32      .then(swr => waitForActive(swr));
     33  }
     34 
     35  function unregister(swr) {
     36    return swr.unregister()
     37      .then(result => {
     38        ok(result, "Unregister should return true.");
     39      }, err => {
     40        dump("Unregistering the SW failed with " + err + "\n");
     41        throw err;
     42      });
     43  }
     44 
     45  function subscribe(swr) {
     46    return swr.pushManager.subscribe()
     47      .then(sub => {
     48        ok(true, "successful registered for push notification");
     49        return sub;
     50      }, err => {
     51        ok(false, "could not register for push notification");
     52        throw err;
     53      });
     54  }
     55 
     56  function subscribeFail(swr) {
     57    return new Promise((res) => {
     58      swr.pushManager.subscribe()
     59        .then(() => {
     60          ok(false, "successful registered for push notification");
     61          throw new Error("Should fail");
     62        }, () => {
     63          ok(true, "could not register for push notification");
     64          res(swr);
     65        });
     66    });
     67  }
     68 
     69  function getEndpointExpectNull(swr) {
     70    return swr.pushManager.getSubscription()
     71      .then(pushSubscription => {
     72        ok(pushSubscription == null, "getEndpoint should return null when app not subscribed.");
     73      }, err => {
     74        ok(false, "could not register for push notification");
     75        throw err;
     76      });
     77  }
     78 
     79  function getEndpoint(swr, subOld) {
     80    return swr.pushManager.getSubscription()
     81      .then(sub => {
     82        ok(subOld.endpoint == sub.endpoint, "getEndpoint - Got the same endpoint back.");
     83        return sub;
     84      }, err => {
     85          ok(false, "could not register for push notification");
     86          throw err;
     87      });
     88  }
     89 
     90  // Load chrome script to change offline status in the
     91  // parent process.
     92  var offlineChromeScript = SpecialPowers.loadChromeScript(_ => {
     93    /* eslint-env mozilla/chrome-script */
     94    addMessageListener("change-status", function(offline) {
     95      // eslint-disable-next-line mozilla/use-services
     96      const ioService = Cc["@mozilla.org/network/io-service;1"]
     97        .getService(Ci.nsIIOService);
     98      ioService.offline = offline;
     99    });
    100  });
    101 
    102  function offlineObserver(res) {
    103    this._res = res;
    104  }
    105  offlineObserver.prototype = {
    106    _res: null,
    107 
    108    observe(subject, topic, data) {
    109    debug("observe: " + subject + " " + topic + " " + data);
    110    if (topic === "network:offline-status-changed") {
    111      // eslint-disable-next-line mozilla/use-services
    112      const obsService = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
    113        .getService(SpecialPowers.Ci.nsIObserverService);
    114      obsService.removeObserver(this, topic);
    115      this._res(null);
    116    }
    117  },
    118  };
    119 
    120  function changeOfflineState(offline) {
    121    return new Promise(function(res) {
    122      // eslint-disable-next-line mozilla/use-services
    123      const obsService = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
    124        .getService(SpecialPowers.Ci.nsIObserverService);
    125      obsService.addObserver(SpecialPowers.wrapCallbackObject(new offlineObserver(res)),
    126                               "network:offline-status-changed");
    127      offlineChromeScript.sendAsyncMessage("change-status", offline);
    128    });
    129  }
    130 
    131  function changePushServerConnectionEnabled(enable) {
    132    debug("changePushServerConnectionEnabled");
    133    SpecialPowers.setBoolPref("dom.push.connection.enabled", enable);
    134  }
    135 
    136  function unsubscribe(sub) {
    137    return sub.unsubscribe()
    138      .then(_ => { ok(true, "Unsubscribed!"); });
    139  }
    140 
    141  // go offline then go online
    142  function runTest1() {
    143    return registerServiceWorker()
    144    .then(swr =>
    145      getEndpointExpectNull(swr)
    146        .then(_ => changeOfflineState(true))
    147        .then(_ => subscribeFail(swr))
    148        .then(_ => getEndpointExpectNull(swr))
    149        .then(_ => changeOfflineState(false))
    150        .then(_ => subscribe(swr))
    151        .then(sub => getEndpoint(swr, sub)
    152          .then(s => unsubscribe(s))
    153        )
    154        .then(_ => getEndpointExpectNull(swr))
    155        .then(_ => unregister(swr))
    156    )
    157    .catch(err => {
    158      ok(false, "Some test failed with error " + err);
    159    });
    160  }
    161 
    162  // disable - enable push connection.
    163  function runTest2() {
    164    return registerServiceWorker()
    165    .then(swr =>
    166      getEndpointExpectNull(swr)
    167        .then(_ => changePushServerConnectionEnabled(false))
    168        .then(_ => subscribeFail(swr))
    169        .then(_ => getEndpointExpectNull(swr))
    170        .then(_ => changePushServerConnectionEnabled(true))
    171        .then(_ => subscribe(swr))
    172        .then(sub => getEndpoint(swr, sub)
    173          .then(s => unsubscribe(s))
    174        )
    175        .then(_ => getEndpointExpectNull(swr))
    176        .then(_ => unregister(swr))
    177    )
    178    .catch(err => {
    179      ok(false, "Some test failed with error " + err);
    180    });
    181  }
    182 
    183  // go offline - disable - enable - go online
    184  function runTest3() {
    185    return registerServiceWorker()
    186    .then(swr =>
    187      getEndpointExpectNull(swr)
    188        .then(_ => changeOfflineState(true))
    189        .then(_ => subscribeFail(swr))
    190        .then(_ => getEndpointExpectNull(swr))
    191        .then(_ => changePushServerConnectionEnabled(false))
    192        .then(_ => subscribeFail(swr))
    193        .then(_ => getEndpointExpectNull(swr))
    194        .then(_ => changePushServerConnectionEnabled(true))
    195        .then(_ => subscribeFail(swr))
    196        .then(_ => getEndpointExpectNull(swr))
    197        .then(_ => changeOfflineState(false))
    198        .then(_ => subscribe(swr))
    199        .then(sub => getEndpoint(swr, sub)
    200          .then(s => unsubscribe(s))
    201        )
    202        .then(_ => getEndpointExpectNull(swr))
    203        .then(_ => unregister(swr))
    204    )
    205    .catch(err => {
    206      ok(false, "Some test failed with error " + err);
    207    });
    208  }
    209 
    210  // disable - offline - online - enable.
    211  function runTest4() {
    212    return registerServiceWorker()
    213    .then(swr =>
    214      getEndpointExpectNull(swr)
    215        .then(_ => changePushServerConnectionEnabled(false))
    216        .then(_ => subscribeFail(swr))
    217        .then(_ => getEndpointExpectNull(swr))
    218        .then(_ => changeOfflineState(true))
    219        .then(_ => subscribeFail(swr))
    220        .then(_ => getEndpointExpectNull(swr))
    221        .then(_ => changeOfflineState(false))
    222        .then(_ => subscribeFail(swr))
    223        .then(_ => getEndpointExpectNull(swr))
    224        .then(_ => changePushServerConnectionEnabled(true))
    225        .then(_ => subscribe(swr))
    226        .then(sub => getEndpoint(swr, sub)
    227          .then(s => unsubscribe(s))
    228        )
    229        .then(_ => getEndpointExpectNull(swr))
    230        .then(_ => unregister(swr))
    231    )
    232    .catch(err => {
    233      ok(false, "Some test failed with error " + err);
    234    });
    235  }
    236 
    237  // go offline - disable - go online - enable
    238  function runTest5() {
    239    return registerServiceWorker()
    240    .then(swr =>
    241      getEndpointExpectNull(swr)
    242        .then(_ => changeOfflineState(true))
    243        .then(_ => subscribeFail(swr))
    244        .then(_ => getEndpointExpectNull(swr))
    245        .then(_ => changePushServerConnectionEnabled(false))
    246        .then(_ => subscribeFail(swr))
    247        .then(_ => getEndpointExpectNull(swr))
    248        .then(_ => changeOfflineState(false))
    249        .then(_ => subscribeFail(swr))
    250        .then(_ => getEndpointExpectNull(swr))
    251        .then(_ => changePushServerConnectionEnabled(true))
    252        .then(_ => subscribe(swr))
    253        .then(sub => getEndpoint(swr, sub)
    254          .then(s => unsubscribe(s))
    255        )
    256        .then(_ => getEndpointExpectNull(swr))
    257        .then(_ => unregister(swr))
    258    )
    259    .catch(err => {
    260      ok(false, "Some test failed with error " + err);
    261    });
    262  }
    263 
    264  // disable - go offline - enable - go online.
    265  function runTest6() {
    266    return registerServiceWorker()
    267    .then(swr =>
    268      getEndpointExpectNull(swr)
    269        .then(_ => changePushServerConnectionEnabled(false))
    270        .then(_ => subscribeFail(swr))
    271        .then(_ => getEndpointExpectNull(swr))
    272        .then(_ => changeOfflineState(true))
    273        .then(_ => subscribeFail(swr))
    274        .then(_ => getEndpointExpectNull(swr))
    275        .then(_ => changePushServerConnectionEnabled(true))
    276        .then(_ => subscribeFail(swr))
    277        .then(_ => getEndpointExpectNull(swr))
    278        .then(_ => changeOfflineState(false))
    279        .then(_ => subscribe(swr))
    280        .then(sub => getEndpoint(swr, sub)
    281          .then(s => unsubscribe(s))
    282        )
    283        .then(_ => getEndpointExpectNull(swr))
    284        .then(_ => unregister(swr))
    285    )
    286    .catch(err => {
    287      ok(false, "Some test failed with error " + err);
    288    });
    289  }
    290 
    291  function runTest() {
    292    runTest1()
    293    .then(_ => runTest2())
    294    .then(_ => runTest3())
    295    .then(_ => runTest4())
    296    .then(_ => runTest5())
    297    .then(_ => runTest6())
    298    .then(SimpleTest.finish);
    299  }
    300 
    301  setupPrefsAndMockSocket(new MockWebSocket())
    302    .then(_ => setPushPermission(true))
    303    .then(_ => runTest());
    304  SimpleTest.waitForExplicitFinish();
    305 </script>
    306 </body>
    307 </html>