tor-browser

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

head.js (3884B)


      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 getNativeInterface, waitForMacEventWithInfo, waitForMacEvent, waitForStateChange,
      8   NSRange, NSDictionary, stringForRange, AXTextStateChangeTypeEdit,
      9   AXTextEditTypeDelete, AXTextEditTypeTyping, AXTextStateChangeTypeSelectionMove,
     10   AXTextStateChangeTypeSelectionExtend, AXTextSelectionDirectionUnknown,
     11   AXTextSelectionDirectionPrevious, AXTextSelectionDirectionNext,
     12   AXTextSelectionDirectionDiscontiguous, AXTextSelectionGranularityUnknown,
     13   AXTextSelectionDirectionBeginning, AXTextSelectionDirectionEnd,
     14   AXTextSelectionGranularityCharacter, AXTextSelectionGranularityWord,
     15   AXTextSelectionGranularityLine */
     16 
     17 // Load the shared-head file first.
     18 Services.scriptloader.loadSubScript(
     19  "chrome://mochitests/content/browser/accessible/tests/browser/shared-head.js",
     20  this
     21 );
     22 
     23 // Loading and common.js from accessible/tests/mochitest/ for all tests, as
     24 // well as promisified-events.js.
     25 loadScripts(
     26  { name: "common.js", dir: MOCHITESTS_DIR },
     27  { name: "promisified-events.js", dir: MOCHITESTS_DIR }
     28 );
     29 
     30 // AXTextStateChangeType enum values
     31 const AXTextStateChangeTypeEdit = 1;
     32 const AXTextStateChangeTypeSelectionMove = 2;
     33 const AXTextStateChangeTypeSelectionExtend = 3;
     34 
     35 // AXTextEditType enum values
     36 const AXTextEditTypeDelete = 1;
     37 const AXTextEditTypeTyping = 3;
     38 
     39 // AXTextSelectionDirection enum values
     40 const AXTextSelectionDirectionUnknown = 0;
     41 const AXTextSelectionDirectionBeginning = 1;
     42 const AXTextSelectionDirectionEnd = 2;
     43 const AXTextSelectionDirectionPrevious = 3;
     44 const AXTextSelectionDirectionNext = 4;
     45 const AXTextSelectionDirectionDiscontiguous = 5;
     46 
     47 // AXTextSelectionGranularity enum values
     48 const AXTextSelectionGranularityUnknown = 0;
     49 const AXTextSelectionGranularityCharacter = 1;
     50 const AXTextSelectionGranularityWord = 2;
     51 const AXTextSelectionGranularityLine = 3;
     52 
     53 function getNativeInterface(accDoc, id) {
     54  return findAccessibleChildByID(accDoc, id).nativeInterface.QueryInterface(
     55    Ci.nsIAccessibleMacInterface
     56  );
     57 }
     58 
     59 function waitForMacEventWithInfo(notificationType, filter) {
     60  let filterFunc = (macIface, data) => {
     61    if (!filter) {
     62      return true;
     63    }
     64 
     65    if (typeof filter == "function") {
     66      return filter(macIface, data);
     67    }
     68 
     69    return macIface.getAttributeValue("AXDOMIdentifier") == filter;
     70  };
     71 
     72  return new Promise(resolve => {
     73    let eventObserver = {
     74      observe(subject, topic, data) {
     75        let macEvent = subject.QueryInterface(Ci.nsIAccessibleMacEvent);
     76        if (
     77          data === notificationType &&
     78          filterFunc(macEvent.macIface, macEvent.data)
     79        ) {
     80          Services.obs.removeObserver(this, "accessible-mac-event");
     81          resolve(macEvent);
     82        }
     83      },
     84    };
     85    Services.obs.addObserver(eventObserver, "accessible-mac-event");
     86  });
     87 }
     88 
     89 function waitForMacEvent(notificationType, filter) {
     90  return waitForMacEventWithInfo(notificationType, filter).then(
     91    e => e.macIface
     92  );
     93 }
     94 
     95 function NSRange(location, length) {
     96  return {
     97    valueType: "NSRange",
     98    value: [location, length],
     99  };
    100 }
    101 
    102 function NSDictionary(dict) {
    103  return {
    104    objectType: "NSDictionary",
    105    object: dict,
    106  };
    107 }
    108 
    109 function stringForRange(macDoc, range) {
    110  if (!range) {
    111    return "";
    112  }
    113 
    114  let str = macDoc.getParameterizedAttributeValue(
    115    "AXStringForTextMarkerRange",
    116    range
    117  );
    118 
    119  let attrStr = macDoc.getParameterizedAttributeValue(
    120    "AXAttributedStringForTextMarkerRange",
    121    range
    122  );
    123 
    124  // This is a fly-by test to make sure our attributed strings
    125  // always match our flat strings.
    126  is(
    127    attrStr.map(({ string }) => string).join(""),
    128    str,
    129    "attributed text matches non-attributed text"
    130  );
    131 
    132  return str;
    133 }