tor-browser

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

test_dont_use_document_fonts.html (4620B)


      1 <!doctype html>
      2 <title>Test for preference to not use document fonts</title>
      3 <script src="/resources/testharness.js"></script>
      4 <script src="/resources/testharnessreport.js"></script>
      5 <link rel='stylesheet' href='/resources/testharness.css'>
      6 <div id="content"></div>
      7 <script>
      8 const content = document.getElementById("content");
      9 
     10 // This is just a subset of browser.display.use_document_fonts.icon_font_allowlist
     11 // that we feel are worth double-checking via this test.  In particular:
     12 // * Chromium Bug Tracker and https://developers.google.com use "Material Icons"
     13 // * Google Translate and Google Timeline use "Material Icons Extended"
     14 // * https://fonts.google.com/icons uses "Material Symbols Outlined"
     15 // * Google Calendar and Google Contacts use "Google Material Icons"
     16 const kKnownLigatureIconFonts = "Material Icons, Material Icons Extended, " +
     17      "Material Symbols Outlined, Google Material Icons";
     18 
     19 setup({explicit_done: true })
     20 
     21 content.style.fontFamily = "initial";
     22 const kInitialFamily = getComputedStyle(content).fontFamily;
     23 content.style.fontFamily = "";
     24 
     25 const kTests = [
     26  {
     27    specified: "monospace",
     28    computed: "monospace",
     29    description: "Single generic family should not be changed",
     30  },
     31  {
     32    specified: "monospace, sans-serif",
     33    computed: "monospace, sans-serif",
     34    description: "Generic families should not be changed",
     35  },
     36  {
     37    specified: "Courier, monospace",
     38    computed: "monospace, Courier",
     39    description: "Generics are preferred, but may still fall back to document fonts",
     40  },
     41  {
     42    specified: "system-ui, sans-serif",
     43    computed: "sans-serif, system-ui",
     44    description: "system-ui is not prioritized",
     45  },
     46  {
     47    specified: "Courier, something-else",
     48    computed: `${kInitialFamily}, Courier, something-else`,
     49    description: "Generic is prepended to the font-family if none is found",
     50  },
     51  {
     52    specified: kKnownLigatureIconFonts + ", something-else, sans-serif",
     53    computed: kKnownLigatureIconFonts + ", sans-serif, something-else",
     54    description: "Known ligature-icon fonts remain ahead of the generic",
     55  },
     56  {
     57    specified: "Material Icons, something-else, Material Symbols Outlined, sans-serif",
     58    computed: "Material Icons, sans-serif, something-else, Material Symbols Outlined",
     59    description: "Generic is moved ahead of the first non-allowlisted font",
     60  },
     61  {
     62    specified: "Material Icons, something-else, Material Symbols Outlined",
     63    computed: `Material Icons, ${kInitialFamily}, something-else, Material Symbols Outlined`,
     64    description: "Default generic is inserted ahead of the first non-allowlisted font",
     65  },
     66  {
     67    specified: "Material Icons, cursive, Material Symbols Outlined, serif",
     68    computed: "Material Icons, serif, cursive, Material Symbols Outlined",
     69    description: "cursive is not treated as a generic to be prioritized",
     70  },
     71  {
     72    specified: "Material Icons, fantasy, Material Symbols Outlined",
     73    computed: `Material Icons, ${kInitialFamily}, fantasy, Material Symbols Outlined`,
     74    description: "fantasy is not treated as a generic to be prioritized",
     75  },
     76 ];
     77 
     78 let systemFont;
     79 
     80 // compute expectations while the pref is not active yet.
     81 test(function() {
     82  for (const test of kTests) {
     83    content.style.fontFamily = "";
     84    content.style.fontFamily = test.computed;
     85    assert_not_equals(content.style.fontFamily, "", `computed font ${test.computed} was invalid`);
     86    test.expected = getComputedStyle(content).fontFamily;
     87  }
     88 
     89  content.style.font = "menu";
     90  systemFont = getComputedStyle(content).fontFamily;
     91  assert_not_equals(systemFont, "", `computed menu system font was invalid`);
     92 
     93  content.style.font = "";
     94 }, "Sanity");
     95 
     96 function runTest({ specified, computed, description, expected }) {
     97  promise_test(async function() {
     98    content.style.fontFamily = "";
     99    content.style.fontFamily = specified;
    100    const before = getComputedStyle(content).fontFamily;
    101    await SpecialPowers.pushPrefEnv({'set': [['browser.display.use_document_fonts', 0]]});
    102    const after = getComputedStyle(content).fontFamily;
    103    if (specified != computed) {
    104      assert_not_equals(before, after);
    105    }
    106    assert_equals(after, expected);
    107    await SpecialPowers.popPrefEnv();
    108  }, description);
    109 }
    110 
    111 (async function() {
    112  for (const test of kTests)
    113    runTest(test);
    114  promise_test(async function() {
    115    await SpecialPowers.pushPrefEnv({'set': [['browser.display.use_document_fonts', 0]]});
    116    content.style.font = "menu";
    117    assert_equals(getComputedStyle(content).fontFamily, systemFont);
    118    await SpecialPowers.popPrefEnv();
    119  }, "System font should be honored");
    120 
    121  done();
    122 })();
    123 </script>