summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/markup/test/helper_markup_accessibility_navigation.js
blob: a49f1e7ba3a54a7430aa6ef06ef92405d804e078 (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
/* 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/. */
/* eslint no-unused-vars: [2, {"vars": "local"}] */
/* import-globals-from head.js */
"use strict";

/**
 * Execute a keyboard event and check that the state is as expected (focused element, aria
 * attribute etc...).
 *
 * @param {InspectorPanel} inspector
 *        Current instance of the inspector being tested.
 * @param {Object} elms
 *        Map of elements that will be used to retrieve live references to children
 *        elements
 * @param {Element} focused
 *        Element expected to be focused
 * @param {Element} activedescendant
 *        Element expected to be the aria activedescendant of the root node
 */
function testNavigationState(inspector, elms, focused, activedescendant) {
  let doc = inspector.markup.doc;
  let id = activedescendant.getAttribute("id");
  is(doc.activeElement, focused, `Keyboard focus should be set to ${focused}`);
  is(elms.root.elt.getAttribute("aria-activedescendant"), id,
    `Active descendant should be set to ${id}`);
}

/**
 * Execute a keyboard event and check that the state is as expected (focused element, aria
 * attribute etc...).
 *
 * @param {InspectorPanel} inspector
 *        Current instance of the inspector being tested.
 * @param {Object} elms
 *        MarkupContainers/Elements that will be used to retrieve references to other
 *        elements based on objects' paths.
 * @param {Object} testData
 *        - {String} desc: description for better logging.
 *        - {String} key: keyboard event's key.
 *        - {Object} options, optional: event data such as shiftKey, etc.
 *        - {String} focused: path to expected focused element in elms map.
 *        - {String} activedescendant: path to expected aria-activedescendant element in
 *          elms map.
 *        - {String} waitFor, optional: markupview event to wait for if keyboard actions
 *          result in async updates. Also accepts the inspector event "inspector-updated".
 */
function* runAccessibilityNavigationTest(inspector, elms,
  {desc, key, options, focused, activedescendant, waitFor}) {
  info(desc);

  let markup = inspector.markup;
  let doc = markup.doc;
  let win = doc.defaultView;

  let updated;
  if (waitFor) {
    updated = waitFor === "inspector-updated" ?
      inspector.once(waitFor) : markup.once(waitFor);
  } else {
    updated = Promise.resolve();
  }
  EventUtils.synthesizeKey(key, options, win);
  yield updated;

  let focusedElement = lookupPath(elms, focused);
  let activeDescendantElement = lookupPath(elms, activedescendant);
  testNavigationState(inspector, elms, focusedElement, activeDescendantElement);
}