browser_BrowserTestUtils.js (8795B)
1 function getLastEventDetails(browser) { 2 return SpecialPowers.spawn(browser, [], async function () { 3 return content.document.getElementById("out").textContent; 4 }); 5 } 6 7 add_setup(async function () { 8 await SpecialPowers.pushPrefEnv({ 9 set: [["test.wait300msAfterTabSwitch", true]], 10 }); 11 }); 12 13 add_task(async function () { 14 let onClickEvt = 15 'document.getElementById("out").textContent = event.target.localName + "," + event.clientX + "," + event.clientY;'; 16 const url = 17 "<body onclick='" + 18 onClickEvt + 19 "' style='margin: 0'>" + 20 "<button id='one' style='margin: 0; margin-left: 16px; margin-top: 14px; width: 80px; height: 40px;'>Test</button>" + 21 "<div onmousedown='event.preventDefault()' style='margin: 0; width: 80px; height: 60px;'>Other</div>" + 22 "<span id='out'></span></body>"; 23 let tab = await BrowserTestUtils.openNewForegroundTab( 24 gBrowser, 25 "data:text/html," + url 26 ); 27 28 let browser = tab.linkedBrowser; 29 await BrowserTestUtils.synthesizeMouseAtCenter("#one", {}, browser); 30 let details = await getLastEventDetails(browser); 31 32 is(details, "button,56,34", "synthesizeMouseAtCenter"); 33 34 await BrowserTestUtils.synthesizeMouse("#one", 4, 9, {}, browser); 35 details = await getLastEventDetails(browser); 36 is(details, "button,20,23", "synthesizeMouse"); 37 38 await BrowserTestUtils.synthesizeMouseAtPoint(15, 6, {}, browser); 39 details = await getLastEventDetails(browser); 40 is(details, "body,15,6", "synthesizeMouseAtPoint on body"); 41 42 await BrowserTestUtils.synthesizeMouseAtPoint( 43 20, 44 22, 45 {}, 46 browser.browsingContext 47 ); 48 details = await getLastEventDetails(browser); 49 is(details, "button,20,22", "synthesizeMouseAtPoint on button"); 50 51 await BrowserTestUtils.synthesizeMouseAtCenter("body > div", {}, browser); 52 details = await getLastEventDetails(browser); 53 is(details, "div,40,84", "synthesizeMouseAtCenter with complex selector"); 54 55 let cancelled = await BrowserTestUtils.synthesizeMouseAtCenter( 56 "body > div", 57 { type: "mousedown" }, 58 browser 59 ); 60 details = await getLastEventDetails(browser); 61 is( 62 details, 63 "div,40,84", 64 "synthesizeMouseAtCenter mousedown with complex selector" 65 ); 66 ok( 67 cancelled, 68 "synthesizeMouseAtCenter mousedown with complex selector not cancelled" 69 ); 70 71 cancelled = await BrowserTestUtils.synthesizeMouseAtCenter( 72 "body > div", 73 { type: "mouseup" }, 74 browser 75 ); 76 details = await getLastEventDetails(browser); 77 is( 78 details, 79 "div,40,84", 80 "synthesizeMouseAtCenter mouseup with complex selector" 81 ); 82 ok( 83 !cancelled, 84 "synthesizeMouseAtCenter mouseup with complex selector cancelled" 85 ); 86 87 gBrowser.removeTab(tab); 88 }); 89 90 add_task(async function testSynthesizeMouseAtPointsButtons() { 91 let onMouseEvt = 92 'document.getElementById("mouselog").textContent += "/" + [event.type,event.clientX,event.clientY,event.button,event.buttons].join(",");'; 93 94 let getLastMouseEventDetails = browser => { 95 return SpecialPowers.spawn(browser, [], async () => { 96 let log = content.document.getElementById("mouselog").textContent; 97 content.document.getElementById("mouselog").textContent = ""; 98 return log; 99 }); 100 }; 101 102 const url = 103 "<body" + 104 "' onmousedown='" + 105 onMouseEvt + 106 "' onmousemove='" + 107 onMouseEvt + 108 "' onmouseup='" + 109 onMouseEvt + 110 "' style='margin: 0'>" + 111 "<div style='margin: 0; width: 80px; height: 60px;'>Mouse area</div>" + 112 "<span id='mouselog'></span>" + 113 "</body>"; 114 let tab = await BrowserTestUtils.openNewForegroundTab( 115 gBrowser, 116 "data:text/html," + url 117 ); 118 119 let browser = tab.linkedBrowser; 120 let details; 121 122 await BrowserTestUtils.synthesizeMouseAtPoint( 123 21, 124 22, 125 { 126 type: "mousemove", 127 }, 128 browser.browsingContext 129 ); 130 details = await getLastMouseEventDetails(browser); 131 is(details, "/mousemove,21,22,0,0", "synthesizeMouseAtPoint mousemove"); 132 133 await BrowserTestUtils.synthesizeMouseAtPoint( 134 22, 135 23, 136 {}, 137 browser.browsingContext 138 ); 139 details = await getLastMouseEventDetails(browser); 140 is( 141 details, 142 "/mousedown,22,23,0,1/mouseup,22,23,0,0", 143 "synthesizeMouseAtPoint default action includes buttons on mousedown only" 144 ); 145 146 await BrowserTestUtils.synthesizeMouseAtPoint( 147 20, 148 22, 149 { 150 type: "mousedown", 151 }, 152 browser.browsingContext 153 ); 154 details = await getLastMouseEventDetails(browser); 155 is( 156 details, 157 "/mousedown,20,22,0,1", 158 "synthesizeMouseAtPoint mousedown includes buttons" 159 ); 160 161 await BrowserTestUtils.synthesizeMouseAtPoint( 162 21, 163 20, 164 { 165 type: "mouseup", 166 }, 167 browser.browsingContext 168 ); 169 details = await getLastMouseEventDetails(browser); 170 is(details, "/mouseup,21,20,0,0", "synthesizeMouseAtPoint mouseup"); 171 172 await BrowserTestUtils.synthesizeMouseAtPoint( 173 20, 174 22, 175 { 176 type: "mousedown", 177 button: 2, 178 }, 179 browser.browsingContext 180 ); 181 details = await getLastMouseEventDetails(browser); 182 is( 183 details, 184 "/mousedown,20,22,2,2", 185 186 "synthesizeMouseAtPoint mousedown respects specified button 2" 187 ); 188 189 await BrowserTestUtils.synthesizeMouseAtPoint( 190 21, 191 20, 192 { 193 type: "mouseup", 194 button: 2, 195 }, 196 browser.browsingContext 197 ); 198 details = await getLastMouseEventDetails(browser); 199 is( 200 details, 201 "/mouseup,21,20,2,0", 202 "synthesizeMouseAtPoint mouseup with button 2" 203 ); 204 205 gBrowser.removeTab(tab); 206 }); 207 208 add_task(async function mouse_in_iframe() { 209 let onClickEvt = "document.body.lastChild.textContent = event.target.id;"; 210 const url = `<iframe style='margin: 30px;' src='data:text/html,<body onclick="${onClickEvt}"> 211 <p><button>One</button></p><p><button id="two">Two</button></p><p id="out"></p></body>'></iframe> 212 <iframe style='margin: 10px;' src='data:text/html,<body onclick="${onClickEvt}"> 213 <p><button>Three</button></p><p><button id="four">Four</button></p><p id="out"></p></body>'></iframe>`; 214 let tab = await BrowserTestUtils.openNewForegroundTab( 215 gBrowser, 216 "data:text/html," + url 217 ); 218 219 let browser = tab.linkedBrowser; 220 221 await BrowserTestUtils.synthesizeMouse( 222 "#two", 223 5, 224 10, 225 {}, 226 browser.browsingContext.children[0] 227 ); 228 229 let details = await getLastEventDetails(browser.browsingContext.children[0]); 230 is(details, "two", "synthesizeMouse"); 231 232 await BrowserTestUtils.synthesizeMouse( 233 "#four", 234 5, 235 10, 236 {}, 237 browser.browsingContext.children[1] 238 ); 239 details = await getLastEventDetails(browser.browsingContext.children[1]); 240 is(details, "four", "synthesizeMouse"); 241 242 gBrowser.removeTab(tab); 243 }); 244 245 add_task(async function () { 246 await BrowserTestUtils.registerAboutPage( 247 registerCleanupFunction, 248 "about-pages-are-cool", 249 getRootDirectory(gTestPath) + "dummy.html", 250 0 251 ); 252 let tab = await BrowserTestUtils.openNewForegroundTab( 253 gBrowser, 254 "about:about-pages-are-cool", 255 true 256 ); 257 ok(tab, "Successfully created an about: page and loaded it."); 258 BrowserTestUtils.removeTab(tab); 259 try { 260 await BrowserTestUtils.unregisterAboutPage("about-pages-are-cool"); 261 ok(true, "Successfully unregistered the about page."); 262 } catch (ex) { 263 ok(false, "Should not throw unregistering a known about: page"); 264 } 265 await BrowserTestUtils.unregisterAboutPage("random-other-about-page").then( 266 () => { 267 ok( 268 false, 269 "Should not have succeeded unregistering an unknown about: page." 270 ); 271 }, 272 () => { 273 ok( 274 true, 275 "Should have returned a rejected promise trying to unregister an unknown about page" 276 ); 277 } 278 ); 279 }); 280 281 add_task(async function testWaitForEvent() { 282 // A promise returned by BrowserTestUtils.waitForEvent should not be resolved 283 // in the same event tick as the event listener is called. 284 let eventListenerCalled = false; 285 let waitForEventResolved = false; 286 // Use capturing phase to make sure the event listener added by 287 // BrowserTestUtils.waitForEvent is called before the normal event listener 288 // below. 289 let eventPromise = BrowserTestUtils.waitForEvent( 290 gBrowser, 291 "dummyevent", 292 true 293 ); 294 eventPromise.then(() => { 295 waitForEventResolved = true; 296 }); 297 // Add normal event listener that is called after the event listener added by 298 // BrowserTestUtils.waitForEvent. 299 gBrowser.addEventListener( 300 "dummyevent", 301 () => { 302 eventListenerCalled = true; 303 is( 304 waitForEventResolved, 305 false, 306 "BrowserTestUtils.waitForEvent promise resolution handler shouldn't be called at this point." 307 ); 308 }, 309 { once: true } 310 ); 311 312 var event = new CustomEvent("dummyevent"); 313 gBrowser.dispatchEvent(event); 314 315 await eventPromise; 316 317 is(eventListenerCalled, true, "dummyevent listener should be called"); 318 });