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);
16 is(Services.focus.focusedWindow, win, "New window should be focused");
18 info("re-focusing the original window");
21 let focusBack = BrowserTestUtils.waitForEvent(window, "focus", true);
24 is(Services.focus.focusedWindow, window, "should focus back");
27 info("Clicking on the second link.");
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.");
37 Services.focus.focusedWindow,
39 "Existing window should've been targeted and focused"
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);
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 () {
65 return new Promise(resolve => {
67 .querySelector("iframe")
68 .addEventListener("load", function (e) {
69 info("Got load on the frame: " + pending);
77 is(Services.focus.focusedWindow, win, "Focus shouldn't have moved");
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);
90 is(Services.focus.focusedWindow, win, "New window should be focused");
92 info("re-focusing the original window");
95 let focusBack = BrowserTestUtils.waitForEvent(window, "focus", true);
98 is(Services.focus.focusedWindow, window, "should focus back");
101 info("Trying to steal focus.");
103 await SpecialPowers.spawn(browser, [], function () {
104 content.document.clearUserGestureActivation();
105 content.document.getElementById("link-2").click();
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));
114 Services.focus.focusedWindow,
116 "Shouldn't steal focus without user gesture"