browser_isactiveintab.js (4780B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 function documentURL(origin, html) { 5 let params = new URLSearchParams(); 6 params.append("html", html.trim()); 7 return `${origin}/document-builder.sjs?${params.toString()}`; 8 } 9 10 add_task(async function checkIsActiveInTab() { 11 // This test creates a few tricky situations with navigation and iframes and 12 // examines the results of various ways you might think to check if a page 13 // is currently visible, and confirms that isActiveInTab works, even if the 14 // others don't. 15 16 // Make a top level page with two nested iframes. 17 const IFRAME2_URL = documentURL("https://example.com", `<h1>iframe2</h1>`); 18 const IFRAME1_URL = documentURL( 19 "https://example.com", 20 `<iframe src=${JSON.stringify(IFRAME2_URL)}></iframe><h1>iframe1</h1>` 21 ); 22 const TEST_URL = documentURL( 23 "https://example.com", 24 `<iframe src=${JSON.stringify(IFRAME1_URL)}></iframe><h1>top window</h1>` 25 ); 26 27 await BrowserTestUtils.withNewTab(TEST_URL, async browser => { 28 let topBC1 = browser.browsingContext; 29 let topWindowGlobal1 = topBC1.currentWindowGlobal; 30 31 is( 32 browser.browsingContext.children.length, 33 1, 34 "only one child for top window" 35 ); 36 let iframe1 = browser.browsingContext.children[0]; 37 let iframeWindowGlobal1a = iframe1.currentWindowGlobal; 38 39 is(iframe1.children.length, 1, "only one child for iframe"); 40 let iframe2 = iframe1.children[0]; 41 let iframeWindowGlobal2 = iframe2.currentWindowGlobal; 42 43 ok(topWindowGlobal1.isActiveInTab, "top window global is active in tab"); 44 ok(iframeWindowGlobal1a.isActiveInTab, "topmost iframe is active in tab"); 45 ok(iframeWindowGlobal2.isActiveInTab, "innermost iframe is active in tab"); 46 47 // Do a same-origin navigation on the topmost iframe. 48 let iframe1bURI = 49 "https://example.com/browser/dom/ipc/tests/file_dummy.html"; 50 let loadedIframe = BrowserTestUtils.browserLoaded( 51 browser, 52 true, 53 iframe1bURI 54 ); 55 await SpecialPowers.spawn( 56 iframe1, 57 [iframe1bURI], 58 async function (_iframe1bURI) { 59 content.location = _iframe1bURI; 60 } 61 ); 62 await loadedIframe; 63 64 ok( 65 topWindowGlobal1.isActiveInTab, 66 "top window global is still active in tab" 67 ); 68 69 let iframeWindowGlobal1b = iframe1.currentWindowGlobal; 70 isnot( 71 iframeWindowGlobal1a, 72 iframeWindowGlobal1b, 73 "navigating changed the iframe's current window" 74 ); 75 76 // This tests the !CanSend() case but unfortunately not the 77 // `bc->GetCurrentWindowGlobal() != this` case. Apparently the latter will 78 // only hold temporarily, so that is likely hard to test. 79 ok( 80 !iframeWindowGlobal1a.isActiveInTab, 81 "topmost iframe is not active in tab" 82 ); 83 84 ok(iframeWindowGlobal1b.isActiveInTab, "new iframe is active in tab"); 85 86 is( 87 iframe2.currentWindowGlobal, 88 iframeWindowGlobal2, 89 "innermost iframe current window global has not changed" 90 ); 91 92 ok( 93 iframeWindowGlobal2.isCurrentGlobal, 94 "innermost iframe is still the current global for its BC" 95 ); 96 97 // With a same-origin navigation, this hits the !bc->AncestorsAreCurrent() 98 // case. (With a cross-origin navigation, this hits the !CanSend() case.) 99 ok( 100 !iframeWindowGlobal2.isActiveInTab, 101 "innermost iframe is not active in tab" 102 ); 103 104 // Load a new page into the tab to test the behavior when a page is in 105 // the BFCache. 106 let newTopURI = "https://example.com/browser/dom/ipc/tests/file_dummy.html"; 107 let loadedTop2 = BrowserTestUtils.browserLoaded(browser, false, newTopURI); 108 await BrowserTestUtils.startLoadingURIString(browser, newTopURI); 109 await loadedTop2; 110 111 isnot(browser.browsingContext, topBC1, "Navigated to a new BC"); 112 113 is( 114 topBC1.currentWindowGlobal, 115 topWindowGlobal1, 116 "old top window is still the current window global for the old BC" 117 ); 118 ok(topWindowGlobal1.isInBFCache, "old top window's BC is in the BFCache"); 119 ok(!topWindowGlobal1.isCurrentGlobal, "old top frame isn't current"); 120 ok(!topWindowGlobal1.isActiveInTab, "old top frame not active in tab"); 121 122 is( 123 iframe1.currentWindowGlobal, 124 iframeWindowGlobal1b, 125 "old iframe is still the current window global for the BC" 126 ); 127 ok(!iframeWindowGlobal1b.isCurrentGlobal, "newer top iframe isn't current"); 128 ok( 129 iframeWindowGlobal1b.isInBFCache, 130 "old top window's BC is in the BFCache" 131 ); 132 ok(iframe1.ancestorsAreCurrent, "ancestors of iframe are current"); 133 ok( 134 !iframeWindowGlobal1b.isActiveInTab, 135 "newer top iframe is active in not active in tab after top level navigation" 136 ); 137 }); 138 });