Closed Bug 1509380 Opened 6 years ago Closed 5 years ago

Newly opened chrome window is not activated/focused with Focusmanager test mode enabled and Firefox in the background

Categories

(Core :: DOM: Core & HTML, defect, P3)

defect

Tracking

()

RESOLVED WONTFIX

People

(Reporter: whimboo, Unassigned)

References

Details

For Marionette tests I want to get bug 1507803 fixed but stumble over the problem that the new chrome window which I open via `window.openDialog()` is not being activated nor focused. But that only when Firefox is not the top-most application.

>   function waitForEvent(target, type, args) {
>     return new Promise(resolve => {
>       let params = Object.assign({once: true}, args);
>       target.addEventListener(type, event => {
>         dump(`** Received DOM event ${event.type} for ${event.target}\n`);
>         resolve();
>       }, params);
>     });
>   }
> 
>   function waitForFocus(win) {
>     return Promise.all([
>       waitForEvent(win, "activate"),
>       waitForEvent(win, "focus", {capture: true}),
>     ]);
>   }
> 
>   // Open a window, wait for it to receive focus
>   let win = window.openDialog(url, null, "chrome,centerscreen");
>
>   await waitForFocus(win);

Running the test twice with Firefox in foreground and background I can see the following differences:

In foreground:

> [Parent 19989: Main Thread]: D/Focus Window 0x128ed0a20 Hidden [Currently: 0x119165220 0x119165220]
> [Parent 19989: Main Thread]: D/Focus   Hide Window: about:blank
> [Parent 19989: Main Thread]: D/Focus   Focused Window: chrome://browser/content/browser.xul
> [Parent 19989: Main Thread]: D/Focus   Active Window: chrome://browser/content/browser.xul
> [Parent 19989: Main Thread]: D/Focus Window 0x128ed0a20 Shown [Currently: 0x119165220 0x119165220]
> [Parent 19989: Main Thread]: D/Focus Shown Window: chrome://marionette/content/test.xul
> [Parent 19989: Main Thread]: D/Focus  Focused Window: chrome://browser/content/browser.xul
> [Parent 19989: Main Thread]: D/Focus Window 0x128ed1a20 Shown [Currently: 0x119165220 0x119165220]
> [Parent 19989: Main Thread]: D/Focus Shown Window: chrome://marionette/content/test_nested_iframe.xul
> [Parent 19989: Main Thread]: D/Focus  Focused Window: chrome://browser/content/browser.xul
> [Parent 19989: Main Thread]: D/Focus Window 0x128ed1820 Shown [Currently: 0x119165220 0x119165220]
> [Parent 19989: Main Thread]: D/Focus Shown Window: chrome://marionette/content/test2.xul
> [Parent 19989: Main Thread]: D/Focus  Focused Window: chrome://browser/content/browser.xul
> [Parent 19989: Main Thread]: D/Focus Window 0x128ed1e20 Shown [Currently: 0x119165220 0x119165220]
> [Parent 19989: Main Thread]: D/Focus Shown Window: chrome://marionette/content/test2.xul
> [Parent 19989: Main Thread]: D/Focus  Focused Window: chrome://browser/content/browser.xul
> [Parent 19989: Main Thread]: D/Focus Window 0x128ed0a20 Raised [Currently: 0x119165220 0x119165220]
> [Parent 19989: Main Thread]: D/Focus   Raised Window: 0x128ed0a20 chrome://marionette/content/test.xul
> [Parent 19989: Main Thread]: D/Focus   Active Window: 0x119165220 chrome://browser/content/browser.xul
> [Parent 19989: Main Thread]: D/Focus Window 0x119165220 Lowered [Currently: 0x119165220 0x119165220]
> [Parent 19989: Main Thread]: D/Focus   Lowered Window: chrome://browser/content/browser.xul
> [Parent 19989: Main Thread]: D/Focus   Active Window: chrome://browser/content/browser.xul
> [Parent 19989: Main Thread]: D/Focus <<Blur begin>>
> [Parent 19989: Main Thread]: D/Focus Element browser has been blurred
> [Parent 19989: Main Thread]: D/Focus Remote browser deactivated
> [Parent 19989: Main Thread]: D/Focus <<Focus begin>>
> [Parent 19989: Main Thread]: D/Focus Element (none) has been focused
> [Parent 19989: Main Thread]: D/Focus  from window
> [Parent 19989: Main Thread]: D/Focus  [Newdoc: 1 FocusChanged: 0 Raised: 1 Flags: 0]
> [Parent 19989: Main Thread]: D/Focus Update Caret: 0 1
> [Parent 19989: Main Thread]: D/Focus <<MoveFocus begin Type: 1 Flags: 0>>
> [Parent 19989: Main Thread]: D/Focus  Focused Window: 0x128ed0a20 chrome://marionette/content/test.xul

In background:

