tor-browser

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

browser_page_style_menu.js (5455B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 function fillPopupAndGetItems() {
      7  let menupopup = document.getElementById("pageStyleMenu").menupopup;
      8  gPageStyleMenu.fillPopup(menupopup);
      9  return Array.from(menupopup.querySelectorAll("menuseparator ~ menuitem"));
     10 }
     11 
     12 function getRootColor() {
     13  return SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
     14    return content.document.defaultView.getComputedStyle(
     15      content.document.documentElement
     16    ).color;
     17  });
     18 }
     19 
     20 const RED = "rgb(255, 0, 0)";
     21 const LIME = "rgb(0, 255, 0)";
     22 const BLUE = "rgb(0, 0, 255)";
     23 
     24 const kStyleSheetsInPageStyleSample = 18;
     25 
     26 /*
     27 * Test that the right stylesheets do (and others don't) show up
     28 * in the page style menu.
     29 */
     30 add_task(async function test_menu() {
     31  let tab = await BrowserTestUtils.openNewForegroundTab(
     32    gBrowser,
     33    "about:blank",
     34    false
     35  );
     36  let browser = tab.linkedBrowser;
     37  BrowserTestUtils.startLoadingURIString(
     38    browser,
     39    WEB_ROOT + "page_style_sample.html"
     40  );
     41  await promiseStylesheetsLoaded(browser, kStyleSheetsInPageStyleSample);
     42 
     43  let menuitems = fillPopupAndGetItems();
     44  let items = menuitems.map(el => ({
     45    label: el.getAttribute("label"),
     46    checked: el.hasAttribute("checked"),
     47  }));
     48 
     49  let validLinks = await SpecialPowers.spawn(
     50    gBrowser.selectedBrowser,
     51    [items],
     52    function (contentItems) {
     53      let contentValidLinks = 0;
     54      for (let el of content.document.querySelectorAll("link, style")) {
     55        var title = el.getAttribute("title");
     56        var rel = el.getAttribute("rel");
     57        var media = el.getAttribute("media");
     58        var idstring =
     59          el.nodeName +
     60          " " +
     61          (title ? title : "without title and") +
     62          ' with rel="' +
     63          rel +
     64          '"' +
     65          (media ? ' and media="' + media + '"' : "");
     66 
     67        var item = contentItems.filter(aItem => aItem.label == title);
     68        var found = item.length == 1;
     69        var checked = found && item[0].checked;
     70 
     71        switch (el.getAttribute("data-state")) {
     72          case "0":
     73            ok(!found, idstring + " should not show up in page style menu");
     74            break;
     75          case "1":
     76            contentValidLinks++;
     77            ok(found, idstring + " should show up in page style menu");
     78            ok(!checked, idstring + " should not be selected");
     79            break;
     80          case "2":
     81            contentValidLinks++;
     82            ok(found, idstring + " should show up in page style menu");
     83            ok(checked, idstring + " should be selected");
     84            break;
     85          default:
     86            throw new Error(
     87              "data-state attribute is missing or has invalid value"
     88            );
     89        }
     90      }
     91      return contentValidLinks;
     92    }
     93  );
     94 
     95  ok(menuitems.length, "At least one item in the menu");
     96  is(menuitems.length, validLinks, "all valid links found");
     97 
     98  is(await getRootColor(), LIME, "Root should be lime (styles should apply)");
     99 
    100  let disableStyles = document.getElementById("menu_pageStyleNoStyle");
    101  let defaultStyles = document.getElementById("menu_pageStylePersistentOnly");
    102  let otherStyles = menuitems[0].parentNode.querySelector("[label='28']");
    103 
    104  // Assert that the menu works as expected.
    105  disableStyles.click();
    106 
    107  await TestUtils.waitForCondition(async function () {
    108    let color = await getRootColor();
    109    return color != LIME && color != BLUE;
    110  }, "ensuring disabled styles work");
    111 
    112  otherStyles.click();
    113 
    114  await TestUtils.waitForCondition(async function () {
    115    let color = await getRootColor();
    116    return color == BLUE;
    117  }, "ensuring alternate styles work. clicking on: " + otherStyles.label);
    118 
    119  defaultStyles.click();
    120 
    121  info("ensuring default styles work");
    122  await TestUtils.waitForCondition(async function () {
    123    let color = await getRootColor();
    124    return color == LIME;
    125  }, "ensuring default styles work");
    126 
    127  BrowserTestUtils.removeTab(tab);
    128 });
    129 
    130 add_task(async function test_default_style_with_no_sheets() {
    131  const PAGE = WEB_ROOT + "page_style_only_alternates.html";
    132  await BrowserTestUtils.withNewTab(
    133    {
    134      gBrowser,
    135      url: PAGE,
    136      waitForLoad: true,
    137    },
    138    async function (browser) {
    139      await promiseStylesheetsLoaded(browser, 2);
    140 
    141      let menuitems = fillPopupAndGetItems();
    142      is(menuitems.length, 2, "Should've found two style sets");
    143      is(
    144        await getRootColor(),
    145        BLUE,
    146        "First found style should become the preferred one and apply"
    147      );
    148 
    149      // Reset the styles.
    150      document.getElementById("menu_pageStylePersistentOnly").click();
    151      await TestUtils.waitForCondition(async function () {
    152        let color = await getRootColor();
    153        return color != BLUE && color != RED;
    154      });
    155 
    156      ok(
    157        true,
    158        "Should reset the style properly even if there are no non-alternate stylesheets"
    159      );
    160    }
    161  );
    162 });
    163 
    164 add_task(async function test_page_style_file() {
    165  const FILE_PAGE = Services.io.newFileURI(
    166    new FileUtils.File(getTestFilePath("page_style_sample.html"))
    167  ).spec;
    168  await BrowserTestUtils.withNewTab(FILE_PAGE, async function (browser) {
    169    await promiseStylesheetsLoaded(browser, kStyleSheetsInPageStyleSample);
    170    let menuitems = fillPopupAndGetItems();
    171    is(
    172      menuitems.length,
    173      kStyleSheetsInPageStyleSample,
    174      "Should have the right amount of items even for file: URI."
    175    );
    176  });
    177 });