tor-browser

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

browser_help_panel_cloning.js (3528B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /* global PanelUI */
      7 
      8 let gAppMenuStrings = new Localization(
      9  ["branding/brand.ftl", "browser/appmenu.ftl"],
     10  true
     11 );
     12 
     13 const CLONED_ATTRS = ["command", "oncommand", "onclick", "key", "disabled"];
     14 
     15 /**
     16 * Tests that the Help panel inside of the AppMenu properly clones
     17 * the items from the Help menupopup. Also ensures that the AppMenu
     18 * string variants for those menuitems exist inside of appmenu.ftl.
     19 */
     20 add_task(async function test_help_panel_cloning() {
     21  await gCUITestUtils.openMainMenu();
     22  registerCleanupFunction(async () => {
     23    await gCUITestUtils.hideMainMenu();
     24  });
     25 
     26  // Showing the Help panel should be enough to get the menupopup to
     27  // populate itself.
     28  let anchor = document.getElementById("PanelUI-menu-button");
     29  PanelUI.showHelpView(anchor);
     30 
     31  let appMenuHelpSubview = document.getElementById("PanelUI-helpView");
     32  await BrowserTestUtils.waitForEvent(appMenuHelpSubview, "ViewShowing");
     33 
     34  let helpMenuPopup = document.getElementById("menu_HelpPopup");
     35  let helpMenuPopupItems = helpMenuPopup.querySelectorAll("menuitem");
     36 
     37  for (let helpMenuPopupItem of helpMenuPopupItems) {
     38    if (helpMenuPopupItem.hidden) {
     39      continue;
     40    }
     41 
     42    let appMenuHelpId = "appMenu_" + helpMenuPopupItem.id;
     43    info(`Checking ${appMenuHelpId}`);
     44 
     45    let appMenuHelpItem = appMenuHelpSubview.querySelector(`#${appMenuHelpId}`);
     46    Assert.ok(appMenuHelpItem, "Should have found a cloned AppMenu help item");
     47 
     48    let appMenuHelpItemL10nId = appMenuHelpItem.dataset.l10nId;
     49    // There is a convention that the Help menu item should have an
     50    // appmenu-data-l10n-id attribute set as the AppMenu-specific localization
     51    // id.
     52    Assert.equal(
     53      helpMenuPopupItem.getAttribute("appmenu-data-l10n-id"),
     54      appMenuHelpItemL10nId,
     55      "Help menuitem supplied a data-l10n-id for the AppMenu Help item"
     56    );
     57 
     58    let [strings] = gAppMenuStrings.formatMessagesSync([
     59      { id: appMenuHelpItemL10nId },
     60    ]);
     61    Assert.ok(strings, "Should have found strings for the AppMenu help item");
     62 
     63    // Make sure the CLONED_ATTRs are actually cloned.
     64    for (let attr of CLONED_ATTRS) {
     65      if (attr == "oncommand" && helpMenuPopupItem.hasAttribute("command")) {
     66        // If the original element had a "command" attribute set, then the
     67        // cloned element will have its "oncommand" attribute set to equal
     68        // the "oncommand" attribute of the <command> pointed to via the
     69        // original's "command" attribute once it is inserted into the DOM.
     70        //
     71        // This is by virtue of the broadcasting ability of XUL <command>
     72        // elements.
     73        let commandNode = document.getElementById(
     74          helpMenuPopupItem.getAttribute("command")
     75        );
     76        Assert.equal(
     77          commandNode.getAttribute("oncommand"),
     78          appMenuHelpItem.getAttribute("oncommand"),
     79          "oncommand was properly cloned."
     80        );
     81      } else if (attr == "disabled") {
     82        // We really clone the property, so the attribute value might differ (e.g. "true" vs "").
     83        Assert.equal(
     84          helpMenuPopupItem.hasAttribute(attr),
     85          appMenuHelpItem.hasAttribute(attr),
     86          `${attr} property was cloned.`
     87        );
     88      } else {
     89        Assert.equal(
     90          helpMenuPopupItem.getAttribute(attr),
     91          appMenuHelpItem.getAttribute(attr),
     92          `${attr} attribute was cloned.`
     93        );
     94      }
     95    }
     96  }
     97 });