Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / base / test / browser_bug1691214.js
blob5f40df2ea3b03089e9353954d90374117f7e3458
1 /* -*- Mode: JavaScript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 const BASE_URL = "http://mochi.test:8888/browser/dom/base/test/";
9 add_task(async function bug1691214() {
10   await BrowserTestUtils.withNewTab(
11     BASE_URL + "file_bug1691214.html",
12     async function (browser) {
13       let win = await newFocusedWindow(function () {
14         return BrowserTestUtils.synthesizeMouseAtCenter("#link-1", {}, browser);
15       });
16       is(Services.focus.focusedWindow, win, "New window should be focused");
18       info("re-focusing the original window");
20       {
21         let focusBack = BrowserTestUtils.waitForEvent(window, "focus", true);
22         window.focus();
23         await focusBack;
24         is(Services.focus.focusedWindow, window, "should focus back");
25       }
27       info("Clicking on the second link.");
29       {
30         let focus = BrowserTestUtils.waitForEvent(win, "focus", true);
31         await BrowserTestUtils.synthesizeMouseAtCenter("#link-2", {}, browser);
32         info("Waiting for re-focus of the opened window.");
33         await focus;
34       }
36       is(
37         Services.focus.focusedWindow,
38         win,
39         "Existing window should've been targeted and focused"
40       );
42       win.close();
43     }
44   );
45 });
47 // The tab has a form infinitely submitting to an iframe, and that shouldn't
48 // switch focus back. For that, we open a window from the tab, make sure it's
49 // focused, and then wait for three submissions (but since we need to listen to
50 // trusted events from chrome code, we use the iframe load event instead).
51 add_task(async function bug1700871() {
52   await BrowserTestUtils.withNewTab(
53     BASE_URL + "file_bug1700871.html",
54     async function (browser) {
55       let win = await newFocusedWindow(function () {
56         return BrowserTestUtils.synthesizeMouseAtCenter("#link-1", {}, browser);
57       });
59       is(Services.focus.focusedWindow, win, "New window should be focused");
61       info("waiting for three submit events and ensuring focus hasn't moved");
63       await SpecialPowers.spawn(browser, [], function () {
64         let pending = 3;
65         return new Promise(resolve => {
66           content.document
67             .querySelector("iframe")
68             .addEventListener("load", function (e) {
69               info("Got load on the frame: " + pending);
70               if (!--pending) {
71                 resolve();
72               }
73             });
74         });
75       });
77       is(Services.focus.focusedWindow, win, "Focus shouldn't have moved");
78       win.close();
79     }
80   );
81 });
83 add_task(async function bug1793829() {
84   await BrowserTestUtils.withNewTab(
85     BASE_URL + "file_bug1691214.html",
86     async function (browser) {
87       let win = await newFocusedWindow(function () {
88         return BrowserTestUtils.synthesizeMouseAtCenter("#link-1", {}, browser);
89       });
90       is(Services.focus.focusedWindow, win, "New window should be focused");
92       info("re-focusing the original window");
94       {
95         let focusBack = BrowserTestUtils.waitForEvent(window, "focus", true);
96         window.focus();
97         await focusBack;
98         is(Services.focus.focusedWindow, window, "should focus back");
99       }
101       info("Trying to steal focus.");
103       await SpecialPowers.spawn(browser, [], function () {
104         content.document.clearUserGestureActivation();
105         content.document.getElementById("link-2").click();
106       });
108       // We need to test that nothing happened, which is hard without an
109       // arbitrary timeout.
110       // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
111       await new Promise(c => setTimeout(c, 2000));
113       is(
114         Services.focus.focusedWindow,
115         window,
116         "Shouldn't steal focus without user gesture"
117       );
119       win.close();
120     }
121   );