document-has-system-focus.html (2038B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>HTML Test: focus - document has system focus</title> 4 <link rel="help" href="https://html.spec.whatwg.org/#has-focus-steps"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <input id="input"> 8 <script> 9 10 promise_test(async t => { 11 await new Promise(r => window.onload = r); 12 // This test requires the document to have focus as a starting condition. 13 // Whether a newly loaded page receives focus or not, seems to be somewhat 14 // browser-dependent and situation-dependent. For instance, Firefox appears to 15 // focus the page immediately if the page was loaded with the refresh button, 16 // but not if it was loaded from pressing ENTER in the URL bar. To ensure a 17 // reliable starting condition for this test, we give an extra push for focus. 18 if (!document.hasFocus()) { 19 const input = document.getElementById("input"); 20 input.focus(); 21 await new Promise(r => input.onfocus = r); 22 } 23 assert_true(document.hasFocus(), "Document has focus as starting condition."); 24 25 let gotBlur = false; 26 window.onblur = () => gotBlur = true; 27 const popup = window.open("support/popup.html", "otherwindow", "resizable"); 28 assert_not_equals(popup, null, "Test requires popup be opened"); 29 t.add_cleanup(() => popup.close()); 30 const msg = await new Promise(r => window.onmessage = ({data}) => r(data)); 31 assert_equals(msg, "focus = true", 32 "Test requires popups be focused (may require harness flags)"); 33 assert_true(gotBlur, "Document received blur event when popup opened"); 34 assert_false(document.hasFocus(), "Document lost focus when popup opened"); 35 36 const p = new Promise(r => window.onfocus = r); 37 popup.close(); 38 await p; 39 assert_true(true, "Document received focus event when popup closed"); 40 assert_true(document.hasFocus(), "Document regained focus when popup closed"); 41 }, "Top-level document receives blur/focus events and loses system focus " + 42 "during opening/closing of a popup"); 43 44 </script>