Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit a26dda1

Browse files
committed
Bug 1541446 - added audited event to AccessibleActor/Front. r=pbro
Differential Revision: https://phabricator.services.mozilla.com/D26457 --HG-- extra : moz-landing-system : lando
1 parent b71ac3d commit a26dda1

10 files changed

Lines changed: 115 additions & 12 deletions

File tree

devtools/client/accessibility/components/Checks.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const ColorContrastCheck =
1414
createFactory(require("./ColorContrastAccessibility").ColorContrastCheck);
1515
const { L10N } = require("../utils/l10n");
1616

17+
const { accessibility: { AUDIT_TYPE } } = require("devtools/shared/constants");
18+
1719
function EmptyChecks() {
1820
return (
1921
div({
@@ -33,7 +35,7 @@ class Checks extends Component {
3335
};
3436
}
3537

36-
contrastRatio(contrastRatio) {
38+
[AUDIT_TYPE.CONTRAST](contrastRatio) {
3739
return ColorContrastCheck(contrastRatio);
3840
}
3941

devtools/client/accessibility/test/browser/browser_accessibility_sidebar_checks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const TEST_URI = `<html>
2727
const tests = [{
2828
desc: "Test the initial accessibility audit state.",
2929
expected: {
30-
audit: { contrastRatio: null },
30+
audit: { CONTRAST: null },
3131
},
3232
}, {
3333
desc: "Check accessible representing text node in red.",
@@ -38,7 +38,7 @@ const tests = [{
3838
},
3939
expected: {
4040
audit: {
41-
"contrastRatio": {
41+
"CONTRAST": {
4242
"value": 4.00,
4343
"color": [255, 0, 0, 1],
4444
"backgroundColor": [255, 255, 255, 1],
@@ -54,7 +54,7 @@ const tests = [{
5454
},
5555
expected: {
5656
audit: {
57-
"contrastRatio": {
57+
"CONTRAST": {
5858
"value": 8.59,
5959
"color": [0, 0, 255, 1],
6060
"backgroundColor": [255, 255, 255, 1],

devtools/server/actors/accessibility/accessible.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
const { Ci, Cu } = require("chrome");
88
const { Actor, ActorClassWithSpec } = require("devtools/shared/protocol");
99
const { accessibleSpec } = require("devtools/shared/specs/accessibility");
10+
const { accessibility: { AUDIT_TYPE } } = require("devtools/shared/constants");
1011

1112
loader.lazyRequireGetter(this, "getContrastRatioFor", "devtools/server/actors/accessibility/contrast", true);
1213
loader.lazyRequireGetter(this, "isDefunct", "devtools/server/actors/utils/accessibility", true);
1314
loader.lazyRequireGetter(this, "findCssSelector", "devtools/shared/inspector/css-logic", true);
15+
loader.lazyRequireGetter(this, "events", "devtools/shared/event-emitter");
1416

1517
const RELATIONS_TO_IGNORE = new Set([
1618
Ci.nsIAccessibleRelation.RELATION_CONTAINING_APPLICATION,
@@ -155,6 +157,10 @@ const AccessibleActor = ActorClassWithSpec(accessibleSpec, {
155157
this.rawAccessible = null;
156158
},
157159

160+
get isDestroyed() {
161+
return this.actorID == null;
162+
},
163+
158164
get role() {
159165
if (this.isDefunct) {
160166
return null;
@@ -369,6 +375,7 @@ const AccessibleActor = ActorClassWithSpec(accessibleSpec, {
369375
states: this.states,
370376
actions: this.actions,
371377
attributes: this.attributes,
378+
checks: this._lastAudit,
372379
};
373380
},
374381

@@ -388,13 +395,16 @@ const AccessibleActor = ActorClassWithSpec(accessibleSpec, {
388395

389396
const { DOMNode: rawNode } = this.rawAccessible;
390397
const win = rawNode.ownerGlobal;
391-
this.walker.clearStyles(win);
398+
// Keep the reference to the walker actor in case the actor gets destroyed
399+
// during the colour contrast ratio calculation.
400+
const { walker } = this;
401+
walker.clearStyles(win);
392402
const contrastRatio = await getContrastRatioFor(rawNode.parentNode, {
393403
bounds: this.bounds,
394404
win,
395405
});
396406

397-
this.walker.restoreStyles(win);
407+
walker.restoreStyles(win);
398408

399409
return contrastRatio;
400410
},
@@ -405,7 +415,7 @@ const AccessibleActor = ActorClassWithSpec(accessibleSpec, {
405415
* @return {Object|null}
406416
* Audit results for the accessible object.
407417
*/
408-
async audit() {
418+
audit() {
409419
if (this._auditing) {
410420
return this._auditing;
411421
}
@@ -418,12 +428,23 @@ const AccessibleActor = ActorClassWithSpec(accessibleSpec, {
418428
]).then(([
419429
contrastRatio,
420430
]) => {
421-
const audit = this.isDefunct ? null : {
422-
contrastRatio,
423-
};
431+
let audit = null;
432+
if (!this.isDefunct && !this.isDestroyed) {
433+
audit = {
434+
[AUDIT_TYPE.CONTRAST]: contrastRatio,
435+
};
436+
this._lastAudit = audit;
437+
events.emit(this, "audited", audit);
438+
}
424439

425-
this._auditing = null;
426440
return audit;
441+
}).catch(error => {
442+
if (!this.isDefunct && !this.isDestroyed) {
443+
throw error;
444+
}
445+
return null;
446+
}).finally(() => {
447+
this._auditing = null;
427448
});
428449

429450
return this._auditing;

devtools/server/actors/highlighters/utils/accessibility.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const STRINGS_URI = "devtools/shared/locales/accessibility.properties";
1313
loader.lazyRequireGetter(this, "LocalizationHelper", "devtools/shared/l10n", true);
1414
DevToolsUtils.defineLazyGetter(this, "L10N", () => new LocalizationHelper(STRINGS_URI));
1515

16+
const { accessibility: { AUDIT_TYPE } } = require("devtools/shared/constants");
17+
1618
// Max string length for truncating accessible name values.
1719
const MAX_STRING_LENGTH = 50;
1820

@@ -542,7 +544,7 @@ class ContrastRatio extends AuditReport {
542544
* True if the contrast ratio markup was updated correctly and infobar audit
543545
* block should be visible.
544546
*/
545-
update({ contrastRatio }) {
547+
update({ [AUDIT_TYPE.CONTRAST]: contrastRatio }) {
546548
const els = {};
547549
for (const key of ["label", "min", "max", "error", "separator"]) {
548550
const el = els[key] = this.getElement(`contrast-ratio-${key}`);

devtools/server/tests/browser/browser.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ skip-if = (os == 'win' && processor == 'aarch64') # bug 1533184
4242
[browser_accessibility_infobar_show.js]
4343
[browser_accessibility_node.js]
4444
skip-if = (os == 'win' && processor == 'aarch64') # bug 1533184
45+
[browser_accessibility_node_audit.js]
4546
[browser_accessibility_node_events.js]
4647
skip-if = (os == 'win' && processor == 'aarch64') # bug 1533184
4748
[browser_accessibility_simple.js]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
"use strict";
6+
7+
/**
8+
* Checks functionality around audit for the AccessibleActor. This includes
9+
* tests for the return value when calling the audit method, payload of the
10+
* corresponding event as well as the AccesibleFront state being up to date.
11+
*/
12+
13+
add_task(async function() {
14+
const {target, walker, accessibility} =
15+
await initAccessibilityFrontForUrl(MAIN_DOMAIN + "doc_accessibility_infobar.html");
16+
17+
const a11yWalker = await accessibility.getWalker();
18+
await accessibility.enable();
19+
const buttonNode = await walker.querySelector(walker.rootNode, "#button");
20+
const buttonFront = await a11yWalker.getAccessibleFor(buttonNode);
21+
const [textLeafNode] = await buttonFront.children();
22+
23+
const onAudited = textLeafNode.once("audited");
24+
const audit = await textLeafNode.audit();
25+
const auditFromEvent = await onAudited;
26+
27+
const expectedAudit = {
28+
"CONTRAST": {
29+
"value": 21,
30+
"color": [0, 0, 0, 1],
31+
"backgroundColor": [255, 255, 255, 1],
32+
"isLargeText": false,
33+
},
34+
};
35+
36+
Assert.deepEqual(audit, expectedAudit, "Audit results are correct.");
37+
Assert.deepEqual(textLeafNode.checks, expectedAudit, "Checks are correct.");
38+
Assert.deepEqual(auditFromEvent, expectedAudit,
39+
"Audit results from event are correct.");
40+
41+
await accessibility.disable();
42+
await waitForA11yShutdown();
43+
await target.destroy();
44+
gBrowser.removeCurrentTab();
45+
});

devtools/shared/constants.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
"use strict";
6+
7+
/**
8+
* Constants used in various panels, shared between client and the server.
9+
*/
10+
11+
/* Accessibility Panel ====================================================== */
12+
13+
exports.accessibility = {
14+
// List of audit types.
15+
AUDIT_TYPE: {
16+
CONTRAST: "CONTRAST",
17+
},
18+
};

devtools/shared/fronts/accessibility.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class AccessibleFront extends FrontClassWithSpec(accessibleSpec) {
1818
constructor(client) {
1919
super(client);
2020

21+
this.before("audited", this.audited.bind(this));
2122
this.before("name-change", this.nameChange.bind(this));
2223
this.before("value-change", this.valueChange.bind(this));
2324
this.before("description-change", this.descriptionChange.bind(this));
@@ -78,6 +79,10 @@ class AccessibleFront extends FrontClassWithSpec(accessibleSpec) {
7879
return this._form.attributes;
7980
}
8081

82+
get checks() {
83+
return this._form.checks;
84+
}
85+
8186
form(form) {
8287
this.actorID = form.actor;
8388
this._form = form;
@@ -136,6 +141,10 @@ class AccessibleFront extends FrontClassWithSpec(accessibleSpec) {
136141
attributesChange(attributes) {
137142
this._form.attributes = attributes;
138143
}
144+
145+
audited(checks) {
146+
this._form.checks = checks;
147+
}
139148
}
140149

141150
class AccessibleWalkerFront extends FrontClassWithSpec(accessibleWalkerSpec) {

devtools/shared/moz.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ DevToolsModules(
4747
'async-utils.js',
4848
'base-loader.js',
4949
'builtin-modules.js',
50+
'constants.js',
5051
'content-observer.js',
5152
'debounce.js',
5253
'defer.js',

devtools/shared/specs/accessibility.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ const accessibleSpec = generateActorSpec({
7777
type: "indexInParentChange",
7878
indexInParent: Arg(0, "number"),
7979
},
80+
"audited": {
81+
type: "audited",
82+
audit: Arg(0, "nullable:json"),
83+
},
8084
},
8185

8286
methods: {

0 commit comments

Comments
 (0)