tor-browser

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

send-on-deactivate-with-background-sync.https.window.js (5088B)


      1 // META: script=/resources/testdriver.js
      2 // META: script=/resources/testdriver-vendor.js
      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=/html/browsers/browsing-the-web/back-forward-cache/resources/rc-helper.js
      8 // META: script=/fetch/fetch-later/resources/fetch-later-helper.js
      9 // META: timeout=long
     10 
     11 'use strict';
     12 
     13 async function setBackgroundSyncEnabled(enabled) {
     14  const status = enabled ? 'granted' : 'denied';
     15  await test_driver.set_permission({name: 'background-sync'}, status);
     16 }
     17 
     18 parallelPromiseTest(async t => {
     19  // Enables BackgroundSync permission such that deferred request won't be
     20  // immediately sent out on entering BFCache.
     21  await setBackgroundSyncEnabled(true);
     22 
     23  const uuid = token();
     24  const url = generateSetBeaconURL(uuid);
     25  // Sets no option to test the default behavior when a document enters BFCache.
     26  const helper = new RemoteContextHelper();
     27  // Opens a window with noopener so that BFCache will work.
     28  const rc1 = await helper.addWindow(
     29      /*config=*/ null, /*options=*/ {features: 'noopener'});
     30 
     31  // Creates a fetchLater request with default config in remote, which should
     32  // only be sent on page discarded (not on entering BFCache).
     33  await rc1.executeScript(url => {
     34    fetchLater(url);
     35    // Add a pageshow listener to stash the BFCache event.
     36    window.addEventListener('pageshow', e => {
     37      window.pageshowEvent = e;
     38    });
     39  }, [url]);
     40  // Navigates away to let page enter BFCache.
     41  const rc2 = await rc1.navigateToNew();
     42  // Navigates back.
     43  await rc2.historyBack();
     44  // Verifies the page was BFCached.
     45  assert_true(await rc1.executeScript(() => {
     46    return window.pageshowEvent.persisted;
     47  }));
     48 
     49  // By default, pending requests are all flushed on BFCache no matter
     50  // BackgroundSync is on or not. See http://b/310541607#comment28.
     51  await expectBeacon(uuid, {count: 1});
     52 }, `fetchLater() does send on page entering BFCache even if BackgroundSync is on.`);
     53 
     54 parallelPromiseTest(async t => {
     55  // Enables BackgroundSync permission such that deferred request won't be
     56  // immediately sent out on entering BFCache.
     57  await setBackgroundSyncEnabled(true);
     58 
     59  const uuid = token();
     60  const url = generateSetBeaconURL(uuid);
     61  // activateAfter = 0s means the request should be sent out right on
     62  // document becoming deactivated (BFCached or frozen) after navigating away.
     63  const options = {activateAfter: 0};
     64  const helper = new RemoteContextHelper();
     65  // Opens a window with noopener so that BFCache will work.
     66  const rc1 = await helper.addWindow(
     67      /*config=*/ null, /*options=*/ {features: 'noopener'});
     68 
     69  // Creates a fetchLater request in remote which should only be sent on
     70  // navigating away.
     71  await rc1.executeScript((url, options) => {
     72    fetchLater(url, options);
     73 
     74    // Add a pageshow listener to stash the BFCache event.
     75    window.addEventListener('pageshow', e => {
     76      window.pageshowEvent = e;
     77    });
     78  }, [url, options]);
     79  // Navigates away to trigger request sending.
     80  const rc2 = await rc1.navigateToNew();
     81  // Navigates back.
     82  await rc2.historyBack();
     83  // Verifies the page was BFCached.
     84  assert_true(await rc1.executeScript(() => {
     85    return window.pageshowEvent.persisted;
     86  }));
     87 
     88  await expectBeacon(uuid, {count: 1});
     89 }, `fetchLater() with activateAfter=0 sends on page entering BFCache if BackgroundSync is on.`);
     90 
     91 parallelPromiseTest(async t => {
     92  // Enables BackgroundSync permission such that deferred request won't be
     93  // immediately sent out on entering BFCache.
     94  await setBackgroundSyncEnabled(true);
     95 
     96  const uuid = token();
     97  const url = generateSetBeaconURL(uuid);
     98  // activateAfter = 1m means the request should NOT be sent out on
     99  // document becoming deactivated (BFCached or frozen) until after 1 minute.
    100  const options = {activateAfter: 60000};
    101  const helper = new RemoteContextHelper();
    102  // Opens a window with noopener so that BFCache will work.
    103  const rc1 = await helper.addWindow(
    104      /*config=*/ null, /*options=*/ {features: 'noopener'});
    105 
    106  // Creates a fetchLater request in remote which should only be sent on
    107  // navigating away.
    108  await rc1.executeScript((url, options) => {
    109    fetchLater(url, options);
    110 
    111    // Adds a pageshow listener to stash the BFCache event.
    112    window.addEventListener('pageshow', e => {
    113      window.pageshowEvent = e;
    114    });
    115  }, [url, options]);
    116  // Navigates away to trigger request sending.
    117  const rc2 = await rc1.navigateToNew();
    118  // Navigates back.
    119  await rc2.historyBack();
    120  // Verifies the page was BFCached.
    121  assert_true(await rc1.executeScript(() => {
    122    return window.pageshowEvent.persisted;
    123  }));
    124 
    125  // By default, pending requests are all flushed on BFCache no matter
    126  // BackgroundSync is on or not. See http://b/310541607#comment28.
    127  await expectBeacon(uuid, {count: 1});
    128 }, `fetchLater() with activateAfter=1m does send on page entering BFCache even if BackgroundSync is on.`);