browser_searchModeSwitcher_restore.js (3202B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 /* eslint-disable mozilla/no-arbitrary-setTimeout */ 5 "use strict"; 6 requestLongerTimeout(4); 7 8 ChromeUtils.defineESModuleGetters(this, { 9 setTimeout: "resource://gre/modules/Timer.sys.mjs", 10 sinon: "resource://testing-common/Sinon.sys.mjs", 11 }); 12 13 const BOOKMARKS_ICON_URL = "chrome://browser/skin/bookmark.svg"; 14 15 add_setup(async function () { 16 await SpecialPowers.pushPrefEnv({ 17 set: [["browser.urlbar.scotchBonnet.enableOverride", true]], 18 }); 19 20 forgetClosedWindows(); 21 }); 22 23 add_task(async function () { 24 let win = await BrowserTestUtils.openNewBrowserWindow(); 25 26 await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "about:logo"); 27 await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "about:home"); 28 29 info("Set bookmarks search mode"); 30 await win.gURLBar.setSearchMode( 31 { source: 1, isPreview: false }, 32 win.gBrowser.selectedBrowser 33 ); 34 35 await BrowserTestUtils.closeWindow(win); 36 37 ok(SessionStore.getClosedWindowCount(), "Should have a closed window"); 38 await forceSaveState(); 39 40 win = SessionStore.undoCloseWindow(0); 41 42 await TestUtils.topicObserved( 43 "sessionstore-single-window-restored", 44 subject => subject == win 45 ); 46 47 let persistSandbox = sinon.createSandbox(); 48 const originalUpdateSearchIcon = 49 win.gURLBar.searchModeSwitcher.updateSearchIcon; 50 let updateCalled = 0; 51 52 persistSandbox 53 .stub(win.gURLBar.searchModeSwitcher, "updateSearchIcon") 54 .callsFake(async function () { 55 await originalUpdateSearchIcon.call(this); 56 updateCalled++; 57 }); 58 59 let defaultEngine = await Services.search.getDefault(); 60 let defaultEngineIconURL = await defaultEngine.getIconURL(); 61 62 let defaultEngineIconCallsStack = []; 63 64 persistSandbox.stub(defaultEngine, "getIconURL").callsFake(async function () { 65 const iconURL = defaultEngineIconURL; 66 defaultEngineIconURL = null; 67 defaultEngineIconCallsStack.push(true); 68 await new Promise(resolve => setTimeout(resolve, 200)); 69 defaultEngineIconCallsStack.pop(); 70 return Promise.resolve(iconURL); 71 }); 72 73 registerCleanupFunction(async function () { 74 persistSandbox.restore(); 75 }); 76 77 await TabStateFlusher.flush(win.gBrowser.selectedBrowser); 78 79 is(win.gBrowser.tabs.length, 3, "The restored window should have 3 tabs"); 80 81 // Search mode switcher icon update will trigger once. 82 await BrowserTestUtils.waitForCondition(() => updateCalled == 1); 83 84 let searchModeSwitcherButton = win.gURLBar.querySelector( 85 ".searchmode-switcher-icon" 86 ); 87 let regex = /url\("([^"]+)"\)/; 88 let searchModeSwitcherIconUrl = win 89 .getComputedStyle(searchModeSwitcherButton) 90 .listStyleImage.match(regex); 91 92 Assert.equal( 93 searchModeSwitcherIconUrl[1], 94 BOOKMARKS_ICON_URL, 95 "Search mode switcher should display bookmarks icon." 96 ); 97 98 persistSandbox.restore(); 99 100 // Wait for any ongoing calls to defaultEngine.getIconURL() to finish. 101 await BrowserTestUtils.waitForCondition( 102 () => !defaultEngineIconCallsStack.length 103 ); 104 105 await BrowserTestUtils.closeWindow(win); 106 107 // Clean up 108 await promiseAllButPrimaryWindowClosed(); 109 forgetClosedWindows(); 110 });