tor-browser

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

browser_accessibility_infobar_show.js (5862B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 // Checks for the AccessibleHighlighter's and XULWindowHighlighter's infobar components.
      8 
      9 add_task(async function () {
     10  await BrowserTestUtils.withNewTab(
     11    {
     12      gBrowser,
     13      url: MAIN_DOMAIN + "doc_accessibility_infobar.html",
     14    },
     15    async function (browser) {
     16      await SpecialPowers.spawn(browser, [], async function () {
     17        const { require } = ChromeUtils.importESModule(
     18          "resource://devtools/shared/loader/Loader.sys.mjs"
     19        );
     20        const {
     21          HighlighterEnvironment,
     22        } = require("resource://devtools/server/actors/highlighters.js");
     23        const {
     24          AccessibleHighlighter,
     25        } = require("resource://devtools/server/actors/highlighters/accessible.js");
     26 
     27        /**
     28         * Get whether or not infobar container is hidden.
     29         *
     30         * @param  {object} infobar
     31         *         Accessible highlighter's infobar component.
     32         * @return {string | null} If the infobar container is hidden.
     33         */
     34        function isContainerHidden(infobar) {
     35          return !!infobar
     36            .getElement("accessible-infobar-container")
     37            .getAttribute("hidden");
     38        }
     39 
     40        /**
     41         * Get name of accessible object.
     42         *
     43         * @param  {object} infobar
     44         *         Accessible highlighter's infobar component.
     45         * @return {string} The text content of the infobar-name element.
     46         */
     47        function getName(infobar) {
     48          return infobar.getTextContent("accessible-infobar-name");
     49        }
     50 
     51        /**
     52         * Get role of accessible object.
     53         *
     54         * @param  {object} infobar
     55         *         Accessible highlighter's infobar component.
     56         * @return {string} The text content of the infobar-role element.
     57         */
     58        function getRole(infobar) {
     59          return infobar.getTextContent("accessible-infobar-role");
     60        }
     61 
     62        /**
     63         * Checks for updated content for an infobar with valid bounds.
     64         *
     65         * @param  {object} infobar
     66         *         Accessible highlighter's infobar component.
     67         * @param  {object} options
     68         *         Options to pass for the highlighter's show method.
     69         *         Available options:
     70         *         - {String} role
     71         *           Role value of the accessible.
     72         *         - {String} name
     73         *           Name value of the accessible.
     74         *         - {Boolean} shouldBeHidden
     75         *           If the infobar component should be hidden.
     76         */
     77        function checkInfobar(infobar, { shouldBeHidden, role, name }) {
     78          is(
     79            isContainerHidden(infobar),
     80            shouldBeHidden,
     81            "Infobar's hidden state is correct."
     82          );
     83 
     84          if (shouldBeHidden) {
     85            return;
     86          }
     87 
     88          is(getRole(infobar), role, "infobarRole text content is correct");
     89          is(
     90            getName(infobar),
     91            `"${name}"`,
     92            "infoBarName text content is correct"
     93          );
     94        }
     95 
     96        /**
     97         * Checks for updated content of an infobar with valid bounds.
     98         *
     99         * @param  {Element} node
    100         *         Node to check infobar content on.
    101         * @param  {object}  highlighter
    102         *         Accessible highlighter.
    103         */
    104        function testInfobar(node, highlighter) {
    105          const infobar = highlighter.accessibleInfobar;
    106          const bounds = {
    107            x: 0,
    108            y: 0,
    109            w: 250,
    110            h: 100,
    111          };
    112 
    113          info("Check that infobar is shown with valid bounds.");
    114          highlighter.show(node, {
    115            ...bounds,
    116            role: "button",
    117            name: "Accessible Button",
    118          });
    119 
    120          checkInfobar(infobar, {
    121            role: "button",
    122            name: "Accessible Button",
    123            shouldBeHidden: false,
    124          });
    125          highlighter.hide();
    126 
    127          info("Check that infobar is hidden after .hide() is called.");
    128          checkInfobar(infobar, { shouldBeHidden: true });
    129 
    130          info("Check to make sure content is updated with new options.");
    131          highlighter.show(node, {
    132            ...bounds,
    133            name: "Test link",
    134            role: "link",
    135          });
    136          checkInfobar(infobar, {
    137            name: "Test link",
    138            role: "link",
    139            shouldBeHidden: false,
    140          });
    141          highlighter.hide();
    142        }
    143 
    144        // Start testing. First, create highlighter environment and initialize.
    145        const env = new HighlighterEnvironment();
    146        env.initFromWindow(content.window);
    147 
    148        // Wait for loading highlighter environment content to complete before creating the
    149        // highlighter.
    150        await new Promise(resolve => {
    151          const doc = env.document;
    152 
    153          function onContentLoaded() {
    154            if (
    155              doc.readyState === "interactive" ||
    156              doc.readyState === "complete"
    157            ) {
    158              resolve();
    159            } else {
    160              doc.addEventListener("DOMContentLoaded", onContentLoaded, {
    161                once: true,
    162              });
    163            }
    164          }
    165 
    166          onContentLoaded();
    167        });
    168 
    169        // Now, we can test the Infobar and XULWindowInfobar components with their
    170        // respective highlighters.
    171        const node = content.document.createElement("div");
    172        content.document.body.append(node);
    173 
    174        info("Checks for Infobar's show method");
    175        const highlighter = new AccessibleHighlighter(env);
    176        await highlighter.isReady;
    177        testInfobar(node, highlighter);
    178      });
    179    }
    180  );
    181 });