tor-browser

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

head.js (3224B)


      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 /* exported waitForIFrameA11yReady, waitForIFrameUpdates, spawnTestStates, testVisibility */
      8 
      9 // Load the shared-head file first.
     10 Services.scriptloader.loadSubScript(
     11  "chrome://mochitests/content/browser/accessible/tests/browser/shared-head.js",
     12  this
     13 );
     14 
     15 // Loading and common.js from accessible/tests/mochitest/ for all tests, as
     16 // well as promisified-events.js.
     17 /* import-globals-from ../../mochitest/states.js */
     18 /* import-globals-from ../../mochitest/role.js */
     19 loadScripts(
     20  { name: "common.js", dir: MOCHITESTS_DIR },
     21  { name: "promisified-events.js", dir: MOCHITESTS_DIR },
     22  { name: "role.js", dir: MOCHITESTS_DIR },
     23  { name: "states.js", dir: MOCHITESTS_DIR }
     24 );
     25 
     26 // This is another version of addA11yLoadEvent for fission.
     27 async function waitForIFrameA11yReady(iFrameBrowsingContext) {
     28  await SimpleTest.promiseFocus(window);
     29 
     30  await SpecialPowers.spawn(iFrameBrowsingContext, [], () => {
     31    return new Promise(resolve => {
     32      function waitForDocLoad() {
     33        SpecialPowers.executeSoon(() => {
     34          const acc = SpecialPowers.Cc[
     35            "@mozilla.org/accessibilityService;1"
     36          ].getService(SpecialPowers.Ci.nsIAccessibilityService);
     37 
     38          const accDoc = acc.getAccessibleFor(content.document);
     39          let state = {};
     40          accDoc.getState(state, {});
     41          if (state.value & SpecialPowers.Ci.nsIAccessibleStates.STATE_BUSY) {
     42            SpecialPowers.executeSoon(waitForDocLoad);
     43            return;
     44          }
     45          resolve();
     46        }, 0);
     47      }
     48      waitForDocLoad();
     49    });
     50  });
     51 }
     52 
     53 // A utility function to make sure the information of scroll position or visible
     54 // area changes reach to out-of-process iframes.
     55 async function waitForIFrameUpdates() {
     56  // Wait for two frames since the information is notified via asynchronous IPC
     57  // calls.
     58  await new Promise(resolve => requestAnimationFrame(resolve));
     59  await new Promise(resolve => requestAnimationFrame(resolve));
     60 }
     61 
     62 // A utility function to test the state of |elementId| element in out-of-process
     63 // |browsingContext|.
     64 async function spawnTestStates(browsingContext, elementId, expectedStates) {
     65  function testStates(id, expected, unexpected) {
     66    const acc = SpecialPowers.Cc[
     67      "@mozilla.org/accessibilityService;1"
     68    ].getService(SpecialPowers.Ci.nsIAccessibilityService);
     69    const target = content.document.getElementById(id);
     70    let state = {};
     71    acc.getAccessibleFor(target).getState(state, {});
     72    if (expected === 0) {
     73      Assert.equal(state.value, expected);
     74    } else {
     75      Assert.ok(state.value & expected);
     76    }
     77    Assert.ok(!(state.value & unexpected));
     78  }
     79  await SpecialPowers.spawn(
     80    browsingContext,
     81    [elementId, expectedStates],
     82    testStates
     83  );
     84 }
     85 
     86 function testVisibility(acc, shouldBeOffscreen, shouldBeInvisible) {
     87  const [states] = getStates(acc);
     88  let looksGood = shouldBeOffscreen == ((states & STATE_OFFSCREEN) != 0);
     89  looksGood &= shouldBeInvisible == ((states & STATE_INVISIBLE) != 0);
     90  return looksGood;
     91 }