tor-browser

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

head.js (3790B)


      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 initAccService, shutdownAccService, waitForEvent, accConsumersChanged */
      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 const { CommonUtils } = ChromeUtils.importESModule(
     16  "chrome://mochitests/content/browser/accessible/tests/browser/Common.sys.mjs"
     17 );
     18 
     19 /**
     20 * Capture when 'a11y-consumers-changed' event is fired.
     21 *
     22 * @param  {?object} target
     23 *         [optional] browser object that indicates that accessibility service
     24 *         is in content process.
     25 * @return {Array}
     26 *         List of promises where first one is the promise for when the event
     27 *         observer is added and the second one for when the event is observed.
     28 */
     29 function accConsumersChanged(target) {
     30  return target
     31    ? [
     32        SpecialPowers.spawn(target, [], () =>
     33          content.CommonUtils.addAccConsumersChangedObserver()
     34        ),
     35        SpecialPowers.spawn(target, [], () =>
     36          content.CommonUtils.observeAccConsumersChanged()
     37        ),
     38      ]
     39    : [
     40        CommonUtils.addAccConsumersChangedObserver(),
     41        CommonUtils.observeAccConsumersChanged(),
     42      ];
     43 }
     44 
     45 /**
     46 * Capture when accessibility service is initialized.
     47 *
     48 * @param  {?object} target
     49 *         [optional] browser object that indicates that accessibility service
     50 *         is expected to be initialized in content process.
     51 * @return {Array}
     52 *         List of promises where first one is the promise for when the event
     53 *         observer is added and the second one for when the event is observed.
     54 */
     55 function initAccService(target) {
     56  return target
     57    ? [
     58        SpecialPowers.spawn(target, [], () =>
     59          content.CommonUtils.addAccServiceInitializedObserver()
     60        ),
     61        SpecialPowers.spawn(target, [], () =>
     62          content.CommonUtils.observeAccServiceInitialized()
     63        ),
     64      ]
     65    : [
     66        CommonUtils.addAccServiceInitializedObserver(),
     67        CommonUtils.observeAccServiceInitialized(),
     68      ];
     69 }
     70 
     71 /**
     72 * Capture when accessibility service is shutdown.
     73 *
     74 * @param  {?object} target
     75 *         [optional] browser object that indicates that accessibility service
     76 *         is expected to be shutdown in content process.
     77 * @return {Array}
     78 *         List of promises where first one is the promise for when the event
     79 *         observer is added and the second one for when the event is observed.
     80 */
     81 function shutdownAccService(target) {
     82  return target
     83    ? [
     84        SpecialPowers.spawn(target, [], () =>
     85          content.CommonUtils.addAccServiceShutdownObserver()
     86        ),
     87        SpecialPowers.spawn(target, [], () =>
     88          content.CommonUtils.observeAccServiceShutdown()
     89        ),
     90      ]
     91    : [
     92        CommonUtils.addAccServiceShutdownObserver(),
     93        CommonUtils.observeAccServiceShutdown(),
     94      ];
     95 }
     96 
     97 /**
     98 * Simpler verions of waitForEvent defined in
     99 * accessible/tests/browser/events.js
    100 */
    101 function waitForEvent(eventType, expectedId) {
    102  return new Promise(resolve => {
    103    let eventObserver = {
    104      observe(subject) {
    105        let event = subject.QueryInterface(Ci.nsIAccessibleEvent);
    106        let id;
    107        try {
    108          id = event.accessible.id;
    109        } catch (e) {
    110          // This can throw NS_ERROR_FAILURE.
    111        }
    112        if (event.eventType === eventType && id === expectedId) {
    113          Services.obs.removeObserver(this, "accessible-event");
    114          resolve(event);
    115        }
    116      },
    117    };
    118    Services.obs.addObserver(eventObserver, "accessible-event");
    119  });
    120 }