browser_accessibility_walker_audit.js (4537B)
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 const { 8 accessibility: { AUDIT_TYPE, ISSUE_TYPE, SCORES }, 9 } = require("resource://devtools/shared/constants.js"); 10 11 // Checks for the AccessibleWalkerActor audit. 12 add_task(async function () { 13 const { target, a11yWalker, parentAccessibility } = 14 await initAccessibilityFrontsForUrl( 15 MAIN_DOMAIN + "doc_accessibility_audit.html" 16 ); 17 18 const accessibles = [ 19 { 20 name: "", 21 role: "document", 22 childCount: 2, 23 checks: { 24 [AUDIT_TYPE.CONTRAST]: null, 25 [AUDIT_TYPE.KEYBOARD]: null, 26 [AUDIT_TYPE.TEXT_LABEL]: { 27 score: SCORES.FAIL, 28 issue: ISSUE_TYPE.DOCUMENT_NO_TITLE, 29 }, 30 }, 31 }, 32 { 33 name: 34 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " + 35 "eiusmod tempor incididunt ut labore et dolore magna aliqua.", 36 role: "paragraph", 37 childCount: 1, 38 checks: { 39 [AUDIT_TYPE.CONTRAST]: null, 40 [AUDIT_TYPE.KEYBOARD]: null, 41 [AUDIT_TYPE.TEXT_LABEL]: null, 42 }, 43 }, 44 { 45 name: 46 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " + 47 "eiusmod tempor incididunt ut labore et dolore magna aliqua.", 48 role: "text leaf", 49 childCount: 0, 50 checks: { 51 [AUDIT_TYPE.CONTRAST]: { 52 value: 4.0, 53 color: [255, 0, 0, 1], 54 backgroundColor: [255, 255, 255, 1], 55 isLargeText: false, 56 score: SCORES.FAIL, 57 }, 58 [AUDIT_TYPE.KEYBOARD]: null, 59 [AUDIT_TYPE.TEXT_LABEL]: null, 60 }, 61 }, 62 { 63 name: "", 64 role: "paragraph", 65 childCount: 1, 66 checks: { 67 [AUDIT_TYPE.CONTRAST]: null, 68 [AUDIT_TYPE.KEYBOARD]: null, 69 [AUDIT_TYPE.TEXT_LABEL]: null, 70 }, 71 }, 72 { 73 name: "Accessible Paragraph", 74 role: "text leaf", 75 childCount: 0, 76 checks: { 77 [AUDIT_TYPE.CONTRAST]: { 78 value: 4.0, 79 color: [255, 0, 0, 1], 80 backgroundColor: [255, 255, 255, 1], 81 isLargeText: false, 82 score: SCORES.FAIL, 83 }, 84 [AUDIT_TYPE.KEYBOARD]: null, 85 [AUDIT_TYPE.TEXT_LABEL]: null, 86 }, 87 }, 88 ]; 89 const total = accessibles.length; 90 const auditProgress = [ 91 { total, percentage: 20, completed: 1 }, 92 { total, percentage: 40, completed: 2 }, 93 { total, percentage: 60, completed: 3 }, 94 { total, percentage: 80, completed: 4 }, 95 { total, percentage: 100, completed: 5 }, 96 ]; 97 98 function findAccessible(name, role) { 99 return accessibles.find( 100 accessible => accessible.name === name && accessible.role === role 101 ); 102 } 103 104 async function checkWalkerAudit(walker, expectedSize, options) { 105 info("Checking AccessibleWalker audit functionality"); 106 const expectedProgress = Array.from(auditProgress); 107 const ancestries = await new Promise((resolve, reject) => { 108 const auditEventHandler = ({ type, ancestries: response, progress }) => { 109 switch (type) { 110 case "error": 111 walker.off("audit-event", auditEventHandler); 112 reject(); 113 break; 114 case "completed": 115 walker.off("audit-event", auditEventHandler); 116 resolve(response); 117 is(expectedProgress.length, 0, "All progress events fired"); 118 break; 119 case "progress": 120 SimpleTest.isDeeply( 121 progress, 122 expectedProgress.shift(), 123 "Progress data is correct" 124 ); 125 break; 126 default: 127 break; 128 } 129 }; 130 131 walker.on("audit-event", auditEventHandler); 132 walker.startAudit(options); 133 }); 134 135 is(ancestries.length, expectedSize, "The size of ancestries is correct"); 136 for (const ancestry of ancestries) { 137 for (const { accessible, children } of ancestry) { 138 checkA11yFront( 139 accessible, 140 findAccessible(accessibles.name, accessibles.role) 141 ); 142 for (const child of children) { 143 checkA11yFront(child, findAccessible(child.name, child.role)); 144 } 145 } 146 } 147 } 148 149 await checkWalkerAudit(a11yWalker, 3); 150 await checkWalkerAudit(a11yWalker, 2, { types: [AUDIT_TYPE.CONTRAST] }); 151 152 await waitForA11yShutdown(parentAccessibility); 153 await target.destroy(); 154 gBrowser.removeCurrentTab(); 155 });