browser_browsing_context_active_change.js (3294B)
1 "use strict"; 2 3 // Test browsing-context-active-change notification which is fired on the parent process 4 // to know when a BrowsingContext becomes visible/hidden. 5 6 function observeTopic(aTopic) { 7 return new Promise(resolve => { 8 function observer(subject, topic) { 9 is(topic, aTopic, "observing correct topic"); 10 ok( 11 BrowsingContext.isInstance(subject), 12 "subject to be a BrowsingContext" 13 ); 14 Services.obs.removeObserver(observer, aTopic); 15 resolve(); 16 } 17 18 Services.obs.addObserver(observer, aTopic); 19 }); 20 } 21 22 function waitForActiveChange() { 23 return observeTopic("browsing-context-active-change"); 24 } 25 26 function waitForAttached() { 27 return observeTopic("browsing-context-attached"); 28 } 29 30 add_task(async function () { 31 const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser); 32 const browser = tab.linkedBrowser; 33 34 let onActiveChange = waitForActiveChange(); 35 browser.docShellIsActive = false; 36 await onActiveChange; 37 is( 38 browser.browsingContext.isActive, 39 false, 40 "The browsing context is no longer active" 41 ); 42 43 onActiveChange = waitForActiveChange(); 44 browser.docShellIsActive = true; 45 await onActiveChange; 46 is( 47 browser.browsingContext.isActive, 48 true, 49 "The browsing context is active again" 50 ); 51 52 BrowserTestUtils.removeTab(tab); 53 }); 54 55 // Tests that BrowsingContext.isActive becomes false in BF cache. 56 add_task(async function () { 57 const TEST_PATH1 = 58 getRootDirectory(gTestPath).replace( 59 "chrome://mochitests/content", 60 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 61 "http://example.com" 62 ) + "dummy_page.html"; 63 const TEST_PATH2 = 64 getRootDirectory(gTestPath).replace( 65 "chrome://mochitests/content", 66 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 67 "http://example.com" 68 ) + "dummy_iframe_page.html"; 69 70 let attached = waitForActiveChange(); 71 const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_PATH1); 72 await attached; 73 74 const firstBC = tab.linkedBrowser.browsingContext; 75 is(firstBC.isActive, true, "The first browsing context is now active"); 76 is(firstBC.currentURI.spec, TEST_PATH1); 77 78 // Load a new page now. 79 let stoppedLoadingPromise = BrowserTestUtils.browserStopped( 80 tab.linkedBrowser, 81 TEST_PATH2 82 ); 83 await loadURI(TEST_PATH2); 84 await stoppedLoadingPromise; 85 const secondBC = tab.linkedBrowser.browsingContext; 86 await TestUtils.waitForCondition(() => secondBC.isActive); 87 88 isnot(firstBC, secondBC); 89 90 is(secondBC.isActive, true, "The second browsing context is now active"); 91 is(secondBC.currentURI.spec, TEST_PATH2); 92 is(firstBC.isActive, false, "The first browsing context is no longer active"); 93 is(firstBC.currentURI.spec, TEST_PATH1); 94 95 // Now try to back to the previous page, unlike above cases we don't wait 96 // "browsing-context-active-change" since it's not notified on this history back. 97 stoppedLoadingPromise = BrowserTestUtils.browserStopped( 98 tab.linkedBrowser, 99 TEST_PATH1 100 ); 101 gBrowser.goBack(); 102 await stoppedLoadingPromise; 103 104 is( 105 secondBC.isActive, 106 false, 107 "The second browsing context is no longer active" 108 ); 109 is(firstBC.isActive, true, "The first browsing context is active again"); 110 111 BrowserTestUtils.removeTab(tab); 112 });