tor-browser

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

browser_accessibility_simulator.js (2435B)


      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 const {
      8  accessibility: {
      9    SIMULATION_TYPE: { PROTANOPIA },
     10  },
     11 } = require("resource://devtools/shared/constants.js");
     12 const {
     13  simulation: {
     14    COLOR_TRANSFORMATION_MATRICES: {
     15      PROTANOPIA: PROTANOPIA_MATRIX,
     16      NONE: DEFAULT_MATRIX,
     17    },
     18  },
     19 } = require("resource://devtools/server/actors/accessibility/constants.js");
     20 
     21 // Checks for the SimulatorActor
     22 
     23 async function setup() {
     24  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
     25    content.window.testColorMatrix = function (actual, expected) {
     26      for (const idx in actual) {
     27        is(
     28          actual[idx].toFixed(3),
     29          expected[idx].toFixed(3),
     30          "Color matrix value is set correctly."
     31        );
     32      }
     33    };
     34  });
     35  SimpleTest.registerCleanupFunction(async function () {
     36    await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
     37      content.window.testColorMatrix = null;
     38    });
     39  });
     40 }
     41 
     42 async function testSimulate(simulator, matrix, type = null) {
     43  const matrixApplied = await simulator.simulate({ types: type ? [type] : [] });
     44  ok(matrixApplied, "Simulation color matrix is successfully applied.");
     45 
     46  await SpecialPowers.spawn(
     47    gBrowser.selectedBrowser,
     48    [[type, matrix]],
     49    ([simulationType, simulationMatrix]) => {
     50      const { window } = content;
     51      info(
     52        `Test that color matrix is set to ${
     53          simulationType || "default"
     54        } simulation values.`
     55      );
     56      window.testColorMatrix(
     57        window.docShell.getColorMatrix(),
     58        simulationMatrix
     59      );
     60    }
     61  );
     62 }
     63 
     64 add_task(async function () {
     65  const { target, accessibility } = await initAccessibilityFrontsForUrl(
     66    MAIN_DOMAIN + "doc_accessibility.html",
     67    { enableByDefault: false }
     68  );
     69 
     70  const simulator = accessibility.simulatorFront;
     71  if (!simulator) {
     72    ok(false, "Missing simulator actor.");
     73    return;
     74  }
     75 
     76  await setup();
     77 
     78  info("Test that protanopia is successfully simulated.");
     79  await testSimulate(simulator, PROTANOPIA_MATRIX, PROTANOPIA);
     80 
     81  info(
     82    "Test that simulations are successfully removed by setting default color matrix."
     83  );
     84  await testSimulate(simulator, DEFAULT_MATRIX);
     85 
     86  await target.destroy();
     87  gBrowser.removeCurrentTab();
     88 });