accessibility.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
  6. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  7. Cu.import("resource://gre/modules/Log.jsm");
  8. const logger = Log.repository.getLogger("Marionette");
  9. Cu.import("chrome://marionette/content/error.js");
  10. XPCOMUtils.defineLazyModuleGetter(
  11. this, "setInterval", "resource://gre/modules/Timer.jsm");
  12. XPCOMUtils.defineLazyModuleGetter(
  13. this, "clearInterval", "resource://gre/modules/Timer.jsm");
  14. XPCOMUtils.defineLazyGetter(this, "service", () => {
  15. let service;
  16. try {
  17. service = Cc["@mozilla.org/accessibilityService;1"].getService(
  18. Ci.nsIAccessibilityService);
  19. } catch (e) {
  20. logger.warn("Accessibility module is not present");
  21. } finally {
  22. return service;
  23. }
  24. });
  25. this.EXPORTED_SYMBOLS = ["accessibility"];
  26. /**
  27. * Number of attempts to get an accessible object for an element.
  28. * We attempt more than once because accessible tree can be out of sync
  29. * with the DOM tree for a short period of time.
  30. */
  31. const GET_ACCESSIBLE_ATTEMPTS = 100;
  32. /**
  33. * An interval between attempts to retrieve an accessible object for an
  34. * element.
  35. */
  36. const GET_ACCESSIBLE_ATTEMPT_INTERVAL = 10;
  37. this.accessibility = {
  38. get service() {
  39. return service;
  40. }
  41. };
  42. /**
  43. * Accessible states used to check element"s state from the accessiblity API
  44. * perspective.
  45. * Note: if gecko is built with --disable-accessibility, the interfaces are not
  46. * defined. This is why we use getters instead to be able to use these
  47. * statically.
  48. */
  49. accessibility.State = {
  50. get Unavailable() {
  51. return Ci.nsIAccessibleStates.STATE_UNAVAILABLE;
  52. },
  53. get Focusable() {
  54. return Ci.nsIAccessibleStates.STATE_FOCUSABLE;
  55. },
  56. get Selectable() {
  57. return Ci.nsIAccessibleStates.STATE_SELECTABLE;
  58. },
  59. get Selected() {
  60. return Ci.nsIAccessibleStates.STATE_SELECTED;
  61. }
  62. };
  63. /**
  64. * Accessible object roles that support some action.
  65. */
  66. accessibility.ActionableRoles = new Set([
  67. "checkbutton",
  68. "check menu item",
  69. "check rich option",
  70. "combobox",
  71. "combobox option",
  72. "entry",
  73. "key",
  74. "link",
  75. "listbox option",
  76. "listbox rich option",
  77. "menuitem",
  78. "option",
  79. "outlineitem",
  80. "pagetab",
  81. "pushbutton",
  82. "radiobutton",
  83. "radio menu item",
  84. "rowheader",
  85. "slider",
  86. "spinbutton",
  87. "switch",
  88. ]);
  89. /**
  90. * Factory function that constructs a new {@code accessibility.Checks}
  91. * object with enforced strictness or not.
  92. */
  93. accessibility.get = function (strict = false) {
  94. return new accessibility.Checks(!!strict);
  95. };
  96. /**
  97. * Component responsible for interacting with platform accessibility
  98. * API.
  99. *
  100. * Its methods serve as wrappers for testing content and chrome
  101. * accessibility as well as accessibility of user interactions.
  102. */
  103. accessibility.Checks = class {
  104. /**
  105. * @param {boolean} strict
  106. * Flag indicating whether the accessibility issue should be logged
  107. * or cause an error to be thrown. Default is to log to stdout.
  108. */
  109. constructor(strict) {
  110. this.strict = strict;
  111. }
  112. /**
  113. * Get an accessible object for an element.
  114. *
  115. * @param {DOMElement|XULElement} element
  116. * Element to get the accessible object for.
  117. * @param {boolean=} mustHaveAccessible
  118. * Flag indicating that the element must have an accessible object.
  119. * Defaults to not require this.
  120. *
  121. * @return {Promise: nsIAccessible}
  122. * Promise with an accessibility object for the given element.
  123. */
  124. getAccessible(element, mustHaveAccessible = false) {
  125. if (!this.strict) {
  126. return Promise.resolve();
  127. }
  128. return new Promise((resolve, reject) => {
  129. if (!accessibility.service) {
  130. reject();
  131. return;
  132. }
  133. let acc = accessibility.service.getAccessibleFor(element);
  134. if (acc || !mustHaveAccessible) {
  135. // if accessible object is found, return it;
  136. // if it is not required, also resolve
  137. resolve(acc);
  138. } else {
  139. // if we require an accessible object, we need to poll for it
  140. // because accessible tree might be
  141. // out of sync with DOM tree for a short time
  142. let attempts = GET_ACCESSIBLE_ATTEMPTS;
  143. let intervalId = setInterval(() => {
  144. let acc = accessibility.service.getAccessibleFor(element);
  145. if (acc || --attempts <= 0) {
  146. clearInterval(intervalId);
  147. if (acc) {
  148. resolve(acc);
  149. } else {
  150. reject();
  151. }
  152. }
  153. }, GET_ACCESSIBLE_ATTEMPT_INTERVAL);
  154. }
  155. }).catch(() => this.error(
  156. "Element does not have an accessible object", element));
  157. };
  158. /**
  159. * Test if the accessible has a role that supports some arbitrary
  160. * action.
  161. *
  162. * @param {nsIAccessible} accessible
  163. * Accessible object.
  164. *
  165. * @return {boolean}
  166. * True if an actionable role is found on the accessible, false
  167. * otherwise.
  168. */
  169. isActionableRole(accessible) {
  170. return accessibility.ActionableRoles.has(
  171. accessibility.service.getStringRole(accessible.role));
  172. }
  173. /**
  174. * Test if an accessible has at least one action that it supports.
  175. *
  176. * @param {nsIAccessible} accessible
  177. * Accessible object.
  178. *
  179. * @return {boolean}
  180. * True if the accessible has at least one supported action,
  181. * false otherwise.
  182. */
  183. hasActionCount(accessible) {
  184. return accessible.actionCount > 0;
  185. }
  186. /**
  187. * Test if an accessible has a valid name.
  188. *
  189. * @param {nsIAccessible} accessible
  190. * Accessible object.
  191. *
  192. * @return {boolean}
  193. * True if the accessible has a non-empty valid name, or false if
  194. * this is not the case.
  195. */
  196. hasValidName(accessible) {
  197. return accessible.name && accessible.name.trim();
  198. }
  199. /**
  200. * Test if an accessible has a {@code hidden} attribute.
  201. *
  202. * @param {nsIAccessible} accessible
  203. * Accessible object.
  204. *
  205. * @return {boolean}
  206. * True if the accesible object has a {@code hidden} attribute,
  207. * false otherwise.
  208. */
  209. hasHiddenAttribute(accessible) {
  210. let hidden = false;
  211. try {
  212. hidden = accessible.attributes.getStringProperty("hidden");
  213. } finally {
  214. // if the property is missing, error will be thrown
  215. return hidden && hidden === "true";
  216. }
  217. }
  218. /**
  219. * Verify if an accessible has a given state.
  220. * Test if an accessible has a given state.
  221. *
  222. * @param {nsIAccessible} accessible
  223. * Accessible object to test.
  224. * @param {number} stateToMatch
  225. * State to match.
  226. *
  227. * @return {boolean}
  228. * True if |accessible| has |stateToMatch|, false otherwise.
  229. */
  230. matchState(accessible, stateToMatch) {
  231. let state = {};
  232. accessible.getState(state, {});
  233. return !!(state.value & stateToMatch);
  234. }
  235. /**
  236. * Test if an accessible is hidden from the user.
  237. *
  238. * @param {nsIAccessible} accessible
  239. * Accessible object.
  240. *
  241. * @return {boolean}
  242. * True if element is hidden from user, false otherwise.
  243. */
  244. isHidden(accessible) {
  245. while (accessible) {
  246. if (this.hasHiddenAttribute(accessible)) {
  247. return true;
  248. }
  249. accessible = accessible.parent;
  250. }
  251. return false;
  252. }
  253. /**
  254. * Test if the element's visible state corresponds to its accessibility
  255. * API visibility.
  256. *
  257. * @param {nsIAccessible} accessible
  258. * Accessible object.
  259. * @param {DOMElement|XULElement} element
  260. * Element associated with |accessible|.
  261. * @param {boolean} visible
  262. * Visibility state of |element|.
  263. *
  264. * @throws ElementNotAccessibleError
  265. * If |element|'s visibility state does not correspond to
  266. * |accessible|'s.
  267. */
  268. assertVisible(accessible, element, visible) {
  269. if (!accessible) {
  270. return;
  271. }
  272. let hiddenAccessibility = this.isHidden(accessible);
  273. let message;
  274. if (visible && hiddenAccessibility) {
  275. message = "Element is not currently visible via the accessibility API " +
  276. "and may not be manipulated by it";
  277. } else if (!visible && !hiddenAccessibility) {
  278. message = "Element is currently only visible via the accessibility API " +
  279. "and can be manipulated by it";
  280. }
  281. this.error(message, element);
  282. }
  283. /**
  284. * Test if the element's unavailable accessibility state matches the
  285. * enabled state.
  286. *
  287. * @param {nsIAccessible} accessible
  288. * Accessible object.
  289. * @param {DOMElement|XULElement} element
  290. * Element associated with |accessible|.
  291. * @param {boolean} enabled
  292. * Enabled state of |element|.
  293. *
  294. * @throws ElementNotAccessibleError
  295. * If |element|'s enabled state does not match |accessible|'s.
  296. */
  297. assertEnabled(accessible, element, enabled) {
  298. if (!accessible) {
  299. return;
  300. }
  301. let win = element.ownerDocument.defaultView;
  302. let disabledAccessibility = this.matchState(
  303. accessible, accessibility.State.Unavailable);
  304. let explorable = win.getComputedStyle(element)
  305. .getPropertyValue("pointer-events") !== "none";
  306. let message;
  307. if (!explorable && !disabledAccessibility) {
  308. message = "Element is enabled but is not explorable via the " +
  309. "accessibility API";
  310. } else if (enabled && disabledAccessibility) {
  311. message = "Element is enabled but disabled via the accessibility API";
  312. } else if (!enabled && !disabledAccessibility) {
  313. message = "Element is disabled but enabled via the accessibility API";
  314. }
  315. this.error(message, element);
  316. }
  317. /**
  318. * Test if it is possible to activate an element with the accessibility
  319. * API.
  320. *
  321. * @param {nsIAccessible} accessible
  322. * Accessible object.
  323. * @param {DOMElement|XULElement} element
  324. * Element associated with |accessible|.
  325. *
  326. * @throws ElementNotAccessibleError
  327. * If it is impossible to activate |element| with |accessible|.
  328. */
  329. assertActionable(accessible, element) {
  330. if (!accessible) {
  331. return;
  332. }
  333. let message;
  334. if (!this.hasActionCount(accessible)) {
  335. message = "Element does not support any accessible actions";
  336. } else if (!this.isActionableRole(accessible)) {
  337. message = "Element does not have a correct accessibility role " +
  338. "and may not be manipulated via the accessibility API";
  339. } else if (!this.hasValidName(accessible)) {
  340. message = "Element is missing an accessible name";
  341. } else if (!this.matchState(accessible, accessibility.State.Focusable)) {
  342. message = "Element is not focusable via the accessibility API";
  343. }
  344. this.error(message, element);
  345. }
  346. /**
  347. * Test that an element's selected state corresponds to its
  348. * accessibility API selected state.
  349. *
  350. * @param {nsIAccessible} accessible
  351. * Accessible object.
  352. * @param {DOMElement|XULElement}
  353. * Element associated with |accessible|.
  354. * @param {boolean} selected
  355. * The |element|s selected state.
  356. *
  357. * @throws ElementNotAccessibleError
  358. * If |element|'s selected state does not correspond to
  359. * |accessible|'s.
  360. */
  361. assertSelected(accessible, element, selected) {
  362. if (!accessible) {
  363. return;
  364. }
  365. // element is not selectable via the accessibility API
  366. if (!this.matchState(accessible, accessibility.State.Selectable)) {
  367. return;
  368. }
  369. let selectedAccessibility = this.matchState(accessible, accessibility.State.Selected);
  370. let message;
  371. if (selected && !selectedAccessibility) {
  372. message = "Element is selected but not selected via the accessibility API";
  373. } else if (!selected && selectedAccessibility) {
  374. message = "Element is not selected but selected via the accessibility API";
  375. }
  376. this.error(message, element);
  377. }
  378. /**
  379. * Throw an error if strict accessibility checks are enforced and log
  380. * the error to the log.
  381. *
  382. * @param {string} message
  383. * @param {DOMElement|XULElement} element
  384. * Element that caused an error.
  385. *
  386. * @throws ElementNotAccessibleError
  387. * If |strict| is true.
  388. */
  389. error(message, element) {
  390. if (!message || !this.strict) {
  391. return;
  392. }
  393. if (element) {
  394. let {id, tagName, className} = element;
  395. message += `: id: ${id}, tagName: ${tagName}, className: ${className}`;
  396. }
  397. throw new ElementNotAccessibleError(message);
  398. }
  399. };