browser_906076_lazy_tabs.js (4495B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 const TEST_STATE = { 5 windows: [ 6 { 7 tabs: [ 8 { 9 entries: [{ url: "http://example.com", triggeringPrincipal_base64 }], 10 }, 11 { 12 entries: [{ url: "http://example.com", triggeringPrincipal_base64 }], 13 }, 14 { 15 entries: [{ url: "http://example.com", triggeringPrincipal_base64 }], 16 }, 17 { 18 entries: [{ url: "http://example.com", triggeringPrincipal_base64 }], 19 }, 20 { 21 entries: [{ url: "http://example.com", triggeringPrincipal_base64 }], 22 }, 23 { 24 entries: [{ url: "http://example.com", triggeringPrincipal_base64 }], 25 }, 26 { 27 entries: [{ url: "http://example.com", triggeringPrincipal_base64 }], 28 }, 29 { 30 entries: [{ url: "http://example.com", triggeringPrincipal_base64 }], 31 }, 32 { 33 entries: [{ url: "http://example.com", triggeringPrincipal_base64 }], 34 }, 35 { 36 entries: [{ url: "http://example.com", triggeringPrincipal_base64 }], 37 }, 38 ], 39 }, 40 ], 41 }; 42 43 const TEST_STATE_2 = { 44 windows: [ 45 { 46 tabs: [ 47 { entries: [{ url: "about:robots", triggeringPrincipal_base64 }] }, 48 { 49 entries: [], 50 userTypedValue: "http://example.com", 51 userTypedClear: 1, 52 }, 53 ], 54 }, 55 ], 56 }; 57 58 function countNonLazyTabs(win) { 59 win = win || window; 60 let count = 0; 61 for (let browser of win.gBrowser.browsers) { 62 if (browser.isConnected) { 63 count++; 64 } 65 } 66 return count; 67 } 68 69 /** 70 * Test that lazy browsers do not get prematurely inserted by 71 * code accessing browser bound properties on the unbound browser. 72 */ 73 74 add_task(async function test() { 75 await SpecialPowers.pushPrefEnv({ 76 set: [ 77 ["browser.sessionstore.restore_on_demand", true], 78 ["browser.sessionstore.restore_tabs_lazily", true], 79 ], 80 }); 81 82 let backupState = SessionStore.getBrowserState(); 83 84 await promiseBrowserState(TEST_STATE); 85 86 info( 87 "Check that no lazy browsers get unnecessarily inserted after session restore" 88 ); 89 is(countNonLazyTabs(), 1, "Window has only 1 non-lazy tab"); 90 91 await TestUtils.topicObserved("sessionstore-state-write-complete"); 92 93 // When sessionstore write occurs, tabs are checked for state changes. 94 // Make sure none of them insert their browsers when this happens. 95 info("Check that no lazy browsers get inserted after sessionstore write"); 96 is(countNonLazyTabs(), 1, "Window has only 1 non-lazy tab"); 97 98 info("Check that lazy browser gets inserted properly"); 99 ok( 100 !gBrowser.browsers[1].isConnected, 101 "The browser that we're attempting to insert is indeed lazy" 102 ); 103 gBrowser._insertBrowser(gBrowser.tabs[1]); 104 is(countNonLazyTabs(), 2, "Window now has 2 non-lazy tabs"); 105 106 // Check if any lazy tabs got inserted when window closes. 107 let newWindow = await promiseNewWindowLoaded(); 108 109 SessionStore.setWindowState(newWindow, JSON.stringify(TEST_STATE)); 110 111 await new Promise(resolve => { 112 newWindow.addEventListener( 113 "unload", 114 () => { 115 info("Check that no lazy browsers get inserted when window closes"); 116 is(countNonLazyTabs(newWindow), 1, "Window has only 1 non-lazy tab"); 117 118 info( 119 "Check that it is not possible to insert a lazy browser after the window closed" 120 ); 121 ok( 122 !newWindow.gBrowser.browsers[1].isConnected, 123 "The browser that we're attempting to insert is indeed lazy" 124 ); 125 newWindow.gBrowser._insertBrowser(newWindow.gBrowser.tabs[1]); 126 is( 127 countNonLazyTabs(newWindow), 128 1, 129 "Window still has only 1 non-lazy tab" 130 ); 131 132 resolve(); 133 }, 134 { once: true } 135 ); 136 137 newWindow.close(); 138 }); 139 140 // Bug 1365933. 141 info( 142 "Check that session with tab having empty entries array gets restored properly" 143 ); 144 await promiseBrowserState(TEST_STATE_2); 145 146 is(gBrowser.tabs.length, 2, "Window has 2 tabs"); 147 is( 148 gBrowser.selectedBrowser.currentURI.spec, 149 "about:robots", 150 "Tab has the expected URL" 151 ); 152 153 gBrowser.selectedTab = gBrowser.tabs[1]; 154 await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser); 155 is( 156 gBrowser.selectedBrowser.currentURI.spec, 157 "http://example.com/", 158 "Tab has the expected URL" 159 ); 160 161 // Cleanup. 162 await promiseBrowserState(backupState); 163 });