browser_focus_steal_from_chrome.js (7963B)
1 add_task(async function () { 2 requestLongerTimeout(2); 3 4 let testingList = [ 5 { 6 uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><input id='target'></body>", 7 tagName: "INPUT", 8 methodName: "focus", 9 }, 10 { 11 uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').select(); }, 10);\"><input id='target'></body>", 12 tagName: "INPUT", 13 methodName: "select", 14 }, 15 { 16 uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><a href='about:blank' id='target'>anchor</a></body>", 17 tagName: "A", 18 methodName: "focus", 19 }, 20 { 21 uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><button id='target'>button</button></body>", 22 tagName: "BUTTON", 23 methodName: "focus", 24 }, 25 { 26 uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><select id='target'><option>item1</option></select></body>", 27 tagName: "SELECT", 28 methodName: "focus", 29 }, 30 { 31 uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><textarea id='target'>textarea</textarea></body>", 32 tagName: "TEXTAREA", 33 methodName: "focus", 34 }, 35 { 36 uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').select(); }, 10);\"><textarea id='target'>textarea</textarea></body>", 37 tagName: "TEXTAREA", 38 methodName: "select", 39 }, 40 { 41 uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><label id='target'><input></label></body>", 42 tagName: "INPUT", 43 methodName: "focus of label element", 44 }, 45 { 46 uri: "data:text/html,<body onload=\"setTimeout(function () { document.getElementById('target').focus(); }, 10);\"><fieldset><legend id='target'>legend</legend><input></fieldset></body>", 47 tagName: "INPUT", 48 methodName: "focus of legend element", 49 }, 50 { 51 uri: 52 'data:text/html,<body onload="setTimeout(function () {' + 53 " var element = document.getElementById('target');" + 54 " var event = document.createEvent('MouseEvent');" + 55 " event.initMouseEvent('click', true, true, window," + 56 " 1, 0, 0, 0, 0, false, false, false, false, 0, element);" + 57 ' element.dispatchEvent(event); }, 10);">' + 58 "<label id='target'><input></label></body>", 59 tagName: "INPUT", 60 methodName: "click event on the label element", 61 }, 62 ]; 63 64 await BrowserTestUtils.withNewTab("about:blank", async function (bg) { 65 await BrowserTestUtils.withNewTab("about:blank", async function (fg) { 66 for (let test of testingList) { 67 // Focus the foreground tab's content 68 fg.focus(); 69 70 // Load the URIs. 71 BrowserTestUtils.startLoadingURIString(bg, test.uri); 72 await BrowserTestUtils.browserLoaded(bg); 73 BrowserTestUtils.startLoadingURIString(fg, test.uri); 74 await BrowserTestUtils.browserLoaded(fg); 75 76 ok(true, "Test1: Both of the tabs are loaded"); 77 78 // Confirm that the contents should be able to steal focus from content. 79 await SpecialPowers.spawn(fg, [test], test => { 80 return new Promise(res => { 81 function f() { 82 let e = content.document.activeElement; 83 if (e.tagName != test.tagName) { 84 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout 85 content.setTimeout(f, 10); 86 } else { 87 is( 88 Services.focus.focusedElement, 89 e, 90 "the foreground tab's " + 91 test.tagName + 92 " element isn't focused by the " + 93 test.methodName + 94 " (Test1: content can steal focus)" 95 ); 96 res(); 97 } 98 } 99 f(); 100 }); 101 }); 102 103 await SpecialPowers.spawn(bg, [test], test => { 104 return new Promise(res => { 105 function f() { 106 let e = content.document.activeElement; 107 if (e.tagName != test.tagName) { 108 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout 109 content.setTimeout(f, 10); 110 } else { 111 isnot( 112 Services.focus.focusedElement, 113 e, 114 "the background tab's " + 115 test.tagName + 116 " element is focused by the " + 117 test.methodName + 118 " (Test1: content can steal focus)" 119 ); 120 res(); 121 } 122 } 123 f(); 124 }); 125 }); 126 127 if (fg.isRemoteBrowser) { 128 is( 129 Services.focus.focusedElement, 130 fg, 131 "Focus should be on the content in the parent process" 132 ); 133 } 134 135 // Focus chrome 136 gURLBar.focus(); 137 let originalFocus = Services.focus.focusedElement; 138 139 // Load about:blank just to make sure that everything works nicely 140 BrowserTestUtils.startLoadingURIString(bg, "about:blank"); 141 await BrowserTestUtils.browserLoaded(bg, { wantLoad: "about:blank" }); 142 BrowserTestUtils.startLoadingURIString(fg, "about:blank"); 143 await BrowserTestUtils.browserLoaded(fg, { wantLoad: "about:blank" }); 144 145 // Load the URIs. 146 BrowserTestUtils.startLoadingURIString(bg, test.uri); 147 await BrowserTestUtils.browserLoaded(bg); 148 BrowserTestUtils.startLoadingURIString(fg, test.uri); 149 await BrowserTestUtils.browserLoaded(fg); 150 151 ok(true, "Test2: Both of the tabs are loaded"); 152 153 // Confirm that the contents should be able to steal focus from content. 154 await SpecialPowers.spawn(fg, [test], test => { 155 return new Promise(res => { 156 function f() { 157 let e = content.document.activeElement; 158 if (e.tagName != test.tagName) { 159 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout 160 content.setTimeout(f, 10); 161 } else { 162 isnot( 163 Services.focus.focusedElement, 164 e, 165 "the foreground tab's " + 166 test.tagName + 167 " element is focused by the " + 168 test.methodName + 169 " (Test2: content can NOT steal focus)" 170 ); 171 res(); 172 } 173 } 174 f(); 175 }); 176 }); 177 178 await SpecialPowers.spawn(bg, [test], test => { 179 return new Promise(res => { 180 function f() { 181 let e = content.document.activeElement; 182 if (e.tagName != test.tagName) { 183 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout 184 content.setTimeout(f, 10); 185 } else { 186 isnot( 187 Services.focus.focusedElement, 188 e, 189 "the background tab's " + 190 test.tagName + 191 " element is focused by the " + 192 test.methodName + 193 " (Test2: content can NOT steal focus)" 194 ); 195 res(); 196 } 197 } 198 f(); 199 }); 200 }); 201 202 is( 203 Services.focus.focusedElement, 204 originalFocus, 205 "The parent process's focus has shifted " + 206 "(methodName = " + 207 test.methodName + 208 ")" + 209 " (Test2: content can NOT steal focus)" 210 ); 211 } 212 }); 213 }); 214 });