browser/components/sessionstore/test/browser_attributes.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 function test() {
michael@0 5 TestRunner.run();
michael@0 6 }
michael@0 7
michael@0 8 /**
michael@0 9 * This test makes sure that we correctly preserve tab attributes when storing
michael@0 10 * and restoring tabs. It also ensures that we skip special attributes like
michael@0 11 * 'image' and 'pending' that need to be handled differently or internally.
michael@0 12 */
michael@0 13
michael@0 14 const PREF = "browser.sessionstore.restore_on_demand";
michael@0 15
michael@0 16 function runTests() {
michael@0 17 Services.prefs.setBoolPref(PREF, true)
michael@0 18 registerCleanupFunction(() => Services.prefs.clearUserPref(PREF));
michael@0 19
michael@0 20 // Add a new tab with a nice icon.
michael@0 21 let tab = gBrowser.addTab("about:robots");
michael@0 22 yield whenBrowserLoaded(tab.linkedBrowser);
michael@0 23
michael@0 24 // Check that the tab has an 'image' attribute.
michael@0 25 ok(tab.hasAttribute("image"), "tab.image exists");
michael@0 26
michael@0 27 // Make sure we do not persist 'image' attributes.
michael@0 28 ss.persistTabAttribute("image");
michael@0 29 let {attributes} = JSON.parse(ss.getTabState(tab));
michael@0 30 ok(!("image" in attributes), "'image' attribute not saved");
michael@0 31 ok(!("custom" in attributes), "'custom' attribute not saved");
michael@0 32
michael@0 33 // Test persisting a custom attribute.
michael@0 34 tab.setAttribute("custom", "foobar");
michael@0 35 ss.persistTabAttribute("custom");
michael@0 36
michael@0 37 let {attributes} = JSON.parse(ss.getTabState(tab));
michael@0 38 is(attributes.custom, "foobar", "'custom' attribute is correct");
michael@0 39
michael@0 40 // Make sure we're backwards compatible and restore old 'image' attributes.
michael@0 41 let state = {
michael@0 42 entries: [{url: "about:mozilla"}],
michael@0 43 attributes: {custom: "foobaz", image: gBrowser.getIcon(tab)}
michael@0 44 };
michael@0 45
michael@0 46 // Prepare a pending tab waiting to be restored.
michael@0 47 whenTabRestoring(tab);
michael@0 48 yield ss.setTabState(tab, JSON.stringify(state));
michael@0 49
michael@0 50 ok(tab.hasAttribute("pending"), "tab is pending");
michael@0 51 is(gBrowser.getIcon(tab), state.attributes.image, "tab has correct icon");
michael@0 52
michael@0 53 // Let the pending tab load.
michael@0 54 gBrowser.selectedTab = tab;
michael@0 55 yield whenTabRestored(tab);
michael@0 56
michael@0 57 // Ensure no 'image' or 'pending' attributes are stored.
michael@0 58 let {attributes} = JSON.parse(ss.getTabState(tab));
michael@0 59 ok(!("image" in attributes), "'image' attribute not saved");
michael@0 60 ok(!("pending" in attributes), "'pending' attribute not saved");
michael@0 61 is(attributes.custom, "foobaz", "'custom' attribute is correct");
michael@0 62
michael@0 63 // Clean up.
michael@0 64 gBrowser.removeTab(tab);
michael@0 65 }
michael@0 66
michael@0 67 function whenTabRestoring(tab) {
michael@0 68 tab.addEventListener("SSTabRestoring", function onRestoring() {
michael@0 69 tab.removeEventListener("SSTabRestoring", onRestoring);
michael@0 70 executeSoon(next);
michael@0 71 });
michael@0 72 }

mercurial