browser_set_focus_after_reuse_bcg.js (3342B)
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/. */ 6 7 const SITE_A_URL = 8 getRootDirectory(gTestPath).replace( 9 "chrome://mochitests/content", 10 "https://example.com" 11 ) + "file_set_focus_after_reuse_bcg_1.html"; 12 13 const SITE_B_URL = 14 getRootDirectory(gTestPath).replace( 15 "chrome://mochitests/content", 16 "https://example.org" 17 ) + "file_set_focus_after_reuse_bcg_2.html"; 18 19 async function test_set_focus_after_reuse_bcg() { 20 const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, SITE_A_URL); 21 22 async function clickButtonToNavigateSiteB() { 23 // Navigate to site B 24 const siteBLoaded = BrowserTestUtils.browserLoaded( 25 tab.linkedBrowser, 26 false, 27 SITE_B_URL 28 ); 29 await SpecialPowers.spawn(tab.linkedBrowser, [], async function () { 30 content.document.notifyUserGestureActivation(); 31 var button = content.document.querySelector("button"); 32 button.click(); 33 }); 34 await siteBLoaded; 35 } 36 37 async function clickButtonToEmbedIframe() { 38 // Make site B to embed the iframe that has the same origin as site A. 39 const embeddedIframeLoaded = SpecialPowers.spawn( 40 tab.linkedBrowser, 41 [], 42 async function () { 43 await new Promise(r => { 44 const iframe = content.document.querySelector("iframe"); 45 iframe.onload = r; 46 }); 47 } 48 ); 49 50 await SpecialPowers.spawn(tab.linkedBrowser, [], async function () { 51 var button = content.document.querySelector("button"); 52 button.click(); 53 }); 54 await embeddedIframeLoaded; 55 } 56 57 await clickButtonToNavigateSiteB(); 58 59 await clickButtonToEmbedIframe(); 60 61 // Navigate back to site A. 62 const pageNavigatedBackToSite1 = BrowserTestUtils.waitForContentEvent( 63 tab.linkedBrowser, 64 "pageshow" 65 ); 66 tab.linkedBrowser.goBack(); 67 await pageNavigatedBackToSite1; 68 69 // Reload site A. 70 // This reloading is quite important to reproduce this bug as it'll 71 // sync some BFCache status to the parent process for site A, which 72 // allows the BCG to reuse the already-subscribed process of site A. 73 await BrowserTestUtils.reloadTab(tab, { 74 includeSubFrames: true, 75 }); 76 77 // TODO (sefeng): If we use tab.linkedBrowser.goForward() for 78 // the navigation, it'll trigger bug 1917343. 79 await clickButtonToNavigateSiteB(); 80 81 await clickButtonToEmbedIframe(); 82 83 // Wait for the <input> to be focused within the embedded iframe. 84 const activeBCInIframeProcess = await SpecialPowers.spawn( 85 tab.linkedBrowser, 86 [], 87 async function () { 88 let iframe = content.document.querySelector("iframe"); 89 90 // Get the active browsing context of the process that the iframe belongs to 91 return SpecialPowers.spawn(iframe, [], () => { 92 const FocusManager = SpecialPowers.Services.focus; 93 return FocusManager.activeBrowsingContext; 94 }); 95 } 96 ); 97 98 Assert.ok( 99 !!activeBCInIframeProcess, 100 "activeBC should be set to the iframe process" 101 ); 102 BrowserTestUtils.removeTab(tab); 103 } 104 105 add_task(async function () { 106 await test_set_focus_after_reuse_bcg(); 107 });