Open Bug 1775637 Opened 4 years ago Updated 4 years ago

Focus events get fired on window multiple times during page load in a new tab

Categories

(Core :: DOM: UI Events & Focus Handling, defect)

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

  1. https://searchfox.org/mozilla-central/rev/c30349265c87047a324a471d2f39a216b6749262/dom/base/nsFocusManager.cpp#770-771
  2. https://searchfox.org/mozilla-central/rev/c30349265c87047a324a471d2f39a216b6749262/dom/base/nsFocusManager.cpp#986-987
  3. 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).

Severity: -- → S3
Summary: multiple focus events get fired on window during page load in a new tab → Focus events get fired on window multiple times during page load in a new tab
You need to log in before you can comment on or make changes to this bug.