tor-browser

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

test_privateBrowsing.html (3425B)


      1 <html xmlns="http://www.w3.org/1999/xhtml">
      2 <head>
      3  <title>Test for ServiceWorker - Private Browsing</title>
      4  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
      5  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
      6 </head>
      7 <body>
      8 
      9 <script type="application/javascript">
     10 const {BrowserTestUtils} = ChromeUtils.importESModule(
     11  "resource://testing-common/BrowserTestUtils.sys.mjs"
     12 );
     13 
     14 var mainWindow;
     15 
     16 var contentPage = "http://mochi.test:8888/chrome/dom/workers/test/empty.html";
     17 var workerScope = "http://mochi.test:8888/chrome/dom/serviceworkers/test/";
     18 var workerURL = workerScope + "worker.js";
     19 
     20 function testOnWindow(aIsPrivate, aCallback) {
     21  var win = mainWindow.OpenBrowserWindow({private: aIsPrivate});
     22  win.addEventListener("load", function() {
     23    win.addEventListener("DOMContentLoaded", function onInnerLoad() {
     24      if (win.content.location.href != contentPage) {
     25        BrowserTestUtils.startLoadingURIString(win.gBrowser, contentPage);
     26        return;
     27      }
     28 
     29      win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
     30      SimpleTest.executeSoon(function() { aCallback(win); });
     31    }, true);
     32  }, {capture: true, once: true});
     33 }
     34 
     35 function setupWindow() {
     36  mainWindow = window.browsingContext.topChromeWindow;
     37  runTest();
     38 }
     39 
     40 var wN;
     41 var registration;
     42 var wP;
     43 
     44 function testPrivateWindow() {
     45  testOnWindow(true, function(aWin) {
     46    wP = aWin;
     47    ok(!wP.content.eval('"serviceWorker" in navigator'), "ServiceWorkers are not available for private windows");
     48    runTest();
     49  });
     50 }
     51 
     52 function doTests() {
     53  testOnWindow(false, function(aWin) {
     54    wN = aWin;
     55    ok("serviceWorker" in wN.content.navigator, "ServiceWorkers are available for normal windows");
     56 
     57    wN.content.navigator.serviceWorker.register(workerURL,
     58                                                { scope: workerScope })
     59      .then(function(aRegistration) {
     60        registration = aRegistration;
     61        ok(registration, "Registering a service worker in a normal window should succeed");
     62 
     63        // Bug 1255621: We should be able to load a controlled document in a private window.
     64        testPrivateWindow();
     65      }, function(aError) {
     66        ok(false, "Error registering worker in normal window: " + aError);
     67        testPrivateWindow();
     68      });
     69  });
     70 }
     71 
     72 var steps = [
     73  setupWindow,
     74  doTests
     75 ];
     76 
     77 function cleanup() {
     78  wN.close();
     79  wP.close();
     80 
     81  SimpleTest.finish();
     82 }
     83 
     84 function runTest() {
     85  if (!steps.length) {
     86    registration.unregister().then(cleanup, cleanup);
     87 
     88    return;
     89  }
     90 
     91  var step = steps.shift();
     92  step();
     93 }
     94 
     95 SimpleTest.waitForExplicitFinish();
     96 SpecialPowers.pushPrefEnv({"set": [
     97  // The anti-tracking test browser_partitionedServiceWorkers.js now covers the
     98  // matrix of policy decisions around exposure of ServiceWorkers to PBM.  This
     99  // file is now a legacy crash-test from bug 1255621 that we won't crash if we
    100  // try and navigate to an origin with a SW registered in non-PBM in PBM where
    101  // ServiceWorkers are disabled, so we force the SW PBM setting to false.
    102  //
    103  // This file can be removed when we remove that pref.
    104  ["dom.serviceWorkers.privateBrowsing.enabled", false],
    105  ["dom.serviceWorkers.enabled", true],
    106  ["dom.serviceWorkers.testing.enabled", true],
    107  ["browser.startup.page", 0],
    108  ["browser.startup.homepage_override.mstone", "ignore"],
    109 ]}, runTest);
    110 
    111 </script>
    112 </body>
    113 </html>