browser_link.js (6502B)
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 /* import-globals-from ../../mochitest/role.js */ 8 /* import-globals-from ../../mochitest/states.js */ 9 loadScripts( 10 { name: "role.js", dir: MOCHITESTS_DIR }, 11 { name: "states.js", dir: MOCHITESTS_DIR } 12 ); 13 14 ChromeUtils.defineESModuleGetters(this, { 15 PlacesTestUtils: "resource://testing-common/PlacesTestUtils.sys.mjs", 16 }); 17 18 /** 19 * Test visited link properties. 20 */ 21 addAccessibleTask( 22 ` 23 <a id="link" href="http://www.example.com/">I am a non-visited link</a><br> 24 `, 25 async (browser, accDoc) => { 26 let link = getNativeInterface(accDoc, "link"); 27 let stateChanged = waitForEvent(EVENT_STATE_CHANGE, "link"); 28 29 is(link.getAttributeValue("AXVisited"), 0, "Link has not been visited"); 30 31 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 32 await PlacesTestUtils.addVisits(["http://www.example.com/"]); 33 34 await stateChanged; 35 is(link.getAttributeValue("AXVisited"), 1, "Link has been visited"); 36 37 // Ensure history is cleared before running 38 await PlacesUtils.history.clear(); 39 } 40 ); 41 42 /** 43 * Test linked vs unlinked anchor tags 44 */ 45 addAccessibleTask( 46 ` 47 <a id="link1" href="#">I am a link link</a> 48 <a id="link2" onclick="console.log('hi')">I am a link-ish link</a> 49 <a id="link3">I am a non-link link</a> 50 `, 51 async (browser, accDoc) => { 52 let link1 = getNativeInterface(accDoc, "link1"); 53 is( 54 link1.getAttributeValue("AXRole"), 55 "AXLink", 56 "a[href] gets correct link role" 57 ); 58 ok( 59 link1.attributeNames.includes("AXVisited"), 60 "Link has visited attribute" 61 ); 62 ok(link1.attributeNames.includes("AXURL"), "Link has URL attribute"); 63 64 let link2 = getNativeInterface(accDoc, "link2"); 65 is( 66 link2.getAttributeValue("AXRole"), 67 "AXLink", 68 "a[onclick] gets correct link role" 69 ); 70 ok( 71 link2.attributeNames.includes("AXVisited"), 72 "Link has visited attribute" 73 ); 74 ok(link2.attributeNames.includes("AXURL"), "Link has URL attribute"); 75 76 let link3 = getNativeInterface(accDoc, "link3"); 77 is( 78 link3.getAttributeValue("AXRole"), 79 "AXGroup", 80 "bare <a> gets correct group role" 81 ); 82 83 let onRecreation = waitForEvent(EVENT_SHOW, "link1"); 84 await SpecialPowers.spawn(browser, [], () => { 85 content.document.getElementById("link1").removeAttribute("href"); 86 }); 87 await onRecreation; 88 link1 = getNativeInterface(accDoc, "link1"); 89 is( 90 link1.getAttributeValue("AXRole"), 91 "AXGroup", 92 "<a> stripped from href gets group role" 93 ); 94 95 onRecreation = waitForEvent(EVENT_SHOW, "link2"); 96 await SpecialPowers.spawn(browser, [], () => { 97 content.document.getElementById("link2").removeAttribute("onclick"); 98 }); 99 await onRecreation; 100 link2 = getNativeInterface(accDoc, "link2"); 101 is( 102 link2.getAttributeValue("AXRole"), 103 "AXGroup", 104 "<a> stripped from onclick gets group role" 105 ); 106 107 onRecreation = waitForEvent(EVENT_SHOW, "link3"); 108 await SpecialPowers.spawn(browser, [], () => { 109 content.document 110 .getElementById("link3") 111 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 112 .setAttribute("href", "http://example.com"); 113 }); 114 await onRecreation; 115 link3 = getNativeInterface(accDoc, "link3"); 116 is( 117 link3.getAttributeValue("AXRole"), 118 "AXLink", 119 "href added to bare a gets link role" 120 ); 121 122 ok( 123 link3.attributeNames.includes("AXVisited"), 124 "Link has visited attribute" 125 ); 126 ok(link3.attributeNames.includes("AXURL"), "Link has URL attribute"); 127 } 128 ); 129 130 /** 131 * Test anchors and linked ui elements attr 132 */ 133 addAccessibleTask( 134 ` 135 <a id="link0" href="http://example.com">I am a link</a> 136 <a id="link1" href="#">I am a link with an empty anchor</a> 137 <a id="link2" href="#hello">I am a link with no corresponding element</a> 138 <a id="link3" href="#world">I am a link with a corresponding element</a> 139 <a id="link4" href="#empty">I jump to an empty element</a> 140 <a id="link5" href="#namedElem">I jump to a named element</a> 141 <a id="link6" href="#emptyNamed">I jump to an empty named element</a> 142 <h1 id="world">I am that element</h1> 143 <h2 id="empty"></h2> 144 <a name="namedElem">I have a name</a> 145 <a name="emptyNamed"></a> 146 <h3>I have no name and no ID</h3> 147 <h4></h4> 148 `, 149 async (browser, accDoc) => { 150 let link0 = getNativeInterface(accDoc, "link0"); 151 let link1 = getNativeInterface(accDoc, "link1"); 152 let link2 = getNativeInterface(accDoc, "link2"); 153 let link3 = getNativeInterface(accDoc, "link3"); 154 let link4 = getNativeInterface(accDoc, "link4"); 155 let link5 = getNativeInterface(accDoc, "link5"); 156 let link6 = getNativeInterface(accDoc, "link6"); 157 158 is( 159 link0.getAttributeValue("AXLinkedUIElements").length, 160 0, 161 "Link 0 has no linked UI elements" 162 ); 163 is( 164 link1.getAttributeValue("AXLinkedUIElements").length, 165 0, 166 "Link 1 has no linked UI elements" 167 ); 168 is( 169 link2.getAttributeValue("AXLinkedUIElements").length, 170 0, 171 "Link 2 has no linked UI elements" 172 ); 173 is( 174 link3.getAttributeValue("AXLinkedUIElements").length, 175 1, 176 "Link 3 has one linked UI element" 177 ); 178 is( 179 link3 180 .getAttributeValue("AXLinkedUIElements")[0] 181 .getAttributeValue("AXTitle"), 182 "I am that element", 183 "Link 3 is linked to the heading" 184 ); 185 is( 186 link4.getAttributeValue("AXLinkedUIElements").length, 187 1, 188 "Link 4 has one linked UI element" 189 ); 190 is( 191 link4 192 .getAttributeValue("AXLinkedUIElements")[0] 193 .getAttributeValue("AXTitle"), 194 null, 195 "Link 4 is linked to the heading" 196 ); 197 is( 198 link5.getAttributeValue("AXLinkedUIElements").length, 199 1, 200 "Link 5 has one linked UI element" 201 ); 202 is( 203 link5 204 .getAttributeValue("AXLinkedUIElements")[0] 205 .getAttributeValue("AXTitle"), 206 "", 207 "Link 5 is linked to a named element" 208 ); 209 is( 210 link6.getAttributeValue("AXLinkedUIElements").length, 211 1, 212 "Link 6 has one linked UI element" 213 ); 214 is( 215 link6 216 .getAttributeValue("AXLinkedUIElements")[0] 217 .getAttributeValue("AXTitle"), 218 "", 219 "Link 6 is linked to an empty named element" 220 ); 221 } 222 );