tor-browser

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

entangled-after-back-forward-cache-restore.https.tentative.window.js (2626B)


      1 // META: timeout=long
      2 // META: title=Confirm close event is not fired when the page enters BFCache and MessagePort still works after the page is restored.
      3 // META: script=/common/dispatcher/dispatcher.js
      4 // META: script=/common/get-host-info.sub.js
      5 // META: script=/common/utils.js
      6 // META: script=/html/browsers/browsing-the-web/remote-context-helper/resources/remote-context-helper.js
      7 // META: script=/service-workers/service-worker/resources/test-helpers.sub.js
      8 // META: script=/html/browsers/browsing-the-web/back-forward-cache/resources/rc-helper.js
      9 
     10 promise_test(async t => {
     11  // Register a service worker.
     12  const scope =
     13      '/html/browsers/browsing-the-web/remote-context-helper/resources'
     14  const workerUrl =
     15      `/html/browsers/browsing-the-web/back-forward-cache/resources/` +
     16      `service-worker.js?pipe=header(Service-Worker-Allowed,${scope})`;
     17  const registration =
     18      await service_worker_unregister_and_register(t, workerUrl, scope);
     19  t.add_cleanup(_ => registration.unregister());
     20  await wait_for_state(t, registration.installing, 'activated');
     21 
     22  // Open a window with noopener so that BFCache will work.
     23  const rcHelper = new RemoteContextHelper();
     24  const rc1 = await rcHelper.addWindow(
     25      /*extraConfig=*/ null, /*options=*/ {features: 'noopener'});
     26 
     27  // Confirm the page is controlled.
     28  assert_true(
     29      await rc1.executeScript(
     30          () => (navigator.serviceWorker.controller !== null)),
     31      'The page should be controlled before navigation');
     32 
     33  // Send MessagePort to the service worker.
     34  await rc1.executeScript(() => {
     35    const {port1, port2} = new MessageChannel();
     36    port1.start();
     37    const ctrl = navigator.serviceWorker.controller;
     38    ctrl.postMessage({type: 'storeMessagePort'}, [port2]);
     39    self.waitForMessage = (sentMessage) => {
     40      return new Promise(resolve => {
     41        port1.addEventListener('message', (event) => {
     42          resolve(event.data);
     43        });
     44        port1.postMessage(sentMessage);
     45      });
     46    };
     47  });
     48 
     49  // Verify that the page was BFCached.
     50  await assertBFCacheEligibility(rc1, /*shouldRestoreFromBFCache=*/ true);
     51 
     52  // Confirm MessagePort can still work after the page is restored from
     53  // BFCache.
     54  assert_equals(
     55      await rc1.executeScript(
     56          async () =>
     57              await self.waitForMessage('Confirm the ports can communicate')),
     58      'Receive message');
     59 
     60  // Confirm the close event was not fired.
     61  assert_false(await rc1.executeScript(
     62      async () =>
     63          await self.waitForMessage('Ask if the close event was fired')));
     64 }, 'MessagePort still works after the page is restored from BFCache');