summaryrefslogtreecommitdiffstats
path: root/accessible/tests/browser/windows/uia/head.js
blob: 5b453ce6feb35e63ae6f96abe31be371f3124780 (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
121
122
123
124
125
126
127
128
/* 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 gIsUiaEnabled, addUiaTask, definePyVar, assignPyVarToUiaWithId, setUpWaitForUiaEvent, setUpWaitForUiaPropEvent, waitForUiaEvent, testPatternAbsent, testPythonRaises */

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

// Loading and common.js from accessible/tests/mochitest/ for all tests, as
// well as promisified-events.js.
loadScripts(
  { name: "common.js", dir: MOCHITESTS_DIR },
  { name: "promisified-events.js", dir: MOCHITESTS_DIR }
);

let gIsUiaEnabled = false;

/**
 * This is like addAccessibleTask, but takes two additional boolean options:
 * - uiaEnabled: Whether to run a variation of this test with Gecko UIA enabled.
 * - uiaDisabled: Whether to run a variation of this test with UIA disabled. In
 *   this case, UIA will rely entirely on the IA2 -> UIA proxy.
 * If both are set, the test will be run twice with different configurations.
 * You can determine which variant is currently running using the gIsUiaEnabled
 * variable. This is useful for conditional tests; e.g. if Gecko UIA supports
 * something that the IA2 -> UIA proxy doesn't support.
 */
function addUiaTask(doc, task, options = {}) {
  const { uiaEnabled = true, uiaDisabled = true } = options;

  function addTask(shouldEnable) {
    async function uiaTask(browser, docAcc, topDocAcc) {
      await SpecialPowers.pushPrefEnv({
        set: [["accessibility.uia.enable", shouldEnable]],
      });
      gIsUiaEnabled = shouldEnable;
      info(shouldEnable ? "Gecko UIA enabled" : "Gecko UIA disabled");
      await task(browser, docAcc, topDocAcc);
    }
    addAccessibleTask(doc, uiaTask, options);
  }

  if (uiaEnabled) {
    addTask(true);
  }
  if (uiaDisabled) {
    addTask(false);
  }
}

/**
 * Define a global Python variable and assign it to a given Python expression.
 */
function definePyVar(varName, expression) {
  return runPython(`
    global ${varName}
    ${varName} = ${expression}
  `);
}

/**
 * Get the UIA element with the given id and assign it to a global Python
 * variable using the id as the variable name.
 */
function assignPyVarToUiaWithId(id) {
  return definePyVar(id, `findUiaByDomId(doc, "${id}")`);
}

/**
 * Set up to wait for a UIA event. You must await this before performing the
 * action which fires the event.
 */
function setUpWaitForUiaEvent(eventName, id) {
  return definePyVar(
    "onEvent",
    `WaitForUiaEvent(eventId=UIA_${eventName}EventId, match="${id}")`
  );
}

/**
 * Set up to wait for a UIA property change event. You must await this before
 * performing the action which fires the event.
 */
function setUpWaitForUiaPropEvent(propName, id) {
  return definePyVar(
    "onEvent",
    `WaitForUiaEvent(property=UIA_${propName}PropertyId, match="${id}")`
  );
}

/**
 * Wait for the event requested in setUpWaitForUia*Event.
 */
function waitForUiaEvent() {
  return runPython(`
    onEvent.wait()
  `);
}

/**
 * Verify that a UIA element does *not* support the given control pattern.
 */
async function testPatternAbsent(id, patternName) {
  const hasPattern = await runPython(`
    el = findUiaByDomId(doc, "${id}")
    return bool(getUiaPattern(el, "${patternName}"))
  `);
  ok(!hasPattern, `${id} doesn't have ${patternName} pattern`);
}

/**
 * Verify that a Python expression raises an exception.
 */
async function testPythonRaises(expression, message) {
  let failed = false;
  try {
    await runPython(expression);
  } catch {
    failed = true;
  }
  ok(failed, message);
}