browser_attributes.js (3869B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 /** 5 * This test makes sure that we correctly preserve tab attributes when storing 6 * and restoring tabs. It also ensures that we skip special attributes like 7 * 'image', 'muted', and 'pending' that need to be 8 * handled differently or internally. 9 */ 10 11 const PREF = "browser.sessionstore.restore_on_demand"; 12 13 add_task(async function test() { 14 Services.prefs.setBoolPref(PREF, true); 15 registerCleanupFunction(() => Services.prefs.clearUserPref(PREF)); 16 17 // Add a new tab with a nice icon. 18 let tab = BrowserTestUtils.addTab(gBrowser, "about:robots"); 19 await promiseBrowserLoaded(tab.linkedBrowser); 20 21 // Because there is debounce logic in FaviconLoader.sys.mjs to reduce the 22 // favicon loads, we have to wait some time before checking that icon was 23 // stored properly. 24 await BrowserTestUtils.waitForCondition( 25 () => { 26 return gBrowser.getIcon(tab) != null; 27 }, 28 "wait for favicon load to finish", 29 100, 30 5 31 ); 32 33 // Check that the tab has an 'image' attribute. 34 ok(tab.hasAttribute("image"), "tab.image exists"); 35 36 tab.toggleMuteAudio(); 37 // Check that the tab has a 'muted' attribute. 38 ok(tab.hasAttribute("muted"), "tab.muted exists"); 39 40 // Make sure we do not persist 'image' and 'muted' attributes. 41 let { attributes } = JSON.parse(ss.getTabState(tab)); 42 ok(!("image" in attributes), "'image' attribute not saved"); 43 ok(!("muted" in attributes), "'muted' attribute not saved"); 44 ok(!("customizemode" in attributes), "'customizemode' attribute not saved"); 45 46 // Test persisting a customizemode attribute. 47 { 48 let customizationReady = BrowserTestUtils.waitForEvent( 49 gNavToolbox, 50 "customizationready" 51 ); 52 gCustomizeMode.enter(); 53 await customizationReady; 54 } 55 56 let customizeIcon = gBrowser.getIcon(gBrowser.selectedTab); 57 ({ attributes } = JSON.parse(ss.getTabState(gBrowser.selectedTab))); 58 ok(!("image" in attributes), "'image' attribute not saved"); 59 is(attributes.customizemode, "true", "'customizemode' attribute is correct"); 60 61 { 62 let afterCustomization = BrowserTestUtils.waitForEvent( 63 gNavToolbox, 64 "aftercustomization" 65 ); 66 gCustomizeMode.exit(); 67 await afterCustomization; 68 } 69 70 // Test restoring a customizemode tab. 71 let state = { 72 entries: [], 73 attributes: { customizemode: "true", nonpersisted: "true" }, 74 }; 75 76 // Customize mode doesn't like being restored on top of a non-blank tab. 77 // For the moment, it appears it isn't possible to restore customizemode onto 78 // an existing non-blank tab outside of tests, however this may be a latent 79 // bug if we ever try to do that in the future. 80 let principal = Services.scriptSecurityManager.createNullPrincipal({}); 81 tab.linkedBrowser.createAboutBlankDocumentViewer(principal, principal); 82 83 // Prepare a pending tab waiting to be restored. 84 let promise = promiseTabRestoring(tab); 85 ss.setTabState(tab, JSON.stringify(state)); 86 await promise; 87 88 ok(tab.hasAttribute("pending"), "tab is pending"); 89 ok(tab.hasAttribute("customizemode"), "tab is in customizemode"); 90 ok(!tab.hasAttribute("nonpersisted"), "tab has no nonpersisted attribute"); 91 is(gBrowser.getIcon(tab), customizeIcon, "tab has correct icon"); 92 ok(!state.attributes.image, "'image' attribute not saved"); 93 94 // Let the pending tab load. 95 gBrowser.selectedTab = tab; 96 97 // Ensure no 'image' or 'pending' attributes are stored. 98 ({ attributes } = JSON.parse(ss.getTabState(tab))); 99 ok(!("image" in attributes), "'image' attribute not saved"); 100 ok(!("pending" in attributes), "'pending' attribute not saved"); 101 ok(!("nonpersisted" in attributes), "'nonpersisted' attribute not saved"); 102 is(attributes.customizemode, "true", "'customizemode' attribute is correct"); 103 104 // Clean up. 105 gBrowser.removeTab(tab); 106 });