Visibility API document.hasFocus() not returning correct status when changing tab via "show all tabs" button.

Originator:yeng.yang
Number:rdar://38228113 Date Originated:3/7/2018
Status:Open Resolved:
Product:Safari Product Version:11.0.3 (13604.5.6)
Classification:Other Bug Reproducible:Always
 
Summary:
When navigating/creating tabs in Safari using the "Show All Tabs" button, the visibility state of the Visibility API of the previous tab is not properly updated. Though the tab has lost focus, document.hasFocus() still returns true and document.hidden still returns false within the tab that is no longer in focus.

Steps to Reproduce:
1. Open any page in Safari. We shall call this TabA.
https://drive.google.com/open?id=1oqitiKSQZNWrBAVZYzZlVJN-LMjpC1xL

2. Open web console for the TabA and run following code to log hasFocus() status:
setInterval(function(){ console.log(Date.now() + ' hasFocus() = ' + document.hasFocus()); }, 1000);
 
3. Click on site in TabA to set focus and starting point and observe that hasFocus() is true, which is expected
https://drive.google.com/open?id=1xLvyuvLOeIPXRbKHHxSDqycpWz4ejGXR

4. Now click on the "Show All Tabs" button in top right corner to show all tabs.
5. Observe in the console log of TabA and see that hasFocus() is still true. Expecting hasFocus() to be false here as the blur event has already fired.
https://drive.google.com/open?id=1E6sJDdyRNU-VG-QsqA71oeLo-HXaxslx

6. Click the big "+" to create a new tab. This will then bring focus to the new tab.
7. Observe the console log of TabA and see that hasFocus() is still true. Expecting hasFocus() to be false as we have not fully navigated away from TabA.
https://drive.google.com/open?id=1veN9nx0Z1YwZWzZ12LTCdl1_sywwrYdg

8. In console log of TabA, execute document.hidden and observe that it is false. Expecting document.hidden to be true as the tab is no longer in view.

Expected Results:
On navigating away from a tab window when using the "Show All Tabs" button, the state of the Visibility API should be updated properly for the tab that is no longer in focus. 
- document.hasFocus() should return false
- document.hidden should return true

Actual Results:
On navigating away from a tab window when using the "Show All Tabs" button, the state of the Visibility API is not updated and returns values that state it is still in focus.
- document.hasFocus() returns true
- document.hidden returns false

Version/Build:
11.0.3 (13604.5.6)

Configuration:
none

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!