tor-browser

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

browser_favicon_load.js (5038B)


      1 /**
      2 * Bug 1247843 - A test case for testing whether the channel used to load favicon
      3 * has correct classFlags.
      4 * Note that this test is modified based on browser_favicon_userContextId.js.
      5 */
      6 
      7 const CC = Components.Constructor;
      8 
      9 // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     10 const TEST_SITE = "http://example.net";
     11 const TEST_THIRD_PARTY_SITE = "http://mochi.test:8888";
     12 
     13 const TEST_PAGE =
     14  TEST_SITE + "/browser/browser/base/content/test/favicons/file_favicon.html";
     15 const FAVICON_URI =
     16  TEST_SITE + "/browser/browser/base/content/test/favicons/file_favicon.png";
     17 const TEST_THIRD_PARTY_PAGE =
     18  TEST_SITE +
     19  "/browser/browser/base/content/test/favicons/file_favicon_thirdParty.html";
     20 const THIRD_PARTY_FAVICON_URI =
     21  TEST_THIRD_PARTY_SITE +
     22  "/browser/browser/base/content/test/favicons/file_favicon.png";
     23 
     24 ChromeUtils.defineESModuleGetters(this, {
     25  PlacesTestUtils: "resource://testing-common/PlacesTestUtils.sys.mjs",
     26 });
     27 
     28 let systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
     29 
     30 function clearAllImageCaches() {
     31  var tools = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools);
     32  var imageCache = tools.getImgCacheForDocument(window.document);
     33  imageCache.clearCache(true); // true=chrome
     34  imageCache.clearCache(false); // false=content
     35 }
     36 
     37 function clearAllPlacesFavicons() {
     38  return new Promise(resolve => {
     39    Services.obs.addObserver(function observer() {
     40      Services.obs.removeObserver(observer, "places-favicons-expired");
     41      resolve();
     42    }, "places-favicons-expired");
     43 
     44    PlacesUtils.favicons.expireAllFavicons();
     45  });
     46 }
     47 
     48 function FaviconObserver(aPageURI, aFaviconURL, aTailingEnabled) {
     49  this.reset(aPageURI, aFaviconURL, aTailingEnabled);
     50 }
     51 
     52 FaviconObserver.prototype = {
     53  observe(aSubject, aTopic) {
     54    // Make sure that the topic is 'http-on-modify-request'.
     55    if (aTopic === "http-on-modify-request") {
     56      let httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
     57      let reqLoadInfo = httpChannel.loadInfo;
     58      // Make sure this is a favicon request.
     59      if (httpChannel.URI.spec !== this._faviconURL) {
     60        return;
     61      }
     62 
     63      let cos = aSubject.QueryInterface(Ci.nsIClassOfService);
     64      if (!cos) {
     65        ok(false, "Http channel should implement nsIClassOfService.");
     66        return;
     67      }
     68 
     69      if (!reqLoadInfo) {
     70        ok(false, "Should have load info.");
     71        return;
     72      }
     73 
     74      let haveTailFlag = !!(cos.classFlags & Ci.nsIClassOfService.Tail);
     75      info("classFlags=" + cos.classFlags);
     76      is(haveTailFlag, this._tailingEnabled, "Should have correct cos flag.");
     77    } else {
     78      ok(false, "Received unexpected topic: ", aTopic);
     79    }
     80 
     81    this._faviconLoaded.resolve();
     82  },
     83 
     84  reset(aPageURI, aFaviconURL, aTailingEnabled) {
     85    this._faviconURL = aFaviconURL;
     86    this._faviconLoaded = Promise.withResolvers();
     87    this._tailingEnabled = aTailingEnabled;
     88  },
     89 
     90  get promise() {
     91    return this._faviconLoaded.promise;
     92  },
     93 };
     94 
     95 function waitOnFaviconLoaded(aFaviconURL) {
     96  return PlacesTestUtils.waitForNotification("favicon-changed", events =>
     97    events.some(e => e.faviconUrl == aFaviconURL)
     98  );
     99 }
    100 
    101 async function doTest(aTestPage, aFaviconURL, aTailingEnabled) {
    102  let pageURI = Services.io.newURI(aTestPage);
    103 
    104  // Create the observer object for observing favion channels.
    105  let observer = new FaviconObserver(pageURI, aFaviconURL, aTailingEnabled);
    106 
    107  let promiseWaitOnFaviconLoaded = waitOnFaviconLoaded(aFaviconURL);
    108 
    109  // Add the observer earlier in case we miss it.
    110  Services.obs.addObserver(observer, "http-on-modify-request");
    111 
    112  // Open the tab.
    113  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, aTestPage);
    114  // Waiting for favicon requests are all made.
    115  await observer.promise;
    116  // Waiting for favicon loaded.
    117  await promiseWaitOnFaviconLoaded;
    118 
    119  Services.obs.removeObserver(observer, "http-on-modify-request");
    120 
    121  // Close the tab.
    122  BrowserTestUtils.removeTab(tab);
    123 }
    124 
    125 async function setupTailingPreference(aTailingEnabled) {
    126  await SpecialPowers.pushPrefEnv({
    127    set: [["network.http.tailing.enabled", aTailingEnabled]],
    128  });
    129 }
    130 
    131 async function cleanup() {
    132  // Clear all cookies.
    133  Services.cookies.removeAll();
    134  // Clear cache.
    135  Services.cache2.clear();
    136  // Clear Places favicon caches.
    137  await clearAllPlacesFavicons();
    138  // Clear all image caches and network caches.
    139  clearAllImageCaches();
    140  // Clear Places history.
    141  await PlacesUtils.history.clear();
    142 }
    143 
    144 // A clean up function to prevent affecting other tests.
    145 registerCleanupFunction(async () => {
    146  await cleanup();
    147 });
    148 
    149 add_task(async function test_favicon_with_tailing_enabled() {
    150  await cleanup();
    151 
    152  let tailingEnabled = true;
    153 
    154  await setupTailingPreference(tailingEnabled);
    155 
    156  await doTest(TEST_PAGE, FAVICON_URI, tailingEnabled);
    157 });
    158 
    159 add_task(async function test_favicon_with_tailing_disabled() {
    160  await cleanup();
    161 
    162  let tailingEnabled = false;
    163 
    164  await setupTailingPreference(tailingEnabled);
    165 
    166  await doTest(TEST_THIRD_PARTY_PAGE, THIRD_PARTY_FAVICON_URI, tailingEnabled);
    167 });