browser_bug1791083.js (2453B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 const URL = 5 "data:text/html," + 6 "<style> a:hover {background-color: black}</style>" + 7 "<body style='width:100px;height:100px'>" + 8 "<a href='http://www.example.com'>Click Me</a>" + 9 "</body>"; 10 11 function isAnchorHovered(win) { 12 return SpecialPowers.spawn( 13 win.gBrowser.selectedBrowser, 14 [], 15 async function () { 16 const a = content.document.querySelector("a"); 17 return a.matches(":hover"); 18 } 19 ); 20 } 21 22 add_task(async function test() { 23 let newWin = await BrowserTestUtils.openNewBrowserWindow(); 24 25 // This bug is only reproducible if the cursor is out of the viewport, so 26 // we resize the window to ensure the cursor is out of the viewport. 27 28 // SynthesizeMouse isn't sufficient because it only synthesizes 29 // mouse events without actually moving the cursor permanently to a 30 // new location. 31 newWin.resizeTo(50, 50); 32 33 BrowserTestUtils.startLoadingURIString(newWin.gBrowser.selectedBrowser, URL); 34 await BrowserTestUtils.browserLoaded(newWin.gBrowser.selectedBrowser); 35 36 await SpecialPowers.spawn( 37 newWin.gBrowser.selectedBrowser, 38 [], 39 async function () { 40 const a = content.document.querySelector("a"); 41 await EventUtils.synthesizeMouseAtCenter( 42 a, 43 { type: "mousemove" }, 44 content 45 ); 46 } 47 ); 48 49 // We've hovered the anchor element. 50 let anchorHovered = await isAnchorHovered(newWin); 51 ok(anchorHovered, "Anchor should be hovered"); 52 53 let locationChange = BrowserTestUtils.waitForLocationChange(newWin.gBrowser); 54 55 // Click the anchor to navigate away 56 await SpecialPowers.spawn( 57 newWin.gBrowser.selectedBrowser, 58 [], 59 async function () { 60 const a = content.document.querySelector("a"); 61 await EventUtils.synthesizeMouseAtCenter( 62 a, 63 { type: "mousedown" }, 64 content 65 ); 66 await EventUtils.synthesizeMouseAtCenter(a, { type: "mouseup" }, content); 67 } 68 ); 69 await locationChange; 70 71 // Navigate back to the previous page which has the anchor 72 locationChange = BrowserTestUtils.waitForLocationChange(newWin.gBrowser); 73 newWin.gBrowser.selectedBrowser.goBack(); 74 await locationChange; 75 76 // Hover state should be cleared upon page caching. 77 anchorHovered = await isAnchorHovered(newWin); 78 ok(!anchorHovered, "Anchor should not be hovered"); 79 80 BrowserTestUtils.closeWindow(newWin); 81 });