browser_accessibility_infobar_audit_keyboard.js (5053B)
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 // Checks for the AccessibleHighlighter's infobar component and its keyboard 8 // audit. 9 10 add_task(async function () { 11 await BrowserTestUtils.withNewTab( 12 { 13 gBrowser, 14 url: MAIN_DOMAIN + "doc_accessibility_infobar.html", 15 }, 16 async function (browser) { 17 await SpecialPowers.spawn(browser, [], async function () { 18 const { require } = ChromeUtils.importESModule( 19 "resource://devtools/shared/loader/Loader.sys.mjs" 20 ); 21 const { 22 HighlighterEnvironment, 23 } = require("resource://devtools/server/actors/highlighters.js"); 24 const { 25 AccessibleHighlighter, 26 } = require("resource://devtools/server/actors/highlighters/accessible.js"); 27 const { 28 LocalizationHelper, 29 } = require("resource://devtools/shared/l10n.js"); 30 const L10N = new LocalizationHelper( 31 "devtools/shared/locales/accessibility.properties" 32 ); 33 34 const { 35 accessibility: { 36 AUDIT_TYPE, 37 ISSUE_TYPE: { 38 [AUDIT_TYPE.KEYBOARD]: { 39 INTERACTIVE_NO_ACTION, 40 FOCUSABLE_NO_SEMANTICS, 41 }, 42 }, 43 SCORES: { FAIL, WARNING }, 44 }, 45 } = require("resource://devtools/shared/constants.js"); 46 47 /** 48 * Checks for updated content for an infobar. 49 * 50 * @param {object} infobar 51 * Accessible highlighter's infobar component. 52 * @param {object} audit 53 * Audit information that is passed on highlighter show. 54 */ 55 function checkKeyboard(infobar, audit) { 56 const { issue, score } = audit || {}; 57 let expected = ""; 58 if (issue) { 59 const { ISSUE_TO_INFOBAR_LABEL_MAP } = 60 infobar.audit.reports[AUDIT_TYPE.KEYBOARD].constructor; 61 expected = L10N.getStr(ISSUE_TO_INFOBAR_LABEL_MAP[issue]); 62 } 63 64 is( 65 infobar.getTextContent("accessible-keyboard"), 66 expected, 67 "infobar keyboard audit text content is correct" 68 ); 69 if (score) { 70 ok( 71 infobar 72 .getElement("accessible-keyboard") 73 .classList.contains(score) 74 ); 75 } 76 } 77 78 // Start testing. First, create highlighter environment and initialize. 79 const env = new HighlighterEnvironment(); 80 env.initFromWindow(content.window); 81 82 // Wait for loading highlighter environment content to complete before creating the 83 // highlighter. 84 await new Promise(resolve => { 85 const doc = env.document; 86 87 function onContentLoaded() { 88 if ( 89 doc.readyState === "interactive" || 90 doc.readyState === "complete" 91 ) { 92 resolve(); 93 } else { 94 doc.addEventListener("DOMContentLoaded", onContentLoaded, { 95 once: true, 96 }); 97 } 98 } 99 100 onContentLoaded(); 101 }); 102 103 // Now, we can test the Infobar's audit content. 104 const node = content.document.createElement("div"); 105 content.document.body.append(node); 106 const highlighter = new AccessibleHighlighter(env); 107 await highlighter.isReady; 108 const infobar = highlighter.accessibleInfobar; 109 const bounds = { 110 x: 0, 111 y: 0, 112 w: 250, 113 h: 100, 114 }; 115 116 const tests = [ 117 { 118 desc: "Infobar is shown with no keyboard audit content when no audit.", 119 }, 120 { 121 desc: "Infobar is shown with no keyboard audit content when audit is null.", 122 audit: null, 123 }, 124 { 125 desc: 126 "Infobar is shown with no keyboard audit content when empty " + 127 "keyboard audit.", 128 audit: { [AUDIT_TYPE.KEYBOARD]: null }, 129 }, 130 { 131 desc: "Infobar is shown with keyboard audit content for an error.", 132 audit: { 133 [AUDIT_TYPE.KEYBOARD]: { 134 score: FAIL, 135 issue: INTERACTIVE_NO_ACTION, 136 }, 137 }, 138 }, 139 { 140 desc: "Infobar is shown with keyboard audit content for a warning.", 141 audit: { 142 [AUDIT_TYPE.KEYBOARD]: { 143 score: WARNING, 144 issue: FOCUSABLE_NO_SEMANTICS, 145 }, 146 }, 147 }, 148 ]; 149 150 for (const test of tests) { 151 const { desc, audit } = test; 152 153 info(desc); 154 highlighter.show(node, { ...bounds, audit }); 155 checkKeyboard(infobar, audit && audit[AUDIT_TYPE.KEYBOARD]); 156 highlighter.hide(); 157 } 158 }); 159 } 160 ); 161 });