> [Parent 21236: Main Thread]: D/Focus Window 0x133eec220 Hidden [Currently: 0x124997620 0x124997620]
> [Parent 21236: Main Thread]: D/Focus   Hide Window: about:blank
> [Parent 21236: Main Thread]: D/Focus   Focused Window: chrome://browser/content/browser.xul
> [Parent 21236: Main Thread]: D/Focus   Active Window: chrome://browser/content/browser.xul
> [Parent 21236: Main Thread]: D/Focus Window 0x124997620 Lowered [Currently: 0x124997620 0x124997620]
> [Parent 21236: Main Thread]: D/Focus   Lowered Window: chrome://browser/content/browser.xul
> [Parent 21236: Main Thread]: D/Focus   Active Window: chrome://browser/content/browser.xul
> [Parent 21236: Main Thread]: D/Focus <<Blur begin>>
> [Parent 21236: Main Thread]: D/Focus Element browser has been blurred
> [Parent 21236: Main Thread]: D/Focus Remote browser deactivated
> [Parent 21236: Main Thread]: D/Focus Window 0x133eec220 Raised [Currently: 0x0 0x0]
> [Parent 21236: Main Thread]: D/Focus   Raised Window: 0x133eec220 chrome://marionette/content/test.xul
> [Parent 21236: Main Thread]: D/Focus <<Focus begin>>
> [Parent 21236: Main Thread]: D/Focus Element (none) has been focused
> [Parent 21236: Main Thread]: D/Focus  from window
> [Parent 21236: Main Thread]: D/Focus  [Newdoc: 1 FocusChanged: 0 Raised: 1 Flags: 0]
> [Parent 21236: Main Thread]: D/Focus Update Caret: 0 1
> [Parent 21236: Main Thread]: D/Focus Window 0x133eec220 Lowered [Currently: 0x133eec220 0x133eec220]
> [Parent 21236: Main Thread]: D/Focus   Lowered Window: chrome://marionette/content/test.xul
> [Parent 21236: Main Thread]: D/Focus   Active Window: chrome://marionette/content/test.xul
> [Parent 21236: Main Thread]: D/Focus <<Blur begin>>

It means that `nsFocusManager::WindowShown()` [1] is never run. If I'm not wrong it's getting called by `nsGlobalWindowInner::SetReadyForFocus()` [2]. But I cannot see where this is called from.

Neil, if you could help out that would be really appreciated. Thanks!

[1] https://searchfox.org/mozilla-central/rev/8f89901f2d69d9783f946a7458a6d7ee70635a94/dom/base/nsFocusManager.cpp#893 
[2] https://searchfox.org/mozilla-central/rev/876022232b15425bb9efde189caf747823b39567/dom/base/nsGlobalWindowInner.cpp#4619
Flags: needinfo?(enndeakin)
Please note that when I add a `win.focus` directly after opening the new window, there is a crash as covered in bug 1509234.
See Also: → 1509234
I would expect that when Firefox is in the background, any new windows opened would also open in the background, and no focus change or events would occur.
Flags: needinfo?(enndeakin)
Priority: -- → P3
(In reply to Neil Deakin from comment #2)
> I would expect that when Firefox is in the background, any new windows
> opened would also open in the background, and no focus change or events
> would occur.

But doesn't the focus manager test mode exist to simulate that Firefox is in foreground? Only with it enabled we are able to successfully run tests against different Firefox instances in parallel. And the situation as described on this bug happens all the time when a new window gets opened, and we have to check for both the focus and activate events. 

Note that so far we didn't check for those events, which might have caused tests to be flaky and prone to race-conditions. When Gijs notified me about that problem, he implemented a temporary workaround first, and I took this approach for the `New Window` command implementation in Marionette.

Also this bug is not about opening a window in background whether the Firefox instance is the top-most application or not. In such a case I would understand why no such events should be fired. But here the new window really opens on top of the most recently used one.
Flags: needinfo?(enndeakin)
Summary: Newly opened chrome window is not activated/focused wit Focusmanager test mode enabled and Firefox in the background → Newly opened chrome window is not activated/focused with Focusmanager test mode enabled and Firefox in the background
(In reply to Henrik Skupin (:whimboo) from comment #3)

> But doesn't the focus manager test mode exist to simulate that Firefox is in
> foreground?

No, it just prevents us from opening windows in front.

You'll need to intentionally focus the window when opening if you need to. On some systems, it may not be possible to do this when another application is raised.
Flags: needinfo?(enndeakin)

Ok, so at least that works for me now and the patch is going to be landed soon on bug 1504756.

But do you know why I cannot just call win.focus() but have to do it via a setTimeout with 0ms?

win.setTimeout(() => win.focus(), 0);

If I just call win.focus() it crashes with bug 1509234. Do you have an idea, or could you help with that?

Or should I better try to set the focus via Services.focus.activeWindow? But I haven't tested that yet, and it may also crash.

Flags: needinfo?(enndeakin)

That's an accessibility crash that happening when a refresh occurs. I don't really know about this, sorry.

Flags: needinfo?(enndeakin)

(In reply to Neil Deakin from comment #6)

That's an accessibility crash that happening when a refresh occurs. I don't really know about this, sorry.

Ok, I will try to find someone on the other bug to have a look at it.

(In reply to Henrik Skupin (:whimboo) from comment #5)

Or should I better try to set the focus via Services.focus.activeWindow? But I haven't tested that yet, and it may also crash.

Do you have any final hint for me what is best here? Using win.focus() or the focusmanager to set the focus of a chrome window.

Flags: needinfo?(enndeakin)

They will both do the same thing, so you should just use window.focus()

Flags: needinfo?(enndeakin)

Thanks. So I will close this bug as Wontfix then.

Status: NEW → RESOLVED
Closed: 5 years ago
Resolution: --- → WONTFIX
Component: DOM → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.