1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/test/browser_attributes.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,72 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +function test() { 1.8 + TestRunner.run(); 1.9 +} 1.10 + 1.11 +/** 1.12 + * This test makes sure that we correctly preserve tab attributes when storing 1.13 + * and restoring tabs. It also ensures that we skip special attributes like 1.14 + * 'image' and 'pending' that need to be handled differently or internally. 1.15 + */ 1.16 + 1.17 +const PREF = "browser.sessionstore.restore_on_demand"; 1.18 + 1.19 +function runTests() { 1.20 + Services.prefs.setBoolPref(PREF, true) 1.21 + registerCleanupFunction(() => Services.prefs.clearUserPref(PREF)); 1.22 + 1.23 + // Add a new tab with a nice icon. 1.24 + let tab = gBrowser.addTab("about:robots"); 1.25 + yield whenBrowserLoaded(tab.linkedBrowser); 1.26 + 1.27 + // Check that the tab has an 'image' attribute. 1.28 + ok(tab.hasAttribute("image"), "tab.image exists"); 1.29 + 1.30 + // Make sure we do not persist 'image' attributes. 1.31 + ss.persistTabAttribute("image"); 1.32 + let {attributes} = JSON.parse(ss.getTabState(tab)); 1.33 + ok(!("image" in attributes), "'image' attribute not saved"); 1.34 + ok(!("custom" in attributes), "'custom' attribute not saved"); 1.35 + 1.36 + // Test persisting a custom attribute. 1.37 + tab.setAttribute("custom", "foobar"); 1.38 + ss.persistTabAttribute("custom"); 1.39 + 1.40 + let {attributes} = JSON.parse(ss.getTabState(tab)); 1.41 + is(attributes.custom, "foobar", "'custom' attribute is correct"); 1.42 + 1.43 + // Make sure we're backwards compatible and restore old 'image' attributes. 1.44 + let state = { 1.45 + entries: [{url: "about:mozilla"}], 1.46 + attributes: {custom: "foobaz", image: gBrowser.getIcon(tab)} 1.47 + }; 1.48 + 1.49 + // Prepare a pending tab waiting to be restored. 1.50 + whenTabRestoring(tab); 1.51 + yield ss.setTabState(tab, JSON.stringify(state)); 1.52 + 1.53 + ok(tab.hasAttribute("pending"), "tab is pending"); 1.54 + is(gBrowser.getIcon(tab), state.attributes.image, "tab has correct icon"); 1.55 + 1.56 + // Let the pending tab load. 1.57 + gBrowser.selectedTab = tab; 1.58 + yield whenTabRestored(tab); 1.59 + 1.60 + // Ensure no 'image' or 'pending' attributes are stored. 1.61 + let {attributes} = JSON.parse(ss.getTabState(tab)); 1.62 + ok(!("image" in attributes), "'image' attribute not saved"); 1.63 + ok(!("pending" in attributes), "'pending' attribute not saved"); 1.64 + is(attributes.custom, "foobaz", "'custom' attribute is correct"); 1.65 + 1.66 + // Clean up. 1.67 + gBrowser.removeTab(tab); 1.68 +} 1.69 + 1.70 +function whenTabRestoring(tab) { 1.71 + tab.addEventListener("SSTabRestoring", function onRestoring() { 1.72 + tab.removeEventListener("SSTabRestoring", onRestoring); 1.73 + executeSoon(next); 1.74 + }); 1.75 +}