summaryrefslogtreecommitdiffstats
path: root/accessible/tests/browser/head.js
blob: 2e1ab7f89721bc434f8fa2ab4aff19fe0882fd50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

/* exported initAccService, shutdownAccService, waitForEvent, accConsumersChanged */

// Load the shared-head file first.
Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/accessible/tests/browser/shared-head.js",
  this
);

const { CommonUtils } = ChromeUtils.importESModule(
  "chrome://mochitests/content/browser/accessible/tests/browser/Common.sys.mjs"
);

/**
 * Capture when 'a11y-consumers-changed' event is fired.
 *
 * @param  {?Object} target
 *         [optional] browser object that indicates that accessibility service
 *         is in content process.
 * @return {Array}
 *         List of promises where first one is the promise for when the event
 *         observer is added and the second one for when the event is observed.
 */
function accConsumersChanged(target) {
  return target
    ? [
        SpecialPowers.spawn(target, [], () =>
          content.CommonUtils.addAccConsumersChangedObserver()
        ),
        SpecialPowers.spawn(target, [], () =>
          content.CommonUtils.observeAccConsumersChanged()
        ),
      ]
    : [
        CommonUtils.addAccConsumersChangedObserver(),
        CommonUtils.observeAccConsumersChanged(),
      ];
}

/**
 * Capture when accessibility service is initialized.
 *
 * @param  {?Object} target
 *         [optional] browser object that indicates that accessibility service
 *         is expected to be initialized in content process.
 * @return {Array}
 *         List of promises where first one is the promise for when the event
 *         observer is added and the second one for when the event is observed.
 */
function initAccService(target) {
  return target
    ? [
        SpecialPowers.spawn(target, [], () =>
          content.CommonUtils.addAccServiceInitializedObserver()
        ),
        SpecialPowers.spawn(target, [], () =>
          content.CommonUtils.observeAccServiceInitialized()
        ),
      ]
    : [
        CommonUtils.addAccServiceInitializedObserver(),
        CommonUtils.observeAccServiceInitialized(),
      ];
}

/**
 * Capture when accessibility service is shutdown.
 *
 * @param  {?Object} target
 *         [optional] browser object that indicates that accessibility service
 *         is expected to be shutdown in content process.
 * @return {Array}
 *         List of promises where first one is the promise for when the event
 *         observer is added and the second one for when the event is observed.
 */
function shutdownAccService(target) {
  return target
    ? [
        SpecialPowers.spawn(target, [], () =>
          content.CommonUtils.addAccServiceShutdownObserver()
        ),
        SpecialPowers.spawn(target, [], () =>
          content.CommonUtils.observeAccServiceShutdown()
        ),
      ]
    : [
        CommonUtils.addAccServiceShutdownObserver(),
        CommonUtils.observeAccServiceShutdown(),
      ];
}

/**
 * Simpler verions of waitForEvent defined in
 * accessible/tests/browser/events.js
 */
function waitForEvent(eventType, expectedId) {
  return new Promise(resolve => {
    let eventObserver = {
      observe(subject) {
        let event = subject.QueryInterface(Ci.nsIAccessibleEvent);
        let id;
        try {
          id = event.accessible.id;
        } catch (e) {
          // This can throw NS_ERROR_FAILURE.
        }
        if (event.eventType === eventType && id === expectedId) {
          Services.obs.removeObserver(this, "accessible-event");
          resolve(event);
        }
      },
    };
    Services.obs.addObserver(eventObserver, "accessible-event");
  });
}