tor-browser

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

browser_103_assets.js (6907B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // On debug osx test machine, verify chaos mode takes slightly too long
      6 requestLongerTimeout(2);
      7 
      8 Services.prefs.setBoolPref("network.early-hints.enabled", true);
      9 
     10 const { request_count_checking } = ChromeUtils.importESModule(
     11  "resource://testing-common/early_hint_preload_test_helper.sys.mjs"
     12 );
     13 
     14 // - testName is just there to be printed during Asserts when failing
     15 // - asset is the asset type, see early_hint_asset_html.sjs for possible values
     16 //   for the asset type fetch see test_hint_fetch due to timing issues
     17 // - variant:
     18 //   - "normal": no early hints, expects one normal request expected
     19 //   - "hinted": early hints sent, expects one hinted request
     20 //   - "reload": early hints sent, resources non-cacheable, two early-hint requests expected
     21 //   - "cached": same as reload, but resources are cacheable, so only one hinted network request expected
     22 async function test_hint_asset(testName, asset, variant) {
     23  // reset the count
     24  let headers = new Headers();
     25  headers.append("X-Early-Hint-Count-Start", "");
     26  await fetch(
     27    "http://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs",
     28    { headers }
     29  );
     30 
     31  let requestUrl = `https://example.com/browser/netwerk/test/browser/early_hint_asset_html.sjs?as=${asset}&hinted=${
     32    variant !== "normal" ? "1" : "0"
     33  }&cached=${variant === "cached" ? "1" : "0"}`;
     34 
     35  let numConnectBackRemaining = 0;
     36  if (variant === "hinted") {
     37    numConnectBackRemaining = 1;
     38  } else if (variant === "reload" || variant === "cached") {
     39    numConnectBackRemaining = 2;
     40  }
     41 
     42  let observer = {
     43    QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
     44    observe(aSubject, aTopic) {
     45      if (aTopic == "earlyhints-connectback") {
     46        numConnectBackRemaining -= 1;
     47      }
     48    },
     49  };
     50  Services.obs.addObserver(observer, "earlyhints-connectback");
     51 
     52  await BrowserTestUtils.withNewTab(
     53    {
     54      gBrowser,
     55      url: requestUrl,
     56      waitForLoad: true,
     57    },
     58    async function (browser) {
     59      if (asset === "fetch") {
     60        // wait until the fetch is complete
     61        await TestUtils.waitForCondition(_ => {
     62          return SpecialPowers.spawn(browser, [], _ => {
     63            return (
     64              content.document.getElementsByTagName("h2")[0] != undefined &&
     65              content.document.getElementsByTagName("h2")[0].textContent !==
     66                "Fetching..." // default text set by early_hint_asset_html.sjs
     67            );
     68          });
     69        });
     70      }
     71 
     72      // reload
     73      if (variant === "reload" || variant === "cached") {
     74        await BrowserTestUtils.reloadTab(gBrowser.selectedTab);
     75      }
     76 
     77      if (asset === "fetch") {
     78        // wait until the fetch is complete
     79        await TestUtils.waitForCondition(_ => {
     80          return SpecialPowers.spawn(browser, [], _ => {
     81            return (
     82              content.document.getElementsByTagName("h2")[0] != undefined &&
     83              content.document.getElementsByTagName("h2")[0].textContent !==
     84                "Fetching..." // default text set by early_hint_asset_html.sjs
     85            );
     86          });
     87        });
     88      }
     89    }
     90  );
     91  Services.obs.removeObserver(observer, "earlyhints-connectback");
     92 
     93  let gotRequestCount = await fetch(
     94    "http://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs"
     95  ).then(response => response.json());
     96  if (
     97    asset === "script" &&
     98    variant === "cached" &&
     99    numConnectBackRemaining === 1
    100  ) {
    101    // If the navigation cache is enabled
    102    // (dom.script_loader.experimental.navigation_cache),
    103    // the script can be cached in the per-process cache storage, and in that
    104    // case the channel isn't opened, and the "earlyhints-connectback"
    105    // notification isn't observed.
    106    info(
    107      `${testName} (${asset}+${variant}) in-memory-cached script's notification is skipped`
    108    );
    109  } else {
    110    Assert.equal(
    111      numConnectBackRemaining,
    112      0,
    113      `${testName} (${asset}+${variant}) no remaining connect back expected`
    114    );
    115  }
    116 
    117  let expectedRequestCount;
    118  if (variant === "normal") {
    119    expectedRequestCount = { hinted: 0, normal: 1 };
    120  } else if (variant === "hinted") {
    121    expectedRequestCount = { hinted: 1, normal: 0 };
    122  } else if (variant === "reload") {
    123    expectedRequestCount = { hinted: 2, normal: 0 };
    124  } else if (variant === "cached") {
    125    expectedRequestCount = { hinted: 1, normal: 0 };
    126  }
    127 
    128  await request_count_checking(
    129    `${testName} (${asset}+${variant})`,
    130    gotRequestCount,
    131    expectedRequestCount
    132  );
    133  if (variant === "cached") {
    134    Services.cache2.clear();
    135  }
    136 }
    137 
    138 // preload image
    139 add_task(async function test_103_asset_image() {
    140  await test_hint_asset("test_103_asset_normal", "image", "normal");
    141  await test_hint_asset("test_103_asset_hinted", "image", "hinted");
    142  await test_hint_asset("test_103_asset_reload", "image", "reload");
    143  // TODO(Bug 1815884): await test_hint_asset("test_103_asset_cached", "image", "cached");
    144 });
    145 
    146 // preload css
    147 add_task(async function test_103_asset_style() {
    148  await test_hint_asset("test_103_asset_normal", "style", "normal");
    149  await test_hint_asset("test_103_asset_hinted", "style", "hinted");
    150  await test_hint_asset("test_103_asset_reload", "style", "reload");
    151  // TODO(Bug 1815884): await test_hint_asset("test_103_asset_cached", "style", "cached");
    152 });
    153 
    154 // preload javascript
    155 add_task(async function test_103_asset_javascript() {
    156  await test_hint_asset("test_103_asset_normal", "script", "normal");
    157  await test_hint_asset("test_103_asset_hinted", "script", "hinted");
    158  await test_hint_asset("test_103_asset_reload", "script", "reload");
    159  await test_hint_asset("test_103_asset_cached", "script", "cached");
    160 });
    161 
    162 // preload javascript module
    163 add_task(async function test_103_asset_module() {
    164  await test_hint_asset("test_103_asset_normal", "module", "normal");
    165  await test_hint_asset("test_103_asset_hinted", "module", "hinted");
    166  await test_hint_asset("test_103_asset_reload", "module", "reload");
    167  await test_hint_asset("test_103_asset_cached", "module", "cached");
    168 });
    169 
    170 // preload font
    171 add_task(async function test_103_asset_font() {
    172  await test_hint_asset("test_103_asset_normal", "font", "normal");
    173  await test_hint_asset("test_103_asset_hinted", "font", "hinted");
    174  await test_hint_asset("test_103_asset_reload", "font", "reload");
    175  await test_hint_asset("test_103_asset_cached", "font", "cached");
    176 });
    177 
    178 // preload fetch
    179 add_task(async function test_103_asset_fetch() {
    180  await test_hint_asset("test_103_asset_normal", "fetch", "normal");
    181  await test_hint_asset("test_103_asset_hinted", "fetch", "hinted");
    182  await test_hint_asset("test_103_asset_reload", "fetch", "reload");
    183  await test_hint_asset("test_103_asset_cached", "fetch", "cached");
    184 });