browser_tabs_in_urlbar.js (4586B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /** 6 * Tests that tabs which aren't displayed yet (i.e. need to be reloaded) are 7 * still displayed in the address bar results. 8 */ 9 10 const RESTRICT_TOKEN_OPENPAGE = "%"; 11 12 const { PlacesTestUtils } = ChromeUtils.importESModule( 13 "resource://testing-common/PlacesTestUtils.sys.mjs" 14 ); 15 16 const { UrlbarProviderOpenTabs } = ChromeUtils.importESModule( 17 "moz-src:///browser/components/urlbar/UrlbarProviderOpenTabs.sys.mjs" 18 ); 19 20 const { UrlbarTestUtils } = ChromeUtils.importESModule( 21 "resource://testing-common/UrlbarTestUtils.sys.mjs" 22 ); 23 24 var stateBackup = ss.getBrowserState(); 25 26 add_setup(async function () { 27 await SpecialPowers.pushPrefEnv({ 28 set: [ 29 // Set the pref to true so we know exactly how many tabs should be restoring at 30 // any given time. This guarantees that a finishing load won't start another. 31 ["browser.sessionstore.restore_on_demand", true], 32 // Don't restore tabs lazily. 33 ["browser.sessionstore.restore_tabs_lazily", false], 34 ], 35 }); 36 37 registerCleanupFunction(() => { 38 ss.setBrowserState(stateBackup); 39 }); 40 41 info("Waiting for the Places DB to be initialized"); 42 await PlacesUtils.promiseLargeCacheDBConnection(); 43 await UrlbarProviderOpenTabs.promiseDBPopulated; 44 }); 45 46 add_task(async function test_unrestored_tabs_listed() { 47 const state = { 48 windows: [ 49 { 50 tabs: [ 51 { 52 entries: [ 53 { url: "http://example.org/#1", triggeringPrincipal_base64 }, 54 ], 55 }, 56 { 57 entries: [ 58 { url: "http://example.org/#2", triggeringPrincipal_base64 }, 59 ], 60 }, 61 { 62 entries: [ 63 { url: "http://example.org/#3", triggeringPrincipal_base64 }, 64 ], 65 }, 66 { 67 entries: [ 68 { url: "http://example.org/#4", triggeringPrincipal_base64 }, 69 ], 70 }, 71 ], 72 selected: 1, 73 }, 74 ], 75 }; 76 77 const tabsForEnsure = new Set(); 78 state.windows[0].tabs.forEach(function (tab) { 79 tabsForEnsure.add(tab.entries[0].url); 80 }); 81 82 let tabsRestoring = 0; 83 let tabsRestored = 0; 84 85 await new Promise(resolve => { 86 function handleEvent(aEvent) { 87 if (aEvent.type == "SSTabRestoring") { 88 tabsRestoring++; 89 } else { 90 tabsRestored++; 91 } 92 93 if (tabsRestoring < state.windows[0].tabs.length || tabsRestored < 1) { 94 return; 95 } 96 97 gBrowser.tabContainer.removeEventListener( 98 "SSTabRestoring", 99 handleEvent, 100 true 101 ); 102 gBrowser.tabContainer.removeEventListener( 103 "SSTabRestored", 104 handleEvent, 105 true 106 ); 107 executeSoon(resolve); 108 } 109 110 // currentURI is set before SSTabRestoring is fired, so we can sucessfully check 111 // after that has fired for all tabs. Since 1 tab will be restored though, we 112 // also need to wait for 1 SSTabRestored since currentURI will be set, unset, then set. 113 gBrowser.tabContainer.addEventListener("SSTabRestoring", handleEvent, true); 114 gBrowser.tabContainer.addEventListener("SSTabRestored", handleEvent, true); 115 ss.setBrowserState(JSON.stringify(state)); 116 }); 117 118 // Ensure any database statements started by UrlbarProviderOpenTabs are 119 // complete before continuing. 120 await PlacesTestUtils.promiseAsyncUpdates(); 121 122 // Remove the current tab from tabsForEnsure, because switch to tab doesn't 123 // suggest it. 124 tabsForEnsure.delete(gBrowser.currentURI.spec); 125 info("Searching open pages."); 126 await UrlbarTestUtils.promiseAutocompleteResultPopup({ 127 window, 128 waitForFocus, 129 value: RESTRICT_TOKEN_OPENPAGE, 130 }); 131 const total = UrlbarTestUtils.getResultCount(window); 132 info(`Found ${total} matches`); 133 134 // Check to see the expected uris and titles match up (in any order) 135 for (let i = 0; i < total; i++) { 136 const result = await UrlbarTestUtils.getDetailsOfResultAt(window, i); 137 if (result.heuristic) { 138 info("Skip heuristic match"); 139 continue; 140 } 141 const url = result.url; 142 Assert.ok( 143 tabsForEnsure.has(url), 144 `Should have the found result '${url}' in the expected list of entries` 145 ); 146 // Remove the found entry from expected results. 147 tabsForEnsure.delete(url); 148 } 149 // Make sure there is no reported open page that is not open. 150 Assert.equal(tabsForEnsure.size, 0, "Should have found all the tabs"); 151 });