tor-browser

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

browser_native_font_cache_macos.js (4570B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 add_setup(async function () {
      7  await SpecialPowers.pushPrefEnv({
      8    set: [["test.wait300msAfterTabSwitch", true]],
      9  });
     10 });
     11 
     12 add_task(async () => {
     13  // Create a tab that loads a system font.
     14  const CROSS_ORIGIN_DOMAIN = "https://example.com";
     15  const TARGET_URL = `${CROSS_ORIGIN_DOMAIN}/browser/gfx/tests/browser/file_native_font_cache_macos.html`;
     16  await BrowserTestUtils.withNewTab(
     17    { gBrowser, url: TARGET_URL },
     18    async browser => {
     19      await SpecialPowers.spawn(browser, [], async () => {
     20        // Capture a snapshot of the tab, which will load the system font in the
     21        // parent process.
     22        const TARGET_WIDTH = 200;
     23        const TARGET_HEIGHT = 100;
     24 
     25        const rect = new content.window.DOMRect(
     26          0,
     27          0,
     28          TARGET_WIDTH,
     29          TARGET_HEIGHT
     30        );
     31        await SpecialPowers.snapshotContext(content.window, rect, "white");
     32      });
     33    }
     34  );
     35 
     36  // Now create a tab that shows the memory reporter.
     37  await BrowserTestUtils.withNewTab(
     38    { gBrowser, url: "about:memory" },
     39    async browser => {
     40      // Click the "Measure" button.
     41      await SpecialPowers.spawn(browser, [], () => {
     42        let measureButton = content.document.getElementById("measureButton");
     43        measureButton.click();
     44      });
     45 
     46      // Copy the page text and check for an expected start with string.
     47      let copiedText = await new Promise(resolve => {
     48        const REPORT_TIMEOUT_MS = 15 * 1e3;
     49        const EXPECTED_START_WITH = "Total resident memory (approximate)";
     50        let mostRecentTextOnClipboard = "";
     51 
     52        SimpleTest.waitForClipboard(
     53          textOnClipboard => {
     54            mostRecentTextOnClipboard = textOnClipboard;
     55            const gotExpected = textOnClipboard.startsWith(EXPECTED_START_WITH);
     56            if (!gotExpected) {
     57              // Try copying again.
     58              EventUtils.synthesizeKey("A", { accelKey: true });
     59              EventUtils.synthesizeKey("C", { accelKey: true });
     60            }
     61            return gotExpected;
     62          },
     63          () => {
     64            EventUtils.synthesizeKey("A", { accelKey: true });
     65            EventUtils.synthesizeKey("C", { accelKey: true });
     66          },
     67          () => {
     68            resolve(mostRecentTextOnClipboard);
     69          },
     70          () => {
     71            info(`Didn't find expected text within ${REPORT_TIMEOUT_MS}ms.`);
     72            dump("*******ACTUAL*******\n");
     73            dump("<<<" + mostRecentTextOnClipboard + ">>>\n");
     74            dump("********************\n");
     75            resolve("");
     76          },
     77          "text/plain",
     78          REPORT_TIMEOUT_MS
     79        );
     80      });
     81 
     82      isnot(copiedText, "", "Got some text from clipboard.");
     83 
     84      // Search the copied text for our desired pattern. Initially, check for
     85      // a line with "native-font-resource-mac". If that exists, ensure that it
     86      // has less than a maximum MB. If that doesn't exist, check instead for
     87      // a line with "gfx" before the "Other Measurements" section. If that
     88      // exists, it is tested against the same MB limit. If it doesn't exist,
     89      // that is an indication that "gfx" doesn't occur in the first section
     90      // "Explicit Allocations', and therefore isn't holding memory at all.
     91      const MB_EXCLUSIVE_MAX = 20;
     92      const nfrm_line = /^.*?(\d+)\.\d+ MB.*-- native-font-resource-mac/m;
     93      const nfrm_match = nfrm_line.exec(copiedText);
     94      if (nfrm_match) {
     95        const nfrm_mb = nfrm_match[1];
     96        Assert.less(
     97          nfrm_mb,
     98          MB_EXCLUSIVE_MAX,
     99          `native-font-resource-mac ${nfrm_mb} MB should be less than ${MB_EXCLUSIVE_MAX} MB.`
    100        );
    101      } else {
    102        // Figure out where the "Other Measurements" section begins.
    103        const om_line = /^Other Measurements$/m;
    104        const om_match = om_line.exec(copiedText);
    105 
    106        // Find the first gfx line, and if it occurs before the "Other
    107        // Measurements" section, check its size.
    108        const gfx_line = /^.*?(\d+)\.\d+ MB.*-- gfx/m;
    109        const gfx_match = gfx_line.exec(copiedText);
    110        if (gfx_match && gfx_match.index < om_match.index) {
    111          const gfx_mb = gfx_match[1];
    112          Assert.less(
    113            gfx_mb,
    114            MB_EXCLUSIVE_MAX,
    115            `Explicit Allocations gfx ${gfx_mb} MB should be less than ${MB_EXCLUSIVE_MAX} MB.`
    116          );
    117        } else {
    118          ok(true, "Explicit Allocations gfx is not listed.");
    119        }
    120      }
    121    }
    122  );
    123 });