focus.html (2839B)
1 <!DOCTYPE HTML> 2 <meta name="timeout" content="long"> 3 <link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#focused-area-of-the-document"> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src="/common/utils.js"></script> 7 <script src="/common/dispatcher/dispatcher.js"></script> 8 <script src="resources/helper.sub.js"></script> 9 <script> 10 // Focus should remain the same and thus blur/focus events shouldn't be fired 11 // when page gets into and out of BFCache, as explicitly noted in the spec: 12 // https://html.spec.whatwg.org/multipage/interaction.html#focused-area-of-the-document 13 // "Even if a document is not fully active and not shown to the user, it can still 14 // have a focused area of the document. If a document's fully active state changes, 15 // its focused area of the document will stay the same." 16 runBfcacheTest({ 17 openFunc: (url) => window.open(url + '&events=pagehide,pageshow,load', 18 '_blank', 'noopener'), 19 funcBeforeNavigation: () => { 20 // Create and focus on an <input> before navigation. 21 // Focus/blur events on the <input> are recorded. 22 const textInput = document.createElement('input'); 23 textInput.setAttribute('type', 'text'); 24 textInput.setAttribute('id', 'toBeFocused'); 25 textInput.onfocus = () => { 26 recordEvent('input.focus'); 27 }; 28 textInput.onblur = () => { 29 recordEvent('input.blur'); 30 }; 31 document.body.appendChild(textInput); 32 textInput.focus(); 33 window.activeElementBeforePageHide = document.activeElement; 34 window.addEventListener('pagehide', () => { 35 window.activeElementOnPageHide = document.activeElement; 36 }); 37 }, 38 funcAfterAssertion: async (pageA) => { 39 assert_true( 40 await pageA.execute_script(() => { 41 return window.activeElementBeforePageHide === 42 document.querySelector('#toBeFocused'); 43 }), 44 'activeElement before pagehide'); 45 46 assert_true( 47 await pageA.execute_script(() => { 48 return window.activeElementOnPageHide === 49 document.querySelector('#toBeFocused'); 50 }), 51 'activeElement on pagehide'); 52 53 assert_true( 54 await pageA.execute_script(() => { 55 return document.activeElement === 56 document.querySelector('#toBeFocused'); 57 }), 58 'activeElement after navigation'); 59 60 assert_array_equals( 61 await pageA.execute_script(() => getRecordedEvents()), 62 [ 63 'window.load', 64 'window.pageshow', 65 'input.focus', 66 'window.pagehide.persisted', 67 'window.pageshow.persisted' 68 ], 69 'blur/focus events should not be fired ' + 70 'when page gets into and out of BFCache'); 71 } 72 }, 'Focus should be kept when page gets into and out of BFCache'); 73 </script>