michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: function test() { michael@0: TestRunner.run(); michael@0: } michael@0: michael@0: /** michael@0: * This test makes sure that we correctly preserve tab attributes when storing michael@0: * and restoring tabs. It also ensures that we skip special attributes like michael@0: * 'image' and 'pending' that need to be handled differently or internally. michael@0: */ michael@0: michael@0: const PREF = "browser.sessionstore.restore_on_demand"; michael@0: michael@0: function runTests() { michael@0: Services.prefs.setBoolPref(PREF, true) michael@0: registerCleanupFunction(() => Services.prefs.clearUserPref(PREF)); michael@0: michael@0: // Add a new tab with a nice icon. michael@0: let tab = gBrowser.addTab("about:robots"); michael@0: yield whenBrowserLoaded(tab.linkedBrowser); michael@0: michael@0: // Check that the tab has an 'image' attribute. michael@0: ok(tab.hasAttribute("image"), "tab.image exists"); michael@0: michael@0: // Make sure we do not persist 'image' attributes. michael@0: ss.persistTabAttribute("image"); michael@0: let {attributes} = JSON.parse(ss.getTabState(tab)); michael@0: ok(!("image" in attributes), "'image' attribute not saved"); michael@0: ok(!("custom" in attributes), "'custom' attribute not saved"); michael@0: michael@0: // Test persisting a custom attribute. michael@0: tab.setAttribute("custom", "foobar"); michael@0: ss.persistTabAttribute("custom"); michael@0: michael@0: let {attributes} = JSON.parse(ss.getTabState(tab)); michael@0: is(attributes.custom, "foobar", "'custom' attribute is correct"); michael@0: michael@0: // Make sure we're backwards compatible and restore old 'image' attributes. michael@0: let state = { michael@0: entries: [{url: "about:mozilla"}], michael@0: attributes: {custom: "foobaz", image: gBrowser.getIcon(tab)} michael@0: }; michael@0: michael@0: // Prepare a pending tab waiting to be restored. michael@0: whenTabRestoring(tab); michael@0: yield ss.setTabState(tab, JSON.stringify(state)); michael@0: michael@0: ok(tab.hasAttribute("pending"), "tab is pending"); michael@0: is(gBrowser.getIcon(tab), state.attributes.image, "tab has correct icon"); michael@0: michael@0: // Let the pending tab load. michael@0: gBrowser.selectedTab = tab; michael@0: yield whenTabRestored(tab); michael@0: michael@0: // Ensure no 'image' or 'pending' attributes are stored. michael@0: let {attributes} = JSON.parse(ss.getTabState(tab)); michael@0: ok(!("image" in attributes), "'image' attribute not saved"); michael@0: ok(!("pending" in attributes), "'pending' attribute not saved"); michael@0: is(attributes.custom, "foobaz", "'custom' attribute is correct"); michael@0: michael@0: // Clean up. michael@0: gBrowser.removeTab(tab); michael@0: } michael@0: michael@0: function whenTabRestoring(tab) { michael@0: tab.addEventListener("SSTabRestoring", function onRestoring() { michael@0: tab.removeEventListener("SSTabRestoring", onRestoring); michael@0: executeSoon(next); michael@0: }); michael@0: }