summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/browser/browser_accessibility_node_audit.js
blob: a3115a28462d1a7706d5f9f2d832511947fe445b (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
/* 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";

/**
 * Checks functionality around audit for the AccessibleActor. This includes
 * tests for the return value when calling the audit method, payload of the
 * corresponding event as well as the AccesibleFront state being up to date.
 */

const {
  accessibility: { AUDIT_TYPE, SCORES },
} = require("resource://devtools/shared/constants.js");
const EMPTY_AUDIT = Object.keys(AUDIT_TYPE).reduce((audit, key) => {
  audit[key] = null;
  return audit;
}, {});

const EXPECTED_CONTRAST_DATA = {
  value: 21,
  color: [0, 0, 0, 1],
  backgroundColor: [255, 255, 255, 1],
  isLargeText: true,
  score: SCORES.AAA,
};

const EMPTY_CONTRAST_AUDIT = {
  [AUDIT_TYPE.CONTRAST]: null,
};

const CONTRAST_AUDIT = {
  [AUDIT_TYPE.CONTRAST]: EXPECTED_CONTRAST_DATA,
};

const FULL_AUDIT = {
  ...EMPTY_AUDIT,
  [AUDIT_TYPE.CONTRAST]: EXPECTED_CONTRAST_DATA,
};

async function checkAudit(a11yWalker, node, expected, options) {
  const front = await a11yWalker.getAccessibleFor(node);
  const [textLeafNode] = await front.children();

  const onAudited = textLeafNode.once("audited");
  const audit = await textLeafNode.audit(options);
  const auditFromEvent = await onAudited;

  Assert.deepEqual(audit, expected.audit, "Audit results are correct.");
  Assert.deepEqual(textLeafNode.checks, expected.checks, "Checks are correct.");
  Assert.deepEqual(
    auditFromEvent,
    expected.audit,
    "Audit results from event are correct."
  );
}

add_task(async function () {
  const { target, walker, a11yWalker, parentAccessibility } =
    await initAccessibilityFrontsForUrl(
      MAIN_DOMAIN + "doc_accessibility_infobar.html"
    );

  const headerNode = await walker.querySelector(walker.rootNode, "#h1");
  await checkAudit(
    a11yWalker,
    headerNode,
    { audit: CONTRAST_AUDIT, checks: CONTRAST_AUDIT },
    { types: [AUDIT_TYPE.CONTRAST] }
  );
  await checkAudit(a11yWalker, headerNode, {
    audit: FULL_AUDIT,
    checks: FULL_AUDIT,
  });
  await checkAudit(
    a11yWalker,
    headerNode,
    { audit: CONTRAST_AUDIT, checks: FULL_AUDIT },
    { types: [AUDIT_TYPE.CONTRAST] }
  );
  await checkAudit(
    a11yWalker,
    headerNode,
    { audit: FULL_AUDIT, checks: FULL_AUDIT },
    { types: [] }
  );

  const paragraphNode = await walker.querySelector(walker.rootNode, "#p");
  await checkAudit(
    a11yWalker,
    paragraphNode,
    { audit: EMPTY_CONTRAST_AUDIT, checks: EMPTY_CONTRAST_AUDIT },
    { types: [AUDIT_TYPE.CONTRAST] }
  );
  await checkAudit(a11yWalker, paragraphNode, {
    audit: EMPTY_AUDIT,
    checks: EMPTY_AUDIT,
  });
  await checkAudit(
    a11yWalker,
    paragraphNode,
    { audit: EMPTY_CONTRAST_AUDIT, checks: EMPTY_AUDIT },
    { types: [AUDIT_TYPE.CONTRAST] }
  );
  await checkAudit(
    a11yWalker,
    paragraphNode,
    { audit: EMPTY_AUDIT, checks: EMPTY_AUDIT },
    { types: [] }
  );

  await waitForA11yShutdown(parentAccessibility);
  await target.destroy();
  gBrowser.removeCurrentTab();
});