tor-browser

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

browser_startup_images.js (4063B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * This test checks that any images we load on startup are actually used,
      8 * so we don't waste IO and cycles loading images the user doesn't see.
      9 * It has a list of known problematic images that we aim to reduce to
     10 * empty.
     11 */
     12 
     13 /* A list of images that are loaded at startup but not shown.
     14 * List items support the following attributes:
     15 *  - file: The location of the loaded image file.
     16 *  - hidpi: An alternative hidpi file location for retina screens, if one exists.
     17 *           May be the magic string <not loaded> in strange cases where
     18 *           only the low-resolution image is loaded but not shown.
     19 *  - platforms: An array of the platforms where the issue is occurring.
     20 *               Possible values are linux, win, macosx.
     21 *  - intermittentNotLoaded: an array of platforms where this image is
     22 *                           intermittently not loaded, e.g. because it is
     23 *                           loaded during the time we stop recording.
     24 *  - intermittentShown: An array of platforms where this image is
     25 *                       intermittently shown, even though the list implies
     26 *                       it might not be shown.
     27 *
     28 * PLEASE do not add items to this list.
     29 *
     30 * PLEASE DO remove items from this list.
     31 */
     32 const knownUnshownImages = [
     33  {
     34    file: "chrome://global/skin/icons/arrow-left.svg",
     35    platforms: ["linux", "win", "macosx"],
     36  },
     37 
     38  {
     39    file: "chrome://browser/skin/toolbar-drag-indicator.svg",
     40    platforms: ["linux", "win", "macosx"],
     41  },
     42 
     43  {
     44    file: "chrome://global/skin/icons/chevron.svg",
     45    platforms: ["win", "linux", "macosx"],
     46    intermittentShown: ["win", "linux"],
     47  },
     48 
     49  {
     50    file: "chrome://global/skin/icons/highlights.svg",
     51    platforms: ["win", "linux", "macosx"],
     52    intermittentShown: ["win", "linux"],
     53  },
     54 ];
     55 
     56 add_task(async function () {
     57  if (!AppConstants.DEBUG) {
     58    ok(false, "You need to run this test on a debug build.");
     59  }
     60 
     61  let startupRecorder =
     62    Cc["@mozilla.org/test/startuprecorder;1"].getService().wrappedJSObject;
     63  await startupRecorder.done;
     64 
     65  let data = Cu.cloneInto(startupRecorder.data.images, {});
     66  let knownImagesForPlatform = knownUnshownImages.filter(el => {
     67    return el.platforms.includes(AppConstants.platform);
     68  });
     69 
     70  {
     71    let results = await PerfTestHelpers.throttledMapPromises(
     72      knownImagesForPlatform,
     73      async image => ({
     74        uri: image.file,
     75        exists: await PerfTestHelpers.checkURIExists(image.file),
     76      })
     77    );
     78    for (let { uri, exists } of results) {
     79      ok(exists, `Unshown image entry ${uri} must exist`);
     80    }
     81  }
     82 
     83  let loadedImages = data["image-loading"];
     84  let shownImages = data["image-drawing"];
     85 
     86  for (let loaded of loadedImages.values()) {
     87    let knownImage = knownImagesForPlatform.find(el => {
     88      if (window.devicePixelRatio >= 2 && el.hidpi && el.hidpi == loaded) {
     89        return true;
     90      }
     91      return el.file == loaded;
     92    });
     93    if (knownImage) {
     94      if (
     95        !knownImage.intermittentShown ||
     96        !knownImage.intermittentShown.includes(AppConstants.platform)
     97      ) {
     98        todo(
     99          shownImages.has(loaded),
    100          `Image ${loaded} should not have been shown.`
    101        );
    102      }
    103      continue;
    104    }
    105    ok(
    106      shownImages.has(loaded),
    107      `Loaded image ${loaded} should have been shown.`
    108    );
    109  }
    110 
    111  // Check for known images that are no longer used.
    112  for (let item of knownImagesForPlatform) {
    113    if (
    114      !item.intermittentNotLoaded ||
    115      !item.intermittentNotLoaded.includes(AppConstants.platform)
    116    ) {
    117      if (window.devicePixelRatio >= 2 && item.hidpi) {
    118        if (item.hidpi != "<not loaded>") {
    119          ok(
    120            loadedImages.has(item.hidpi),
    121            `Image ${item.hidpi} should have been loaded.`
    122          );
    123        }
    124      } else {
    125        ok(
    126          loadedImages.has(item.file),
    127          `Image ${item.file} should have been loaded.`
    128        );
    129      }
    130    }
    131  }
    132 });