Focus events get fired on window multiple times during page load in a new tab
Categories
(Core :: DOM: UI Events & Focus Handling, defect)
Tracking
()
People
(Reporter: edgar, Unassigned)
Details
Found this while investigating bug 1775118, the test fails intermittently because sometimes the initial focus event get fired after load event. Spec doesn't expect the load event and initial focus happens in a certain order, so I tried to modified the test to have a way to ignore the initial focus event.
I tried following way first
// wait for load event
if (!window.document.hasFocus()) {
// wait for the focus event in order to ignore it.
}
// start test
However, that doesn't work, because window.document.hasFocus() always returns true even while the initial focus event doesn't yet happen after load event. That's a bit weird, I would expect the hasFocus() returns false if we don't receive the initial focus event.
So I take a closer look, and I found we actually tries to fire focus event multiple times, but since they usually happens before page starts running scripts, so they are not script-noticeable behavior.
And focus event get fired three times on
- https://searchfox.org/mozilla-central/rev/c30349265c87047a324a471d2f39a216b6749262/dom/base/nsFocusManager.cpp#770-771
- https://searchfox.org/mozilla-central/rev/c30349265c87047a324a471d2f39a216b6749262/dom/base/nsFocusManager.cpp#986-987
- and https://searchfox.org/mozilla-central/rev/c30349265c87047a324a471d2f39a216b6749262/dom/base/nsFocusManager.cpp#986-987 again
where 2) 3) always pass true for aIsNewDocument which will trigger sending focus event on window. I guess sometime 3) happens a bit late after script starts running, so script saw that focus event as a "initial" focus event, and the hasFocus() doesn't work because the focus state has already been updated in 1).
| Reporter | ||
Updated•4 years ago
|
| Reporter | ||
Comment 1•4 years ago
|
||
| Reporter | ||
Comment 2•4 years ago
|
||
Description
•