tor-browser

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

browser_accessibility_infobar_audit_text_label.js (5357B)


      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 infobar component and its text label
      8 // audit.
      9 
     10 add_task(async function () {
     11  await BrowserTestUtils.withNewTab(
     12    {
     13      gBrowser,
     14      url: MAIN_DOMAIN + "doc_accessibility_infobar.html",
     15    },
     16    async function (browser) {
     17      await SpecialPowers.spawn(browser, [], async function () {
     18        const { require } = ChromeUtils.importESModule(
     19          "resource://devtools/shared/loader/Loader.sys.mjs"
     20        );
     21        const {
     22          HighlighterEnvironment,
     23        } = require("resource://devtools/server/actors/highlighters.js");
     24        const {
     25          AccessibleHighlighter,
     26        } = require("resource://devtools/server/actors/highlighters/accessible.js");
     27        const {
     28          LocalizationHelper,
     29        } = require("resource://devtools/shared/l10n.js");
     30        const L10N = new LocalizationHelper(
     31          "devtools/shared/locales/accessibility.properties"
     32        );
     33 
     34        const {
     35          accessibility: {
     36            AUDIT_TYPE,
     37            ISSUE_TYPE: {
     38              [AUDIT_TYPE.TEXT_LABEL]: {
     39                DIALOG_NO_NAME,
     40                FORM_NO_VISIBLE_NAME,
     41                TOOLBAR_NO_NAME,
     42              },
     43            },
     44            SCORES: { BEST_PRACTICES, FAIL, WARNING },
     45          },
     46        } = require("resource://devtools/shared/constants.js");
     47 
     48        /**
     49         * Checks for updated content for an infobar.
     50         *
     51         * @param  {object} infobar
     52         *         Accessible highlighter's infobar component.
     53         * @param  {object} audit
     54         *         Audit information that is passed on highlighter show.
     55         */
     56        function checkTextLabel(infobar, audit) {
     57          const { issue, score } = audit || {};
     58          let expected = "";
     59          if (issue) {
     60            const { ISSUE_TO_INFOBAR_LABEL_MAP } =
     61              infobar.audit.reports[AUDIT_TYPE.TEXT_LABEL].constructor;
     62            expected = L10N.getStr(ISSUE_TO_INFOBAR_LABEL_MAP[issue]);
     63          }
     64 
     65          is(
     66            infobar.getTextContent("accessible-text-label"),
     67            expected,
     68            "infobar text label audit text content is correct"
     69          );
     70          if (score) {
     71            ok(
     72              infobar
     73                .getElement("accessible-text-label")
     74                .classList.contains(score)
     75            );
     76          }
     77        }
     78 
     79        // Start testing. First, create highlighter environment and initialize.
     80        const env = new HighlighterEnvironment();
     81        env.initFromWindow(content.window);
     82 
     83        // Wait for loading highlighter environment content to complete before creating the
     84        // highlighter.
     85        await new Promise(resolve => {
     86          const doc = env.document;
     87 
     88          function onContentLoaded() {
     89            if (
     90              doc.readyState === "interactive" ||
     91              doc.readyState === "complete"
     92            ) {
     93              resolve();
     94            } else {
     95              doc.addEventListener("DOMContentLoaded", onContentLoaded, {
     96                once: true,
     97              });
     98            }
     99          }
    100 
    101          onContentLoaded();
    102        });
    103 
    104        // Now, we can test the Infobar's audit content.
    105        const node = content.document.createElement("div");
    106        content.document.body.append(node);
    107        const highlighter = new AccessibleHighlighter(env);
    108        await highlighter.isReady;
    109        const infobar = highlighter.accessibleInfobar;
    110        const bounds = {
    111          x: 0,
    112          y: 0,
    113          w: 250,
    114          h: 100,
    115        };
    116 
    117        const tests = [
    118          {
    119            desc: "Infobar is shown with no text label audit content when no audit.",
    120          },
    121          {
    122            desc: "Infobar is shown with no text label audit content when audit is null.",
    123            audit: null,
    124          },
    125          {
    126            desc:
    127              "Infobar is shown with no text label audit content when empty " +
    128              "text label audit.",
    129            audit: { [AUDIT_TYPE.TEXT_LABEL]: null },
    130          },
    131          {
    132            desc: "Infobar is shown with text label audit content for an error.",
    133            audit: {
    134              [AUDIT_TYPE.TEXT_LABEL]: { score: FAIL, issue: TOOLBAR_NO_NAME },
    135            },
    136          },
    137          {
    138            desc: "Infobar is shown with text label audit content for a warning.",
    139            audit: {
    140              [AUDIT_TYPE.TEXT_LABEL]: {
    141                score: WARNING,
    142                issue: FORM_NO_VISIBLE_NAME,
    143              },
    144            },
    145          },
    146          {
    147            desc: "Infobar is shown with text label audit content for best practices.",
    148            audit: {
    149              [AUDIT_TYPE.TEXT_LABEL]: {
    150                score: BEST_PRACTICES,
    151                issue: DIALOG_NO_NAME,
    152              },
    153            },
    154          },
    155        ];
    156 
    157        for (const test of tests) {
    158          const { desc, audit } = test;
    159 
    160          info(desc);
    161          highlighter.show(node, { ...bounds, audit });
    162          checkTextLabel(infobar, audit && audit[AUDIT_TYPE.TEXT_LABEL]);
    163          highlighter.hide();
    164        }
    165      });
    166    }
    167  );
    168 });